blob: 2209ea465afd8278875756c33c04217eb0834ee0 [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
egdaniel378092f2014-12-03 10:40:13 -080010#include "GrXferProcessor.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000011#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000012#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070013#include "SkData.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000014#include "SkMessageBus.h"
15#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080016#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070017#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080018#include "SkYUVPlanesCache.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
bsalomon37f9a262015-02-02 13:00:10 -080087enum Stretch {
88 kNo_Stretch,
89 kBilerp_Stretch,
90 kNearest_Stretch
91};
92
93static Stretch get_stretch_type(const GrContext* ctx, int width, int height,
94 const GrTextureParams* params) {
95 if (params && params->isTiled()) {
bsalomond2a6f4e2015-02-04 10:55:54 -080096 if (!ctx->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon37f9a262015-02-02 13:00:10 -080097 switch(params->filterMode()) {
98 case GrTextureParams::kNone_FilterMode:
99 return kNearest_Stretch;
100 case GrTextureParams::kBilerp_FilterMode:
101 case GrTextureParams::kMipMap_FilterMode:
102 return kBilerp_Stretch;
103 }
104 }
105 }
106 return kNo_Stretch;
107}
108
109static bool make_resize_key(const GrContentKey& origKey, Stretch stretch, GrContentKey* resizeKey) {
110 if (origKey.isValid() && kNo_Stretch != stretch) {
111 static const GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
112 GrContentKey::Builder builder(resizeKey, origKey, kDomain, 1);
113 builder[0] = stretch;
114 builder.finish();
115 return true;
116 }
117 SkASSERT(!resizeKey->isValid());
118 return false;
119}
120
121static void generate_bitmap_keys(const SkBitmap& bitmap,
122 Stretch stretch,
123 GrContentKey* key,
124 GrContentKey* resizedKey) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000125 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
126 // are unique.
127 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000128 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800129 uint32_t width = SkToU16(bitmap.width());
130 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000131
bsalomon24db3b12015-01-23 04:24:04 -0800132 static const GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
133 GrContentKey::Builder builder(key, kDomain, 4);
134 builder[0] = genID;
135 builder[1] = origin.fX;
136 builder[2] = origin.fY;
137 builder[3] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800138 builder.finish();
139
140 if (kNo_Stretch != stretch) {
141 make_resize_key(*key, stretch, resizedKey);
142 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000143}
144
bsalomonf2703d82014-10-28 14:33:06 -0700145static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
146 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000147 desc->fWidth = bitmap.width();
148 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000149 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000150 desc->fSampleCnt = 0;
151}
152
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000153namespace {
154
155// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
156class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
157public:
bsalomon24db3b12015-01-23 04:24:04 -0800158 explicit GrResourceInvalidator(const GrContentKey& key) : fKey(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000159private:
bsalomon24db3b12015-01-23 04:24:04 -0800160 GrContentKey fKey;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000161
mtklein72c9faa2015-01-09 10:06:39 -0800162 void onChange() SK_OVERRIDE {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000163 const GrResourceInvalidatedMessage message = { fKey };
164 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
165 }
166};
167
168} // namespace
169
bsalomon37f9a262015-02-02 13:00:10 -0800170#if 0 // TODO: plug this back up
bsalomon24db3b12015-01-23 04:24:04 -0800171static void add_genID_listener(const GrContentKey& key, SkPixelRef* pixelRef) {
bsalomon49f085d2014-09-05 13:34:00 -0700172 SkASSERT(pixelRef);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000173 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
174}
bsalomon37f9a262015-02-02 13:00:10 -0800175#endif
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000176
bsalomon37f9a262015-02-02 13:00:10 -0800177static GrTexture* create_texture_for_bmp(GrContext* ctx,
178 const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700179 GrSurfaceDesc desc,
sugoi0249ec22014-09-09 08:12:34 -0700180 const void* pixels,
181 size_t rowBytes) {
bsalomond0423582015-02-06 08:49:24 -0800182 GrTexture* result = ctx->createTexture(desc, true, pixels, rowBytes);
183 if (result && optionalKey.isValid()) {
184 SkAssertResult(ctx->addResourceToCache(optionalKey, result));
sugoi0249ec22014-09-09 08:12:34 -0700185 }
186 return result;
187}
188
bsalomon37f9a262015-02-02 13:00:10 -0800189// creates a new texture that is the input texture scaled up to the next power of two in
190// width or height. If optionalKey is valid it will be set on the new texture. stretch
191// controls whether the scaling is done using nearest or bilerp filtering.
192GrTexture* resize_texture(GrTexture* inputTexture, Stretch stretch,
193 const GrContentKey& optionalKey) {
194 SkASSERT(kNo_Stretch != stretch);
195
196 GrContext* context = inputTexture->getContext();
197 SkASSERT(context);
198
199 // Either it's a cache miss or the original wasn't cached to begin with.
200 GrSurfaceDesc rtDesc = inputTexture->desc();
201 rtDesc.fFlags = rtDesc.fFlags |
202 kRenderTarget_GrSurfaceFlag |
203 kNoStencil_GrSurfaceFlag;
204 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
205 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
206 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
207
208 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
209 // fail.
210 if (!context->isConfigRenderable(rtDesc.fConfig, false)) {
211 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
212 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
213 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
214 } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
215 rtDesc.fConfig = kSkia8888_GrPixelConfig;
216 } else {
217 return NULL;
218 }
219 } else if (kRGB_GrColorComponentFlags ==
220 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
221 if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
222 rtDesc.fConfig = kSkia8888_GrPixelConfig;
223 } else {
224 return NULL;
225 }
226 } else {
227 return NULL;
228 }
229 }
230
231 GrTexture* resized = create_texture_for_bmp(context, optionalKey, rtDesc, NULL, 0);
232
233 if (!resized) {
234 return NULL;
235 }
236 GrPaint paint;
237
238 // If filtering is not desired then we want to ensure all texels in the resampled image are
239 // copies of texels from the original.
240 GrTextureParams params(SkShader::kClamp_TileMode,
241 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
242 GrTextureParams::kNone_FilterMode);
243 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
244
245 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
246 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
247
248 GrContext::AutoRenderTarget autoRT(context, resized->asRenderTarget());
249 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
250 context->drawNonAARectToRect(paint, SkMatrix::I(), rect, localRect);
251
252 return resized;
253}
254
krajcevski8c111f72014-06-02 13:51:34 -0700255#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon37f9a262015-02-02 13:00:10 -0800256static GrTexture *load_etc1_texture(GrContext* ctx, const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700257 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700258 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700259
260 // Is this even encoded data?
261 if (NULL == data) {
262 return NULL;
263 }
264
265 // Is this a valid PKM encoded data?
266 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700267 if (etc1_pkm_is_valid(bytes)) {
268 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
269 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
270
271 // Does the data match the dimensions of the bitmap? If not,
272 // then we don't know how to scale the image to match it...
273 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
274 encodedHeight != static_cast<uint32_t>(bm.height())) {
275 return NULL;
276 }
277
278 // Everything seems good... skip ahead to the data.
279 bytes += ETC_PKM_HEADER_SIZE;
280 desc.fConfig = kETC1_GrPixelConfig;
281 } else if (SkKTXFile::is_ktx(bytes)) {
282 SkKTXFile ktx(data);
283
284 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700285 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700286 return NULL;
287 }
288
289 // Does the data match the dimensions of the bitmap? If not,
290 // then we don't know how to scale the image to match it...
291 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
292 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800293 }
krajcevski99ffe242014-06-03 13:04:35 -0700294
295 bytes = ktx.pixelData();
296 desc.fConfig = kETC1_GrPixelConfig;
297 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700298 return NULL;
299 }
300
bsalomon37f9a262015-02-02 13:00:10 -0800301 return create_texture_for_bmp(ctx, optionalKey, desc, bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700302}
krajcevski8c111f72014-06-02 13:51:34 -0700303#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700304
bsalomon37f9a262015-02-02 13:00:10 -0800305static GrTexture* load_yuv_texture(GrContext* ctx, const GrContentKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700306 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700307 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700308 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800309 if ((NULL == pixelRef) ||
310 (pixelRef->info().width() != bm.info().width()) ||
311 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700312 return NULL;
313 }
314
sugoiba18f912015-02-04 10:53:03 -0800315 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800316 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800317 SkAutoTUnref<SkCachedData> cachedData;
318 SkAutoMalloc storage;
319 if (useCache) {
320 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
321 }
sugoi692135f2015-01-19 10:10:27 -0800322
sugoi518d83d2014-07-21 11:37:39 -0700323 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800324 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800325 planes[0] = (void*)cachedData->data();
326 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
327 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
328 } else {
329 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
330 // rounded up to JPEG block size and be larger than the image's width and height.
331 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
332 return NULL;
333 }
sugoi518d83d2014-07-21 11:37:39 -0700334
sugoi692135f2015-01-19 10:10:27 -0800335 // Allocate the memory for YUV
336 size_t totalSize(0);
337 for (int i = 0; i < 3; ++i) {
338 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
339 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
340 totalSize += yuvInfo.fSizeInMemory[i];
341 }
sugoiba18f912015-02-04 10:53:03 -0800342 if (useCache) {
343 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
344 planes[0] = cachedData->writable_data();
345 } else {
346 storage.reset(totalSize);
347 planes[0] = storage.get();
348 }
sugoi692135f2015-01-19 10:10:27 -0800349 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
350 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700351
sugoi692135f2015-01-19 10:10:27 -0800352 // Get the YUV planes and update plane sizes to actual image size
353 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
354 &yuvInfo.fColorSpace)) {
355 return NULL;
356 }
357
sugoiba18f912015-02-04 10:53:03 -0800358 if (useCache) {
359 // Decoding is done, cache the resulting YUV planes
360 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
361 }
sugoi518d83d2014-07-21 11:37:39 -0700362 }
363
bsalomonf2703d82014-10-28 14:33:06 -0700364 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700365 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700366 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700367 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800368 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
369 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
bsalomone3059732014-10-14 11:47:22 -0700370 yuvTextures[i].reset(
371 ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch));
372 if (!yuvTextures[i] ||
373 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800374 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700375 return NULL;
376 }
377 }
378
bsalomonf2703d82014-10-28 14:33:06 -0700379 GrSurfaceDesc rtDesc = desc;
sugoi518d83d2014-07-21 11:37:39 -0700380 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700381 kRenderTarget_GrSurfaceFlag |
382 kNoStencil_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700383
bsalomon37f9a262015-02-02 13:00:10 -0800384 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, NULL, 0);
385 if (!result) {
386 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700387 }
388
bsalomon37f9a262015-02-02 13:00:10 -0800389 GrRenderTarget* renderTarget = result->asRenderTarget();
390 SkASSERT(renderTarget);
391
392 SkAutoTUnref<GrFragmentProcessor>
393 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
394 yuvInfo.fColorSpace));
395 GrPaint paint;
396 paint.addColorProcessor(yuvToRgbProcessor);
397 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
398 SkIntToScalar(yuvInfo.fSize[0].fHeight));
399 GrContext::AutoRenderTarget autoRT(ctx, renderTarget);
400 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
401 ctx->drawRect(paint, SkMatrix::I(), r);
402
sugoi518d83d2014-07-21 11:37:39 -0700403 return result;
404}
405
bsalomon37f9a262015-02-02 13:00:10 -0800406static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
407 const SkBitmap& origBitmap,
408 const GrContentKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 SkBitmap tmpBitmap;
410
411 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000412
bsalomonf2703d82014-10-28 14:33:06 -0700413 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000414 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000415
reed0689d7b2014-06-14 05:30:20 -0700416 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800417 if (ctx->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700418 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
419 bitmap->width(), bitmap->height());
420 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700421 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
423 // our compressed data will be trimmed, so pass width() for its
424 // "rowBytes", since they are the same now.
bsalomon37f9a262015-02-02 13:00:10 -0800425 return create_texture_for_bmp(ctx, optionalKey, desc, storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000427 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 // now bitmap points to our temp, which has been promoted to 32bits
429 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000430 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 }
krajcevski309e8692014-06-02 08:02:45 -0700432 }
krajcevski9c0e6292014-06-02 07:38:14 -0700433
434 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700435#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800436 // Make sure that the underlying device supports ETC1 textures before we go ahead
437 // and check the data.
438 else if (ctx->isConfigTexturable(kETC1_GrPixelConfig)
439 // If the bitmap had compressed data and was then uncompressed, it'll still return
440 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
441 // the bitmap has available pixels, then they might not be what the decompressed
442 // data is.
443 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800444 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700445 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700446 return texture;
447 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448 }
krajcevski8c111f72014-06-02 13:51:34 -0700449#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000450
bsalomond2a6f4e2015-02-04 10:55:54 -0800451 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
452 if (texture) {
453 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700454 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800455
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000456 SkAutoLockPixels alp(*bitmap);
457 if (!bitmap->readyToDraw()) {
458 return NULL;
459 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000460
bsalomon37f9a262015-02-02 13:00:10 -0800461 return create_texture_for_bmp(ctx, optionalKey, desc, bitmap->getPixels(), bitmap->rowBytes());
462}
463
464static GrTexture* create_bitmap_texture(GrContext* ctx,
465 const SkBitmap& bmp,
466 Stretch stretch,
467 const GrContentKey& unstretchedKey,
468 const GrContentKey& stretchedKey) {
469 if (kNo_Stretch != stretch) {
470 SkAutoTUnref<GrTexture> unstretched;
471 // Check if we have the unstretched version in the cache, if not create it.
472 if (unstretchedKey.isValid()) {
473 unstretched.reset(ctx->findAndRefCachedTexture(unstretchedKey));
474 }
475 if (!unstretched) {
476 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
477 if (!unstretched) {
478 return NULL;
479 }
480 }
481 GrTexture* resized = resize_texture(unstretched, stretch, stretchedKey);
482 return resized;
483 }
484
485 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
486
reed@google.comac10a2d2010-12-22 21:39:39 +0000487}
488
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000489bool GrIsBitmapInCache(const GrContext* ctx,
490 const SkBitmap& bitmap,
491 const GrTextureParams* params) {
bsalomon88425562015-02-04 09:12:46 -0800492 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
493
494 // Handle the case where the bitmap is explicitly texture backed.
495 GrTexture* texture = bitmap.getTexture();
496 if (texture) {
497 if (kNo_Stretch == stretch) {
498 return true;
499 }
500 // No keys for volatile bitmaps.
501 if (bitmap.isVolatile()) {
502 return false;
503 }
504 const GrContentKey& key = texture->getContentKey();
505 if (!key.isValid()) {
506 return false;
507 }
508 GrContentKey resizedKey;
509 make_resize_key(key, stretch, &resizedKey);
510 return ctx->isResourceInCache(resizedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800511 }
512
bsalomon37f9a262015-02-02 13:00:10 -0800513 // We don't cache volatile bitmaps
514 if (bitmap.isVolatile()) {
515 return false;
516 }
517
bsalomon37f9a262015-02-02 13:00:10 -0800518 GrContentKey key, resizedKey;
519 generate_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800520 return ctx->isResourceInCache((kNo_Stretch == stretch) ? key : resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000521}
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
bsalomonbcf0a522014-10-08 08:40:09 -0700523GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
524 const SkBitmap& bitmap,
525 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000526
bsalomon37f9a262015-02-02 13:00:10 -0800527 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
bsalomon88425562015-02-04 09:12:46 -0800528
529 GrTexture* result = bitmap.getTexture();
530 if (result) {
531 if (kNo_Stretch == stretch) {
532 return SkRef(result);
533 }
534 GrContentKey resizedKey;
535 // Don't create a key for the resized version if the bmp is volatile.
536 if (!bitmap.isVolatile()) {
537 const GrContentKey& key = result->getContentKey();
538 if (key.isValid()) {
539 make_resize_key(key, stretch, &resizedKey);
540 GrTexture* stretched = ctx->findAndRefCachedTexture(resizedKey);
541 if (stretched) {
542 return stretched;
543 }
544 }
545 }
546 return resize_texture(result, stretch, resizedKey);
547 }
548
bsalomon37f9a262015-02-02 13:00:10 -0800549 GrContentKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000550
bsalomon37f9a262015-02-02 13:00:10 -0800551 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000552 // If the bitmap isn't changing try to find a cached copy first.
bsalomon37f9a262015-02-02 13:00:10 -0800553 generate_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000554
bsalomon37f9a262015-02-02 13:00:10 -0800555 result = ctx->findAndRefCachedTexture(resizedKey.isValid() ? resizedKey : key);
556 if (result) {
557 return result;
558 }
559 }
bsalomone137db82015-01-31 20:10:56 -0800560
bsalomon37f9a262015-02-02 13:00:10 -0800561 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
562 if (result) {
563 return result;
564 }
bsalomone137db82015-01-31 20:10:56 -0800565
bsalomon37f9a262015-02-02 13:00:10 -0800566 SkDebugf("---- failed to create texture for cache [%d %d]\n",
567 bitmap.width(), bitmap.height());
568
569 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000570}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000571///////////////////////////////////////////////////////////////////////////////
572
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000573// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
574// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800575GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000576 switch (ct) {
577 case kUnknown_SkColorType:
578 return kUnknown_GrPixelConfig;
579 case kAlpha_8_SkColorType:
580 return kAlpha_8_GrPixelConfig;
581 case kRGB_565_SkColorType:
582 return kRGB_565_GrPixelConfig;
583 case kARGB_4444_SkColorType:
584 return kRGBA_4444_GrPixelConfig;
585 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800586// if (kSRGB_SkColorProfileType == pt) {
587// return kSRGBA_8888_GrPixelConfig;
588// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000589 return kRGBA_8888_GrPixelConfig;
590 case kBGRA_8888_SkColorType:
591 return kBGRA_8888_GrPixelConfig;
592 case kIndex_8_SkColorType:
593 return kIndex_8_GrPixelConfig;
594 }
595 SkASSERT(0); // shouldn't get here
596 return kUnknown_GrPixelConfig;
597}
598
jvanverthfa1e8a72014-12-22 08:31:49 -0800599bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
600 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000601 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800602 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000603 switch (config) {
604 case kAlpha_8_GrPixelConfig:
605 ct = kAlpha_8_SkColorType;
606 break;
607 case kIndex_8_GrPixelConfig:
608 ct = kIndex_8_SkColorType;
609 break;
610 case kRGB_565_GrPixelConfig:
611 ct = kRGB_565_SkColorType;
612 break;
613 case kRGBA_4444_GrPixelConfig:
614 ct = kARGB_4444_SkColorType;
615 break;
616 case kRGBA_8888_GrPixelConfig:
617 ct = kRGBA_8888_SkColorType;
618 break;
619 case kBGRA_8888_GrPixelConfig:
620 ct = kBGRA_8888_SkColorType;
621 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800622 case kSRGBA_8888_GrPixelConfig:
623 ct = kRGBA_8888_SkColorType;
624 pt = kSRGB_SkColorProfileType;
625 break;
reed@google.combf790232013-12-13 19:45:58 +0000626 default:
627 return false;
628 }
629 if (ctOut) {
630 *ctOut = ct;
631 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800632 if (ptOut) {
633 *ptOut = pt;
634 }
reed@google.combf790232013-12-13 19:45:58 +0000635 return true;
636}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000637
638///////////////////////////////////////////////////////////////////////////////
639
bsalomon83d081a2014-07-08 09:56:10 -0700640void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000641 bool constantColor, GrPaint* grPaint) {
642
643 grPaint->setDither(skPaint.isDither());
644 grPaint->setAntiAlias(skPaint.isAntiAlias());
645
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000646 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800647 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800648 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000649 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800650 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000651 }
egdaniel378092f2014-12-03 10:40:13 -0800652 SkASSERT(xpFactory);
653 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800654
dandov9de5b512014-06-10 14:38:28 -0700655 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700656 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000657
658 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700659 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000660 // if the source color is a constant then apply the filter here once rather than per pixel
661 // in a shader.
662 if (constantColor) {
663 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
664 grPaint->setColor(SkColor2GrColor(filtered));
665 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700666 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
667 if (fp.get()) {
668 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000669 }
670 }
671 }
krajcevskif461a8f2014-06-19 14:14:06 -0700672
673#ifndef SK_IGNORE_GPU_DITHER
674 // If the dither flag is set, then we need to see if the underlying context
675 // supports it. If not, then install a dither effect.
676 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
677 // What are we rendering into?
678 const GrRenderTarget *target = context->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700679 SkASSERT(target);
krajcevskif461a8f2014-06-19 14:14:06 -0700680
681 // Suspect the dithering flag has no effect on these configs, otherwise
682 // fall back on setting the appropriate state.
683 if (target->config() == kRGBA_8888_GrPixelConfig ||
684 target->config() == kBGRA_8888_GrPixelConfig) {
685 // The dither flag is set and the target is likely
686 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700687 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
688 if (fp.get()) {
689 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700690 grPaint->setDither(false);
691 }
692 }
693 }
694#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000695}
696
joshualitt5531d512014-12-17 15:50:11 -0800697void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000698 bool constantColor, GrPaint* grPaint) {
699 SkShader* shader = skPaint.getShader();
700 if (NULL == shader) {
dandov9de5b512014-06-10 14:38:28 -0700701 SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
702 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000703 return;
704 }
705
bsalomon83d081a2014-07-08 09:56:10 -0700706 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700707
708 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700709 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700710 // want them messing around with the context.
711 {
joshualittb0a8a372014-09-23 09:50:21 -0700712 // SkShader::asFragmentProcessor() may do offscreen rendering. Save off the current RT,
joshualitt5531d512014-12-17 15:50:11 -0800713 // and clip
krajcevskif461a8f2014-06-19 14:14:06 -0700714 GrContext::AutoRenderTarget art(context, NULL);
715 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
krajcevskif461a8f2014-06-19 14:14:06 -0700716
bsalomon83d081a2014-07-08 09:56:10 -0700717 // Allow the shader to modify paintColor and also create an effect to be installed as
718 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700719 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800720 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700721 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700722 constantColor = false;
723 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000724 }
krajcevskif461a8f2014-06-19 14:14:06 -0700725
joshualittb0a8a372014-09-23 09:50:21 -0700726 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700727 // 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 -0700728 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000729}