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