blob: a87caf7a708ccbb1fdef30ad76fb72978a47fb1b [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
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070011#include "GrDrawContext.h"
egdaniel378092f2014-12-03 10:40:13 -080012#include "GrXferProcessor.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000013#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000014#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070015#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080016#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070017#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000018#include "SkMessageBus.h"
19#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080020#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070021#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080022#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070023#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080024#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070025#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070026
krajcevski8c111f72014-06-02 13:51:34 -070027#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070028# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070029# include "etc1.h"
30#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000031
32/* Fill out buffer with the compressed format Ganesh expects from a colortable
33 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000034
35 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000036 we could detect that the colortable.count is <= 16, and then repack the
37 indices as nibbles to save RAM, but it would take more time (i.e. a lot
38 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000039
reed@google.comac10a2d2010-12-22 21:39:39 +000040 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
41 as the colortable.count says it is.
42 */
bsalomone79a2da2014-10-24 12:42:51 -070043static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070044 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000045
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000046 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000047 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000048 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000049 return;
50 }
51
52 SkColorTable* ctable = bitmap.getColorTable();
53 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000054
reed@google.com7111d462014-03-25 16:20:24 +000055 const int count = ctable->count();
56
57 SkDstPixelInfo dstPI;
58 dstPI.fColorType = kRGBA_8888_SkColorType;
59 dstPI.fAlphaType = kPremul_SkAlphaType;
60 dstPI.fPixels = buffer;
61 dstPI.fRowBytes = count * sizeof(SkPMColor);
62
63 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000064 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000065 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080066 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000067 srcPI.fRowBytes = count * sizeof(SkPMColor);
68
69 srcPI.convertPixelsTo(&dstPI, count, 1);
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070072 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000073
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000074 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
76 } else {
77 // need to trim off the extra bytes per row
78 size_t width = bitmap.width();
79 size_t rowBytes = bitmap.rowBytes();
80 const char* src = (const char*)bitmap.getPixels();
81 for (int y = 0; y < bitmap.height(); y++) {
82 memcpy(dst, src, width);
83 src += rowBytes;
84 dst += width;
85 }
86 }
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
bsalomonc59a1df2015-06-01 07:13:42 -070091struct Stretch {
92 enum Type {
93 kNone_Type,
94 kBilerp_Type,
95 kNearest_Type
96 } fType;
97 int fWidth;
98 int fHeight;
bsalomon37f9a262015-02-02 13:00:10 -080099};
100
bsalomonc59a1df2015-06-01 07:13:42 -0700101static void get_stretch(const GrContext* ctx, int width, int height,
102 const GrTextureParams* params, Stretch* stretch) {
103 stretch->fType = Stretch::kNone_Type;
104 bool doStretch = false;
105 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
106 (!SkIsPow2(width) || !SkIsPow2(height))) {
107 doStretch = true;
108 stretch->fWidth = GrNextPow2(width);
109 stretch->fHeight = GrNextPow2(height);
110 } else if (width < ctx->caps()->minTextureSize() ||
111 height < ctx->caps()->minTextureSize()) {
112 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
113 // up using the GPU. If issues persist we may need to CPU-stretch.
114 doStretch = true;
115 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
116 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800117 }
bsalomonc59a1df2015-06-01 07:13:42 -0700118 if (doStretch) {
119 switch(params->filterMode()) {
120 case GrTextureParams::kNone_FilterMode:
121 stretch->fType = Stretch::kNearest_Type;
122 break;
123 case GrTextureParams::kBilerp_FilterMode:
124 case GrTextureParams::kMipMap_FilterMode:
125 stretch->fType = Stretch::kBilerp_Type;
126 break;
127 }
128 } else {
129 stretch->fWidth = -1;
130 stretch->fHeight = -1;
131 stretch->fType = Stretch::kNone_Type;
132 }
bsalomon37f9a262015-02-02 13:00:10 -0800133}
134
bsalomonc59a1df2015-06-01 07:13:42 -0700135static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800136 GrUniqueKey* stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700137 if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
138 uint32_t width = SkToU16(stretch.fWidth);
139 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800140 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
bsalomonc59a1df2015-06-01 07:13:42 -0700141 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 3);
142 builder[0] = stretch.fType;
143 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800144 builder.finish();
145 return true;
146 }
bsalomon23e619c2015-02-06 11:54:28 -0800147 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800148 return false;
149}
150
bsalomon8718aaf2015-02-19 07:24:21 -0800151static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000152 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
153 // are unique.
154 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000155 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800156 uint32_t width = SkToU16(bitmap.width());
157 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000158
bsalomon8718aaf2015-02-19 07:24:21 -0800159 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
160 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800161 builder[0] = genID;
162 builder[1] = origin.fX;
163 builder[2] = origin.fY;
164 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800165}
bsalomon37f9a262015-02-02 13:00:10 -0800166
bsalomon23e619c2015-02-06 11:54:28 -0800167static void make_bitmap_keys(const SkBitmap& bitmap,
bsalomonc59a1df2015-06-01 07:13:42 -0700168 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800169 GrUniqueKey* key,
170 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800171 make_unstretched_key(bitmap, key);
bsalomonc59a1df2015-06-01 07:13:42 -0700172 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800173 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800174 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000175}
176
bsalomonf2703d82014-10-28 14:33:06 -0700177static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
178 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000179 desc->fWidth = bitmap.width();
180 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000181 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000182 desc->fSampleCnt = 0;
183}
184
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000185namespace {
186
187// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800188class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000189public:
bsalomon8718aaf2015-02-19 07:24:21 -0800190 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000191private:
bsalomon8718aaf2015-02-19 07:24:21 -0800192 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000193
mtklein36352bf2015-03-25 18:17:31 -0700194 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800195 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000196 }
197};
198
199} // namespace
200
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000201
bsalomon37f9a262015-02-02 13:00:10 -0800202static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800203 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700204 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800205 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700206 const void* pixels,
207 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700208 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800209 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800210 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
211 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomond309e7a2015-04-30 14:18:54 -0700212 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700213 }
214 return result;
215}
216
bsalomonc59a1df2015-06-01 07:13:42 -0700217// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
218// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
219// filtering and the size to stretch the texture to.
220GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
221 SkPixelRef* pixelRef,
222 const GrUniqueKey& optionalKey) {
223 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800224
225 GrContext* context = inputTexture->getContext();
226 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700227 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800228
229 // Either it's a cache miss or the original wasn't cached to begin with.
230 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800231 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700232 rtDesc.fWidth = stretch.fWidth;
233 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800234 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
235
236 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
237 // fail.
bsalomon76228632015-05-29 08:02:10 -0700238 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800239 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700240 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800241 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700242 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800243 rtDesc.fConfig = kSkia8888_GrPixelConfig;
244 } else {
245 return NULL;
246 }
247 } else if (kRGB_GrColorComponentFlags ==
248 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700249 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800250 rtDesc.fConfig = kSkia8888_GrPixelConfig;
251 } else {
252 return NULL;
253 }
254 } else {
255 return NULL;
256 }
257 }
258
bsalomon23e619c2015-02-06 11:54:28 -0800259 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800260
bsalomon23e619c2015-02-06 11:54:28 -0800261 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800262 return NULL;
263 }
264 GrPaint paint;
265
266 // If filtering is not desired then we want to ensure all texels in the resampled image are
267 // copies of texels from the original.
268 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700269 Stretch::kBilerp_Type == stretch.fType ?
270 GrTextureParams::kBilerp_FilterMode :
271 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800272 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
273
274 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
275 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
276
robertphillipsea461502015-05-26 11:38:03 -0700277 GrDrawContext* drawContext = context->drawContext();
278 if (!drawContext) {
279 return NULL;
280 }
281
282 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
283 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800284
bsalomon23e619c2015-02-06 11:54:28 -0800285 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800286}
287
krajcevski8c111f72014-06-02 13:51:34 -0700288#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800289static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700290 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700291 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700292
293 // Is this even encoded data?
294 if (NULL == data) {
295 return NULL;
296 }
297
298 // Is this a valid PKM encoded data?
299 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700300 if (etc1_pkm_is_valid(bytes)) {
301 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
302 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
303
304 // Does the data match the dimensions of the bitmap? If not,
305 // then we don't know how to scale the image to match it...
306 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
307 encodedHeight != static_cast<uint32_t>(bm.height())) {
308 return NULL;
309 }
310
311 // Everything seems good... skip ahead to the data.
312 bytes += ETC_PKM_HEADER_SIZE;
313 desc.fConfig = kETC1_GrPixelConfig;
314 } else if (SkKTXFile::is_ktx(bytes)) {
315 SkKTXFile ktx(data);
316
317 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700318 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700319 return NULL;
320 }
321
322 // Does the data match the dimensions of the bitmap? If not,
323 // then we don't know how to scale the image to match it...
324 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
325 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800326 }
krajcevski99ffe242014-06-03 13:04:35 -0700327
328 bytes = ktx.pixelData();
329 desc.fConfig = kETC1_GrPixelConfig;
330 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700331 return NULL;
332 }
333
bsalomon23e619c2015-02-06 11:54:28 -0800334 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700335}
krajcevski8c111f72014-06-02 13:51:34 -0700336#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700337
bsalomon8718aaf2015-02-19 07:24:21 -0800338static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700339 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700340 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700341 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800342 if ((NULL == pixelRef) ||
343 (pixelRef->info().width() != bm.info().width()) ||
344 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700345 return NULL;
346 }
347
sugoiba18f912015-02-04 10:53:03 -0800348 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800349 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800350 SkAutoTUnref<SkCachedData> cachedData;
351 SkAutoMalloc storage;
352 if (useCache) {
353 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
354 }
sugoi692135f2015-01-19 10:10:27 -0800355
sugoi518d83d2014-07-21 11:37:39 -0700356 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800357 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800358 planes[0] = (void*)cachedData->data();
359 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
360 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
361 } else {
362 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
363 // rounded up to JPEG block size and be larger than the image's width and height.
364 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
365 return NULL;
366 }
sugoi518d83d2014-07-21 11:37:39 -0700367
sugoi692135f2015-01-19 10:10:27 -0800368 // Allocate the memory for YUV
369 size_t totalSize(0);
370 for (int i = 0; i < 3; ++i) {
371 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
372 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
373 totalSize += yuvInfo.fSizeInMemory[i];
374 }
sugoiba18f912015-02-04 10:53:03 -0800375 if (useCache) {
376 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
377 planes[0] = cachedData->writable_data();
378 } else {
379 storage.reset(totalSize);
380 planes[0] = storage.get();
381 }
sugoi692135f2015-01-19 10:10:27 -0800382 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
383 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700384
sugoi692135f2015-01-19 10:10:27 -0800385 // Get the YUV planes and update plane sizes to actual image size
386 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
387 &yuvInfo.fColorSpace)) {
388 return NULL;
389 }
390
sugoiba18f912015-02-04 10:53:03 -0800391 if (useCache) {
392 // Decoding is done, cache the resulting YUV planes
393 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
394 }
sugoi518d83d2014-07-21 11:37:39 -0700395 }
396
bsalomonf2703d82014-10-28 14:33:06 -0700397 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700398 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700399 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700400 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800401 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
402 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
sugoi4ab3dbb2015-03-06 05:16:52 -0800403 bool needsExactTexture =
404 (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
405 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bsalomond309e7a2015-04-30 14:18:54 -0700406 yuvTextures[i].reset(ctx->textureProvider()->refScratchTexture(yuvDesc,
407 needsExactTexture ? GrTextureProvider::kExact_ScratchTexMatch :
408 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700409 if (!yuvTextures[i] ||
410 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800411 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700412 return NULL;
413 }
414 }
415
bsalomonf2703d82014-10-28 14:33:06 -0700416 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800417 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700418
bsalomon23e619c2015-02-06 11:54:28 -0800419 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800420 if (!result) {
421 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700422 }
423
bsalomon37f9a262015-02-02 13:00:10 -0800424 GrRenderTarget* renderTarget = result->asRenderTarget();
425 SkASSERT(renderTarget);
426
427 SkAutoTUnref<GrFragmentProcessor>
428 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
sugoi4ccce7e2015-02-13 13:57:09 -0800429 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800430 GrPaint paint;
431 paint.addColorProcessor(yuvToRgbProcessor);
432 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
433 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800434
robertphillipsea461502015-05-26 11:38:03 -0700435 GrDrawContext* drawContext = ctx->drawContext();
436 if (!drawContext) {
437 return NULL;
438 }
439
440 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800441
sugoi518d83d2014-07-21 11:37:39 -0700442 return result;
443}
444
bsalomon37f9a262015-02-02 13:00:10 -0800445static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
446 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800447 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 SkBitmap tmpBitmap;
449
450 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000451
bsalomonf2703d82014-10-28 14:33:06 -0700452 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000453 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700454 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455
reed0689d7b2014-06-14 05:30:20 -0700456 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700457 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700458 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
459 bitmap->width(), bitmap->height());
460 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700461 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 // our compressed data will be trimmed, so pass width() for its
464 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800465 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
466 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000468 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000469 // now bitmap points to our temp, which has been promoted to 32bits
470 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000471 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000472 }
krajcevski309e8692014-06-02 08:02:45 -0700473 }
krajcevski9c0e6292014-06-02 07:38:14 -0700474
475 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700476#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800477 // Make sure that the underlying device supports ETC1 textures before we go ahead
478 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700479 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800480 // If the bitmap had compressed data and was then uncompressed, it'll still return
481 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
482 // the bitmap has available pixels, then they might not be what the decompressed
483 // data is.
484 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800485 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700486 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700487 return texture;
488 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489 }
krajcevski8c111f72014-06-02 13:51:34 -0700490#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000491
bsalomond2a6f4e2015-02-04 10:55:54 -0800492 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
493 if (texture) {
494 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700495 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800496
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000497 SkAutoLockPixels alp(*bitmap);
498 if (!bitmap->readyToDraw()) {
499 return NULL;
500 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000501
bsalomon23e619c2015-02-06 11:54:28 -0800502 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
503 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800504}
505
506static GrTexture* create_bitmap_texture(GrContext* ctx,
507 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700508 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800509 const GrUniqueKey& unstretchedKey,
510 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700511 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800512 SkAutoTUnref<GrTexture> unstretched;
513 // Check if we have the unstretched version in the cache, if not create it.
514 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700515 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800516 }
517 if (!unstretched) {
518 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
519 if (!unstretched) {
520 return NULL;
521 }
522 }
bsalomonc59a1df2015-06-01 07:13:42 -0700523 GrTexture* stretched = stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon23e619c2015-02-06 11:54:28 -0800524 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800525 }
526
527 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
528
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000531bool GrIsBitmapInCache(const GrContext* ctx,
532 const SkBitmap& bitmap,
533 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700534 Stretch stretch;
535 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800536
537 // Handle the case where the bitmap is explicitly texture backed.
538 GrTexture* texture = bitmap.getTexture();
539 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700540 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800541 return true;
542 }
543 // No keys for volatile bitmaps.
544 if (bitmap.isVolatile()) {
545 return false;
546 }
bsalomon8718aaf2015-02-19 07:24:21 -0800547 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800548 if (!key.isValid()) {
549 return false;
550 }
bsalomon8718aaf2015-02-19 07:24:21 -0800551 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800552 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700553 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800554 }
555
bsalomon37f9a262015-02-02 13:00:10 -0800556 // We don't cache volatile bitmaps
557 if (bitmap.isVolatile()) {
558 return false;
559 }
560
bsalomon8718aaf2015-02-19 07:24:21 -0800561 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800562 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700563 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700564 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000565}
reed@google.comac10a2d2010-12-22 21:39:39 +0000566
bsalomonbcf0a522014-10-08 08:40:09 -0700567GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
568 const SkBitmap& bitmap,
569 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000570
bsalomonc59a1df2015-06-01 07:13:42 -0700571 Stretch stretch;
572 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800573
574 GrTexture* result = bitmap.getTexture();
575 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700576 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800577 return SkRef(result);
578 }
bsalomon8718aaf2015-02-19 07:24:21 -0800579 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800580 // Don't create a key for the resized version if the bmp is volatile.
581 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800582 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800583 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800584 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700585 GrTexture* stretched =
586 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800587 if (stretched) {
588 return stretched;
589 }
590 }
591 }
bsalomonc59a1df2015-06-01 07:13:42 -0700592 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800593 }
594
bsalomon8718aaf2015-02-19 07:24:21 -0800595 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000596
bsalomon37f9a262015-02-02 13:00:10 -0800597 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000598 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800599 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000600
bsalomond309e7a2015-04-30 14:18:54 -0700601 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
602 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800603 if (result) {
604 return result;
605 }
606 }
bsalomone137db82015-01-31 20:10:56 -0800607
bsalomon37f9a262015-02-02 13:00:10 -0800608 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
609 if (result) {
610 return result;
611 }
bsalomone137db82015-01-31 20:10:56 -0800612
joshualitt5f5a8d72015-02-25 14:09:45 -0800613 SkErrorInternals::SetError( kInternalError_SkError,
614 "---- failed to create texture for cache [%d %d]\n",
615 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800616
617 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000618}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000619///////////////////////////////////////////////////////////////////////////////
620
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000621// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
622// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800623GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000624 switch (ct) {
625 case kUnknown_SkColorType:
626 return kUnknown_GrPixelConfig;
627 case kAlpha_8_SkColorType:
628 return kAlpha_8_GrPixelConfig;
629 case kRGB_565_SkColorType:
630 return kRGB_565_GrPixelConfig;
631 case kARGB_4444_SkColorType:
632 return kRGBA_4444_GrPixelConfig;
633 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700634 //if (kSRGB_SkColorProfileType == pt) {
635 // return kSRGBA_8888_GrPixelConfig;
636 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000637 return kRGBA_8888_GrPixelConfig;
638 case kBGRA_8888_SkColorType:
639 return kBGRA_8888_GrPixelConfig;
640 case kIndex_8_SkColorType:
641 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700642 case kGray_8_SkColorType:
643 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000644 }
645 SkASSERT(0); // shouldn't get here
646 return kUnknown_GrPixelConfig;
647}
648
jvanverthfa1e8a72014-12-22 08:31:49 -0800649bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
650 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000651 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800652 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000653 switch (config) {
654 case kAlpha_8_GrPixelConfig:
655 ct = kAlpha_8_SkColorType;
656 break;
657 case kIndex_8_GrPixelConfig:
658 ct = kIndex_8_SkColorType;
659 break;
660 case kRGB_565_GrPixelConfig:
661 ct = kRGB_565_SkColorType;
662 break;
663 case kRGBA_4444_GrPixelConfig:
664 ct = kARGB_4444_SkColorType;
665 break;
666 case kRGBA_8888_GrPixelConfig:
667 ct = kRGBA_8888_SkColorType;
668 break;
669 case kBGRA_8888_GrPixelConfig:
670 ct = kBGRA_8888_SkColorType;
671 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800672 case kSRGBA_8888_GrPixelConfig:
673 ct = kRGBA_8888_SkColorType;
674 pt = kSRGB_SkColorProfileType;
675 break;
reed@google.combf790232013-12-13 19:45:58 +0000676 default:
677 return false;
678 }
679 if (ctOut) {
680 *ctOut = ct;
681 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800682 if (ptOut) {
683 *ptOut = pt;
684 }
reed@google.combf790232013-12-13 19:45:58 +0000685 return true;
686}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000687
688///////////////////////////////////////////////////////////////////////////////
689
bsalomonbed83a62015-04-15 14:18:34 -0700690bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800691 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000692
693 grPaint->setDither(skPaint.isDither());
694 grPaint->setAntiAlias(skPaint.isAntiAlias());
695
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000696 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800697 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800698 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000699 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700700 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800701 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000702 }
egdaniel378092f2014-12-03 10:40:13 -0800703 SkASSERT(xpFactory);
704 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800705
dandov9de5b512014-06-10 14:38:28 -0700706 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700707 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000708
709 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700710 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000711 // if the source color is a constant then apply the filter here once rather than per pixel
712 // in a shader.
713 if (constantColor) {
714 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
715 grPaint->setColor(SkColor2GrColor(filtered));
716 } else {
reedcff10b22015-03-03 06:41:45 -0800717 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700718 // return false if failed?
reedcff10b22015-03-03 06:41:45 -0800719 if (colorFilter->asFragmentProcessors(context, &array)) {
720 for (int i = 0; i < array.count(); ++i) {
721 grPaint->addColorProcessor(array[i]);
722 array[i]->unref();
723 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000724 }
725 }
726 }
krajcevskif461a8f2014-06-19 14:14:06 -0700727
728#ifndef SK_IGNORE_GPU_DITHER
729 // If the dither flag is set, then we need to see if the underlying context
730 // supports it. If not, then install a dither effect.
731 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
732 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800733 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700734
735 // Suspect the dithering flag has no effect on these configs, otherwise
736 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800737 if (GrPixelConfigIs8888(rt->config()) ||
738 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700739 // The dither flag is set and the target is likely
740 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700741 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
742 if (fp.get()) {
743 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700744 grPaint->setDither(false);
745 }
746 }
747 }
748#endif
bsalomonbed83a62015-04-15 14:18:34 -0700749 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000750}
751
bsalomonbed83a62015-04-15 14:18:34 -0700752bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
753 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000754 SkShader* shader = skPaint.getShader();
755 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700756 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
757 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000758 }
759
bsalomon83d081a2014-07-08 09:56:10 -0700760 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700761
762 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700763 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700764 // want them messing around with the context.
765 {
bsalomon83d081a2014-07-08 09:56:10 -0700766 // Allow the shader to modify paintColor and also create an effect to be installed as
767 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700768 GrFragmentProcessor* fp = NULL;
bsalomonbed83a62015-04-15 14:18:34 -0700769 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) {
770 return false;
771 }
772 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700773 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700774 constantColor = false;
775 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000776 }
krajcevskif461a8f2014-06-19 14:14:06 -0700777
joshualittb0a8a372014-09-23 09:50:21 -0700778 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700779 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
bsalomonbed83a62015-04-15 14:18:34 -0700780 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000781}
reed8b26b992015-05-07 15:36:17 -0700782
783SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
784#ifdef SK_DEBUG
785 const GrSurfaceDesc& desc = tex->desc();
786 SkASSERT(w <= desc.fWidth);
787 SkASSERT(h <= desc.fHeight);
788#endif
789 const GrPixelConfig config = tex->config();
790 SkColorType ct;
791 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
792 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
793 ct = kUnknown_SkColorType;
794 }
795 return SkImageInfo::Make(w, h, ct, at);
796}
797
798
799void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
800 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
801 dst->setInfo(info);
802 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
803}