blob: 3334095a95f700b8183bef085d053de92d87ecda [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);
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
bsalomon37f9a262015-02-02 13:00:10 -0800431 GrPaint paint;
joshualitt2cdec312015-07-09 07:31:31 -0700432 SkAutoTUnref<GrFragmentProcessor>
433 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yuvTextures[0],
434 yuvTextures[1], yuvTextures[2],
435 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800436 paint.addColorProcessor(yuvToRgbProcessor);
437 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
438 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800439
robertphillipsea461502015-05-26 11:38:03 -0700440 GrDrawContext* drawContext = ctx->drawContext();
441 if (!drawContext) {
442 return NULL;
443 }
444
445 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800446
sugoi518d83d2014-07-21 11:37:39 -0700447 return result;
448}
449
bsalomon37f9a262015-02-02 13:00:10 -0800450static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
451 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800452 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700453 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
454 origBitmap.height() < ctx->caps()->minTextureSize()) {
455 return NULL;
456 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 SkBitmap tmpBitmap;
458
459 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460
bsalomonf2703d82014-10-28 14:33:06 -0700461 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000462 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700463 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464
reed0689d7b2014-06-14 05:30:20 -0700465 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700466 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700467 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
468 bitmap->width(), bitmap->height());
469 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700470 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
472 // our compressed data will be trimmed, so pass width() for its
473 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800474 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
475 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000477 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 // now bitmap points to our temp, which has been promoted to 32bits
479 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000480 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 }
krajcevski309e8692014-06-02 08:02:45 -0700482 }
krajcevski9c0e6292014-06-02 07:38:14 -0700483
484 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700485#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800486 // Make sure that the underlying device supports ETC1 textures before we go ahead
487 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700488 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800489 // If the bitmap had compressed data and was then uncompressed, it'll still return
490 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
491 // the bitmap has available pixels, then they might not be what the decompressed
492 // data is.
493 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800494 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700495 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700496 return texture;
497 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 }
krajcevski8c111f72014-06-02 13:51:34 -0700499#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomond2a6f4e2015-02-04 10:55:54 -0800501 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
502 if (texture) {
503 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700504 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800505
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000506 SkAutoLockPixels alp(*bitmap);
507 if (!bitmap->readyToDraw()) {
508 return NULL;
509 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000510
bsalomon23e619c2015-02-06 11:54:28 -0800511 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
512 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800513}
514
bsalomonb4d40ef2015-07-15 10:12:16 -0700515static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
516 SkBitmap stretched;
517 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
518 SkCanvas canvas(stretched);
519 SkPaint paint;
520 switch (stretch.fType) {
521 case Stretch::kNearest_Type:
522 paint.setFilterQuality(kNone_SkFilterQuality);
523 break;
524 case Stretch::kBilerp_Type:
525 paint.setFilterQuality(kLow_SkFilterQuality);
526 break;
527 case Stretch::kNone_Type:
528 SkDEBUGFAIL("Shouldn't get here.");
529 break;
530 }
531 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700532 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700533 return stretched;
534}
535
bsalomon37f9a262015-02-02 13:00:10 -0800536static GrTexture* create_bitmap_texture(GrContext* ctx,
537 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700538 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800539 const GrUniqueKey& unstretchedKey,
540 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700541 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800542 SkAutoTUnref<GrTexture> unstretched;
543 // Check if we have the unstretched version in the cache, if not create it.
544 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700545 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800546 }
547 if (!unstretched) {
548 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
549 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700550 // We might not have been able to create a unstrecthed texture because it is smaller
551 // than the min texture size. In that case do cpu stretching.
552 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
553 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800554 }
555 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700556 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800557 }
bsalomon37f9a262015-02-02 13:00:10 -0800558 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000559}
560
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000561bool GrIsBitmapInCache(const GrContext* ctx,
562 const SkBitmap& bitmap,
563 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700564 Stretch stretch;
565 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800566
567 // Handle the case where the bitmap is explicitly texture backed.
568 GrTexture* texture = bitmap.getTexture();
569 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700570 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800571 return true;
572 }
573 // No keys for volatile bitmaps.
574 if (bitmap.isVolatile()) {
575 return false;
576 }
bsalomon8718aaf2015-02-19 07:24:21 -0800577 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800578 if (!key.isValid()) {
579 return false;
580 }
bsalomon8718aaf2015-02-19 07:24:21 -0800581 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800582 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700583 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800584 }
585
bsalomon37f9a262015-02-02 13:00:10 -0800586 // We don't cache volatile bitmaps
587 if (bitmap.isVolatile()) {
588 return false;
589 }
590
bsalomon8718aaf2015-02-19 07:24:21 -0800591 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800592 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700593 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700594 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000595}
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
bsalomonbcf0a522014-10-08 08:40:09 -0700597GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
598 const SkBitmap& bitmap,
599 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000600
bsalomonc59a1df2015-06-01 07:13:42 -0700601 Stretch stretch;
602 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800603
604 GrTexture* result = bitmap.getTexture();
605 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700606 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800607 return SkRef(result);
608 }
bsalomon8718aaf2015-02-19 07:24:21 -0800609 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800610 // Don't create a key for the resized version if the bmp is volatile.
611 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800612 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800613 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800614 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700615 GrTexture* stretched =
616 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800617 if (stretched) {
618 return stretched;
619 }
620 }
621 }
bsalomonc59a1df2015-06-01 07:13:42 -0700622 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800623 }
624
bsalomon8718aaf2015-02-19 07:24:21 -0800625 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000626
bsalomon37f9a262015-02-02 13:00:10 -0800627 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000628 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800629 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000630
bsalomond309e7a2015-04-30 14:18:54 -0700631 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
632 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800633 if (result) {
634 return result;
635 }
636 }
bsalomone137db82015-01-31 20:10:56 -0800637
bsalomon37f9a262015-02-02 13:00:10 -0800638 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
639 if (result) {
640 return result;
641 }
bsalomone137db82015-01-31 20:10:56 -0800642
joshualitt5f5a8d72015-02-25 14:09:45 -0800643 SkErrorInternals::SetError( kInternalError_SkError,
644 "---- failed to create texture for cache [%d %d]\n",
645 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800646
647 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000648}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000649///////////////////////////////////////////////////////////////////////////////
650
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000651// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
652// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800653GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000654 switch (ct) {
655 case kUnknown_SkColorType:
656 return kUnknown_GrPixelConfig;
657 case kAlpha_8_SkColorType:
658 return kAlpha_8_GrPixelConfig;
659 case kRGB_565_SkColorType:
660 return kRGB_565_GrPixelConfig;
661 case kARGB_4444_SkColorType:
662 return kRGBA_4444_GrPixelConfig;
663 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700664 //if (kSRGB_SkColorProfileType == pt) {
665 // return kSRGBA_8888_GrPixelConfig;
666 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000667 return kRGBA_8888_GrPixelConfig;
668 case kBGRA_8888_SkColorType:
669 return kBGRA_8888_GrPixelConfig;
670 case kIndex_8_SkColorType:
671 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700672 case kGray_8_SkColorType:
673 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000674 }
675 SkASSERT(0); // shouldn't get here
676 return kUnknown_GrPixelConfig;
677}
678
jvanverthfa1e8a72014-12-22 08:31:49 -0800679bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
680 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000681 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800682 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000683 switch (config) {
684 case kAlpha_8_GrPixelConfig:
685 ct = kAlpha_8_SkColorType;
686 break;
687 case kIndex_8_GrPixelConfig:
688 ct = kIndex_8_SkColorType;
689 break;
690 case kRGB_565_GrPixelConfig:
691 ct = kRGB_565_SkColorType;
692 break;
693 case kRGBA_4444_GrPixelConfig:
694 ct = kARGB_4444_SkColorType;
695 break;
696 case kRGBA_8888_GrPixelConfig:
697 ct = kRGBA_8888_SkColorType;
698 break;
699 case kBGRA_8888_GrPixelConfig:
700 ct = kBGRA_8888_SkColorType;
701 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800702 case kSRGBA_8888_GrPixelConfig:
703 ct = kRGBA_8888_SkColorType;
704 pt = kSRGB_SkColorProfileType;
705 break;
reed@google.combf790232013-12-13 19:45:58 +0000706 default:
707 return false;
708 }
709 if (ctOut) {
710 *ctOut = ct;
711 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800712 if (ptOut) {
713 *ptOut = pt;
714 }
reed@google.combf790232013-12-13 19:45:58 +0000715 return true;
716}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000717
718///////////////////////////////////////////////////////////////////////////////
719
bsalomonbed83a62015-04-15 14:18:34 -0700720bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800721 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000722
723 grPaint->setDither(skPaint.isDither());
724 grPaint->setAntiAlias(skPaint.isAntiAlias());
725
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000726 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800727 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800728 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000729 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700730 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800731 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000732 }
egdaniel378092f2014-12-03 10:40:13 -0800733 SkASSERT(xpFactory);
734 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800735
dandov9de5b512014-06-10 14:38:28 -0700736 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700737 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000738
739 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700740 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000741 // if the source color is a constant then apply the filter here once rather than per pixel
742 // in a shader.
743 if (constantColor) {
744 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
745 grPaint->setColor(SkColor2GrColor(filtered));
746 } else {
reedcff10b22015-03-03 06:41:45 -0800747 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700748 // return false if failed?
joshualitt9cc17752015-07-09 06:28:14 -0700749 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
joshualitt2cff1762015-07-08 07:58:18 -0700750 &array)) {
reedcff10b22015-03-03 06:41:45 -0800751 for (int i = 0; i < array.count(); ++i) {
752 grPaint->addColorProcessor(array[i]);
753 array[i]->unref();
754 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000755 }
756 }
757 }
krajcevskif461a8f2014-06-19 14:14:06 -0700758
759#ifndef SK_IGNORE_GPU_DITHER
760 // If the dither flag is set, then we need to see if the underlying context
761 // supports it. If not, then install a dither effect.
762 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
763 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800764 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700765
766 // Suspect the dithering flag has no effect on these configs, otherwise
767 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800768 if (GrPixelConfigIs8888(rt->config()) ||
769 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700770 // The dither flag is set and the target is likely
771 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700772 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
773 if (fp.get()) {
774 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700775 grPaint->setDither(false);
776 }
777 }
778 }
779#endif
bsalomonbed83a62015-04-15 14:18:34 -0700780 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000781}
782
bsalomonbed83a62015-04-15 14:18:34 -0700783bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
784 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000785 SkShader* shader = skPaint.getShader();
786 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700787 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
788 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000789 }
790
bsalomon83d081a2014-07-08 09:56:10 -0700791 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700792
793 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700794 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700795 // want them messing around with the context.
796 {
bsalomon83d081a2014-07-08 09:56:10 -0700797 // Allow the shader to modify paintColor and also create an effect to be installed as
798 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700799 GrFragmentProcessor* fp = NULL;
joshualitt8ca93e72015-07-08 06:51:43 -0700800 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor,
joshualitt9cc17752015-07-09 06:28:14 -0700801 grPaint->getProcessorDataManager(), &fp)) {
bsalomonbed83a62015-04-15 14:18:34 -0700802 return false;
803 }
804 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700805 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700806 constantColor = false;
807 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000808 }
krajcevskif461a8f2014-06-19 14:14:06 -0700809
joshualittb0a8a372014-09-23 09:50:21 -0700810 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700811 // 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 -0700812 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000813}
reed8b26b992015-05-07 15:36:17 -0700814
815SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
816#ifdef SK_DEBUG
817 const GrSurfaceDesc& desc = tex->desc();
818 SkASSERT(w <= desc.fWidth);
819 SkASSERT(h <= desc.fHeight);
820#endif
821 const GrPixelConfig config = tex->config();
822 SkColorType ct;
823 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
824 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
825 ct = kUnknown_SkColorType;
826 }
827 return SkImageInfo::Make(w, h, ct, at);
828}
829
830
831void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
832 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
833 dst->setInfo(info);
834 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
835}