blob: d069fe0434c196ce0d368cc5070512b03d9e23e0 [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();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800201 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomon37f9a262015-02-02 13:00:10 -0800202 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
203 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
204 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
205
206 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
207 // fail.
208 if (!context->isConfigRenderable(rtDesc.fConfig, false)) {
209 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
210 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
211 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
212 } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
213 rtDesc.fConfig = kSkia8888_GrPixelConfig;
214 } else {
215 return NULL;
216 }
217 } else if (kRGB_GrColorComponentFlags ==
218 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
219 if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
220 rtDesc.fConfig = kSkia8888_GrPixelConfig;
221 } else {
222 return NULL;
223 }
224 } else {
225 return NULL;
226 }
227 }
228
bsalomon23e619c2015-02-06 11:54:28 -0800229 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800230
bsalomon23e619c2015-02-06 11:54:28 -0800231 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800232 return NULL;
233 }
234 GrPaint paint;
235
236 // If filtering is not desired then we want to ensure all texels in the resampled image are
237 // copies of texels from the original.
238 GrTextureParams params(SkShader::kClamp_TileMode,
239 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
240 GrTextureParams::kNone_FilterMode);
241 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
242
243 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
244 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
245
joshualitt29070592015-02-25 13:04:43 -0800246 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
247 context->drawNonAARectToRect(stretched->asRenderTarget(), paint, SkMatrix::I(), rect,
248 localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800249
bsalomon23e619c2015-02-06 11:54:28 -0800250 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800251}
252
krajcevski8c111f72014-06-02 13:51:34 -0700253#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800254static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700255 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700256 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700257
258 // Is this even encoded data?
259 if (NULL == data) {
260 return NULL;
261 }
262
263 // Is this a valid PKM encoded data?
264 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700265 if (etc1_pkm_is_valid(bytes)) {
266 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
267 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
268
269 // Does the data match the dimensions of the bitmap? If not,
270 // then we don't know how to scale the image to match it...
271 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
272 encodedHeight != static_cast<uint32_t>(bm.height())) {
273 return NULL;
274 }
275
276 // Everything seems good... skip ahead to the data.
277 bytes += ETC_PKM_HEADER_SIZE;
278 desc.fConfig = kETC1_GrPixelConfig;
279 } else if (SkKTXFile::is_ktx(bytes)) {
280 SkKTXFile ktx(data);
281
282 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700283 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700284 return NULL;
285 }
286
287 // Does the data match the dimensions of the bitmap? If not,
288 // then we don't know how to scale the image to match it...
289 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
290 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800291 }
krajcevski99ffe242014-06-03 13:04:35 -0700292
293 bytes = ktx.pixelData();
294 desc.fConfig = kETC1_GrPixelConfig;
295 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700296 return NULL;
297 }
298
bsalomon23e619c2015-02-06 11:54:28 -0800299 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700300}
krajcevski8c111f72014-06-02 13:51:34 -0700301#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700302
bsalomon8718aaf2015-02-19 07:24:21 -0800303static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700304 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700305 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700306 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800307 if ((NULL == pixelRef) ||
308 (pixelRef->info().width() != bm.info().width()) ||
309 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700310 return NULL;
311 }
312
sugoiba18f912015-02-04 10:53:03 -0800313 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800314 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800315 SkAutoTUnref<SkCachedData> cachedData;
316 SkAutoMalloc storage;
317 if (useCache) {
318 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
319 }
sugoi692135f2015-01-19 10:10:27 -0800320
sugoi518d83d2014-07-21 11:37:39 -0700321 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800322 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800323 planes[0] = (void*)cachedData->data();
324 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
325 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
326 } else {
327 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
328 // rounded up to JPEG block size and be larger than the image's width and height.
329 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
330 return NULL;
331 }
sugoi518d83d2014-07-21 11:37:39 -0700332
sugoi692135f2015-01-19 10:10:27 -0800333 // Allocate the memory for YUV
334 size_t totalSize(0);
335 for (int i = 0; i < 3; ++i) {
336 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
337 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
338 totalSize += yuvInfo.fSizeInMemory[i];
339 }
sugoiba18f912015-02-04 10:53:03 -0800340 if (useCache) {
341 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
342 planes[0] = cachedData->writable_data();
343 } else {
344 storage.reset(totalSize);
345 planes[0] = storage.get();
346 }
sugoi692135f2015-01-19 10:10:27 -0800347 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
348 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700349
sugoi692135f2015-01-19 10:10:27 -0800350 // Get the YUV planes and update plane sizes to actual image size
351 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
352 &yuvInfo.fColorSpace)) {
353 return NULL;
354 }
355
sugoiba18f912015-02-04 10:53:03 -0800356 if (useCache) {
357 // Decoding is done, cache the resulting YUV planes
358 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
359 }
sugoi518d83d2014-07-21 11:37:39 -0700360 }
361
bsalomonf2703d82014-10-28 14:33:06 -0700362 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700363 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700364 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700365 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800366 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
367 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
bsalomone3059732014-10-14 11:47:22 -0700368 yuvTextures[i].reset(
369 ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch));
370 if (!yuvTextures[i] ||
371 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800372 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700373 return NULL;
374 }
375 }
376
bsalomonf2703d82014-10-28 14:33:06 -0700377 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800378 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700379
bsalomon23e619c2015-02-06 11:54:28 -0800380 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800381 if (!result) {
382 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700383 }
384
bsalomon37f9a262015-02-02 13:00:10 -0800385 GrRenderTarget* renderTarget = result->asRenderTarget();
386 SkASSERT(renderTarget);
387
388 SkAutoTUnref<GrFragmentProcessor>
389 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
sugoi4ccce7e2015-02-13 13:57:09 -0800390 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800391 GrPaint paint;
392 paint.addColorProcessor(yuvToRgbProcessor);
393 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
394 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt29070592015-02-25 13:04:43 -0800395 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip);
396 ctx->drawRect(renderTarget, paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800397
sugoi518d83d2014-07-21 11:37:39 -0700398 return result;
399}
400
bsalomon37f9a262015-02-02 13:00:10 -0800401static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
402 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800403 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 SkBitmap tmpBitmap;
405
406 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
bsalomonf2703d82014-10-28 14:33:06 -0700408 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000409 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000410
reed0689d7b2014-06-14 05:30:20 -0700411 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800412 if (ctx->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700413 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
414 bitmap->width(), bitmap->height());
415 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700416 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
418 // our compressed data will be trimmed, so pass width() for its
419 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800420 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
421 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000423 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 // now bitmap points to our temp, which has been promoted to 32bits
425 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000426 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 }
krajcevski309e8692014-06-02 08:02:45 -0700428 }
krajcevski9c0e6292014-06-02 07:38:14 -0700429
430 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700431#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800432 // Make sure that the underlying device supports ETC1 textures before we go ahead
433 // and check the data.
434 else if (ctx->isConfigTexturable(kETC1_GrPixelConfig)
435 // If the bitmap had compressed data and was then uncompressed, it'll still return
436 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
437 // the bitmap has available pixels, then they might not be what the decompressed
438 // data is.
439 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800440 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700441 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700442 return texture;
443 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444 }
krajcevski8c111f72014-06-02 13:51:34 -0700445#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
bsalomond2a6f4e2015-02-04 10:55:54 -0800447 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
448 if (texture) {
449 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700450 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800451
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000452 SkAutoLockPixels alp(*bitmap);
453 if (!bitmap->readyToDraw()) {
454 return NULL;
455 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000456
bsalomon23e619c2015-02-06 11:54:28 -0800457 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
458 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800459}
460
461static GrTexture* create_bitmap_texture(GrContext* ctx,
462 const SkBitmap& bmp,
463 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800464 const GrUniqueKey& unstretchedKey,
465 const GrUniqueKey& stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800466 if (kNo_Stretch != stretch) {
467 SkAutoTUnref<GrTexture> unstretched;
468 // Check if we have the unstretched version in the cache, if not create it.
469 if (unstretchedKey.isValid()) {
470 unstretched.reset(ctx->findAndRefCachedTexture(unstretchedKey));
471 }
472 if (!unstretched) {
473 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
474 if (!unstretched) {
475 return NULL;
476 }
477 }
bsalomon23e619c2015-02-06 11:54:28 -0800478 GrTexture* stretched = stretch_texture_to_next_pot(unstretched, stretch, bmp.pixelRef(),
479 stretchedKey);
480 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800481 }
482
483 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
484
reed@google.comac10a2d2010-12-22 21:39:39 +0000485}
486
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000487bool GrIsBitmapInCache(const GrContext* ctx,
488 const SkBitmap& bitmap,
489 const GrTextureParams* params) {
bsalomon88425562015-02-04 09:12:46 -0800490 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
491
492 // Handle the case where the bitmap is explicitly texture backed.
493 GrTexture* texture = bitmap.getTexture();
494 if (texture) {
495 if (kNo_Stretch == stretch) {
496 return true;
497 }
498 // No keys for volatile bitmaps.
499 if (bitmap.isVolatile()) {
500 return false;
501 }
bsalomon8718aaf2015-02-19 07:24:21 -0800502 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800503 if (!key.isValid()) {
504 return false;
505 }
bsalomon8718aaf2015-02-19 07:24:21 -0800506 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800507 make_stretched_key(key, stretch, &stretchedKey);
508 return ctx->isResourceInCache(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800509 }
510
bsalomon37f9a262015-02-02 13:00:10 -0800511 // We don't cache volatile bitmaps
512 if (bitmap.isVolatile()) {
513 return false;
514 }
515
bsalomon8718aaf2015-02-19 07:24:21 -0800516 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800517 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
518 return ctx->isResourceInCache((kNo_Stretch == stretch) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000519}
reed@google.comac10a2d2010-12-22 21:39:39 +0000520
bsalomonbcf0a522014-10-08 08:40:09 -0700521GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
522 const SkBitmap& bitmap,
523 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000524
bsalomon37f9a262015-02-02 13:00:10 -0800525 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
bsalomon88425562015-02-04 09:12:46 -0800526
527 GrTexture* result = bitmap.getTexture();
528 if (result) {
529 if (kNo_Stretch == stretch) {
530 return SkRef(result);
531 }
bsalomon8718aaf2015-02-19 07:24:21 -0800532 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800533 // Don't create a key for the resized version if the bmp is volatile.
534 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800535 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800536 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800537 make_stretched_key(key, stretch, &stretchedKey);
538 GrTexture* stretched = ctx->findAndRefCachedTexture(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800539 if (stretched) {
540 return stretched;
541 }
542 }
543 }
bsalomon23e619c2015-02-06 11:54:28 -0800544 return stretch_texture_to_next_pot(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800545 }
546
bsalomon8718aaf2015-02-19 07:24:21 -0800547 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000548
bsalomon37f9a262015-02-02 13:00:10 -0800549 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000550 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800551 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000552
bsalomon37f9a262015-02-02 13:00:10 -0800553 result = ctx->findAndRefCachedTexture(resizedKey.isValid() ? resizedKey : key);
554 if (result) {
555 return result;
556 }
557 }
bsalomone137db82015-01-31 20:10:56 -0800558
bsalomon37f9a262015-02-02 13:00:10 -0800559 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
560 if (result) {
561 return result;
562 }
bsalomone137db82015-01-31 20:10:56 -0800563
bsalomon37f9a262015-02-02 13:00:10 -0800564 SkDebugf("---- failed to create texture for cache [%d %d]\n",
565 bitmap.width(), bitmap.height());
566
567 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000568}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000569///////////////////////////////////////////////////////////////////////////////
570
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000571// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
572// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800573GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000574 switch (ct) {
575 case kUnknown_SkColorType:
576 return kUnknown_GrPixelConfig;
577 case kAlpha_8_SkColorType:
578 return kAlpha_8_GrPixelConfig;
579 case kRGB_565_SkColorType:
580 return kRGB_565_GrPixelConfig;
581 case kARGB_4444_SkColorType:
582 return kRGBA_4444_GrPixelConfig;
583 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800584// if (kSRGB_SkColorProfileType == pt) {
585// return kSRGBA_8888_GrPixelConfig;
586// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000587 return kRGBA_8888_GrPixelConfig;
588 case kBGRA_8888_SkColorType:
589 return kBGRA_8888_GrPixelConfig;
590 case kIndex_8_SkColorType:
591 return kIndex_8_GrPixelConfig;
592 }
593 SkASSERT(0); // shouldn't get here
594 return kUnknown_GrPixelConfig;
595}
596
jvanverthfa1e8a72014-12-22 08:31:49 -0800597bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
598 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000599 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800600 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000601 switch (config) {
602 case kAlpha_8_GrPixelConfig:
603 ct = kAlpha_8_SkColorType;
604 break;
605 case kIndex_8_GrPixelConfig:
606 ct = kIndex_8_SkColorType;
607 break;
608 case kRGB_565_GrPixelConfig:
609 ct = kRGB_565_SkColorType;
610 break;
611 case kRGBA_4444_GrPixelConfig:
612 ct = kARGB_4444_SkColorType;
613 break;
614 case kRGBA_8888_GrPixelConfig:
615 ct = kRGBA_8888_SkColorType;
616 break;
617 case kBGRA_8888_GrPixelConfig:
618 ct = kBGRA_8888_SkColorType;
619 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800620 case kSRGBA_8888_GrPixelConfig:
621 ct = kRGBA_8888_SkColorType;
622 pt = kSRGB_SkColorProfileType;
623 break;
reed@google.combf790232013-12-13 19:45:58 +0000624 default:
625 return false;
626 }
627 if (ctOut) {
628 *ctOut = ct;
629 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800630 if (ptOut) {
631 *ptOut = pt;
632 }
reed@google.combf790232013-12-13 19:45:58 +0000633 return true;
634}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000635
636///////////////////////////////////////////////////////////////////////////////
637
joshualitt25d9c152015-02-18 12:29:52 -0800638void SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
639 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000640
641 grPaint->setDither(skPaint.isDither());
642 grPaint->setAntiAlias(skPaint.isAntiAlias());
643
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000644 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800645 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800646 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000647 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800648 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000649 }
egdaniel378092f2014-12-03 10:40:13 -0800650 SkASSERT(xpFactory);
651 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800652
dandov9de5b512014-06-10 14:38:28 -0700653 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700654 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000655
656 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700657 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000658 // if the source color is a constant then apply the filter here once rather than per pixel
659 // in a shader.
660 if (constantColor) {
661 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
662 grPaint->setColor(SkColor2GrColor(filtered));
663 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700664 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
665 if (fp.get()) {
666 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000667 }
668 }
669 }
krajcevskif461a8f2014-06-19 14:14:06 -0700670
671#ifndef SK_IGNORE_GPU_DITHER
672 // If the dither flag is set, then we need to see if the underlying context
673 // supports it. If not, then install a dither effect.
674 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
675 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800676 SkASSERT(rt);
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.
joshualitt25d9c152015-02-18 12:29:52 -0800680 if (GrPixelConfigIs8888(rt->config()) ||
681 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700682 // 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
joshualitt25d9c152015-02-18 12:29:52 -0800694void SkPaint2GrPaintShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
695 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000696 SkShader* shader = skPaint.getShader();
697 if (NULL == shader) {
joshualitt25d9c152015-02-18 12:29:52 -0800698 SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
dandov9de5b512014-06-10 14:38:28 -0700699 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 {
joshualitt29070592015-02-25 13:04:43 -0800709 GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip);
710
bsalomon83d081a2014-07-08 09:56:10 -0700711 // Allow the shader to modify paintColor and also create an effect to be installed as
712 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700713 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800714 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700715 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700716 constantColor = false;
717 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000718 }
krajcevskif461a8f2014-06-19 14:14:06 -0700719
joshualittb0a8a372014-09-23 09:50:21 -0700720 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700721 // 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 -0800722 SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000723}