blob: a1821c32c3085d998dfe831c9133b261cd53e83a [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
reed8f343722015-08-13 13:32:39 -0700158static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID,
159 U16CPU width, U16CPU height, SkIPoint origin) {
160 SkASSERT((uint16_t)width == width);
161 SkASSERT((uint16_t)height == height);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000162
bsalomon8718aaf2015-02-19 07:24:21 -0800163 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
164 GrUniqueKey::Builder builder(key, kDomain, 4);
reed8f343722015-08-13 13:32:39 -0700165 builder[0] = imageID;
bsalomon24db3b12015-01-23 04:24:04 -0800166 builder[1] = origin.fX;
167 builder[2] = origin.fY;
168 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800169}
bsalomon37f9a262015-02-02 13:00:10 -0800170
reed8f343722015-08-13 13:32:39 -0700171void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID,
172 U16CPU width, U16CPU height, SkIPoint origin,
173 const GrCaps& caps, SkImageUsageType usage) {
174 const Stretch::Type stretches[] = {
175 Stretch::kNone_Type, // kUntiled_SkImageUsageType
176 Stretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
177 Stretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
178 };
179
180 const bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
181 const bool needToStretch = !isPow2 &&
182 usage != kUntiled_SkImageUsageType &&
183 !caps.npotTextureTileSupport();
184
185 if (needToStretch) {
186 GrUniqueKey tmpKey;
187 make_unstretched_key(&tmpKey, imageID, width, height, origin);
188
189 Stretch stretch;
190 stretch.fType = stretches[usage];
191 stretch.fWidth = SkNextPow2(width);
192 stretch.fHeight = SkNextPow2(height);
193 if (!make_stretched_key(tmpKey, stretch, key)) {
194 goto UNSTRETCHED;
195 }
196 } else {
197 UNSTRETCHED:
198 make_unstretched_key(key, imageID, width, height, origin);
199 }
200}
201
202static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
203 make_unstretched_key(key, bitmap.getGenerationID(), bitmap.width(), bitmap.height(),
204 bitmap.pixelRefOrigin());
205}
206
bsalomon23e619c2015-02-06 11:54:28 -0800207static void make_bitmap_keys(const SkBitmap& bitmap,
bsalomonc59a1df2015-06-01 07:13:42 -0700208 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800209 GrUniqueKey* key,
210 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800211 make_unstretched_key(bitmap, key);
bsalomonc59a1df2015-06-01 07:13:42 -0700212 if (Stretch::kNone_Type != stretch.fType) {
bsalomon23e619c2015-02-06 11:54:28 -0800213 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800214 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000215}
216
bsalomonf2703d82014-10-28 14:33:06 -0700217static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
218 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000219 desc->fWidth = bitmap.width();
220 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000221 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000222 desc->fSampleCnt = 0;
223}
224
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000225namespace {
226
227// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800228class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000229public:
bsalomon8718aaf2015-02-19 07:24:21 -0800230 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000231private:
bsalomon8718aaf2015-02-19 07:24:21 -0800232 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000233
mtklein36352bf2015-03-25 18:17:31 -0700234 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800235 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000236 }
237};
238
239} // namespace
240
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000241
reed43fe6182015-09-08 08:37:36 -0700242GrTexture* GrCreateTextureForPixels(GrContext* ctx,
243 const GrUniqueKey& optionalKey,
244 GrSurfaceDesc desc,
245 SkPixelRef* pixelRefForInvalidationNotification,
246 const void* pixels,
247 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700248 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800249 if (result && optionalKey.isValid()) {
reed43fe6182015-09-08 08:37:36 -0700250 if (pixelRefForInvalidationNotification) {
251 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
252 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
253 }
bsalomond309e7a2015-04-30 14:18:54 -0700254 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700255 }
256 return result;
257}
258
bsalomonc59a1df2015-06-01 07:13:42 -0700259// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
260// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
261// filtering and the size to stretch the texture to.
262GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
263 SkPixelRef* pixelRef,
264 const GrUniqueKey& optionalKey) {
265 SkASSERT(Stretch::kNone_Type != stretch.fType);
bsalomon37f9a262015-02-02 13:00:10 -0800266
267 GrContext* context = inputTexture->getContext();
268 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700269 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800270
271 // Either it's a cache miss or the original wasn't cached to begin with.
272 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800273 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomonc59a1df2015-06-01 07:13:42 -0700274 rtDesc.fWidth = stretch.fWidth;
275 rtDesc.fHeight = stretch.fHeight;
bsalomon37f9a262015-02-02 13:00:10 -0800276 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
277
278 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
279 // fail.
bsalomon76228632015-05-29 08:02:10 -0700280 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800281 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700282 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800283 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700284 } else 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 if (kRGB_GrColorComponentFlags ==
290 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700291 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800292 rtDesc.fConfig = kSkia8888_GrPixelConfig;
293 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700294 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800295 }
296 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800298 }
299 }
300
reed43fe6182015-09-08 08:37:36 -0700301 SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
302 pixelRef, nullptr,0));
bsalomon23e619c2015-02-06 11:54:28 -0800303 if (!stretched) {
halcanary96fcdcc2015-08-27 07:41:13 -0700304 return nullptr;
bsalomon37f9a262015-02-02 13:00:10 -0800305 }
306 GrPaint paint;
307
308 // If filtering is not desired then we want to ensure all texels in the resampled image are
309 // copies of texels from the original.
310 GrTextureParams params(SkShader::kClamp_TileMode,
bsalomonc59a1df2015-06-01 07:13:42 -0700311 Stretch::kBilerp_Type == stretch.fType ?
312 GrTextureParams::kBilerp_FilterMode :
313 GrTextureParams::kNone_FilterMode);
bsalomon37f9a262015-02-02 13:00:10 -0800314 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
315
316 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
317 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
318
robertphillipsc9a37062015-09-01 08:34:28 -0700319 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700320 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700321 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700322 }
323
324 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
325 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800326
reed43fe6182015-09-08 08:37:36 -0700327 return stretched.detach();
bsalomon37f9a262015-02-02 13:00:10 -0800328}
329
reed43fe6182015-09-08 08:37:36 -0700330GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
331 int expectedW, int expectedH,
332 const void** outStartOfDataToUpload) {
333 *outStartOfDataToUpload = nullptr;
krajcevski8c111f72014-06-02 13:51:34 -0700334#ifndef SK_IGNORE_ETC1_SUPPORT
reed43fe6182015-09-08 08:37:36 -0700335 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
336 return kUnknown_GrPixelConfig;
krajcevski9c0e6292014-06-02 07:38:14 -0700337 }
338
reed43fe6182015-09-08 08:37:36 -0700339 const uint8_t* bytes = data->bytes();
340 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
krajcevski99ffe242014-06-03 13:04:35 -0700341 // Does the data match the dimensions of the bitmap? If not,
342 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700343 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
344 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
345 {
346 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700347 }
348
reed43fe6182015-09-08 08:37:36 -0700349 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
350 return kETC1_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700351 } else if (SkKTXFile::is_ktx(bytes)) {
352 SkKTXFile ktx(data);
353
354 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700355 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
reed43fe6182015-09-08 08:37:36 -0700356 return kUnknown_GrPixelConfig;
krajcevski99ffe242014-06-03 13:04:35 -0700357 }
358
359 // Does the data match the dimensions of the bitmap? If not,
360 // then we don't know how to scale the image to match it...
reed43fe6182015-09-08 08:37:36 -0700361 if (ktx.width() != expectedW || ktx.height() != expectedH) {
362 return kUnknown_GrPixelConfig;
mtklein775b8192014-12-02 09:11:25 -0800363 }
krajcevski99ffe242014-06-03 13:04:35 -0700364
reed43fe6182015-09-08 08:37:36 -0700365 *outStartOfDataToUpload = ktx.pixelData();
366 return kETC1_GrPixelConfig;
367 }
368#endif
369 return kUnknown_GrPixelConfig;
370}
371
372static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
373 const SkBitmap &bm, GrSurfaceDesc desc) {
374 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
375 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700376 return nullptr;
krajcevski9c0e6292014-06-02 07:38:14 -0700377 }
378
reed43fe6182015-09-08 08:37:36 -0700379 const void* startOfTexData;
380 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
381 &startOfTexData);
382 if (kUnknown_GrPixelConfig == desc.fConfig) {
383 return nullptr;
384 }
385
386 return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700387}
reed43fe6182015-09-08 08:37:36 -0700388
389/*
390 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
391 * be gone from SkPixelRef, and we can remove this subclass entirely.
392 */
393class PixelRef_GrYUVProvider : public GrYUVProvider {
394 SkPixelRef* fPR;
395
396public:
397 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
398
399 uint32_t onGetID() override { return fPR->getGenerationID(); }
400 bool onGetYUVSizes(SkISize sizes[3]) override {
401 return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
402 }
403 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
404 SkYUVColorSpace* space) override {
405 return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
406 }
407};
krajcevski9c0e6292014-06-02 07:38:14 -0700408
bsalomon8718aaf2015-02-19 07:24:21 -0800409static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700410 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700411 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700412 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700413 if ((nullptr == pixelRef) ||
sugoi692135f2015-01-19 10:10:27 -0800414 (pixelRef->info().width() != bm.info().width()) ||
415 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700416 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700417 }
418
sugoiba18f912015-02-04 10:53:03 -0800419 const bool useCache = optionalKey.isValid();
reed43fe6182015-09-08 08:37:36 -0700420 PixelRef_GrYUVProvider provider(pixelRef);
421 GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
422 if (!texture) {
423 return nullptr;
424 }
425
sugoiba18f912015-02-04 10:53:03 -0800426 if (useCache) {
reed43fe6182015-09-08 08:37:36 -0700427 BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
428 pixelRef->addGenIDChangeListener(listener);
429 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
sugoiba18f912015-02-04 10:53:03 -0800430 }
reed43fe6182015-09-08 08:37:36 -0700431 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700432}
433
bsalomon37f9a262015-02-02 13:00:10 -0800434static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
435 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800436 const GrUniqueKey& optionalKey) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700437 if (origBitmap.width() < ctx->caps()->minTextureSize() ||
438 origBitmap.height() < ctx->caps()->minTextureSize()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700439 return nullptr;
bsalomonb4d40ef2015-07-15 10:12:16 -0700440 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 SkBitmap tmpBitmap;
442
443 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444
bsalomonf2703d82014-10-28 14:33:06 -0700445 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000446 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700447 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448
reed0689d7b2014-06-14 05:30:20 -0700449 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700450 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700451 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
452 bitmap->width(), bitmap->height());
453 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700454 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
456 // our compressed data will be trimmed, so pass width() for its
457 // "rowBytes", since they are the same now.
reed43fe6182015-09-08 08:37:36 -0700458 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
459 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000461 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 // now bitmap points to our temp, which has been promoted to 32bits
463 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000464 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 }
reed43fe6182015-09-08 08:37:36 -0700466 } else if (!bitmap->readyToDraw()) {
467 // If the bitmap had compressed data and was then uncompressed, it'll still return
468 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
469 // the bitmap has available pixels, then they might not be what the decompressed
470 // data is.
bsalomon37f9a262015-02-02 13:00:10 -0800471 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700472 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700473 return texture;
474 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomond2a6f4e2015-02-04 10:55:54 -0800477 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
478 if (texture) {
479 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700480 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800481
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000482 SkAutoLockPixels alp(*bitmap);
483 if (!bitmap->readyToDraw()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700484 return nullptr;
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000485 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000486
reed43fe6182015-09-08 08:37:36 -0700487 return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
488 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800489}
490
bsalomonb4d40ef2015-07-15 10:12:16 -0700491static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
492 SkBitmap stretched;
493 stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
494 SkCanvas canvas(stretched);
495 SkPaint paint;
496 switch (stretch.fType) {
497 case Stretch::kNearest_Type:
498 paint.setFilterQuality(kNone_SkFilterQuality);
499 break;
500 case Stretch::kBilerp_Type:
501 paint.setFilterQuality(kLow_SkFilterQuality);
502 break;
503 case Stretch::kNone_Type:
504 SkDEBUGFAIL("Shouldn't get here.");
505 break;
506 }
507 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
reed84984ef2015-07-17 07:09:43 -0700508 canvas.drawBitmapRect(bmp, dstRect, &paint);
bsalomonb4d40ef2015-07-15 10:12:16 -0700509 return stretched;
510}
511
bsalomon37f9a262015-02-02 13:00:10 -0800512static GrTexture* create_bitmap_texture(GrContext* ctx,
513 const SkBitmap& bmp,
bsalomonc59a1df2015-06-01 07:13:42 -0700514 const Stretch& stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800515 const GrUniqueKey& unstretchedKey,
516 const GrUniqueKey& stretchedKey) {
bsalomonc59a1df2015-06-01 07:13:42 -0700517 if (Stretch::kNone_Type != stretch.fType) {
bsalomon37f9a262015-02-02 13:00:10 -0800518 SkAutoTUnref<GrTexture> unstretched;
519 // Check if we have the unstretched version in the cache, if not create it.
520 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700521 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800522 }
523 if (!unstretched) {
524 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
525 if (!unstretched) {
bsalomonb4d40ef2015-07-15 10:12:16 -0700526 // We might not have been able to create a unstrecthed texture because it is smaller
527 // than the min texture size. In that case do cpu stretching.
528 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
529 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800530 }
531 }
bsalomonb4d40ef2015-07-15 10:12:16 -0700532 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800533 }
bsalomon37f9a262015-02-02 13:00:10 -0800534 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
reed@google.comac10a2d2010-12-22 21:39:39 +0000535}
536
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000537bool GrIsBitmapInCache(const GrContext* ctx,
538 const SkBitmap& bitmap,
539 const GrTextureParams* params) {
bsalomonc59a1df2015-06-01 07:13:42 -0700540 Stretch stretch;
541 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800542
543 // Handle the case where the bitmap is explicitly texture backed.
544 GrTexture* texture = bitmap.getTexture();
545 if (texture) {
bsalomonc59a1df2015-06-01 07:13:42 -0700546 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800547 return true;
548 }
549 // No keys for volatile bitmaps.
550 if (bitmap.isVolatile()) {
551 return false;
552 }
bsalomon8718aaf2015-02-19 07:24:21 -0800553 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800554 if (!key.isValid()) {
555 return false;
556 }
bsalomon8718aaf2015-02-19 07:24:21 -0800557 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800558 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700559 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800560 }
561
bsalomon37f9a262015-02-02 13:00:10 -0800562 // We don't cache volatile bitmaps
563 if (bitmap.isVolatile()) {
564 return false;
565 }
566
bsalomon8718aaf2015-02-19 07:24:21 -0800567 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800568 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700569 return ctx->textureProvider()->existsTextureWithUniqueKey(
bsalomonc59a1df2015-06-01 07:13:42 -0700570 (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000571}
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
bsalomonbcf0a522014-10-08 08:40:09 -0700573GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
574 const SkBitmap& bitmap,
575 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000576
bsalomonc59a1df2015-06-01 07:13:42 -0700577 Stretch stretch;
578 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
bsalomon88425562015-02-04 09:12:46 -0800579
580 GrTexture* result = bitmap.getTexture();
581 if (result) {
bsalomonc59a1df2015-06-01 07:13:42 -0700582 if (Stretch::kNone_Type == stretch.fType) {
bsalomon88425562015-02-04 09:12:46 -0800583 return SkRef(result);
584 }
bsalomon8718aaf2015-02-19 07:24:21 -0800585 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800586 // Don't create a key for the resized version if the bmp is volatile.
587 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800588 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800589 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800590 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700591 GrTexture* stretched =
592 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800593 if (stretched) {
594 return stretched;
595 }
596 }
597 }
bsalomonc59a1df2015-06-01 07:13:42 -0700598 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800599 }
600
bsalomon8718aaf2015-02-19 07:24:21 -0800601 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000602
bsalomon37f9a262015-02-02 13:00:10 -0800603 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000604 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800605 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000606
bsalomond309e7a2015-04-30 14:18:54 -0700607 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
608 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800609 if (result) {
610 return result;
611 }
612 }
bsalomone137db82015-01-31 20:10:56 -0800613
bsalomon37f9a262015-02-02 13:00:10 -0800614 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
615 if (result) {
616 return result;
617 }
bsalomone137db82015-01-31 20:10:56 -0800618
joshualitt5f5a8d72015-02-25 14:09:45 -0800619 SkErrorInternals::SetError( kInternalError_SkError,
620 "---- failed to create texture for cache [%d %d]\n",
621 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800622
halcanary96fcdcc2015-08-27 07:41:13 -0700623 return nullptr;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000624}
reed8f343722015-08-13 13:32:39 -0700625
626// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
627// into a wrapper that contains the inverse of these tables.
628GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
629 const SkBitmap& bitmap,
630 SkImageUsageType usage) {
631 // Just need a params that will trigger the correct cache key / etc, since the usage doesn't
632 // tell us the specifics about filter level or specific tiling.
633
634 const SkShader::TileMode tiles[] = {
635 SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
636 SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
637 SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
638 };
639
640 const GrTextureParams::FilterMode filters[] = {
641 GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
642 GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
643 GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
644 };
645
646 GrTextureParams params(tiles[usage], filters[usage]);
647 return GrRefCachedBitmapTexture(ctx, bitmap, &params);
648}
649
rileya@google.com24f3ad12012-07-18 21:47:40 +0000650///////////////////////////////////////////////////////////////////////////////
651
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000652// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
653// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800654GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000655 switch (ct) {
656 case kUnknown_SkColorType:
657 return kUnknown_GrPixelConfig;
658 case kAlpha_8_SkColorType:
659 return kAlpha_8_GrPixelConfig;
660 case kRGB_565_SkColorType:
661 return kRGB_565_GrPixelConfig;
662 case kARGB_4444_SkColorType:
663 return kRGBA_4444_GrPixelConfig;
664 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700665 //if (kSRGB_SkColorProfileType == pt) {
666 // return kSRGBA_8888_GrPixelConfig;
667 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000668 return kRGBA_8888_GrPixelConfig;
669 case kBGRA_8888_SkColorType:
670 return kBGRA_8888_GrPixelConfig;
671 case kIndex_8_SkColorType:
672 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700673 case kGray_8_SkColorType:
674 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000675 }
676 SkASSERT(0); // shouldn't get here
677 return kUnknown_GrPixelConfig;
678}
679
jvanverthfa1e8a72014-12-22 08:31:49 -0800680bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
681 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000682 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800683 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000684 switch (config) {
685 case kAlpha_8_GrPixelConfig:
686 ct = kAlpha_8_SkColorType;
687 break;
688 case kIndex_8_GrPixelConfig:
689 ct = kIndex_8_SkColorType;
690 break;
691 case kRGB_565_GrPixelConfig:
692 ct = kRGB_565_SkColorType;
693 break;
694 case kRGBA_4444_GrPixelConfig:
695 ct = kARGB_4444_SkColorType;
696 break;
697 case kRGBA_8888_GrPixelConfig:
698 ct = kRGBA_8888_SkColorType;
699 break;
700 case kBGRA_8888_GrPixelConfig:
701 ct = kBGRA_8888_SkColorType;
702 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800703 case kSRGBA_8888_GrPixelConfig:
704 ct = kRGBA_8888_SkColorType;
705 pt = kSRGB_SkColorProfileType;
706 break;
reed@google.combf790232013-12-13 19:45:58 +0000707 default:
708 return false;
709 }
710 if (ctOut) {
711 *ctOut = ct;
712 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800713 if (ptOut) {
714 *ptOut = pt;
715 }
reed@google.combf790232013-12-13 19:45:58 +0000716 return true;
717}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000718
719///////////////////////////////////////////////////////////////////////////////
720
bsalomonbed83a62015-04-15 14:18:34 -0700721bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800722 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000723
724 grPaint->setDither(skPaint.isDither());
725 grPaint->setAntiAlias(skPaint.isAntiAlias());
726
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000727 SkXfermode* mode = skPaint.getXfermode();
halcanary96fcdcc2015-08-27 07:41:13 -0700728 GrXPFactory* xpFactory = nullptr;
egdaniel58136162015-01-20 10:19:22 -0800729 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000730 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700731 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800732 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000733 }
egdaniel378092f2014-12-03 10:40:13 -0800734 SkASSERT(xpFactory);
735 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800736
dandov9de5b512014-06-10 14:38:28 -0700737 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700738 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000739
740 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700741 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000742 // if the source color is a constant then apply the filter here once rather than per pixel
743 // in a shader.
744 if (constantColor) {
745 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
746 grPaint->setColor(SkColor2GrColor(filtered));
747 } else {
reedcff10b22015-03-03 06:41:45 -0800748 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700749 // return false if failed?
joshualitt9cc17752015-07-09 06:28:14 -0700750 if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
joshualitt2cff1762015-07-08 07:58:18 -0700751 &array)) {
reedcff10b22015-03-03 06:41:45 -0800752 for (int i = 0; i < array.count(); ++i) {
bsalomonac856c92015-08-27 06:30:17 -0700753 grPaint->addColorFragmentProcessor(array[i]);
reedcff10b22015-03-03 06:41:45 -0800754 array[i]->unref();
755 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000756 }
757 }
758 }
krajcevskif461a8f2014-06-19 14:14:06 -0700759
760#ifndef SK_IGNORE_GPU_DITHER
761 // If the dither flag is set, then we need to see if the underlying context
762 // supports it. If not, then install a dither effect.
bsalomonac856c92015-08-27 06:30:17 -0700763 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
krajcevskif461a8f2014-06-19 14:14:06 -0700764 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800765 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700766
767 // Suspect the dithering flag has no effect on these configs, otherwise
768 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800769 if (GrPixelConfigIs8888(rt->config()) ||
770 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700771 // The dither flag is set and the target is likely
772 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700773 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
774 if (fp.get()) {
bsalomonac856c92015-08-27 06:30:17 -0700775 grPaint->addColorFragmentProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700776 grPaint->setDither(false);
777 }
778 }
779 }
780#endif
bsalomonbed83a62015-04-15 14:18:34 -0700781 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000782}
783
bsalomonbed83a62015-04-15 14:18:34 -0700784bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
785 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000786 SkShader* shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700787 if (nullptr == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700788 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
789 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000790 }
791
bsalomon83d081a2014-07-08 09:56:10 -0700792 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700793
bsalomonc21b09e2015-08-28 18:46:56 -0700794 const GrFragmentProcessor* fp = shader->asFragmentProcessor(context, viewM, NULL,
795 skPaint.getFilterQuality(), grPaint->getProcessorDataManager());
796 if (!fp) {
797 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000798 }
bsalomonc21b09e2015-08-28 18:46:56 -0700799 grPaint->addColorFragmentProcessor(fp)->unref();
800 constantColor = false;
krajcevskif461a8f2014-06-19 14:14:06 -0700801
joshualittb0a8a372014-09-23 09:50:21 -0700802 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700803 // If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
bsalomonbed83a62015-04-15 14:18:34 -0700804 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000805}
reed8b26b992015-05-07 15:36:17 -0700806
807SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
808#ifdef SK_DEBUG
809 const GrSurfaceDesc& desc = tex->desc();
810 SkASSERT(w <= desc.fWidth);
811 SkASSERT(h <= desc.fHeight);
812#endif
813 const GrPixelConfig config = tex->config();
814 SkColorType ct;
815 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700816 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700817 ct = kUnknown_SkColorType;
818 }
819 return SkImageInfo::Make(w, h, ct, at);
820}
821
822
823void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
824 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
825 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700826 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700827}
joshualitt9bc39542015-08-12 12:57:54 -0700828
829GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
830 const SkMatrix& viewM,
831 const SkMatrix& localM,
832 bool* doBicubic) {
833 *doBicubic = false;
834 GrTextureParams::FilterMode textureFilterMode;
835 switch (paintFilterQuality) {
836 case kNone_SkFilterQuality:
837 textureFilterMode = GrTextureParams::kNone_FilterMode;
838 break;
839 case kLow_SkFilterQuality:
840 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
841 break;
842 case kMedium_SkFilterQuality: {
843 SkMatrix matrix;
844 matrix.setConcat(viewM, localM);
845 if (matrix.getMinScale() < SK_Scalar1) {
846 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
847 } else {
848 // Don't trigger MIP level generation unnecessarily.
849 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
850 }
851 break;
852 }
853 case kHigh_SkFilterQuality: {
854 SkMatrix matrix;
855 matrix.setConcat(viewM, localM);
856 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
857 break;
858 }
859 default:
860 SkErrorInternals::SetError( kInvalidPaint_SkError,
861 "Sorry, I don't understand the filtering "
862 "mode you asked for. Falling back to "
863 "MIPMaps.");
864 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
865 break;
866
867 }
868 return textureFilterMode;
869}