aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrTextureParamsAdjuster.cpp
blob: adeec45c8ac5d5c91b98ee3c0ca728a4b95722cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrTextureParamsAdjuster.h"

#include "GrCaps.h"
#include "GrContext.h"
#include "GrDrawContext.h"
#include "GrGpu.h"
#include "GrTexture.h"
#include "GrTextureParams.h"
#include "GrTextureProvider.h"
#include "SkCanvas.h"
#include "SkGr.h"
#include "SkGrPriv.h"

typedef GrTextureParamsAdjuster::CopyParams CopyParams;

static GrTexture* copy_on_gpu(GrTexture* inputTexture, const CopyParams& copyParams) {
    GrContext* context = inputTexture->getContext();
    SkASSERT(context);
    const GrCaps* caps = context->caps();

    // Either it's a cache miss or the original wasn't cached to begin with.
    GrSurfaceDesc rtDesc = inputTexture->desc();
    rtDesc.fFlags =  rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
    rtDesc.fWidth  = copyParams.fWidth;
    rtDesc.fHeight = copyParams.fHeight;
    rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);

    // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
    // fail.
    if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
        if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
            if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
                rtDesc.fConfig = kAlpha_8_GrPixelConfig;
            } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
                rtDesc.fConfig = kSkia8888_GrPixelConfig;
            } else {
                return nullptr;
            }
        } else if (kRGB_GrColorComponentFlags ==
                   (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
            if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
                rtDesc.fConfig = kSkia8888_GrPixelConfig;
            } else {
                return nullptr;
            }
        } else {
            return nullptr;
        }
    }

    SkAutoTUnref<GrTexture> copy(context->textureProvider()->createTexture(rtDesc, true));
    if (!copy) {
        return nullptr;
    }

    GrPaint paint;

    // If filtering is not desired then we want to ensure all texels in the resampled image are
    // copies of texels from the original.
    GrTextureParams params(SkShader::kClamp_TileMode, copyParams.fFilter);
    paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);

    SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
    SkRect localRect = SkRect::MakeWH(1.f, 1.f);

    SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(copy->asRenderTarget()));
    if (!drawContext) {
        return nullptr;
    }

    drawContext->drawNonAARectToRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, localRect);
    return copy.detach();
}

static SkBitmap copy_on_cpu(const SkBitmap& bmp, const CopyParams& copyParams) {
    SkBitmap stretched;
    stretched.allocN32Pixels(copyParams.fWidth, copyParams.fHeight);
    SkCanvas canvas(stretched);
    SkPaint paint;
    switch (copyParams.fFilter) {
        case GrTextureParams::kNone_FilterMode:
            paint.setFilterQuality(kNone_SkFilterQuality);
            break;
        case GrTextureParams::kBilerp_FilterMode:
            paint.setFilterQuality(kLow_SkFilterQuality);
            break;
        case GrTextureParams::kMipMap_FilterMode:
            paint.setFilterQuality(kMedium_SkFilterQuality);
            break;
    }
    SkRect dstRect = SkRect::MakeWH(SkIntToScalar(copyParams.fWidth),
                                    SkIntToScalar(copyParams.fHeight));
    canvas.drawBitmapRect(bmp, dstRect, &paint);
    return stretched;
}

GrTexture* GrTextureParamsAdjuster::refTextureForParams(GrContext* ctx,
                                                        const GrTextureParams& params) {
    CopyParams copyParams;
    if (!ctx->getGpu()->makeCopyForTextureParams(this->width(), this->height(), params,
                                                 &copyParams)) {
        return this->refOriginalTexture(ctx);
    }
    GrUniqueKey copyKey;
    this->makeCopyKey(copyParams, &copyKey);
    if (copyKey.isValid()) {
        GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(copyKey);
        if (result) {
            return result;
        }
    }

    GrTexture* result = this->generateTextureForParams(ctx, copyParams);
    if (!result) {
        return nullptr;
    }

    if (copyKey.isValid()) {
        ctx->textureProvider()->assignUniqueKeyToTexture(copyKey, result);
        this->didCacheCopy(copyKey);
    }
    return result;
}

GrTexture* GrTextureParamsAdjuster::generateTextureForParams(GrContext* ctx,
                                                             const CopyParams& copyParams) {
    if (this->width() < ctx->caps()->minTextureSize() ||
        this->height() < ctx->caps()->minTextureSize())
    {
        // we can't trust our ability to use HW to perform the stretch, so we request
        // a raster instead, and perform the stretch on the CPU.
        SkBitmap bitmap;
        if (!this->getROBitmap(&bitmap)) {
            return nullptr;
        }
        SkBitmap stretchedBmp = copy_on_cpu(bitmap, copyParams);
        return GrUploadBitmapToTexture(ctx, bitmap);
    } else {
        SkAutoTUnref<GrTexture> original(this->refOriginalTexture(ctx));
        if (!original) {
            return nullptr;
        }
        return copy_on_gpu(original, copyParams);
    }
}