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