blob: 49d3c6bb48ddb2017807dec8128cd8ca221b507b [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"
egdaniel378092f2014-12-03 10:40:13 -08009
10#include "GrDrawTargetCaps.h"
11#include "GrGpu.h"
12#include "GrXferProcessor.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000013#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000014#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070015#include "SkData.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000016#include "SkMessageBus.h"
17#include "SkPixelRef.h"
krajcevski40a1e112014-08-05 14:13:36 -070018#include "SkTextureCompressor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070019#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080020#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070021#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070022
krajcevski8c111f72014-06-02 13:51:34 -070023#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070024# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070025# include "etc1.h"
26#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28/* Fill out buffer with the compressed format Ganesh expects from a colortable
29 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000030
31 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000032 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.com5782d712011-01-21 21:03:59 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 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 */
bsalomone79a2da2014-10-24 12:42:51 -070039static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070040 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000041
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000042 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000043 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000044 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000045 return;
46 }
47
48 SkColorTable* ctable = bitmap.getColorTable();
49 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000050
reed@google.com7111d462014-03-25 16:20:24 +000051 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.org28fcae22014-04-11 17:15:40 +000060 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000061 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080062 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000063 srcPI.fRowBytes = count * sizeof(SkPMColor);
64
65 srcPI.convertPixelsTo(&dstPI, count, 1);
66
reed@google.comac10a2d2010-12-22 21:39:39 +000067 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070068 dst += 256 * sizeof(GrColor);
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
bsalomonf2703d82014-10-28 14:33:06 -0700108static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
109 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000110 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
mtklein72c9faa2015-01-09 10:06:39 -0800125 void onChange() SK_OVERRIDE {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000126 const GrResourceInvalidatedMessage message = { fKey };
127 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
128 }
129};
130
131} // namespace
132
133static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
bsalomon49f085d2014-09-05 13:34:00 -0700134 SkASSERT(pixelRef);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000135 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
136}
137
sugoi0249ec22014-09-09 08:12:34 -0700138static GrTexture* sk_gr_allocate_texture(GrContext* ctx,
139 bool cache,
140 const GrTextureParams* params,
141 const SkBitmap& bm,
bsalomonf2703d82014-10-28 14:33:06 -0700142 GrSurfaceDesc desc,
sugoi0249ec22014-09-09 08:12:34 -0700143 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.
bsalomone3059732014-10-14 11:47:22 -0700162 result = ctx->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
sugoi0249ec22014-09-09 08:12:34 -0700163 if (pixels) {
164 result->writePixels(0, 0, bm.width(), bm.height(), desc.fConfig, pixels, rowBytes);
165 }
166 }
167 return result;
168}
169
krajcevski8c111f72014-06-02 13:51:34 -0700170#ifndef SK_IGNORE_ETC1_SUPPORT
sugoi0249ec22014-09-09 08:12:34 -0700171static GrTexture *load_etc1_texture(GrContext* ctx, bool cache,
krajcevski9c0e6292014-06-02 07:38:14 -0700172 const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700173 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700174 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700175
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();
krajcevski99ffe242014-06-03 13:04:35 -0700183 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?
krajcevski40a1e112014-08-05 14:13:36 -0700201 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700202 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;
mtklein775b8192014-12-02 09:11:25 -0800209 }
krajcevski99ffe242014-06-03 13:04:35 -0700210
211 bytes = ktx.pixelData();
212 desc.fConfig = kETC1_GrPixelConfig;
213 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700214 return NULL;
215 }
216
sugoi0249ec22014-09-09 08:12:34 -0700217 return sk_gr_allocate_texture(ctx, cache, params, bm, desc, bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700218}
krajcevski8c111f72014-06-02 13:51:34 -0700219#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700220
sugoi0249ec22014-09-09 08:12:34 -0700221static GrTexture *load_yuv_texture(GrContext* ctx, bool cache, const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700222 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700223 // 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
sugoi518d83d2014-07-21 11:37:39 -0700229 SkPixelRef* pixelRef = bm.pixelRef();
230 SkISize yuvSizes[3];
rileyaabaef862014-09-12 17:45:58 -0700231 if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL, NULL)) {
sugoi518d83d2014-07-21 11:37:39 -0700232 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
rileyaabaef862014-09-12 17:45:58 -0700248 SkYUVColorSpace colorSpace;
249
sugoi518d83d2014-07-21 11:37:39 -0700250 // Get the YUV planes
rileyaabaef862014-09-12 17:45:58 -0700251 if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes, &colorSpace)) {
sugoi518d83d2014-07-21 11:37:39 -0700252 return NULL;
253 }
254
bsalomonf2703d82014-10-28 14:33:06 -0700255 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700256 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700257 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700258 for (int i = 0; i < 3; ++i) {
259 yuvDesc.fWidth = yuvSizes[i].fWidth;
260 yuvDesc.fHeight = yuvSizes[i].fHeight;
bsalomone3059732014-10-14 11:47:22 -0700261 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])) {
sugoi518d83d2014-07-21 11:37:39 -0700266 return NULL;
267 }
268 }
269
bsalomonf2703d82014-10-28 14:33:06 -0700270 GrSurfaceDesc rtDesc = desc;
sugoi518d83d2014-07-21 11:37:39 -0700271 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700272 kRenderTarget_GrSurfaceFlag |
273 kNoStencil_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700274
sugoi0249ec22014-09-09 08:12:34 -0700275 GrTexture* result = sk_gr_allocate_texture(ctx, cache, params, bm, rtDesc, NULL, 0);
sugoi518d83d2014-07-21 11:37:39 -0700276
sugoi518d83d2014-07-21 11:37:39 -0700277 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700278 if (renderTarget) {
bsalomone3059732014-10-14 11:47:22 -0700279 SkAutoTUnref<GrFragmentProcessor> yuvToRgbProcessor(
280 GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2], colorSpace));
sugoi518d83d2014-07-21 11:37:39 -0700281 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -0700282 paint.addColorProcessor(yuvToRgbProcessor);
sugoi518d83d2014-07-21 11:37:39 -0700283 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvSizes[0].fWidth),
284 SkIntToScalar(yuvSizes[0].fHeight));
285 GrContext::AutoRenderTarget autoRT(ctx, renderTarget);
sugoi518d83d2014-07-21 11:37:39 -0700286 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
joshualitt5531d512014-12-17 15:50:11 -0800287 ctx->drawRect(paint, SkMatrix::I(), r);
sugoi518d83d2014-07-21 11:37:39 -0700288 } else {
289 SkSafeSetNull(result);
290 }
291
292 return result;
293}
294
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000295static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000296 bool cache,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000297 const GrTextureParams* params,
298 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 SkBitmap tmpBitmap;
300
301 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000302
bsalomonf2703d82014-10-28 14:33:06 -0700303 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000304 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000305
reed0689d7b2014-06-14 05:30:20 -0700306 if (kIndex_8_SkColorType == bitmap->colorType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 // build_compressed_data doesn't do npot->pot expansion
308 // and paletted textures can't be sub-updated
bsalomone79a2da2014-10-24 12:42:51 -0700309 if (cache && ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
bsalomond4cb9222014-08-11 14:19:09 -0700310 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
311 bitmap->width(), bitmap->height());
312 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700313 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
315 // our compressed data will be trimmed, so pass width() for its
316 // "rowBytes", since they are the same now.
sugoi0249ec22014-09-09 08:12:34 -0700317 return sk_gr_allocate_texture(ctx, cache, params, origBitmap,
318 desc, storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000320 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 // now bitmap points to our temp, which has been promoted to 32bits
322 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000323 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 }
krajcevski309e8692014-06-02 08:02:45 -0700325 }
krajcevski9c0e6292014-06-02 07:38:14 -0700326
327 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700328#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9a3cdbb2014-06-05 07:03:39 -0700329 else if (
bungeman77cd8b02014-09-10 14:59:59 -0700330 // We do not support scratch ETC1 textures, hence they should all be at least
331 // trying to go to the cache.
332 cache
krajcevski9a3cdbb2014-06-05 07:03:39 -0700333 // Make sure that the underlying device supports ETC1 textures before we go ahead
334 // and check the data.
bungeman77cd8b02014-09-10 14:59:59 -0700335 && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700336 // If the bitmap had compressed data and was then uncompressed, it'll still return
337 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
338 // the bitmap has available pixels, then they might not be what the decompressed
339 // data is.
340 && !(bitmap->readyToDraw())) {
sugoi0249ec22014-09-09 08:12:34 -0700341 GrTexture *texture = load_etc1_texture(ctx, cache, params, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700342 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700343 return texture;
344 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000345 }
krajcevski8c111f72014-06-02 13:51:34 -0700346#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000347
sugoi518d83d2014-07-21 11:37:39 -0700348 else {
sugoi0249ec22014-09-09 08:12:34 -0700349 GrTexture *texture = load_yuv_texture(ctx, cache, params, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700350 if (texture) {
sugoi518d83d2014-07-21 11:37:39 -0700351 return texture;
352 }
353 }
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000354 SkAutoLockPixels alp(*bitmap);
355 if (!bitmap->readyToDraw()) {
356 return NULL;
357 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000358
sugoi0249ec22014-09-09 08:12:34 -0700359 return sk_gr_allocate_texture(ctx, cache, params, origBitmap, desc,
360 bitmap->getPixels(), bitmap->rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000361}
362
bsalomon9ed7f572014-12-19 12:26:37 -0800363static GrTexture* get_texture_backing_bmp(const SkBitmap& bitmap, const GrContext* context,
364 const GrTextureParams* params) {
365 if (GrTexture* texture = bitmap.getTexture()) {
366 // Our texture-resizing-for-tiling used to upscale NPOT textures for tiling only works with
367 // content-key cached resources. Rather than invest in that legacy code path, we'll just
368 // take the horribly slow route of causing a cache miss which will cause the pixels to be
369 // read and reuploaded to a texture with a content key.
370 if (params && !context->getGpu()->caps()->npotTextureTileSupport() &&
371 (params->isTiled() || GrTextureParams::kMipMap_FilterMode == params->filterMode())) {
372 return NULL;
373 }
374 return texture;
375 }
376 return NULL;
377}
378
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000379bool GrIsBitmapInCache(const GrContext* ctx,
380 const SkBitmap& bitmap,
381 const GrTextureParams* params) {
bsalomon9ed7f572014-12-19 12:26:37 -0800382 if (get_texture_backing_bmp(bitmap, ctx, params)) {
383 return true;
384 }
385
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000386 GrCacheID cacheID;
387 generate_bitmap_cache_id(bitmap, &cacheID);
388
bsalomonf2703d82014-10-28 14:33:06 -0700389 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000390 generate_bitmap_texture_desc(bitmap, &desc);
391 return ctx->isTextureInCache(desc, cacheID, params);
392}
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
bsalomonbcf0a522014-10-08 08:40:09 -0700394GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
395 const SkBitmap& bitmap,
396 const GrTextureParams* params) {
bsalomon9ed7f572014-12-19 12:26:37 -0800397 GrTexture* result = get_texture_backing_bmp(bitmap, ctx, params);
398 if (result) {
399 return SkRef(result);
400 }
rileya@google.com24f3ad12012-07-18 21:47:40 +0000401
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000402 bool cache = !bitmap.isVolatile();
403
404 if (cache) {
405 // If the bitmap isn't changing try to find a cached copy first.
406
407 GrCacheID cacheID;
408 generate_bitmap_cache_id(bitmap, &cacheID);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000409
bsalomonf2703d82014-10-28 14:33:06 -0700410 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000411 generate_bitmap_texture_desc(bitmap, &desc);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000412
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000413 result = ctx->findAndRefTexture(desc, cacheID, params);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000414 }
415 if (NULL == result) {
416 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000417 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000418 if (NULL == result) {
tfarina38406c82014-10-31 07:11:12 -0700419 SkDebugf("---- failed to create texture for cache [%d %d]\n",
420 bitmap.width(), bitmap.height());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000421 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000422 return result;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000423}
424
rileya@google.com24f3ad12012-07-18 21:47:40 +0000425///////////////////////////////////////////////////////////////////////////////
426
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000427// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
428// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800429GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000430 switch (ct) {
431 case kUnknown_SkColorType:
432 return kUnknown_GrPixelConfig;
433 case kAlpha_8_SkColorType:
434 return kAlpha_8_GrPixelConfig;
435 case kRGB_565_SkColorType:
436 return kRGB_565_GrPixelConfig;
437 case kARGB_4444_SkColorType:
438 return kRGBA_4444_GrPixelConfig;
439 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800440// if (kSRGB_SkColorProfileType == pt) {
441// return kSRGBA_8888_GrPixelConfig;
442// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000443 return kRGBA_8888_GrPixelConfig;
444 case kBGRA_8888_SkColorType:
445 return kBGRA_8888_GrPixelConfig;
446 case kIndex_8_SkColorType:
447 return kIndex_8_GrPixelConfig;
448 }
449 SkASSERT(0); // shouldn't get here
450 return kUnknown_GrPixelConfig;
451}
452
jvanverthfa1e8a72014-12-22 08:31:49 -0800453bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
454 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000455 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800456 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000457 switch (config) {
458 case kAlpha_8_GrPixelConfig:
459 ct = kAlpha_8_SkColorType;
460 break;
461 case kIndex_8_GrPixelConfig:
462 ct = kIndex_8_SkColorType;
463 break;
464 case kRGB_565_GrPixelConfig:
465 ct = kRGB_565_SkColorType;
466 break;
467 case kRGBA_4444_GrPixelConfig:
468 ct = kARGB_4444_SkColorType;
469 break;
470 case kRGBA_8888_GrPixelConfig:
471 ct = kRGBA_8888_SkColorType;
472 break;
473 case kBGRA_8888_GrPixelConfig:
474 ct = kBGRA_8888_SkColorType;
475 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800476 case kSRGBA_8888_GrPixelConfig:
477 ct = kRGBA_8888_SkColorType;
478 pt = kSRGB_SkColorProfileType;
479 break;
reed@google.combf790232013-12-13 19:45:58 +0000480 default:
481 return false;
482 }
483 if (ctOut) {
484 *ctOut = ct;
485 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800486 if (ptOut) {
487 *ptOut = pt;
488 }
reed@google.combf790232013-12-13 19:45:58 +0000489 return true;
490}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000491
492///////////////////////////////////////////////////////////////////////////////
493
bsalomon83d081a2014-07-08 09:56:10 -0700494void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000495 bool constantColor, GrPaint* grPaint) {
496
497 grPaint->setDither(skPaint.isDither());
498 grPaint->setAntiAlias(skPaint.isAntiAlias());
499
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000500 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800501 GrFragmentProcessor* fragmentProcessor = NULL;
502 GrXPFactory* xpFactory = NULL;
egdaniel95131432014-12-09 11:15:43 -0800503 if (SkXfermode::AsFragmentProcessorOrXPFactory(mode, &fragmentProcessor, &xpFactory)) {
egdaniel378092f2014-12-03 10:40:13 -0800504 if (fragmentProcessor) {
505 SkASSERT(NULL == xpFactory);
506 grPaint->addColorProcessor(fragmentProcessor)->unref();
egdanielc016fb82014-12-03 11:41:54 -0800507 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000508 }
509 } else {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000510 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800511 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000512 }
egdaniel378092f2014-12-03 10:40:13 -0800513 SkASSERT(xpFactory);
514 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800515
dandov9de5b512014-06-10 14:38:28 -0700516 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700517 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000518
519 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700520 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000521 // if the source color is a constant then apply the filter here once rather than per pixel
522 // in a shader.
523 if (constantColor) {
524 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
525 grPaint->setColor(SkColor2GrColor(filtered));
526 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700527 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
528 if (fp.get()) {
529 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000530 }
531 }
532 }
krajcevskif461a8f2014-06-19 14:14:06 -0700533
534#ifndef SK_IGNORE_GPU_DITHER
535 // If the dither flag is set, then we need to see if the underlying context
536 // supports it. If not, then install a dither effect.
537 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
538 // What are we rendering into?
539 const GrRenderTarget *target = context->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700540 SkASSERT(target);
krajcevskif461a8f2014-06-19 14:14:06 -0700541
542 // Suspect the dithering flag has no effect on these configs, otherwise
543 // fall back on setting the appropriate state.
544 if (target->config() == kRGBA_8888_GrPixelConfig ||
545 target->config() == kBGRA_8888_GrPixelConfig) {
546 // The dither flag is set and the target is likely
547 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700548 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
549 if (fp.get()) {
550 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700551 grPaint->setDither(false);
552 }
553 }
554 }
555#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000556}
557
joshualitt5531d512014-12-17 15:50:11 -0800558void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000559 bool constantColor, GrPaint* grPaint) {
560 SkShader* shader = skPaint.getShader();
561 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700562 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
563 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000564 return;
565 }
566
bsalomon83d081a2014-07-08 09:56:10 -0700567 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700568
569 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700570 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700571 // want them messing around with the context.
572 {
joshualittb0a8a372014-09-23 09:50:21 -0700573 // SkShader::asFragmentProcessor() may do offscreen rendering. Save off the current RT,
joshualitt5531d512014-12-17 15:50:11 -0800574 // and clip
krajcevskif461a8f2014-06-19 14:14:06 -0700575 GrContext::AutoRenderTarget art(context, NULL);
576 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
krajcevskif461a8f2014-06-19 14:14:06 -0700577
bsalomon83d081a2014-07-08 09:56:10 -0700578 // Allow the shader to modify paintColor and also create an effect to be installed as
579 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700580 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800581 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700582 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700583 constantColor = false;
584 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000585 }
krajcevskif461a8f2014-06-19 14:14:06 -0700586
joshualittb0a8a372014-09-23 09:50:21 -0700587 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700588 // 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 -0700589 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000590}