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