blob: 332f084c5b0193467264a806bcc70f3235a5dc3e [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) {
bsalomon0513c832015-06-01 10:22:48 -0700119 if (params) {
120 switch(params->filterMode()) {
121 case GrTextureParams::kNone_FilterMode:
122 stretch->fType = Stretch::kNearest_Type;
123 break;
124 case GrTextureParams::kBilerp_FilterMode:
125 case GrTextureParams::kMipMap_FilterMode:
126 stretch->fType = Stretch::kBilerp_Type;
127 break;
128 }
129 } else {
130 stretch->fType = Stretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700131 }
132 } else {
133 stretch->fWidth = -1;
134 stretch->fHeight = -1;
135 stretch->fType = Stretch::kNone_Type;
136 }
bsalomon37f9a262015-02-02 13:00:10 -0800137}
138
bsalomonc59a1df2015-06-01 07:13:42 -0700139static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800140 GrUniqueKey* stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700141 if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
142 uint32_t width = SkToU16(stretch.fWidth);
143 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800144 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
bsalomonc59a1df2015-06-01 07:13:42 -0700145 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 3);
146 builder[0] = stretch.fType;
147 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800148 builder.finish();
149 return true;
150 }
bsalomon23e619c2015-02-06 11:54:28 -0800151 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800152 return false;
153}
154
bsalomon8718aaf2015-02-19 07:24:21 -0800155static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000156 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
157 // are unique.
158 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000159 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800160 uint32_t width = SkToU16(bitmap.width());
161 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000162
bsalomon8718aaf2015-02-19 07:24:21 -0800163 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
164 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800165 builder[0] = genID;
166 builder[1] = origin.fX;
167 builder[2] = origin.fY;
168 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800169}
bsalomon37f9a262015-02-02 13:00:10 -0800170
bsalomon23e619c2015-02-06 11:54:28 -0800171static void make_bitmap_keys(const SkBitmap& bitmap,
bsalomonc59a1df2015-06-01 07:13:42 -0700172 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800173 GrUniqueKey* key,
174 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800175 make_unstretched_key(bitmap, key);
bsalomonc59a1df2015-06-01 07:13:42 -0700176 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800177 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800178 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000179}
180
bsalomonf2703d82014-10-28 14:33:06 -0700181static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
182 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000183 desc->fWidth = bitmap.width();
184 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000185 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000186 desc->fSampleCnt = 0;
187}
188
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000189namespace {
190
191// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800192class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000193public:
bsalomon8718aaf2015-02-19 07:24:21 -0800194 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000195private:
bsalomon8718aaf2015-02-19 07:24:21 -0800196 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000197
mtklein36352bf2015-03-25 18:17:31 -0700198 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800199 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000200 }
201};
202
203} // namespace
204
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000205
bsalomon37f9a262015-02-02 13:00:10 -0800206static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800207 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700208 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800209 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700210 const void* pixels,
211 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700212 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800213 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800214 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
215 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomond309e7a2015-04-30 14:18:54 -0700216 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700217 }
218 return result;
219}
220
bsalomonc59a1df2015-06-01 07:13:42 -0700221// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
222// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
223// filtering and the size to stretch the texture to.
224GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
225 SkPixelRef* pixelRef,
226 const GrUniqueKey& optionalKey) {
227 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800228
229 GrContext* context = inputTexture->getContext();
230 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700231 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800232
233 // Either it's a cache miss or the original wasn't cached to begin with.
234 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800235 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700236 rtDesc.fWidth = stretch.fWidth;
237 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800238 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
239
240 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
241 // fail.
bsalomon76228632015-05-29 08:02:10 -0700242 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800243 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700244 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800245 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700246 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800247 rtDesc.fConfig = kSkia8888_GrPixelConfig;
248 } else {
249 return NULL;
250 }
251 } else if (kRGB_GrColorComponentFlags ==
252 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700253 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800254 rtDesc.fConfig = kSkia8888_GrPixelConfig;
255 } else {
256 return NULL;
257 }
258 } else {
259 return NULL;
260 }
261 }
262
bsalomon23e619c2015-02-06 11:54:28 -0800263 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800264
bsalomon23e619c2015-02-06 11:54:28 -0800265 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800266 return NULL;
267 }
268 GrPaint paint;
269
270 // If filtering is not desired then we want to ensure all texels in the resampled image are
271 // copies of texels from the original.
272 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700273 Stretch::kBilerp_Type == stretch.fType ?
274 GrTextureParams::kBilerp_FilterMode :
275 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800276 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
277
278 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
279 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
280
robertphillipsea461502015-05-26 11:38:03 -0700281 GrDrawContext* drawContext = context->drawContext();
282 if (!drawContext) {
283 return NULL;
284 }
285
286 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
287 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800288
bsalomon23e619c2015-02-06 11:54:28 -0800289 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800290}
291
krajcevski8c111f72014-06-02 13:51:34 -0700292#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800293static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700294 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700295 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700296
297 // Is this even encoded data?
298 if (NULL == data) {
299 return NULL;
300 }
301
302 // Is this a valid PKM encoded data?
303 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700304 if (etc1_pkm_is_valid(bytes)) {
305 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
306 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
307
308 // Does the data match the dimensions of the bitmap? If not,
309 // then we don't know how to scale the image to match it...
310 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
311 encodedHeight != static_cast<uint32_t>(bm.height())) {
312 return NULL;
313 }
314
315 // Everything seems good... skip ahead to the data.
316 bytes += ETC_PKM_HEADER_SIZE;
317 desc.fConfig = kETC1_GrPixelConfig;
318 } else if (SkKTXFile::is_ktx(bytes)) {
319 SkKTXFile ktx(data);
320
321 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700322 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700323 return NULL;
324 }
325
326 // Does the data match the dimensions of the bitmap? If not,
327 // then we don't know how to scale the image to match it...
328 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
329 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800330 }
krajcevski99ffe242014-06-03 13:04:35 -0700331
332 bytes = ktx.pixelData();
333 desc.fConfig = kETC1_GrPixelConfig;
334 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700335 return NULL;
336 }
337
bsalomon23e619c2015-02-06 11:54:28 -0800338 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700339}
krajcevski8c111f72014-06-02 13:51:34 -0700340#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700341
bsalomon8718aaf2015-02-19 07:24:21 -0800342static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700343 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700344 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700345 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800346 if ((NULL == pixelRef) ||
347 (pixelRef->info().width() != bm.info().width()) ||
348 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700349 return NULL;
350 }
351
sugoiba18f912015-02-04 10:53:03 -0800352 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800353 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800354 SkAutoTUnref<SkCachedData> cachedData;
355 SkAutoMalloc storage;
356 if (useCache) {
357 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
358 }
sugoi692135f2015-01-19 10:10:27 -0800359
sugoi518d83d2014-07-21 11:37:39 -0700360 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800361 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800362 planes[0] = (void*)cachedData->data();
363 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
364 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
365 } else {
366 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
367 // rounded up to JPEG block size and be larger than the image's width and height.
368 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
369 return NULL;
370 }
sugoi518d83d2014-07-21 11:37:39 -0700371
sugoi692135f2015-01-19 10:10:27 -0800372 // Allocate the memory for YUV
373 size_t totalSize(0);
374 for (int i = 0; i < 3; ++i) {
375 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
376 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
377 totalSize += yuvInfo.fSizeInMemory[i];
378 }
sugoiba18f912015-02-04 10:53:03 -0800379 if (useCache) {
380 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
381 planes[0] = cachedData->writable_data();
382 } else {
383 storage.reset(totalSize);
384 planes[0] = storage.get();
385 }
sugoi692135f2015-01-19 10:10:27 -0800386 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
387 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700388
sugoi692135f2015-01-19 10:10:27 -0800389 // Get the YUV planes and update plane sizes to actual image size
390 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
391 &yuvInfo.fColorSpace)) {
392 return NULL;
393 }
394
sugoiba18f912015-02-04 10:53:03 -0800395 if (useCache) {
396 // Decoding is done, cache the resulting YUV planes
397 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
398 }
sugoi518d83d2014-07-21 11:37:39 -0700399 }
400
bsalomonf2703d82014-10-28 14:33:06 -0700401 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700402 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700403 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700404 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800405 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
406 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
sugoi4ab3dbb2015-03-06 05:16:52 -0800407 bool needsExactTexture =
408 (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
409 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bsalomond309e7a2015-04-30 14:18:54 -0700410 yuvTextures[i].reset(ctx->textureProvider()->refScratchTexture(yuvDesc,
411 needsExactTexture ? GrTextureProvider::kExact_ScratchTexMatch :
412 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700413 if (!yuvTextures[i] ||
414 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800415 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700416 return NULL;
417 }
418 }
419
bsalomonf2703d82014-10-28 14:33:06 -0700420 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800421 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700422
bsalomon23e619c2015-02-06 11:54:28 -0800423 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800424 if (!result) {
425 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700426 }
427
bsalomon37f9a262015-02-02 13:00:10 -0800428 GrRenderTarget* renderTarget = result->asRenderTarget();
429 SkASSERT(renderTarget);
430
431 SkAutoTUnref<GrFragmentProcessor>
432 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
sugoi4ccce7e2015-02-13 13:57:09 -0800433 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800434 GrPaint paint;
435 paint.addColorProcessor(yuvToRgbProcessor);
436 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
437 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800438
robertphillipsea461502015-05-26 11:38:03 -0700439 GrDrawContext* drawContext = ctx->drawContext();
440 if (!drawContext) {
441 return NULL;
442 }
443
444 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800445
sugoi518d83d2014-07-21 11:37:39 -0700446 return result;
447}
448
bsalomon37f9a262015-02-02 13:00:10 -0800449static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
450 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800451 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 SkBitmap tmpBitmap;
453
454 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455
bsalomonf2703d82014-10-28 14:33:06 -0700456 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000457 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700458 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459
reed0689d7b2014-06-14 05:30:20 -0700460 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700461 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700462 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
463 bitmap->width(), bitmap->height());
464 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700465 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
467 // our compressed data will be trimmed, so pass width() for its
468 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800469 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
470 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000471 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000472 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 // now bitmap points to our temp, which has been promoted to 32bits
474 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000475 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 }
krajcevski309e8692014-06-02 08:02:45 -0700477 }
krajcevski9c0e6292014-06-02 07:38:14 -0700478
479 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700480#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800481 // Make sure that the underlying device supports ETC1 textures before we go ahead
482 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700483 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800484 // If the bitmap had compressed data and was then uncompressed, it'll still return
485 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
486 // the bitmap has available pixels, then they might not be what the decompressed
487 // data is.
488 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800489 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700490 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700491 return texture;
492 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 }
krajcevski8c111f72014-06-02 13:51:34 -0700494#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomond2a6f4e2015-02-04 10:55:54 -0800496 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
497 if (texture) {
498 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700499 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800500
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000501 SkAutoLockPixels alp(*bitmap);
502 if (!bitmap->readyToDraw()) {
503 return NULL;
504 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000505
bsalomon23e619c2015-02-06 11:54:28 -0800506 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
507 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800508}
509
510static GrTexture* create_bitmap_texture(GrContext* ctx,
511 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700512 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800513 const GrUniqueKey& unstretchedKey,
514 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700515 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800516 SkAutoTUnref<GrTexture> unstretched;
517 // Check if we have the unstretched version in the cache, if not create it.
518 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700519 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800520 }
521 if (!unstretched) {
522 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
523 if (!unstretched) {
524 return NULL;
525 }
526 }
bsalomonc59a1df2015-06-01 07:13:42 -0700527 GrTexture* stretched = stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon23e619c2015-02-06 11:54:28 -0800528 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800529 }
530
531 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
532
reed@google.comac10a2d2010-12-22 21:39:39 +0000533}
534
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000535bool GrIsBitmapInCache(const GrContext* ctx,
536 const SkBitmap& bitmap,
537 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700538 Stretch stretch;
539 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800540
541 // Handle the case where the bitmap is explicitly texture backed.
542 GrTexture* texture = bitmap.getTexture();
543 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700544 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800545 return true;
546 }
547 // No keys for volatile bitmaps.
548 if (bitmap.isVolatile()) {
549 return false;
550 }
bsalomon8718aaf2015-02-19 07:24:21 -0800551 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800552 if (!key.isValid()) {
553 return false;
554 }
bsalomon8718aaf2015-02-19 07:24:21 -0800555 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800556 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700557 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800558 }
559
bsalomon37f9a262015-02-02 13:00:10 -0800560 // We don't cache volatile bitmaps
561 if (bitmap.isVolatile()) {
562 return false;
563 }
564
bsalomon8718aaf2015-02-19 07:24:21 -0800565 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800566 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700567 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700568 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000569}
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomonbcf0a522014-10-08 08:40:09 -0700571GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
572 const SkBitmap& bitmap,
573 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000574
bsalomonc59a1df2015-06-01 07:13:42 -0700575 Stretch stretch;
576 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800577
578 GrTexture* result = bitmap.getTexture();
579 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700580 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800581 return SkRef(result);
582 }
bsalomon8718aaf2015-02-19 07:24:21 -0800583 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800584 // Don't create a key for the resized version if the bmp is volatile.
585 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800586 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800587 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800588 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700589 GrTexture* stretched =
590 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800591 if (stretched) {
592 return stretched;
593 }
594 }
595 }
bsalomonc59a1df2015-06-01 07:13:42 -0700596 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800597 }
598
bsalomon8718aaf2015-02-19 07:24:21 -0800599 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000600
bsalomon37f9a262015-02-02 13:00:10 -0800601 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000602 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800603 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000604
bsalomond309e7a2015-04-30 14:18:54 -0700605 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
606 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800607 if (result) {
608 return result;
609 }
610 }
bsalomone137db82015-01-31 20:10:56 -0800611
bsalomon37f9a262015-02-02 13:00:10 -0800612 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
613 if (result) {
614 return result;
615 }
bsalomone137db82015-01-31 20:10:56 -0800616
joshualitt5f5a8d72015-02-25 14:09:45 -0800617 SkErrorInternals::SetError( kInternalError_SkError,
618 "---- failed to create texture for cache [%d %d]\n",
619 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800620
621 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000622}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000623///////////////////////////////////////////////////////////////////////////////
624
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000625// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
626// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800627GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000628 switch (ct) {
629 case kUnknown_SkColorType:
630 return kUnknown_GrPixelConfig;
631 case kAlpha_8_SkColorType:
632 return kAlpha_8_GrPixelConfig;
633 case kRGB_565_SkColorType:
634 return kRGB_565_GrPixelConfig;
635 case kARGB_4444_SkColorType:
636 return kRGBA_4444_GrPixelConfig;
637 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700638 //if (kSRGB_SkColorProfileType == pt) {
639 // return kSRGBA_8888_GrPixelConfig;
640 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000641 return kRGBA_8888_GrPixelConfig;
642 case kBGRA_8888_SkColorType:
643 return kBGRA_8888_GrPixelConfig;
644 case kIndex_8_SkColorType:
645 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700646 case kGray_8_SkColorType:
647 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000648 }
649 SkASSERT(0); // shouldn't get here
650 return kUnknown_GrPixelConfig;
651}
652
jvanverthfa1e8a72014-12-22 08:31:49 -0800653bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
654 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000655 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800656 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000657 switch (config) {
658 case kAlpha_8_GrPixelConfig:
659 ct = kAlpha_8_SkColorType;
660 break;
661 case kIndex_8_GrPixelConfig:
662 ct = kIndex_8_SkColorType;
663 break;
664 case kRGB_565_GrPixelConfig:
665 ct = kRGB_565_SkColorType;
666 break;
667 case kRGBA_4444_GrPixelConfig:
668 ct = kARGB_4444_SkColorType;
669 break;
670 case kRGBA_8888_GrPixelConfig:
671 ct = kRGBA_8888_SkColorType;
672 break;
673 case kBGRA_8888_GrPixelConfig:
674 ct = kBGRA_8888_SkColorType;
675 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800676 case kSRGBA_8888_GrPixelConfig:
677 ct = kRGBA_8888_SkColorType;
678 pt = kSRGB_SkColorProfileType;
679 break;
reed@google.combf790232013-12-13 19:45:58 +0000680 default:
681 return false;
682 }
683 if (ctOut) {
684 *ctOut = ct;
685 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800686 if (ptOut) {
687 *ptOut = pt;
688 }
reed@google.combf790232013-12-13 19:45:58 +0000689 return true;
690}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000691
692///////////////////////////////////////////////////////////////////////////////
693
bsalomonbed83a62015-04-15 14:18:34 -0700694bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800695 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000696
697 grPaint->setDither(skPaint.isDither());
698 grPaint->setAntiAlias(skPaint.isAntiAlias());
699
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000700 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800701 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800702 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000703 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700704 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800705 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000706 }
egdaniel378092f2014-12-03 10:40:13 -0800707 SkASSERT(xpFactory);
708 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800709
dandov9de5b512014-06-10 14:38:28 -0700710 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700711 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000712
713 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700714 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000715 // if the source color is a constant then apply the filter here once rather than per pixel
716 // in a shader.
717 if (constantColor) {
718 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
719 grPaint->setColor(SkColor2GrColor(filtered));
720 } else {
reedcff10b22015-03-03 06:41:45 -0800721 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700722 // return false if failed?
reedcff10b22015-03-03 06:41:45 -0800723 if (colorFilter->asFragmentProcessors(context, &array)) {
724 for (int i = 0; i < array.count(); ++i) {
725 grPaint->addColorProcessor(array[i]);
726 array[i]->unref();
727 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000728 }
729 }
730 }
krajcevskif461a8f2014-06-19 14:14:06 -0700731
732#ifndef SK_IGNORE_GPU_DITHER
733 // If the dither flag is set, then we need to see if the underlying context
734 // supports it. If not, then install a dither effect.
735 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
736 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800737 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700738
739 // Suspect the dithering flag has no effect on these configs, otherwise
740 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800741 if (GrPixelConfigIs8888(rt->config()) ||
742 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700743 // The dither flag is set and the target is likely
744 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700745 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
746 if (fp.get()) {
747 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700748 grPaint->setDither(false);
749 }
750 }
751 }
752#endif
bsalomonbed83a62015-04-15 14:18:34 -0700753 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000754}
755
bsalomonbed83a62015-04-15 14:18:34 -0700756bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
757 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000758 SkShader* shader = skPaint.getShader();
759 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700760 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
761 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000762 }
763
bsalomon83d081a2014-07-08 09:56:10 -0700764 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700765
766 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700767 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700768 // want them messing around with the context.
769 {
bsalomon83d081a2014-07-08 09:56:10 -0700770 // Allow the shader to modify paintColor and also create an effect to be installed as
771 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700772 GrFragmentProcessor* fp = NULL;
bsalomonbed83a62015-04-15 14:18:34 -0700773 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) {
774 return false;
775 }
776 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700777 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700778 constantColor = false;
779 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000780 }
krajcevskif461a8f2014-06-19 14:14:06 -0700781
joshualittb0a8a372014-09-23 09:50:21 -0700782 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700783 // 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 -0700784 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000785}
reed8b26b992015-05-07 15:36:17 -0700786
787SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
788#ifdef SK_DEBUG
789 const GrSurfaceDesc& desc = tex->desc();
790 SkASSERT(w <= desc.fWidth);
791 SkASSERT(h <= desc.fHeight);
792#endif
793 const GrPixelConfig config = tex->config();
794 SkColorType ct;
795 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
796 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
797 ct = kUnknown_SkColorType;
798 }
799 return SkImageInfo::Make(w, h, ct, at);
800}
801
802
803void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
804 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
805 dst->setInfo(info);
806 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
807}