blob: 8f9555ad52a2328eb804c1c10c3efb4487740415 [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"
bsalomonf276ac52015-10-09 13:36:42 -070013#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "GrDrawContext.h"
egdaniel378092f2014-12-03 10:40:13 -080015#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070016#include "GrYUVProvider.h"
17
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000018#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000019#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070020#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070021#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080022#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070023#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000024#include "SkMessageBus.h"
25#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080026#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070027#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080028#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070029#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070030#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070031#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080032#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070033#include "effects/GrXfermodeFragmentProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070034#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070035
krajcevski8c111f72014-06-02 13:51:34 -070036#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070037# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070038# include "etc1.h"
39#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000040
reed856e9d92015-09-30 12:21:45 -070041bool GrTextureUsageSupported(const GrCaps& caps, int width, int height, SkImageUsageType usage) {
42 if (caps.npotTextureTileSupport()) {
43 return true;
44 }
45 const bool is_pow2 = SkIsPow2(width) && SkIsPow2(height);
46 return is_pow2 || kUntiled_SkImageUsageType == usage;
47}
48
49GrTextureParams GrImageUsageToTextureParams(SkImageUsageType usage) {
50 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
51 // tell us the specifics about filter level or specific tiling.
52
53 const SkShader::TileMode tiles[] = {
54 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
55 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
56 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
57 };
58
59 const GrTextureParams::FilterMode filters[] = {
60 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
61 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
62 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
63 };
64
65 return GrTextureParams(tiles[usage], filters[usage]);
66}
67
reed@google.comac10a2d2010-12-22 21:39:39 +000068/* Fill out buffer with the compressed format Ganesh expects from a colortable
69 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000070
71 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000072 we could detect that the colortable.count is <= 16, and then repack the
73 indices as nibbles to save RAM, but it would take more time (i.e. a lot
74 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000075
reed@google.comac10a2d2010-12-22 21:39:39 +000076 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
77 as the colortable.count says it is.
78 */
bsalomone79a2da2014-10-24 12:42:51 -070079static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070080 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000082 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000083 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000084 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000085 return;
86 }
87
88 SkColorTable* ctable = bitmap.getColorTable();
89 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000090
reed@google.com7111d462014-03-25 16:20:24 +000091 const int count = ctable->count();
92
93 SkDstPixelInfo dstPI;
94 dstPI.fColorType = kRGBA_8888_SkColorType;
95 dstPI.fAlphaType = kPremul_SkAlphaType;
96 dstPI.fPixels = buffer;
97 dstPI.fRowBytes = count * sizeof(SkPMColor);
98
99 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000100 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000101 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800102 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000103 srcPI.fRowBytes = count * sizeof(SkPMColor);
104
105 srcPI.convertPixelsTo(&dstPI, count, 1);
106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700108 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000110 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
112 } else {
113 // need to trim off the extra bytes per row
114 size_t width = bitmap.width();
115 size_t rowBytes = bitmap.rowBytes();
116 const char* src = (const char*)bitmap.getPixels();
117 for (int y = 0; y < bitmap.height(); y++) {
118 memcpy(dst, src, width);
119 src += rowBytes;
120 dst += width;
121 }
122 }
123}
124
125////////////////////////////////////////////////////////////////////////////////
126
bsalomonc59a1df2015-06-01 07:13:42 -0700127static void get_stretch(const GrContext* ctx, int width, int height,
reedb5d32632015-09-29 13:36:50 -0700128 const GrTextureParams* params, SkGrStretch* stretch) {
129 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700130 bool doStretch = false;
131 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
132 (!SkIsPow2(width) || !SkIsPow2(height))) {
133 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700134 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
135 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
136 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700137 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
138 // up using the GPU. If issues persist we may need to CPU-stretch.
139 doStretch = true;
140 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
141 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800142 }
bsalomonc59a1df2015-06-01 07:13:42 -0700143 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700144 if (params) {
145 switch(params->filterMode()) {
146 case GrTextureParams::kNone_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700147 stretch->fType = SkGrStretch::kNearest_Type;
bsalomon0513c832015-06-01 10:22:48 -0700148 break;
149 case GrTextureParams::kBilerp_FilterMode:
150 case GrTextureParams::kMipMap_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700151 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomon0513c832015-06-01 10:22:48 -0700152 break;
153 }
154 } else {
reedb5d32632015-09-29 13:36:50 -0700155 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700156 }
157 } else {
158 stretch->fWidth = -1;
159 stretch->fHeight = -1;
reedb5d32632015-09-29 13:36:50 -0700160 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700161 }
bsalomon37f9a262015-02-02 13:00:10 -0800162}
163
reed856e9d92015-09-30 12:21:45 -0700164bool GrMakeStretchedKey(const GrUniqueKey& origKey, const SkGrStretch& stretch,
165 GrUniqueKey* stretchedKey) {
reedb5d32632015-09-29 13:36:50 -0700166 if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) {
bsalomonc59a1df2015-06-01 07:13:42 -0700167 uint32_t width = SkToU16(stretch.fWidth);
168 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800169 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700170 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700171 builder[0] = stretch.fType;
172 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800173 builder.finish();
174 return true;
175 }
bsalomon23e619c2015-02-06 11:54:28 -0800176 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800177 return false;
178}
179
reed85d91782015-09-10 14:33:38 -0700180static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
181 SkASSERT(SkIsU16(subset.width()));
182 SkASSERT(SkIsU16(subset.height()));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000183
bsalomon8718aaf2015-02-19 07:24:21 -0800184 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
185 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700186 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700187 builder[1] = subset.x();
188 builder[2] = subset.y();
189 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800190}
bsalomon37f9a262015-02-02 13:00:10 -0800191
reed85d91782015-09-10 14:33:38 -0700192void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
reed8f343722015-08-13 13:32:39 -0700193 const GrCaps& caps, SkImageUsageType usage) {
reedb5d32632015-09-29 13:36:50 -0700194 const SkGrStretch::Type stretches[] = {
195 SkGrStretch::kNone_Type, // kUntiled_SkImageUsageType
196 SkGrStretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
197 SkGrStretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
reed8f343722015-08-13 13:32:39 -0700198 };
199
reed856e9d92015-09-30 12:21:45 -0700200 if (!GrTextureUsageSupported(caps, subset.width(), subset.height(), usage)) {
reed8f343722015-08-13 13:32:39 -0700201 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700202 make_unstretched_key(&tmpKey, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700203
reedb5d32632015-09-29 13:36:50 -0700204 SkGrStretch stretch;
reed8f343722015-08-13 13:32:39 -0700205 stretch.fType = stretches[usage];
reed85d91782015-09-10 14:33:38 -0700206 stretch.fWidth = SkNextPow2(subset.width());
207 stretch.fHeight = SkNextPow2(subset.height());
reed856e9d92015-09-30 12:21:45 -0700208 if (!GrMakeStretchedKey(tmpKey, stretch, key)) {
reed8f343722015-08-13 13:32:39 -0700209 goto UNSTRETCHED;
210 }
211 } else {
212 UNSTRETCHED:
reed85d91782015-09-10 14:33:38 -0700213 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700214 }
215}
216
reedb5d32632015-09-29 13:36:50 -0700217static void make_image_keys(uint32_t imageID, const SkIRect& subset, const SkGrStretch& stretch,
reed85d91782015-09-10 14:33:38 -0700218 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
219 make_unstretched_key(key, imageID, subset);
reedb5d32632015-09-29 13:36:50 -0700220 if (SkGrStretch::kNone_Type != stretch.fType) {
reed856e9d92015-09-30 12:21:45 -0700221 GrMakeStretchedKey(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800222 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000223}
224
reed3322a812015-09-16 10:09:24 -0700225GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
226 GrSurfaceDesc desc;
227 desc.fFlags = kNone_GrSurfaceFlags;
228 desc.fWidth = info.width();
229 desc.fHeight = info.height();
230 desc.fConfig = SkImageInfo2GrPixelConfig(info);
231 desc.fSampleCnt = 0;
232 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000233}
234
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000235namespace {
236
237// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800238class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000239public:
bsalomon8718aaf2015-02-19 07:24:21 -0800240 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000241private:
bsalomon8718aaf2015-02-19 07:24:21 -0800242 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000243
mtklein36352bf2015-03-25 18:17:31 -0700244 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800245 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000246 }
247};
248
249} // namespace
250
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000251
reed43fe6182015-09-08 08:37:36 -0700252GrTexture* GrCreateTextureForPixels(GrContext* ctx,
253 const GrUniqueKey& optionalKey,
254 GrSurfaceDesc desc,
255 SkPixelRef* pixelRefForInvalidationNotification,
256 const void* pixels,
257 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700258 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800259 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700260 if (pixelRefForInvalidationNotification) {
261 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
262 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
263 }
bsalomond309e7a2015-04-30 14:18:54 -0700264 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700265 }
266 return result;
267}
268
bsalomonc59a1df2015-06-01 07:13:42 -0700269// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
270// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
271// filtering and the size to stretch the texture to.
reedb5d32632015-09-29 13:36:50 -0700272GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch,
bsalomonc59a1df2015-06-01 07:13:42 -0700273 SkPixelRef* pixelRef,
274 const GrUniqueKey& optionalKey) {
reedb5d32632015-09-29 13:36:50 -0700275 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800276
277 GrContext* context = inputTexture->getContext();
278 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700279 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800280
281 // Either it's a cache miss or the original wasn't cached to begin with.
282 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800283 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700284 rtDesc.fWidth = stretch.fWidth;
285 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800286 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
287
288 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
289 // fail.
bsalomon76228632015-05-29 08:02:10 -0700290 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800291 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700292 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800293 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700294 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800295 rtDesc.fConfig = kSkia8888_GrPixelConfig;
296 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800298 }
299 } else if (kRGB_GrColorComponentFlags ==
300 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700301 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800302 rtDesc.fConfig = kSkia8888_GrPixelConfig;
303 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700304 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800305 }
306 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700307 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800308 }
309 }
310
reed43fe6182015-09-08 08:37:36 -0700311 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
312 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800313 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700314 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800315 }
316 GrPaint paint;
317
318 // If filtering is not desired then we want to ensure all texels in the resampled image are
319 // copies of texels from the original.
320 GrTextureParams params(SkShader::kClamp_TileMode,
reedb5d32632015-09-29 13:36:50 -0700321 SkGrStretch::kBilerp_Type == stretch.fType ?
bsalomonc59a1df2015-06-01 07:13:42 -0700322 GrTextureParams::kBilerp_FilterMode :
323 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800324 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
325
326 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
327 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
328
robertphillipsc9a37062015-09-01 08:34:28 -0700329 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700330 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700331 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700332 }
333
334 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
335 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800336
reed43fe6182015-09-08 08:37:36 -0700337 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800338}
339
reed43fe6182015-09-08 08:37:36 -0700340GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
341 int expectedW, int expectedH,
342 const void** outStartOfDataToUpload) {
343 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700344#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700345 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
346 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700347 }
348
reed43fe6182015-09-08 08:37:36 -0700349 const uint8_t* bytes = data->bytes();
350 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700351 // Does the data match the dimensions of the bitmap? If not,
352 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700353 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
354 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
355 {
356 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700357 }
358
reed43fe6182015-09-08 08:37:36 -0700359 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
360 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700361 } else if (SkKTXFile::is_ktx(bytes)) {
362 SkKTXFile ktx(data);
363
364 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700365 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700366 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700367 }
368
369 // Does the data match the dimensions of the bitmap? If not,
370 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700371 if (ktx.width() != expectedW || ktx.height() != expectedH) {
372 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800373 }
krajcevski99ffe242014-06-03 13:04:35 -0700374
reed43fe6182015-09-08 08:37:36 -0700375 *outStartOfDataToUpload = ktx.pixelData();
376 return kETC1_GrPixelConfig;
377 }
378#endif
379 return kUnknown_GrPixelConfig;
380}
381
382static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
383 const SkBitmap &bm, GrSurfaceDesc desc) {
384 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
385 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700386 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700387 }
388
reed43fe6182015-09-08 08:37:36 -0700389 const void* startOfTexData;
390 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
391 &startOfTexData);
392 if (kUnknown_GrPixelConfig == desc.fConfig) {
393 return nullptr;
394 }
395
396 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700397}
reed43fe6182015-09-08 08:37:36 -0700398
399/*
400 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
401 * be gone from SkPixelRef, and we can remove this subclass entirely.
402 */
403class PixelRef_GrYUVProvider : public GrYUVProvider {
404 SkPixelRef* fPR;
405
406public:
407 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
408
409 uint32_t onGetID() override { return fPR->getGenerationID(); }
410 bool onGetYUVSizes(SkISize sizes[3]) override {
411 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
412 }
413 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
414 SkYUVColorSpace* space) override {
415 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
416 }
417};
krajcevski9c0e6292014-06-02 07:38:14 -0700418
bsalomon8718aaf2015-02-19 07:24:21 -0800419static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700420 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700421 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700422 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700423 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800424 (pixelRef->info().width() != bm.info().width()) ||
425 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700426 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700427 }
428
sugoiba18f912015-02-04 10:53:03 -0800429 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700430 PixelRef_GrYUVProvider provider(pixelRef);
431 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
432 if (!texture) {
433 return nullptr;
434 }
435
sugoiba18f912015-02-04 10:53:03 -0800436 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700437 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
438 pixelRef->addGenIDChangeListener(listener);
439 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800440 }
reed43fe6182015-09-08 08:37:36 -0700441 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700442}
443
bsalomon37f9a262015-02-02 13:00:10 -0800444static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
445 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800446 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700447 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
448 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700449 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700450 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 SkBitmap tmpBitmap;
452
453 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454
reed3322a812015-09-16 10:09:24 -0700455 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700456 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457
reed0689d7b2014-06-14 05:30:20 -0700458 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700459 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700460 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
461 bitmap->width(), bitmap->height());
462 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700463 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
465 // our compressed data will be trimmed, so pass width() for its
466 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700467 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
468 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000469 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000470 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471 // now bitmap points to our temp, which has been promoted to 32bits
472 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000473 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 }
reed43fe6182015-09-08 08:37:36 -0700475 } else if (!bitmap->readyToDraw()) {
476 // If the bitmap had compressed data and was then uncompressed, it'll still return
477 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
478 // the bitmap has available pixels, then they might not be what the decompressed
479 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800480 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700481 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700482 return texture;
483 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomond2a6f4e2015-02-04 10:55:54 -0800486 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
487 if (texture) {
488 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700489 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800490
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000491 SkAutoLockPixels alp(*bitmap);
492 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700493 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000494 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000495
reed43fe6182015-09-08 08:37:36 -0700496 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
497 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800498}
499
reedb5d32632015-09-29 13:36:50 -0700500static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700501 SkBitmap stretched;
502 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
503 SkCanvas canvas(stretched);
504 SkPaint paint;
505 switch (stretch.fType) {
reedb5d32632015-09-29 13:36:50 -0700506 case SkGrStretch::kNearest_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700507 paint.setFilterQuality(kNone_SkFilterQuality);
508 break;
reedb5d32632015-09-29 13:36:50 -0700509 case SkGrStretch::kBilerp_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700510 paint.setFilterQuality(kLow_SkFilterQuality);
511 break;
reedb5d32632015-09-29 13:36:50 -0700512 case SkGrStretch::kNone_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700513 SkDEBUGFAIL("Shouldn't get here.");
514 break;
515 }
516 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700517 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700518 return stretched;
519}
520
reed85d91782015-09-10 14:33:38 -0700521bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
522 GrTexture* nativeTexture, const GrTextureParams* params) {
reedb5d32632015-09-29 13:36:50 -0700523 SkGrStretch stretch;
reed85d91782015-09-10 14:33:38 -0700524 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800525
reed85d91782015-09-10 14:33:38 -0700526 // Handle the case where the bitmap/image is explicitly texture backed.
527 if (nativeTexture) {
reedb5d32632015-09-29 13:36:50 -0700528 if (SkGrStretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800529 return true;
530 }
reed85d91782015-09-10 14:33:38 -0700531 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800532 if (!key.isValid()) {
533 return false;
534 }
bsalomon8718aaf2015-02-19 07:24:21 -0800535 GrUniqueKey stretchedKey;
reed856e9d92015-09-30 12:21:45 -0700536 GrMakeStretchedKey(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700537 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800538 }
539
bsalomon8718aaf2015-02-19 07:24:21 -0800540 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700541 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700542 return ctx->textureProvider()->existsTextureWithUniqueKey(
reedb5d32632015-09-29 13:36:50 -0700543 (SkGrStretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000544}
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
reedb5d32632015-09-29 13:36:50 -0700546class Bitmap_GrTextureMaker : public GrTextureMaker {
547public:
548 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
549 : INHERITED(bitmap.width(), bitmap.height())
550 , fBitmap(bitmap)
551 {}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000552
reedb5d32632015-09-29 13:36:50 -0700553protected:
554 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
555 GrTexture* tex = fBitmap.getTexture();
556 if (tex) {
557 return SkRef(tex);
bsalomon88425562015-02-04 09:12:46 -0800558 }
bsalomon88425562015-02-04 09:12:46 -0800559
reedb5d32632015-09-29 13:36:50 -0700560 GrUniqueKey unstretchedKey;
561 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000562
reedb5d32632015-09-29 13:36:50 -0700563 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800564 if (result) {
565 return result;
566 }
reedb5d32632015-09-29 13:36:50 -0700567 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800568 }
bsalomone137db82015-01-31 20:10:56 -0800569
reedb5d32632015-09-29 13:36:50 -0700570 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
571 if (fBitmap.isVolatile()) {
572 return false;
573 }
574
575 GrUniqueKey unstretchedKey;
576 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
reed856e9d92015-09-30 12:21:45 -0700577 return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800578 }
bsalomone137db82015-01-31 20:10:56 -0800579
reedb5d32632015-09-29 13:36:50 -0700580 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
581 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey));
582 }
bsalomon37f9a262015-02-02 13:00:10 -0800583
reedb5d32632015-09-29 13:36:50 -0700584 bool onGetROBitmap(SkBitmap* bitmap) override {
585 *bitmap = fBitmap;
586 return true;
587 }
588
589private:
590 const SkBitmap fBitmap;
591
592 typedef GrTextureMaker INHERITED;
593};
594
595GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
596 const GrTextureParams* params) {
597 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000598}
reed8f343722015-08-13 13:32:39 -0700599
600// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
601// into a wrapper that contains the inverse of these tables.
602GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
603 const SkBitmap& bitmap,
604 SkImageUsageType usage) {
reed856e9d92015-09-30 12:21:45 -0700605 GrTextureParams params = GrImageUsageToTextureParams(usage);
reed8f343722015-08-13 13:32:39 -0700606 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
607}
608
rileya@google.com24f3ad12012-07-18 21:47:40 +0000609///////////////////////////////////////////////////////////////////////////////
610
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000611// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
612// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800613GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000614 switch (ct) {
615 case kUnknown_SkColorType:
616 return kUnknown_GrPixelConfig;
617 case kAlpha_8_SkColorType:
618 return kAlpha_8_GrPixelConfig;
619 case kRGB_565_SkColorType:
620 return kRGB_565_GrPixelConfig;
621 case kARGB_4444_SkColorType:
622 return kRGBA_4444_GrPixelConfig;
623 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700624 //if (kSRGB_SkColorProfileType == pt) {
625 // return kSRGBA_8888_GrPixelConfig;
626 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000627 return kRGBA_8888_GrPixelConfig;
628 case kBGRA_8888_SkColorType:
629 return kBGRA_8888_GrPixelConfig;
630 case kIndex_8_SkColorType:
631 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700632 case kGray_8_SkColorType:
633 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000634 }
635 SkASSERT(0); // shouldn't get here
636 return kUnknown_GrPixelConfig;
637}
638
jvanverthfa1e8a72014-12-22 08:31:49 -0800639bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
640 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000641 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800642 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000643 switch (config) {
644 case kAlpha_8_GrPixelConfig:
645 ct = kAlpha_8_SkColorType;
646 break;
647 case kIndex_8_GrPixelConfig:
648 ct = kIndex_8_SkColorType;
649 break;
650 case kRGB_565_GrPixelConfig:
651 ct = kRGB_565_SkColorType;
652 break;
653 case kRGBA_4444_GrPixelConfig:
654 ct = kARGB_4444_SkColorType;
655 break;
656 case kRGBA_8888_GrPixelConfig:
657 ct = kRGBA_8888_SkColorType;
658 break;
659 case kBGRA_8888_GrPixelConfig:
660 ct = kBGRA_8888_SkColorType;
661 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800662 case kSRGBA_8888_GrPixelConfig:
663 ct = kRGBA_8888_SkColorType;
664 pt = kSRGB_SkColorProfileType;
665 break;
reed@google.combf790232013-12-13 19:45:58 +0000666 default:
667 return false;
668 }
669 if (ctOut) {
670 *ctOut = ct;
671 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800672 if (ptOut) {
673 *ptOut = pt;
674 }
reed@google.combf790232013-12-13 19:45:58 +0000675 return true;
676}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000677
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000678
bsalomonf1b7a1d2015-09-28 06:26:28 -0700679////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000680
bsalomonaa48d362015-10-01 08:34:17 -0700681static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
682 if (primitiveIsSrc) {
683 return SkXfermode::kSrc_Mode != mode;
684 } else {
685 return SkXfermode::kDst_Mode != mode;
686 }
687}
688
bsalomonf1b7a1d2015-09-28 06:26:28 -0700689static inline bool skpaint_to_grpaint_impl(GrContext* context,
690 const SkPaint& skPaint,
691 const SkMatrix& viewM,
692 const GrFragmentProcessor** shaderProcessor,
693 SkXfermode::Mode* primColorMode,
694 bool primitiveIsSrc,
695 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000696 grPaint->setAntiAlias(skPaint.isAntiAlias());
697
bsalomonf1b7a1d2015-09-28 06:26:28 -0700698 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
699 // of per-vertex colors.
700 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700701 const GrFragmentProcessor* shaderFP = nullptr;
702 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
703 if (shaderProcessor) {
704 shaderFP = *shaderProcessor;
705 } else if (const SkShader* shader = skPaint.getShader()) {
706 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700707 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700708 shaderFP = aufp;
709 if (!shaderFP) {
710 return false;
711 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700712 }
713 }
714
715 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
716 // a known constant value. In that case we can simply apply a color filter during this
717 // conversion without converting the color filter to a GrFragmentProcessor.
718 bool applyColorFilterToPaintColor = false;
719 if (shaderFP) {
720 if (primColorMode) {
721 // There is a blend between the primitive color and the shader color. The shader sees
722 // the opaque paint color. The shader's output is blended using the provided mode by
723 // the primitive color. The blended color is then modulated by the paint's alpha.
724
725 // The geometry processor will insert the primitive color to start the color chain, so
726 // the GrPaint color will be ignored.
727
728 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
729
730 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
731 aufp.reset(shaderFP);
732
733 if (primitiveIsSrc) {
734 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
735 *primColorMode);
736 } else {
737 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
738 *primColorMode);
739 }
740 aufp.reset(shaderFP);
741 // The above may return null if compose results in a pass through of the prim color.
742 if (shaderFP) {
743 grPaint->addColorFragmentProcessor(shaderFP);
744 }
745
746 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
747 if (GrColor_WHITE != paintAlpha) {
748 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
749 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
750 }
751 } else {
752 // The shader's FP sees the paint unpremul color
753 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
754 grPaint->addColorFragmentProcessor(shaderFP);
755 }
756 } else {
757 if (primColorMode) {
758 // There is a blend between the primitive color and the paint color. The blend considers
759 // the opaque paint color. The paint's alpha is applied to the post-blended color.
760 SkAutoTUnref<const GrFragmentProcessor> processor(
761 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
762 GrConstColorProcessor::kIgnore_InputMode));
763 if (primitiveIsSrc) {
764 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
765 *primColorMode));
766 } else {
767 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
768 *primColorMode));
769
770 }
771 if (processor) {
772 grPaint->addColorFragmentProcessor(processor);
773 }
774
bsalomonaa48d362015-10-01 08:34:17 -0700775 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700776
777 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700778 if (GrColor_WHITE != paintAlpha) {
779 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
780 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
781 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700782 } else {
783 // No shader, no primitive color.
784 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
785 applyColorFilterToPaintColor = true;
786 }
787 }
788
789 SkColorFilter* colorFilter = skPaint.getColorFilter();
790 if (colorFilter) {
791 if (applyColorFilterToPaintColor) {
792 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
793 } else {
bsalomone25eea42015-09-29 06:38:55 -0700794 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700795 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700796 if (cfFP) {
797 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700798 } else {
799 return false;
800 }
801 }
802 }
803
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000804 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700805 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800806 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000807 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700808 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800809 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000810 }
egdaniel378092f2014-12-03 10:40:13 -0800811 SkASSERT(xpFactory);
812 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800813
krajcevskif461a8f2014-06-19 14:14:06 -0700814#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700815 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700816 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700817 }
818#endif
bsalomonbed83a62015-04-15 14:18:34 -0700819 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000820}
821
bsalomonf1b7a1d2015-09-28 06:26:28 -0700822bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
823 GrPaint* grPaint) {
824 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
825}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000826
bsalomonf1b7a1d2015-09-28 06:26:28 -0700827/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
828bool SkPaintToGrPaintReplaceShader(GrContext* context,
829 const SkPaint& skPaint,
830 const GrFragmentProcessor* shaderFP,
831 GrPaint* grPaint) {
832 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700833 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000834 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700835 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
836 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000837}
reed8b26b992015-05-07 15:36:17 -0700838
bsalomonf1b7a1d2015-09-28 06:26:28 -0700839/** Ignores the SkShader (if any) on skPaint. */
840bool SkPaintToGrPaintNoShader(GrContext* context,
841 const SkPaint& skPaint,
842 GrPaint* grPaint) {
843 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
844 static const GrFragmentProcessor* kNullShaderFP = nullptr;
845 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
846 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
847 grPaint);
848}
849
850/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
851be setup as a vertex attribute using the specified SkXfermode::Mode. */
852bool SkPaintToGrPaintWithXfermode(GrContext* context,
853 const SkPaint& skPaint,
854 const SkMatrix& viewM,
855 SkXfermode::Mode primColorMode,
856 bool primitiveIsSrc,
857 GrPaint* grPaint) {
858 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
859 grPaint);
860}
861
862
863////////////////////////////////////////////////////////////////////////////////////////////////
864
reed8b26b992015-05-07 15:36:17 -0700865SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
866#ifdef SK_DEBUG
867 const GrSurfaceDesc& desc = tex->desc();
868 SkASSERT(w <= desc.fWidth);
869 SkASSERT(h <= desc.fHeight);
870#endif
871 const GrPixelConfig config = tex->config();
872 SkColorType ct;
873 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700874 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700875 ct = kUnknown_SkColorType;
876 }
877 return SkImageInfo::Make(w, h, ct, at);
878}
879
880
881void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
882 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
883 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700884 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700885}
joshualitt9bc39542015-08-12 12:57:54 -0700886
887GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
888 const SkMatrix& viewM,
889 const SkMatrix& localM,
890 bool* doBicubic) {
891 *doBicubic = false;
892 GrTextureParams::FilterMode textureFilterMode;
893 switch (paintFilterQuality) {
894 case kNone_SkFilterQuality:
895 textureFilterMode = GrTextureParams::kNone_FilterMode;
896 break;
897 case kLow_SkFilterQuality:
898 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
899 break;
900 case kMedium_SkFilterQuality: {
901 SkMatrix matrix;
902 matrix.setConcat(viewM, localM);
903 if (matrix.getMinScale() < SK_Scalar1) {
904 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
905 } else {
906 // Don't trigger MIP level generation unnecessarily.
907 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
908 }
909 break;
910 }
911 case kHigh_SkFilterQuality: {
912 SkMatrix matrix;
913 matrix.setConcat(viewM, localM);
914 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
915 break;
916 }
917 default:
918 SkErrorInternals::SetError( kInvalidPaint_SkError,
919 "Sorry, I don't understand the filtering "
920 "mode you asked for. Falling back to "
921 "MIPMaps.");
922 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
923 break;
924
925 }
926 return textureFilterMode;
927}
reedb5d32632015-09-29 13:36:50 -0700928
929////////////////////////////////////////////////////////////////////////////////////////////////
930
reed856e9d92015-09-30 12:21:45 -0700931GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, SkImageUsageType usage) {
932 GrTextureParams params = GrImageUsageToTextureParams(usage);
933 return this->refCachedTexture(ctx, &params);
934}
935
reedb5d32632015-09-29 13:36:50 -0700936GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams* params) {
937 SkGrStretch stretch;
938 get_stretch(ctx, this->width(), this->height(), params, &stretch);
939
940 if (SkGrStretch::kNone_Type == stretch.fType) {
941 return this->onRefUnstretchedTexture(ctx);
942 }
943
944 GrUniqueKey stretchedKey;
945 if (this->onMakeStretchedKey(stretch, &stretchedKey)) {
946 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
947 if (result) {
948 return result;
949 }
950 }
951
952 GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch);
953 if (!result) {
954 return nullptr;
955 }
956
957 if (stretchedKey.isValid()) {
958 ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result);
959 this->onNotifyStretchCached(stretchedKey);
960 }
961 return result;
962}
963
964GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) {
965 if (this->width() < ctx->caps()->minTextureSize() ||
966 this->height() < ctx->caps()->minTextureSize())
967 {
968 // we can't trust our ability to use HW to perform the stretch, so we request
969 // a raster instead, and perform the stretch on the CPU.
970 SkBitmap bitmap;
971 if (!this->onGetROBitmap(&bitmap)) {
972 return nullptr;
973 }
974 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
975 return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey());
976 } else {
977 SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx));
978 if (!unstretched) {
979 return nullptr;
980 }
981 return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey());
982 }
983}