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 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 10 | #include "GrXferProcessor.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 12 | #include "SkConfig8888.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 14 | #include "SkErrorInternals.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 15 | #include "SkMessageBus.h" |
| 16 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 17 | #include "SkResourceCache.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 18 | #include "SkTextureCompressor.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 19 | #include "SkYUVPlanesCache.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 20 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 21 | #include "effects/GrPorterDuffXferProcessor.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 22 | #include "effects/GrYUVtoRGBEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 23 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 24 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 25 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 26 | # include "etc1.h" |
| 27 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | |
| 29 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 30 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 31 | |
| 32 | 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] | 33 | we could detect that the colortable.count is <= 16, and then repack the |
| 34 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 35 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 36 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 38 | as the colortable.count says it is. |
| 39 | */ |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 40 | static void build_index8_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 41 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 43 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 45 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
| 49 | SkColorTable* ctable = bitmap.getColorTable(); |
| 50 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 51 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 52 | const int count = ctable->count(); |
| 53 | |
| 54 | SkDstPixelInfo dstPI; |
| 55 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 56 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 57 | dstPI.fPixels = buffer; |
| 58 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 59 | |
| 60 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 61 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 62 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 63 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 64 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 65 | |
| 66 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 67 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | // 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] | 69 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 71 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 73 | } else { |
| 74 | // need to trim off the extra bytes per row |
| 75 | size_t width = bitmap.width(); |
| 76 | size_t rowBytes = bitmap.rowBytes(); |
| 77 | const char* src = (const char*)bitmap.getPixels(); |
| 78 | for (int y = 0; y < bitmap.height(); y++) { |
| 79 | memcpy(dst, src, width); |
| 80 | src += rowBytes; |
| 81 | dst += width; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
| 87 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 88 | enum Stretch { |
| 89 | kNo_Stretch, |
| 90 | kBilerp_Stretch, |
| 91 | kNearest_Stretch |
| 92 | }; |
| 93 | |
| 94 | static Stretch get_stretch_type(const GrContext* ctx, int width, int height, |
| 95 | const GrTextureParams* params) { |
| 96 | if (params && params->isTiled()) { |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 97 | if (!ctx->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 98 | switch(params->filterMode()) { |
| 99 | case GrTextureParams::kNone_FilterMode: |
| 100 | return kNearest_Stretch; |
| 101 | case GrTextureParams::kBilerp_FilterMode: |
| 102 | case GrTextureParams::kMipMap_FilterMode: |
| 103 | return kBilerp_Stretch; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return kNo_Stretch; |
| 108 | } |
| 109 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 110 | static bool make_stretched_key(const GrUniqueKey& origKey, Stretch stretch, |
| 111 | GrUniqueKey* stretchedKey) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 112 | if (origKey.isValid() && kNo_Stretch != stretch) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 113 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 114 | GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 1); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 115 | builder[0] = stretch; |
| 116 | builder.finish(); |
| 117 | return true; |
| 118 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 119 | SkASSERT(!stretchedKey->isValid()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 123 | static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 124 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 125 | // are unique. |
| 126 | uint32_t genID = bitmap.getGenerationID(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 127 | SkIPoint origin = bitmap.pixelRefOrigin(); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 128 | uint32_t width = SkToU16(bitmap.width()); |
| 129 | uint32_t height = SkToU16(bitmap.height()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 130 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 131 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 132 | GrUniqueKey::Builder builder(key, kDomain, 4); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 133 | builder[0] = genID; |
| 134 | builder[1] = origin.fX; |
| 135 | builder[2] = origin.fY; |
| 136 | builder[3] = width | (height << 16); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 137 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 138 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 139 | static void make_bitmap_keys(const SkBitmap& bitmap, |
| 140 | Stretch stretch, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 141 | GrUniqueKey* key, |
| 142 | GrUniqueKey* stretchedKey) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 143 | make_unstretched_key(bitmap, key); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 144 | if (kNo_Stretch != stretch) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 145 | make_stretched_key(*key, stretch, stretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 146 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 147 | } |
| 148 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 149 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) { |
| 150 | desc->fFlags = kNone_GrSurfaceFlags; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 151 | desc->fWidth = bitmap.width(); |
| 152 | desc->fHeight = bitmap.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 153 | desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 154 | desc->fSampleCnt = 0; |
| 155 | } |
| 156 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 157 | namespace { |
| 158 | |
| 159 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 160 | class BitmapInvalidator : public SkPixelRef::GenIDChangeListener { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 161 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 162 | explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 163 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 164 | GrUniqueKeyInvalidatedMessage fMsg; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 165 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | void onChange() override { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 167 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
| 171 | } // namespace |
| 172 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 173 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 174 | static GrTexture* create_texture_for_bmp(GrContext* ctx, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 175 | const GrUniqueKey& optionalKey, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 176 | GrSurfaceDesc desc, |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 177 | SkPixelRef* pixelRefForInvalidationNotification, |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 178 | const void* pixels, |
| 179 | size_t rowBytes) { |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 180 | GrTexture* result = ctx->createTexture(desc, true, pixels, rowBytes); |
| 181 | if (result && optionalKey.isValid()) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 182 | BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey)); |
| 183 | pixelRefForInvalidationNotification->addGenIDChangeListener(listener); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 184 | ctx->addResourceToCache(optionalKey, result); |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 189 | // creates a new texture that is the input texture scaled up to the next power of two in |
| 190 | // width or height. If optionalKey is valid it will be set on the new texture. stretch |
| 191 | // controls whether the scaling is done using nearest or bilerp filtering. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 192 | GrTexture* stretch_texture_to_next_pot(GrTexture* inputTexture, Stretch stretch, |
| 193 | SkPixelRef* pixelRef, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 194 | const GrUniqueKey& optionalKey) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 195 | SkASSERT(kNo_Stretch != stretch); |
| 196 | |
| 197 | GrContext* context = inputTexture->getContext(); |
| 198 | SkASSERT(context); |
| 199 | |
| 200 | // Either it's a cache miss or the original wasn't cached to begin with. |
| 201 | GrSurfaceDesc rtDesc = inputTexture->desc(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 202 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 203 | rtDesc.fWidth = GrNextPow2(rtDesc.fWidth); |
| 204 | rtDesc.fHeight = GrNextPow2(rtDesc.fHeight); |
| 205 | rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig); |
| 206 | |
| 207 | // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise, |
| 208 | // fail. |
| 209 | if (!context->isConfigRenderable(rtDesc.fConfig, false)) { |
| 210 | if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) { |
| 211 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 212 | rtDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 213 | } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
| 214 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 215 | } else { |
| 216 | return NULL; |
| 217 | } |
| 218 | } else if (kRGB_GrColorComponentFlags == |
| 219 | (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) { |
| 220 | if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
| 221 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 222 | } else { |
| 223 | return NULL; |
| 224 | } |
| 225 | } else { |
| 226 | return NULL; |
| 227 | } |
| 228 | } |
| 229 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 230 | GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 231 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 232 | if (!stretched) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
| 235 | GrPaint paint; |
| 236 | |
| 237 | // If filtering is not desired then we want to ensure all texels in the resampled image are |
| 238 | // copies of texels from the original. |
| 239 | GrTextureParams params(SkShader::kClamp_TileMode, |
| 240 | kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode : |
| 241 | GrTextureParams::kNone_FilterMode); |
| 242 | paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params); |
| 243 | |
| 244 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight)); |
| 245 | SkRect localRect = SkRect::MakeWH(1.f, 1.f); |
| 246 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 247 | context->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint, |
| 248 | SkMatrix::I(), rect, localRect); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 249 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 250 | return stretched; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 251 | } |
| 252 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 253 | #ifndef SK_IGNORE_ETC1_SUPPORT |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 254 | static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 255 | const SkBitmap &bm, GrSurfaceDesc desc) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 256 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 257 | |
| 258 | // Is this even encoded data? |
| 259 | if (NULL == data) { |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | // Is this a valid PKM encoded data? |
| 264 | const uint8_t *bytes = data->bytes(); |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 265 | if (etc1_pkm_is_valid(bytes)) { |
| 266 | uint32_t encodedWidth = etc1_pkm_get_width(bytes); |
| 267 | uint32_t encodedHeight = etc1_pkm_get_height(bytes); |
| 268 | |
| 269 | // Does the data match the dimensions of the bitmap? If not, |
| 270 | // then we don't know how to scale the image to match it... |
| 271 | if (encodedWidth != static_cast<uint32_t>(bm.width()) || |
| 272 | encodedHeight != static_cast<uint32_t>(bm.height())) { |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | // Everything seems good... skip ahead to the data. |
| 277 | bytes += ETC_PKM_HEADER_SIZE; |
| 278 | desc.fConfig = kETC1_GrPixelConfig; |
| 279 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 280 | SkKTXFile ktx(data); |
| 281 | |
| 282 | // Is it actually an ETC1 texture? |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 283 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | // Does the data match the dimensions of the bitmap? If not, |
| 288 | // then we don't know how to scale the image to match it... |
| 289 | if (ktx.width() != bm.width() || ktx.height() != bm.height()) { |
| 290 | return NULL; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 291 | } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 292 | |
| 293 | bytes = ktx.pixelData(); |
| 294 | desc.fConfig = kETC1_GrPixelConfig; |
| 295 | } else { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 296 | return NULL; |
| 297 | } |
| 298 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 299 | return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 300 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 301 | #endif // SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 302 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 303 | static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 304 | const SkBitmap& bm, const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 305 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 306 | SkPixelRef* pixelRef = bm.pixelRef(); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 307 | if ((NULL == pixelRef) || |
| 308 | (pixelRef->info().width() != bm.info().width()) || |
| 309 | (pixelRef->info().height() != bm.info().height())) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 310 | return NULL; |
| 311 | } |
| 312 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 313 | const bool useCache = optionalKey.isValid(); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 314 | SkYUVPlanesCache::Info yuvInfo; |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 315 | SkAutoTUnref<SkCachedData> cachedData; |
| 316 | SkAutoMalloc storage; |
| 317 | if (useCache) { |
| 318 | cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo)); |
| 319 | } |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 320 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 321 | void* planes[3]; |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 322 | if (cachedData.get()) { |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 323 | planes[0] = (void*)cachedData->data(); |
| 324 | planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0]; |
| 325 | planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1]; |
| 326 | } else { |
| 327 | // Fetch yuv plane sizes for memory allocation. Here, width and height can be |
| 328 | // rounded up to JPEG block size and be larger than the image's width and height. |
| 329 | if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) { |
| 330 | return NULL; |
| 331 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 332 | |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 333 | // Allocate the memory for YUV |
| 334 | size_t totalSize(0); |
| 335 | for (int i = 0; i < 3; ++i) { |
| 336 | yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth; |
| 337 | yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight; |
| 338 | totalSize += yuvInfo.fSizeInMemory[i]; |
| 339 | } |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 340 | if (useCache) { |
| 341 | cachedData.reset(SkResourceCache::NewCachedData(totalSize)); |
| 342 | planes[0] = cachedData->writable_data(); |
| 343 | } else { |
| 344 | storage.reset(totalSize); |
| 345 | planes[0] = storage.get(); |
| 346 | } |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 347 | planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0]; |
| 348 | planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1]; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 349 | |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 350 | // Get the YUV planes and update plane sizes to actual image size |
| 351 | if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes, |
| 352 | &yuvInfo.fColorSpace)) { |
| 353 | return NULL; |
| 354 | } |
| 355 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 356 | if (useCache) { |
| 357 | // Decoding is done, cache the resulting YUV planes |
| 358 | SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo); |
| 359 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 360 | } |
| 361 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 362 | GrSurfaceDesc yuvDesc; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 363 | yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 364 | SkAutoTUnref<GrTexture> yuvTextures[3]; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 365 | for (int i = 0; i < 3; ++i) { |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 366 | yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; |
| 367 | yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; |
sugoi | 4ab3dbb | 2015-03-06 05:16:52 -0800 | [diff] [blame] | 368 | bool needsExactTexture = |
| 369 | (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) || |
| 370 | (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight); |
| 371 | yuvTextures[i].reset(ctx->refScratchTexture(yuvDesc, |
| 372 | needsExactTexture ? GrContext::kExact_ScratchTexMatch : |
| 373 | GrContext::kApprox_ScratchTexMatch)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 374 | if (!yuvTextures[i] || |
| 375 | !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 376 | yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
| 379 | } |
| 380 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 381 | GrSurfaceDesc rtDesc = desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 382 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 383 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 384 | GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 385 | if (!result) { |
| 386 | return NULL; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 387 | } |
| 388 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 389 | GrRenderTarget* renderTarget = result->asRenderTarget(); |
| 390 | SkASSERT(renderTarget); |
| 391 | |
| 392 | SkAutoTUnref<GrFragmentProcessor> |
| 393 | yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2], |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 394 | yuvInfo.fSize, yuvInfo.fColorSpace)); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 395 | GrPaint paint; |
| 396 | paint.addColorProcessor(yuvToRgbProcessor); |
| 397 | SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth), |
| 398 | SkIntToScalar(yuvInfo.fSize[0].fHeight)); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 399 | |
| 400 | ctx->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 401 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 402 | return result; |
| 403 | } |
| 404 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 405 | static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx, |
| 406 | const SkBitmap& origBitmap, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 407 | const GrUniqueKey& optionalKey) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 408 | SkBitmap tmpBitmap; |
| 409 | |
| 410 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 411 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 412 | GrSurfaceDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 413 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 414 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 415 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 416 | if (ctx->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 417 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 418 | bitmap->width(), bitmap->height()); |
| 419 | SkAutoMalloc storage(imageSize); |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 420 | build_index8_data(storage.get(), origBitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | |
| 422 | // our compressed data will be trimmed, so pass width() for its |
| 423 | // "rowBytes", since they are the same now. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 424 | return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 425 | storage.get(), bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 426 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 427 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | // now bitmap points to our temp, which has been promoted to 32bits |
| 429 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 430 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 431 | } |
krajcevski | 309e869 | 2014-06-02 08:02:45 -0700 | [diff] [blame] | 432 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 433 | |
| 434 | // Is this an ETC1 encoded texture? |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 435 | #ifndef SK_IGNORE_ETC1_SUPPORT |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 436 | // Make sure that the underlying device supports ETC1 textures before we go ahead |
| 437 | // and check the data. |
| 438 | else if (ctx->isConfigTexturable(kETC1_GrPixelConfig) |
| 439 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 440 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 441 | // the bitmap has available pixels, then they might not be what the decompressed |
| 442 | // data is. |
| 443 | && !(bitmap->readyToDraw())) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 444 | GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 445 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 446 | return texture; |
| 447 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 448 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 449 | #endif // SK_IGNORE_ETC1_SUPPORT |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 451 | GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc); |
| 452 | if (texture) { |
| 453 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 454 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 455 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 456 | SkAutoLockPixels alp(*bitmap); |
| 457 | if (!bitmap->readyToDraw()) { |
| 458 | return NULL; |
| 459 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 460 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 461 | return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 462 | bitmap->getPixels(), bitmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static GrTexture* create_bitmap_texture(GrContext* ctx, |
| 466 | const SkBitmap& bmp, |
| 467 | Stretch stretch, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 468 | const GrUniqueKey& unstretchedKey, |
| 469 | const GrUniqueKey& stretchedKey) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 470 | if (kNo_Stretch != stretch) { |
| 471 | SkAutoTUnref<GrTexture> unstretched; |
| 472 | // Check if we have the unstretched version in the cache, if not create it. |
| 473 | if (unstretchedKey.isValid()) { |
| 474 | unstretched.reset(ctx->findAndRefCachedTexture(unstretchedKey)); |
| 475 | } |
| 476 | if (!unstretched) { |
| 477 | unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey)); |
| 478 | if (!unstretched) { |
| 479 | return NULL; |
| 480 | } |
| 481 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 482 | GrTexture* stretched = stretch_texture_to_next_pot(unstretched, stretch, bmp.pixelRef(), |
| 483 | stretchedKey); |
| 484 | return stretched; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey); |
| 488 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 489 | } |
| 490 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 491 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 492 | const SkBitmap& bitmap, |
| 493 | const GrTextureParams* params) { |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 494 | Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params); |
| 495 | |
| 496 | // Handle the case where the bitmap is explicitly texture backed. |
| 497 | GrTexture* texture = bitmap.getTexture(); |
| 498 | if (texture) { |
| 499 | if (kNo_Stretch == stretch) { |
| 500 | return true; |
| 501 | } |
| 502 | // No keys for volatile bitmaps. |
| 503 | if (bitmap.isVolatile()) { |
| 504 | return false; |
| 505 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 506 | const GrUniqueKey& key = texture->getUniqueKey(); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 507 | if (!key.isValid()) { |
| 508 | return false; |
| 509 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 510 | GrUniqueKey stretchedKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 511 | make_stretched_key(key, stretch, &stretchedKey); |
| 512 | return ctx->isResourceInCache(stretchedKey); |
bsalomon | 9ed7f57 | 2014-12-19 12:26:37 -0800 | [diff] [blame] | 513 | } |
| 514 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 515 | // We don't cache volatile bitmaps |
| 516 | if (bitmap.isVolatile()) { |
| 517 | return false; |
| 518 | } |
| 519 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 520 | GrUniqueKey key, stretchedKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 521 | make_bitmap_keys(bitmap, stretch, &key, &stretchedKey); |
| 522 | return ctx->isResourceInCache((kNo_Stretch == stretch) ? key : stretchedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 523 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 524 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 525 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, |
| 526 | const SkBitmap& bitmap, |
| 527 | const GrTextureParams* params) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 528 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 529 | Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 530 | |
| 531 | GrTexture* result = bitmap.getTexture(); |
| 532 | if (result) { |
| 533 | if (kNo_Stretch == stretch) { |
| 534 | return SkRef(result); |
| 535 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 536 | GrUniqueKey stretchedKey; |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 537 | // Don't create a key for the resized version if the bmp is volatile. |
| 538 | if (!bitmap.isVolatile()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 539 | const GrUniqueKey& key = result->getUniqueKey(); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 540 | if (key.isValid()) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 541 | make_stretched_key(key, stretch, &stretchedKey); |
| 542 | GrTexture* stretched = ctx->findAndRefCachedTexture(stretchedKey); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 543 | if (stretched) { |
| 544 | return stretched; |
| 545 | } |
| 546 | } |
| 547 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 548 | return stretch_texture_to_next_pot(result, stretch, bitmap.pixelRef(), stretchedKey); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 549 | } |
| 550 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 551 | GrUniqueKey key, resizedKey; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 552 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 553 | if (!bitmap.isVolatile()) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 554 | // If the bitmap isn't changing try to find a cached copy first. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 555 | make_bitmap_keys(bitmap, stretch, &key, &resizedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 556 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 557 | result = ctx->findAndRefCachedTexture(resizedKey.isValid() ? resizedKey : key); |
| 558 | if (result) { |
| 559 | return result; |
| 560 | } |
| 561 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 562 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 563 | result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey); |
| 564 | if (result) { |
| 565 | return result; |
| 566 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 567 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 568 | SkErrorInternals::SetError( kInternalError_SkError, |
| 569 | "---- failed to create texture for cache [%d %d]\n", |
| 570 | bitmap.width(), bitmap.height()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 571 | |
| 572 | return NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 573 | } |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 574 | /////////////////////////////////////////////////////////////////////////////// |
| 575 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 576 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 577 | // alpha info, that will be considered. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 578 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 579 | switch (ct) { |
| 580 | case kUnknown_SkColorType: |
| 581 | return kUnknown_GrPixelConfig; |
| 582 | case kAlpha_8_SkColorType: |
| 583 | return kAlpha_8_GrPixelConfig; |
| 584 | case kRGB_565_SkColorType: |
| 585 | return kRGB_565_GrPixelConfig; |
| 586 | case kARGB_4444_SkColorType: |
| 587 | return kRGBA_4444_GrPixelConfig; |
| 588 | case kRGBA_8888_SkColorType: |
jvanverth | fe43c40 | 2014-12-22 10:29:30 -0800 | [diff] [blame] | 589 | // if (kSRGB_SkColorProfileType == pt) { |
| 590 | // return kSRGBA_8888_GrPixelConfig; |
| 591 | // } |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 592 | return kRGBA_8888_GrPixelConfig; |
| 593 | case kBGRA_8888_SkColorType: |
| 594 | return kBGRA_8888_GrPixelConfig; |
| 595 | case kIndex_8_SkColorType: |
| 596 | return kIndex_8_GrPixelConfig; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 597 | case kGray_8_SkColorType: |
| 598 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 599 | } |
| 600 | SkASSERT(0); // shouldn't get here |
| 601 | return kUnknown_GrPixelConfig; |
| 602 | } |
| 603 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 604 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 605 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 606 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 607 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 608 | switch (config) { |
| 609 | case kAlpha_8_GrPixelConfig: |
| 610 | ct = kAlpha_8_SkColorType; |
| 611 | break; |
| 612 | case kIndex_8_GrPixelConfig: |
| 613 | ct = kIndex_8_SkColorType; |
| 614 | break; |
| 615 | case kRGB_565_GrPixelConfig: |
| 616 | ct = kRGB_565_SkColorType; |
| 617 | break; |
| 618 | case kRGBA_4444_GrPixelConfig: |
| 619 | ct = kARGB_4444_SkColorType; |
| 620 | break; |
| 621 | case kRGBA_8888_GrPixelConfig: |
| 622 | ct = kRGBA_8888_SkColorType; |
| 623 | break; |
| 624 | case kBGRA_8888_GrPixelConfig: |
| 625 | ct = kBGRA_8888_SkColorType; |
| 626 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 627 | case kSRGBA_8888_GrPixelConfig: |
| 628 | ct = kRGBA_8888_SkColorType; |
| 629 | pt = kSRGB_SkColorProfileType; |
| 630 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 631 | default: |
| 632 | return false; |
| 633 | } |
| 634 | if (ctOut) { |
| 635 | *ctOut = ct; |
| 636 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 637 | if (ptOut) { |
| 638 | *ptOut = pt; |
| 639 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 640 | return true; |
| 641 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 642 | |
| 643 | /////////////////////////////////////////////////////////////////////////////// |
| 644 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 645 | bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 646 | GrColor paintColor, bool constantColor, GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 647 | |
| 648 | grPaint->setDither(skPaint.isDither()); |
| 649 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 650 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 651 | SkXfermode* mode = skPaint.getXfermode(); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 652 | GrXPFactory* xpFactory = NULL; |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame] | 653 | if (!SkXfermode::AsXPFactory(mode, &xpFactory)) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 654 | // Fall back to src-over |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 655 | // return false here? |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 656 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 657 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 658 | SkASSERT(xpFactory); |
| 659 | grPaint->setXPFactory(xpFactory)->unref(); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 660 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 661 | //set the color of the paint to the one of the parameter |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 662 | grPaint->setColor(paintColor); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 663 | |
| 664 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 665 | if (colorFilter) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 666 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 667 | // in a shader. |
| 668 | if (constantColor) { |
| 669 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 670 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 671 | } else { |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 672 | SkTDArray<GrFragmentProcessor*> array; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 673 | // return false if failed? |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 674 | if (colorFilter->asFragmentProcessors(context, &array)) { |
| 675 | for (int i = 0; i < array.count(); ++i) { |
| 676 | grPaint->addColorProcessor(array[i]); |
| 677 | array[i]->unref(); |
| 678 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 682 | |
| 683 | #ifndef SK_IGNORE_GPU_DITHER |
| 684 | // If the dither flag is set, then we need to see if the underlying context |
| 685 | // supports it. If not, then install a dither effect. |
| 686 | if (skPaint.isDither() && grPaint->numColorStages() > 0) { |
| 687 | // What are we rendering into? |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 688 | SkASSERT(rt); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 689 | |
| 690 | // Suspect the dithering flag has no effect on these configs, otherwise |
| 691 | // fall back on setting the appropriate state. |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 692 | if (GrPixelConfigIs8888(rt->config()) || |
| 693 | GrPixelConfigIs8888(rt->config())) { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 694 | // The dither flag is set and the target is likely |
| 695 | // not going to be dithered by the GPU. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 696 | SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create()); |
| 697 | if (fp.get()) { |
| 698 | grPaint->addColorProcessor(fp); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 699 | grPaint->setDither(false); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 704 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 705 | } |
| 706 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 707 | bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint, |
| 708 | const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 709 | SkShader* shader = skPaint.getShader(); |
| 710 | if (NULL == shader) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 711 | return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()), |
| 712 | constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 713 | } |
| 714 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 715 | GrColor paintColor = SkColor2GrColor(skPaint.getColor()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 716 | |
| 717 | // Start a new block here in order to preserve our context state after calling |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 718 | // asFragmentProcessor(). Since these calls get passed back to the client, we don't really |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 719 | // want them messing around with the context. |
| 720 | { |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 721 | // Allow the shader to modify paintColor and also create an effect to be installed as |
| 722 | // the first color effect on the GrPaint. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 723 | GrFragmentProcessor* fp = NULL; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame^] | 724 | if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) { |
| 725 | return false; |
| 726 | } |
| 727 | if (fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 728 | grPaint->addColorProcessor(fp)->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 729 | constantColor = false; |
| 730 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 731 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 732 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 733 | // The grcolor is automatically set when calling asFragmentProcessor. |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 734 | // 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^] | 735 | return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 736 | } |