blob: 5c0464da20acd72ef60f4569b62fe679c6c619e3 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "SkGr.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00009#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000010#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070011#include "SkData.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000012#include "SkMessageBus.h"
13#include "SkPixelRef.h"
14#include "GrResourceCache.h"
krajcevski9c0e6292014-06-02 07:38:14 -070015#include "GrGpu.h"
krajcevskif461a8f2014-06-19 14:14:06 -070016#include "effects/GrDitherEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070017#include "GrDrawTargetCaps.h"
sugoi518d83d2014-07-21 11:37:39 -070018#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070019
krajcevski8c111f72014-06-02 13:51:34 -070020#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070021# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070022# include "etc1.h"
23#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25/* Fill out buffer with the compressed format Ganesh expects from a colortable
26 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000027
28 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000029 we could detect that the colortable.count is <= 16, and then repack the
30 indices as nibbles to save RAM, but it would take more time (i.e. a lot
31 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
34 as the colortable.count says it is.
35 */
36static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070037 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000038
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000039 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000040 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000041 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000042 return;
43 }
44
45 SkColorTable* ctable = bitmap.getColorTable();
46 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000047
reed@google.com7111d462014-03-25 16:20:24 +000048 const int count = ctable->count();
49
50 SkDstPixelInfo dstPI;
51 dstPI.fColorType = kRGBA_8888_SkColorType;
52 dstPI.fAlphaType = kPremul_SkAlphaType;
53 dstPI.fPixels = buffer;
54 dstPI.fRowBytes = count * sizeof(SkPMColor);
55
56 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000057 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000058 srcPI.fAlphaType = kPremul_SkAlphaType;
59 srcPI.fPixels = ctable->lockColors();
60 srcPI.fRowBytes = count * sizeof(SkPMColor);
61
62 srcPI.convertPixelsTo(&dstPI, count, 1);
63
reed@google.com0a6151d2013-10-10 14:44:56 +000064 ctable->unlockColors();
bsalomon@google.com5782d712011-01-21 21:03:59 +000065
reed@google.comac10a2d2010-12-22 21:39:39 +000066 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000067 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000068
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000069 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000070 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
71 } else {
72 // need to trim off the extra bytes per row
73 size_t width = bitmap.width();
74 size_t rowBytes = bitmap.rowBytes();
75 const char* src = (const char*)bitmap.getPixels();
76 for (int y = 0; y < bitmap.height(); y++) {
77 memcpy(dst, src, width);
78 src += rowBytes;
79 dst += width;
80 }
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000086static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) {
87 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
88 // are unique.
89 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +000090 SkIPoint origin = bitmap.pixelRefOrigin();
91 int16_t width = SkToS16(bitmap.width());
92 int16_t height = SkToS16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000093
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +000094 GrCacheID::Key key;
reed@google.com672588b2014-01-08 15:42:01 +000095 memcpy(key.fData8 + 0, &genID, 4);
96 memcpy(key.fData8 + 4, &origin.fX, 4);
97 memcpy(key.fData8 + 8, &origin.fY, 4);
98 memcpy(key.fData8 + 12, &width, 2);
99 memcpy(key.fData8 + 14, &height, 2);
100 static const size_t kKeyDataSize = 16;
bsalomon@google.com10a9fb82013-01-02 19:29:57 +0000101 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
reed@google.com672588b2014-01-08 15:42:01 +0000102 GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000103 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
104 id->reset(gBitmapTextureDomain, key);
105}
106
107static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
108 desc->fFlags = kNone_GrTextureFlags;
109 desc->fWidth = bitmap.width();
110 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000111 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000112 desc->fSampleCnt = 0;
113}
114
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000115namespace {
116
117// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
118class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
119public:
120 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
121private:
122 GrResourceKey fKey;
123
124 virtual void onChange() SK_OVERRIDE {
125 const GrResourceInvalidatedMessage message = { fKey };
126 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
127 }
128};
129
130} // namespace
131
132static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
133 SkASSERT(NULL != pixelRef);
134 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
135}
136
krajcevski8c111f72014-06-02 13:51:34 -0700137#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700138static GrTexture *load_etc1_texture(GrContext* ctx,
139 const GrTextureParams* params,
140 const SkBitmap &bm, GrTextureDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700141 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700142
143 // Is this even encoded data?
144 if (NULL == data) {
145 return NULL;
146 }
147
148 // Is this a valid PKM encoded data?
149 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700150 if (etc1_pkm_is_valid(bytes)) {
151 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
152 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
153
154 // Does the data match the dimensions of the bitmap? If not,
155 // then we don't know how to scale the image to match it...
156 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
157 encodedHeight != static_cast<uint32_t>(bm.height())) {
158 return NULL;
159 }
160
161 // Everything seems good... skip ahead to the data.
162 bytes += ETC_PKM_HEADER_SIZE;
163 desc.fConfig = kETC1_GrPixelConfig;
164 } else if (SkKTXFile::is_ktx(bytes)) {
165 SkKTXFile ktx(data);
166
167 // Is it actually an ETC1 texture?
168 if (!ktx.isETC1()) {
169 return NULL;
170 }
171
172 // Does the data match the dimensions of the bitmap? If not,
173 // then we don't know how to scale the image to match it...
174 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
175 return NULL;
176 }
177
178 bytes = ktx.pixelData();
179 desc.fConfig = kETC1_GrPixelConfig;
180 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700181 return NULL;
182 }
183
krajcevski9c0e6292014-06-02 07:38:14 -0700184 // This texture is likely to be used again so leave it in the cache
185 GrCacheID cacheID;
186 generate_bitmap_cache_id(bm, &cacheID);
187
188 GrResourceKey key;
189 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key);
190 if (NULL != result) {
191 add_genID_listener(key, bm.pixelRef());
192 }
193 return result;
194}
krajcevski8c111f72014-06-02 13:51:34 -0700195#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700196
sugoi518d83d2014-07-21 11:37:39 -0700197static GrTexture *load_yuv_texture(GrContext* ctx, const GrTextureParams* params,
198 const SkBitmap& bm, const GrTextureDesc& desc) {
199 GrTexture* result = NULL;
200
201 SkPixelRef* pixelRef = bm.pixelRef();
202 SkISize yuvSizes[3];
203 if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL)) {
204 return NULL;
205 }
206
207 // Allocate the memory for YUV
208 size_t totalSize(0);
209 size_t sizes[3], rowBytes[3];
210 for (int i = 0; i < 3; ++i) {
211 rowBytes[i] = yuvSizes[i].fWidth;
212 totalSize += sizes[i] = rowBytes[i] * yuvSizes[i].fHeight;
213 }
214 SkAutoMalloc storage(totalSize);
215 void* planes[3];
216 planes[0] = storage.get();
217 planes[1] = (uint8_t*)planes[0] + sizes[0];
218 planes[2] = (uint8_t*)planes[1] + sizes[1];
219
220 // Get the YUV planes
221 if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes)) {
222 return NULL;
223 }
224
225 GrTextureDesc yuvDesc;
226 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
227 GrAutoScratchTexture yuvTextures[3];
228 for (int i = 0; i < 3; ++i) {
229 yuvDesc.fWidth = yuvSizes[i].fWidth;
230 yuvDesc.fHeight = yuvSizes[i].fHeight;
231 yuvTextures[i].set(ctx, yuvDesc);
232 if ((NULL == yuvTextures[i].texture()) ||
233 !ctx->writeTexturePixels(yuvTextures[i].texture(),
234 0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
235 yuvDesc.fConfig, planes[i], rowBytes[i])) {
236 return NULL;
237 }
238 }
239
240 GrTextureDesc rtDesc = desc;
241 rtDesc.fFlags = rtDesc.fFlags |
242 kRenderTarget_GrTextureFlagBit |
243 kNoStencil_GrTextureFlagBit;
244
245 // This texture is likely to be used again so leave it in the cache
246 GrCacheID cacheID;
247 generate_bitmap_cache_id(bm, &cacheID);
248
249 GrResourceKey key;
250 result = ctx->createTexture(params, rtDesc, cacheID, NULL, 0, &key);
251 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL;
252 if (NULL != renderTarget) {
253 add_genID_listener(key, bm.pixelRef());
254 SkAutoTUnref<GrEffect> yuvToRgbEffect(GrYUVtoRGBEffect::Create(
255 yuvTextures[0].texture(), yuvTextures[1].texture(), yuvTextures[2].texture()));
256 GrPaint paint;
257 paint.addColorEffect(yuvToRgbEffect);
258 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvSizes[0].fWidth),
259 SkIntToScalar(yuvSizes[0].fHeight));
260 GrContext::AutoRenderTarget autoRT(ctx, renderTarget);
261 GrContext::AutoMatrix am;
262 am.setIdentity(ctx);
263 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
264 ctx->drawRect(paint, r);
265 } else {
266 SkSafeSetNull(result);
267 }
268
269 return result;
270}
271
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000272static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000273 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000274 const GrTextureParams* params,
275 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 SkBitmap tmpBitmap;
277
278 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000279
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000280 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000281 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000282
reed0689d7b2014-06-14 05:30:20 -0700283 if (kIndex_8_SkColorType == bitmap->colorType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 // build_compressed_data doesn't do npot->pot expansion
285 // and paletted textures can't be sub-updated
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000286 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
287 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000289
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 build_compressed_data(storage.get(), origBitmap);
291
292 // our compressed data will be trimmed, so pass width() for its
293 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000294
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000295 if (cache) {
296 GrCacheID cacheID;
297 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000298
299 GrResourceKey key;
300 GrTexture* result = ctx->createTexture(params, desc, cacheID,
301 storage.get(), bitmap->width(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000302 if (NULL != result) {
303 add_genID_listener(key, origBitmap.pixelRef());
304 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000305 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000306 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000307 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000308 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000309 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000310 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000312 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000313 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000315 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 // now bitmap points to our temp, which has been promoted to 32bits
317 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000318 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 }
krajcevski309e8692014-06-02 08:02:45 -0700320 }
krajcevski9c0e6292014-06-02 07:38:14 -0700321
322 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700323#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9a3cdbb2014-06-05 07:03:39 -0700324 else if (
325 // We do not support scratch ETC1 textures, hence they should all be at least
326 // trying to go to the cache.
327 cache
328 // Make sure that the underlying device supports ETC1 textures before we go ahead
329 // and check the data.
330 && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig)
331 // If the bitmap had compressed data and was then uncompressed, it'll still return
332 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
333 // the bitmap has available pixels, then they might not be what the decompressed
334 // data is.
335 && !(bitmap->readyToDraw())) {
krajcevski9c0e6292014-06-02 07:38:14 -0700336 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc);
337 if (NULL != texture) {
338 return texture;
339 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000340 }
krajcevski8c111f72014-06-02 13:51:34 -0700341#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
sugoi518d83d2014-07-21 11:37:39 -0700343 else {
344 GrTexture *texture = load_yuv_texture(ctx, params, *bitmap, desc);
345 if (NULL != texture) {
346 return texture;
347 }
348 }
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000349 SkAutoLockPixels alp(*bitmap);
350 if (!bitmap->readyToDraw()) {
351 return NULL;
352 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000353 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000354 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000355 GrCacheID cacheID;
356 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000357
358 GrResourceKey key;
359 GrTexture* result = ctx->createTexture(params, desc, cacheID,
360 bitmap->getPixels(), bitmap->rowBytes(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000361 if (NULL != result) {
362 add_genID_listener(key, origBitmap.pixelRef());
363 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000364 return result;
365 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000366 // This texture is unlikely to be used again (in its present form) so
367 // just use a scratch texture. This will remove the texture from the
368 // cache so no one else can find it. Additionally, once unlocked, the
369 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000370 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000371 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000372 result->writePixels(0, 0,
373 bitmap->width(), bitmap->height(),
374 desc.fConfig,
375 bitmap->getPixels(),
376 bitmap->rowBytes());
377 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000378 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000379}
380
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000381bool GrIsBitmapInCache(const GrContext* ctx,
382 const SkBitmap& bitmap,
383 const GrTextureParams* params) {
384 GrCacheID cacheID;
385 generate_bitmap_cache_id(bitmap, &cacheID);
386
387 GrTextureDesc desc;
388 generate_bitmap_texture_desc(bitmap, &desc);
389 return ctx->isTextureInCache(desc, cacheID, params);
390}
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000392GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
393 const SkBitmap& bitmap,
394 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000395 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000396
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000397 bool cache = !bitmap.isVolatile();
398
399 if (cache) {
400 // If the bitmap isn't changing try to find a cached copy first.
401
402 GrCacheID cacheID;
403 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000404
405 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000406 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000407
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000408 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000409 }
410 if (NULL == result) {
411 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000412 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000413 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000414 GrPrintf("---- failed to create texture for cache [%d %d]\n",
415 bitmap.width(), bitmap.height());
416 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000417 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000418}
419
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000420void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000421 SkASSERT(NULL != texture->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000422
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000423 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000424 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000425}
426
427///////////////////////////////////////////////////////////////////////////////
428
reedc3b32662014-06-17 08:38:31 -0700429#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
rileya@google.com24f3ad12012-07-18 21:47:40 +0000430GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 switch (config) {
432 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000433 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000434 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000435 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000437 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000439 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000441 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 default:
reed@google.com2cb14802013-06-26 14:35:02 +0000443 // kNo_Config, kA1_Config missing
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000444 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000445 }
446}
reedc3b32662014-06-17 08:38:31 -0700447#endif
reed@google.combf790232013-12-13 19:45:58 +0000448
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000449// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
450// alpha info, that will be considered.
451GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) {
452 switch (ct) {
453 case kUnknown_SkColorType:
454 return kUnknown_GrPixelConfig;
455 case kAlpha_8_SkColorType:
456 return kAlpha_8_GrPixelConfig;
457 case kRGB_565_SkColorType:
458 return kRGB_565_GrPixelConfig;
459 case kARGB_4444_SkColorType:
460 return kRGBA_4444_GrPixelConfig;
461 case kRGBA_8888_SkColorType:
462 return kRGBA_8888_GrPixelConfig;
463 case kBGRA_8888_SkColorType:
464 return kBGRA_8888_GrPixelConfig;
465 case kIndex_8_SkColorType:
466 return kIndex_8_GrPixelConfig;
467 }
468 SkASSERT(0); // shouldn't get here
469 return kUnknown_GrPixelConfig;
470}
471
reed@google.combf790232013-12-13 19:45:58 +0000472bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
473 SkColorType ct;
474 switch (config) {
475 case kAlpha_8_GrPixelConfig:
476 ct = kAlpha_8_SkColorType;
477 break;
478 case kIndex_8_GrPixelConfig:
479 ct = kIndex_8_SkColorType;
480 break;
481 case kRGB_565_GrPixelConfig:
482 ct = kRGB_565_SkColorType;
483 break;
484 case kRGBA_4444_GrPixelConfig:
485 ct = kARGB_4444_SkColorType;
486 break;
487 case kRGBA_8888_GrPixelConfig:
488 ct = kRGBA_8888_SkColorType;
489 break;
490 case kBGRA_8888_GrPixelConfig:
491 ct = kBGRA_8888_SkColorType;
492 break;
493 default:
494 return false;
495 }
496 if (ctOut) {
497 *ctOut = ct;
498 }
499 return true;
500}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000501
502///////////////////////////////////////////////////////////////////////////////
503
bsalomon83d081a2014-07-08 09:56:10 -0700504void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000505 bool constantColor, GrPaint* grPaint) {
506
507 grPaint->setDither(skPaint.isDither());
508 grPaint->setAntiAlias(skPaint.isAntiAlias());
509
510 SkXfermode::Coeff sm;
511 SkXfermode::Coeff dm;
512
513 SkXfermode* mode = skPaint.getXfermode();
bsalomon83d081a2014-07-08 09:56:10 -0700514 GrEffect* xferEffect = NULL;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000515 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
516 if (NULL != xferEffect) {
517 grPaint->addColorEffect(xferEffect)->unref();
518 sm = SkXfermode::kOne_Coeff;
519 dm = SkXfermode::kZero_Coeff;
520 }
521 } else {
522 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
523 // Fall back to src-over
524 sm = SkXfermode::kOne_Coeff;
525 dm = SkXfermode::kISA_Coeff;
526 }
527 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
dandov9de5b512014-06-10 14:38:28 -0700528
529 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700530 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000531
532 SkColorFilter* colorFilter = skPaint.getColorFilter();
533 if (NULL != colorFilter) {
534 // if the source color is a constant then apply the filter here once rather than per pixel
535 // in a shader.
536 if (constantColor) {
537 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
538 grPaint->setColor(SkColor2GrColor(filtered));
539 } else {
bsalomon83d081a2014-07-08 09:56:10 -0700540 SkAutoTUnref<GrEffect> effect(colorFilter->asNewEffect(context));
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000541 if (NULL != effect.get()) {
542 grPaint->addColorEffect(effect);
543 }
544 }
545 }
krajcevskif461a8f2014-06-19 14:14:06 -0700546
547#ifndef SK_IGNORE_GPU_DITHER
548 // If the dither flag is set, then we need to see if the underlying context
549 // supports it. If not, then install a dither effect.
550 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
551 // What are we rendering into?
552 const GrRenderTarget *target = context->getRenderTarget();
553 SkASSERT(NULL != target);
554
555 // Suspect the dithering flag has no effect on these configs, otherwise
556 // fall back on setting the appropriate state.
557 if (target->config() == kRGBA_8888_GrPixelConfig ||
558 target->config() == kBGRA_8888_GrPixelConfig) {
559 // The dither flag is set and the target is likely
560 // not going to be dithered by the GPU.
bsalomon83d081a2014-07-08 09:56:10 -0700561 SkAutoTUnref<GrEffect> effect(GrDitherEffect::Create());
krajcevskif461a8f2014-06-19 14:14:06 -0700562 if (NULL != effect.get()) {
563 grPaint->addColorEffect(effect);
564 grPaint->setDither(false);
565 }
566 }
567 }
568#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000569}
570
commit-bot@chromium.org9e967ad2014-05-20 15:06:29 +0000571/**
572 * Unlike GrContext::AutoMatrix, this doesn't require setting a new matrix. GrContext::AutoMatrix
573 * likes to set the new matrix in its constructor because it is usually necessary to simulataneously
574 * update a GrPaint. This AutoMatrix is used while initially setting up GrPaint, however.
575 */
576class AutoMatrix {
577public:
578 AutoMatrix(GrContext* context) {
579 fMatrix = context->getMatrix();
580 fContext = context;
581 }
582 ~AutoMatrix() {
583 SkASSERT(NULL != fContext);
584 fContext->setMatrix(fMatrix);
585 }
586private:
587 GrContext* fContext;
588 SkMatrix fMatrix;
589};
590
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000591void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000592 bool constantColor, GrPaint* grPaint) {
593 SkShader* shader = skPaint.getShader();
594 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700595 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
596 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000597 return;
598 }
599
bsalomon83d081a2014-07-08 09:56:10 -0700600 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700601
602 // Start a new block here in order to preserve our context state after calling
603 // asNewEffect(). Since these calls get passed back to the client, we don't really
604 // want them messing around with the context.
605 {
bsalomon83d081a2014-07-08 09:56:10 -0700606 // SkShader::asNewEffect() may do offscreen rendering. Save off the current RT, clip, and
607 // matrix. We don't reset the matrix on the context because SkShader::asNewEffect may use
608 // GrContext::getMatrix() to know the transformation from local coords to device space.
krajcevskif461a8f2014-06-19 14:14:06 -0700609 GrContext::AutoRenderTarget art(context, NULL);
610 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
611 AutoMatrix am(context);
612
bsalomon83d081a2014-07-08 09:56:10 -0700613 // Allow the shader to modify paintColor and also create an effect to be installed as
614 // the first color effect on the GrPaint.
615 GrEffect* effect = NULL;
616 if (shader->asNewEffect(context, skPaint, NULL, &paintColor, &effect) && NULL != effect) {
617 grPaint->addColorEffect(effect)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700618 constantColor = false;
619 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000620 }
krajcevskif461a8f2014-06-19 14:14:06 -0700621
dandov9de5b512014-06-10 14:38:28 -0700622 // The grcolor is automatically set when calling asneweffect.
623 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
bsalomon83d081a2014-07-08 09:56:10 -0700624 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000625}