blob: cff61727d8d25c0a11d4efa43fff08b7aadce000 [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
reedb5d32632015-09-29 13:36:50 -07008#include "GrTextureMaker.h"
9
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "SkGr.h"
egdaniel378092f2014-12-03 10:40:13 -080011
bsalomon76228632015-05-29 08:02:10 -070012#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070013#include "GrDrawContext.h"
egdaniel378092f2014-12-03 10:40:13 -080014#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070015#include "GrYUVProvider.h"
16
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000017#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000018#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070019#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070020#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080021#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070022#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000023#include "SkMessageBus.h"
24#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080025#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070026#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080027#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070028#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070029#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070030#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080031#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070032#include "effects/GrXfermodeFragmentProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070033#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070034
krajcevski8c111f72014-06-02 13:51:34 -070035#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070036# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070037# include "etc1.h"
38#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000039
reed856e9d92015-09-30 12:21:45 -070040bool GrTextureUsageSupported(const GrCaps& caps, int width, int height, SkImageUsageType usage) {
41 if (caps.npotTextureTileSupport()) {
42 return true;
43 }
44 const bool is_pow2 = SkIsPow2(width) && SkIsPow2(height);
45 return is_pow2 || kUntiled_SkImageUsageType == usage;
46}
47
48GrTextureParams GrImageUsageToTextureParams(SkImageUsageType usage) {
49 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
50 // tell us the specifics about filter level or specific tiling.
51
52 const SkShader::TileMode tiles[] = {
53 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
54 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
55 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
56 };
57
58 const GrTextureParams::FilterMode filters[] = {
59 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
60 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
61 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
62 };
63
64 return GrTextureParams(tiles[usage], filters[usage]);
65}
66
reed@google.comac10a2d2010-12-22 21:39:39 +000067/* Fill out buffer with the compressed format Ganesh expects from a colortable
68 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000069
70 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000071 we could detect that the colortable.count is <= 16, and then repack the
72 indices as nibbles to save RAM, but it would take more time (i.e. a lot
73 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000074
reed@google.comac10a2d2010-12-22 21:39:39 +000075 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
76 as the colortable.count says it is.
77 */
bsalomone79a2da2014-10-24 12:42:51 -070078static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070079 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000080
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000081 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000082 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000083 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000084 return;
85 }
86
87 SkColorTable* ctable = bitmap.getColorTable();
88 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000089
reed@google.com7111d462014-03-25 16:20:24 +000090 const int count = ctable->count();
91
92 SkDstPixelInfo dstPI;
93 dstPI.fColorType = kRGBA_8888_SkColorType;
94 dstPI.fAlphaType = kPremul_SkAlphaType;
95 dstPI.fPixels = buffer;
96 dstPI.fRowBytes = count * sizeof(SkPMColor);
97
98 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000099 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000100 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800101 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000102 srcPI.fRowBytes = count * sizeof(SkPMColor);
103
104 srcPI.convertPixelsTo(&dstPI, count, 1);
105
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700107 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000109 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
111 } else {
112 // need to trim off the extra bytes per row
113 size_t width = bitmap.width();
114 size_t rowBytes = bitmap.rowBytes();
115 const char* src = (const char*)bitmap.getPixels();
116 for (int y = 0; y < bitmap.height(); y++) {
117 memcpy(dst, src, width);
118 src += rowBytes;
119 dst += width;
120 }
121 }
122}
123
124////////////////////////////////////////////////////////////////////////////////
125
bsalomonc59a1df2015-06-01 07:13:42 -0700126static void get_stretch(const GrContext* ctx, int width, int height,
reedb5d32632015-09-29 13:36:50 -0700127 const GrTextureParams* params, SkGrStretch* stretch) {
128 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700129 bool doStretch = false;
130 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
131 (!SkIsPow2(width) || !SkIsPow2(height))) {
132 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700133 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
134 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
135 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700136 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
137 // up using the GPU. If issues persist we may need to CPU-stretch.
138 doStretch = true;
139 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
140 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800141 }
bsalomonc59a1df2015-06-01 07:13:42 -0700142 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700143 if (params) {
144 switch(params->filterMode()) {
145 case GrTextureParams::kNone_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700146 stretch->fType = SkGrStretch::kNearest_Type;
bsalomon0513c832015-06-01 10:22:48 -0700147 break;
148 case GrTextureParams::kBilerp_FilterMode:
149 case GrTextureParams::kMipMap_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700150 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomon0513c832015-06-01 10:22:48 -0700151 break;
152 }
153 } else {
reedb5d32632015-09-29 13:36:50 -0700154 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700155 }
156 } else {
157 stretch->fWidth = -1;
158 stretch->fHeight = -1;
reedb5d32632015-09-29 13:36:50 -0700159 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700160 }
bsalomon37f9a262015-02-02 13:00:10 -0800161}
162
reed856e9d92015-09-30 12:21:45 -0700163bool GrMakeStretchedKey(const GrUniqueKey& origKey, const SkGrStretch& stretch,
164 GrUniqueKey* stretchedKey) {
reedb5d32632015-09-29 13:36:50 -0700165 if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) {
bsalomonc59a1df2015-06-01 07:13:42 -0700166 uint32_t width = SkToU16(stretch.fWidth);
167 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800168 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700169 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700170 builder[0] = stretch.fType;
171 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800172 builder.finish();
173 return true;
174 }
bsalomon23e619c2015-02-06 11:54:28 -0800175 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800176 return false;
177}
178
reed85d91782015-09-10 14:33:38 -0700179static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
180 SkASSERT(SkIsU16(subset.width()));
181 SkASSERT(SkIsU16(subset.height()));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000182
bsalomon8718aaf2015-02-19 07:24:21 -0800183 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
184 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700185 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700186 builder[1] = subset.x();
187 builder[2] = subset.y();
188 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800189}
bsalomon37f9a262015-02-02 13:00:10 -0800190
reed85d91782015-09-10 14:33:38 -0700191void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
reed8f343722015-08-13 13:32:39 -0700192 const GrCaps& caps, SkImageUsageType usage) {
reedb5d32632015-09-29 13:36:50 -0700193 const SkGrStretch::Type stretches[] = {
194 SkGrStretch::kNone_Type, // kUntiled_SkImageUsageType
195 SkGrStretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
196 SkGrStretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
reed8f343722015-08-13 13:32:39 -0700197 };
198
reed856e9d92015-09-30 12:21:45 -0700199 if (!GrTextureUsageSupported(caps, subset.width(), subset.height(), usage)) {
reed8f343722015-08-13 13:32:39 -0700200 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700201 make_unstretched_key(&tmpKey, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700202
reedb5d32632015-09-29 13:36:50 -0700203 SkGrStretch stretch;
reed8f343722015-08-13 13:32:39 -0700204 stretch.fType = stretches[usage];
reed85d91782015-09-10 14:33:38 -0700205 stretch.fWidth = SkNextPow2(subset.width());
206 stretch.fHeight = SkNextPow2(subset.height());
reed856e9d92015-09-30 12:21:45 -0700207 if (!GrMakeStretchedKey(tmpKey, stretch, key)) {
reed8f343722015-08-13 13:32:39 -0700208 goto UNSTRETCHED;
209 }
210 } else {
211 UNSTRETCHED:
reed85d91782015-09-10 14:33:38 -0700212 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700213 }
214}
215
reedb5d32632015-09-29 13:36:50 -0700216static void make_image_keys(uint32_t imageID, const SkIRect& subset, const SkGrStretch& stretch,
reed85d91782015-09-10 14:33:38 -0700217 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
218 make_unstretched_key(key, imageID, subset);
reedb5d32632015-09-29 13:36:50 -0700219 if (SkGrStretch::kNone_Type != stretch.fType) {
reed856e9d92015-09-30 12:21:45 -0700220 GrMakeStretchedKey(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800221 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000222}
223
reed3322a812015-09-16 10:09:24 -0700224GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
225 GrSurfaceDesc desc;
226 desc.fFlags = kNone_GrSurfaceFlags;
227 desc.fWidth = info.width();
228 desc.fHeight = info.height();
229 desc.fConfig = SkImageInfo2GrPixelConfig(info);
230 desc.fSampleCnt = 0;
231 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000232}
233
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000234namespace {
235
236// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800237class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000238public:
bsalomon8718aaf2015-02-19 07:24:21 -0800239 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000240private:
bsalomon8718aaf2015-02-19 07:24:21 -0800241 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000242
mtklein36352bf2015-03-25 18:17:31 -0700243 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800244 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000245 }
246};
247
248} // namespace
249
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000250
reed43fe6182015-09-08 08:37:36 -0700251GrTexture* GrCreateTextureForPixels(GrContext* ctx,
252 const GrUniqueKey& optionalKey,
253 GrSurfaceDesc desc,
254 SkPixelRef* pixelRefForInvalidationNotification,
255 const void* pixels,
256 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700257 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800258 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700259 if (pixelRefForInvalidationNotification) {
260 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
261 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
262 }
bsalomond309e7a2015-04-30 14:18:54 -0700263 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700264 }
265 return result;
266}
267
bsalomonc59a1df2015-06-01 07:13:42 -0700268// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
269// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
270// filtering and the size to stretch the texture to.
reedb5d32632015-09-29 13:36:50 -0700271GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch,
bsalomonc59a1df2015-06-01 07:13:42 -0700272 SkPixelRef* pixelRef,
273 const GrUniqueKey& optionalKey) {
reedb5d32632015-09-29 13:36:50 -0700274 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800275
276 GrContext* context = inputTexture->getContext();
277 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700278 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800279
280 // Either it's a cache miss or the original wasn't cached to begin with.
281 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800282 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700283 rtDesc.fWidth = stretch.fWidth;
284 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800285 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
286
287 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
288 // fail.
bsalomon76228632015-05-29 08:02:10 -0700289 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800290 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700291 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800292 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700293 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800294 rtDesc.fConfig = kSkia8888_GrPixelConfig;
295 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800297 }
298 } else if (kRGB_GrColorComponentFlags ==
299 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700300 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800301 rtDesc.fConfig = kSkia8888_GrPixelConfig;
302 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700303 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800304 }
305 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700306 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800307 }
308 }
309
reed43fe6182015-09-08 08:37:36 -0700310 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
311 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800312 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700313 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800314 }
315 GrPaint paint;
316
317 // If filtering is not desired then we want to ensure all texels in the resampled image are
318 // copies of texels from the original.
319 GrTextureParams params(SkShader::kClamp_TileMode,
reedb5d32632015-09-29 13:36:50 -0700320 SkGrStretch::kBilerp_Type == stretch.fType ?
bsalomonc59a1df2015-06-01 07:13:42 -0700321 GrTextureParams::kBilerp_FilterMode :
322 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800323 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
324
325 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
326 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
327
robertphillipsc9a37062015-09-01 08:34:28 -0700328 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700329 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700330 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700331 }
332
333 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
334 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800335
reed43fe6182015-09-08 08:37:36 -0700336 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800337}
338
reed43fe6182015-09-08 08:37:36 -0700339GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
340 int expectedW, int expectedH,
341 const void** outStartOfDataToUpload) {
342 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700343#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700344 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
345 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700346 }
347
reed43fe6182015-09-08 08:37:36 -0700348 const uint8_t* bytes = data->bytes();
349 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700350 // Does the data match the dimensions of the bitmap? If not,
351 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700352 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
353 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
354 {
355 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700356 }
357
reed43fe6182015-09-08 08:37:36 -0700358 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
359 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700360 } else if (SkKTXFile::is_ktx(bytes)) {
361 SkKTXFile ktx(data);
362
363 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700364 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700365 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700366 }
367
368 // Does the data match the dimensions of the bitmap? If not,
369 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700370 if (ktx.width() != expectedW || ktx.height() != expectedH) {
371 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800372 }
krajcevski99ffe242014-06-03 13:04:35 -0700373
reed43fe6182015-09-08 08:37:36 -0700374 *outStartOfDataToUpload = ktx.pixelData();
375 return kETC1_GrPixelConfig;
376 }
377#endif
378 return kUnknown_GrPixelConfig;
379}
380
381static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
382 const SkBitmap &bm, GrSurfaceDesc desc) {
383 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
384 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700385 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700386 }
387
reed43fe6182015-09-08 08:37:36 -0700388 const void* startOfTexData;
389 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
390 &startOfTexData);
391 if (kUnknown_GrPixelConfig == desc.fConfig) {
392 return nullptr;
393 }
394
395 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700396}
reed43fe6182015-09-08 08:37:36 -0700397
398/*
399 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
400 * be gone from SkPixelRef, and we can remove this subclass entirely.
401 */
402class PixelRef_GrYUVProvider : public GrYUVProvider {
403 SkPixelRef* fPR;
404
405public:
406 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
407
408 uint32_t onGetID() override { return fPR->getGenerationID(); }
409 bool onGetYUVSizes(SkISize sizes[3]) override {
410 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
411 }
412 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
413 SkYUVColorSpace* space) override {
414 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
415 }
416};
krajcevski9c0e6292014-06-02 07:38:14 -0700417
bsalomon8718aaf2015-02-19 07:24:21 -0800418static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700419 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700420 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700421 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700422 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800423 (pixelRef->info().width() != bm.info().width()) ||
424 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700425 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700426 }
427
sugoiba18f912015-02-04 10:53:03 -0800428 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700429 PixelRef_GrYUVProvider provider(pixelRef);
430 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
431 if (!texture) {
432 return nullptr;
433 }
434
sugoiba18f912015-02-04 10:53:03 -0800435 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700436 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
437 pixelRef->addGenIDChangeListener(listener);
438 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800439 }
reed43fe6182015-09-08 08:37:36 -0700440 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700441}
442
bsalomon37f9a262015-02-02 13:00:10 -0800443static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
444 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800445 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700446 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
447 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700448 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700449 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000450 SkBitmap tmpBitmap;
451
452 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453
reed3322a812015-09-16 10:09:24 -0700454 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700455 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456
reed0689d7b2014-06-14 05:30:20 -0700457 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700458 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700459 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
460 bitmap->width(), bitmap->height());
461 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700462 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
464 // our compressed data will be trimmed, so pass width() for its
465 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700466 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
467 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000469 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // now bitmap points to our temp, which has been promoted to 32bits
471 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000472 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 }
reed43fe6182015-09-08 08:37:36 -0700474 } else if (!bitmap->readyToDraw()) {
475 // If the bitmap had compressed data and was then uncompressed, it'll still return
476 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
477 // the bitmap has available pixels, then they might not be what the decompressed
478 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800479 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700480 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700481 return texture;
482 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomond2a6f4e2015-02-04 10:55:54 -0800485 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
486 if (texture) {
487 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700488 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800489
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000490 SkAutoLockPixels alp(*bitmap);
491 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700492 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000493 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000494
reed43fe6182015-09-08 08:37:36 -0700495 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
496 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800497}
498
reedb5d32632015-09-29 13:36:50 -0700499static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700500 SkBitmap stretched;
501 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
502 SkCanvas canvas(stretched);
503 SkPaint paint;
504 switch (stretch.fType) {
reedb5d32632015-09-29 13:36:50 -0700505 case SkGrStretch::kNearest_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700506 paint.setFilterQuality(kNone_SkFilterQuality);
507 break;
reedb5d32632015-09-29 13:36:50 -0700508 case SkGrStretch::kBilerp_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700509 paint.setFilterQuality(kLow_SkFilterQuality);
510 break;
reedb5d32632015-09-29 13:36:50 -0700511 case SkGrStretch::kNone_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700512 SkDEBUGFAIL("Shouldn't get here.");
513 break;
514 }
515 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700516 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700517 return stretched;
518}
519
reed85d91782015-09-10 14:33:38 -0700520bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
521 GrTexture* nativeTexture, const GrTextureParams* params) {
reedb5d32632015-09-29 13:36:50 -0700522 SkGrStretch stretch;
reed85d91782015-09-10 14:33:38 -0700523 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800524
reed85d91782015-09-10 14:33:38 -0700525 // Handle the case where the bitmap/image is explicitly texture backed.
526 if (nativeTexture) {
reedb5d32632015-09-29 13:36:50 -0700527 if (SkGrStretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800528 return true;
529 }
reed85d91782015-09-10 14:33:38 -0700530 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800531 if (!key.isValid()) {
532 return false;
533 }
bsalomon8718aaf2015-02-19 07:24:21 -0800534 GrUniqueKey stretchedKey;
reed856e9d92015-09-30 12:21:45 -0700535 GrMakeStretchedKey(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700536 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800537 }
538
bsalomon8718aaf2015-02-19 07:24:21 -0800539 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700540 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700541 return ctx->textureProvider()->existsTextureWithUniqueKey(
reedb5d32632015-09-29 13:36:50 -0700542 (SkGrStretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000543}
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
reedb5d32632015-09-29 13:36:50 -0700545class Bitmap_GrTextureMaker : public GrTextureMaker {
546public:
547 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
548 : INHERITED(bitmap.width(), bitmap.height())
549 , fBitmap(bitmap)
550 {}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551
reedb5d32632015-09-29 13:36:50 -0700552protected:
553 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
554 GrTexture* tex = fBitmap.getTexture();
555 if (tex) {
556 return SkRef(tex);
bsalomon88425562015-02-04 09:12:46 -0800557 }
bsalomon88425562015-02-04 09:12:46 -0800558
reedb5d32632015-09-29 13:36:50 -0700559 GrUniqueKey unstretchedKey;
560 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000561
reedb5d32632015-09-29 13:36:50 -0700562 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800563 if (result) {
564 return result;
565 }
reedb5d32632015-09-29 13:36:50 -0700566 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800567 }
bsalomone137db82015-01-31 20:10:56 -0800568
reedb5d32632015-09-29 13:36:50 -0700569 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
570 if (fBitmap.isVolatile()) {
571 return false;
572 }
573
574 GrUniqueKey unstretchedKey;
575 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
reed856e9d92015-09-30 12:21:45 -0700576 return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800577 }
bsalomone137db82015-01-31 20:10:56 -0800578
reedb5d32632015-09-29 13:36:50 -0700579 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
580 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey));
581 }
bsalomon37f9a262015-02-02 13:00:10 -0800582
reedb5d32632015-09-29 13:36:50 -0700583 bool onGetROBitmap(SkBitmap* bitmap) override {
584 *bitmap = fBitmap;
585 return true;
586 }
587
588private:
589 const SkBitmap fBitmap;
590
591 typedef GrTextureMaker INHERITED;
592};
593
594GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
595 const GrTextureParams* params) {
596 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000597}
reed8f343722015-08-13 13:32:39 -0700598
599// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
600// into a wrapper that contains the inverse of these tables.
601GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
602 const SkBitmap& bitmap,
603 SkImageUsageType usage) {
reed856e9d92015-09-30 12:21:45 -0700604 GrTextureParams params = GrImageUsageToTextureParams(usage);
reed8f343722015-08-13 13:32:39 -0700605 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
606}
607
rileya@google.com24f3ad12012-07-18 21:47:40 +0000608///////////////////////////////////////////////////////////////////////////////
609
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000610// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
611// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800612GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000613 switch (ct) {
614 case kUnknown_SkColorType:
615 return kUnknown_GrPixelConfig;
616 case kAlpha_8_SkColorType:
617 return kAlpha_8_GrPixelConfig;
618 case kRGB_565_SkColorType:
619 return kRGB_565_GrPixelConfig;
620 case kARGB_4444_SkColorType:
621 return kRGBA_4444_GrPixelConfig;
622 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700623 //if (kSRGB_SkColorProfileType == pt) {
624 // return kSRGBA_8888_GrPixelConfig;
625 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000626 return kRGBA_8888_GrPixelConfig;
627 case kBGRA_8888_SkColorType:
628 return kBGRA_8888_GrPixelConfig;
629 case kIndex_8_SkColorType:
630 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700631 case kGray_8_SkColorType:
632 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000633 }
634 SkASSERT(0); // shouldn't get here
635 return kUnknown_GrPixelConfig;
636}
637
jvanverthfa1e8a72014-12-22 08:31:49 -0800638bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
639 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000640 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800641 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000642 switch (config) {
643 case kAlpha_8_GrPixelConfig:
644 ct = kAlpha_8_SkColorType;
645 break;
646 case kIndex_8_GrPixelConfig:
647 ct = kIndex_8_SkColorType;
648 break;
649 case kRGB_565_GrPixelConfig:
650 ct = kRGB_565_SkColorType;
651 break;
652 case kRGBA_4444_GrPixelConfig:
653 ct = kARGB_4444_SkColorType;
654 break;
655 case kRGBA_8888_GrPixelConfig:
656 ct = kRGBA_8888_SkColorType;
657 break;
658 case kBGRA_8888_GrPixelConfig:
659 ct = kBGRA_8888_SkColorType;
660 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800661 case kSRGBA_8888_GrPixelConfig:
662 ct = kRGBA_8888_SkColorType;
663 pt = kSRGB_SkColorProfileType;
664 break;
reed@google.combf790232013-12-13 19:45:58 +0000665 default:
666 return false;
667 }
668 if (ctOut) {
669 *ctOut = ct;
670 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800671 if (ptOut) {
672 *ptOut = pt;
673 }
reed@google.combf790232013-12-13 19:45:58 +0000674 return true;
675}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000676
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000677
bsalomonf1b7a1d2015-09-28 06:26:28 -0700678////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000679
bsalomonaa48d362015-10-01 08:34:17 -0700680static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
681 if (primitiveIsSrc) {
682 return SkXfermode::kSrc_Mode != mode;
683 } else {
684 return SkXfermode::kDst_Mode != mode;
685 }
686}
687
bsalomonf1b7a1d2015-09-28 06:26:28 -0700688static inline bool skpaint_to_grpaint_impl(GrContext* context,
689 const SkPaint& skPaint,
690 const SkMatrix& viewM,
691 const GrFragmentProcessor** shaderProcessor,
692 SkXfermode::Mode* primColorMode,
693 bool primitiveIsSrc,
694 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000695 grPaint->setAntiAlias(skPaint.isAntiAlias());
696
bsalomonf1b7a1d2015-09-28 06:26:28 -0700697 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
698 // of per-vertex colors.
699 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700700 const GrFragmentProcessor* shaderFP = nullptr;
701 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
702 if (shaderProcessor) {
703 shaderFP = *shaderProcessor;
704 } else if (const SkShader* shader = skPaint.getShader()) {
705 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700706 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700707 shaderFP = aufp;
708 if (!shaderFP) {
709 return false;
710 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700711 }
712 }
713
714 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
715 // a known constant value. In that case we can simply apply a color filter during this
716 // conversion without converting the color filter to a GrFragmentProcessor.
717 bool applyColorFilterToPaintColor = false;
718 if (shaderFP) {
719 if (primColorMode) {
720 // There is a blend between the primitive color and the shader color. The shader sees
721 // the opaque paint color. The shader's output is blended using the provided mode by
722 // the primitive color. The blended color is then modulated by the paint's alpha.
723
724 // The geometry processor will insert the primitive color to start the color chain, so
725 // the GrPaint color will be ignored.
726
727 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
728
729 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
730 aufp.reset(shaderFP);
731
732 if (primitiveIsSrc) {
733 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
734 *primColorMode);
735 } else {
736 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
737 *primColorMode);
738 }
739 aufp.reset(shaderFP);
740 // The above may return null if compose results in a pass through of the prim color.
741 if (shaderFP) {
742 grPaint->addColorFragmentProcessor(shaderFP);
743 }
744
745 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
746 if (GrColor_WHITE != paintAlpha) {
747 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
748 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
749 }
750 } else {
751 // The shader's FP sees the paint unpremul color
752 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
753 grPaint->addColorFragmentProcessor(shaderFP);
754 }
755 } else {
756 if (primColorMode) {
757 // There is a blend between the primitive color and the paint color. The blend considers
758 // the opaque paint color. The paint's alpha is applied to the post-blended color.
759 SkAutoTUnref<const GrFragmentProcessor> processor(
760 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
761 GrConstColorProcessor::kIgnore_InputMode));
762 if (primitiveIsSrc) {
763 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
764 *primColorMode));
765 } else {
766 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
767 *primColorMode));
768
769 }
770 if (processor) {
771 grPaint->addColorFragmentProcessor(processor);
772 }
773
bsalomonaa48d362015-10-01 08:34:17 -0700774 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700775
776 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700777 if (GrColor_WHITE != paintAlpha) {
778 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
779 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
780 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700781 } else {
782 // No shader, no primitive color.
783 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
784 applyColorFilterToPaintColor = true;
785 }
786 }
787
788 SkColorFilter* colorFilter = skPaint.getColorFilter();
789 if (colorFilter) {
790 if (applyColorFilterToPaintColor) {
791 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
792 } else {
bsalomone25eea42015-09-29 06:38:55 -0700793 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700794 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700795 if (cfFP) {
796 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700797 } else {
798 return false;
799 }
800 }
801 }
802
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000803 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700804 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800805 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000806 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700807 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800808 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000809 }
egdaniel378092f2014-12-03 10:40:13 -0800810 SkASSERT(xpFactory);
811 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800812
krajcevskif461a8f2014-06-19 14:14:06 -0700813#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700814 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700815 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700816 }
817#endif
bsalomonbed83a62015-04-15 14:18:34 -0700818 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000819}
820
bsalomonf1b7a1d2015-09-28 06:26:28 -0700821bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
822 GrPaint* grPaint) {
823 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
824}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000825
bsalomonf1b7a1d2015-09-28 06:26:28 -0700826/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
827bool SkPaintToGrPaintReplaceShader(GrContext* context,
828 const SkPaint& skPaint,
829 const GrFragmentProcessor* shaderFP,
830 GrPaint* grPaint) {
831 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700832 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000833 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700834 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
835 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000836}
reed8b26b992015-05-07 15:36:17 -0700837
bsalomonf1b7a1d2015-09-28 06:26:28 -0700838/** Ignores the SkShader (if any) on skPaint. */
839bool SkPaintToGrPaintNoShader(GrContext* context,
840 const SkPaint& skPaint,
841 GrPaint* grPaint) {
842 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
843 static const GrFragmentProcessor* kNullShaderFP = nullptr;
844 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
845 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
846 grPaint);
847}
848
849/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
850be setup as a vertex attribute using the specified SkXfermode::Mode. */
851bool SkPaintToGrPaintWithXfermode(GrContext* context,
852 const SkPaint& skPaint,
853 const SkMatrix& viewM,
854 SkXfermode::Mode primColorMode,
855 bool primitiveIsSrc,
856 GrPaint* grPaint) {
857 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
858 grPaint);
859}
860
861
862////////////////////////////////////////////////////////////////////////////////////////////////
863
reed8b26b992015-05-07 15:36:17 -0700864SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
865#ifdef SK_DEBUG
866 const GrSurfaceDesc& desc = tex->desc();
867 SkASSERT(w <= desc.fWidth);
868 SkASSERT(h <= desc.fHeight);
869#endif
870 const GrPixelConfig config = tex->config();
871 SkColorType ct;
872 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700873 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700874 ct = kUnknown_SkColorType;
875 }
876 return SkImageInfo::Make(w, h, ct, at);
877}
878
879
880void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
881 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
882 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700883 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700884}
joshualitt9bc39542015-08-12 12:57:54 -0700885
886GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
887 const SkMatrix& viewM,
888 const SkMatrix& localM,
889 bool* doBicubic) {
890 *doBicubic = false;
891 GrTextureParams::FilterMode textureFilterMode;
892 switch (paintFilterQuality) {
893 case kNone_SkFilterQuality:
894 textureFilterMode = GrTextureParams::kNone_FilterMode;
895 break;
896 case kLow_SkFilterQuality:
897 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
898 break;
899 case kMedium_SkFilterQuality: {
900 SkMatrix matrix;
901 matrix.setConcat(viewM, localM);
902 if (matrix.getMinScale() < SK_Scalar1) {
903 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
904 } else {
905 // Don't trigger MIP level generation unnecessarily.
906 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
907 }
908 break;
909 }
910 case kHigh_SkFilterQuality: {
911 SkMatrix matrix;
912 matrix.setConcat(viewM, localM);
913 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
914 break;
915 }
916 default:
917 SkErrorInternals::SetError( kInvalidPaint_SkError,
918 "Sorry, I don't understand the filtering "
919 "mode you asked for. Falling back to "
920 "MIPMaps.");
921 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
922 break;
923
924 }
925 return textureFilterMode;
926}
reedb5d32632015-09-29 13:36:50 -0700927
928////////////////////////////////////////////////////////////////////////////////////////////////
929
reed856e9d92015-09-30 12:21:45 -0700930GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, SkImageUsageType usage) {
931 GrTextureParams params = GrImageUsageToTextureParams(usage);
932 return this->refCachedTexture(ctx, &params);
933}
934
reedb5d32632015-09-29 13:36:50 -0700935GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams* params) {
936 SkGrStretch stretch;
937 get_stretch(ctx, this->width(), this->height(), params, &stretch);
938
939 if (SkGrStretch::kNone_Type == stretch.fType) {
940 return this->onRefUnstretchedTexture(ctx);
941 }
942
943 GrUniqueKey stretchedKey;
944 if (this->onMakeStretchedKey(stretch, &stretchedKey)) {
945 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
946 if (result) {
947 return result;
948 }
949 }
950
951 GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch);
952 if (!result) {
953 return nullptr;
954 }
955
956 if (stretchedKey.isValid()) {
957 ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result);
958 this->onNotifyStretchCached(stretchedKey);
959 }
960 return result;
961}
962
963GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) {
964 if (this->width() < ctx->caps()->minTextureSize() ||
965 this->height() < ctx->caps()->minTextureSize())
966 {
967 // we can't trust our ability to use HW to perform the stretch, so we request
968 // a raster instead, and perform the stretch on the CPU.
969 SkBitmap bitmap;
970 if (!this->onGetROBitmap(&bitmap)) {
971 return nullptr;
972 }
973 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
974 return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey());
975 } else {
976 SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx));
977 if (!unstretched) {
978 return nullptr;
979 }
980 return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey());
981 }
982}