reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 8 | #include "GrTextureMaker.h" |
| 9 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "SkGr.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 11 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #include "GrDrawContext.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 16 | #include "GrYUVProvider.h" |
| 17 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 18 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 19 | #include "SkConfig8888.h" |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 20 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 21 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 22 | #include "SkErrorInternals.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 23 | #include "SkGrPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 24 | #include "SkMessageBus.h" |
| 25 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 26 | #include "SkResourceCache.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 27 | #include "SkTextureCompressor.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 28 | #include "SkYUVPlanesCache.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 29 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 30 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 31 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 32 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 33 | #include "effects/GrXfermodeFragmentProcessor.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 34 | #include "effects/GrYUVtoRGBEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 35 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 36 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 37 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 38 | # include "etc1.h" |
| 39 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
| 41 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 42 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 43 | |
| 44 | At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | we could detect that the colortable.count is <= 16, and then repack the |
| 46 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 47 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 48 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 50 | as the colortable.count says it is. |
| 51 | */ |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 52 | static void build_index8_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 53 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 55 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 57 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
| 61 | SkColorTable* ctable = bitmap.getColorTable(); |
| 62 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 63 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 64 | const int count = ctable->count(); |
| 65 | |
| 66 | SkDstPixelInfo dstPI; |
| 67 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 68 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 69 | dstPI.fPixels = buffer; |
| 70 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 71 | |
| 72 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 73 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 74 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 75 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 76 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 77 | |
| 78 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 79 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | // always skip a full 256 number of entries, even if we memcpy'd fewer |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 81 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 83 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 85 | } else { |
| 86 | // need to trim off the extra bytes per row |
| 87 | size_t width = bitmap.width(); |
| 88 | size_t rowBytes = bitmap.rowBytes(); |
| 89 | const char* src = (const char*)bitmap.getPixels(); |
| 90 | for (int y = 0; y < bitmap.height(); y++) { |
| 91 | memcpy(dst, src, width); |
| 92 | src += rowBytes; |
| 93 | dst += width; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //////////////////////////////////////////////////////////////////////////////// |
| 99 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 100 | static void get_stretch(const GrCaps& caps, int width, int height, |
| 101 | const GrTextureParams& params, SkGrStretch* stretch) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 102 | stretch->fType = SkGrStretch::kNone_Type; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 103 | bool doStretch = false; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 104 | if (params.isTiled() && !caps.npotTextureTileSupport() && |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 105 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
| 106 | doStretch = true; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 107 | stretch->fWidth = GrNextPow2(SkTMax(width, caps.minTextureSize())); |
| 108 | stretch->fHeight = GrNextPow2(SkTMax(height, caps.minTextureSize())); |
| 109 | } else if (width < caps.minTextureSize() || height < caps.minTextureSize()) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 110 | // The small texture issues appear to be with tiling. Hence it seems ok to scale them |
| 111 | // up using the GPU. If issues persist we may need to CPU-stretch. |
| 112 | doStretch = true; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 113 | stretch->fWidth = SkTMax(width, caps.minTextureSize()); |
| 114 | stretch->fHeight = SkTMax(height, caps.minTextureSize()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 115 | } |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 116 | if (doStretch) { |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 117 | switch(params.filterMode()) { |
| 118 | case GrTextureParams::kNone_FilterMode: |
| 119 | stretch->fType = SkGrStretch::kNearest_Type; |
| 120 | break; |
| 121 | case GrTextureParams::kBilerp_FilterMode: |
| 122 | case GrTextureParams::kMipMap_FilterMode: |
| 123 | stretch->fType = SkGrStretch::kBilerp_Type; |
| 124 | break; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 125 | } |
| 126 | } else { |
| 127 | stretch->fWidth = -1; |
| 128 | stretch->fHeight = -1; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 129 | stretch->fType = SkGrStretch::kNone_Type; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 130 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 131 | } |
| 132 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 133 | bool GrMakeStretchedKey(const GrUniqueKey& origKey, const SkGrStretch& stretch, |
| 134 | GrUniqueKey* stretchedKey) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 135 | if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 136 | uint32_t width = SkToU16(stretch.fWidth); |
| 137 | uint32_t height = SkToU16(stretch.fHeight); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 138 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 139 | GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2); |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 140 | builder[0] = stretch.fType; |
| 141 | builder[1] = width | (height << 16); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 142 | builder.finish(); |
| 143 | return true; |
| 144 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 145 | SkASSERT(!stretchedKey->isValid()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 149 | static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) { |
| 150 | SkASSERT(SkIsU16(subset.width())); |
| 151 | SkASSERT(SkIsU16(subset.height())); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 152 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 153 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 154 | GrUniqueKey::Builder builder(key, kDomain, 4); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 155 | builder[0] = imageID; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 156 | builder[1] = subset.x(); |
| 157 | builder[2] = subset.y(); |
| 158 | builder[3] = subset.width() | (subset.height() << 16); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 159 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 160 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 161 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset, |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 162 | const GrCaps& caps, const GrTextureParams& params) { |
| 163 | SkGrStretch stretch; |
| 164 | get_stretch(caps, subset.width(), subset.height(), params, &stretch); |
| 165 | if (SkGrStretch::kNone_Type != stretch.fType) { |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 166 | GrUniqueKey tmpKey; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 167 | make_unstretched_key(&tmpKey, imageID, subset); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 168 | if (!GrMakeStretchedKey(tmpKey, stretch, key)) { |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 169 | *key = tmpKey; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 170 | } |
| 171 | } else { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 172 | make_unstretched_key(key, imageID, subset); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
reed | 3322a81 | 2015-09-16 10:09:24 -0700 | [diff] [blame] | 176 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) { |
| 177 | GrSurfaceDesc desc; |
| 178 | desc.fFlags = kNone_GrSurfaceFlags; |
| 179 | desc.fWidth = info.width(); |
| 180 | desc.fHeight = info.height(); |
| 181 | desc.fConfig = SkImageInfo2GrPixelConfig(info); |
| 182 | desc.fSampleCnt = 0; |
| 183 | return desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 186 | namespace { |
| 187 | |
| 188 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 189 | class BitmapInvalidator : public SkPixelRef::GenIDChangeListener { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 190 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 191 | explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 192 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 193 | GrUniqueKeyInvalidatedMessage fMsg; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 194 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 195 | void onChange() override { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 196 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | |
| 200 | } // namespace |
| 201 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 202 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 203 | GrTexture* GrCreateTextureForPixels(GrContext* ctx, |
| 204 | const GrUniqueKey& optionalKey, |
| 205 | GrSurfaceDesc desc, |
| 206 | SkPixelRef* pixelRefForInvalidationNotification, |
| 207 | const void* pixels, |
| 208 | size_t rowBytes) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 209 | GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes); |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 210 | if (result && optionalKey.isValid()) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 211 | if (pixelRefForInvalidationNotification) { |
| 212 | BitmapInvalidator* listener = new BitmapInvalidator(optionalKey); |
| 213 | pixelRefForInvalidationNotification->addGenIDChangeListener(listener); |
| 214 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 215 | ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result); |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 216 | } |
| 217 | return result; |
| 218 | } |
| 219 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 220 | // creates a new texture that is the input texture scaled up. If optionalKey is valid it will be |
| 221 | // set on the new texture. stretch controls whether the scaling is done using nearest or bilerp |
| 222 | // filtering and the size to stretch the texture to. |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 223 | GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch, |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 224 | SkPixelRef* pixelRef, |
| 225 | const GrUniqueKey& optionalKey) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 226 | SkASSERT(SkGrStretch::kNone_Type != stretch.fType); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 227 | |
| 228 | GrContext* context = inputTexture->getContext(); |
| 229 | SkASSERT(context); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 230 | const GrCaps* caps = context->caps(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 231 | |
| 232 | // Either it's a cache miss or the original wasn't cached to begin with. |
| 233 | GrSurfaceDesc rtDesc = inputTexture->desc(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 234 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 235 | rtDesc.fWidth = stretch.fWidth; |
| 236 | rtDesc.fHeight = stretch.fHeight; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 237 | rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig); |
| 238 | |
| 239 | // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise, |
| 240 | // fail. |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 241 | if (!caps->isConfigRenderable(rtDesc.fConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 242 | if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 243 | if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 244 | rtDesc.fConfig = kAlpha_8_GrPixelConfig; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 245 | } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 246 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 247 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 249 | } |
| 250 | } else if (kRGB_GrColorComponentFlags == |
| 251 | (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 252 | if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 253 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 254 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 255 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 256 | } |
| 257 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 262 | SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc, |
| 263 | pixelRef, nullptr,0)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 264 | if (!stretched) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 265 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 266 | } |
| 267 | GrPaint paint; |
| 268 | |
| 269 | // If filtering is not desired then we want to ensure all texels in the resampled image are |
| 270 | // copies of texels from the original. |
| 271 | GrTextureParams params(SkShader::kClamp_TileMode, |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 272 | SkGrStretch::kBilerp_Type == stretch.fType ? |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 273 | GrTextureParams::kBilerp_FilterMode : |
| 274 | GrTextureParams::kNone_FilterMode); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 275 | paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params); |
| 276 | |
| 277 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight)); |
| 278 | SkRect localRect = SkRect::MakeWH(1.f, 1.f); |
| 279 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 280 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(stretched->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 281 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 282 | return nullptr; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 283 | } |
| 284 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 285 | drawContext->drawNonAARectToRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, localRect); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 286 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 287 | return stretched.detach(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 288 | } |
| 289 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 290 | GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
| 291 | int expectedW, int expectedH, |
| 292 | const void** outStartOfDataToUpload) { |
| 293 | *outStartOfDataToUpload = nullptr; |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 294 | #ifndef SK_IGNORE_ETC1_SUPPORT |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 295 | if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { |
| 296 | return kUnknown_GrPixelConfig; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 297 | } |
| 298 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 299 | const uint8_t* bytes = data->bytes(); |
| 300 | if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 301 | // Does the data match the dimensions of the bitmap? If not, |
| 302 | // then we don't know how to scale the image to match it... |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 303 | if (etc1_pkm_get_width(bytes) != (unsigned)expectedW || |
| 304 | etc1_pkm_get_height(bytes) != (unsigned)expectedH) |
| 305 | { |
| 306 | return kUnknown_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 307 | } |
| 308 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 309 | *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE; |
| 310 | return kETC1_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 311 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 312 | SkKTXFile ktx(data); |
| 313 | |
| 314 | // Is it actually an ETC1 texture? |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 315 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 316 | return kUnknown_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Does the data match the dimensions of the bitmap? If not, |
| 320 | // then we don't know how to scale the image to match it... |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 321 | if (ktx.width() != expectedW || ktx.height() != expectedH) { |
| 322 | return kUnknown_GrPixelConfig; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 323 | } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 324 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 325 | *outStartOfDataToUpload = ktx.pixelData(); |
| 326 | return kETC1_GrPixelConfig; |
| 327 | } |
| 328 | #endif |
| 329 | return kUnknown_GrPixelConfig; |
| 330 | } |
| 331 | |
| 332 | static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
| 333 | const SkBitmap &bm, GrSurfaceDesc desc) { |
| 334 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
| 335 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 336 | return nullptr; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 337 | } |
| 338 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 339 | const void* startOfTexData; |
| 340 | desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(), |
| 341 | &startOfTexData); |
| 342 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 343 | return nullptr; |
| 344 | } |
| 345 | |
| 346 | return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 347 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will |
| 351 | * be gone from SkPixelRef, and we can remove this subclass entirely. |
| 352 | */ |
| 353 | class PixelRef_GrYUVProvider : public GrYUVProvider { |
| 354 | SkPixelRef* fPR; |
| 355 | |
| 356 | public: |
| 357 | PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {} |
| 358 | |
| 359 | uint32_t onGetID() override { return fPR->getGenerationID(); } |
| 360 | bool onGetYUVSizes(SkISize sizes[3]) override { |
| 361 | return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr); |
| 362 | } |
| 363 | bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
| 364 | SkYUVColorSpace* space) override { |
| 365 | return fPR->getYUV8Planes(sizes, planes, rowBytes, space); |
| 366 | } |
| 367 | }; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 368 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 369 | static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 370 | const SkBitmap& bm, const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 371 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 372 | SkPixelRef* pixelRef = bm.pixelRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 373 | if ((nullptr == pixelRef) || |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 374 | (pixelRef->info().width() != bm.info().width()) || |
| 375 | (pixelRef->info().height() != bm.info().height())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 376 | return nullptr; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 377 | } |
| 378 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 379 | const bool useCache = optionalKey.isValid(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 380 | PixelRef_GrYUVProvider provider(pixelRef); |
| 381 | GrTexture* texture = provider.refAsTexture(ctx, desc, useCache); |
| 382 | if (!texture) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 386 | if (useCache) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 387 | BitmapInvalidator* listener = new BitmapInvalidator(optionalKey); |
| 388 | pixelRef->addGenIDChangeListener(listener); |
| 389 | ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture); |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 390 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 391 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 392 | } |
| 393 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 394 | static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx, |
| 395 | const SkBitmap& origBitmap, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 396 | const GrUniqueKey& optionalKey) { |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 397 | if (origBitmap.width() < ctx->caps()->minTextureSize() || |
| 398 | origBitmap.height() < ctx->caps()->minTextureSize()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 399 | return nullptr; |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 400 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 401 | SkBitmap tmpBitmap; |
| 402 | |
| 403 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 404 | |
reed | 3322a81 | 2015-09-16 10:09:24 -0700 | [diff] [blame] | 405 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info()); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 406 | const GrCaps* caps = ctx->caps(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 407 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 408 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 409 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 410 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 411 | bitmap->width(), bitmap->height()); |
| 412 | SkAutoMalloc storage(imageSize); |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 413 | build_index8_data(storage.get(), origBitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | |
| 415 | // our compressed data will be trimmed, so pass width() for its |
| 416 | // "rowBytes", since they are the same now. |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 417 | return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 418 | storage.get(), bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 419 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 420 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | // now bitmap points to our temp, which has been promoted to 32bits |
| 422 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 423 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 424 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 425 | } else if (!bitmap->readyToDraw()) { |
| 426 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 427 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 428 | // the bitmap has available pixels, then they might not be what the decompressed |
| 429 | // data is. |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 430 | GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 431 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 432 | return texture; |
| 433 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 434 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 435 | |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 436 | GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc); |
| 437 | if (texture) { |
| 438 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 439 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 440 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 441 | SkAutoLockPixels alp(*bitmap); |
| 442 | if (!bitmap->readyToDraw()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 443 | return nullptr; |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 444 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 445 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 446 | return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 447 | bitmap->getPixels(), bitmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 448 | } |
| 449 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 450 | static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) { |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 451 | SkBitmap stretched; |
| 452 | stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight); |
| 453 | SkCanvas canvas(stretched); |
| 454 | SkPaint paint; |
| 455 | switch (stretch.fType) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 456 | case SkGrStretch::kNearest_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 457 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 458 | break; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 459 | case SkGrStretch::kBilerp_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 460 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 461 | break; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 462 | case SkGrStretch::kNone_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 463 | SkDEBUGFAIL("Shouldn't get here."); |
| 464 | break; |
| 465 | } |
| 466 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight)); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 467 | canvas.drawBitmapRect(bmp, dstRect, &paint); |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 468 | return stretched; |
| 469 | } |
| 470 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 471 | class Bitmap_GrTextureMaker : public GrTextureMaker { |
| 472 | public: |
| 473 | Bitmap_GrTextureMaker(const SkBitmap& bitmap) |
| 474 | : INHERITED(bitmap.width(), bitmap.height()) |
| 475 | , fBitmap(bitmap) |
| 476 | {} |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 477 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 478 | protected: |
| 479 | GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { |
| 480 | GrTexture* tex = fBitmap.getTexture(); |
| 481 | if (tex) { |
| 482 | return SkRef(tex); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 483 | } |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 484 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 485 | GrUniqueKey unstretchedKey; |
| 486 | make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 487 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 488 | GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 489 | if (result) { |
| 490 | return result; |
| 491 | } |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 492 | return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 493 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 494 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 495 | bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override { |
| 496 | if (fBitmap.isVolatile()) { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | GrUniqueKey unstretchedKey; |
| 501 | make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 502 | return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 503 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 504 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 505 | void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { |
| 506 | fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey)); |
| 507 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 508 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 509 | bool onGetROBitmap(SkBitmap* bitmap) override { |
| 510 | *bitmap = fBitmap; |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | const SkBitmap fBitmap; |
| 516 | |
| 517 | typedef GrTextureMaker INHERITED; |
| 518 | }; |
| 519 | |
| 520 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 521 | const GrTextureParams& params) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 522 | return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 523 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 524 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 525 | /////////////////////////////////////////////////////////////////////////////// |
| 526 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 527 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 528 | // alpha info, that will be considered. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 529 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 530 | switch (ct) { |
| 531 | case kUnknown_SkColorType: |
| 532 | return kUnknown_GrPixelConfig; |
| 533 | case kAlpha_8_SkColorType: |
| 534 | return kAlpha_8_GrPixelConfig; |
| 535 | case kRGB_565_SkColorType: |
| 536 | return kRGB_565_GrPixelConfig; |
| 537 | case kARGB_4444_SkColorType: |
| 538 | return kRGBA_4444_GrPixelConfig; |
| 539 | case kRGBA_8888_SkColorType: |
jvanverth | 99babf2 | 2015-05-22 06:06:40 -0700 | [diff] [blame] | 540 | //if (kSRGB_SkColorProfileType == pt) { |
| 541 | // return kSRGBA_8888_GrPixelConfig; |
| 542 | //} |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 543 | return kRGBA_8888_GrPixelConfig; |
| 544 | case kBGRA_8888_SkColorType: |
| 545 | return kBGRA_8888_GrPixelConfig; |
| 546 | case kIndex_8_SkColorType: |
| 547 | return kIndex_8_GrPixelConfig; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 548 | case kGray_8_SkColorType: |
| 549 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 550 | } |
| 551 | SkASSERT(0); // shouldn't get here |
| 552 | return kUnknown_GrPixelConfig; |
| 553 | } |
| 554 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 555 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 556 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 557 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 558 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 559 | switch (config) { |
| 560 | case kAlpha_8_GrPixelConfig: |
| 561 | ct = kAlpha_8_SkColorType; |
| 562 | break; |
| 563 | case kIndex_8_GrPixelConfig: |
| 564 | ct = kIndex_8_SkColorType; |
| 565 | break; |
| 566 | case kRGB_565_GrPixelConfig: |
| 567 | ct = kRGB_565_SkColorType; |
| 568 | break; |
| 569 | case kRGBA_4444_GrPixelConfig: |
| 570 | ct = kARGB_4444_SkColorType; |
| 571 | break; |
| 572 | case kRGBA_8888_GrPixelConfig: |
| 573 | ct = kRGBA_8888_SkColorType; |
| 574 | break; |
| 575 | case kBGRA_8888_GrPixelConfig: |
| 576 | ct = kBGRA_8888_SkColorType; |
| 577 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 578 | case kSRGBA_8888_GrPixelConfig: |
| 579 | ct = kRGBA_8888_SkColorType; |
| 580 | pt = kSRGB_SkColorProfileType; |
| 581 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 582 | default: |
| 583 | return false; |
| 584 | } |
| 585 | if (ctOut) { |
| 586 | *ctOut = ct; |
| 587 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 588 | if (ptOut) { |
| 589 | *ptOut = pt; |
| 590 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 591 | return true; |
| 592 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 593 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 594 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 595 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 596 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 597 | static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) { |
| 598 | if (primitiveIsSrc) { |
| 599 | return SkXfermode::kSrc_Mode != mode; |
| 600 | } else { |
| 601 | return SkXfermode::kDst_Mode != mode; |
| 602 | } |
| 603 | } |
| 604 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 605 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
| 606 | const SkPaint& skPaint, |
| 607 | const SkMatrix& viewM, |
| 608 | const GrFragmentProcessor** shaderProcessor, |
| 609 | SkXfermode::Mode* primColorMode, |
| 610 | bool primitiveIsSrc, |
| 611 | GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 612 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 613 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 614 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 615 | // of per-vertex colors. |
| 616 | SkAutoTUnref<const GrFragmentProcessor> aufp; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 617 | const GrFragmentProcessor* shaderFP = nullptr; |
| 618 | if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { |
| 619 | if (shaderProcessor) { |
| 620 | shaderFP = *shaderProcessor; |
| 621 | } else if (const SkShader* shader = skPaint.getShader()) { |
| 622 | aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 623 | skPaint.getFilterQuality())); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 624 | shaderFP = aufp; |
| 625 | if (!shaderFP) { |
| 626 | return false; |
| 627 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 632 | // a known constant value. In that case we can simply apply a color filter during this |
| 633 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 634 | bool applyColorFilterToPaintColor = false; |
| 635 | if (shaderFP) { |
| 636 | if (primColorMode) { |
| 637 | // There is a blend between the primitive color and the shader color. The shader sees |
| 638 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 639 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 640 | |
| 641 | // The geometry processor will insert the primitive color to start the color chain, so |
| 642 | // the GrPaint color will be ignored. |
| 643 | |
| 644 | GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor()); |
| 645 | |
| 646 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
| 647 | aufp.reset(shaderFP); |
| 648 | |
| 649 | if (primitiveIsSrc) { |
| 650 | shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP, |
| 651 | *primColorMode); |
| 652 | } else { |
| 653 | shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP, |
| 654 | *primColorMode); |
| 655 | } |
| 656 | aufp.reset(shaderFP); |
| 657 | // The above may return null if compose results in a pass through of the prim color. |
| 658 | if (shaderFP) { |
| 659 | grPaint->addColorFragmentProcessor(shaderFP); |
| 660 | } |
| 661 | |
| 662 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 663 | if (GrColor_WHITE != paintAlpha) { |
| 664 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 665 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 666 | } |
| 667 | } else { |
| 668 | // The shader's FP sees the paint unpremul color |
| 669 | grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor())); |
| 670 | grPaint->addColorFragmentProcessor(shaderFP); |
| 671 | } |
| 672 | } else { |
| 673 | if (primColorMode) { |
| 674 | // There is a blend between the primitive color and the paint color. The blend considers |
| 675 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
| 676 | SkAutoTUnref<const GrFragmentProcessor> processor( |
| 677 | GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()), |
| 678 | GrConstColorProcessor::kIgnore_InputMode)); |
| 679 | if (primitiveIsSrc) { |
| 680 | processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor, |
| 681 | *primColorMode)); |
| 682 | } else { |
| 683 | processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor, |
| 684 | *primColorMode)); |
| 685 | |
| 686 | } |
| 687 | if (processor) { |
| 688 | grPaint->addColorFragmentProcessor(processor); |
| 689 | } |
| 690 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 691 | grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor())); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 692 | |
| 693 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 694 | if (GrColor_WHITE != paintAlpha) { |
| 695 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 696 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 697 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 698 | } else { |
| 699 | // No shader, no primitive color. |
| 700 | grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor())); |
| 701 | applyColorFilterToPaintColor = true; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 706 | if (colorFilter) { |
| 707 | if (applyColorFilterToPaintColor) { |
| 708 | grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor()))); |
| 709 | } else { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 710 | SkAutoTUnref<const GrFragmentProcessor> cfFP( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 711 | colorFilter->asFragmentProcessor(context)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 712 | if (cfFP) { |
| 713 | grPaint->addColorFragmentProcessor(cfFP); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 714 | } else { |
| 715 | return false; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 720 | SkXfermode* mode = skPaint.getXfermode(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 721 | GrXPFactory* xpFactory = nullptr; |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame] | 722 | if (!SkXfermode::AsXPFactory(mode, &xpFactory)) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 723 | // Fall back to src-over |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 724 | // return false here? |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 725 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 726 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 727 | SkASSERT(xpFactory); |
| 728 | grPaint->setXPFactory(xpFactory)->unref(); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 729 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 730 | #ifndef SK_IGNORE_GPU_DITHER |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 731 | if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) { |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 732 | grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 733 | } |
| 734 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 735 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 736 | } |
| 737 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 738 | bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM, |
| 739 | GrPaint* grPaint) { |
| 740 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint); |
| 741 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 742 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 743 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 744 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
| 745 | const SkPaint& skPaint, |
| 746 | const GrFragmentProcessor* shaderFP, |
| 747 | GrPaint* grPaint) { |
| 748 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 749 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 750 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 751 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false, |
| 752 | grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 753 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 754 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 755 | /** Ignores the SkShader (if any) on skPaint. */ |
| 756 | bool SkPaintToGrPaintNoShader(GrContext* context, |
| 757 | const SkPaint& skPaint, |
| 758 | GrPaint* grPaint) { |
| 759 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
| 760 | static const GrFragmentProcessor* kNullShaderFP = nullptr; |
| 761 | static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP; |
| 762 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false, |
| 763 | grPaint); |
| 764 | } |
| 765 | |
| 766 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
| 767 | be setup as a vertex attribute using the specified SkXfermode::Mode. */ |
| 768 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
| 769 | const SkPaint& skPaint, |
| 770 | const SkMatrix& viewM, |
| 771 | SkXfermode::Mode primColorMode, |
| 772 | bool primitiveIsSrc, |
| 773 | GrPaint* grPaint) { |
| 774 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc, |
| 775 | grPaint); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 780 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 781 | SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { |
| 782 | #ifdef SK_DEBUG |
| 783 | const GrSurfaceDesc& desc = tex->desc(); |
| 784 | SkASSERT(w <= desc.fWidth); |
| 785 | SkASSERT(h <= desc.fHeight); |
| 786 | #endif |
| 787 | const GrPixelConfig config = tex->config(); |
| 788 | SkColorType ct; |
| 789 | SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 790 | if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 791 | ct = kUnknown_SkColorType; |
| 792 | } |
| 793 | return SkImageInfo::Make(w, h, ct, at); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) { |
| 798 | const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque); |
| 799 | dst->setInfo(info); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 800 | dst->setPixelRef(new SkGrPixelRef(info, src))->unref(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 801 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 802 | |
| 803 | GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 804 | const SkMatrix& viewM, |
| 805 | const SkMatrix& localM, |
| 806 | bool* doBicubic) { |
| 807 | *doBicubic = false; |
| 808 | GrTextureParams::FilterMode textureFilterMode; |
| 809 | switch (paintFilterQuality) { |
| 810 | case kNone_SkFilterQuality: |
| 811 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 812 | break; |
| 813 | case kLow_SkFilterQuality: |
| 814 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 815 | break; |
| 816 | case kMedium_SkFilterQuality: { |
| 817 | SkMatrix matrix; |
| 818 | matrix.setConcat(viewM, localM); |
| 819 | if (matrix.getMinScale() < SK_Scalar1) { |
| 820 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 821 | } else { |
| 822 | // Don't trigger MIP level generation unnecessarily. |
| 823 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 824 | } |
| 825 | break; |
| 826 | } |
| 827 | case kHigh_SkFilterQuality: { |
| 828 | SkMatrix matrix; |
| 829 | matrix.setConcat(viewM, localM); |
| 830 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 831 | break; |
| 832 | } |
| 833 | default: |
| 834 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 835 | "Sorry, I don't understand the filtering " |
| 836 | "mode you asked for. Falling back to " |
| 837 | "MIPMaps."); |
| 838 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 839 | break; |
| 840 | |
| 841 | } |
| 842 | return textureFilterMode; |
| 843 | } |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 844 | |
| 845 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 846 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 847 | GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams& params) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 848 | SkGrStretch stretch; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 849 | get_stretch(*ctx->caps(), this->width(), this->height(), params, &stretch); |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 850 | |
| 851 | if (SkGrStretch::kNone_Type == stretch.fType) { |
| 852 | return this->onRefUnstretchedTexture(ctx); |
| 853 | } |
| 854 | |
| 855 | GrUniqueKey stretchedKey; |
| 856 | if (this->onMakeStretchedKey(stretch, &stretchedKey)) { |
| 857 | GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey); |
| 858 | if (result) { |
| 859 | return result; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch); |
| 864 | if (!result) { |
| 865 | return nullptr; |
| 866 | } |
| 867 | |
| 868 | if (stretchedKey.isValid()) { |
| 869 | ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result); |
| 870 | this->onNotifyStretchCached(stretchedKey); |
| 871 | } |
| 872 | return result; |
| 873 | } |
| 874 | |
| 875 | GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) { |
| 876 | if (this->width() < ctx->caps()->minTextureSize() || |
| 877 | this->height() < ctx->caps()->minTextureSize()) |
| 878 | { |
| 879 | // we can't trust our ability to use HW to perform the stretch, so we request |
| 880 | // a raster instead, and perform the stretch on the CPU. |
| 881 | SkBitmap bitmap; |
| 882 | if (!this->onGetROBitmap(&bitmap)) { |
| 883 | return nullptr; |
| 884 | } |
| 885 | SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch); |
| 886 | return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey()); |
| 887 | } else { |
| 888 | SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx)); |
| 889 | if (!unstretched) { |
| 890 | return nullptr; |
| 891 | } |
| 892 | return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey()); |
| 893 | } |
| 894 | } |