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