blob: 09c83b929a8c575b1a2b9e15116c76abc4968c5c [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
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "SkGr.h"
egdaniel378092f2014-12-03 10:40:13 -08009
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070011#include "GrDrawContext.h"
egdaniel378092f2014-12-03 10:40:13 -080012#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070013#include "GrYUVProvider.h"
14
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000015#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000016#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070017#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070018#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080019#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070020#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000021#include "SkMessageBus.h"
22#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080023#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070024#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080025#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070026#include "effects/GrBicubicEffect.h"
krajcevskif461a8f2014-06-19 14:14:06 -070027#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080028#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070029#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070030
krajcevski8c111f72014-06-02 13:51:34 -070031#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070032# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070033# include "etc1.h"
34#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000035
36/* Fill out buffer with the compressed format Ganesh expects from a colortable
37 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000038
39 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000040 we could detect that the colortable.count is <= 16, and then repack the
41 indices as nibbles to save RAM, but it would take more time (i.e. a lot
42 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
45 as the colortable.count says it is.
46 */
bsalomone79a2da2014-10-24 12:42:51 -070047static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070048 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000049
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000050 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000051 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000052 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000053 return;
54 }
55
56 SkColorTable* ctable = bitmap.getColorTable();
57 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000058
reed@google.com7111d462014-03-25 16:20:24 +000059 const int count = ctable->count();
60
61 SkDstPixelInfo dstPI;
62 dstPI.fColorType = kRGBA_8888_SkColorType;
63 dstPI.fAlphaType = kPremul_SkAlphaType;
64 dstPI.fPixels = buffer;
65 dstPI.fRowBytes = count * sizeof(SkPMColor);
66
67 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000068 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000069 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080070 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000071 srcPI.fRowBytes = count * sizeof(SkPMColor);
72
73 srcPI.convertPixelsTo(&dstPI, count, 1);
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070076 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000077
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000078 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000079 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
80 } else {
81 // need to trim off the extra bytes per row
82 size_t width = bitmap.width();
83 size_t rowBytes = bitmap.rowBytes();
84 const char* src = (const char*)bitmap.getPixels();
85 for (int y = 0; y < bitmap.height(); y++) {
86 memcpy(dst, src, width);
87 src += rowBytes;
88 dst += width;
89 }
90 }
91}
92
93////////////////////////////////////////////////////////////////////////////////
94
bsalomonc59a1df2015-06-01 07:13:42 -070095struct Stretch {
96 enum Type {
97 kNone_Type,
98 kBilerp_Type,
99 kNearest_Type
100 } fType;
101 int fWidth;
102 int fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800103};
104
bsalomonc59a1df2015-06-01 07:13:42 -0700105static void get_stretch(const GrContext* ctx, int width, int height,
106 const GrTextureParams* params, Stretch* stretch) {
107 stretch->fType = Stretch::kNone_Type;
108 bool doStretch = false;
109 if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
110 (!SkIsPow2(width) || !SkIsPow2(height))) {
111 doStretch = true;
bsalomonb4d40ef2015-07-15 10:12:16 -0700112 stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
113 stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
114 } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
bsalomonc59a1df2015-06-01 07:13:42 -0700115 // The small texture issues appear to be with tiling. Hence it seems ok to scale them
116 // up using the GPU. If issues persist we may need to CPU-stretch.
117 doStretch = true;
118 stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
119 stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
bsalomon37f9a262015-02-02 13:00:10 -0800120 }
bsalomonc59a1df2015-06-01 07:13:42 -0700121 if (doStretch) {
bsalomon0513c832015-06-01 10:22:48 -0700122 if (params) {
123 switch(params->filterMode()) {
124 case GrTextureParams::kNone_FilterMode:
125 stretch->fType = Stretch::kNearest_Type;
126 break;
127 case GrTextureParams::kBilerp_FilterMode:
128 case GrTextureParams::kMipMap_FilterMode:
129 stretch->fType = Stretch::kBilerp_Type;
130 break;
131 }
132 } else {
133 stretch->fType = Stretch::kBilerp_Type;
bsalomonc59a1df2015-06-01 07:13:42 -0700134 }
135 } else {
136 stretch->fWidth = -1;
137 stretch->fHeight = -1;
138 stretch->fType = Stretch::kNone_Type;
139 }
bsalomon37f9a262015-02-02 13:00:10 -0800140}
141
bsalomonc59a1df2015-06-01 07:13:42 -0700142static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800143 GrUniqueKey* stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700144 if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
145 uint32_t width = SkToU16(stretch.fWidth);
146 uint32_t height = SkToU16(stretch.fHeight);
bsalomon8718aaf2015-02-19 07:24:21 -0800147 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
reed8f343722015-08-13 13:32:39 -0700148 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
bsalomonc59a1df2015-06-01 07:13:42 -0700149 builder[0] = stretch.fType;
150 builder[1] = width | (height << 16);
bsalomon37f9a262015-02-02 13:00:10 -0800151 builder.finish();
152 return true;
153 }
bsalomon23e619c2015-02-06 11:54:28 -0800154 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800155 return false;
156}
157
reed85d91782015-09-10 14:33:38 -0700158static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) {
159 SkASSERT(SkIsU16(subset.width()));
160 SkASSERT(SkIsU16(subset.height()));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000161
bsalomon8718aaf2015-02-19 07:24:21 -0800162 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
163 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700164 builder[0] = imageID;
reed85d91782015-09-10 14:33:38 -0700165 builder[1] = subset.x();
166 builder[2] = subset.y();
167 builder[3] = subset.width() | (subset.height() << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800168}
bsalomon37f9a262015-02-02 13:00:10 -0800169
reed85d91782015-09-10 14:33:38 -0700170void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset,
reed8f343722015-08-13 13:32:39 -0700171 const GrCaps& caps, SkImageUsageType usage) {
172 const Stretch::Type stretches[] = {
173 Stretch::kNone_Type, // kUntiled_SkImageUsageType
174 Stretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
175 Stretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
176 };
177
reed85d91782015-09-10 14:33:38 -0700178 const bool isPow2 = SkIsPow2(subset.width()) && SkIsPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700179 const bool needToStretch = !isPow2 &&
180 usage != kUntiled_SkImageUsageType &&
181 !caps.npotTextureTileSupport();
182
183 if (needToStretch) {
184 GrUniqueKey tmpKey;
reed85d91782015-09-10 14:33:38 -0700185 make_unstretched_key(&tmpKey, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700186
187 Stretch stretch;
188 stretch.fType = stretches[usage];
reed85d91782015-09-10 14:33:38 -0700189 stretch.fWidth = SkNextPow2(subset.width());
190 stretch.fHeight = SkNextPow2(subset.height());
reed8f343722015-08-13 13:32:39 -0700191 if (!make_stretched_key(tmpKey, stretch, key)) {
192 goto UNSTRETCHED;
193 }
194 } else {
195 UNSTRETCHED:
reed85d91782015-09-10 14:33:38 -0700196 make_unstretched_key(key, imageID, subset);
reed8f343722015-08-13 13:32:39 -0700197 }
198}
199
reed85d91782015-09-10 14:33:38 -0700200static void make_image_keys(uint32_t imageID, const SkIRect& subset, const Stretch& stretch,
201 GrUniqueKey* key, GrUniqueKey* stretchedKey) {
202 make_unstretched_key(key, imageID, subset);
bsalomonc59a1df2015-06-01 07:13:42 -0700203 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800204 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800205 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000206}
207
reed3322a812015-09-16 10:09:24 -0700208GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
209 GrSurfaceDesc desc;
210 desc.fFlags = kNone_GrSurfaceFlags;
211 desc.fWidth = info.width();
212 desc.fHeight = info.height();
213 desc.fConfig = SkImageInfo2GrPixelConfig(info);
214 desc.fSampleCnt = 0;
215 return desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000216}
217
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000218namespace {
219
220// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800221class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000222public:
bsalomon8718aaf2015-02-19 07:24:21 -0800223 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000224private:
bsalomon8718aaf2015-02-19 07:24:21 -0800225 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000226
mtklein36352bf2015-03-25 18:17:31 -0700227 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800228 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000229 }
230};
231
232} // namespace
233
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000234
reed43fe6182015-09-08 08:37:36 -0700235GrTexture* GrCreateTextureForPixels(GrContext* ctx,
236 const GrUniqueKey& optionalKey,
237 GrSurfaceDesc desc,
238 SkPixelRef* pixelRefForInvalidationNotification,
239 const void* pixels,
240 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700241 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800242 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700243 if (pixelRefForInvalidationNotification) {
244 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
245 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
246 }
bsalomond309e7a2015-04-30 14:18:54 -0700247 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700248 }
249 return result;
250}
251
bsalomonc59a1df2015-06-01 07:13:42 -0700252// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
253// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
254// filtering and the size to stretch the texture to.
255GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
256 SkPixelRef* pixelRef,
257 const GrUniqueKey& optionalKey) {
258 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800259
260 GrContext* context = inputTexture->getContext();
261 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700262 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800263
264 // Either it's a cache miss or the original wasn't cached to begin with.
265 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800266 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700267 rtDesc.fWidth = stretch.fWidth;
268 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800269 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
270
271 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
272 // fail.
bsalomon76228632015-05-29 08:02:10 -0700273 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800274 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700275 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800276 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700277 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800278 rtDesc.fConfig = kSkia8888_GrPixelConfig;
279 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700280 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800281 }
282 } else if (kRGB_GrColorComponentFlags ==
283 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700284 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800285 rtDesc.fConfig = kSkia8888_GrPixelConfig;
286 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700287 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800288 }
289 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700290 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800291 }
292 }
293
reed43fe6182015-09-08 08:37:36 -0700294 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
295 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800296 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800298 }
299 GrPaint paint;
300
301 // If filtering is not desired then we want to ensure all texels in the resampled image are
302 // copies of texels from the original.
303 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700304 Stretch::kBilerp_Type == stretch.fType ?
305 GrTextureParams::kBilerp_FilterMode :
306 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800307 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
308
309 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
310 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
311
robertphillipsc9a37062015-09-01 08:34:28 -0700312 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700313 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700314 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700315 }
316
317 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
318 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800319
reed43fe6182015-09-08 08:37:36 -0700320 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800321}
322
reed43fe6182015-09-08 08:37:36 -0700323GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
324 int expectedW, int expectedH,
325 const void** outStartOfDataToUpload) {
326 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700327#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700328 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
329 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700330 }
331
reed43fe6182015-09-08 08:37:36 -0700332 const uint8_t* bytes = data->bytes();
333 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700334 // Does the data match the dimensions of the bitmap? If not,
335 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700336 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
337 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
338 {
339 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700340 }
341
reed43fe6182015-09-08 08:37:36 -0700342 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
343 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700344 } else if (SkKTXFile::is_ktx(bytes)) {
345 SkKTXFile ktx(data);
346
347 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700348 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700349 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700350 }
351
352 // Does the data match the dimensions of the bitmap? If not,
353 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700354 if (ktx.width() != expectedW || ktx.height() != expectedH) {
355 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800356 }
krajcevski99ffe242014-06-03 13:04:35 -0700357
reed43fe6182015-09-08 08:37:36 -0700358 *outStartOfDataToUpload = ktx.pixelData();
359 return kETC1_GrPixelConfig;
360 }
361#endif
362 return kUnknown_GrPixelConfig;
363}
364
365static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
366 const SkBitmap &bm, GrSurfaceDesc desc) {
367 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
368 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700369 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700370 }
371
reed43fe6182015-09-08 08:37:36 -0700372 const void* startOfTexData;
373 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
374 &startOfTexData);
375 if (kUnknown_GrPixelConfig == desc.fConfig) {
376 return nullptr;
377 }
378
379 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700380}
reed43fe6182015-09-08 08:37:36 -0700381
382/*
383 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
384 * be gone from SkPixelRef, and we can remove this subclass entirely.
385 */
386class PixelRef_GrYUVProvider : public GrYUVProvider {
387 SkPixelRef* fPR;
388
389public:
390 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
391
392 uint32_t onGetID() override { return fPR->getGenerationID(); }
393 bool onGetYUVSizes(SkISize sizes[3]) override {
394 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
395 }
396 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
397 SkYUVColorSpace* space) override {
398 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
399 }
400};
krajcevski9c0e6292014-06-02 07:38:14 -0700401
bsalomon8718aaf2015-02-19 07:24:21 -0800402static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700403 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700404 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700405 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700406 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800407 (pixelRef->info().width() != bm.info().width()) ||
408 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700409 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700410 }
411
sugoiba18f912015-02-04 10:53:03 -0800412 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700413 PixelRef_GrYUVProvider provider(pixelRef);
414 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
415 if (!texture) {
416 return nullptr;
417 }
418
sugoiba18f912015-02-04 10:53:03 -0800419 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700420 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
421 pixelRef->addGenIDChangeListener(listener);
422 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800423 }
reed43fe6182015-09-08 08:37:36 -0700424 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700425}
426
bsalomon37f9a262015-02-02 13:00:10 -0800427static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
428 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800429 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700430 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
431 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700432 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700433 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000434 SkBitmap tmpBitmap;
435
436 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437
reed3322a812015-09-16 10:09:24 -0700438 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
bsalomon76228632015-05-29 08:02:10 -0700439 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440
reed0689d7b2014-06-14 05:30:20 -0700441 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700442 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700443 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
444 bitmap->width(), bitmap->height());
445 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700446 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
448 // our compressed data will be trimmed, so pass width() for its
449 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700450 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
451 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000453 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 // now bitmap points to our temp, which has been promoted to 32bits
455 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000456 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 }
reed43fe6182015-09-08 08:37:36 -0700458 } else if (!bitmap->readyToDraw()) {
459 // If the bitmap had compressed data and was then uncompressed, it'll still return
460 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
461 // the bitmap has available pixels, then they might not be what the decompressed
462 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800463 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700464 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700465 return texture;
466 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomond2a6f4e2015-02-04 10:55:54 -0800469 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
470 if (texture) {
471 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700472 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800473
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000474 SkAutoLockPixels alp(*bitmap);
475 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700476 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000477 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000478
reed43fe6182015-09-08 08:37:36 -0700479 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
480 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800481}
482
bsalomonb4d40ef2015-07-15 10:12:16 -0700483static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
484 SkBitmap stretched;
485 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
486 SkCanvas canvas(stretched);
487 SkPaint paint;
488 switch (stretch.fType) {
489 case Stretch::kNearest_Type:
490 paint.setFilterQuality(kNone_SkFilterQuality);
491 break;
492 case Stretch::kBilerp_Type:
493 paint.setFilterQuality(kLow_SkFilterQuality);
494 break;
495 case Stretch::kNone_Type:
496 SkDEBUGFAIL("Shouldn't get here.");
497 break;
498 }
499 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700500 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700501 return stretched;
502}
503
bsalomon37f9a262015-02-02 13:00:10 -0800504static GrTexture* create_bitmap_texture(GrContext* ctx,
505 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700506 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800507 const GrUniqueKey& unstretchedKey,
508 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700509 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800510 SkAutoTUnref<GrTexture> unstretched;
511 // Check if we have the unstretched version in the cache, if not create it.
512 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700513 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800514 }
515 if (!unstretched) {
516 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
517 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700518 // We might not have been able to create a unstrecthed texture because it is smaller
519 // than the min texture size. In that case do cpu stretching.
520 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
521 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800522 }
523 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700524 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800525 }
bsalomon37f9a262015-02-02 13:00:10 -0800526 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
reed85d91782015-09-10 14:33:38 -0700529bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset,
530 GrTexture* nativeTexture, const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700531 Stretch stretch;
reed85d91782015-09-10 14:33:38 -0700532 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800533
reed85d91782015-09-10 14:33:38 -0700534 // Handle the case where the bitmap/image is explicitly texture backed.
535 if (nativeTexture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700536 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800537 return true;
538 }
reed85d91782015-09-10 14:33:38 -0700539 const GrUniqueKey& key = nativeTexture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800540 if (!key.isValid()) {
541 return false;
542 }
bsalomon8718aaf2015-02-19 07:24:21 -0800543 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800544 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700545 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800546 }
547
bsalomon8718aaf2015-02-19 07:24:21 -0800548 GrUniqueKey key, stretchedKey;
reed85d91782015-09-10 14:33:38 -0700549 make_image_keys(imageID, subset, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700550 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700551 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000552}
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
reed85d91782015-09-10 14:33:38 -0700554bool GrIsBitmapInCache(const GrContext* ctx, const SkBitmap& bitmap,
555 const GrTextureParams* params) {
556 if (bitmap.isVolatile()) {
557 return false; // we don't cache volatile bitmaps.
558 }
559 return GrIsImageInCache(ctx, bitmap.getGenerationID(), bitmap.getSubset(), bitmap.getTexture(),
560 params);
561}
562
bsalomonbcf0a522014-10-08 08:40:09 -0700563GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
564 const SkBitmap& bitmap,
565 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000566
bsalomonc59a1df2015-06-01 07:13:42 -0700567 Stretch stretch;
568 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800569
570 GrTexture* result = bitmap.getTexture();
571 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700572 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800573 return SkRef(result);
574 }
bsalomon8718aaf2015-02-19 07:24:21 -0800575 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800576 // Don't create a key for the resized version if the bmp is volatile.
577 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800578 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800579 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800580 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700581 GrTexture* stretched =
582 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800583 if (stretched) {
584 return stretched;
585 }
586 }
587 }
bsalomonc59a1df2015-06-01 07:13:42 -0700588 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800589 }
590
bsalomon8718aaf2015-02-19 07:24:21 -0800591 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000592
bsalomon37f9a262015-02-02 13:00:10 -0800593 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000594 // If the bitmap isn't changing try to find a cached copy first.
reed85d91782015-09-10 14:33:38 -0700595 make_image_keys(bitmap.getGenerationID(), bitmap.getSubset(), stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000596
bsalomond309e7a2015-04-30 14:18:54 -0700597 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
598 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800599 if (result) {
600 return result;
601 }
602 }
bsalomone137db82015-01-31 20:10:56 -0800603
bsalomon37f9a262015-02-02 13:00:10 -0800604 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
605 if (result) {
606 return result;
607 }
bsalomone137db82015-01-31 20:10:56 -0800608
joshualitt5f5a8d72015-02-25 14:09:45 -0800609 SkErrorInternals::SetError( kInternalError_SkError,
610 "---- failed to create texture for cache [%d %d]\n",
611 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800612
halcanary96fcdcc2015-08-27 07:41:13 -0700613 return nullptr;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000614}
reed8f343722015-08-13 13:32:39 -0700615
616// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
617// into a wrapper that contains the inverse of these tables.
618GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
619 const SkBitmap& bitmap,
620 SkImageUsageType usage) {
621 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
622 // tell us the specifics about filter level or specific tiling.
623
624 const SkShader::TileMode tiles[] = {
625 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
626 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
627 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
628 };
629
630 const GrTextureParams::FilterMode filters[] = {
631 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
632 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
633 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
634 };
635
636 GrTextureParams params(tiles[usage], filters[usage]);
637 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
638}
639
rileya@google.com24f3ad12012-07-18 21:47:40 +0000640///////////////////////////////////////////////////////////////////////////////
641
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000642// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
643// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800644GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000645 switch (ct) {
646 case kUnknown_SkColorType:
647 return kUnknown_GrPixelConfig;
648 case kAlpha_8_SkColorType:
649 return kAlpha_8_GrPixelConfig;
650 case kRGB_565_SkColorType:
651 return kRGB_565_GrPixelConfig;
652 case kARGB_4444_SkColorType:
653 return kRGBA_4444_GrPixelConfig;
654 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700655 //if (kSRGB_SkColorProfileType == pt) {
656 // return kSRGBA_8888_GrPixelConfig;
657 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000658 return kRGBA_8888_GrPixelConfig;
659 case kBGRA_8888_SkColorType:
660 return kBGRA_8888_GrPixelConfig;
661 case kIndex_8_SkColorType:
662 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700663 case kGray_8_SkColorType:
664 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000665 }
666 SkASSERT(0); // shouldn't get here
667 return kUnknown_GrPixelConfig;
668}
669
jvanverthfa1e8a72014-12-22 08:31:49 -0800670bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
671 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000672 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800673 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000674 switch (config) {
675 case kAlpha_8_GrPixelConfig:
676 ct = kAlpha_8_SkColorType;
677 break;
678 case kIndex_8_GrPixelConfig:
679 ct = kIndex_8_SkColorType;
680 break;
681 case kRGB_565_GrPixelConfig:
682 ct = kRGB_565_SkColorType;
683 break;
684 case kRGBA_4444_GrPixelConfig:
685 ct = kARGB_4444_SkColorType;
686 break;
687 case kRGBA_8888_GrPixelConfig:
688 ct = kRGBA_8888_SkColorType;
689 break;
690 case kBGRA_8888_GrPixelConfig:
691 ct = kBGRA_8888_SkColorType;
692 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800693 case kSRGBA_8888_GrPixelConfig:
694 ct = kRGBA_8888_SkColorType;
695 pt = kSRGB_SkColorProfileType;
696 break;
reed@google.combf790232013-12-13 19:45:58 +0000697 default:
698 return false;
699 }
700 if (ctOut) {
701 *ctOut = ct;
702 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800703 if (ptOut) {
704 *ptOut = pt;
705 }
reed@google.combf790232013-12-13 19:45:58 +0000706 return true;
707}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000708
709///////////////////////////////////////////////////////////////////////////////
710
bsalomonaca31fe2015-09-22 11:38:46 -0700711bool SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor paintColor,
712 bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000713
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000714 grPaint->setAntiAlias(skPaint.isAntiAlias());
715
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000716 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700717 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800718 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000719 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700720 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800721 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000722 }
egdaniel378092f2014-12-03 10:40:13 -0800723 SkASSERT(xpFactory);
724 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800725
dandov9de5b512014-06-10 14:38:28 -0700726 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700727 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000728
729 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700730 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000731 // if the source color is a constant then apply the filter here once rather than per pixel
732 // in a shader.
733 if (constantColor) {
734 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
735 grPaint->setColor(SkColor2GrColor(filtered));
736 } else {
bsalomonae4738f2015-09-15 15:33:27 -0700737 SkTDArray<const GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700738 // return false if failed?
joshualitt9cc17752015-07-09 06:28:14 -0700739 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
joshualitt2cff1762015-07-08 07:58:18 -0700740 &array)) {
reedcff10b22015-03-03 06:41:45 -0800741 for (int i = 0; i < array.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -0700742 grPaint->addColorFragmentProcessor(array[i]);
reedcff10b22015-03-03 06:41:45 -0800743 array[i]->unref();
744 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000745 }
746 }
747 }
krajcevskif461a8f2014-06-19 14:14:06 -0700748
749#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700750 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700751 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700752 }
753#endif
bsalomonbed83a62015-04-15 14:18:34 -0700754 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000755}
756
bsalomonaca31fe2015-09-22 11:38:46 -0700757bool SkPaint2GrPaint(GrContext* context,const SkPaint& skPaint, const SkMatrix& viewM,
758 bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000759 SkShader* shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700760 if (nullptr == shader) {
bsalomonaca31fe2015-09-22 11:38:46 -0700761 return SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()),
bsalomonbed83a62015-04-15 14:18:34 -0700762 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000763 }
764
bsalomon83d081a2014-07-08 09:56:10 -0700765 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700766
bsalomonc21b09e2015-08-28 18:46:56 -0700767 const GrFragmentProcessor* fp = shader->asFragmentProcessor(context, viewM, NULL,
768 skPaint.getFilterQuality(), grPaint->getProcessorDataManager());
769 if (!fp) {
770 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000771 }
bsalomonc21b09e2015-08-28 18:46:56 -0700772 grPaint->addColorFragmentProcessor(fp)->unref();
773 constantColor = false;
krajcevskif461a8f2014-06-19 14:14:06 -0700774
joshualittb0a8a372014-09-23 09:50:21 -0700775 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700776 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
bsalomonaca31fe2015-09-22 11:38:46 -0700777 return SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000778}
reed8b26b992015-05-07 15:36:17 -0700779
780SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
781#ifdef SK_DEBUG
782 const GrSurfaceDesc& desc = tex->desc();
783 SkASSERT(w <= desc.fWidth);
784 SkASSERT(h <= desc.fHeight);
785#endif
786 const GrPixelConfig config = tex->config();
787 SkColorType ct;
788 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700789 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700790 ct = kUnknown_SkColorType;
791 }
792 return SkImageInfo::Make(w, h, ct, at);
793}
794
795
796void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
797 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
798 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700799 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700800}
joshualitt9bc39542015-08-12 12:57:54 -0700801
802GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
803 const SkMatrix& viewM,
804 const SkMatrix& localM,
805 bool* doBicubic) {
806 *doBicubic = false;
807 GrTextureParams::FilterMode textureFilterMode;
808 switch (paintFilterQuality) {
809 case kNone_SkFilterQuality:
810 textureFilterMode = GrTextureParams::kNone_FilterMode;
811 break;
812 case kLow_SkFilterQuality:
813 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
814 break;
815 case kMedium_SkFilterQuality: {
816 SkMatrix matrix;
817 matrix.setConcat(viewM, localM);
818 if (matrix.getMinScale() < SK_Scalar1) {
819 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
820 } else {
821 // Don't trigger MIP level generation unnecessarily.
822 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
823 }
824 break;
825 }
826 case kHigh_SkFilterQuality: {
827 SkMatrix matrix;
828 matrix.setConcat(viewM, localM);
829 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
830 break;
831 }
832 default:
833 SkErrorInternals::SetError( kInvalidPaint_SkError,
834 "Sorry, I don't understand the filtering "
835 "mode you asked for. Falling back to "
836 "MIPMaps.");
837 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
838 break;
839
840 }
841 return textureFilterMode;
842}