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