blob: 1813433b300c2e3b003ec8172eeb66ecd095acd4 [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
reed3322a812015-09-16 10:09:24 -0700176GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
177 GrSurfaceDesc desc;
178 desc.fFlags = kNone_GrSurfaceFlags;
179 desc.fWidth = info.width();
180 desc.fHeight = info.height();
181 desc.fConfig = SkImageInfo2GrPixelConfig(info);
182 desc.fSampleCnt = 0;
183 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000184}
185
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000186namespace {
187
188// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800189class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000190public:
bsalomon8718aaf2015-02-19 07:24:21 -0800191 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000192private:
bsalomon8718aaf2015-02-19 07:24:21 -0800193 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000194
mtklein36352bf2015-03-25 18:17:31 -0700195 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800196 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000197 }
198};
199
200} // namespace
201
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000202
reed43fe6182015-09-08 08:37:36 -0700203GrTexture* GrCreateTextureForPixels(GrContext* ctx,
204 const GrUniqueKey& optionalKey,
205 GrSurfaceDesc desc,
206 SkPixelRef* pixelRefForInvalidationNotification,
207 const void* pixels,
208 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700209 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800210 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700211 if (pixelRefForInvalidationNotification) {
212 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
213 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
214 }
bsalomond309e7a2015-04-30 14:18:54 -0700215 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700216 }
217 return result;
218}
219
bsalomonc59a1df2015-06-01 07:13:42 -0700220// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
221// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
222// filtering and the size to stretch the texture to.
reedb5d32632015-09-29 13:36:50 -0700223GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch,
bsalomonc59a1df2015-06-01 07:13:42 -0700224 SkPixelRef* pixelRef,
225 const GrUniqueKey& optionalKey) {
reedb5d32632015-09-29 13:36:50 -0700226 SkASSERT(SkGrStretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800227
228 GrContext* context = inputTexture->getContext();
229 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700230 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800231
232 // Either it's a cache miss or the original wasn't cached to begin with.
233 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800234 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700235 rtDesc.fWidth = stretch.fWidth;
236 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800237 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
238
239 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
240 // fail.
bsalomon76228632015-05-29 08:02:10 -0700241 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800242 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700243 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800244 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700245 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800246 rtDesc.fConfig = kSkia8888_GrPixelConfig;
247 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700248 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800249 }
250 } else if (kRGB_GrColorComponentFlags ==
251 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700252 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800253 rtDesc.fConfig = kSkia8888_GrPixelConfig;
254 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700255 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800256 }
257 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800259 }
260 }
261
reed43fe6182015-09-08 08:37:36 -0700262 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
263 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800264 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700265 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800266 }
267 GrPaint paint;
268
269 // If filtering is not desired then we want to ensure all texels in the resampled image are
270 // copies of texels from the original.
271 GrTextureParams params(SkShader::kClamp_TileMode,
reedb5d32632015-09-29 13:36:50 -0700272 SkGrStretch::kBilerp_Type == stretch.fType ?
bsalomonc59a1df2015-06-01 07:13:42 -0700273 GrTextureParams::kBilerp_FilterMode :
274 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800275 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
276
277 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
278 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
279
robertphillips2e1e51f2015-10-15 08:01:48 -0700280 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(stretched->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700281 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700282 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700283 }
284
robertphillips2e1e51f2015-10-15 08:01:48 -0700285 drawContext->drawNonAARectToRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800286
reed43fe6182015-09-08 08:37:36 -0700287 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800288}
289
reed43fe6182015-09-08 08:37:36 -0700290GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
291 int expectedW, int expectedH,
292 const void** outStartOfDataToUpload) {
293 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700294#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700295 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
296 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700297 }
298
reed43fe6182015-09-08 08:37:36 -0700299 const uint8_t* bytes = data->bytes();
300 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700301 // Does the data match the dimensions of the bitmap? If not,
302 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700303 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
304 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
305 {
306 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700307 }
308
reed43fe6182015-09-08 08:37:36 -0700309 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
310 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700311 } else if (SkKTXFile::is_ktx(bytes)) {
312 SkKTXFile ktx(data);
313
314 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700315 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700316 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700317 }
318
319 // Does the data match the dimensions of the bitmap? If not,
320 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700321 if (ktx.width() != expectedW || ktx.height() != expectedH) {
322 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800323 }
krajcevski99ffe242014-06-03 13:04:35 -0700324
reed43fe6182015-09-08 08:37:36 -0700325 *outStartOfDataToUpload = ktx.pixelData();
326 return kETC1_GrPixelConfig;
327 }
328#endif
329 return kUnknown_GrPixelConfig;
330}
331
332static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
333 const SkBitmap &bm, GrSurfaceDesc desc) {
334 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
335 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700336 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700337 }
338
reed43fe6182015-09-08 08:37:36 -0700339 const void* startOfTexData;
340 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
341 &startOfTexData);
342 if (kUnknown_GrPixelConfig == desc.fConfig) {
343 return nullptr;
344 }
345
346 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700347}
reed43fe6182015-09-08 08:37:36 -0700348
349/*
350 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
351 * be gone from SkPixelRef, and we can remove this subclass entirely.
352 */
353class PixelRef_GrYUVProvider : public GrYUVProvider {
354 SkPixelRef* fPR;
355
356public:
357 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
358
359 uint32_t onGetID() override { return fPR->getGenerationID(); }
360 bool onGetYUVSizes(SkISize sizes[3]) override {
361 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
362 }
363 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
364 SkYUVColorSpace* space) override {
365 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
366 }
367};
krajcevski9c0e6292014-06-02 07:38:14 -0700368
bsalomon8718aaf2015-02-19 07:24:21 -0800369static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700370 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700371 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700372 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700373 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800374 (pixelRef->info().width() != bm.info().width()) ||
375 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700376 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700377 }
378
sugoiba18f912015-02-04 10:53:03 -0800379 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700380 PixelRef_GrYUVProvider provider(pixelRef);
381 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
382 if (!texture) {
383 return nullptr;
384 }
385
sugoiba18f912015-02-04 10:53:03 -0800386 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700387 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
388 pixelRef->addGenIDChangeListener(listener);
389 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800390 }
reed43fe6182015-09-08 08:37:36 -0700391 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700392}
393
bsalomon37f9a262015-02-02 13:00:10 -0800394static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
395 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800396 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700397 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
398 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700399 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700400 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 SkBitmap tmpBitmap;
402
403 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000404
reed3322a812015-09-16 10:09:24 -0700405 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700406 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
reed0689d7b2014-06-14 05:30:20 -0700408 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700409 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700410 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
411 bitmap->width(), bitmap->height());
412 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700413 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000414
415 // our compressed data will be trimmed, so pass width() for its
416 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700417 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
418 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000419 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000420 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 // now bitmap points to our temp, which has been promoted to 32bits
422 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000423 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 }
reed43fe6182015-09-08 08:37:36 -0700425 } else if (!bitmap->readyToDraw()) {
426 // If the bitmap had compressed data and was then uncompressed, it'll still return
427 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
428 // the bitmap has available pixels, then they might not be what the decompressed
429 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800430 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700431 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700432 return texture;
433 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
bsalomond2a6f4e2015-02-04 10:55:54 -0800436 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
437 if (texture) {
438 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700439 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800440
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000441 SkAutoLockPixels alp(*bitmap);
442 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700443 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000444 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000445
reed43fe6182015-09-08 08:37:36 -0700446 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
447 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800448}
449
reedb5d32632015-09-29 13:36:50 -0700450static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700451 SkBitmap stretched;
452 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
453 SkCanvas canvas(stretched);
454 SkPaint paint;
455 switch (stretch.fType) {
reedb5d32632015-09-29 13:36:50 -0700456 case SkGrStretch::kNearest_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700457 paint.setFilterQuality(kNone_SkFilterQuality);
458 break;
reedb5d32632015-09-29 13:36:50 -0700459 case SkGrStretch::kBilerp_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700460 paint.setFilterQuality(kLow_SkFilterQuality);
461 break;
reedb5d32632015-09-29 13:36:50 -0700462 case SkGrStretch::kNone_Type:
bsalomonb4d40ef2015-07-15 10:12:16 -0700463 SkDEBUGFAIL("Shouldn't get here.");
464 break;
465 }
466 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700467 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700468 return stretched;
469}
470
reedb5d32632015-09-29 13:36:50 -0700471class Bitmap_GrTextureMaker : public GrTextureMaker {
472public:
473 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
474 : INHERITED(bitmap.width(), bitmap.height())
475 , fBitmap(bitmap)
476 {}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000477
reedb5d32632015-09-29 13:36:50 -0700478protected:
479 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override {
480 GrTexture* tex = fBitmap.getTexture();
481 if (tex) {
482 return SkRef(tex);
bsalomon88425562015-02-04 09:12:46 -0800483 }
bsalomon88425562015-02-04 09:12:46 -0800484
reedb5d32632015-09-29 13:36:50 -0700485 GrUniqueKey unstretchedKey;
486 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000487
reedb5d32632015-09-29 13:36:50 -0700488 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800489 if (result) {
490 return result;
491 }
reedb5d32632015-09-29 13:36:50 -0700492 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800493 }
bsalomone137db82015-01-31 20:10:56 -0800494
reedb5d32632015-09-29 13:36:50 -0700495 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override {
496 if (fBitmap.isVolatile()) {
497 return false;
498 }
499
500 GrUniqueKey unstretchedKey;
501 make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset());
reed856e9d92015-09-30 12:21:45 -0700502 return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800503 }
bsalomone137db82015-01-31 20:10:56 -0800504
reedb5d32632015-09-29 13:36:50 -0700505 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
506 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey));
507 }
bsalomon37f9a262015-02-02 13:00:10 -0800508
reedb5d32632015-09-29 13:36:50 -0700509 bool onGetROBitmap(SkBitmap* bitmap) override {
510 *bitmap = fBitmap;
511 return true;
512 }
513
514private:
515 const SkBitmap fBitmap;
516
517 typedef GrTextureMaker INHERITED;
518};
519
520GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700521 const GrTextureParams& params) {
reedb5d32632015-09-29 13:36:50 -0700522 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000523}
reed8f343722015-08-13 13:32:39 -0700524
rileya@google.com24f3ad12012-07-18 21:47:40 +0000525///////////////////////////////////////////////////////////////////////////////
526
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000527// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
528// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800529GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000530 switch (ct) {
531 case kUnknown_SkColorType:
532 return kUnknown_GrPixelConfig;
533 case kAlpha_8_SkColorType:
534 return kAlpha_8_GrPixelConfig;
535 case kRGB_565_SkColorType:
536 return kRGB_565_GrPixelConfig;
537 case kARGB_4444_SkColorType:
538 return kRGBA_4444_GrPixelConfig;
539 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700540 //if (kSRGB_SkColorProfileType == pt) {
541 // return kSRGBA_8888_GrPixelConfig;
542 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000543 return kRGBA_8888_GrPixelConfig;
544 case kBGRA_8888_SkColorType:
545 return kBGRA_8888_GrPixelConfig;
546 case kIndex_8_SkColorType:
547 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700548 case kGray_8_SkColorType:
549 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000550 }
551 SkASSERT(0); // shouldn't get here
552 return kUnknown_GrPixelConfig;
553}
554
jvanverthfa1e8a72014-12-22 08:31:49 -0800555bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
556 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000557 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800558 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000559 switch (config) {
560 case kAlpha_8_GrPixelConfig:
561 ct = kAlpha_8_SkColorType;
562 break;
563 case kIndex_8_GrPixelConfig:
564 ct = kIndex_8_SkColorType;
565 break;
566 case kRGB_565_GrPixelConfig:
567 ct = kRGB_565_SkColorType;
568 break;
569 case kRGBA_4444_GrPixelConfig:
570 ct = kARGB_4444_SkColorType;
571 break;
572 case kRGBA_8888_GrPixelConfig:
573 ct = kRGBA_8888_SkColorType;
574 break;
575 case kBGRA_8888_GrPixelConfig:
576 ct = kBGRA_8888_SkColorType;
577 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800578 case kSRGBA_8888_GrPixelConfig:
579 ct = kRGBA_8888_SkColorType;
580 pt = kSRGB_SkColorProfileType;
581 break;
reed@google.combf790232013-12-13 19:45:58 +0000582 default:
583 return false;
584 }
585 if (ctOut) {
586 *ctOut = ct;
587 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800588 if (ptOut) {
589 *ptOut = pt;
590 }
reed@google.combf790232013-12-13 19:45:58 +0000591 return true;
592}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000593
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000594
bsalomonf1b7a1d2015-09-28 06:26:28 -0700595////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000596
bsalomonaa48d362015-10-01 08:34:17 -0700597static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
598 if (primitiveIsSrc) {
599 return SkXfermode::kSrc_Mode != mode;
600 } else {
601 return SkXfermode::kDst_Mode != mode;
602 }
603}
604
bsalomonf1b7a1d2015-09-28 06:26:28 -0700605static inline bool skpaint_to_grpaint_impl(GrContext* context,
606 const SkPaint& skPaint,
607 const SkMatrix& viewM,
608 const GrFragmentProcessor** shaderProcessor,
609 SkXfermode::Mode* primColorMode,
610 bool primitiveIsSrc,
611 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000612 grPaint->setAntiAlias(skPaint.isAntiAlias());
613
bsalomonf1b7a1d2015-09-28 06:26:28 -0700614 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
615 // of per-vertex colors.
616 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700617 const GrFragmentProcessor* shaderFP = nullptr;
618 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
619 if (shaderProcessor) {
620 shaderFP = *shaderProcessor;
621 } else if (const SkShader* shader = skPaint.getShader()) {
622 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700623 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700624 shaderFP = aufp;
625 if (!shaderFP) {
626 return false;
627 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700628 }
629 }
630
631 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
632 // a known constant value. In that case we can simply apply a color filter during this
633 // conversion without converting the color filter to a GrFragmentProcessor.
634 bool applyColorFilterToPaintColor = false;
635 if (shaderFP) {
636 if (primColorMode) {
637 // There is a blend between the primitive color and the shader color. The shader sees
638 // the opaque paint color. The shader's output is blended using the provided mode by
639 // the primitive color. The blended color is then modulated by the paint's alpha.
640
641 // The geometry processor will insert the primitive color to start the color chain, so
642 // the GrPaint color will be ignored.
643
644 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
645
646 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
647 aufp.reset(shaderFP);
648
649 if (primitiveIsSrc) {
650 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
651 *primColorMode);
652 } else {
653 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
654 *primColorMode);
655 }
656 aufp.reset(shaderFP);
657 // The above may return null if compose results in a pass through of the prim color.
658 if (shaderFP) {
659 grPaint->addColorFragmentProcessor(shaderFP);
660 }
661
662 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
663 if (GrColor_WHITE != paintAlpha) {
664 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
665 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
666 }
667 } else {
668 // The shader's FP sees the paint unpremul color
669 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
670 grPaint->addColorFragmentProcessor(shaderFP);
671 }
672 } else {
673 if (primColorMode) {
674 // There is a blend between the primitive color and the paint color. The blend considers
675 // the opaque paint color. The paint's alpha is applied to the post-blended color.
676 SkAutoTUnref<const GrFragmentProcessor> processor(
677 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
678 GrConstColorProcessor::kIgnore_InputMode));
679 if (primitiveIsSrc) {
680 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
681 *primColorMode));
682 } else {
683 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
684 *primColorMode));
685
686 }
687 if (processor) {
688 grPaint->addColorFragmentProcessor(processor);
689 }
690
bsalomonaa48d362015-10-01 08:34:17 -0700691 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700692
693 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700694 if (GrColor_WHITE != paintAlpha) {
695 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
696 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
697 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700698 } else {
699 // No shader, no primitive color.
700 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
701 applyColorFilterToPaintColor = true;
702 }
703 }
704
705 SkColorFilter* colorFilter = skPaint.getColorFilter();
706 if (colorFilter) {
707 if (applyColorFilterToPaintColor) {
708 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
709 } else {
bsalomone25eea42015-09-29 06:38:55 -0700710 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700711 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700712 if (cfFP) {
713 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700714 } else {
715 return false;
716 }
717 }
718 }
719
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000720 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700721 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800722 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000723 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700724 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800725 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000726 }
egdaniel378092f2014-12-03 10:40:13 -0800727 SkASSERT(xpFactory);
728 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800729
krajcevskif461a8f2014-06-19 14:14:06 -0700730#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700731 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700732 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700733 }
734#endif
bsalomonbed83a62015-04-15 14:18:34 -0700735 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000736}
737
bsalomonf1b7a1d2015-09-28 06:26:28 -0700738bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
739 GrPaint* grPaint) {
740 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
741}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000742
bsalomonf1b7a1d2015-09-28 06:26:28 -0700743/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
744bool SkPaintToGrPaintReplaceShader(GrContext* context,
745 const SkPaint& skPaint,
746 const GrFragmentProcessor* shaderFP,
747 GrPaint* grPaint) {
748 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700749 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000750 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700751 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
752 grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000753}
reed8b26b992015-05-07 15:36:17 -0700754
bsalomonf1b7a1d2015-09-28 06:26:28 -0700755/** Ignores the SkShader (if any) on skPaint. */
756bool SkPaintToGrPaintNoShader(GrContext* context,
757 const SkPaint& skPaint,
758 GrPaint* grPaint) {
759 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
760 static const GrFragmentProcessor* kNullShaderFP = nullptr;
761 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
762 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
763 grPaint);
764}
765
766/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
767be setup as a vertex attribute using the specified SkXfermode::Mode. */
768bool SkPaintToGrPaintWithXfermode(GrContext* context,
769 const SkPaint& skPaint,
770 const SkMatrix& viewM,
771 SkXfermode::Mode primColorMode,
772 bool primitiveIsSrc,
773 GrPaint* grPaint) {
774 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
775 grPaint);
776}
777
778
779////////////////////////////////////////////////////////////////////////////////////////////////
780
reed8b26b992015-05-07 15:36:17 -0700781SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
782#ifdef SK_DEBUG
783 const GrSurfaceDesc& desc = tex->desc();
784 SkASSERT(w <= desc.fWidth);
785 SkASSERT(h <= desc.fHeight);
786#endif
787 const GrPixelConfig config = tex->config();
788 SkColorType ct;
789 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700790 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700791 ct = kUnknown_SkColorType;
792 }
793 return SkImageInfo::Make(w, h, ct, at);
794}
795
796
797void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
798 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
799 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700800 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700801}
joshualitt9bc39542015-08-12 12:57:54 -0700802
803GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
804 const SkMatrix& viewM,
805 const SkMatrix& localM,
806 bool* doBicubic) {
807 *doBicubic = false;
808 GrTextureParams::FilterMode textureFilterMode;
809 switch (paintFilterQuality) {
810 case kNone_SkFilterQuality:
811 textureFilterMode = GrTextureParams::kNone_FilterMode;
812 break;
813 case kLow_SkFilterQuality:
814 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
815 break;
816 case kMedium_SkFilterQuality: {
817 SkMatrix matrix;
818 matrix.setConcat(viewM, localM);
819 if (matrix.getMinScale() < SK_Scalar1) {
820 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
821 } else {
822 // Don't trigger MIP level generation unnecessarily.
823 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
824 }
825 break;
826 }
827 case kHigh_SkFilterQuality: {
828 SkMatrix matrix;
829 matrix.setConcat(viewM, localM);
830 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
831 break;
832 }
833 default:
834 SkErrorInternals::SetError( kInvalidPaint_SkError,
835 "Sorry, I don't understand the filtering "
836 "mode you asked for. Falling back to "
837 "MIPMaps.");
838 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
839 break;
840
841 }
842 return textureFilterMode;
843}
reedb5d32632015-09-29 13:36:50 -0700844
845////////////////////////////////////////////////////////////////////////////////////////////////
846
bsalomonafa95e22015-10-12 10:39:46 -0700847GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams& params) {
reedb5d32632015-09-29 13:36:50 -0700848 SkGrStretch stretch;
bsalomonafa95e22015-10-12 10:39:46 -0700849 get_stretch(*ctx->caps(), this->width(), this->height(), params, &stretch);
reedb5d32632015-09-29 13:36:50 -0700850
851 if (SkGrStretch::kNone_Type == stretch.fType) {
852 return this->onRefUnstretchedTexture(ctx);
853 }
854
855 GrUniqueKey stretchedKey;
856 if (this->onMakeStretchedKey(stretch, &stretchedKey)) {
857 GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
858 if (result) {
859 return result;
860 }
861 }
862
863 GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch);
864 if (!result) {
865 return nullptr;
866 }
867
868 if (stretchedKey.isValid()) {
869 ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result);
870 this->onNotifyStretchCached(stretchedKey);
871 }
872 return result;
873}
874
875GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) {
876 if (this->width() < ctx->caps()->minTextureSize() ||
877 this->height() < ctx->caps()->minTextureSize())
878 {
879 // we can't trust our ability to use HW to perform the stretch, so we request
880 // a raster instead, and perform the stretch on the CPU.
881 SkBitmap bitmap;
882 if (!this->onGetROBitmap(&bitmap)) {
883 return nullptr;
884 }
885 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
886 return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey());
887 } else {
888 SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx));
889 if (!unstretched) {
890 return nullptr;
891 }
892 return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey());
893 }
894}