blob: e13c2c7adcd329bae458a0e0b0760099ac33eb33 [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
bsalomon8718aaf2015-02-19 07:24:21 -0800109static bool make_stretched_key(const GrUniqueKey& origKey, Stretch stretch,
110 GrUniqueKey* stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800111 if (origKey.isValid() && kNo_Stretch != stretch) {
bsalomon8718aaf2015-02-19 07:24:21 -0800112 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
113 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 1);
bsalomon37f9a262015-02-02 13:00:10 -0800114 builder[0] = stretch;
115 builder.finish();
116 return true;
117 }
bsalomon23e619c2015-02-06 11:54:28 -0800118 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800119 return false;
120}
121
bsalomon8718aaf2015-02-19 07:24:21 -0800122static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000123 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
124 // are unique.
125 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000126 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800127 uint32_t width = SkToU16(bitmap.width());
128 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000129
bsalomon8718aaf2015-02-19 07:24:21 -0800130 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
131 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800132 builder[0] = genID;
133 builder[1] = origin.fX;
134 builder[2] = origin.fY;
135 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800136}
bsalomon37f9a262015-02-02 13:00:10 -0800137
bsalomon23e619c2015-02-06 11:54:28 -0800138static void make_bitmap_keys(const SkBitmap& bitmap,
139 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800140 GrUniqueKey* key,
141 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800142 make_unstretched_key(bitmap, key);
bsalomon37f9a262015-02-02 13:00:10 -0800143 if (kNo_Stretch != stretch) {
bsalomon23e619c2015-02-06 11:54:28 -0800144 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800145 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000146}
147
bsalomonf2703d82014-10-28 14:33:06 -0700148static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
149 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000150 desc->fWidth = bitmap.width();
151 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000152 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000153 desc->fSampleCnt = 0;
154}
155
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000156namespace {
157
158// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800159class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000160public:
bsalomon8718aaf2015-02-19 07:24:21 -0800161 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000162private:
bsalomon8718aaf2015-02-19 07:24:21 -0800163 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000164
mtklein72c9faa2015-01-09 10:06:39 -0800165 void onChange() SK_OVERRIDE {
bsalomon8718aaf2015-02-19 07:24:21 -0800166 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000167 }
168};
169
170} // namespace
171
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000172
bsalomon37f9a262015-02-02 13:00:10 -0800173static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800174 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700175 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800176 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700177 const void* pixels,
178 size_t rowBytes) {
bsalomond0423582015-02-06 08:49:24 -0800179 GrTexture* result = ctx->createTexture(desc, true, pixels, rowBytes);
180 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800181 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
182 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomonf99e9612015-02-19 08:24:16 -0800183 ctx->addResourceToCache(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700184 }
185 return result;
186}
187
bsalomon37f9a262015-02-02 13:00:10 -0800188// creates a new texture that is the input texture scaled up to the next power of two in
189// width or height. If optionalKey is valid it will be set on the new texture. stretch
190// controls whether the scaling is done using nearest or bilerp filtering.
bsalomon23e619c2015-02-06 11:54:28 -0800191GrTexture* stretch_texture_to_next_pot(GrTexture* inputTexture, Stretch stretch,
192 SkPixelRef* pixelRef,
bsalomon8718aaf2015-02-19 07:24:21 -0800193 const GrUniqueKey& optionalKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800194 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
bsalomon23e619c2015-02-06 11:54:28 -0800231 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800232
bsalomon23e619c2015-02-06 11:54:28 -0800233 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800234 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
bsalomon37f9a262015-02-02 13:00:10 -0800248 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
joshualitt25d9c152015-02-18 12:29:52 -0800249 context->drawNonAARectToRect(stretched->asRenderTarget(), paint, SkMatrix::I(), rect,
250 localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800251
bsalomon23e619c2015-02-06 11:54:28 -0800252 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800253}
254
krajcevski8c111f72014-06-02 13:51:34 -0700255#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800256static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& 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
bsalomon23e619c2015-02-06 11:54:28 -0800301 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), 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
bsalomon8718aaf2015-02-19 07:24:21 -0800305static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& 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
bsalomon23e619c2015-02-06 11:54:28 -0800384 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800385 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],
sugoi4ccce7e2015-02-13 13:57:09 -0800394 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800395 GrPaint paint;
396 paint.addColorProcessor(yuvToRgbProcessor);
397 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
398 SkIntToScalar(yuvInfo.fSize[0].fHeight));
bsalomon37f9a262015-02-02 13:00:10 -0800399 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
joshualitt25d9c152015-02-18 12:29:52 -0800400 ctx->drawRect(renderTarget, paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800401
sugoi518d83d2014-07-21 11:37:39 -0700402 return result;
403}
404
bsalomon37f9a262015-02-02 13:00:10 -0800405static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
406 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800407 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 SkBitmap tmpBitmap;
409
410 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000411
bsalomonf2703d82014-10-28 14:33:06 -0700412 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000413 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000414
reed0689d7b2014-06-14 05:30:20 -0700415 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800416 if (ctx->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700417 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
418 bitmap->width(), bitmap->height());
419 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700420 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
422 // our compressed data will be trimmed, so pass width() for its
423 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800424 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
425 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
bsalomon23e619c2015-02-06 11:54:28 -0800461 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
462 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800463}
464
465static GrTexture* create_bitmap_texture(GrContext* ctx,
466 const SkBitmap& bmp,
467 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800468 const GrUniqueKey& unstretchedKey,
469 const GrUniqueKey& stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800470 if (kNo_Stretch != stretch) {
471 SkAutoTUnref<GrTexture> unstretched;
472 // Check if we have the unstretched version in the cache, if not create it.
473 if (unstretchedKey.isValid()) {
474 unstretched.reset(ctx->findAndRefCachedTexture(unstretchedKey));
475 }
476 if (!unstretched) {
477 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
478 if (!unstretched) {
479 return NULL;
480 }
481 }
bsalomon23e619c2015-02-06 11:54:28 -0800482 GrTexture* stretched = stretch_texture_to_next_pot(unstretched, stretch, bmp.pixelRef(),
483 stretchedKey);
484 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800485 }
486
487 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
488
reed@google.comac10a2d2010-12-22 21:39:39 +0000489}
490
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000491bool GrIsBitmapInCache(const GrContext* ctx,
492 const SkBitmap& bitmap,
493 const GrTextureParams* params) {
bsalomon88425562015-02-04 09:12:46 -0800494 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
495
496 // Handle the case where the bitmap is explicitly texture backed.
497 GrTexture* texture = bitmap.getTexture();
498 if (texture) {
499 if (kNo_Stretch == stretch) {
500 return true;
501 }
502 // No keys for volatile bitmaps.
503 if (bitmap.isVolatile()) {
504 return false;
505 }
bsalomon8718aaf2015-02-19 07:24:21 -0800506 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800507 if (!key.isValid()) {
508 return false;
509 }
bsalomon8718aaf2015-02-19 07:24:21 -0800510 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800511 make_stretched_key(key, stretch, &stretchedKey);
512 return ctx->isResourceInCache(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800513 }
514
bsalomon37f9a262015-02-02 13:00:10 -0800515 // We don't cache volatile bitmaps
516 if (bitmap.isVolatile()) {
517 return false;
518 }
519
bsalomon8718aaf2015-02-19 07:24:21 -0800520 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800521 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
522 return ctx->isResourceInCache((kNo_Stretch == stretch) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000523}
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
bsalomonbcf0a522014-10-08 08:40:09 -0700525GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
526 const SkBitmap& bitmap,
527 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000528
bsalomon37f9a262015-02-02 13:00:10 -0800529 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
bsalomon88425562015-02-04 09:12:46 -0800530
531 GrTexture* result = bitmap.getTexture();
532 if (result) {
533 if (kNo_Stretch == stretch) {
534 return SkRef(result);
535 }
bsalomon8718aaf2015-02-19 07:24:21 -0800536 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800537 // Don't create a key for the resized version if the bmp is volatile.
538 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800539 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800540 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800541 make_stretched_key(key, stretch, &stretchedKey);
542 GrTexture* stretched = ctx->findAndRefCachedTexture(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800543 if (stretched) {
544 return stretched;
545 }
546 }
547 }
bsalomon23e619c2015-02-06 11:54:28 -0800548 return stretch_texture_to_next_pot(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800549 }
550
bsalomon8718aaf2015-02-19 07:24:21 -0800551 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000552
bsalomon37f9a262015-02-02 13:00:10 -0800553 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000554 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800555 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000556
bsalomon37f9a262015-02-02 13:00:10 -0800557 result = ctx->findAndRefCachedTexture(resizedKey.isValid() ? resizedKey : key);
558 if (result) {
559 return result;
560 }
561 }
bsalomone137db82015-01-31 20:10:56 -0800562
bsalomon37f9a262015-02-02 13:00:10 -0800563 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
564 if (result) {
565 return result;
566 }
bsalomone137db82015-01-31 20:10:56 -0800567
bsalomon37f9a262015-02-02 13:00:10 -0800568 SkDebugf("---- failed to create texture for cache [%d %d]\n",
569 bitmap.width(), bitmap.height());
570
571 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000572}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000573///////////////////////////////////////////////////////////////////////////////
574
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000575// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
576// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800577GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000578 switch (ct) {
579 case kUnknown_SkColorType:
580 return kUnknown_GrPixelConfig;
581 case kAlpha_8_SkColorType:
582 return kAlpha_8_GrPixelConfig;
583 case kRGB_565_SkColorType:
584 return kRGB_565_GrPixelConfig;
585 case kARGB_4444_SkColorType:
586 return kRGBA_4444_GrPixelConfig;
587 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800588// if (kSRGB_SkColorProfileType == pt) {
589// return kSRGBA_8888_GrPixelConfig;
590// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000591 return kRGBA_8888_GrPixelConfig;
592 case kBGRA_8888_SkColorType:
593 return kBGRA_8888_GrPixelConfig;
594 case kIndex_8_SkColorType:
595 return kIndex_8_GrPixelConfig;
596 }
597 SkASSERT(0); // shouldn't get here
598 return kUnknown_GrPixelConfig;
599}
600
jvanverthfa1e8a72014-12-22 08:31:49 -0800601bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
602 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000603 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800604 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000605 switch (config) {
606 case kAlpha_8_GrPixelConfig:
607 ct = kAlpha_8_SkColorType;
608 break;
609 case kIndex_8_GrPixelConfig:
610 ct = kIndex_8_SkColorType;
611 break;
612 case kRGB_565_GrPixelConfig:
613 ct = kRGB_565_SkColorType;
614 break;
615 case kRGBA_4444_GrPixelConfig:
616 ct = kARGB_4444_SkColorType;
617 break;
618 case kRGBA_8888_GrPixelConfig:
619 ct = kRGBA_8888_SkColorType;
620 break;
621 case kBGRA_8888_GrPixelConfig:
622 ct = kBGRA_8888_SkColorType;
623 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800624 case kSRGBA_8888_GrPixelConfig:
625 ct = kRGBA_8888_SkColorType;
626 pt = kSRGB_SkColorProfileType;
627 break;
reed@google.combf790232013-12-13 19:45:58 +0000628 default:
629 return false;
630 }
631 if (ctOut) {
632 *ctOut = ct;
633 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800634 if (ptOut) {
635 *ptOut = pt;
636 }
reed@google.combf790232013-12-13 19:45:58 +0000637 return true;
638}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000639
640///////////////////////////////////////////////////////////////////////////////
641
joshualitt25d9c152015-02-18 12:29:52 -0800642void SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
643 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000644
645 grPaint->setDither(skPaint.isDither());
646 grPaint->setAntiAlias(skPaint.isAntiAlias());
647
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000648 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800649 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800650 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000651 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800652 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000653 }
egdaniel378092f2014-12-03 10:40:13 -0800654 SkASSERT(xpFactory);
655 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800656
dandov9de5b512014-06-10 14:38:28 -0700657 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700658 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000659
660 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700661 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000662 // if the source color is a constant then apply the filter here once rather than per pixel
663 // in a shader.
664 if (constantColor) {
665 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
666 grPaint->setColor(SkColor2GrColor(filtered));
667 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700668 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
669 if (fp.get()) {
670 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000671 }
672 }
673 }
krajcevskif461a8f2014-06-19 14:14:06 -0700674
675#ifndef SK_IGNORE_GPU_DITHER
676 // If the dither flag is set, then we need to see if the underlying context
677 // supports it. If not, then install a dither effect.
678 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
679 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800680 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700681
682 // Suspect the dithering flag has no effect on these configs, otherwise
683 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800684 if (GrPixelConfigIs8888(rt->config()) ||
685 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700686 // The dither flag is set and the target is likely
687 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700688 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
689 if (fp.get()) {
690 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700691 grPaint->setDither(false);
692 }
693 }
694 }
695#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000696}
697
joshualitt25d9c152015-02-18 12:29:52 -0800698void SkPaint2GrPaintShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
699 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000700 SkShader* shader = skPaint.getShader();
701 if (NULL == shader) {
joshualitt25d9c152015-02-18 12:29:52 -0800702 SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
dandov9de5b512014-06-10 14:38:28 -0700703 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000704 return;
705 }
706
bsalomon83d081a2014-07-08 09:56:10 -0700707 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700708
709 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700710 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700711 // want them messing around with the context.
712 {
krajcevskif461a8f2014-06-19 14:14:06 -0700713 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
krajcevskif461a8f2014-06-19 14:14:06 -0700714
bsalomon83d081a2014-07-08 09:56:10 -0700715 // Allow the shader to modify paintColor and also create an effect to be installed as
716 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700717 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800718 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700719 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700720 constantColor = false;
721 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000722 }
krajcevskif461a8f2014-06-19 14:14:06 -0700723
joshualittb0a8a372014-09-23 09:50:21 -0700724 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700725 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
joshualitt25d9c152015-02-18 12:29:52 -0800726 SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000727}