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" |
| 12 | #include "GrXferProcessor.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 13 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 14 | #include "SkConfig8888.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 15 | #include "SkData.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 16 | #include "SkMessageBus.h" |
| 17 | #include "SkPixelRef.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 18 | #include "SkTextureCompressor.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@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 87 | static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { |
| 88 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 89 | // are unique. |
| 90 | uint32_t genID = bitmap.getGenerationID(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 91 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 92 | int16_t width = SkToS16(bitmap.width()); |
| 93 | int16_t height = SkToS16(bitmap.height()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 94 | |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 95 | GrCacheID::Key key; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 96 | memcpy(key.fData8 + 0, &genID, 4); |
| 97 | memcpy(key.fData8 + 4, &origin.fX, 4); |
| 98 | memcpy(key.fData8 + 8, &origin.fY, 4); |
| 99 | memcpy(key.fData8 + 12, &width, 2); |
| 100 | memcpy(key.fData8 + 14, &height, 2); |
| 101 | static const size_t kKeyDataSize = 16; |
bsalomon@google.com | 10a9fb8 | 2013-01-02 19:29:57 +0000 | [diff] [blame] | 102 | memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 103 | GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 104 | static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); |
| 105 | id->reset(gBitmapTextureDomain, key); |
| 106 | } |
| 107 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 108 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) { |
| 109 | desc->fFlags = kNone_GrSurfaceFlags; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 110 | desc->fWidth = bitmap.width(); |
| 111 | desc->fHeight = bitmap.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 112 | desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 113 | desc->fSampleCnt = 0; |
| 114 | } |
| 115 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 116 | namespace { |
| 117 | |
| 118 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
| 119 | class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { |
| 120 | public: |
| 121 | explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} |
| 122 | private: |
| 123 | GrResourceKey fKey; |
| 124 | |
| 125 | virtual void onChange() SK_OVERRIDE { |
| 126 | const GrResourceInvalidatedMessage message = { fKey }; |
| 127 | SkMessageBus<GrResourceInvalidatedMessage>::Post(message); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | } // namespace |
| 132 | |
| 133 | static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 134 | SkASSERT(pixelRef); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 135 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 136 | } |
| 137 | |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 138 | static GrTexture* sk_gr_allocate_texture(GrContext* ctx, |
| 139 | bool cache, |
| 140 | const GrTextureParams* params, |
| 141 | const SkBitmap& bm, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 142 | GrSurfaceDesc desc, |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 143 | const void* pixels, |
| 144 | size_t rowBytes) { |
| 145 | GrTexture* result; |
| 146 | if (cache) { |
| 147 | // This texture is likely to be used again so leave it in the cache |
| 148 | GrCacheID cacheID; |
| 149 | generate_bitmap_cache_id(bm, &cacheID); |
| 150 | |
| 151 | GrResourceKey key; |
| 152 | result = ctx->createTexture(params, desc, cacheID, pixels, rowBytes, &key); |
| 153 | if (result) { |
| 154 | add_genID_listener(key, bm.pixelRef()); |
| 155 | } |
| 156 | } else { |
| 157 | // This texture is unlikely to be used again (in its present form) so |
| 158 | // just use a scratch texture. This will remove the texture from the |
| 159 | // cache so no one else can find it. Additionally, once unlocked, the |
| 160 | // scratch texture will go to the end of the list for purging so will |
| 161 | // likely be available for this volatile bitmap the next time around. |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 162 | result = ctx->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch); |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 163 | if (pixels) { |
| 164 | result->writePixels(0, 0, bm.width(), bm.height(), desc.fConfig, pixels, rowBytes); |
| 165 | } |
| 166 | } |
| 167 | return result; |
| 168 | } |
| 169 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 170 | #ifndef SK_IGNORE_ETC1_SUPPORT |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 171 | static GrTexture *load_etc1_texture(GrContext* ctx, bool cache, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 172 | const GrTextureParams* params, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 173 | const SkBitmap &bm, GrSurfaceDesc desc) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 174 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 175 | |
| 176 | // Is this even encoded data? |
| 177 | if (NULL == data) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | // Is this a valid PKM encoded data? |
| 182 | const uint8_t *bytes = data->bytes(); |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 183 | if (etc1_pkm_is_valid(bytes)) { |
| 184 | uint32_t encodedWidth = etc1_pkm_get_width(bytes); |
| 185 | uint32_t encodedHeight = etc1_pkm_get_height(bytes); |
| 186 | |
| 187 | // Does the data match the dimensions of the bitmap? If not, |
| 188 | // then we don't know how to scale the image to match it... |
| 189 | if (encodedWidth != static_cast<uint32_t>(bm.width()) || |
| 190 | encodedHeight != static_cast<uint32_t>(bm.height())) { |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | // Everything seems good... skip ahead to the data. |
| 195 | bytes += ETC_PKM_HEADER_SIZE; |
| 196 | desc.fConfig = kETC1_GrPixelConfig; |
| 197 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 198 | SkKTXFile ktx(data); |
| 199 | |
| 200 | // Is it actually an ETC1 texture? |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 201 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | // Does the data match the dimensions of the bitmap? If not, |
| 206 | // then we don't know how to scale the image to match it... |
| 207 | if (ktx.width() != bm.width() || ktx.height() != bm.height()) { |
| 208 | return NULL; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 209 | } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 210 | |
| 211 | bytes = ktx.pixelData(); |
| 212 | desc.fConfig = kETC1_GrPixelConfig; |
| 213 | } else { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 214 | return NULL; |
| 215 | } |
| 216 | |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 217 | return sk_gr_allocate_texture(ctx, cache, params, bm, desc, bytes, 0); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 218 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 219 | #endif // SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 220 | |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 221 | static GrTexture *load_yuv_texture(GrContext* ctx, bool cache, const GrTextureParams* params, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 222 | const SkBitmap& bm, const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 223 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
| 224 | if ((bm.pixelRef()->info().width() != bm.info().width()) || |
| 225 | (bm.pixelRef()->info().height() != bm.info().height())) { |
| 226 | return NULL; |
| 227 | } |
| 228 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 229 | SkPixelRef* pixelRef = bm.pixelRef(); |
| 230 | SkISize yuvSizes[3]; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 231 | if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL, NULL)) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | // Allocate the memory for YUV |
| 236 | size_t totalSize(0); |
| 237 | size_t sizes[3], rowBytes[3]; |
| 238 | for (int i = 0; i < 3; ++i) { |
| 239 | rowBytes[i] = yuvSizes[i].fWidth; |
| 240 | totalSize += sizes[i] = rowBytes[i] * yuvSizes[i].fHeight; |
| 241 | } |
| 242 | SkAutoMalloc storage(totalSize); |
| 243 | void* planes[3]; |
| 244 | planes[0] = storage.get(); |
| 245 | planes[1] = (uint8_t*)planes[0] + sizes[0]; |
| 246 | planes[2] = (uint8_t*)planes[1] + sizes[1]; |
| 247 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 248 | SkYUVColorSpace colorSpace; |
| 249 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 250 | // Get the YUV planes |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 251 | if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes, &colorSpace)) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 255 | GrSurfaceDesc yuvDesc; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 256 | yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 257 | SkAutoTUnref<GrTexture> yuvTextures[3]; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 258 | for (int i = 0; i < 3; ++i) { |
| 259 | yuvDesc.fWidth = yuvSizes[i].fWidth; |
| 260 | yuvDesc.fHeight = yuvSizes[i].fHeight; |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 261 | yuvTextures[i].reset( |
| 262 | ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch)); |
| 263 | if (!yuvTextures[i] || |
| 264 | !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
| 265 | yuvDesc.fConfig, planes[i], rowBytes[i])) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | } |
| 269 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 270 | GrSurfaceDesc rtDesc = desc; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 271 | rtDesc.fFlags = rtDesc.fFlags | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 272 | kRenderTarget_GrSurfaceFlag | |
| 273 | kNoStencil_GrSurfaceFlag; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 274 | |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 275 | GrTexture* result = sk_gr_allocate_texture(ctx, cache, params, bm, rtDesc, NULL, 0); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 276 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 277 | GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 278 | if (renderTarget) { |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 279 | SkAutoTUnref<GrFragmentProcessor> yuvToRgbProcessor( |
| 280 | GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2], colorSpace)); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 281 | GrPaint paint; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 282 | paint.addColorProcessor(yuvToRgbProcessor); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 283 | SkRect r = SkRect::MakeWH(SkIntToScalar(yuvSizes[0].fWidth), |
| 284 | SkIntToScalar(yuvSizes[0].fHeight)); |
| 285 | GrContext::AutoRenderTarget autoRT(ctx, renderTarget); |
| 286 | GrContext::AutoMatrix am; |
| 287 | am.setIdentity(ctx); |
| 288 | GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip); |
| 289 | ctx->drawRect(paint, r); |
| 290 | } else { |
| 291 | SkSafeSetNull(result); |
| 292 | } |
| 293 | |
| 294 | return result; |
| 295 | } |
| 296 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 297 | static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 298 | bool cache, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 299 | const GrTextureParams* params, |
| 300 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | SkBitmap tmpBitmap; |
| 302 | |
| 303 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 304 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 305 | GrSurfaceDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 306 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 307 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 308 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | // build_compressed_data doesn't do npot->pot expansion |
| 310 | // and paletted textures can't be sub-updated |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 311 | if (cache && ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 312 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 313 | bitmap->width(), bitmap->height()); |
| 314 | SkAutoMalloc storage(imageSize); |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 315 | build_index8_data(storage.get(), origBitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | |
| 317 | // our compressed data will be trimmed, so pass width() for its |
| 318 | // "rowBytes", since they are the same now. |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 319 | return sk_gr_allocate_texture(ctx, cache, params, origBitmap, |
| 320 | desc, storage.get(), bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 321 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 322 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 323 | // now bitmap points to our temp, which has been promoted to 32bits |
| 324 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 325 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 326 | } |
krajcevski | 309e869 | 2014-06-02 08:02:45 -0700 | [diff] [blame] | 327 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 328 | |
| 329 | // Is this an ETC1 encoded texture? |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 330 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 331 | else if ( |
bungeman | 77cd8b0 | 2014-09-10 14:59:59 -0700 | [diff] [blame] | 332 | // We do not support scratch ETC1 textures, hence they should all be at least |
| 333 | // trying to go to the cache. |
| 334 | cache |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 335 | // Make sure that the underlying device supports ETC1 textures before we go ahead |
| 336 | // and check the data. |
bungeman | 77cd8b0 | 2014-09-10 14:59:59 -0700 | [diff] [blame] | 337 | && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig) |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 338 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 339 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 340 | // the bitmap has available pixels, then they might not be what the decompressed |
| 341 | // data is. |
| 342 | && !(bitmap->readyToDraw())) { |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 343 | GrTexture *texture = load_etc1_texture(ctx, cache, params, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 344 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 345 | return texture; |
| 346 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 347 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 348 | #endif // SK_IGNORE_ETC1_SUPPORT |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 349 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 350 | else { |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 351 | GrTexture *texture = load_yuv_texture(ctx, cache, params, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 352 | if (texture) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 353 | return texture; |
| 354 | } |
| 355 | } |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 356 | SkAutoLockPixels alp(*bitmap); |
| 357 | if (!bitmap->readyToDraw()) { |
| 358 | return NULL; |
| 359 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 360 | |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 361 | return sk_gr_allocate_texture(ctx, cache, params, origBitmap, desc, |
| 362 | bitmap->getPixels(), bitmap->rowBytes()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | } |
| 364 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 365 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 366 | const SkBitmap& bitmap, |
| 367 | const GrTextureParams* params) { |
| 368 | GrCacheID cacheID; |
| 369 | generate_bitmap_cache_id(bitmap, &cacheID); |
| 370 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 371 | GrSurfaceDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 372 | generate_bitmap_texture_desc(bitmap, &desc); |
| 373 | return ctx->isTextureInCache(desc, cacheID, params); |
| 374 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 376 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, |
| 377 | const SkBitmap& bitmap, |
| 378 | const GrTextureParams* params) { |
bsalomon | ebfce41 | 2014-10-07 06:20:25 -0700 | [diff] [blame] | 379 | GrTexture* result = NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 380 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 381 | bool cache = !bitmap.isVolatile(); |
| 382 | |
| 383 | if (cache) { |
| 384 | // If the bitmap isn't changing try to find a cached copy first. |
| 385 | |
| 386 | GrCacheID cacheID; |
| 387 | generate_bitmap_cache_id(bitmap, &cacheID); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 388 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 389 | GrSurfaceDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 390 | generate_bitmap_texture_desc(bitmap, &desc); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 391 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 392 | result = ctx->findAndRefTexture(desc, cacheID, params); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 393 | } |
| 394 | if (NULL == result) { |
| 395 | result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 396 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 397 | if (NULL == result) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 398 | SkDebugf("---- failed to create texture for cache [%d %d]\n", |
| 399 | bitmap.width(), bitmap.height()); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 400 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 401 | return result; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 402 | } |
| 403 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 404 | /////////////////////////////////////////////////////////////////////////////// |
| 405 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 406 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 407 | // alpha info, that will be considered. |
| 408 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) { |
| 409 | switch (ct) { |
| 410 | case kUnknown_SkColorType: |
| 411 | return kUnknown_GrPixelConfig; |
| 412 | case kAlpha_8_SkColorType: |
| 413 | return kAlpha_8_GrPixelConfig; |
| 414 | case kRGB_565_SkColorType: |
| 415 | return kRGB_565_GrPixelConfig; |
| 416 | case kARGB_4444_SkColorType: |
| 417 | return kRGBA_4444_GrPixelConfig; |
| 418 | case kRGBA_8888_SkColorType: |
| 419 | return kRGBA_8888_GrPixelConfig; |
| 420 | case kBGRA_8888_SkColorType: |
| 421 | return kBGRA_8888_GrPixelConfig; |
| 422 | case kIndex_8_SkColorType: |
| 423 | return kIndex_8_GrPixelConfig; |
| 424 | } |
| 425 | SkASSERT(0); // shouldn't get here |
| 426 | return kUnknown_GrPixelConfig; |
| 427 | } |
| 428 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 429 | bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) { |
| 430 | SkColorType ct; |
| 431 | switch (config) { |
| 432 | case kAlpha_8_GrPixelConfig: |
| 433 | ct = kAlpha_8_SkColorType; |
| 434 | break; |
| 435 | case kIndex_8_GrPixelConfig: |
| 436 | ct = kIndex_8_SkColorType; |
| 437 | break; |
| 438 | case kRGB_565_GrPixelConfig: |
| 439 | ct = kRGB_565_SkColorType; |
| 440 | break; |
| 441 | case kRGBA_4444_GrPixelConfig: |
| 442 | ct = kARGB_4444_SkColorType; |
| 443 | break; |
| 444 | case kRGBA_8888_GrPixelConfig: |
| 445 | ct = kRGBA_8888_SkColorType; |
| 446 | break; |
| 447 | case kBGRA_8888_GrPixelConfig: |
| 448 | ct = kBGRA_8888_SkColorType; |
| 449 | break; |
| 450 | default: |
| 451 | return false; |
| 452 | } |
| 453 | if (ctOut) { |
| 454 | *ctOut = ct; |
| 455 | } |
| 456 | return true; |
| 457 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 458 | |
| 459 | /////////////////////////////////////////////////////////////////////////////// |
| 460 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 461 | void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor, |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 462 | bool constantColor, GrPaint* grPaint) { |
| 463 | |
| 464 | grPaint->setDither(skPaint.isDither()); |
| 465 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 466 | |
| 467 | SkXfermode::Coeff sm; |
| 468 | SkXfermode::Coeff dm; |
| 469 | |
| 470 | SkXfermode* mode = skPaint.getXfermode(); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame^] | 471 | GrFragmentProcessor* fragmentProcessor = NULL; |
| 472 | GrXPFactory* xpFactory = NULL; |
| 473 | if (SkXfermode::AsFragmentProcessorOrXPFactory(mode, &fragmentProcessor, &xpFactory, |
| 474 | &sm, &dm)) { |
| 475 | if (fragmentProcessor) { |
| 476 | SkASSERT(NULL == xpFactory); |
| 477 | grPaint->addColorProcessor(fragmentProcessor)->unref(); |
| 478 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kOne_Coeff, |
| 479 | SkXfermode::kZero_Coeff); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 480 | sm = SkXfermode::kOne_Coeff; |
| 481 | dm = SkXfermode::kZero_Coeff; |
| 482 | } |
| 483 | } else { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 484 | // Fall back to src-over |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame^] | 485 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kOne_Coeff, |
| 486 | SkXfermode::kISA_Coeff); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 487 | sm = SkXfermode::kOne_Coeff; |
| 488 | dm = SkXfermode::kISA_Coeff; |
| 489 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame^] | 490 | SkASSERT(xpFactory); |
| 491 | grPaint->setXPFactory(xpFactory)->unref(); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 492 | grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 493 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 494 | //set the color of the paint to the one of the parameter |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 495 | grPaint->setColor(paintColor); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 496 | |
| 497 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 498 | if (colorFilter) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 499 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 500 | // in a shader. |
| 501 | if (constantColor) { |
| 502 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 503 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 504 | } else { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 505 | SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context)); |
| 506 | if (fp.get()) { |
| 507 | grPaint->addColorProcessor(fp); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 511 | |
| 512 | #ifndef SK_IGNORE_GPU_DITHER |
| 513 | // If the dither flag is set, then we need to see if the underlying context |
| 514 | // supports it. If not, then install a dither effect. |
| 515 | if (skPaint.isDither() && grPaint->numColorStages() > 0) { |
| 516 | // What are we rendering into? |
| 517 | const GrRenderTarget *target = context->getRenderTarget(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 518 | SkASSERT(target); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 519 | |
| 520 | // Suspect the dithering flag has no effect on these configs, otherwise |
| 521 | // fall back on setting the appropriate state. |
| 522 | if (target->config() == kRGBA_8888_GrPixelConfig || |
| 523 | target->config() == kBGRA_8888_GrPixelConfig) { |
| 524 | // The dither flag is set and the target is likely |
| 525 | // not going to be dithered by the GPU. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 526 | SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create()); |
| 527 | if (fp.get()) { |
| 528 | grPaint->addColorProcessor(fp); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 529 | grPaint->setDither(false); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | #endif |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 534 | } |
| 535 | |
commit-bot@chromium.org | 9e967ad | 2014-05-20 15:06:29 +0000 | [diff] [blame] | 536 | /** |
| 537 | * Unlike GrContext::AutoMatrix, this doesn't require setting a new matrix. GrContext::AutoMatrix |
| 538 | * likes to set the new matrix in its constructor because it is usually necessary to simulataneously |
| 539 | * update a GrPaint. This AutoMatrix is used while initially setting up GrPaint, however. |
| 540 | */ |
| 541 | class AutoMatrix { |
| 542 | public: |
| 543 | AutoMatrix(GrContext* context) { |
| 544 | fMatrix = context->getMatrix(); |
| 545 | fContext = context; |
| 546 | } |
| 547 | ~AutoMatrix() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 548 | SkASSERT(fContext); |
commit-bot@chromium.org | 9e967ad | 2014-05-20 15:06:29 +0000 | [diff] [blame] | 549 | fContext->setMatrix(fMatrix); |
| 550 | } |
| 551 | private: |
| 552 | GrContext* fContext; |
| 553 | SkMatrix fMatrix; |
| 554 | }; |
| 555 | |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 556 | void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint, |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 557 | bool constantColor, GrPaint* grPaint) { |
| 558 | SkShader* shader = skPaint.getShader(); |
| 559 | if (NULL == shader) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 560 | SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()), |
| 561 | constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 565 | GrColor paintColor = SkColor2GrColor(skPaint.getColor()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 566 | |
| 567 | // 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] | 568 | // 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] | 569 | // want them messing around with the context. |
| 570 | { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 571 | // SkShader::asFragmentProcessor() may do offscreen rendering. Save off the current RT, |
| 572 | // clip, and matrix. We don't reset the matrix on the context because |
| 573 | // SkShader::asFragmentProcessor may use GrContext::getMatrix() to know the transformation |
| 574 | // from local coords to device space. |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 575 | GrContext::AutoRenderTarget art(context, NULL); |
| 576 | GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip); |
| 577 | AutoMatrix am(context); |
| 578 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 579 | // Allow the shader to modify paintColor and also create an effect to be installed as |
| 580 | // the first color effect on the GrPaint. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 581 | GrFragmentProcessor* fp = NULL; |
| 582 | if (shader->asFragmentProcessor(context, skPaint, NULL, &paintColor, &fp) && fp) { |
| 583 | grPaint->addColorProcessor(fp)->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 584 | constantColor = false; |
| 585 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 586 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 587 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 588 | // The grcolor is automatically set when calling asFragmentProcessor. |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 589 | // 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] | 590 | SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 591 | } |