blob: ce4af0bfca8a13471b3c2218f42c2d7c31c8f750 [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
41/* Fill out buffer with the compressed format Ganesh expects from a colortable
42 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000043
44 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000045 we could detect that the colortable.count is <= 16, and then repack the
46 indices as nibbles to save RAM, but it would take more time (i.e. a lot
47 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
50 as the colortable.count says it is.
51 */
bsalomone79a2da2014-10-24 12:42:51 -070052static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070053 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000054
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000055 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000056 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000057 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000058 return;
59 }
60
61 SkColorTable* ctable = bitmap.getColorTable();
62 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000063
reed@google.com7111d462014-03-25 16:20:24 +000064 const int count = ctable->count();
65
66 SkDstPixelInfo dstPI;
67 dstPI.fColorType = kRGBA_8888_SkColorType;
68 dstPI.fAlphaType = kPremul_SkAlphaType;
69 dstPI.fPixels = buffer;
70 dstPI.fRowBytes = count * sizeof(SkPMColor);
71
72 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000073 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000074 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080075 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000076 srcPI.fRowBytes = count * sizeof(SkPMColor);
77
78 srcPI.convertPixelsTo(&dstPI, count, 1);
79
reed@google.comac10a2d2010-12-22 21:39:39 +000080 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070081 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000082
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000083 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000084 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
85 } else {
86 // need to trim off the extra bytes per row
87 size_t width = bitmap.width();
88 size_t rowBytes = bitmap.rowBytes();
89 const char* src = (const char*)bitmap.getPixels();
90 for (int y = 0; y < bitmap.height(); y++) {
91 memcpy(dst, src, width);
92 src += rowBytes;
93 dst += width;
94 }
95 }
96}
97
98////////////////////////////////////////////////////////////////////////////////
99
bsalomonafa95e22015-10-12 10:39:46 -0700100static void get_stretch(const GrCaps& caps, int width, int height,
101 const GrTextureParams& params, SkGrStretch* stretch) {
reedb5d32632015-09-29 13:36:50 -0700102 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700103 bool doStretch = false;
bsalomonafa95e22015-10-12 10:39:46 -0700104 if (params.isTiled() && !caps.npotTextureTileSupport() &&
bsalomonc59a1df2015-06-01 07:13:42 -0700105 (!SkIsPow2(width) || !SkIsPow2(height))) {
106 doStretch = true;
bsalomonafa95e22015-10-12 10:39:46 -0700107 stretch->fWidth = GrNextPow2(SkTMax(width, caps.minTextureSize()));
108 stretch->fHeight = GrNextPow2(SkTMax(height, caps.minTextureSize()));
109 } else if (width < caps.minTextureSize() || height < caps.minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700110 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
111 // up using the GPU. If issues persist we may need to CPU-stretch.
112 doStretch = true;
bsalomonafa95e22015-10-12 10:39:46 -0700113 stretch->fWidth = SkTMax(width, caps.minTextureSize());
114 stretch->fHeight = SkTMax(height, caps.minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800115 }
bsalomonc59a1df2015-06-01 07:13:42 -0700116 if (doStretch) {
bsalomonafa95e22015-10-12 10:39:46 -0700117 switch(params.filterMode()) {
118 case GrTextureParams::kNone_FilterMode:
119 stretch->fType = SkGrStretch::kNearest_Type;
120 break;
121 case GrTextureParams::kBilerp_FilterMode:
122 case GrTextureParams::kMipMap_FilterMode:
123 stretch->fType = SkGrStretch::kBilerp_Type;
124 break;
bsalomonc59a1df2015-06-01 07:13:42 -0700125 }
126 } else {
127 stretch->fWidth = -1;
128 stretch->fHeight = -1;
reedb5d32632015-09-29 13:36:50 -0700129 stretch->fType = SkGrStretch::kNone_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700130 }
bsalomon37f9a262015-02-02 13:00:10 -0800131}
132
reed856e9d92015-09-30 12:21:45 -0700133bool GrMakeStretchedKey(const GrUniqueKey& origKey, const SkGrStretch& stretch,
134 GrUniqueKey* stretchedKey) {
reedb5d32632015-09-29 13:36:50 -0700135 if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) {
bsalomonc59a1df2015-06-01 07:13:42 -0700136 uint32_t width = SkToU16(stretch.fWidth);
137 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800138 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700139 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700140 builder[0] = stretch.fType;
141 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800142 builder.finish();
143 return true;
144 }
bsalomon23e619c2015-02-06 11:54:28 -0800145 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800146 return false;
147}
148
reed85d91782015-09-10 14:33:38 -0700149static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
150 SkASSERT(SkIsU16(subset.width()));
151 SkASSERT(SkIsU16(subset.height()));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000152
bsalomon8718aaf2015-02-19 07:24:21 -0800153 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
154 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700155 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700156 builder[1] = subset.x();
157 builder[2] = subset.y();
158 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800159}
bsalomon37f9a262015-02-02 13:00:10 -0800160
reed85d91782015-09-10 14:33:38 -0700161void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
bsalomonafa95e22015-10-12 10:39:46 -0700162 const GrCaps& caps, const GrTextureParams& params) {
163 SkGrStretch stretch;
164 get_stretch(caps, subset.width(), subset.height(), params, &stretch);
165 if (SkGrStretch::kNone_Type != stretch.fType) {
reed8f343722015-08-13 13:32:39 -0700166 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700167 make_unstretched_key(&tmpKey, imageID, subset);
reed856e9d92015-09-30 12:21:45 -0700168 if (!GrMakeStretchedKey(tmpKey, stretch, key)) {
bsalomonafa95e22015-10-12 10:39:46 -0700169 *key = tmpKey;
reed8f343722015-08-13 13:32:39 -0700170 }
171 } else {
reed85d91782015-09-10 14:33:38 -0700172 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700173 }
174}
175
reedb5d32632015-09-29 13:36:50 -0700176static void make_image_keys(uint32_t imageID, const SkIRect& subset, const SkGrStretch& stretch,
reed85d91782015-09-10 14:33:38 -0700177 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
178 make_unstretched_key(key, imageID, subset);
reedb5d32632015-09-29 13:36:50 -0700179 if (SkGrStretch::kNone_Type != stretch.fType) {
reed856e9d92015-09-30 12:21:45 -0700180 GrMakeStretchedKey(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800181 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000182}
183
reed3322a812015-09-16 10:09:24 -0700184GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
185 GrSurfaceDesc desc;
186 desc.fFlags = kNone_GrSurfaceFlags;
187 desc.fWidth = info.width();
188 desc.fHeight = info.height();
189 desc.fConfig = SkImageInfo2GrPixelConfig(info);
190 desc.fSampleCnt = 0;
191 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000192}
193
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000194namespace {
195
196// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800197class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000198public:
bsalomon8718aaf2015-02-19 07:24:21 -0800199 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000200private:
bsalomon8718aaf2015-02-19 07:24:21 -0800201 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000202
mtklein36352bf2015-03-25 18:17:31 -0700203 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800204 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000205 }
206};
207
208} // namespace
209
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000210
reed43fe6182015-09-08 08:37:36 -0700211GrTexture* GrCreateTextureForPixels(GrContext* ctx,
212 const GrUniqueKey& optionalKey,
213 GrSurfaceDesc desc,
214 SkPixelRef* pixelRefForInvalidationNotification,
215 const void* pixels,
216 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700217 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800218 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700219 if (pixelRefForInvalidationNotification) {
220 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
221 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
222 }
bsalomond309e7a2015-04-30 14:18:54 -0700223 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700224 }
225 return result;
226}
227
bsalomonc59a1df2015-06-01 07:13:42 -0700228// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
229// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
230// filtering and the size to stretch the texture to.
reedb5d32632015-09-29 13:36:50 -0700231GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch,
bsalomonc59a1df2015-06-01 07:13:42 -0700232 SkPixelRef* pixelRef,
233 const GrUniqueKey& optionalKey) {
reedb5d32632015-09-29 13:36:50 -0700234 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800235
236 GrContext* context = inputTexture->getContext();
237 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700238 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800239
240 // Either it's a cache miss or the original wasn't cached to begin with.
241 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800242 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700243 rtDesc.fWidth = stretch.fWidth;
244 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800245 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
246
247 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
248 // fail.
bsalomon76228632015-05-29 08:02:10 -0700249 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800250 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700251 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800252 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700253 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800254 rtDesc.fConfig = kSkia8888_GrPixelConfig;
255 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700256 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800257 }
258 } else if (kRGB_GrColorComponentFlags ==
259 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700260 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800261 rtDesc.fConfig = kSkia8888_GrPixelConfig;
262 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700263 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800264 }
265 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700266 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800267 }
268 }
269
reed43fe6182015-09-08 08:37:36 -0700270 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
271 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800272 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700273 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800274 }
275 GrPaint paint;
276
277 // If filtering is not desired then we want to ensure all texels in the resampled image are
278 // copies of texels from the original.
279 GrTextureParams params(SkShader::kClamp_TileMode,
reedb5d32632015-09-29 13:36:50 -0700280 SkGrStretch::kBilerp_Type == stretch.fType ?
bsalomonc59a1df2015-06-01 07:13:42 -0700281 GrTextureParams::kBilerp_FilterMode :
282 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800283 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
284
285 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
286 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
287
robertphillipsc9a37062015-09-01 08:34:28 -0700288 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700289 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700290 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700291 }
292
293 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
294 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800295
reed43fe6182015-09-08 08:37:36 -0700296 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800297}
298
reed43fe6182015-09-08 08:37:36 -0700299GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
300 int expectedW, int expectedH,
301 const void** outStartOfDataToUpload) {
302 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700303#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700304 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
305 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700306 }
307
reed43fe6182015-09-08 08:37:36 -0700308 const uint8_t* bytes = data->bytes();
309 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700310 // Does the data match the dimensions of the bitmap? If not,
311 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700312 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
313 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
314 {
315 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700316 }
317
reed43fe6182015-09-08 08:37:36 -0700318 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
319 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700320 } else if (SkKTXFile::is_ktx(bytes)) {
321 SkKTXFile ktx(data);
322
323 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700324 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700325 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700326 }
327
328 // 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 (ktx.width() != expectedW || ktx.height() != expectedH) {
331 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800332 }
krajcevski99ffe242014-06-03 13:04:35 -0700333
reed43fe6182015-09-08 08:37:36 -0700334 *outStartOfDataToUpload = ktx.pixelData();
335 return kETC1_GrPixelConfig;
336 }
337#endif
338 return kUnknown_GrPixelConfig;
339}
340
341static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
342 const SkBitmap &bm, GrSurfaceDesc desc) {
343 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
344 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700345 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700346 }
347
reed43fe6182015-09-08 08:37:36 -0700348 const void* startOfTexData;
349 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
350 &startOfTexData);
351 if (kUnknown_GrPixelConfig == desc.fConfig) {
352 return nullptr;
353 }
354
355 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700356}
reed43fe6182015-09-08 08:37:36 -0700357
358/*
359 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
360 * be gone from SkPixelRef, and we can remove this subclass entirely.
361 */
362class PixelRef_GrYUVProvider : public GrYUVProvider {
363 SkPixelRef* fPR;
364
365public:
366 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
367
368 uint32_t onGetID() override { return fPR->getGenerationID(); }
369 bool onGetYUVSizes(SkISize sizes[3]) override {
370 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
371 }
372 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
373 SkYUVColorSpace* space) override {
374 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
375 }
376};
krajcevski9c0e6292014-06-02 07:38:14 -0700377
bsalomon8718aaf2015-02-19 07:24:21 -0800378static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700379 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700380 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700381 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700382 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800383 (pixelRef->info().width() != bm.info().width()) ||
384 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700385 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700386 }
387
sugoiba18f912015-02-04 10:53:03 -0800388 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700389 PixelRef_GrYUVProvider provider(pixelRef);
390 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
391 if (!texture) {
392 return nullptr;
393 }
394
sugoiba18f912015-02-04 10:53:03 -0800395 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700396 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
397 pixelRef->addGenIDChangeListener(listener);
398 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800399 }
reed43fe6182015-09-08 08:37:36 -0700400 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700401}
402
bsalomon37f9a262015-02-02 13:00:10 -0800403static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
404 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800405 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700406 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
407 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700408 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700409 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 SkBitmap tmpBitmap;
411
412 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000413
reed3322a812015-09-16 10:09:24 -0700414 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700415 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416
reed0689d7b2014-06-14 05:30:20 -0700417 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700418 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700419 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
420 bitmap->width(), bitmap->height());
421 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700422 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
424 // our compressed data will be trimmed, so pass width() for its
425 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700426 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
427 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000429 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 // now bitmap points to our temp, which has been promoted to 32bits
431 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000432 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 }
reed43fe6182015-09-08 08:37:36 -0700434 } else if (!bitmap->readyToDraw()) {
435 // If the bitmap had compressed data and was then uncompressed, it'll still return
436 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
437 // the bitmap has available pixels, then they might not be what the decompressed
438 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800439 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700440 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700441 return texture;
442 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
bsalomond2a6f4e2015-02-04 10:55:54 -0800445 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
446 if (texture) {
447 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700448 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800449
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000450 SkAutoLockPixels alp(*bitmap);
451 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700452 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000453 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000454
reed43fe6182015-09-08 08:37:36 -0700455 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
456 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800457}
458
reedb5d32632015-09-29 13:36:50 -0700459static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700460 SkBitmap stretched;
461 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
462 SkCanvas canvas(stretched);
463 SkPaint paint;
464 switch (stretch.fType) {
reedb5d32632015-09-29 13:36:50 -0700465 case SkGrStretch::kNearest_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700466 paint.setFilterQuality(kNone_SkFilterQuality);
467 break;
reedb5d32632015-09-29 13:36:50 -0700468 case SkGrStretch::kBilerp_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700469 paint.setFilterQuality(kLow_SkFilterQuality);
470 break;
reedb5d32632015-09-29 13:36:50 -0700471 case SkGrStretch::kNone_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700472 SkDEBUGFAIL("Shouldn't get here.");
473 break;
474 }
475 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700476 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700477 return stretched;
478}
479
reed85d91782015-09-10 14:33:38 -0700480bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
bsalomonafa95e22015-10-12 10:39:46 -0700481 GrTexture* nativeTexture, const GrTextureParams& params) {
reedb5d32632015-09-29 13:36:50 -0700482 SkGrStretch stretch;
bsalomonafa95e22015-10-12 10:39:46 -0700483 get_stretch(*ctx->caps(), subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800484
reed85d91782015-09-10 14:33:38 -0700485 // Handle the case where the bitmap/image is explicitly texture backed.
486 if (nativeTexture) {
reedb5d32632015-09-29 13:36:50 -0700487 if (SkGrStretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800488 return true;
489 }
reed85d91782015-09-10 14:33:38 -0700490 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800491 if (!key.isValid()) {
492 return false;
493 }
bsalomon8718aaf2015-02-19 07:24:21 -0800494 GrUniqueKey stretchedKey;
reed856e9d92015-09-30 12:21:45 -0700495 GrMakeStretchedKey(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700496 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800497 }
498
bsalomon8718aaf2015-02-19 07:24:21 -0800499 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700500 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700501 return ctx->textureProvider()->existsTextureWithUniqueKey(
reedb5d32632015-09-29 13:36:50 -0700502 (SkGrStretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000503}
reed@google.comac10a2d2010-12-22 21:39:39 +0000504
reedb5d32632015-09-29 13:36:50 -0700505class Bitmap_GrTextureMaker : public GrTextureMaker {
506public:
507 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
508 : INHERITED(bitmap.width(), bitmap.height())
509 , fBitmap(bitmap)
510 {}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000511
reedb5d32632015-09-29 13:36:50 -0700512protected:
513 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
514 GrTexture* tex = fBitmap.getTexture();
515 if (tex) {
516 return SkRef(tex);
bsalomon88425562015-02-04 09:12:46 -0800517 }
bsalomon88425562015-02-04 09:12:46 -0800518
reedb5d32632015-09-29 13:36:50 -0700519 GrUniqueKey unstretchedKey;
520 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000521
reedb5d32632015-09-29 13:36:50 -0700522 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800523 if (result) {
524 return result;
525 }
reedb5d32632015-09-29 13:36:50 -0700526 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800527 }
bsalomone137db82015-01-31 20:10:56 -0800528
reedb5d32632015-09-29 13:36:50 -0700529 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
530 if (fBitmap.isVolatile()) {
531 return false;
532 }
533
534 GrUniqueKey unstretchedKey;
535 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
reed856e9d92015-09-30 12:21:45 -0700536 return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800537 }
bsalomone137db82015-01-31 20:10:56 -0800538
reedb5d32632015-09-29 13:36:50 -0700539 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
540 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey));
541 }
bsalomon37f9a262015-02-02 13:00:10 -0800542
reedb5d32632015-09-29 13:36:50 -0700543 bool onGetROBitmap(SkBitmap* bitmap) override {
544 *bitmap = fBitmap;
545 return true;
546 }
547
548private:
549 const SkBitmap fBitmap;
550
551 typedef GrTextureMaker INHERITED;
552};
553
554GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700555 const GrTextureParams& params) {
reedb5d32632015-09-29 13:36:50 -0700556 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000557}
reed8f343722015-08-13 13:32:39 -0700558
rileya@google.com24f3ad12012-07-18 21:47:40 +0000559///////////////////////////////////////////////////////////////////////////////
560
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000561// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
562// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800563GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000564 switch (ct) {
565 case kUnknown_SkColorType:
566 return kUnknown_GrPixelConfig;
567 case kAlpha_8_SkColorType:
568 return kAlpha_8_GrPixelConfig;
569 case kRGB_565_SkColorType:
570 return kRGB_565_GrPixelConfig;
571 case kARGB_4444_SkColorType:
572 return kRGBA_4444_GrPixelConfig;
573 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700574 //if (kSRGB_SkColorProfileType == pt) {
575 // return kSRGBA_8888_GrPixelConfig;
576 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000577 return kRGBA_8888_GrPixelConfig;
578 case kBGRA_8888_SkColorType:
579 return kBGRA_8888_GrPixelConfig;
580 case kIndex_8_SkColorType:
581 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700582 case kGray_8_SkColorType:
583 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000584 }
585 SkASSERT(0); // shouldn't get here
586 return kUnknown_GrPixelConfig;
587}
588
jvanverthfa1e8a72014-12-22 08:31:49 -0800589bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
590 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000591 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800592 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000593 switch (config) {
594 case kAlpha_8_GrPixelConfig:
595 ct = kAlpha_8_SkColorType;
596 break;
597 case kIndex_8_GrPixelConfig:
598 ct = kIndex_8_SkColorType;
599 break;
600 case kRGB_565_GrPixelConfig:
601 ct = kRGB_565_SkColorType;
602 break;
603 case kRGBA_4444_GrPixelConfig:
604 ct = kARGB_4444_SkColorType;
605 break;
606 case kRGBA_8888_GrPixelConfig:
607 ct = kRGBA_8888_SkColorType;
608 break;
609 case kBGRA_8888_GrPixelConfig:
610 ct = kBGRA_8888_SkColorType;
611 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800612 case kSRGBA_8888_GrPixelConfig:
613 ct = kRGBA_8888_SkColorType;
614 pt = kSRGB_SkColorProfileType;
615 break;
reed@google.combf790232013-12-13 19:45:58 +0000616 default:
617 return false;
618 }
619 if (ctOut) {
620 *ctOut = ct;
621 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800622 if (ptOut) {
623 *ptOut = pt;
624 }
reed@google.combf790232013-12-13 19:45:58 +0000625 return true;
626}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000627
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000628
bsalomonf1b7a1d2015-09-28 06:26:28 -0700629////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000630
bsalomonaa48d362015-10-01 08:34:17 -0700631static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
632 if (primitiveIsSrc) {
633 return SkXfermode::kSrc_Mode != mode;
634 } else {
635 return SkXfermode::kDst_Mode != mode;
636 }
637}
638
bsalomonf1b7a1d2015-09-28 06:26:28 -0700639static inline bool skpaint_to_grpaint_impl(GrContext* context,
640 const SkPaint& skPaint,
641 const SkMatrix& viewM,
642 const GrFragmentProcessor** shaderProcessor,
643 SkXfermode::Mode* primColorMode,
644 bool primitiveIsSrc,
645 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000646 grPaint->setAntiAlias(skPaint.isAntiAlias());
647
bsalomonf1b7a1d2015-09-28 06:26:28 -0700648 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
649 // of per-vertex colors.
650 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700651 const GrFragmentProcessor* shaderFP = nullptr;
652 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
653 if (shaderProcessor) {
654 shaderFP = *shaderProcessor;
655 } else if (const SkShader* shader = skPaint.getShader()) {
656 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700657 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700658 shaderFP = aufp;
659 if (!shaderFP) {
660 return false;
661 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700662 }
663 }
664
665 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
666 // a known constant value. In that case we can simply apply a color filter during this
667 // conversion without converting the color filter to a GrFragmentProcessor.
668 bool applyColorFilterToPaintColor = false;
669 if (shaderFP) {
670 if (primColorMode) {
671 // There is a blend between the primitive color and the shader color. The shader sees
672 // the opaque paint color. The shader's output is blended using the provided mode by
673 // the primitive color. The blended color is then modulated by the paint's alpha.
674
675 // The geometry processor will insert the primitive color to start the color chain, so
676 // the GrPaint color will be ignored.
677
678 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
679
680 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
681 aufp.reset(shaderFP);
682
683 if (primitiveIsSrc) {
684 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
685 *primColorMode);
686 } else {
687 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
688 *primColorMode);
689 }
690 aufp.reset(shaderFP);
691 // The above may return null if compose results in a pass through of the prim color.
692 if (shaderFP) {
693 grPaint->addColorFragmentProcessor(shaderFP);
694 }
695
696 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
697 if (GrColor_WHITE != paintAlpha) {
698 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
699 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
700 }
701 } else {
702 // The shader's FP sees the paint unpremul color
703 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
704 grPaint->addColorFragmentProcessor(shaderFP);
705 }
706 } else {
707 if (primColorMode) {
708 // There is a blend between the primitive color and the paint color. The blend considers
709 // the opaque paint color. The paint's alpha is applied to the post-blended color.
710 SkAutoTUnref<const GrFragmentProcessor> processor(
711 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
712 GrConstColorProcessor::kIgnore_InputMode));
713 if (primitiveIsSrc) {
714 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
715 *primColorMode));
716 } else {
717 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
718 *primColorMode));
719
720 }
721 if (processor) {
722 grPaint->addColorFragmentProcessor(processor);
723 }
724
bsalomonaa48d362015-10-01 08:34:17 -0700725 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700726
727 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700728 if (GrColor_WHITE != paintAlpha) {
729 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
730 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
731 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700732 } else {
733 // No shader, no primitive color.
734 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
735 applyColorFilterToPaintColor = true;
736 }
737 }
738
739 SkColorFilter* colorFilter = skPaint.getColorFilter();
740 if (colorFilter) {
741 if (applyColorFilterToPaintColor) {
742 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
743 } else {
bsalomone25eea42015-09-29 06:38:55 -0700744 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700745 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700746 if (cfFP) {
747 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700748 } else {
749 return false;
750 }
751 }
752 }
753
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000754 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700755 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800756 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000757 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700758 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800759 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000760 }
egdaniel378092f2014-12-03 10:40:13 -0800761 SkASSERT(xpFactory);
762 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800763
krajcevskif461a8f2014-06-19 14:14:06 -0700764#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700765 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700766 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700767 }
768#endif
bsalomonbed83a62015-04-15 14:18:34 -0700769 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000770}
771
bsalomonf1b7a1d2015-09-28 06:26:28 -0700772bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
773 GrPaint* grPaint) {
774 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
775}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000776
bsalomonf1b7a1d2015-09-28 06:26:28 -0700777/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
778bool SkPaintToGrPaintReplaceShader(GrContext* context,
779 const SkPaint& skPaint,
780 const GrFragmentProcessor* shaderFP,
781 GrPaint* grPaint) {
782 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700783 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000784 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700785 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
786 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000787}
reed8b26b992015-05-07 15:36:17 -0700788
bsalomonf1b7a1d2015-09-28 06:26:28 -0700789/** Ignores the SkShader (if any) on skPaint. */
790bool SkPaintToGrPaintNoShader(GrContext* context,
791 const SkPaint& skPaint,
792 GrPaint* grPaint) {
793 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
794 static const GrFragmentProcessor* kNullShaderFP = nullptr;
795 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
796 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
797 grPaint);
798}
799
800/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
801be setup as a vertex attribute using the specified SkXfermode::Mode. */
802bool SkPaintToGrPaintWithXfermode(GrContext* context,
803 const SkPaint& skPaint,
804 const SkMatrix& viewM,
805 SkXfermode::Mode primColorMode,
806 bool primitiveIsSrc,
807 GrPaint* grPaint) {
808 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
809 grPaint);
810}
811
812
813////////////////////////////////////////////////////////////////////////////////////////////////
814
reed8b26b992015-05-07 15:36:17 -0700815SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
816#ifdef SK_DEBUG
817 const GrSurfaceDesc& desc = tex->desc();
818 SkASSERT(w <= desc.fWidth);
819 SkASSERT(h <= desc.fHeight);
820#endif
821 const GrPixelConfig config = tex->config();
822 SkColorType ct;
823 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700824 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700825 ct = kUnknown_SkColorType;
826 }
827 return SkImageInfo::Make(w, h, ct, at);
828}
829
830
831void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
832 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
833 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700834 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700835}
joshualitt9bc39542015-08-12 12:57:54 -0700836
837GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
838 const SkMatrix& viewM,
839 const SkMatrix& localM,
840 bool* doBicubic) {
841 *doBicubic = false;
842 GrTextureParams::FilterMode textureFilterMode;
843 switch (paintFilterQuality) {
844 case kNone_SkFilterQuality:
845 textureFilterMode = GrTextureParams::kNone_FilterMode;
846 break;
847 case kLow_SkFilterQuality:
848 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
849 break;
850 case kMedium_SkFilterQuality: {
851 SkMatrix matrix;
852 matrix.setConcat(viewM, localM);
853 if (matrix.getMinScale() < SK_Scalar1) {
854 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
855 } else {
856 // Don't trigger MIP level generation unnecessarily.
857 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
858 }
859 break;
860 }
861 case kHigh_SkFilterQuality: {
862 SkMatrix matrix;
863 matrix.setConcat(viewM, localM);
864 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
865 break;
866 }
867 default:
868 SkErrorInternals::SetError( kInvalidPaint_SkError,
869 "Sorry, I don't understand the filtering "
870 "mode you asked for. Falling back to "
871 "MIPMaps.");
872 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
873 break;
874
875 }
876 return textureFilterMode;
877}
reedb5d32632015-09-29 13:36:50 -0700878
879////////////////////////////////////////////////////////////////////////////////////////////////
880
bsalomonafa95e22015-10-12 10:39:46 -0700881GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams& params) {
reedb5d32632015-09-29 13:36:50 -0700882 SkGrStretch stretch;
bsalomonafa95e22015-10-12 10:39:46 -0700883 get_stretch(*ctx->caps(), this->width(), this->height(), params, &stretch);
reedb5d32632015-09-29 13:36:50 -0700884
885 if (SkGrStretch::kNone_Type == stretch.fType) {
886 return this->onRefUnstretchedTexture(ctx);
887 }
888
889 GrUniqueKey stretchedKey;
890 if (this->onMakeStretchedKey(stretch, &stretchedKey)) {
891 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
892 if (result) {
893 return result;
894 }
895 }
896
897 GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch);
898 if (!result) {
899 return nullptr;
900 }
901
902 if (stretchedKey.isValid()) {
903 ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result);
904 this->onNotifyStretchCached(stretchedKey);
905 }
906 return result;
907}
908
909GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) {
910 if (this->width() < ctx->caps()->minTextureSize() ||
911 this->height() < ctx->caps()->minTextureSize())
912 {
913 // we can't trust our ability to use HW to perform the stretch, so we request
914 // a raster instead, and perform the stretch on the CPU.
915 SkBitmap bitmap;
916 if (!this->onGetROBitmap(&bitmap)) {
917 return nullptr;
918 }
919 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
920 return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey());
921 } else {
922 SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx));
923 if (!unstretched) {
924 return nullptr;
925 }
926 return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey());
927 }
928}