blob: 9aa79618a2dd04f9bd597e97e53b8a5c89b747e6 [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"
bsalomonb4d40ef2015-07-15 10:12:16 -070015#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070016#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080017#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070018#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000019#include "SkMessageBus.h"
20#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080021#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070022#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080023#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070024#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080025#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070026#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070027
krajcevski8c111f72014-06-02 13:51:34 -070028#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070029# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070030# include "etc1.h"
31#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000032
33/* Fill out buffer with the compressed format Ganesh expects from a colortable
34 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000035
36 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000037 we could detect that the colortable.count is <= 16, and then repack the
38 indices as nibbles to save RAM, but it would take more time (i.e. a lot
39 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000040
reed@google.comac10a2d2010-12-22 21:39:39 +000041 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
42 as the colortable.count says it is.
43 */
bsalomone79a2da2014-10-24 12:42:51 -070044static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070045 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000046
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000047 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000048 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000049 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000050 return;
51 }
52
53 SkColorTable* ctable = bitmap.getColorTable();
54 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000055
reed@google.com7111d462014-03-25 16:20:24 +000056 const int count = ctable->count();
57
58 SkDstPixelInfo dstPI;
59 dstPI.fColorType = kRGBA_8888_SkColorType;
60 dstPI.fAlphaType = kPremul_SkAlphaType;
61 dstPI.fPixels = buffer;
62 dstPI.fRowBytes = count * sizeof(SkPMColor);
63
64 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000065 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000066 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080067 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000068 srcPI.fRowBytes = count * sizeof(SkPMColor);
69
70 srcPI.convertPixelsTo(&dstPI, count, 1);
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070073 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000074
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000075 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000076 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
77 } else {
78 // need to trim off the extra bytes per row
79 size_t width = bitmap.width();
80 size_t rowBytes = bitmap.rowBytes();
81 const char* src = (const char*)bitmap.getPixels();
82 for (int y = 0; y < bitmap.height(); y++) {
83 memcpy(dst, src, width);
84 src += rowBytes;
85 dst += width;
86 }
87 }
88}
89
90////////////////////////////////////////////////////////////////////////////////
91
bsalomonc59a1df2015-06-01 07:13:42 -070092struct Stretch {
93 enum Type {
94 kNone_Type,
95 kBilerp_Type,
96 kNearest_Type
97 } fType;
98 int fWidth;
99 int fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800100};
101
bsalomonc59a1df2015-06-01 07:13:42 -0700102static void get_stretch(const GrContext* ctx, int width, int height,
103 const GrTextureParams* params, Stretch* stretch) {
104 stretch->fType = Stretch::kNone_Type;
105 bool doStretch = false;
106 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
107 (!SkIsPow2(width) || !SkIsPow2(height))) {
108 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700109 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
110 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
111 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700112 // 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);
bsalomoneae62002015-07-31 13:59:30 -0700410 if (needsExactTexture) {
411 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, true));
412 } else {
413 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuvDesc));
414 }
bsalomone3059732014-10-14 11:47:22 -0700415 if (!yuvTextures[i] ||
416 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800417 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700418 return NULL;
419 }
420 }
421
bsalomonf2703d82014-10-28 14:33:06 -0700422 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800423 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700424
bsalomon23e619c2015-02-06 11:54:28 -0800425 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800426 if (!result) {
427 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700428 }
429
bsalomon37f9a262015-02-02 13:00:10 -0800430 GrRenderTarget* renderTarget = result->asRenderTarget();
431 SkASSERT(renderTarget);
432
bsalomon37f9a262015-02-02 13:00:10 -0800433 GrPaint paint;
joshualitt2cdec312015-07-09 07:31:31 -0700434 SkAutoTUnref<GrFragmentProcessor>
435 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yuvTextures[0],
436 yuvTextures[1], yuvTextures[2],
437 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800438 paint.addColorProcessor(yuvToRgbProcessor);
439 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
440 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800441
robertphillipsea461502015-05-26 11:38:03 -0700442 GrDrawContext* drawContext = ctx->drawContext();
443 if (!drawContext) {
444 return NULL;
445 }
446
447 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800448
sugoi518d83d2014-07-21 11:37:39 -0700449 return result;
450}
451
bsalomon37f9a262015-02-02 13:00:10 -0800452static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
453 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800454 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700455 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
456 origBitmap.height() < ctx->caps()->minTextureSize()) {
457 return NULL;
458 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 SkBitmap tmpBitmap;
460
461 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462
bsalomonf2703d82014-10-28 14:33:06 -0700463 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000464 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700465 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466
reed0689d7b2014-06-14 05:30:20 -0700467 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700468 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700469 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
470 bitmap->width(), bitmap->height());
471 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700472 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
474 // our compressed data will be trimmed, so pass width() for its
475 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800476 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
477 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000479 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 // now bitmap points to our temp, which has been promoted to 32bits
481 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000482 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 }
krajcevski309e8692014-06-02 08:02:45 -0700484 }
krajcevski9c0e6292014-06-02 07:38:14 -0700485
486 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700487#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800488 // Make sure that the underlying device supports ETC1 textures before we go ahead
489 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700490 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800491 // If the bitmap had compressed data and was then uncompressed, it'll still return
492 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
493 // the bitmap has available pixels, then they might not be what the decompressed
494 // data is.
495 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800496 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700497 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700498 return texture;
499 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 }
krajcevski8c111f72014-06-02 13:51:34 -0700501#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomond2a6f4e2015-02-04 10:55:54 -0800503 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
504 if (texture) {
505 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700506 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800507
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000508 SkAutoLockPixels alp(*bitmap);
509 if (!bitmap->readyToDraw()) {
510 return NULL;
511 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000512
bsalomon23e619c2015-02-06 11:54:28 -0800513 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
514 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800515}
516
bsalomonb4d40ef2015-07-15 10:12:16 -0700517static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
518 SkBitmap stretched;
519 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
520 SkCanvas canvas(stretched);
521 SkPaint paint;
522 switch (stretch.fType) {
523 case Stretch::kNearest_Type:
524 paint.setFilterQuality(kNone_SkFilterQuality);
525 break;
526 case Stretch::kBilerp_Type:
527 paint.setFilterQuality(kLow_SkFilterQuality);
528 break;
529 case Stretch::kNone_Type:
530 SkDEBUGFAIL("Shouldn't get here.");
531 break;
532 }
533 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700534 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700535 return stretched;
536}
537
bsalomon37f9a262015-02-02 13:00:10 -0800538static GrTexture* create_bitmap_texture(GrContext* ctx,
539 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700540 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800541 const GrUniqueKey& unstretchedKey,
542 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700543 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800544 SkAutoTUnref<GrTexture> unstretched;
545 // Check if we have the unstretched version in the cache, if not create it.
546 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700547 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800548 }
549 if (!unstretched) {
550 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
551 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700552 // We might not have been able to create a unstrecthed texture because it is smaller
553 // than the min texture size. In that case do cpu stretching.
554 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
555 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800556 }
557 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700558 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800559 }
bsalomon37f9a262015-02-02 13:00:10 -0800560 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000561}
562
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000563bool GrIsBitmapInCache(const GrContext* ctx,
564 const SkBitmap& bitmap,
565 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700566 Stretch stretch;
567 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800568
569 // Handle the case where the bitmap is explicitly texture backed.
570 GrTexture* texture = bitmap.getTexture();
571 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700572 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800573 return true;
574 }
575 // No keys for volatile bitmaps.
576 if (bitmap.isVolatile()) {
577 return false;
578 }
bsalomon8718aaf2015-02-19 07:24:21 -0800579 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800580 if (!key.isValid()) {
581 return false;
582 }
bsalomon8718aaf2015-02-19 07:24:21 -0800583 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800584 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700585 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800586 }
587
bsalomon37f9a262015-02-02 13:00:10 -0800588 // We don't cache volatile bitmaps
589 if (bitmap.isVolatile()) {
590 return false;
591 }
592
bsalomon8718aaf2015-02-19 07:24:21 -0800593 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800594 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700595 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700596 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000597}
reed@google.comac10a2d2010-12-22 21:39:39 +0000598
bsalomonbcf0a522014-10-08 08:40:09 -0700599GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
600 const SkBitmap& bitmap,
601 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000602
bsalomonc59a1df2015-06-01 07:13:42 -0700603 Stretch stretch;
604 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800605
606 GrTexture* result = bitmap.getTexture();
607 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700608 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800609 return SkRef(result);
610 }
bsalomon8718aaf2015-02-19 07:24:21 -0800611 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800612 // Don't create a key for the resized version if the bmp is volatile.
613 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800614 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800615 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800616 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700617 GrTexture* stretched =
618 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800619 if (stretched) {
620 return stretched;
621 }
622 }
623 }
bsalomonc59a1df2015-06-01 07:13:42 -0700624 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800625 }
626
bsalomon8718aaf2015-02-19 07:24:21 -0800627 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000628
bsalomon37f9a262015-02-02 13:00:10 -0800629 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000630 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800631 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000632
bsalomond309e7a2015-04-30 14:18:54 -0700633 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
634 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800635 if (result) {
636 return result;
637 }
638 }
bsalomone137db82015-01-31 20:10:56 -0800639
bsalomon37f9a262015-02-02 13:00:10 -0800640 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
641 if (result) {
642 return result;
643 }
bsalomone137db82015-01-31 20:10:56 -0800644
joshualitt5f5a8d72015-02-25 14:09:45 -0800645 SkErrorInternals::SetError( kInternalError_SkError,
646 "---- failed to create texture for cache [%d %d]\n",
647 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800648
649 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000650}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000651///////////////////////////////////////////////////////////////////////////////
652
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000653// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
654// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800655GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000656 switch (ct) {
657 case kUnknown_SkColorType:
658 return kUnknown_GrPixelConfig;
659 case kAlpha_8_SkColorType:
660 return kAlpha_8_GrPixelConfig;
661 case kRGB_565_SkColorType:
662 return kRGB_565_GrPixelConfig;
663 case kARGB_4444_SkColorType:
664 return kRGBA_4444_GrPixelConfig;
665 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700666 //if (kSRGB_SkColorProfileType == pt) {
667 // return kSRGBA_8888_GrPixelConfig;
668 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000669 return kRGBA_8888_GrPixelConfig;
670 case kBGRA_8888_SkColorType:
671 return kBGRA_8888_GrPixelConfig;
672 case kIndex_8_SkColorType:
673 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700674 case kGray_8_SkColorType:
675 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000676 }
677 SkASSERT(0); // shouldn't get here
678 return kUnknown_GrPixelConfig;
679}
680
jvanverthfa1e8a72014-12-22 08:31:49 -0800681bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
682 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000683 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800684 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000685 switch (config) {
686 case kAlpha_8_GrPixelConfig:
687 ct = kAlpha_8_SkColorType;
688 break;
689 case kIndex_8_GrPixelConfig:
690 ct = kIndex_8_SkColorType;
691 break;
692 case kRGB_565_GrPixelConfig:
693 ct = kRGB_565_SkColorType;
694 break;
695 case kRGBA_4444_GrPixelConfig:
696 ct = kARGB_4444_SkColorType;
697 break;
698 case kRGBA_8888_GrPixelConfig:
699 ct = kRGBA_8888_SkColorType;
700 break;
701 case kBGRA_8888_GrPixelConfig:
702 ct = kBGRA_8888_SkColorType;
703 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800704 case kSRGBA_8888_GrPixelConfig:
705 ct = kRGBA_8888_SkColorType;
706 pt = kSRGB_SkColorProfileType;
707 break;
reed@google.combf790232013-12-13 19:45:58 +0000708 default:
709 return false;
710 }
711 if (ctOut) {
712 *ctOut = ct;
713 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800714 if (ptOut) {
715 *ptOut = pt;
716 }
reed@google.combf790232013-12-13 19:45:58 +0000717 return true;
718}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000719
720///////////////////////////////////////////////////////////////////////////////
721
bsalomonbed83a62015-04-15 14:18:34 -0700722bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800723 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000724
725 grPaint->setDither(skPaint.isDither());
726 grPaint->setAntiAlias(skPaint.isAntiAlias());
727
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000728 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800729 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800730 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000731 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700732 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800733 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000734 }
egdaniel378092f2014-12-03 10:40:13 -0800735 SkASSERT(xpFactory);
736 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800737
dandov9de5b512014-06-10 14:38:28 -0700738 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700739 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000740
741 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700742 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000743 // if the source color is a constant then apply the filter here once rather than per pixel
744 // in a shader.
745 if (constantColor) {
746 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
747 grPaint->setColor(SkColor2GrColor(filtered));
748 } else {
reedcff10b22015-03-03 06:41:45 -0800749 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700750 // return false if failed?
joshualitt9cc17752015-07-09 06:28:14 -0700751 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
joshualitt2cff1762015-07-08 07:58:18 -0700752 &array)) {
reedcff10b22015-03-03 06:41:45 -0800753 for (int i = 0; i < array.count(); ++i) {
754 grPaint->addColorProcessor(array[i]);
755 array[i]->unref();
756 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000757 }
758 }
759 }
krajcevskif461a8f2014-06-19 14:14:06 -0700760
761#ifndef SK_IGNORE_GPU_DITHER
762 // If the dither flag is set, then we need to see if the underlying context
763 // supports it. If not, then install a dither effect.
764 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
765 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800766 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700767
768 // Suspect the dithering flag has no effect on these configs, otherwise
769 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800770 if (GrPixelConfigIs8888(rt->config()) ||
771 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700772 // The dither flag is set and the target is likely
773 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700774 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
775 if (fp.get()) {
776 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700777 grPaint->setDither(false);
778 }
779 }
780 }
781#endif
bsalomonbed83a62015-04-15 14:18:34 -0700782 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000783}
784
bsalomonbed83a62015-04-15 14:18:34 -0700785bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
786 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000787 SkShader* shader = skPaint.getShader();
788 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700789 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
790 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000791 }
792
bsalomon83d081a2014-07-08 09:56:10 -0700793 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700794
795 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700796 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700797 // want them messing around with the context.
798 {
bsalomon83d081a2014-07-08 09:56:10 -0700799 // Allow the shader to modify paintColor and also create an effect to be installed as
800 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700801 GrFragmentProcessor* fp = NULL;
joshualitt8ca93e72015-07-08 06:51:43 -0700802 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor,
joshualitt9cc17752015-07-09 06:28:14 -0700803 grPaint->getProcessorDataManager(), &fp)) {
bsalomonbed83a62015-04-15 14:18:34 -0700804 return false;
805 }
806 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700807 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700808 constantColor = false;
809 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000810 }
krajcevskif461a8f2014-06-19 14:14:06 -0700811
joshualittb0a8a372014-09-23 09:50:21 -0700812 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700813 // 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 -0700814 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000815}
reed8b26b992015-05-07 15:36:17 -0700816
817SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
818#ifdef SK_DEBUG
819 const GrSurfaceDesc& desc = tex->desc();
820 SkASSERT(w <= desc.fWidth);
821 SkASSERT(h <= desc.fHeight);
822#endif
823 const GrPixelConfig config = tex->config();
824 SkColorType ct;
825 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
826 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
827 ct = kUnknown_SkColorType;
828 }
829 return SkImageInfo::Make(w, h, ct, at);
830}
831
832
833void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
834 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
835 dst->setInfo(info);
836 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
837}