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