blob: 0a45100521232f1ac26dca6b5cac96a1ec6277ff [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"
krajcevski40a1e112014-08-05 14:13:36 -070014#include "SkTextureCompressor.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000015#include "GrResourceCache.h"
krajcevski9c0e6292014-06-02 07:38:14 -070016#include "GrGpu.h"
krajcevskif461a8f2014-06-19 14:14:06 -070017#include "effects/GrDitherEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070018#include "GrDrawTargetCaps.h"
sugoi518d83d2014-07-21 11:37:39 -070019#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070020
krajcevski8c111f72014-06-02 13:51:34 -070021#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070022# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070023# include "etc1.h"
24#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26/* Fill out buffer with the compressed format Ganesh expects from a colortable
27 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000028
29 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000030 we could detect that the colortable.count is <= 16, and then repack the
31 indices as nibbles to save RAM, but it would take more time (i.e. a lot
32 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
35 as the colortable.count says it is.
36 */
37static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070038 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000039
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000040 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000041 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000042 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000043 return;
44 }
45
46 SkColorTable* ctable = bitmap.getColorTable();
47 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000048
reed@google.com7111d462014-03-25 16:20:24 +000049 const int count = ctable->count();
50
51 SkDstPixelInfo dstPI;
52 dstPI.fColorType = kRGBA_8888_SkColorType;
53 dstPI.fAlphaType = kPremul_SkAlphaType;
54 dstPI.fPixels = buffer;
55 dstPI.fRowBytes = count * sizeof(SkPMColor);
56
57 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000058 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000059 srcPI.fAlphaType = kPremul_SkAlphaType;
60 srcPI.fPixels = ctable->lockColors();
61 srcPI.fRowBytes = count * sizeof(SkPMColor);
62
63 srcPI.convertPixelsTo(&dstPI, count, 1);
64
reed@google.com0a6151d2013-10-10 14:44:56 +000065 ctable->unlockColors();
bsalomon@google.com5782d712011-01-21 21:03:59 +000066
reed@google.comac10a2d2010-12-22 21:39:39 +000067 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000068 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000069
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000070 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000071 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.com0797c2c2012-12-20 15:13:01 +000087static 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.com672588b2014-01-08 15:42:01 +000091 SkIPoint origin = bitmap.pixelRefOrigin();
92 int16_t width = SkToS16(bitmap.width());
93 int16_t height = SkToS16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000094
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +000095 GrCacheID::Key key;
reed@google.com672588b2014-01-08 15:42:01 +000096 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.com10a9fb82013-01-02 19:29:57 +0000102 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
reed@google.com672588b2014-01-08 15:42:01 +0000103 GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000104 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
105 id->reset(gBitmapTextureDomain, key);
106}
107
108static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
109 desc->fFlags = kNone_GrTextureFlags;
110 desc->fWidth = bitmap.width();
111 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000112 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000113 desc->fSampleCnt = 0;
114}
115
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000116namespace {
117
118// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
119class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
120public:
121 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
122private:
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
133static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
134 SkASSERT(NULL != pixelRef);
135 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
136}
137
krajcevski8c111f72014-06-02 13:51:34 -0700138#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700139static GrTexture *load_etc1_texture(GrContext* ctx,
140 const GrTextureParams* params,
141 const SkBitmap &bm, GrTextureDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700142 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700143
144 // Is this even encoded data?
145 if (NULL == data) {
146 return NULL;
147 }
148
149 // Is this a valid PKM encoded data?
150 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700151 if (etc1_pkm_is_valid(bytes)) {
152 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
153 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
154
155 // Does the data match the dimensions of the bitmap? If not,
156 // then we don't know how to scale the image to match it...
157 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
158 encodedHeight != static_cast<uint32_t>(bm.height())) {
159 return NULL;
160 }
161
162 // Everything seems good... skip ahead to the data.
163 bytes += ETC_PKM_HEADER_SIZE;
164 desc.fConfig = kETC1_GrPixelConfig;
165 } else if (SkKTXFile::is_ktx(bytes)) {
166 SkKTXFile ktx(data);
167
168 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700169 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700170 return NULL;
171 }
172
173 // Does the data match the dimensions of the bitmap? If not,
174 // then we don't know how to scale the image to match it...
175 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
176 return NULL;
177 }
178
179 bytes = ktx.pixelData();
180 desc.fConfig = kETC1_GrPixelConfig;
181 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700182 return NULL;
183 }
184
krajcevski9c0e6292014-06-02 07:38:14 -0700185 // This texture is likely to be used again so leave it in the cache
186 GrCacheID cacheID;
187 generate_bitmap_cache_id(bm, &cacheID);
188
189 GrResourceKey key;
190 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key);
191 if (NULL != result) {
192 add_genID_listener(key, bm.pixelRef());
193 }
194 return result;
195}
krajcevski8c111f72014-06-02 13:51:34 -0700196#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700197
sugoi518d83d2014-07-21 11:37:39 -0700198static GrTexture *load_yuv_texture(GrContext* ctx, const GrTextureParams* params,
199 const SkBitmap& bm, const GrTextureDesc& desc) {
200 GrTexture* result = NULL;
201
202 SkPixelRef* pixelRef = bm.pixelRef();
203 SkISize yuvSizes[3];
204 if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL)) {
205 return NULL;
206 }
207
208 // Allocate the memory for YUV
209 size_t totalSize(0);
210 size_t sizes[3], rowBytes[3];
211 for (int i = 0; i < 3; ++i) {
212 rowBytes[i] = yuvSizes[i].fWidth;
213 totalSize += sizes[i] = rowBytes[i] * yuvSizes[i].fHeight;
214 }
215 SkAutoMalloc storage(totalSize);
216 void* planes[3];
217 planes[0] = storage.get();
218 planes[1] = (uint8_t*)planes[0] + sizes[0];
219 planes[2] = (uint8_t*)planes[1] + sizes[1];
220
221 // Get the YUV planes
222 if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes)) {
223 return NULL;
224 }
225
226 GrTextureDesc yuvDesc;
227 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
228 GrAutoScratchTexture yuvTextures[3];
229 for (int i = 0; i < 3; ++i) {
230 yuvDesc.fWidth = yuvSizes[i].fWidth;
231 yuvDesc.fHeight = yuvSizes[i].fHeight;
232 yuvTextures[i].set(ctx, yuvDesc);
233 if ((NULL == yuvTextures[i].texture()) ||
234 !ctx->writeTexturePixels(yuvTextures[i].texture(),
235 0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
236 yuvDesc.fConfig, planes[i], rowBytes[i])) {
237 return NULL;
238 }
239 }
240
241 GrTextureDesc rtDesc = desc;
242 rtDesc.fFlags = rtDesc.fFlags |
243 kRenderTarget_GrTextureFlagBit |
244 kNoStencil_GrTextureFlagBit;
245
246 // This texture is likely to be used again so leave it in the cache
247 GrCacheID cacheID;
248 generate_bitmap_cache_id(bm, &cacheID);
249
250 GrResourceKey key;
251 result = ctx->createTexture(params, rtDesc, cacheID, NULL, 0, &key);
252 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL;
253 if (NULL != renderTarget) {
254 add_genID_listener(key, bm.pixelRef());
255 SkAutoTUnref<GrEffect> yuvToRgbEffect(GrYUVtoRGBEffect::Create(
256 yuvTextures[0].texture(), yuvTextures[1].texture(), yuvTextures[2].texture()));
257 GrPaint paint;
258 paint.addColorEffect(yuvToRgbEffect);
259 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvSizes[0].fWidth),
260 SkIntToScalar(yuvSizes[0].fHeight));
261 GrContext::AutoRenderTarget autoRT(ctx, renderTarget);
262 GrContext::AutoMatrix am;
263 am.setIdentity(ctx);
264 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
265 ctx->drawRect(paint, r);
266 } else {
267 SkSafeSetNull(result);
268 }
269
270 return result;
271}
272
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000273static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000274 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000275 const GrTextureParams* params,
276 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000277 SkBitmap tmpBitmap;
278
279 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000280
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000281 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000282 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000283
reed0689d7b2014-06-14 05:30:20 -0700284 if (kIndex_8_SkColorType == bitmap->colorType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 // build_compressed_data doesn't do npot->pot expansion
286 // and paletted textures can't be sub-updated
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000287 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
288 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000290
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 build_compressed_data(storage.get(), origBitmap);
292
293 // our compressed data will be trimmed, so pass width() for its
294 // "rowBytes", since they are the same now.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000295
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000296 if (cache) {
297 GrCacheID cacheID;
298 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000299
300 GrResourceKey key;
301 GrTexture* result = ctx->createTexture(params, desc, cacheID,
302 storage.get(), bitmap->width(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000303 if (NULL != result) {
304 add_genID_listener(key, origBitmap.pixelRef());
305 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000306 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000307 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000308 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000309 GrContext::kExact_ScratchTexMatch);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000310 result->writePixels(0, 0, bitmap->width(),
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000311 bitmap->height(), desc.fConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000312 storage.get());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000313 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000314 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000316 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 // now bitmap points to our temp, which has been promoted to 32bits
318 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000319 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 }
krajcevski309e8692014-06-02 08:02:45 -0700321 }
krajcevski9c0e6292014-06-02 07:38:14 -0700322
323 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700324#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9a3cdbb2014-06-05 07:03:39 -0700325 else if (
326 // We do not support scratch ETC1 textures, hence they should all be at least
327 // trying to go to the cache.
328 cache
329 // Make sure that the underlying device supports ETC1 textures before we go ahead
330 // and check the data.
331 && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig)
332 // If the bitmap had compressed data and was then uncompressed, it'll still return
333 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
334 // the bitmap has available pixels, then they might not be what the decompressed
335 // data is.
336 && !(bitmap->readyToDraw())) {
krajcevski9c0e6292014-06-02 07:38:14 -0700337 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc);
338 if (NULL != texture) {
339 return texture;
340 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000341 }
krajcevski8c111f72014-06-02 13:51:34 -0700342#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
sugoi518d83d2014-07-21 11:37:39 -0700344 else {
345 GrTexture *texture = load_yuv_texture(ctx, params, *bitmap, desc);
346 if (NULL != texture) {
347 return texture;
348 }
349 }
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000350 SkAutoLockPixels alp(*bitmap);
351 if (!bitmap->readyToDraw()) {
352 return NULL;
353 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000354 if (cache) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000355 // This texture is likely to be used again so leave it in the cache
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000356 GrCacheID cacheID;
357 generate_bitmap_cache_id(origBitmap, &cacheID);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000358
359 GrResourceKey key;
360 GrTexture* result = ctx->createTexture(params, desc, cacheID,
361 bitmap->getPixels(), bitmap->rowBytes(), &key);
commit-bot@chromium.org3843f3f2013-10-31 20:22:47 +0000362 if (NULL != result) {
363 add_genID_listener(key, origBitmap.pixelRef());
364 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000365 return result;
366 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000367 // This texture is unlikely to be used again (in its present form) so
368 // just use a scratch texture. This will remove the texture from the
369 // cache so no one else can find it. Additionally, once unlocked, the
370 // scratch texture will go to the end of the list for purging so will
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000371 // likely be available for this volatile bitmap the next time around.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000372 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000373 result->writePixels(0, 0,
374 bitmap->width(), bitmap->height(),
375 desc.fConfig,
376 bitmap->getPixels(),
377 bitmap->rowBytes());
378 return result;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000379 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000380}
381
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000382bool GrIsBitmapInCache(const GrContext* ctx,
383 const SkBitmap& bitmap,
384 const GrTextureParams* params) {
385 GrCacheID cacheID;
386 generate_bitmap_cache_id(bitmap, &cacheID);
387
388 GrTextureDesc desc;
389 generate_bitmap_texture_desc(bitmap, &desc);
390 return ctx->isTextureInCache(desc, cacheID, params);
391}
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000393GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
394 const SkBitmap& bitmap,
395 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000396 GrTexture* result = NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000397
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000398 bool cache = !bitmap.isVolatile();
399
400 if (cache) {
401 // If the bitmap isn't changing try to find a cached copy first.
402
403 GrCacheID cacheID;
404 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000405
406 GrTextureDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000407 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000408
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000409 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000410 }
411 if (NULL == result) {
412 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000413 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000414 if (NULL == result) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000415 GrPrintf("---- failed to create texture for cache [%d %d]\n",
416 bitmap.width(), bitmap.height());
417 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000418 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000419}
420
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000421void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000422 SkASSERT(NULL != texture->getContext());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000423
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000424 texture->getContext()->unlockScratchTexture(texture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000425 texture->unref();
rileya@google.com24f3ad12012-07-18 21:47:40 +0000426}
427
428///////////////////////////////////////////////////////////////////////////////
429
reedc3b32662014-06-17 08:38:31 -0700430#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
rileya@google.com24f3ad12012-07-18 21:47:40 +0000431GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 switch (config) {
433 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000434 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000436 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000438 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000439 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000440 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000442 return kSkia8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 default:
reed@google.com2cb14802013-06-26 14:35:02 +0000444 // kNo_Config, kA1_Config missing
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000445 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 }
447}
reedc3b32662014-06-17 08:38:31 -0700448#endif
reed@google.combf790232013-12-13 19:45:58 +0000449
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000450// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
451// alpha info, that will be considered.
452GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) {
453 switch (ct) {
454 case kUnknown_SkColorType:
455 return kUnknown_GrPixelConfig;
456 case kAlpha_8_SkColorType:
457 return kAlpha_8_GrPixelConfig;
458 case kRGB_565_SkColorType:
459 return kRGB_565_GrPixelConfig;
460 case kARGB_4444_SkColorType:
461 return kRGBA_4444_GrPixelConfig;
462 case kRGBA_8888_SkColorType:
463 return kRGBA_8888_GrPixelConfig;
464 case kBGRA_8888_SkColorType:
465 return kBGRA_8888_GrPixelConfig;
466 case kIndex_8_SkColorType:
467 return kIndex_8_GrPixelConfig;
468 }
469 SkASSERT(0); // shouldn't get here
470 return kUnknown_GrPixelConfig;
471}
472
reed@google.combf790232013-12-13 19:45:58 +0000473bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
474 SkColorType ct;
475 switch (config) {
476 case kAlpha_8_GrPixelConfig:
477 ct = kAlpha_8_SkColorType;
478 break;
479 case kIndex_8_GrPixelConfig:
480 ct = kIndex_8_SkColorType;
481 break;
482 case kRGB_565_GrPixelConfig:
483 ct = kRGB_565_SkColorType;
484 break;
485 case kRGBA_4444_GrPixelConfig:
486 ct = kARGB_4444_SkColorType;
487 break;
488 case kRGBA_8888_GrPixelConfig:
489 ct = kRGBA_8888_SkColorType;
490 break;
491 case kBGRA_8888_GrPixelConfig:
492 ct = kBGRA_8888_SkColorType;
493 break;
494 default:
495 return false;
496 }
497 if (ctOut) {
498 *ctOut = ct;
499 }
500 return true;
501}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000502
503///////////////////////////////////////////////////////////////////////////////
504
bsalomon83d081a2014-07-08 09:56:10 -0700505void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000506 bool constantColor, GrPaint* grPaint) {
507
508 grPaint->setDither(skPaint.isDither());
509 grPaint->setAntiAlias(skPaint.isAntiAlias());
510
511 SkXfermode::Coeff sm;
512 SkXfermode::Coeff dm;
513
514 SkXfermode* mode = skPaint.getXfermode();
bsalomon83d081a2014-07-08 09:56:10 -0700515 GrEffect* xferEffect = NULL;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000516 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
517 if (NULL != xferEffect) {
518 grPaint->addColorEffect(xferEffect)->unref();
519 sm = SkXfermode::kOne_Coeff;
520 dm = SkXfermode::kZero_Coeff;
521 }
522 } else {
523 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
524 // Fall back to src-over
525 sm = SkXfermode::kOne_Coeff;
526 dm = SkXfermode::kISA_Coeff;
527 }
528 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
dandov9de5b512014-06-10 14:38:28 -0700529
530 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700531 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000532
533 SkColorFilter* colorFilter = skPaint.getColorFilter();
534 if (NULL != colorFilter) {
535 // if the source color is a constant then apply the filter here once rather than per pixel
536 // in a shader.
537 if (constantColor) {
538 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
539 grPaint->setColor(SkColor2GrColor(filtered));
540 } else {
bsalomon83d081a2014-07-08 09:56:10 -0700541 SkAutoTUnref<GrEffect> effect(colorFilter->asNewEffect(context));
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000542 if (NULL != effect.get()) {
543 grPaint->addColorEffect(effect);
544 }
545 }
546 }
krajcevskif461a8f2014-06-19 14:14:06 -0700547
548#ifndef SK_IGNORE_GPU_DITHER
549 // If the dither flag is set, then we need to see if the underlying context
550 // supports it. If not, then install a dither effect.
551 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
552 // What are we rendering into?
553 const GrRenderTarget *target = context->getRenderTarget();
554 SkASSERT(NULL != target);
555
556 // Suspect the dithering flag has no effect on these configs, otherwise
557 // fall back on setting the appropriate state.
558 if (target->config() == kRGBA_8888_GrPixelConfig ||
559 target->config() == kBGRA_8888_GrPixelConfig) {
560 // The dither flag is set and the target is likely
561 // not going to be dithered by the GPU.
bsalomon83d081a2014-07-08 09:56:10 -0700562 SkAutoTUnref<GrEffect> effect(GrDitherEffect::Create());
krajcevskif461a8f2014-06-19 14:14:06 -0700563 if (NULL != effect.get()) {
564 grPaint->addColorEffect(effect);
565 grPaint->setDither(false);
566 }
567 }
568 }
569#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000570}
571
commit-bot@chromium.org9e967ad2014-05-20 15:06:29 +0000572/**
573 * Unlike GrContext::AutoMatrix, this doesn't require setting a new matrix. GrContext::AutoMatrix
574 * likes to set the new matrix in its constructor because it is usually necessary to simulataneously
575 * update a GrPaint. This AutoMatrix is used while initially setting up GrPaint, however.
576 */
577class AutoMatrix {
578public:
579 AutoMatrix(GrContext* context) {
580 fMatrix = context->getMatrix();
581 fContext = context;
582 }
583 ~AutoMatrix() {
584 SkASSERT(NULL != fContext);
585 fContext->setMatrix(fMatrix);
586 }
587private:
588 GrContext* fContext;
589 SkMatrix fMatrix;
590};
591
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000592void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000593 bool constantColor, GrPaint* grPaint) {
594 SkShader* shader = skPaint.getShader();
595 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700596 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
597 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000598 return;
599 }
600
bsalomon83d081a2014-07-08 09:56:10 -0700601 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700602
603 // Start a new block here in order to preserve our context state after calling
604 // asNewEffect(). Since these calls get passed back to the client, we don't really
605 // want them messing around with the context.
606 {
bsalomon83d081a2014-07-08 09:56:10 -0700607 // SkShader::asNewEffect() may do offscreen rendering. Save off the current RT, clip, and
608 // matrix. We don't reset the matrix on the context because SkShader::asNewEffect may use
609 // GrContext::getMatrix() to know the transformation from local coords to device space.
krajcevskif461a8f2014-06-19 14:14:06 -0700610 GrContext::AutoRenderTarget art(context, NULL);
611 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
612 AutoMatrix am(context);
613
bsalomon83d081a2014-07-08 09:56:10 -0700614 // Allow the shader to modify paintColor and also create an effect to be installed as
615 // the first color effect on the GrPaint.
616 GrEffect* effect = NULL;
617 if (shader->asNewEffect(context, skPaint, NULL, &paintColor, &effect) && NULL != effect) {
618 grPaint->addColorEffect(effect)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700619 constantColor = false;
620 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000621 }
krajcevskif461a8f2014-06-19 14:14:06 -0700622
dandov9de5b512014-06-10 14:38:28 -0700623 // The grcolor is automatically set when calling asneweffect.
624 // 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 -0700625 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000626}