blob: 2b6c79f0f34cbcb2d9459dd682ad063d1f983e25 [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
40/* Fill out buffer with the compressed format Ganesh expects from a colortable
41 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000042
43 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000044 we could detect that the colortable.count is <= 16, and then repack the
45 indices as nibbles to save RAM, but it would take more time (i.e. a lot
46 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
49 as the colortable.count says it is.
50 */
bsalomone79a2da2014-10-24 12:42:51 -070051static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070052 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000053
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000054 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000055 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000056 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000057 return;
58 }
59
60 SkColorTable* ctable = bitmap.getColorTable();
61 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000062
reed@google.com7111d462014-03-25 16:20:24 +000063 const int count = ctable->count();
64
65 SkDstPixelInfo dstPI;
66 dstPI.fColorType = kRGBA_8888_SkColorType;
67 dstPI.fAlphaType = kPremul_SkAlphaType;
68 dstPI.fPixels = buffer;
69 dstPI.fRowBytes = count * sizeof(SkPMColor);
70
71 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000072 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000073 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080074 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000075 srcPI.fRowBytes = count * sizeof(SkPMColor);
76
77 srcPI.convertPixelsTo(&dstPI, count, 1);
78
reed@google.comac10a2d2010-12-22 21:39:39 +000079 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070080 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000081
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000082 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000083 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
84 } else {
85 // need to trim off the extra bytes per row
86 size_t width = bitmap.width();
87 size_t rowBytes = bitmap.rowBytes();
88 const char* src = (const char*)bitmap.getPixels();
89 for (int y = 0; y < bitmap.height(); y++) {
90 memcpy(dst, src, width);
91 src += rowBytes;
92 dst += width;
93 }
94 }
95}
96
97////////////////////////////////////////////////////////////////////////////////
98
bsalomonc59a1df2015-06-01 07:13:42 -070099static void get_stretch(const GrContext* ctx, int width, int height,
reedb5d32632015-09-29 13:36:50 -0700100 const GrTextureParams* params, SkGrStretch* stretch) {
101 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700102 bool doStretch = false;
103 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
104 (!SkIsPow2(width) || !SkIsPow2(height))) {
105 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700106 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
107 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
108 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700109 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
110 // up using the GPU. If issues persist we may need to CPU-stretch.
111 doStretch = true;
112 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
113 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800114 }
bsalomonc59a1df2015-06-01 07:13:42 -0700115 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700116 if (params) {
117 switch(params->filterMode()) {
118 case GrTextureParams::kNone_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700119 stretch->fType = SkGrStretch::kNearest_Type;
bsalomon0513c832015-06-01 10:22:48 -0700120 break;
121 case GrTextureParams::kBilerp_FilterMode:
122 case GrTextureParams::kMipMap_FilterMode:
reedb5d32632015-09-29 13:36:50 -0700123 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomon0513c832015-06-01 10:22:48 -0700124 break;
125 }
126 } else {
reedb5d32632015-09-29 13:36:50 -0700127 stretch->fType = SkGrStretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700128 }
129 } else {
130 stretch->fWidth = -1;
131 stretch->fHeight = -1;
reedb5d32632015-09-29 13:36:50 -0700132 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700133 }
bsalomon37f9a262015-02-02 13:00:10 -0800134}
135
reedb5d32632015-09-29 13:36:50 -0700136static bool make_stretched_key(const GrUniqueKey& origKey, const SkGrStretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800137 GrUniqueKey* stretchedKey) {
reedb5d32632015-09-29 13:36:50 -0700138 if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) {
bsalomonc59a1df2015-06-01 07:13:42 -0700139 uint32_t width = SkToU16(stretch.fWidth);
140 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800141 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700142 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700143 builder[0] = stretch.fType;
144 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800145 builder.finish();
146 return true;
147 }
bsalomon23e619c2015-02-06 11:54:28 -0800148 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800149 return false;
150}
151
reed85d91782015-09-10 14:33:38 -0700152static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
153 SkASSERT(SkIsU16(subset.width()));
154 SkASSERT(SkIsU16(subset.height()));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155
bsalomon8718aaf2015-02-19 07:24:21 -0800156 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
157 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700158 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700159 builder[1] = subset.x();
160 builder[2] = subset.y();
161 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800162}
bsalomon37f9a262015-02-02 13:00:10 -0800163
reed85d91782015-09-10 14:33:38 -0700164void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
reed8f343722015-08-13 13:32:39 -0700165 const GrCaps& caps, SkImageUsageType usage) {
reedb5d32632015-09-29 13:36:50 -0700166 const SkGrStretch::Type stretches[] = {
167 SkGrStretch::kNone_Type, // kUntiled_SkImageUsageType
168 SkGrStretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
169 SkGrStretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
reed8f343722015-08-13 13:32:39 -0700170 };
171
reed85d91782015-09-10 14:33:38 -0700172 const bool isPow2 = SkIsPow2(subset.width()) && SkIsPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700173 const bool needToStretch = !isPow2 &&
174 usage != kUntiled_SkImageUsageType &&
175 !caps.npotTextureTileSupport();
176
177 if (needToStretch) {
178 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700179 make_unstretched_key(&tmpKey, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700180
reedb5d32632015-09-29 13:36:50 -0700181 SkGrStretch stretch;
reed8f343722015-08-13 13:32:39 -0700182 stretch.fType = stretches[usage];
reed85d91782015-09-10 14:33:38 -0700183 stretch.fWidth = SkNextPow2(subset.width());
184 stretch.fHeight = SkNextPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700185 if (!make_stretched_key(tmpKey, stretch, key)) {
186 goto UNSTRETCHED;
187 }
188 } else {
189 UNSTRETCHED:
reed85d91782015-09-10 14:33:38 -0700190 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700191 }
192}
193
reedb5d32632015-09-29 13:36:50 -0700194static void make_image_keys(uint32_t imageID, const SkIRect& subset, const SkGrStretch& stretch,
reed85d91782015-09-10 14:33:38 -0700195 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
196 make_unstretched_key(key, imageID, subset);
reedb5d32632015-09-29 13:36:50 -0700197 if (SkGrStretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800198 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800199 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000200}
201
reed3322a812015-09-16 10:09:24 -0700202GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
203 GrSurfaceDesc desc;
204 desc.fFlags = kNone_GrSurfaceFlags;
205 desc.fWidth = info.width();
206 desc.fHeight = info.height();
207 desc.fConfig = SkImageInfo2GrPixelConfig(info);
208 desc.fSampleCnt = 0;
209 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000210}
211
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000212namespace {
213
214// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800215class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000216public:
bsalomon8718aaf2015-02-19 07:24:21 -0800217 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000218private:
bsalomon8718aaf2015-02-19 07:24:21 -0800219 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000220
mtklein36352bf2015-03-25 18:17:31 -0700221 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800222 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000223 }
224};
225
226} // namespace
227
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000228
reed43fe6182015-09-08 08:37:36 -0700229GrTexture* GrCreateTextureForPixels(GrContext* ctx,
230 const GrUniqueKey& optionalKey,
231 GrSurfaceDesc desc,
232 SkPixelRef* pixelRefForInvalidationNotification,
233 const void* pixels,
234 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700235 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800236 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700237 if (pixelRefForInvalidationNotification) {
238 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
239 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
240 }
bsalomond309e7a2015-04-30 14:18:54 -0700241 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700242 }
243 return result;
244}
245
bsalomonc59a1df2015-06-01 07:13:42 -0700246// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
247// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
248// filtering and the size to stretch the texture to.
reedb5d32632015-09-29 13:36:50 -0700249GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch,
bsalomonc59a1df2015-06-01 07:13:42 -0700250 SkPixelRef* pixelRef,
251 const GrUniqueKey& optionalKey) {
reedb5d32632015-09-29 13:36:50 -0700252 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800253
254 GrContext* context = inputTexture->getContext();
255 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700256 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800257
258 // Either it's a cache miss or the original wasn't cached to begin with.
259 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800260 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700261 rtDesc.fWidth = stretch.fWidth;
262 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800263 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
264
265 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
266 // fail.
bsalomon76228632015-05-29 08:02:10 -0700267 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800268 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700269 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800270 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700271 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800272 rtDesc.fConfig = kSkia8888_GrPixelConfig;
273 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700274 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800275 }
276 } else if (kRGB_GrColorComponentFlags ==
277 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700278 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800279 rtDesc.fConfig = kSkia8888_GrPixelConfig;
280 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800282 }
283 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700284 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800285 }
286 }
287
reed43fe6182015-09-08 08:37:36 -0700288 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
289 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800290 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700291 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800292 }
293 GrPaint paint;
294
295 // If filtering is not desired then we want to ensure all texels in the resampled image are
296 // copies of texels from the original.
297 GrTextureParams params(SkShader::kClamp_TileMode,
reedb5d32632015-09-29 13:36:50 -0700298 SkGrStretch::kBilerp_Type == stretch.fType ?
bsalomonc59a1df2015-06-01 07:13:42 -0700299 GrTextureParams::kBilerp_FilterMode :
300 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800301 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
302
303 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
304 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
305
robertphillipsc9a37062015-09-01 08:34:28 -0700306 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700307 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700308 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700309 }
310
311 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
312 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800313
reed43fe6182015-09-08 08:37:36 -0700314 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800315}
316
reed43fe6182015-09-08 08:37:36 -0700317GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
318 int expectedW, int expectedH,
319 const void** outStartOfDataToUpload) {
320 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700321#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700322 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
323 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700324 }
325
reed43fe6182015-09-08 08:37:36 -0700326 const uint8_t* bytes = data->bytes();
327 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700328 // Does the data match the dimensions of the bitmap? If not,
329 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700330 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
331 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
332 {
333 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700334 }
335
reed43fe6182015-09-08 08:37:36 -0700336 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
337 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700338 } else if (SkKTXFile::is_ktx(bytes)) {
339 SkKTXFile ktx(data);
340
341 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700342 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700343 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700344 }
345
346 // Does the data match the dimensions of the bitmap? If not,
347 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700348 if (ktx.width() != expectedW || ktx.height() != expectedH) {
349 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800350 }
krajcevski99ffe242014-06-03 13:04:35 -0700351
reed43fe6182015-09-08 08:37:36 -0700352 *outStartOfDataToUpload = ktx.pixelData();
353 return kETC1_GrPixelConfig;
354 }
355#endif
356 return kUnknown_GrPixelConfig;
357}
358
359static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
360 const SkBitmap &bm, GrSurfaceDesc desc) {
361 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
362 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700363 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700364 }
365
reed43fe6182015-09-08 08:37:36 -0700366 const void* startOfTexData;
367 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
368 &startOfTexData);
369 if (kUnknown_GrPixelConfig == desc.fConfig) {
370 return nullptr;
371 }
372
373 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700374}
reed43fe6182015-09-08 08:37:36 -0700375
376/*
377 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
378 * be gone from SkPixelRef, and we can remove this subclass entirely.
379 */
380class PixelRef_GrYUVProvider : public GrYUVProvider {
381 SkPixelRef* fPR;
382
383public:
384 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
385
386 uint32_t onGetID() override { return fPR->getGenerationID(); }
387 bool onGetYUVSizes(SkISize sizes[3]) override {
388 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
389 }
390 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
391 SkYUVColorSpace* space) override {
392 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
393 }
394};
krajcevski9c0e6292014-06-02 07:38:14 -0700395
bsalomon8718aaf2015-02-19 07:24:21 -0800396static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700397 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700398 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700399 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700400 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800401 (pixelRef->info().width() != bm.info().width()) ||
402 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700403 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700404 }
405
sugoiba18f912015-02-04 10:53:03 -0800406 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700407 PixelRef_GrYUVProvider provider(pixelRef);
408 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
409 if (!texture) {
410 return nullptr;
411 }
412
sugoiba18f912015-02-04 10:53:03 -0800413 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700414 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
415 pixelRef->addGenIDChangeListener(listener);
416 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800417 }
reed43fe6182015-09-08 08:37:36 -0700418 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700419}
420
bsalomon37f9a262015-02-02 13:00:10 -0800421static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
422 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800423 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700424 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
425 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700426 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700427 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 SkBitmap tmpBitmap;
429
430 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000431
reed3322a812015-09-16 10:09:24 -0700432 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700433 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
reed0689d7b2014-06-14 05:30:20 -0700435 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700436 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700437 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
438 bitmap->width(), bitmap->height());
439 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700440 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
442 // our compressed data will be trimmed, so pass width() for its
443 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700444 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
445 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000447 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 // now bitmap points to our temp, which has been promoted to 32bits
449 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000450 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 }
reed43fe6182015-09-08 08:37:36 -0700452 } else if (!bitmap->readyToDraw()) {
453 // If the bitmap had compressed data and was then uncompressed, it'll still return
454 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
455 // the bitmap has available pixels, then they might not be what the decompressed
456 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800457 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700458 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700459 return texture;
460 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
bsalomond2a6f4e2015-02-04 10:55:54 -0800463 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
464 if (texture) {
465 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700466 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800467
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000468 SkAutoLockPixels alp(*bitmap);
469 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700470 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000471 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000472
reed43fe6182015-09-08 08:37:36 -0700473 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
474 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800475}
476
reedb5d32632015-09-29 13:36:50 -0700477static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700478 SkBitmap stretched;
479 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
480 SkCanvas canvas(stretched);
481 SkPaint paint;
482 switch (stretch.fType) {
reedb5d32632015-09-29 13:36:50 -0700483 case SkGrStretch::kNearest_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700484 paint.setFilterQuality(kNone_SkFilterQuality);
485 break;
reedb5d32632015-09-29 13:36:50 -0700486 case SkGrStretch::kBilerp_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700487 paint.setFilterQuality(kLow_SkFilterQuality);
488 break;
reedb5d32632015-09-29 13:36:50 -0700489 case SkGrStretch::kNone_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700490 SkDEBUGFAIL("Shouldn't get here.");
491 break;
492 }
493 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700494 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700495 return stretched;
496}
497
reed85d91782015-09-10 14:33:38 -0700498bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
499 GrTexture* nativeTexture, const GrTextureParams* params) {
reedb5d32632015-09-29 13:36:50 -0700500 SkGrStretch stretch;
reed85d91782015-09-10 14:33:38 -0700501 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800502
reed85d91782015-09-10 14:33:38 -0700503 // Handle the case where the bitmap/image is explicitly texture backed.
504 if (nativeTexture) {
reedb5d32632015-09-29 13:36:50 -0700505 if (SkGrStretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800506 return true;
507 }
reed85d91782015-09-10 14:33:38 -0700508 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800509 if (!key.isValid()) {
510 return false;
511 }
bsalomon8718aaf2015-02-19 07:24:21 -0800512 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800513 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700514 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800515 }
516
bsalomon8718aaf2015-02-19 07:24:21 -0800517 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700518 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700519 return ctx->textureProvider()->existsTextureWithUniqueKey(
reedb5d32632015-09-29 13:36:50 -0700520 (SkGrStretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000521}
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
reed85d91782015-09-10 14:33:38 -0700523bool GrIsBitmapInCache(const GrContext* ctx, const SkBitmap& bitmap,
524 const GrTextureParams* params) {
525 if (bitmap.isVolatile()) {
526 return false; // we don't cache volatile bitmaps.
527 }
528 return GrIsImageInCache(ctx, bitmap.getGenerationID(), bitmap.getSubset(), bitmap.getTexture(),
529 params);
530}
531
reedb5d32632015-09-29 13:36:50 -0700532class Bitmap_GrTextureMaker : public GrTextureMaker {
533public:
534 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
535 : INHERITED(bitmap.width(), bitmap.height())
536 , fBitmap(bitmap)
537 {}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000538
reedb5d32632015-09-29 13:36:50 -0700539protected:
540 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
541 GrTexture* tex = fBitmap.getTexture();
542 if (tex) {
543 return SkRef(tex);
bsalomon88425562015-02-04 09:12:46 -0800544 }
bsalomon88425562015-02-04 09:12:46 -0800545
reedb5d32632015-09-29 13:36:50 -0700546 GrUniqueKey unstretchedKey;
547 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000548
reedb5d32632015-09-29 13:36:50 -0700549 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800550 if (result) {
551 return result;
552 }
reedb5d32632015-09-29 13:36:50 -0700553 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800554 }
bsalomone137db82015-01-31 20:10:56 -0800555
reedb5d32632015-09-29 13:36:50 -0700556 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
557 if (fBitmap.isVolatile()) {
558 return false;
559 }
560
561 GrUniqueKey unstretchedKey;
562 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
563 return make_stretched_key(unstretchedKey, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800564 }
bsalomone137db82015-01-31 20:10:56 -0800565
reedb5d32632015-09-29 13:36:50 -0700566 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
567 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey));
568 }
bsalomon37f9a262015-02-02 13:00:10 -0800569
reedb5d32632015-09-29 13:36:50 -0700570 bool onGetROBitmap(SkBitmap* bitmap) override {
571 *bitmap = fBitmap;
572 return true;
573 }
574
575private:
576 const SkBitmap fBitmap;
577
578 typedef GrTextureMaker INHERITED;
579};
580
581GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
582 const GrTextureParams* params) {
583 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000584}
reed8f343722015-08-13 13:32:39 -0700585
586// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
587// into a wrapper that contains the inverse of these tables.
588GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
589 const SkBitmap& bitmap,
590 SkImageUsageType usage) {
591 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
592 // tell us the specifics about filter level or specific tiling.
593
594 const SkShader::TileMode tiles[] = {
595 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
596 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
597 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
598 };
599
600 const GrTextureParams::FilterMode filters[] = {
601 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
602 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
603 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
604 };
605
606 GrTextureParams params(tiles[usage], filters[usage]);
607 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
608}
609
rileya@google.com24f3ad12012-07-18 21:47:40 +0000610///////////////////////////////////////////////////////////////////////////////
611
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000612// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
613// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800614GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000615 switch (ct) {
616 case kUnknown_SkColorType:
617 return kUnknown_GrPixelConfig;
618 case kAlpha_8_SkColorType:
619 return kAlpha_8_GrPixelConfig;
620 case kRGB_565_SkColorType:
621 return kRGB_565_GrPixelConfig;
622 case kARGB_4444_SkColorType:
623 return kRGBA_4444_GrPixelConfig;
624 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700625 //if (kSRGB_SkColorProfileType == pt) {
626 // return kSRGBA_8888_GrPixelConfig;
627 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000628 return kRGBA_8888_GrPixelConfig;
629 case kBGRA_8888_SkColorType:
630 return kBGRA_8888_GrPixelConfig;
631 case kIndex_8_SkColorType:
632 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700633 case kGray_8_SkColorType:
634 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000635 }
636 SkASSERT(0); // shouldn't get here
637 return kUnknown_GrPixelConfig;
638}
639
jvanverthfa1e8a72014-12-22 08:31:49 -0800640bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
641 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000642 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800643 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000644 switch (config) {
645 case kAlpha_8_GrPixelConfig:
646 ct = kAlpha_8_SkColorType;
647 break;
648 case kIndex_8_GrPixelConfig:
649 ct = kIndex_8_SkColorType;
650 break;
651 case kRGB_565_GrPixelConfig:
652 ct = kRGB_565_SkColorType;
653 break;
654 case kRGBA_4444_GrPixelConfig:
655 ct = kARGB_4444_SkColorType;
656 break;
657 case kRGBA_8888_GrPixelConfig:
658 ct = kRGBA_8888_SkColorType;
659 break;
660 case kBGRA_8888_GrPixelConfig:
661 ct = kBGRA_8888_SkColorType;
662 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800663 case kSRGBA_8888_GrPixelConfig:
664 ct = kRGBA_8888_SkColorType;
665 pt = kSRGB_SkColorProfileType;
666 break;
reed@google.combf790232013-12-13 19:45:58 +0000667 default:
668 return false;
669 }
670 if (ctOut) {
671 *ctOut = ct;
672 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800673 if (ptOut) {
674 *ptOut = pt;
675 }
reed@google.combf790232013-12-13 19:45:58 +0000676 return true;
677}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000678
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000679
bsalomonf1b7a1d2015-09-28 06:26:28 -0700680////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000681
bsalomonf1b7a1d2015-09-28 06:26:28 -0700682static inline bool skpaint_to_grpaint_impl(GrContext* context,
683 const SkPaint& skPaint,
684 const SkMatrix& viewM,
685 const GrFragmentProcessor** shaderProcessor,
686 SkXfermode::Mode* primColorMode,
687 bool primitiveIsSrc,
688 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000689 grPaint->setAntiAlias(skPaint.isAntiAlias());
690
bsalomonf1b7a1d2015-09-28 06:26:28 -0700691 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
692 // of per-vertex colors.
693 SkAutoTUnref<const GrFragmentProcessor> aufp;
694 const GrFragmentProcessor* shaderFP = NULL;
695 if (shaderProcessor) {
696 shaderFP = *shaderProcessor;
697 } else if (const SkShader* shader = skPaint.getShader()) {
698 aufp.reset(shader->asFragmentProcessor(context, viewM, NULL, skPaint.getFilterQuality(),
699 grPaint->getProcessorDataManager()));
700 shaderFP = aufp;
701 if (!shaderFP) {
702 return false;
703 }
704 }
705
706 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
707 // a known constant value. In that case we can simply apply a color filter during this
708 // conversion without converting the color filter to a GrFragmentProcessor.
709 bool applyColorFilterToPaintColor = false;
710 if (shaderFP) {
711 if (primColorMode) {
712 // There is a blend between the primitive color and the shader color. The shader sees
713 // the opaque paint color. The shader's output is blended using the provided mode by
714 // the primitive color. The blended color is then modulated by the paint's alpha.
715
716 // The geometry processor will insert the primitive color to start the color chain, so
717 // the GrPaint color will be ignored.
718
719 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
720
721 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
722 aufp.reset(shaderFP);
723
724 if (primitiveIsSrc) {
725 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
726 *primColorMode);
727 } else {
728 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
729 *primColorMode);
730 }
731 aufp.reset(shaderFP);
732 // The above may return null if compose results in a pass through of the prim color.
733 if (shaderFP) {
734 grPaint->addColorFragmentProcessor(shaderFP);
735 }
736
737 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
738 if (GrColor_WHITE != paintAlpha) {
739 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
740 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
741 }
742 } else {
743 // The shader's FP sees the paint unpremul color
744 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
745 grPaint->addColorFragmentProcessor(shaderFP);
746 }
747 } else {
748 if (primColorMode) {
749 // There is a blend between the primitive color and the paint color. The blend considers
750 // the opaque paint color. The paint's alpha is applied to the post-blended color.
751 SkAutoTUnref<const GrFragmentProcessor> processor(
752 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
753 GrConstColorProcessor::kIgnore_InputMode));
754 if (primitiveIsSrc) {
755 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
756 *primColorMode));
757 } else {
758 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
759 *primColorMode));
760
761 }
762 if (processor) {
763 grPaint->addColorFragmentProcessor(processor);
764 }
765
766 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()) | 0xFF000000);
767
768 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
769 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
770 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
771 } else {
772 // No shader, no primitive color.
773 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
774 applyColorFilterToPaintColor = true;
775 }
776 }
777
778 SkColorFilter* colorFilter = skPaint.getColorFilter();
779 if (colorFilter) {
780 if (applyColorFilterToPaintColor) {
781 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
782 } else {
bsalomone25eea42015-09-29 06:38:55 -0700783 SkAutoTUnref<const GrFragmentProcessor> cfFP(
784 colorFilter->asFragmentProcessor(context, grPaint->getProcessorDataManager()));
785 if (cfFP) {
786 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700787 } else {
788 return false;
789 }
790 }
791 }
792
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000793 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700794 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800795 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000796 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700797 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800798 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000799 }
egdaniel378092f2014-12-03 10:40:13 -0800800 SkASSERT(xpFactory);
801 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800802
krajcevskif461a8f2014-06-19 14:14:06 -0700803#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700804 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700805 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700806 }
807#endif
bsalomonbed83a62015-04-15 14:18:34 -0700808 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000809}
810
bsalomonf1b7a1d2015-09-28 06:26:28 -0700811bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
812 GrPaint* grPaint) {
813 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
814}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000815
bsalomonf1b7a1d2015-09-28 06:26:28 -0700816/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
817bool SkPaintToGrPaintReplaceShader(GrContext* context,
818 const SkPaint& skPaint,
819 const GrFragmentProcessor* shaderFP,
820 GrPaint* grPaint) {
821 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700822 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000823 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700824 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
825 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000826}
reed8b26b992015-05-07 15:36:17 -0700827
bsalomonf1b7a1d2015-09-28 06:26:28 -0700828/** Ignores the SkShader (if any) on skPaint. */
829bool SkPaintToGrPaintNoShader(GrContext* context,
830 const SkPaint& skPaint,
831 GrPaint* grPaint) {
832 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
833 static const GrFragmentProcessor* kNullShaderFP = nullptr;
834 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
835 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
836 grPaint);
837}
838
839/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
840be setup as a vertex attribute using the specified SkXfermode::Mode. */
841bool SkPaintToGrPaintWithXfermode(GrContext* context,
842 const SkPaint& skPaint,
843 const SkMatrix& viewM,
844 SkXfermode::Mode primColorMode,
845 bool primitiveIsSrc,
846 GrPaint* grPaint) {
847 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
848 grPaint);
849}
850
851
852////////////////////////////////////////////////////////////////////////////////////////////////
853
reed8b26b992015-05-07 15:36:17 -0700854SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
855#ifdef SK_DEBUG
856 const GrSurfaceDesc& desc = tex->desc();
857 SkASSERT(w <= desc.fWidth);
858 SkASSERT(h <= desc.fHeight);
859#endif
860 const GrPixelConfig config = tex->config();
861 SkColorType ct;
862 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700863 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700864 ct = kUnknown_SkColorType;
865 }
866 return SkImageInfo::Make(w, h, ct, at);
867}
868
869
870void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
871 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
872 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700873 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700874}
joshualitt9bc39542015-08-12 12:57:54 -0700875
876GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
877 const SkMatrix& viewM,
878 const SkMatrix& localM,
879 bool* doBicubic) {
880 *doBicubic = false;
881 GrTextureParams::FilterMode textureFilterMode;
882 switch (paintFilterQuality) {
883 case kNone_SkFilterQuality:
884 textureFilterMode = GrTextureParams::kNone_FilterMode;
885 break;
886 case kLow_SkFilterQuality:
887 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
888 break;
889 case kMedium_SkFilterQuality: {
890 SkMatrix matrix;
891 matrix.setConcat(viewM, localM);
892 if (matrix.getMinScale() < SK_Scalar1) {
893 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
894 } else {
895 // Don't trigger MIP level generation unnecessarily.
896 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
897 }
898 break;
899 }
900 case kHigh_SkFilterQuality: {
901 SkMatrix matrix;
902 matrix.setConcat(viewM, localM);
903 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
904 break;
905 }
906 default:
907 SkErrorInternals::SetError( kInvalidPaint_SkError,
908 "Sorry, I don't understand the filtering "
909 "mode you asked for. Falling back to "
910 "MIPMaps.");
911 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
912 break;
913
914 }
915 return textureFilterMode;
916}
reedb5d32632015-09-29 13:36:50 -0700917
918////////////////////////////////////////////////////////////////////////////////////////////////
919
920GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams* params) {
921 SkGrStretch stretch;
922 get_stretch(ctx, this->width(), this->height(), params, &stretch);
923
924 if (SkGrStretch::kNone_Type == stretch.fType) {
925 return this->onRefUnstretchedTexture(ctx);
926 }
927
928 GrUniqueKey stretchedKey;
929 if (this->onMakeStretchedKey(stretch, &stretchedKey)) {
930 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
931 if (result) {
932 return result;
933 }
934 }
935
936 GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch);
937 if (!result) {
938 return nullptr;
939 }
940
941 if (stretchedKey.isValid()) {
942 ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result);
943 this->onNotifyStretchCached(stretchedKey);
944 }
945 return result;
946}
947
948GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) {
949 if (this->width() < ctx->caps()->minTextureSize() ||
950 this->height() < ctx->caps()->minTextureSize())
951 {
952 // we can't trust our ability to use HW to perform the stretch, so we request
953 // a raster instead, and perform the stretch on the CPU.
954 SkBitmap bitmap;
955 if (!this->onGetROBitmap(&bitmap)) {
956 return nullptr;
957 }
958 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
959 return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey());
960 } else {
961 SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx));
962 if (!unstretched) {
963 return nullptr;
964 }
965 return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey());
966 }
967}