blob: 53f640b6013f87c84d8f6286ec69beb7e96cb4dc [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"
joshualitt9bc39542015-08-12 12:57:54 -070024#include "effects/GrBicubicEffect.h"
krajcevskif461a8f2014-06-19 14:14:06 -070025#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080026#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070027#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070028
krajcevski8c111f72014-06-02 13:51:34 -070029#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070030# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070031# include "etc1.h"
32#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000033
34/* Fill out buffer with the compressed format Ganesh expects from a colortable
35 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000036
37 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000038 we could detect that the colortable.count is <= 16, and then repack the
39 indices as nibbles to save RAM, but it would take more time (i.e. a lot
40 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000041
reed@google.comac10a2d2010-12-22 21:39:39 +000042 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
43 as the colortable.count says it is.
44 */
bsalomone79a2da2014-10-24 12:42:51 -070045static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070046 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000047
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000048 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000049 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000050 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000051 return;
52 }
53
54 SkColorTable* ctable = bitmap.getColorTable();
55 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000056
reed@google.com7111d462014-03-25 16:20:24 +000057 const int count = ctable->count();
58
59 SkDstPixelInfo dstPI;
60 dstPI.fColorType = kRGBA_8888_SkColorType;
61 dstPI.fAlphaType = kPremul_SkAlphaType;
62 dstPI.fPixels = buffer;
63 dstPI.fRowBytes = count * sizeof(SkPMColor);
64
65 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000066 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000067 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080068 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000069 srcPI.fRowBytes = count * sizeof(SkPMColor);
70
71 srcPI.convertPixelsTo(&dstPI, count, 1);
72
reed@google.comac10a2d2010-12-22 21:39:39 +000073 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070074 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000075
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000076 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000077 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
78 } else {
79 // need to trim off the extra bytes per row
80 size_t width = bitmap.width();
81 size_t rowBytes = bitmap.rowBytes();
82 const char* src = (const char*)bitmap.getPixels();
83 for (int y = 0; y < bitmap.height(); y++) {
84 memcpy(dst, src, width);
85 src += rowBytes;
86 dst += width;
87 }
88 }
89}
90
91////////////////////////////////////////////////////////////////////////////////
92
bsalomonc59a1df2015-06-01 07:13:42 -070093struct Stretch {
94 enum Type {
95 kNone_Type,
96 kBilerp_Type,
97 kNearest_Type
98 } fType;
99 int fWidth;
100 int fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800101};
102
bsalomonc59a1df2015-06-01 07:13:42 -0700103static void get_stretch(const GrContext* ctx, int width, int height,
104 const GrTextureParams* params, Stretch* stretch) {
105 stretch->fType = Stretch::kNone_Type;
106 bool doStretch = false;
107 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
108 (!SkIsPow2(width) || !SkIsPow2(height))) {
109 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700110 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
111 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
112 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700113 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
114 // up using the GPU. If issues persist we may need to CPU-stretch.
115 doStretch = true;
116 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
117 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800118 }
bsalomonc59a1df2015-06-01 07:13:42 -0700119 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700120 if (params) {
121 switch(params->filterMode()) {
122 case GrTextureParams::kNone_FilterMode:
123 stretch->fType = Stretch::kNearest_Type;
124 break;
125 case GrTextureParams::kBilerp_FilterMode:
126 case GrTextureParams::kMipMap_FilterMode:
127 stretch->fType = Stretch::kBilerp_Type;
128 break;
129 }
130 } else {
131 stretch->fType = Stretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700132 }
133 } else {
134 stretch->fWidth = -1;
135 stretch->fHeight = -1;
136 stretch->fType = Stretch::kNone_Type;
137 }
bsalomon37f9a262015-02-02 13:00:10 -0800138}
139
bsalomonc59a1df2015-06-01 07:13:42 -0700140static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800141 GrUniqueKey* stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700142 if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
143 uint32_t width = SkToU16(stretch.fWidth);
144 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800145 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
bsalomonc59a1df2015-06-01 07:13:42 -0700146 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 3);
147 builder[0] = stretch.fType;
148 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800149 builder.finish();
150 return true;
151 }
bsalomon23e619c2015-02-06 11:54:28 -0800152 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800153 return false;
154}
155
bsalomon8718aaf2015-02-19 07:24:21 -0800156static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000157 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
158 // are unique.
159 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000160 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800161 uint32_t width = SkToU16(bitmap.width());
162 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000163
bsalomon8718aaf2015-02-19 07:24:21 -0800164 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
165 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800166 builder[0] = genID;
167 builder[1] = origin.fX;
168 builder[2] = origin.fY;
169 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800170}
bsalomon37f9a262015-02-02 13:00:10 -0800171
bsalomon23e619c2015-02-06 11:54:28 -0800172static void make_bitmap_keys(const SkBitmap& bitmap,
bsalomonc59a1df2015-06-01 07:13:42 -0700173 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800174 GrUniqueKey* key,
175 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800176 make_unstretched_key(bitmap, key);
bsalomonc59a1df2015-06-01 07:13:42 -0700177 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800178 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800179 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000180}
181
bsalomonf2703d82014-10-28 14:33:06 -0700182static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
183 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000184 desc->fWidth = bitmap.width();
185 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000186 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000187 desc->fSampleCnt = 0;
188}
189
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000190namespace {
191
192// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800193class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000194public:
bsalomon8718aaf2015-02-19 07:24:21 -0800195 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000196private:
bsalomon8718aaf2015-02-19 07:24:21 -0800197 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800200 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000201 }
202};
203
204} // namespace
205
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000206
bsalomon37f9a262015-02-02 13:00:10 -0800207static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800208 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700209 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800210 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700211 const void* pixels,
212 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700213 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800214 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800215 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
216 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomond309e7a2015-04-30 14:18:54 -0700217 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700218 }
219 return result;
220}
221
bsalomonc59a1df2015-06-01 07:13:42 -0700222// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
223// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
224// filtering and the size to stretch the texture to.
225GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
226 SkPixelRef* pixelRef,
227 const GrUniqueKey& optionalKey) {
228 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800229
230 GrContext* context = inputTexture->getContext();
231 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700232 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800233
234 // Either it's a cache miss or the original wasn't cached to begin with.
235 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800236 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700237 rtDesc.fWidth = stretch.fWidth;
238 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800239 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
240
241 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
242 // fail.
bsalomon76228632015-05-29 08:02:10 -0700243 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800244 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700245 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800246 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700247 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800248 rtDesc.fConfig = kSkia8888_GrPixelConfig;
249 } else {
250 return NULL;
251 }
252 } else if (kRGB_GrColorComponentFlags ==
253 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700254 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800255 rtDesc.fConfig = kSkia8888_GrPixelConfig;
256 } else {
257 return NULL;
258 }
259 } else {
260 return NULL;
261 }
262 }
263
bsalomon23e619c2015-02-06 11:54:28 -0800264 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800265
bsalomon23e619c2015-02-06 11:54:28 -0800266 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800267 return NULL;
268 }
269 GrPaint paint;
270
271 // If filtering is not desired then we want to ensure all texels in the resampled image are
272 // copies of texels from the original.
273 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700274 Stretch::kBilerp_Type == stretch.fType ?
275 GrTextureParams::kBilerp_FilterMode :
276 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800277 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
278
279 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
280 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
281
robertphillipsea461502015-05-26 11:38:03 -0700282 GrDrawContext* drawContext = context->drawContext();
283 if (!drawContext) {
284 return NULL;
285 }
286
287 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
288 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800289
bsalomon23e619c2015-02-06 11:54:28 -0800290 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800291}
292
krajcevski8c111f72014-06-02 13:51:34 -0700293#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800294static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700295 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700296 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700297
298 // Is this even encoded data?
299 if (NULL == data) {
300 return NULL;
301 }
302
303 // Is this a valid PKM encoded data?
304 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700305 if (etc1_pkm_is_valid(bytes)) {
306 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
307 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
308
309 // Does the data match the dimensions of the bitmap? If not,
310 // then we don't know how to scale the image to match it...
311 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
312 encodedHeight != static_cast<uint32_t>(bm.height())) {
313 return NULL;
314 }
315
316 // Everything seems good... skip ahead to the data.
317 bytes += ETC_PKM_HEADER_SIZE;
318 desc.fConfig = kETC1_GrPixelConfig;
319 } else if (SkKTXFile::is_ktx(bytes)) {
320 SkKTXFile ktx(data);
321
322 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700323 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700324 return NULL;
325 }
326
327 // Does the data match the dimensions of the bitmap? If not,
328 // then we don't know how to scale the image to match it...
329 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
330 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800331 }
krajcevski99ffe242014-06-03 13:04:35 -0700332
333 bytes = ktx.pixelData();
334 desc.fConfig = kETC1_GrPixelConfig;
335 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700336 return NULL;
337 }
338
bsalomon23e619c2015-02-06 11:54:28 -0800339 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700340}
krajcevski8c111f72014-06-02 13:51:34 -0700341#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700342
bsalomon8718aaf2015-02-19 07:24:21 -0800343static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700344 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700345 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700346 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800347 if ((NULL == pixelRef) ||
348 (pixelRef->info().width() != bm.info().width()) ||
349 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700350 return NULL;
351 }
352
sugoiba18f912015-02-04 10:53:03 -0800353 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800354 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800355 SkAutoTUnref<SkCachedData> cachedData;
356 SkAutoMalloc storage;
357 if (useCache) {
358 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
359 }
sugoi692135f2015-01-19 10:10:27 -0800360
sugoi518d83d2014-07-21 11:37:39 -0700361 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800362 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800363 planes[0] = (void*)cachedData->data();
364 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
365 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
366 } else {
367 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
368 // rounded up to JPEG block size and be larger than the image's width and height.
369 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
370 return NULL;
371 }
sugoi518d83d2014-07-21 11:37:39 -0700372
sugoi692135f2015-01-19 10:10:27 -0800373 // Allocate the memory for YUV
374 size_t totalSize(0);
375 for (int i = 0; i < 3; ++i) {
376 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
377 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
378 totalSize += yuvInfo.fSizeInMemory[i];
379 }
sugoiba18f912015-02-04 10:53:03 -0800380 if (useCache) {
381 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
382 planes[0] = cachedData->writable_data();
383 } else {
384 storage.reset(totalSize);
385 planes[0] = storage.get();
386 }
sugoi692135f2015-01-19 10:10:27 -0800387 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
388 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700389
sugoi692135f2015-01-19 10:10:27 -0800390 // Get the YUV planes and update plane sizes to actual image size
391 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
392 &yuvInfo.fColorSpace)) {
393 return NULL;
394 }
395
sugoiba18f912015-02-04 10:53:03 -0800396 if (useCache) {
397 // Decoding is done, cache the resulting YUV planes
398 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
399 }
sugoi518d83d2014-07-21 11:37:39 -0700400 }
401
bsalomonf2703d82014-10-28 14:33:06 -0700402 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700403 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700404 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700405 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800406 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
407 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
sugoi4ab3dbb2015-03-06 05:16:52 -0800408 bool needsExactTexture =
409 (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
410 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bsalomoneae62002015-07-31 13:59:30 -0700411 if (needsExactTexture) {
412 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, true));
413 } else {
414 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuvDesc));
415 }
bsalomone3059732014-10-14 11:47:22 -0700416 if (!yuvTextures[i] ||
417 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800418 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700419 return NULL;
420 }
421 }
422
bsalomonf2703d82014-10-28 14:33:06 -0700423 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800424 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700425
bsalomon23e619c2015-02-06 11:54:28 -0800426 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800427 if (!result) {
428 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700429 }
430
bsalomon37f9a262015-02-02 13:00:10 -0800431 GrRenderTarget* renderTarget = result->asRenderTarget();
432 SkASSERT(renderTarget);
433
bsalomon37f9a262015-02-02 13:00:10 -0800434 GrPaint paint;
joshualitt2cdec312015-07-09 07:31:31 -0700435 SkAutoTUnref<GrFragmentProcessor>
436 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yuvTextures[0],
437 yuvTextures[1], yuvTextures[2],
438 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800439 paint.addColorProcessor(yuvToRgbProcessor);
440 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
441 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800442
robertphillipsea461502015-05-26 11:38:03 -0700443 GrDrawContext* drawContext = ctx->drawContext();
444 if (!drawContext) {
445 return NULL;
446 }
447
448 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800449
sugoi518d83d2014-07-21 11:37:39 -0700450 return result;
451}
452
bsalomon37f9a262015-02-02 13:00:10 -0800453static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
454 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800455 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700456 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
457 origBitmap.height() < ctx->caps()->minTextureSize()) {
458 return NULL;
459 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 SkBitmap tmpBitmap;
461
462 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463
bsalomonf2703d82014-10-28 14:33:06 -0700464 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000465 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700466 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467
reed0689d7b2014-06-14 05:30:20 -0700468 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700469 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700470 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
471 bitmap->width(), bitmap->height());
472 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700473 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000474
475 // our compressed data will be trimmed, so pass width() for its
476 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800477 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
478 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000480 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 // now bitmap points to our temp, which has been promoted to 32bits
482 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000483 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 }
krajcevski309e8692014-06-02 08:02:45 -0700485 }
krajcevski9c0e6292014-06-02 07:38:14 -0700486
487 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700488#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800489 // Make sure that the underlying device supports ETC1 textures before we go ahead
490 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700491 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800492 // If the bitmap had compressed data and was then uncompressed, it'll still return
493 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
494 // the bitmap has available pixels, then they might not be what the decompressed
495 // data is.
496 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800497 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700498 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700499 return texture;
500 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 }
krajcevski8c111f72014-06-02 13:51:34 -0700502#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomond2a6f4e2015-02-04 10:55:54 -0800504 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
505 if (texture) {
506 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700507 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800508
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000509 SkAutoLockPixels alp(*bitmap);
510 if (!bitmap->readyToDraw()) {
511 return NULL;
512 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000513
bsalomon23e619c2015-02-06 11:54:28 -0800514 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
515 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800516}
517
bsalomonb4d40ef2015-07-15 10:12:16 -0700518static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
519 SkBitmap stretched;
520 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
521 SkCanvas canvas(stretched);
522 SkPaint paint;
523 switch (stretch.fType) {
524 case Stretch::kNearest_Type:
525 paint.setFilterQuality(kNone_SkFilterQuality);
526 break;
527 case Stretch::kBilerp_Type:
528 paint.setFilterQuality(kLow_SkFilterQuality);
529 break;
530 case Stretch::kNone_Type:
531 SkDEBUGFAIL("Shouldn't get here.");
532 break;
533 }
534 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700535 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700536 return stretched;
537}
538
bsalomon37f9a262015-02-02 13:00:10 -0800539static GrTexture* create_bitmap_texture(GrContext* ctx,
540 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700541 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800542 const GrUniqueKey& unstretchedKey,
543 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700544 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800545 SkAutoTUnref<GrTexture> unstretched;
546 // Check if we have the unstretched version in the cache, if not create it.
547 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700548 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800549 }
550 if (!unstretched) {
551 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
552 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700553 // We might not have been able to create a unstrecthed texture because it is smaller
554 // than the min texture size. In that case do cpu stretching.
555 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
556 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800557 }
558 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700559 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800560 }
bsalomon37f9a262015-02-02 13:00:10 -0800561 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000562}
563
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000564bool GrIsBitmapInCache(const GrContext* ctx,
565 const SkBitmap& bitmap,
566 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700567 Stretch stretch;
568 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800569
570 // Handle the case where the bitmap is explicitly texture backed.
571 GrTexture* texture = bitmap.getTexture();
572 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700573 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800574 return true;
575 }
576 // No keys for volatile bitmaps.
577 if (bitmap.isVolatile()) {
578 return false;
579 }
bsalomon8718aaf2015-02-19 07:24:21 -0800580 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800581 if (!key.isValid()) {
582 return false;
583 }
bsalomon8718aaf2015-02-19 07:24:21 -0800584 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800585 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700586 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800587 }
588
bsalomon37f9a262015-02-02 13:00:10 -0800589 // We don't cache volatile bitmaps
590 if (bitmap.isVolatile()) {
591 return false;
592 }
593
bsalomon8718aaf2015-02-19 07:24:21 -0800594 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800595 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700596 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700597 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000598}
reed@google.comac10a2d2010-12-22 21:39:39 +0000599
bsalomonbcf0a522014-10-08 08:40:09 -0700600GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
601 const SkBitmap& bitmap,
602 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000603
bsalomonc59a1df2015-06-01 07:13:42 -0700604 Stretch stretch;
605 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800606
607 GrTexture* result = bitmap.getTexture();
608 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700609 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800610 return SkRef(result);
611 }
bsalomon8718aaf2015-02-19 07:24:21 -0800612 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800613 // Don't create a key for the resized version if the bmp is volatile.
614 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800615 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800616 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800617 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700618 GrTexture* stretched =
619 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800620 if (stretched) {
621 return stretched;
622 }
623 }
624 }
bsalomonc59a1df2015-06-01 07:13:42 -0700625 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800626 }
627
bsalomon8718aaf2015-02-19 07:24:21 -0800628 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000629
bsalomon37f9a262015-02-02 13:00:10 -0800630 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000631 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800632 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000633
bsalomond309e7a2015-04-30 14:18:54 -0700634 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
635 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800636 if (result) {
637 return result;
638 }
639 }
bsalomone137db82015-01-31 20:10:56 -0800640
bsalomon37f9a262015-02-02 13:00:10 -0800641 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
642 if (result) {
643 return result;
644 }
bsalomone137db82015-01-31 20:10:56 -0800645
joshualitt5f5a8d72015-02-25 14:09:45 -0800646 SkErrorInternals::SetError( kInternalError_SkError,
647 "---- failed to create texture for cache [%d %d]\n",
648 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800649
650 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000651}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000652///////////////////////////////////////////////////////////////////////////////
653
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000654// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
655// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800656GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000657 switch (ct) {
658 case kUnknown_SkColorType:
659 return kUnknown_GrPixelConfig;
660 case kAlpha_8_SkColorType:
661 return kAlpha_8_GrPixelConfig;
662 case kRGB_565_SkColorType:
663 return kRGB_565_GrPixelConfig;
664 case kARGB_4444_SkColorType:
665 return kRGBA_4444_GrPixelConfig;
666 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700667 //if (kSRGB_SkColorProfileType == pt) {
668 // return kSRGBA_8888_GrPixelConfig;
669 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000670 return kRGBA_8888_GrPixelConfig;
671 case kBGRA_8888_SkColorType:
672 return kBGRA_8888_GrPixelConfig;
673 case kIndex_8_SkColorType:
674 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700675 case kGray_8_SkColorType:
676 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000677 }
678 SkASSERT(0); // shouldn't get here
679 return kUnknown_GrPixelConfig;
680}
681
jvanverthfa1e8a72014-12-22 08:31:49 -0800682bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
683 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000684 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800685 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000686 switch (config) {
687 case kAlpha_8_GrPixelConfig:
688 ct = kAlpha_8_SkColorType;
689 break;
690 case kIndex_8_GrPixelConfig:
691 ct = kIndex_8_SkColorType;
692 break;
693 case kRGB_565_GrPixelConfig:
694 ct = kRGB_565_SkColorType;
695 break;
696 case kRGBA_4444_GrPixelConfig:
697 ct = kARGB_4444_SkColorType;
698 break;
699 case kRGBA_8888_GrPixelConfig:
700 ct = kRGBA_8888_SkColorType;
701 break;
702 case kBGRA_8888_GrPixelConfig:
703 ct = kBGRA_8888_SkColorType;
704 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800705 case kSRGBA_8888_GrPixelConfig:
706 ct = kRGBA_8888_SkColorType;
707 pt = kSRGB_SkColorProfileType;
708 break;
reed@google.combf790232013-12-13 19:45:58 +0000709 default:
710 return false;
711 }
712 if (ctOut) {
713 *ctOut = ct;
714 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800715 if (ptOut) {
716 *ptOut = pt;
717 }
reed@google.combf790232013-12-13 19:45:58 +0000718 return true;
719}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000720
721///////////////////////////////////////////////////////////////////////////////
722
bsalomonbed83a62015-04-15 14:18:34 -0700723bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800724 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000725
726 grPaint->setDither(skPaint.isDither());
727 grPaint->setAntiAlias(skPaint.isAntiAlias());
728
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000729 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800730 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800731 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000732 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700733 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800734 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000735 }
egdaniel378092f2014-12-03 10:40:13 -0800736 SkASSERT(xpFactory);
737 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800738
dandov9de5b512014-06-10 14:38:28 -0700739 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700740 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000741
742 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700743 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000744 // if the source color is a constant then apply the filter here once rather than per pixel
745 // in a shader.
746 if (constantColor) {
747 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
748 grPaint->setColor(SkColor2GrColor(filtered));
749 } else {
reedcff10b22015-03-03 06:41:45 -0800750 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700751 // return false if failed?
joshualitt9cc17752015-07-09 06:28:14 -0700752 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
joshualitt2cff1762015-07-08 07:58:18 -0700753 &array)) {
reedcff10b22015-03-03 06:41:45 -0800754 for (int i = 0; i < array.count(); ++i) {
755 grPaint->addColorProcessor(array[i]);
756 array[i]->unref();
757 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000758 }
759 }
760 }
krajcevskif461a8f2014-06-19 14:14:06 -0700761
762#ifndef SK_IGNORE_GPU_DITHER
763 // If the dither flag is set, then we need to see if the underlying context
764 // supports it. If not, then install a dither effect.
765 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
766 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800767 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700768
769 // Suspect the dithering flag has no effect on these configs, otherwise
770 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800771 if (GrPixelConfigIs8888(rt->config()) ||
772 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700773 // The dither flag is set and the target is likely
774 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700775 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
776 if (fp.get()) {
777 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700778 grPaint->setDither(false);
779 }
780 }
781 }
782#endif
bsalomonbed83a62015-04-15 14:18:34 -0700783 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000784}
785
bsalomonbed83a62015-04-15 14:18:34 -0700786bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
787 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000788 SkShader* shader = skPaint.getShader();
789 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700790 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
791 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000792 }
793
bsalomon83d081a2014-07-08 09:56:10 -0700794 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700795
796 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700797 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700798 // want them messing around with the context.
799 {
bsalomon83d081a2014-07-08 09:56:10 -0700800 // Allow the shader to modify paintColor and also create an effect to be installed as
801 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700802 GrFragmentProcessor* fp = NULL;
joshualitt8ca93e72015-07-08 06:51:43 -0700803 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor,
joshualitt9cc17752015-07-09 06:28:14 -0700804 grPaint->getProcessorDataManager(), &fp)) {
bsalomonbed83a62015-04-15 14:18:34 -0700805 return false;
806 }
807 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700808 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700809 constantColor = false;
810 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000811 }
krajcevskif461a8f2014-06-19 14:14:06 -0700812
joshualittb0a8a372014-09-23 09:50:21 -0700813 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700814 // 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 -0700815 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000816}
reed8b26b992015-05-07 15:36:17 -0700817
818SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
819#ifdef SK_DEBUG
820 const GrSurfaceDesc& desc = tex->desc();
821 SkASSERT(w <= desc.fWidth);
822 SkASSERT(h <= desc.fHeight);
823#endif
824 const GrPixelConfig config = tex->config();
825 SkColorType ct;
826 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
827 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
828 ct = kUnknown_SkColorType;
829 }
830 return SkImageInfo::Make(w, h, ct, at);
831}
832
833
834void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
835 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
836 dst->setInfo(info);
837 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
838}
joshualitt9bc39542015-08-12 12:57:54 -0700839
840GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
841 const SkMatrix& viewM,
842 const SkMatrix& localM,
843 bool* doBicubic) {
844 *doBicubic = false;
845 GrTextureParams::FilterMode textureFilterMode;
846 switch (paintFilterQuality) {
847 case kNone_SkFilterQuality:
848 textureFilterMode = GrTextureParams::kNone_FilterMode;
849 break;
850 case kLow_SkFilterQuality:
851 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
852 break;
853 case kMedium_SkFilterQuality: {
854 SkMatrix matrix;
855 matrix.setConcat(viewM, localM);
856 if (matrix.getMinScale() < SK_Scalar1) {
857 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
858 } else {
859 // Don't trigger MIP level generation unnecessarily.
860 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
861 }
862 break;
863 }
864 case kHigh_SkFilterQuality: {
865 SkMatrix matrix;
866 matrix.setConcat(viewM, localM);
867 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
868 break;
869 }
870 default:
871 SkErrorInternals::SetError( kInvalidPaint_SkError,
872 "Sorry, I don't understand the filtering "
873 "mode you asked for. Falling back to "
874 "MIPMaps.");
875 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
876 break;
877
878 }
879 return textureFilterMode;
880}