blob: e0892d8ab974e88db4034b429cba09727ab8cb93 [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"
reed43fe6182015-09-08 08:37:36 -070013#include "GrYUVProvider.h"
14
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000015#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000016#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070017#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070018#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080019#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070020#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000021#include "SkMessageBus.h"
22#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080023#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070024#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080025#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070026#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070027#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070028#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080029#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070030#include "effects/GrXfermodeFragmentProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070031#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070032
krajcevski8c111f72014-06-02 13:51:34 -070033#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070034# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070035# include "etc1.h"
36#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000037
38/* Fill out buffer with the compressed format Ganesh expects from a colortable
39 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000040
41 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000042 we could detect that the colortable.count is <= 16, and then repack the
43 indices as nibbles to save RAM, but it would take more time (i.e. a lot
44 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
47 as the colortable.count says it is.
48 */
bsalomone79a2da2014-10-24 12:42:51 -070049static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070050 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000051
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000052 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000053 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000054 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000055 return;
56 }
57
58 SkColorTable* ctable = bitmap.getColorTable();
59 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000060
reed@google.com7111d462014-03-25 16:20:24 +000061 const int count = ctable->count();
62
63 SkDstPixelInfo dstPI;
64 dstPI.fColorType = kRGBA_8888_SkColorType;
65 dstPI.fAlphaType = kPremul_SkAlphaType;
66 dstPI.fPixels = buffer;
67 dstPI.fRowBytes = count * sizeof(SkPMColor);
68
69 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000070 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000071 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080072 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000073 srcPI.fRowBytes = count * sizeof(SkPMColor);
74
75 srcPI.convertPixelsTo(&dstPI, count, 1);
76
reed@google.comac10a2d2010-12-22 21:39:39 +000077 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070078 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000079
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000080 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000081 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
82 } else {
83 // need to trim off the extra bytes per row
84 size_t width = bitmap.width();
85 size_t rowBytes = bitmap.rowBytes();
86 const char* src = (const char*)bitmap.getPixels();
87 for (int y = 0; y < bitmap.height(); y++) {
88 memcpy(dst, src, width);
89 src += rowBytes;
90 dst += width;
91 }
92 }
93}
94
95////////////////////////////////////////////////////////////////////////////////
96
bsalomonc59a1df2015-06-01 07:13:42 -070097struct Stretch {
98 enum Type {
99 kNone_Type,
100 kBilerp_Type,
101 kNearest_Type
102 } fType;
103 int fWidth;
104 int fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800105};
106
bsalomonc59a1df2015-06-01 07:13:42 -0700107static void get_stretch(const GrContext* ctx, int width, int height,
108 const GrTextureParams* params, Stretch* stretch) {
109 stretch->fType = Stretch::kNone_Type;
110 bool doStretch = false;
111 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
112 (!SkIsPow2(width) || !SkIsPow2(height))) {
113 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700114 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
115 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
116 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700117 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
118 // up using the GPU. If issues persist we may need to CPU-stretch.
119 doStretch = true;
120 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
121 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800122 }
bsalomonc59a1df2015-06-01 07:13:42 -0700123 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700124 if (params) {
125 switch(params->filterMode()) {
126 case GrTextureParams::kNone_FilterMode:
127 stretch->fType = Stretch::kNearest_Type;
128 break;
129 case GrTextureParams::kBilerp_FilterMode:
130 case GrTextureParams::kMipMap_FilterMode:
131 stretch->fType = Stretch::kBilerp_Type;
132 break;
133 }
134 } else {
135 stretch->fType = Stretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700136 }
137 } else {
138 stretch->fWidth = -1;
139 stretch->fHeight = -1;
140 stretch->fType = Stretch::kNone_Type;
141 }
bsalomon37f9a262015-02-02 13:00:10 -0800142}
143
bsalomonc59a1df2015-06-01 07:13:42 -0700144static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800145 GrUniqueKey* stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700146 if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
147 uint32_t width = SkToU16(stretch.fWidth);
148 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800149 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700150 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700151 builder[0] = stretch.fType;
152 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800153 builder.finish();
154 return true;
155 }
bsalomon23e619c2015-02-06 11:54:28 -0800156 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800157 return false;
158}
159
reed85d91782015-09-10 14:33:38 -0700160static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
161 SkASSERT(SkIsU16(subset.width()));
162 SkASSERT(SkIsU16(subset.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);
reed8f343722015-08-13 13:32:39 -0700166 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700167 builder[1] = subset.x();
168 builder[2] = subset.y();
169 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800170}
bsalomon37f9a262015-02-02 13:00:10 -0800171
reed85d91782015-09-10 14:33:38 -0700172void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
reed8f343722015-08-13 13:32:39 -0700173 const GrCaps& caps, SkImageUsageType usage) {
174 const Stretch::Type stretches[] = {
175 Stretch::kNone_Type, // kUntiled_SkImageUsageType
176 Stretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
177 Stretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
178 };
179
reed85d91782015-09-10 14:33:38 -0700180 const bool isPow2 = SkIsPow2(subset.width()) && SkIsPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700181 const bool needToStretch = !isPow2 &&
182 usage != kUntiled_SkImageUsageType &&
183 !caps.npotTextureTileSupport();
184
185 if (needToStretch) {
186 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700187 make_unstretched_key(&tmpKey, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700188
189 Stretch stretch;
190 stretch.fType = stretches[usage];
reed85d91782015-09-10 14:33:38 -0700191 stretch.fWidth = SkNextPow2(subset.width());
192 stretch.fHeight = SkNextPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700193 if (!make_stretched_key(tmpKey, stretch, key)) {
194 goto UNSTRETCHED;
195 }
196 } else {
197 UNSTRETCHED:
reed85d91782015-09-10 14:33:38 -0700198 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700199 }
200}
201
reed85d91782015-09-10 14:33:38 -0700202static void make_image_keys(uint32_t imageID, const SkIRect& subset, const Stretch& stretch,
203 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
204 make_unstretched_key(key, imageID, subset);
bsalomonc59a1df2015-06-01 07:13:42 -0700205 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800206 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800207 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000208}
209
reed3322a812015-09-16 10:09:24 -0700210GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
211 GrSurfaceDesc desc;
212 desc.fFlags = kNone_GrSurfaceFlags;
213 desc.fWidth = info.width();
214 desc.fHeight = info.height();
215 desc.fConfig = SkImageInfo2GrPixelConfig(info);
216 desc.fSampleCnt = 0;
217 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000218}
219
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000220namespace {
221
222// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800223class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000224public:
bsalomon8718aaf2015-02-19 07:24:21 -0800225 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000226private:
bsalomon8718aaf2015-02-19 07:24:21 -0800227 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000228
mtklein36352bf2015-03-25 18:17:31 -0700229 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800230 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000231 }
232};
233
234} // namespace
235
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000236
reed43fe6182015-09-08 08:37:36 -0700237GrTexture* GrCreateTextureForPixels(GrContext* ctx,
238 const GrUniqueKey& optionalKey,
239 GrSurfaceDesc desc,
240 SkPixelRef* pixelRefForInvalidationNotification,
241 const void* pixels,
242 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700243 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800244 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700245 if (pixelRefForInvalidationNotification) {
246 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
247 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
248 }
bsalomond309e7a2015-04-30 14:18:54 -0700249 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700250 }
251 return result;
252}
253
bsalomonc59a1df2015-06-01 07:13:42 -0700254// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
255// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
256// filtering and the size to stretch the texture to.
257GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
258 SkPixelRef* pixelRef,
259 const GrUniqueKey& optionalKey) {
260 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800261
262 GrContext* context = inputTexture->getContext();
263 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700264 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800265
266 // Either it's a cache miss or the original wasn't cached to begin with.
267 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800268 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700269 rtDesc.fWidth = stretch.fWidth;
270 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800271 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
272
273 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
274 // fail.
bsalomon76228632015-05-29 08:02:10 -0700275 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800276 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700277 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800278 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700279 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800280 rtDesc.fConfig = kSkia8888_GrPixelConfig;
281 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700282 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800283 }
284 } else if (kRGB_GrColorComponentFlags ==
285 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700286 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800287 rtDesc.fConfig = kSkia8888_GrPixelConfig;
288 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700289 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800290 }
291 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700292 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800293 }
294 }
295
reed43fe6182015-09-08 08:37:36 -0700296 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
297 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800298 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700299 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800300 }
301 GrPaint paint;
302
303 // If filtering is not desired then we want to ensure all texels in the resampled image are
304 // copies of texels from the original.
305 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700306 Stretch::kBilerp_Type == stretch.fType ?
307 GrTextureParams::kBilerp_FilterMode :
308 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800309 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
310
311 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
312 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
313
robertphillipsc9a37062015-09-01 08:34:28 -0700314 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700315 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700316 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700317 }
318
319 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
320 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800321
reed43fe6182015-09-08 08:37:36 -0700322 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800323}
324
reed43fe6182015-09-08 08:37:36 -0700325GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
326 int expectedW, int expectedH,
327 const void** outStartOfDataToUpload) {
328 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700329#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700330 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
331 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700332 }
333
reed43fe6182015-09-08 08:37:36 -0700334 const uint8_t* bytes = data->bytes();
335 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700336 // Does the data match the dimensions of the bitmap? If not,
337 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700338 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
339 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
340 {
341 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700342 }
343
reed43fe6182015-09-08 08:37:36 -0700344 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
345 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700346 } else if (SkKTXFile::is_ktx(bytes)) {
347 SkKTXFile ktx(data);
348
349 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700350 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700351 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700352 }
353
354 // Does the data match the dimensions of the bitmap? If not,
355 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700356 if (ktx.width() != expectedW || ktx.height() != expectedH) {
357 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800358 }
krajcevski99ffe242014-06-03 13:04:35 -0700359
reed43fe6182015-09-08 08:37:36 -0700360 *outStartOfDataToUpload = ktx.pixelData();
361 return kETC1_GrPixelConfig;
362 }
363#endif
364 return kUnknown_GrPixelConfig;
365}
366
367static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
368 const SkBitmap &bm, GrSurfaceDesc desc) {
369 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
370 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700371 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700372 }
373
reed43fe6182015-09-08 08:37:36 -0700374 const void* startOfTexData;
375 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
376 &startOfTexData);
377 if (kUnknown_GrPixelConfig == desc.fConfig) {
378 return nullptr;
379 }
380
381 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700382}
reed43fe6182015-09-08 08:37:36 -0700383
384/*
385 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
386 * be gone from SkPixelRef, and we can remove this subclass entirely.
387 */
388class PixelRef_GrYUVProvider : public GrYUVProvider {
389 SkPixelRef* fPR;
390
391public:
392 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
393
394 uint32_t onGetID() override { return fPR->getGenerationID(); }
395 bool onGetYUVSizes(SkISize sizes[3]) override {
396 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
397 }
398 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
399 SkYUVColorSpace* space) override {
400 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
401 }
402};
krajcevski9c0e6292014-06-02 07:38:14 -0700403
bsalomon8718aaf2015-02-19 07:24:21 -0800404static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700405 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700406 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700407 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700408 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800409 (pixelRef->info().width() != bm.info().width()) ||
410 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700411 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700412 }
413
sugoiba18f912015-02-04 10:53:03 -0800414 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700415 PixelRef_GrYUVProvider provider(pixelRef);
416 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
417 if (!texture) {
418 return nullptr;
419 }
420
sugoiba18f912015-02-04 10:53:03 -0800421 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700422 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
423 pixelRef->addGenIDChangeListener(listener);
424 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800425 }
reed43fe6182015-09-08 08:37:36 -0700426 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700427}
428
bsalomon37f9a262015-02-02 13:00:10 -0800429static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
430 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800431 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700432 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
433 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700434 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700435 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 SkBitmap tmpBitmap;
437
438 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439
reed3322a812015-09-16 10:09:24 -0700440 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700441 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442
reed0689d7b2014-06-14 05:30:20 -0700443 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700444 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700445 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
446 bitmap->width(), bitmap->height());
447 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700448 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
450 // our compressed data will be trimmed, so pass width() for its
451 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700452 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
453 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000455 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000456 // now bitmap points to our temp, which has been promoted to 32bits
457 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000458 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 }
reed43fe6182015-09-08 08:37:36 -0700460 } else if (!bitmap->readyToDraw()) {
461 // If the bitmap had compressed data and was then uncompressed, it'll still return
462 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
463 // the bitmap has available pixels, then they might not be what the decompressed
464 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800465 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700466 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700467 return texture;
468 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000469 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
bsalomond2a6f4e2015-02-04 10:55:54 -0800471 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
472 if (texture) {
473 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700474 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800475
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000476 SkAutoLockPixels alp(*bitmap);
477 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700478 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000479 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000480
reed43fe6182015-09-08 08:37:36 -0700481 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
482 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800483}
484
bsalomonb4d40ef2015-07-15 10:12:16 -0700485static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
486 SkBitmap stretched;
487 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
488 SkCanvas canvas(stretched);
489 SkPaint paint;
490 switch (stretch.fType) {
491 case Stretch::kNearest_Type:
492 paint.setFilterQuality(kNone_SkFilterQuality);
493 break;
494 case Stretch::kBilerp_Type:
495 paint.setFilterQuality(kLow_SkFilterQuality);
496 break;
497 case Stretch::kNone_Type:
498 SkDEBUGFAIL("Shouldn't get here.");
499 break;
500 }
501 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700502 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700503 return stretched;
504}
505
bsalomon37f9a262015-02-02 13:00:10 -0800506static GrTexture* create_bitmap_texture(GrContext* ctx,
507 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700508 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800509 const GrUniqueKey& unstretchedKey,
510 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700511 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800512 SkAutoTUnref<GrTexture> unstretched;
513 // Check if we have the unstretched version in the cache, if not create it.
514 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700515 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800516 }
517 if (!unstretched) {
518 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
519 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700520 // We might not have been able to create a unstrecthed texture because it is smaller
521 // than the min texture size. In that case do cpu stretching.
522 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
523 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800524 }
525 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700526 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800527 }
bsalomon37f9a262015-02-02 13:00:10 -0800528 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
reed85d91782015-09-10 14:33:38 -0700531bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
532 GrTexture* nativeTexture, const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700533 Stretch stretch;
reed85d91782015-09-10 14:33:38 -0700534 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800535
reed85d91782015-09-10 14:33:38 -0700536 // Handle the case where the bitmap/image is explicitly texture backed.
537 if (nativeTexture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700538 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800539 return true;
540 }
reed85d91782015-09-10 14:33:38 -0700541 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800542 if (!key.isValid()) {
543 return false;
544 }
bsalomon8718aaf2015-02-19 07:24:21 -0800545 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800546 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700547 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800548 }
549
bsalomon8718aaf2015-02-19 07:24:21 -0800550 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700551 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700552 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700553 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000554}
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
reed85d91782015-09-10 14:33:38 -0700556bool GrIsBitmapInCache(const GrContext* ctx, const SkBitmap& bitmap,
557 const GrTextureParams* params) {
558 if (bitmap.isVolatile()) {
559 return false; // we don't cache volatile bitmaps.
560 }
561 return GrIsImageInCache(ctx, bitmap.getGenerationID(), bitmap.getSubset(), bitmap.getTexture(),
562 params);
563}
564
bsalomonbcf0a522014-10-08 08:40:09 -0700565GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
566 const SkBitmap& bitmap,
567 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000568
bsalomonc59a1df2015-06-01 07:13:42 -0700569 Stretch stretch;
570 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800571
572 GrTexture* result = bitmap.getTexture();
573 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700574 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800575 return SkRef(result);
576 }
bsalomon8718aaf2015-02-19 07:24:21 -0800577 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800578 // Don't create a key for the resized version if the bmp is volatile.
579 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800580 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800581 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800582 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700583 GrTexture* stretched =
584 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800585 if (stretched) {
586 return stretched;
587 }
588 }
589 }
bsalomonc59a1df2015-06-01 07:13:42 -0700590 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800591 }
592
bsalomon8718aaf2015-02-19 07:24:21 -0800593 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000594
bsalomon37f9a262015-02-02 13:00:10 -0800595 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000596 // If the bitmap isn't changing try to find a cached copy first.
reed85d91782015-09-10 14:33:38 -0700597 make_image_keys(bitmap.getGenerationID(), bitmap.getSubset(), stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000598
bsalomond309e7a2015-04-30 14:18:54 -0700599 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
600 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800601 if (result) {
602 return result;
603 }
604 }
bsalomone137db82015-01-31 20:10:56 -0800605
bsalomon37f9a262015-02-02 13:00:10 -0800606 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
607 if (result) {
608 return result;
609 }
bsalomone137db82015-01-31 20:10:56 -0800610
joshualitt5f5a8d72015-02-25 14:09:45 -0800611 SkErrorInternals::SetError( kInternalError_SkError,
612 "---- failed to create texture for cache [%d %d]\n",
613 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800614
halcanary96fcdcc2015-08-27 07:41:13 -0700615 return nullptr;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000616}
reed8f343722015-08-13 13:32:39 -0700617
618// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
619// into a wrapper that contains the inverse of these tables.
620GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
621 const SkBitmap& bitmap,
622 SkImageUsageType usage) {
623 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
624 // tell us the specifics about filter level or specific tiling.
625
626 const SkShader::TileMode tiles[] = {
627 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
628 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
629 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
630 };
631
632 const GrTextureParams::FilterMode filters[] = {
633 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
634 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
635 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
636 };
637
638 GrTextureParams params(tiles[usage], filters[usage]);
639 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
640}
641
rileya@google.com24f3ad12012-07-18 21:47:40 +0000642///////////////////////////////////////////////////////////////////////////////
643
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000644// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
645// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800646GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000647 switch (ct) {
648 case kUnknown_SkColorType:
649 return kUnknown_GrPixelConfig;
650 case kAlpha_8_SkColorType:
651 return kAlpha_8_GrPixelConfig;
652 case kRGB_565_SkColorType:
653 return kRGB_565_GrPixelConfig;
654 case kARGB_4444_SkColorType:
655 return kRGBA_4444_GrPixelConfig;
656 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700657 //if (kSRGB_SkColorProfileType == pt) {
658 // return kSRGBA_8888_GrPixelConfig;
659 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000660 return kRGBA_8888_GrPixelConfig;
661 case kBGRA_8888_SkColorType:
662 return kBGRA_8888_GrPixelConfig;
663 case kIndex_8_SkColorType:
664 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700665 case kGray_8_SkColorType:
666 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000667 }
668 SkASSERT(0); // shouldn't get here
669 return kUnknown_GrPixelConfig;
670}
671
jvanverthfa1e8a72014-12-22 08:31:49 -0800672bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
673 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000674 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800675 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000676 switch (config) {
677 case kAlpha_8_GrPixelConfig:
678 ct = kAlpha_8_SkColorType;
679 break;
680 case kIndex_8_GrPixelConfig:
681 ct = kIndex_8_SkColorType;
682 break;
683 case kRGB_565_GrPixelConfig:
684 ct = kRGB_565_SkColorType;
685 break;
686 case kRGBA_4444_GrPixelConfig:
687 ct = kARGB_4444_SkColorType;
688 break;
689 case kRGBA_8888_GrPixelConfig:
690 ct = kRGBA_8888_SkColorType;
691 break;
692 case kBGRA_8888_GrPixelConfig:
693 ct = kBGRA_8888_SkColorType;
694 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800695 case kSRGBA_8888_GrPixelConfig:
696 ct = kRGBA_8888_SkColorType;
697 pt = kSRGB_SkColorProfileType;
698 break;
reed@google.combf790232013-12-13 19:45:58 +0000699 default:
700 return false;
701 }
702 if (ctOut) {
703 *ctOut = ct;
704 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800705 if (ptOut) {
706 *ptOut = pt;
707 }
reed@google.combf790232013-12-13 19:45:58 +0000708 return true;
709}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000710
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000711
bsalomonf1b7a1d2015-09-28 06:26:28 -0700712////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000713
bsalomonf1b7a1d2015-09-28 06:26:28 -0700714static inline bool skpaint_to_grpaint_impl(GrContext* context,
715 const SkPaint& skPaint,
716 const SkMatrix& viewM,
717 const GrFragmentProcessor** shaderProcessor,
718 SkXfermode::Mode* primColorMode,
719 bool primitiveIsSrc,
720 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000721 grPaint->setAntiAlias(skPaint.isAntiAlias());
722
bsalomonf1b7a1d2015-09-28 06:26:28 -0700723 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
724 // of per-vertex colors.
725 SkAutoTUnref<const GrFragmentProcessor> aufp;
726 const GrFragmentProcessor* shaderFP = NULL;
727 if (shaderProcessor) {
728 shaderFP = *shaderProcessor;
729 } else if (const SkShader* shader = skPaint.getShader()) {
730 aufp.reset(shader->asFragmentProcessor(context, viewM, NULL, skPaint.getFilterQuality(),
731 grPaint->getProcessorDataManager()));
732 shaderFP = aufp;
733 if (!shaderFP) {
734 return false;
735 }
736 }
737
738 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
739 // a known constant value. In that case we can simply apply a color filter during this
740 // conversion without converting the color filter to a GrFragmentProcessor.
741 bool applyColorFilterToPaintColor = false;
742 if (shaderFP) {
743 if (primColorMode) {
744 // There is a blend between the primitive color and the shader color. The shader sees
745 // the opaque paint color. The shader's output is blended using the provided mode by
746 // the primitive color. The blended color is then modulated by the paint's alpha.
747
748 // The geometry processor will insert the primitive color to start the color chain, so
749 // the GrPaint color will be ignored.
750
751 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
752
753 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
754 aufp.reset(shaderFP);
755
756 if (primitiveIsSrc) {
757 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
758 *primColorMode);
759 } else {
760 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
761 *primColorMode);
762 }
763 aufp.reset(shaderFP);
764 // The above may return null if compose results in a pass through of the prim color.
765 if (shaderFP) {
766 grPaint->addColorFragmentProcessor(shaderFP);
767 }
768
769 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
770 if (GrColor_WHITE != paintAlpha) {
771 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
772 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
773 }
774 } else {
775 // The shader's FP sees the paint unpremul color
776 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
777 grPaint->addColorFragmentProcessor(shaderFP);
778 }
779 } else {
780 if (primColorMode) {
781 // There is a blend between the primitive color and the paint color. The blend considers
782 // the opaque paint color. The paint's alpha is applied to the post-blended color.
783 SkAutoTUnref<const GrFragmentProcessor> processor(
784 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
785 GrConstColorProcessor::kIgnore_InputMode));
786 if (primitiveIsSrc) {
787 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
788 *primColorMode));
789 } else {
790 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
791 *primColorMode));
792
793 }
794 if (processor) {
795 grPaint->addColorFragmentProcessor(processor);
796 }
797
798 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()) | 0xFF000000);
799
800 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
801 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
802 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
803 } else {
804 // No shader, no primitive color.
805 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
806 applyColorFilterToPaintColor = true;
807 }
808 }
809
810 SkColorFilter* colorFilter = skPaint.getColorFilter();
811 if (colorFilter) {
812 if (applyColorFilterToPaintColor) {
813 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
814 } else {
815 SkTDArray<const GrFragmentProcessor*> array;
816 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
817 &array)) {
818 for (int i = 0; i < array.count(); ++i) {
819 grPaint->addColorFragmentProcessor(array[i])->unref();
820 }
821 } else {
822 return false;
823 }
824 }
825 }
826
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000827 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700828 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800829 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000830 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700831 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800832 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000833 }
egdaniel378092f2014-12-03 10:40:13 -0800834 SkASSERT(xpFactory);
835 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800836
krajcevskif461a8f2014-06-19 14:14:06 -0700837#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700838 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700839 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700840 }
841#endif
bsalomonbed83a62015-04-15 14:18:34 -0700842 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000843}
844
bsalomonf1b7a1d2015-09-28 06:26:28 -0700845bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
846 GrPaint* grPaint) {
847 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
848}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000849
bsalomonf1b7a1d2015-09-28 06:26:28 -0700850/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
851bool SkPaintToGrPaintReplaceShader(GrContext* context,
852 const SkPaint& skPaint,
853 const GrFragmentProcessor* shaderFP,
854 GrPaint* grPaint) {
855 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700856 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000857 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700858 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
859 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000860}
reed8b26b992015-05-07 15:36:17 -0700861
bsalomonf1b7a1d2015-09-28 06:26:28 -0700862/** Ignores the SkShader (if any) on skPaint. */
863bool SkPaintToGrPaintNoShader(GrContext* context,
864 const SkPaint& skPaint,
865 GrPaint* grPaint) {
866 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
867 static const GrFragmentProcessor* kNullShaderFP = nullptr;
868 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
869 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
870 grPaint);
871}
872
873/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
874be setup as a vertex attribute using the specified SkXfermode::Mode. */
875bool SkPaintToGrPaintWithXfermode(GrContext* context,
876 const SkPaint& skPaint,
877 const SkMatrix& viewM,
878 SkXfermode::Mode primColorMode,
879 bool primitiveIsSrc,
880 GrPaint* grPaint) {
881 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
882 grPaint);
883}
884
885
886////////////////////////////////////////////////////////////////////////////////////////////////
887
reed8b26b992015-05-07 15:36:17 -0700888SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
889#ifdef SK_DEBUG
890 const GrSurfaceDesc& desc = tex->desc();
891 SkASSERT(w <= desc.fWidth);
892 SkASSERT(h <= desc.fHeight);
893#endif
894 const GrPixelConfig config = tex->config();
895 SkColorType ct;
896 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700897 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700898 ct = kUnknown_SkColorType;
899 }
900 return SkImageInfo::Make(w, h, ct, at);
901}
902
903
904void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
905 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
906 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700907 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700908}
joshualitt9bc39542015-08-12 12:57:54 -0700909
910GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
911 const SkMatrix& viewM,
912 const SkMatrix& localM,
913 bool* doBicubic) {
914 *doBicubic = false;
915 GrTextureParams::FilterMode textureFilterMode;
916 switch (paintFilterQuality) {
917 case kNone_SkFilterQuality:
918 textureFilterMode = GrTextureParams::kNone_FilterMode;
919 break;
920 case kLow_SkFilterQuality:
921 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
922 break;
923 case kMedium_SkFilterQuality: {
924 SkMatrix matrix;
925 matrix.setConcat(viewM, localM);
926 if (matrix.getMinScale() < SK_Scalar1) {
927 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
928 } else {
929 // Don't trigger MIP level generation unnecessarily.
930 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
931 }
932 break;
933 }
934 case kHigh_SkFilterQuality: {
935 SkMatrix matrix;
936 matrix.setConcat(viewM, localM);
937 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
938 break;
939 }
940 default:
941 SkErrorInternals::SetError( kInvalidPaint_SkError,
942 "Sorry, I don't understand the filtering "
943 "mode you asked for. Falling back to "
944 "MIPMaps.");
945 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
946 break;
947
948 }
949 return textureFilterMode;
950}