blob: 885b453df5f353c88b74ed9381241db01c4dc26a [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"
bsalomon37f9a262015-02-02 13:00:10 -080012#include "GrGpuResourceCacheAccess.h"
egdaniel378092f2014-12-03 10:40:13 -080013#include "GrXferProcessor.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000014#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000015#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070016#include "SkData.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000017#include "SkMessageBus.h"
18#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080019#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070020#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080021#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070022#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080023#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070024#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070025
krajcevski8c111f72014-06-02 13:51:34 -070026#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070027# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070028# include "etc1.h"
29#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31/* Fill out buffer with the compressed format Ganesh expects from a colortable
32 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000033
34 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000035 we could detect that the colortable.count is <= 16, and then repack the
36 indices as nibbles to save RAM, but it would take more time (i.e. a lot
37 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000038
reed@google.comac10a2d2010-12-22 21:39:39 +000039 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
40 as the colortable.count says it is.
41 */
bsalomone79a2da2014-10-24 12:42:51 -070042static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070043 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000044
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000045 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000046 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000047 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000048 return;
49 }
50
51 SkColorTable* ctable = bitmap.getColorTable();
52 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000053
reed@google.com7111d462014-03-25 16:20:24 +000054 const int count = ctable->count();
55
56 SkDstPixelInfo dstPI;
57 dstPI.fColorType = kRGBA_8888_SkColorType;
58 dstPI.fAlphaType = kPremul_SkAlphaType;
59 dstPI.fPixels = buffer;
60 dstPI.fRowBytes = count * sizeof(SkPMColor);
61
62 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000063 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000064 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080065 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000066 srcPI.fRowBytes = count * sizeof(SkPMColor);
67
68 srcPI.convertPixelsTo(&dstPI, count, 1);
69
reed@google.comac10a2d2010-12-22 21:39:39 +000070 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070071 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000072
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000073 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000074 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
75 } else {
76 // need to trim off the extra bytes per row
77 size_t width = bitmap.width();
78 size_t rowBytes = bitmap.rowBytes();
79 const char* src = (const char*)bitmap.getPixels();
80 for (int y = 0; y < bitmap.height(); y++) {
81 memcpy(dst, src, width);
82 src += rowBytes;
83 dst += width;
84 }
85 }
86}
87
88////////////////////////////////////////////////////////////////////////////////
89
bsalomon37f9a262015-02-02 13:00:10 -080090enum Stretch {
91 kNo_Stretch,
92 kBilerp_Stretch,
93 kNearest_Stretch
94};
95
96static Stretch get_stretch_type(const GrContext* ctx, int width, int height,
97 const GrTextureParams* params) {
98 if (params && params->isTiled()) {
99 const GrDrawTargetCaps* caps = ctx->getGpu()->caps();
100 if (!caps->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) {
101 switch(params->filterMode()) {
102 case GrTextureParams::kNone_FilterMode:
103 return kNearest_Stretch;
104 case GrTextureParams::kBilerp_FilterMode:
105 case GrTextureParams::kMipMap_FilterMode:
106 return kBilerp_Stretch;
107 }
108 }
109 }
110 return kNo_Stretch;
111}
112
113static bool make_resize_key(const GrContentKey& origKey, Stretch stretch, GrContentKey* resizeKey) {
114 if (origKey.isValid() && kNo_Stretch != stretch) {
115 static const GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
116 GrContentKey::Builder builder(resizeKey, origKey, kDomain, 1);
117 builder[0] = stretch;
118 builder.finish();
119 return true;
120 }
121 SkASSERT(!resizeKey->isValid());
122 return false;
123}
124
125static void generate_bitmap_keys(const SkBitmap& bitmap,
126 Stretch stretch,
127 GrContentKey* key,
128 GrContentKey* resizedKey) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000129 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
130 // are unique.
131 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000132 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800133 uint32_t width = SkToU16(bitmap.width());
134 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000135
bsalomon24db3b12015-01-23 04:24:04 -0800136 static const GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
137 GrContentKey::Builder builder(key, kDomain, 4);
138 builder[0] = genID;
139 builder[1] = origin.fX;
140 builder[2] = origin.fY;
141 builder[3] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800142 builder.finish();
143
144 if (kNo_Stretch != stretch) {
145 make_resize_key(*key, stretch, resizedKey);
146 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000147}
148
bsalomonf2703d82014-10-28 14:33:06 -0700149static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
150 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000151 desc->fWidth = bitmap.width();
152 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000153 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000154 desc->fSampleCnt = 0;
155}
156
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000157namespace {
158
159// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
160class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
161public:
bsalomon24db3b12015-01-23 04:24:04 -0800162 explicit GrResourceInvalidator(const GrContentKey& key) : fKey(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000163private:
bsalomon24db3b12015-01-23 04:24:04 -0800164 GrContentKey fKey;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000165
mtklein72c9faa2015-01-09 10:06:39 -0800166 void onChange() SK_OVERRIDE {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000167 const GrResourceInvalidatedMessage message = { fKey };
168 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
169 }
170};
171
172} // namespace
173
bsalomon37f9a262015-02-02 13:00:10 -0800174#if 0 // TODO: plug this back up
bsalomon24db3b12015-01-23 04:24:04 -0800175static void add_genID_listener(const GrContentKey& key, SkPixelRef* pixelRef) {
bsalomon49f085d2014-09-05 13:34:00 -0700176 SkASSERT(pixelRef);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000177 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
178}
bsalomon37f9a262015-02-02 13:00:10 -0800179#endif
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000180
bsalomon37f9a262015-02-02 13:00:10 -0800181static GrTexture* create_texture_for_bmp(GrContext* ctx,
182 const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700183 GrSurfaceDesc desc,
sugoi0249ec22014-09-09 08:12:34 -0700184 const void* pixels,
185 size_t rowBytes) {
186 GrTexture* result;
bsalomon37f9a262015-02-02 13:00:10 -0800187 if (optionalKey.isValid()) {
188 result = ctx->createTexture(desc, pixels, rowBytes);
bsalomone137db82015-01-31 20:10:56 -0800189 if (result) {
bsalomon37f9a262015-02-02 13:00:10 -0800190 SkAssertResult(ctx->addResourceToCache(optionalKey, result));
bsalomone137db82015-01-31 20:10:56 -0800191 }
bsalomon37f9a262015-02-02 13:00:10 -0800192 } else {
bsalomone3059732014-10-14 11:47:22 -0700193 result = ctx->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
bsalomon37f9a262015-02-02 13:00:10 -0800194 if (pixels && result) {
195 result->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, pixels, rowBytes);
sugoi0249ec22014-09-09 08:12:34 -0700196 }
197 }
198 return result;
199}
200
bsalomon37f9a262015-02-02 13:00:10 -0800201// creates a new texture that is the input texture scaled up to the next power of two in
202// width or height. If optionalKey is valid it will be set on the new texture. stretch
203// controls whether the scaling is done using nearest or bilerp filtering.
204GrTexture* resize_texture(GrTexture* inputTexture, Stretch stretch,
205 const GrContentKey& optionalKey) {
206 SkASSERT(kNo_Stretch != stretch);
207
208 GrContext* context = inputTexture->getContext();
209 SkASSERT(context);
210
211 // Either it's a cache miss or the original wasn't cached to begin with.
212 GrSurfaceDesc rtDesc = inputTexture->desc();
213 rtDesc.fFlags = rtDesc.fFlags |
214 kRenderTarget_GrSurfaceFlag |
215 kNoStencil_GrSurfaceFlag;
216 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
217 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
218 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
219
220 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
221 // fail.
222 if (!context->isConfigRenderable(rtDesc.fConfig, false)) {
223 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
224 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
225 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
226 } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
227 rtDesc.fConfig = kSkia8888_GrPixelConfig;
228 } else {
229 return NULL;
230 }
231 } else if (kRGB_GrColorComponentFlags ==
232 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
233 if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
234 rtDesc.fConfig = kSkia8888_GrPixelConfig;
235 } else {
236 return NULL;
237 }
238 } else {
239 return NULL;
240 }
241 }
242
243 GrTexture* resized = create_texture_for_bmp(context, optionalKey, rtDesc, NULL, 0);
244
245 if (!resized) {
246 return NULL;
247 }
248 GrPaint paint;
249
250 // If filtering is not desired then we want to ensure all texels in the resampled image are
251 // copies of texels from the original.
252 GrTextureParams params(SkShader::kClamp_TileMode,
253 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
254 GrTextureParams::kNone_FilterMode);
255 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
256
257 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
258 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
259
260 GrContext::AutoRenderTarget autoRT(context, resized->asRenderTarget());
261 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
262 context->drawNonAARectToRect(paint, SkMatrix::I(), rect, localRect);
263
264 return resized;
265}
266
krajcevski8c111f72014-06-02 13:51:34 -0700267#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon37f9a262015-02-02 13:00:10 -0800268static GrTexture *load_etc1_texture(GrContext* ctx, const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700269 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700270 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700271
272 // Is this even encoded data?
273 if (NULL == data) {
274 return NULL;
275 }
276
277 // Is this a valid PKM encoded data?
278 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700279 if (etc1_pkm_is_valid(bytes)) {
280 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
281 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
282
283 // Does the data match the dimensions of the bitmap? If not,
284 // then we don't know how to scale the image to match it...
285 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
286 encodedHeight != static_cast<uint32_t>(bm.height())) {
287 return NULL;
288 }
289
290 // Everything seems good... skip ahead to the data.
291 bytes += ETC_PKM_HEADER_SIZE;
292 desc.fConfig = kETC1_GrPixelConfig;
293 } else if (SkKTXFile::is_ktx(bytes)) {
294 SkKTXFile ktx(data);
295
296 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700297 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700298 return NULL;
299 }
300
301 // Does the data match the dimensions of the bitmap? If not,
302 // then we don't know how to scale the image to match it...
303 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
304 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800305 }
krajcevski99ffe242014-06-03 13:04:35 -0700306
307 bytes = ktx.pixelData();
308 desc.fConfig = kETC1_GrPixelConfig;
309 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700310 return NULL;
311 }
312
bsalomon37f9a262015-02-02 13:00:10 -0800313 return create_texture_for_bmp(ctx, optionalKey, desc, bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700314}
krajcevski8c111f72014-06-02 13:51:34 -0700315#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700316
bsalomon37f9a262015-02-02 13:00:10 -0800317static GrTexture* load_yuv_texture(GrContext* ctx, const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700318 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700319 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700320 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800321 if ((NULL == pixelRef) ||
322 (pixelRef->info().width() != bm.info().width()) ||
323 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700324 return NULL;
325 }
326
sugoi692135f2015-01-19 10:10:27 -0800327 SkYUVPlanesCache::Info yuvInfo;
328 SkAutoTUnref<SkCachedData> cachedData(
329 SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
330
sugoi518d83d2014-07-21 11:37:39 -0700331 void* planes[3];
sugoi4043d172015-01-19 10:31:35 -0800332 if (cachedData && cachedData->data()) {
sugoi692135f2015-01-19 10:10:27 -0800333 planes[0] = (void*)cachedData->data();
334 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
335 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
336 } else {
337 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
338 // rounded up to JPEG block size and be larger than the image's width and height.
339 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
340 return NULL;
341 }
sugoi518d83d2014-07-21 11:37:39 -0700342
sugoi692135f2015-01-19 10:10:27 -0800343 // Allocate the memory for YUV
344 size_t totalSize(0);
345 for (int i = 0; i < 3; ++i) {
346 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
347 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
348 totalSize += yuvInfo.fSizeInMemory[i];
349 }
350 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
351 planes[0] = cachedData->writable_data();
352 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
353 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700354
sugoi692135f2015-01-19 10:10:27 -0800355 // Get the YUV planes and update plane sizes to actual image size
356 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
357 &yuvInfo.fColorSpace)) {
358 return NULL;
359 }
360
361 // Decoding is done, cache the resulting YUV planes
362 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
sugoi518d83d2014-07-21 11:37:39 -0700363 }
364
bsalomonf2703d82014-10-28 14:33:06 -0700365 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700366 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700367 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700368 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800369 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
370 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
bsalomone3059732014-10-14 11:47:22 -0700371 yuvTextures[i].reset(
372 ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch));
373 if (!yuvTextures[i] ||
374 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800375 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700376 return NULL;
377 }
378 }
379
bsalomonf2703d82014-10-28 14:33:06 -0700380 GrSurfaceDesc rtDesc = desc;
sugoi518d83d2014-07-21 11:37:39 -0700381 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700382 kRenderTarget_GrSurfaceFlag |
383 kNoStencil_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700384
bsalomon37f9a262015-02-02 13:00:10 -0800385 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, NULL, 0);
386 if (!result) {
387 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700388 }
389
bsalomon37f9a262015-02-02 13:00:10 -0800390 GrRenderTarget* renderTarget = result->asRenderTarget();
391 SkASSERT(renderTarget);
392
393 SkAutoTUnref<GrFragmentProcessor>
394 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
395 yuvInfo.fColorSpace));
396 GrPaint paint;
397 paint.addColorProcessor(yuvToRgbProcessor);
398 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
399 SkIntToScalar(yuvInfo.fSize[0].fHeight));
400 GrContext::AutoRenderTarget autoRT(ctx, renderTarget);
401 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
402 ctx->drawRect(paint, SkMatrix::I(), r);
403
sugoi518d83d2014-07-21 11:37:39 -0700404 return result;
405}
406
bsalomon37f9a262015-02-02 13:00:10 -0800407static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
408 const SkBitmap& origBitmap,
409 const GrContentKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 SkBitmap tmpBitmap;
411
412 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000413
bsalomonf2703d82014-10-28 14:33:06 -0700414 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000415 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416
reed0689d7b2014-06-14 05:30:20 -0700417 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon37f9a262015-02-02 13:00:10 -0800418 if (ctx->supportsIndex8PixelConfig()) {
bsalomond4cb9222014-08-11 14:19:09 -0700419 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
420 bitmap->width(), bitmap->height());
421 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700422 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
424 // our compressed data will be trimmed, so pass width() for its
425 // "rowBytes", since they are the same now.
bsalomon37f9a262015-02-02 13:00:10 -0800426 return create_texture_for_bmp(ctx, optionalKey, desc, storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000428 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 // now bitmap points to our temp, which has been promoted to 32bits
430 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000431 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 }
krajcevski309e8692014-06-02 08:02:45 -0700433 }
krajcevski9c0e6292014-06-02 07:38:14 -0700434
435 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700436#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski9a3cdbb2014-06-05 07:03:39 -0700437 else if (
bungeman77cd8b02014-09-10 14:59:59 -0700438 // We do not support scratch ETC1 textures, hence they should all be at least
439 // trying to go to the cache.
bsalomon37f9a262015-02-02 13:00:10 -0800440 optionalKey.isValid()
krajcevski9a3cdbb2014-06-05 07:03:39 -0700441 // Make sure that the underlying device supports ETC1 textures before we go ahead
442 // and check the data.
bungeman77cd8b02014-09-10 14:59:59 -0700443 && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700444 // If the bitmap had compressed data and was then uncompressed, it'll still return
445 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
446 // the bitmap has available pixels, then they might not be what the decompressed
447 // data is.
448 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800449 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700450 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700451 return texture;
452 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453 }
krajcevski8c111f72014-06-02 13:51:34 -0700454#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
sugoi518d83d2014-07-21 11:37:39 -0700456 else {
bsalomon37f9a262015-02-02 13:00:10 -0800457 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700458 if (texture) {
sugoi518d83d2014-07-21 11:37:39 -0700459 return texture;
460 }
461 }
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000462 SkAutoLockPixels alp(*bitmap);
463 if (!bitmap->readyToDraw()) {
464 return NULL;
465 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000466
bsalomon37f9a262015-02-02 13:00:10 -0800467 return create_texture_for_bmp(ctx, optionalKey, desc, bitmap->getPixels(), bitmap->rowBytes());
468}
469
470static GrTexture* create_bitmap_texture(GrContext* ctx,
471 const SkBitmap& bmp,
472 Stretch stretch,
473 const GrContentKey& unstretchedKey,
474 const GrContentKey& stretchedKey) {
475 if (kNo_Stretch != stretch) {
476 SkAutoTUnref<GrTexture> unstretched;
477 // Check if we have the unstretched version in the cache, if not create it.
478 if (unstretchedKey.isValid()) {
479 unstretched.reset(ctx->findAndRefCachedTexture(unstretchedKey));
480 }
481 if (!unstretched) {
482 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
483 if (!unstretched) {
484 return NULL;
485 }
486 }
487 GrTexture* resized = resize_texture(unstretched, stretch, stretchedKey);
488 return resized;
489 }
490
491 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
492
reed@google.comac10a2d2010-12-22 21:39:39 +0000493}
494
bsalomon9ed7f572014-12-19 12:26:37 -0800495static GrTexture* get_texture_backing_bmp(const SkBitmap& bitmap, const GrContext* context,
496 const GrTextureParams* params) {
497 if (GrTexture* texture = bitmap.getTexture()) {
498 // Our texture-resizing-for-tiling used to upscale NPOT textures for tiling only works with
499 // content-key cached resources. Rather than invest in that legacy code path, we'll just
500 // take the horribly slow route of causing a cache miss which will cause the pixels to be
501 // read and reuploaded to a texture with a content key.
502 if (params && !context->getGpu()->caps()->npotTextureTileSupport() &&
503 (params->isTiled() || GrTextureParams::kMipMap_FilterMode == params->filterMode())) {
504 return NULL;
505 }
506 return texture;
507 }
508 return NULL;
509}
510
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000511bool GrIsBitmapInCache(const GrContext* ctx,
512 const SkBitmap& bitmap,
513 const GrTextureParams* params) {
bsalomon9ed7f572014-12-19 12:26:37 -0800514 if (get_texture_backing_bmp(bitmap, ctx, params)) {
515 return true;
516 }
517
bsalomon37f9a262015-02-02 13:00:10 -0800518 // We don't cache volatile bitmaps
519 if (bitmap.isVolatile()) {
520 return false;
521 }
522
523 // If it is inherently texture backed, consider it in the cache
524 if (bitmap.getTexture()) {
525 return true;
526 }
527
528 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
529 GrContentKey key, resizedKey;
530 generate_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000531
bsalomonf2703d82014-10-28 14:33:06 -0700532 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000533 generate_bitmap_texture_desc(bitmap, &desc);
bsalomon37f9a262015-02-02 13:00:10 -0800534 return ctx->isResourceInCache((kNo_Stretch == stretch) ? key : resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000535}
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomonbcf0a522014-10-08 08:40:09 -0700537GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
538 const SkBitmap& bitmap,
539 const GrTextureParams* params) {
bsalomon9ed7f572014-12-19 12:26:37 -0800540 GrTexture* result = get_texture_backing_bmp(bitmap, ctx, params);
541 if (result) {
542 return SkRef(result);
543 }
rileya@google.com24f3ad12012-07-18 21:47:40 +0000544
bsalomon37f9a262015-02-02 13:00:10 -0800545 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
546 GrContentKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000547
bsalomon37f9a262015-02-02 13:00:10 -0800548 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000549 // If the bitmap isn't changing try to find a cached copy first.
bsalomon37f9a262015-02-02 13:00:10 -0800550 generate_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000551
bsalomon37f9a262015-02-02 13:00:10 -0800552 result = ctx->findAndRefCachedTexture(resizedKey.isValid() ? resizedKey : key);
553 if (result) {
554 return result;
555 }
556 }
bsalomone137db82015-01-31 20:10:56 -0800557
bsalomon37f9a262015-02-02 13:00:10 -0800558 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
559 if (result) {
560 return result;
561 }
bsalomone137db82015-01-31 20:10:56 -0800562
bsalomon37f9a262015-02-02 13:00:10 -0800563 SkDebugf("---- failed to create texture for cache [%d %d]\n",
564 bitmap.width(), bitmap.height());
565
566 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000567}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000568///////////////////////////////////////////////////////////////////////////////
569
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000570// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
571// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800572GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000573 switch (ct) {
574 case kUnknown_SkColorType:
575 return kUnknown_GrPixelConfig;
576 case kAlpha_8_SkColorType:
577 return kAlpha_8_GrPixelConfig;
578 case kRGB_565_SkColorType:
579 return kRGB_565_GrPixelConfig;
580 case kARGB_4444_SkColorType:
581 return kRGBA_4444_GrPixelConfig;
582 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800583// if (kSRGB_SkColorProfileType == pt) {
584// return kSRGBA_8888_GrPixelConfig;
585// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000586 return kRGBA_8888_GrPixelConfig;
587 case kBGRA_8888_SkColorType:
588 return kBGRA_8888_GrPixelConfig;
589 case kIndex_8_SkColorType:
590 return kIndex_8_GrPixelConfig;
591 }
592 SkASSERT(0); // shouldn't get here
593 return kUnknown_GrPixelConfig;
594}
595
jvanverthfa1e8a72014-12-22 08:31:49 -0800596bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
597 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000598 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800599 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000600 switch (config) {
601 case kAlpha_8_GrPixelConfig:
602 ct = kAlpha_8_SkColorType;
603 break;
604 case kIndex_8_GrPixelConfig:
605 ct = kIndex_8_SkColorType;
606 break;
607 case kRGB_565_GrPixelConfig:
608 ct = kRGB_565_SkColorType;
609 break;
610 case kRGBA_4444_GrPixelConfig:
611 ct = kARGB_4444_SkColorType;
612 break;
613 case kRGBA_8888_GrPixelConfig:
614 ct = kRGBA_8888_SkColorType;
615 break;
616 case kBGRA_8888_GrPixelConfig:
617 ct = kBGRA_8888_SkColorType;
618 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800619 case kSRGBA_8888_GrPixelConfig:
620 ct = kRGBA_8888_SkColorType;
621 pt = kSRGB_SkColorProfileType;
622 break;
reed@google.combf790232013-12-13 19:45:58 +0000623 default:
624 return false;
625 }
626 if (ctOut) {
627 *ctOut = ct;
628 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800629 if (ptOut) {
630 *ptOut = pt;
631 }
reed@google.combf790232013-12-13 19:45:58 +0000632 return true;
633}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000634
635///////////////////////////////////////////////////////////////////////////////
636
bsalomon83d081a2014-07-08 09:56:10 -0700637void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000638 bool constantColor, GrPaint* grPaint) {
639
640 grPaint->setDither(skPaint.isDither());
641 grPaint->setAntiAlias(skPaint.isAntiAlias());
642
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000643 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800644 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800645 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000646 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800647 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000648 }
egdaniel378092f2014-12-03 10:40:13 -0800649 SkASSERT(xpFactory);
650 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800651
dandov9de5b512014-06-10 14:38:28 -0700652 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700653 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000654
655 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700656 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000657 // if the source color is a constant then apply the filter here once rather than per pixel
658 // in a shader.
659 if (constantColor) {
660 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
661 grPaint->setColor(SkColor2GrColor(filtered));
662 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700663 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
664 if (fp.get()) {
665 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000666 }
667 }
668 }
krajcevskif461a8f2014-06-19 14:14:06 -0700669
670#ifndef SK_IGNORE_GPU_DITHER
671 // If the dither flag is set, then we need to see if the underlying context
672 // supports it. If not, then install a dither effect.
673 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
674 // What are we rendering into?
675 const GrRenderTarget *target = context->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700676 SkASSERT(target);
krajcevskif461a8f2014-06-19 14:14:06 -0700677
678 // Suspect the dithering flag has no effect on these configs, otherwise
679 // fall back on setting the appropriate state.
680 if (target->config() == kRGBA_8888_GrPixelConfig ||
681 target->config() == kBGRA_8888_GrPixelConfig) {
682 // The dither flag is set and the target is likely
683 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700684 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
685 if (fp.get()) {
686 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700687 grPaint->setDither(false);
688 }
689 }
690 }
691#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000692}
693
joshualitt5531d512014-12-17 15:50:11 -0800694void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000695 bool constantColor, GrPaint* grPaint) {
696 SkShader* shader = skPaint.getShader();
697 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700698 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
699 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000700 return;
701 }
702
bsalomon83d081a2014-07-08 09:56:10 -0700703 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700704
705 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700706 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700707 // want them messing around with the context.
708 {
joshualittb0a8a372014-09-23 09:50:21 -0700709 // SkShader::asFragmentProcessor() may do offscreen rendering. Save off the current RT,
joshualitt5531d512014-12-17 15:50:11 -0800710 // and clip
krajcevskif461a8f2014-06-19 14:14:06 -0700711 GrContext::AutoRenderTarget art(context, NULL);
712 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
krajcevskif461a8f2014-06-19 14:14:06 -0700713
bsalomon83d081a2014-07-08 09:56:10 -0700714 // Allow the shader to modify paintColor and also create an effect to be installed as
715 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700716 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800717 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700718 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700719 constantColor = false;
720 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000721 }
krajcevskif461a8f2014-06-19 14:14:06 -0700722
joshualittb0a8a372014-09-23 09:50:21 -0700723 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700724 // 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 -0700725 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000726}