blob: 401a50eca8b53145b17734b652271ce993a1707e [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"
joshualitt5f5a8d72015-02-25 14:09:45 -080014#include "SkErrorInternals.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000015#include "SkMessageBus.h"
16#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080017#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070018#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080019#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070020#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080021#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070022#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070023
krajcevski8c111f72014-06-02 13:51:34 -070024#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070025# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070026# include "etc1.h"
27#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29/* Fill out buffer with the compressed format Ganesh expects from a colortable
30 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000031
32 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000033 we could detect that the colortable.count is <= 16, and then repack the
34 indices as nibbles to save RAM, but it would take more time (i.e. a lot
35 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000036
reed@google.comac10a2d2010-12-22 21:39:39 +000037 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
38 as the colortable.count says it is.
39 */
bsalomone79a2da2014-10-24 12:42:51 -070040static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070041 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000042
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000043 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000044 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000045 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000046 return;
47 }
48
49 SkColorTable* ctable = bitmap.getColorTable();
50 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000051
reed@google.com7111d462014-03-25 16:20:24 +000052 const int count = ctable->count();
53
54 SkDstPixelInfo dstPI;
55 dstPI.fColorType = kRGBA_8888_SkColorType;
56 dstPI.fAlphaType = kPremul_SkAlphaType;
57 dstPI.fPixels = buffer;
58 dstPI.fRowBytes = count * sizeof(SkPMColor);
59
60 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000061 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000062 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080063 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000064 srcPI.fRowBytes = count * sizeof(SkPMColor);
65
66 srcPI.convertPixelsTo(&dstPI, count, 1);
67
reed@google.comac10a2d2010-12-22 21:39:39 +000068 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070069 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000070
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000071 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000072 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
73 } else {
74 // need to trim off the extra bytes per row
75 size_t width = bitmap.width();
76 size_t rowBytes = bitmap.rowBytes();
77 const char* src = (const char*)bitmap.getPixels();
78 for (int y = 0; y < bitmap.height(); y++) {
79 memcpy(dst, src, width);
80 src += rowBytes;
81 dst += width;
82 }
83 }
84}
85
86////////////////////////////////////////////////////////////////////////////////
87
bsalomon37f9a262015-02-02 13:00:10 -080088enum Stretch {
89 kNo_Stretch,
90 kBilerp_Stretch,
91 kNearest_Stretch
92};
93
94static Stretch get_stretch_type(const GrContext* ctx, int width, int height,
95 const GrTextureParams* params) {
96 if (params && params->isTiled()) {
bsalomond2a6f4e2015-02-04 10:55:54 -080097 if (!ctx->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon37f9a262015-02-02 13:00:10 -080098 switch(params->filterMode()) {
99 case GrTextureParams::kNone_FilterMode:
100 return kNearest_Stretch;
101 case GrTextureParams::kBilerp_FilterMode:
102 case GrTextureParams::kMipMap_FilterMode:
103 return kBilerp_Stretch;
104 }
105 }
106 }
107 return kNo_Stretch;
108}
109
bsalomon8718aaf2015-02-19 07:24:21 -0800110static bool make_stretched_key(const GrUniqueKey& origKey, Stretch stretch,
111 GrUniqueKey* stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800112 if (origKey.isValid() && kNo_Stretch != stretch) {
bsalomon8718aaf2015-02-19 07:24:21 -0800113 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
114 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 1);
bsalomon37f9a262015-02-02 13:00:10 -0800115 builder[0] = stretch;
116 builder.finish();
117 return true;
118 }
bsalomon23e619c2015-02-06 11:54:28 -0800119 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800120 return false;
121}
122
bsalomon8718aaf2015-02-19 07:24:21 -0800123static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000124 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
125 // are unique.
126 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000127 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800128 uint32_t width = SkToU16(bitmap.width());
129 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000130
bsalomon8718aaf2015-02-19 07:24:21 -0800131 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
132 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800133 builder[0] = genID;
134 builder[1] = origin.fX;
135 builder[2] = origin.fY;
136 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800137}
bsalomon37f9a262015-02-02 13:00:10 -0800138
bsalomon23e619c2015-02-06 11:54:28 -0800139static void make_bitmap_keys(const SkBitmap& bitmap,
140 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800141 GrUniqueKey* key,
142 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800143 make_unstretched_key(bitmap, key);
bsalomon37f9a262015-02-02 13:00:10 -0800144 if (kNo_Stretch != stretch) {
bsalomon23e619c2015-02-06 11:54:28 -0800145 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800146 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000147}
148
bsalomonf2703d82014-10-28 14:33:06 -0700149static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
150 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000151 desc->fWidth = bitmap.width();
152 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000153 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000154 desc->fSampleCnt = 0;
155}
156
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000157namespace {
158
159// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800160class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000161public:
bsalomon8718aaf2015-02-19 07:24:21 -0800162 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000163private:
bsalomon8718aaf2015-02-19 07:24:21 -0800164 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000165
mtklein72c9faa2015-01-09 10:06:39 -0800166 void onChange() SK_OVERRIDE {
bsalomon8718aaf2015-02-19 07:24:21 -0800167 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000168 }
169};
170
171} // namespace
172
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000173
bsalomon37f9a262015-02-02 13:00:10 -0800174static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800175 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700176 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800177 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700178 const void* pixels,
179 size_t rowBytes) {
bsalomond0423582015-02-06 08:49:24 -0800180 GrTexture* result = ctx->createTexture(desc, true, pixels, rowBytes);
181 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800182 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
183 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomonf99e9612015-02-19 08:24:16 -0800184 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.
bsalomon23e619c2015-02-06 11:54:28 -0800192GrTexture* stretch_texture_to_next_pot(GrTexture* inputTexture, Stretch stretch,
193 SkPixelRef* pixelRef,
bsalomon8718aaf2015-02-19 07:24:21 -0800194 const GrUniqueKey& optionalKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800195 SkASSERT(kNo_Stretch != stretch);
196
197 GrContext* context = inputTexture->getContext();
198 SkASSERT(context);
199
200 // Either it's a cache miss or the original wasn't cached to begin with.
201 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800202 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomon37f9a262015-02-02 13:00:10 -0800203 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
204 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
205 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
206
207 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
208 // fail.
209 if (!context->isConfigRenderable(rtDesc.fConfig, false)) {
210 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
211 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
212 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
213 } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
214 rtDesc.fConfig = kSkia8888_GrPixelConfig;
215 } else {
216 return NULL;
217 }
218 } else if (kRGB_GrColorComponentFlags ==
219 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
220 if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
221 rtDesc.fConfig = kSkia8888_GrPixelConfig;
222 } else {
223 return NULL;
224 }
225 } else {
226 return NULL;
227 }
228 }
229
bsalomon23e619c2015-02-06 11:54:28 -0800230 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800231
bsalomon23e619c2015-02-06 11:54:28 -0800232 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800233 return NULL;
234 }
235 GrPaint paint;
236
237 // If filtering is not desired then we want to ensure all texels in the resampled image are
238 // copies of texels from the original.
239 GrTextureParams params(SkShader::kClamp_TileMode,
240 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
241 GrTextureParams::kNone_FilterMode);
242 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
243
244 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
245 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
246
joshualitt570d2f82015-02-25 13:19:48 -0800247 context->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
248 SkMatrix::I(), rect, 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));
joshualitt570d2f82015-02-25 13:19:48 -0800395
396 ctx->drawRect(renderTarget, GrClip::WideOpen(), 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
joshualitt5f5a8d72015-02-25 14:09:45 -0800564 SkErrorInternals::SetError( kInternalError_SkError,
565 "---- failed to create texture for cache [%d %d]\n",
566 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800567
568 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000569}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000570///////////////////////////////////////////////////////////////////////////////
571
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000572// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
573// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800574GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000575 switch (ct) {
576 case kUnknown_SkColorType:
577 return kUnknown_GrPixelConfig;
578 case kAlpha_8_SkColorType:
579 return kAlpha_8_GrPixelConfig;
580 case kRGB_565_SkColorType:
581 return kRGB_565_GrPixelConfig;
582 case kARGB_4444_SkColorType:
583 return kRGBA_4444_GrPixelConfig;
584 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800585// if (kSRGB_SkColorProfileType == pt) {
586// return kSRGBA_8888_GrPixelConfig;
587// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000588 return kRGBA_8888_GrPixelConfig;
589 case kBGRA_8888_SkColorType:
590 return kBGRA_8888_GrPixelConfig;
591 case kIndex_8_SkColorType:
592 return kIndex_8_GrPixelConfig;
593 }
594 SkASSERT(0); // shouldn't get here
595 return kUnknown_GrPixelConfig;
596}
597
jvanverthfa1e8a72014-12-22 08:31:49 -0800598bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
599 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000600 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800601 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000602 switch (config) {
603 case kAlpha_8_GrPixelConfig:
604 ct = kAlpha_8_SkColorType;
605 break;
606 case kIndex_8_GrPixelConfig:
607 ct = kIndex_8_SkColorType;
608 break;
609 case kRGB_565_GrPixelConfig:
610 ct = kRGB_565_SkColorType;
611 break;
612 case kRGBA_4444_GrPixelConfig:
613 ct = kARGB_4444_SkColorType;
614 break;
615 case kRGBA_8888_GrPixelConfig:
616 ct = kRGBA_8888_SkColorType;
617 break;
618 case kBGRA_8888_GrPixelConfig:
619 ct = kBGRA_8888_SkColorType;
620 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800621 case kSRGBA_8888_GrPixelConfig:
622 ct = kRGBA_8888_SkColorType;
623 pt = kSRGB_SkColorProfileType;
624 break;
reed@google.combf790232013-12-13 19:45:58 +0000625 default:
626 return false;
627 }
628 if (ctOut) {
629 *ctOut = ct;
630 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800631 if (ptOut) {
632 *ptOut = pt;
633 }
reed@google.combf790232013-12-13 19:45:58 +0000634 return true;
635}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000636
637///////////////////////////////////////////////////////////////////////////////
638
joshualitt25d9c152015-02-18 12:29:52 -0800639void SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
640 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000641
642 grPaint->setDither(skPaint.isDither());
643 grPaint->setAntiAlias(skPaint.isAntiAlias());
644
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000645 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800646 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800647 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000648 // Fall back to src-over
egdanielc016fb82014-12-03 11:41:54 -0800649 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000650 }
egdaniel378092f2014-12-03 10:40:13 -0800651 SkASSERT(xpFactory);
652 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800653
dandov9de5b512014-06-10 14:38:28 -0700654 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700655 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000656
657 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700658 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000659 // if the source color is a constant then apply the filter here once rather than per pixel
660 // in a shader.
661 if (constantColor) {
662 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
663 grPaint->setColor(SkColor2GrColor(filtered));
664 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700665 SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
666 if (fp.get()) {
667 grPaint->addColorProcessor(fp);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000668 }
669 }
670 }
krajcevskif461a8f2014-06-19 14:14:06 -0700671
672#ifndef SK_IGNORE_GPU_DITHER
673 // If the dither flag is set, then we need to see if the underlying context
674 // supports it. If not, then install a dither effect.
675 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
676 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800677 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700678
679 // Suspect the dithering flag has no effect on these configs, otherwise
680 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800681 if (GrPixelConfigIs8888(rt->config()) ||
682 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700683 // The dither flag is set and the target is likely
684 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700685 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
686 if (fp.get()) {
687 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700688 grPaint->setDither(false);
689 }
690 }
691 }
692#endif
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000693}
694
joshualitt25d9c152015-02-18 12:29:52 -0800695void SkPaint2GrPaintShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
696 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000697 SkShader* shader = skPaint.getShader();
698 if (NULL == shader) {
joshualitt25d9c152015-02-18 12:29:52 -0800699 SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
dandov9de5b512014-06-10 14:38:28 -0700700 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000701 return;
702 }
703
bsalomon83d081a2014-07-08 09:56:10 -0700704 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700705
706 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700707 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700708 // want them messing around with the context.
709 {
bsalomon83d081a2014-07-08 09:56:10 -0700710 // Allow the shader to modify paintColor and also create an effect to be installed as
711 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700712 GrFragmentProcessor* fp = NULL;
joshualitt5531d512014-12-17 15:50:11 -0800713 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp) && fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700714 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700715 constantColor = false;
716 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000717 }
krajcevskif461a8f2014-06-19 14:14:06 -0700718
joshualittb0a8a372014-09-23 09:50:21 -0700719 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700720 // 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 -0800721 SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000722}