blob: 70c81f930a8e54b0482aece09b6bcc44b2a2ea6e [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"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000013#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000014#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070015#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080016#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070017#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000018#include "SkMessageBus.h"
19#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080020#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070021#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080022#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070023#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080024#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070025#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070026
krajcevski8c111f72014-06-02 13:51:34 -070027#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070028# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070029# include "etc1.h"
30#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000031
32/* Fill out buffer with the compressed format Ganesh expects from a colortable
33 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000034
35 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000036 we could detect that the colortable.count is <= 16, and then repack the
37 indices as nibbles to save RAM, but it would take more time (i.e. a lot
38 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000039
reed@google.comac10a2d2010-12-22 21:39:39 +000040 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
41 as the colortable.count says it is.
42 */
bsalomone79a2da2014-10-24 12:42:51 -070043static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070044 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000045
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000046 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000047 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000048 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000049 return;
50 }
51
52 SkColorTable* ctable = bitmap.getColorTable();
53 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000054
reed@google.com7111d462014-03-25 16:20:24 +000055 const int count = ctable->count();
56
57 SkDstPixelInfo dstPI;
58 dstPI.fColorType = kRGBA_8888_SkColorType;
59 dstPI.fAlphaType = kPremul_SkAlphaType;
60 dstPI.fPixels = buffer;
61 dstPI.fRowBytes = count * sizeof(SkPMColor);
62
63 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000064 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000065 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080066 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000067 srcPI.fRowBytes = count * sizeof(SkPMColor);
68
69 srcPI.convertPixelsTo(&dstPI, count, 1);
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070072 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000073
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000074 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
76 } else {
77 // need to trim off the extra bytes per row
78 size_t width = bitmap.width();
79 size_t rowBytes = bitmap.rowBytes();
80 const char* src = (const char*)bitmap.getPixels();
81 for (int y = 0; y < bitmap.height(); y++) {
82 memcpy(dst, src, width);
83 src += rowBytes;
84 dst += width;
85 }
86 }
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
bsalomon37f9a262015-02-02 13:00:10 -080091enum Stretch {
92 kNo_Stretch,
93 kBilerp_Stretch,
94 kNearest_Stretch
95};
96
97static Stretch get_stretch_type(const GrContext* ctx, int width, int height,
98 const GrTextureParams* params) {
99 if (params && params->isTiled()) {
bsalomon76228632015-05-29 08:02:10 -0700100 if (!ctx->caps()->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon37f9a262015-02-02 13:00:10 -0800101 switch(params->filterMode()) {
102 case GrTextureParams::kNone_FilterMode:
103 return kNearest_Stretch;
104 case GrTextureParams::kBilerp_FilterMode:
105 case GrTextureParams::kMipMap_FilterMode:
106 return kBilerp_Stretch;
107 }
108 }
109 }
110 return kNo_Stretch;
111}
112
bsalomon8718aaf2015-02-19 07:24:21 -0800113static bool make_stretched_key(const GrUniqueKey& origKey, Stretch stretch,
114 GrUniqueKey* stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800115 if (origKey.isValid() && kNo_Stretch != stretch) {
bsalomon8718aaf2015-02-19 07:24:21 -0800116 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
117 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 1);
bsalomon37f9a262015-02-02 13:00:10 -0800118 builder[0] = stretch;
119 builder.finish();
120 return true;
121 }
bsalomon23e619c2015-02-06 11:54:28 -0800122 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800123 return false;
124}
125
bsalomon8718aaf2015-02-19 07:24:21 -0800126static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000127 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
128 // are unique.
129 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000130 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800131 uint32_t width = SkToU16(bitmap.width());
132 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133
bsalomon8718aaf2015-02-19 07:24:21 -0800134 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
135 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800136 builder[0] = genID;
137 builder[1] = origin.fX;
138 builder[2] = origin.fY;
139 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800140}
bsalomon37f9a262015-02-02 13:00:10 -0800141
bsalomon23e619c2015-02-06 11:54:28 -0800142static void make_bitmap_keys(const SkBitmap& bitmap,
143 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800144 GrUniqueKey* key,
145 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800146 make_unstretched_key(bitmap, key);
bsalomon37f9a262015-02-02 13:00:10 -0800147 if (kNo_Stretch != stretch) {
bsalomon23e619c2015-02-06 11:54:28 -0800148 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800149 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000150}
151
bsalomonf2703d82014-10-28 14:33:06 -0700152static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
153 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000154 desc->fWidth = bitmap.width();
155 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000156 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000157 desc->fSampleCnt = 0;
158}
159
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000160namespace {
161
162// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800163class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000164public:
bsalomon8718aaf2015-02-19 07:24:21 -0800165 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000166private:
bsalomon8718aaf2015-02-19 07:24:21 -0800167 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000168
mtklein36352bf2015-03-25 18:17:31 -0700169 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800170 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000171 }
172};
173
174} // namespace
175
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000176
bsalomon37f9a262015-02-02 13:00:10 -0800177static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800178 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700179 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800180 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700181 const void* pixels,
182 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700183 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800184 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800185 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
186 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomond309e7a2015-04-30 14:18:54 -0700187 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700188 }
189 return result;
190}
191
bsalomon37f9a262015-02-02 13:00:10 -0800192// creates a new texture that is the input texture scaled up to the next power of two in
193// width or height. If optionalKey is valid it will be set on the new texture. stretch
194// controls whether the scaling is done using nearest or bilerp filtering.
bsalomon23e619c2015-02-06 11:54:28 -0800195GrTexture* stretch_texture_to_next_pot(GrTexture* inputTexture, Stretch stretch,
196 SkPixelRef* pixelRef,
bsalomon8718aaf2015-02-19 07:24:21 -0800197 const GrUniqueKey& optionalKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800198 SkASSERT(kNo_Stretch != stretch);
199
200 GrContext* context = inputTexture->getContext();
201 SkASSERT(context);
bsalomon76228632015-05-29 08:02:10 -0700202 const GrCaps* caps = context->caps();
bsalomon37f9a262015-02-02 13:00:10 -0800203
204 // Either it's a cache miss or the original wasn't cached to begin with.
205 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800206 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomon37f9a262015-02-02 13:00:10 -0800207 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
208 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
209 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
210
211 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
212 // fail.
bsalomon76228632015-05-29 08:02:10 -0700213 if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800214 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
bsalomon76228632015-05-29 08:02:10 -0700215 if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800216 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700217 } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800218 rtDesc.fConfig = kSkia8888_GrPixelConfig;
219 } else {
220 return NULL;
221 }
222 } else if (kRGB_GrColorComponentFlags ==
223 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
bsalomon76228632015-05-29 08:02:10 -0700224 if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
bsalomon37f9a262015-02-02 13:00:10 -0800225 rtDesc.fConfig = kSkia8888_GrPixelConfig;
226 } else {
227 return NULL;
228 }
229 } else {
230 return NULL;
231 }
232 }
233
bsalomon23e619c2015-02-06 11:54:28 -0800234 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800235
bsalomon23e619c2015-02-06 11:54:28 -0800236 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800237 return NULL;
238 }
239 GrPaint paint;
240
241 // If filtering is not desired then we want to ensure all texels in the resampled image are
242 // copies of texels from the original.
243 GrTextureParams params(SkShader::kClamp_TileMode,
244 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
245 GrTextureParams::kNone_FilterMode);
246 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
247
248 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
249 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
250
robertphillipsea461502015-05-26 11:38:03 -0700251 GrDrawContext* drawContext = context->drawContext();
252 if (!drawContext) {
253 return NULL;
254 }
255
256 drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
257 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800258
bsalomon23e619c2015-02-06 11:54:28 -0800259 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800260}
261
krajcevski8c111f72014-06-02 13:51:34 -0700262#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800263static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700264 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700265 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700266
267 // Is this even encoded data?
268 if (NULL == data) {
269 return NULL;
270 }
271
272 // Is this a valid PKM encoded data?
273 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700274 if (etc1_pkm_is_valid(bytes)) {
275 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
276 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
277
278 // Does the data match the dimensions of the bitmap? If not,
279 // then we don't know how to scale the image to match it...
280 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
281 encodedHeight != static_cast<uint32_t>(bm.height())) {
282 return NULL;
283 }
284
285 // Everything seems good... skip ahead to the data.
286 bytes += ETC_PKM_HEADER_SIZE;
287 desc.fConfig = kETC1_GrPixelConfig;
288 } else if (SkKTXFile::is_ktx(bytes)) {
289 SkKTXFile ktx(data);
290
291 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700292 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700293 return NULL;
294 }
295
296 // Does the data match the dimensions of the bitmap? If not,
297 // then we don't know how to scale the image to match it...
298 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
299 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800300 }
krajcevski99ffe242014-06-03 13:04:35 -0700301
302 bytes = ktx.pixelData();
303 desc.fConfig = kETC1_GrPixelConfig;
304 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700305 return NULL;
306 }
307
bsalomon23e619c2015-02-06 11:54:28 -0800308 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700309}
krajcevski8c111f72014-06-02 13:51:34 -0700310#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700311
bsalomon8718aaf2015-02-19 07:24:21 -0800312static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700313 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700314 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700315 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800316 if ((NULL == pixelRef) ||
317 (pixelRef->info().width() != bm.info().width()) ||
318 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700319 return NULL;
320 }
321
sugoiba18f912015-02-04 10:53:03 -0800322 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800323 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800324 SkAutoTUnref<SkCachedData> cachedData;
325 SkAutoMalloc storage;
326 if (useCache) {
327 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
328 }
sugoi692135f2015-01-19 10:10:27 -0800329
sugoi518d83d2014-07-21 11:37:39 -0700330 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800331 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800332 planes[0] = (void*)cachedData->data();
333 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
334 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
335 } else {
336 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
337 // rounded up to JPEG block size and be larger than the image's width and height.
338 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
339 return NULL;
340 }
sugoi518d83d2014-07-21 11:37:39 -0700341
sugoi692135f2015-01-19 10:10:27 -0800342 // Allocate the memory for YUV
343 size_t totalSize(0);
344 for (int i = 0; i < 3; ++i) {
345 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
346 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
347 totalSize += yuvInfo.fSizeInMemory[i];
348 }
sugoiba18f912015-02-04 10:53:03 -0800349 if (useCache) {
350 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
351 planes[0] = cachedData->writable_data();
352 } else {
353 storage.reset(totalSize);
354 planes[0] = storage.get();
355 }
sugoi692135f2015-01-19 10:10:27 -0800356 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
357 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700358
sugoi692135f2015-01-19 10:10:27 -0800359 // Get the YUV planes and update plane sizes to actual image size
360 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
361 &yuvInfo.fColorSpace)) {
362 return NULL;
363 }
364
sugoiba18f912015-02-04 10:53:03 -0800365 if (useCache) {
366 // Decoding is done, cache the resulting YUV planes
367 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
368 }
sugoi518d83d2014-07-21 11:37:39 -0700369 }
370
bsalomonf2703d82014-10-28 14:33:06 -0700371 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700372 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700373 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700374 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800375 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
376 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
sugoi4ab3dbb2015-03-06 05:16:52 -0800377 bool needsExactTexture =
378 (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
379 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bsalomond309e7a2015-04-30 14:18:54 -0700380 yuvTextures[i].reset(ctx->textureProvider()->refScratchTexture(yuvDesc,
381 needsExactTexture ? GrTextureProvider::kExact_ScratchTexMatch :
382 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700383 if (!yuvTextures[i] ||
384 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800385 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700386 return NULL;
387 }
388 }
389
bsalomonf2703d82014-10-28 14:33:06 -0700390 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800391 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700392
bsalomon23e619c2015-02-06 11:54:28 -0800393 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800394 if (!result) {
395 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700396 }
397
bsalomon37f9a262015-02-02 13:00:10 -0800398 GrRenderTarget* renderTarget = result->asRenderTarget();
399 SkASSERT(renderTarget);
400
401 SkAutoTUnref<GrFragmentProcessor>
402 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
sugoi4ccce7e2015-02-13 13:57:09 -0800403 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800404 GrPaint paint;
405 paint.addColorProcessor(yuvToRgbProcessor);
406 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
407 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800408
robertphillipsea461502015-05-26 11:38:03 -0700409 GrDrawContext* drawContext = ctx->drawContext();
410 if (!drawContext) {
411 return NULL;
412 }
413
414 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800415
sugoi518d83d2014-07-21 11:37:39 -0700416 return result;
417}
418
bsalomon37f9a262015-02-02 13:00:10 -0800419static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
420 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800421 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 SkBitmap tmpBitmap;
423
424 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000425
bsalomonf2703d82014-10-28 14:33:06 -0700426 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000427 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon76228632015-05-29 08:02:10 -0700428 const GrCaps* caps = ctx->caps();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429
reed0689d7b2014-06-14 05:30:20 -0700430 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700431 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700432 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
433 bitmap->width(), bitmap->height());
434 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700435 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000436
437 // our compressed data will be trimmed, so pass width() for its
438 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800439 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
440 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000442 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 // now bitmap points to our temp, which has been promoted to 32bits
444 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000445 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 }
krajcevski309e8692014-06-02 08:02:45 -0700447 }
krajcevski9c0e6292014-06-02 07:38:14 -0700448
449 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700450#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800451 // Make sure that the underlying device supports ETC1 textures before we go ahead
452 // and check the data.
bsalomon76228632015-05-29 08:02:10 -0700453 else if (caps->isConfigTexturable(kETC1_GrPixelConfig)
bsalomond2a6f4e2015-02-04 10:55:54 -0800454 // If the bitmap had compressed data and was then uncompressed, it'll still return
455 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
456 // the bitmap has available pixels, then they might not be what the decompressed
457 // data is.
458 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800459 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700460 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700461 return texture;
462 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 }
krajcevski8c111f72014-06-02 13:51:34 -0700464#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomond2a6f4e2015-02-04 10:55:54 -0800466 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
467 if (texture) {
468 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700469 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800470
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000471 SkAutoLockPixels alp(*bitmap);
472 if (!bitmap->readyToDraw()) {
473 return NULL;
474 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000475
bsalomon23e619c2015-02-06 11:54:28 -0800476 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
477 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800478}
479
480static GrTexture* create_bitmap_texture(GrContext* ctx,
481 const SkBitmap& bmp,
482 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800483 const GrUniqueKey& unstretchedKey,
484 const GrUniqueKey& stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800485 if (kNo_Stretch != stretch) {
486 SkAutoTUnref<GrTexture> unstretched;
487 // Check if we have the unstretched version in the cache, if not create it.
488 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700489 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800490 }
491 if (!unstretched) {
492 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
493 if (!unstretched) {
494 return NULL;
495 }
496 }
bsalomon23e619c2015-02-06 11:54:28 -0800497 GrTexture* stretched = stretch_texture_to_next_pot(unstretched, stretch, bmp.pixelRef(),
498 stretchedKey);
499 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800500 }
501
502 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504}
505
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000506bool GrIsBitmapInCache(const GrContext* ctx,
507 const SkBitmap& bitmap,
508 const GrTextureParams* params) {
bsalomon88425562015-02-04 09:12:46 -0800509 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
510
511 // Handle the case where the bitmap is explicitly texture backed.
512 GrTexture* texture = bitmap.getTexture();
513 if (texture) {
514 if (kNo_Stretch == stretch) {
515 return true;
516 }
517 // No keys for volatile bitmaps.
518 if (bitmap.isVolatile()) {
519 return false;
520 }
bsalomon8718aaf2015-02-19 07:24:21 -0800521 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800522 if (!key.isValid()) {
523 return false;
524 }
bsalomon8718aaf2015-02-19 07:24:21 -0800525 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800526 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700527 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800528 }
529
bsalomon37f9a262015-02-02 13:00:10 -0800530 // We don't cache volatile bitmaps
531 if (bitmap.isVolatile()) {
532 return false;
533 }
534
bsalomon8718aaf2015-02-19 07:24:21 -0800535 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800536 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700537 return ctx->textureProvider()->existsTextureWithUniqueKey(
538 (kNo_Stretch == stretch) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000539}
reed@google.comac10a2d2010-12-22 21:39:39 +0000540
bsalomonbcf0a522014-10-08 08:40:09 -0700541GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
542 const SkBitmap& bitmap,
543 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000544
bsalomon37f9a262015-02-02 13:00:10 -0800545 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
bsalomon88425562015-02-04 09:12:46 -0800546
547 GrTexture* result = bitmap.getTexture();
548 if (result) {
549 if (kNo_Stretch == stretch) {
550 return SkRef(result);
551 }
bsalomon8718aaf2015-02-19 07:24:21 -0800552 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800553 // Don't create a key for the resized version if the bmp is volatile.
554 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800555 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800556 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800557 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700558 GrTexture* stretched =
559 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800560 if (stretched) {
561 return stretched;
562 }
563 }
564 }
bsalomon23e619c2015-02-06 11:54:28 -0800565 return stretch_texture_to_next_pot(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800566 }
567
bsalomon8718aaf2015-02-19 07:24:21 -0800568 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000569
bsalomon37f9a262015-02-02 13:00:10 -0800570 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000571 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800572 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000573
bsalomond309e7a2015-04-30 14:18:54 -0700574 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
575 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800576 if (result) {
577 return result;
578 }
579 }
bsalomone137db82015-01-31 20:10:56 -0800580
bsalomon37f9a262015-02-02 13:00:10 -0800581 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
582 if (result) {
583 return result;
584 }
bsalomone137db82015-01-31 20:10:56 -0800585
joshualitt5f5a8d72015-02-25 14:09:45 -0800586 SkErrorInternals::SetError( kInternalError_SkError,
587 "---- failed to create texture for cache [%d %d]\n",
588 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800589
590 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000591}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000592///////////////////////////////////////////////////////////////////////////////
593
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000594// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
595// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800596GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000597 switch (ct) {
598 case kUnknown_SkColorType:
599 return kUnknown_GrPixelConfig;
600 case kAlpha_8_SkColorType:
601 return kAlpha_8_GrPixelConfig;
602 case kRGB_565_SkColorType:
603 return kRGB_565_GrPixelConfig;
604 case kARGB_4444_SkColorType:
605 return kRGBA_4444_GrPixelConfig;
606 case kRGBA_8888_SkColorType:
jvanverth99babf22015-05-22 06:06:40 -0700607 //if (kSRGB_SkColorProfileType == pt) {
608 // return kSRGBA_8888_GrPixelConfig;
609 //}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000610 return kRGBA_8888_GrPixelConfig;
611 case kBGRA_8888_SkColorType:
612 return kBGRA_8888_GrPixelConfig;
613 case kIndex_8_SkColorType:
614 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700615 case kGray_8_SkColorType:
616 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000617 }
618 SkASSERT(0); // shouldn't get here
619 return kUnknown_GrPixelConfig;
620}
621
jvanverthfa1e8a72014-12-22 08:31:49 -0800622bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
623 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000624 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800625 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000626 switch (config) {
627 case kAlpha_8_GrPixelConfig:
628 ct = kAlpha_8_SkColorType;
629 break;
630 case kIndex_8_GrPixelConfig:
631 ct = kIndex_8_SkColorType;
632 break;
633 case kRGB_565_GrPixelConfig:
634 ct = kRGB_565_SkColorType;
635 break;
636 case kRGBA_4444_GrPixelConfig:
637 ct = kARGB_4444_SkColorType;
638 break;
639 case kRGBA_8888_GrPixelConfig:
640 ct = kRGBA_8888_SkColorType;
641 break;
642 case kBGRA_8888_GrPixelConfig:
643 ct = kBGRA_8888_SkColorType;
644 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800645 case kSRGBA_8888_GrPixelConfig:
646 ct = kRGBA_8888_SkColorType;
647 pt = kSRGB_SkColorProfileType;
648 break;
reed@google.combf790232013-12-13 19:45:58 +0000649 default:
650 return false;
651 }
652 if (ctOut) {
653 *ctOut = ct;
654 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800655 if (ptOut) {
656 *ptOut = pt;
657 }
reed@google.combf790232013-12-13 19:45:58 +0000658 return true;
659}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000660
661///////////////////////////////////////////////////////////////////////////////
662
bsalomonbed83a62015-04-15 14:18:34 -0700663bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800664 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000665
666 grPaint->setDither(skPaint.isDither());
667 grPaint->setAntiAlias(skPaint.isAntiAlias());
668
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000669 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800670 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800671 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000672 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700673 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800674 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000675 }
egdaniel378092f2014-12-03 10:40:13 -0800676 SkASSERT(xpFactory);
677 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800678
dandov9de5b512014-06-10 14:38:28 -0700679 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700680 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000681
682 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700683 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000684 // if the source color is a constant then apply the filter here once rather than per pixel
685 // in a shader.
686 if (constantColor) {
687 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
688 grPaint->setColor(SkColor2GrColor(filtered));
689 } else {
reedcff10b22015-03-03 06:41:45 -0800690 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700691 // return false if failed?
reedcff10b22015-03-03 06:41:45 -0800692 if (colorFilter->asFragmentProcessors(context, &array)) {
693 for (int i = 0; i < array.count(); ++i) {
694 grPaint->addColorProcessor(array[i]);
695 array[i]->unref();
696 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000697 }
698 }
699 }
krajcevskif461a8f2014-06-19 14:14:06 -0700700
701#ifndef SK_IGNORE_GPU_DITHER
702 // If the dither flag is set, then we need to see if the underlying context
703 // supports it. If not, then install a dither effect.
704 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
705 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800706 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700707
708 // Suspect the dithering flag has no effect on these configs, otherwise
709 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800710 if (GrPixelConfigIs8888(rt->config()) ||
711 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700712 // The dither flag is set and the target is likely
713 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700714 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
715 if (fp.get()) {
716 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700717 grPaint->setDither(false);
718 }
719 }
720 }
721#endif
bsalomonbed83a62015-04-15 14:18:34 -0700722 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000723}
724
bsalomonbed83a62015-04-15 14:18:34 -0700725bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
726 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000727 SkShader* shader = skPaint.getShader();
728 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700729 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
730 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000731 }
732
bsalomon83d081a2014-07-08 09:56:10 -0700733 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700734
735 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700736 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700737 // want them messing around with the context.
738 {
bsalomon83d081a2014-07-08 09:56:10 -0700739 // Allow the shader to modify paintColor and also create an effect to be installed as
740 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700741 GrFragmentProcessor* fp = NULL;
bsalomonbed83a62015-04-15 14:18:34 -0700742 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) {
743 return false;
744 }
745 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700746 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700747 constantColor = false;
748 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000749 }
krajcevskif461a8f2014-06-19 14:14:06 -0700750
joshualittb0a8a372014-09-23 09:50:21 -0700751 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700752 // 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 -0700753 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000754}
reed8b26b992015-05-07 15:36:17 -0700755
756SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
757#ifdef SK_DEBUG
758 const GrSurfaceDesc& desc = tex->desc();
759 SkASSERT(w <= desc.fWidth);
760 SkASSERT(h <= desc.fHeight);
761#endif
762 const GrPixelConfig config = tex->config();
763 SkColorType ct;
764 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
765 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
766 ct = kUnknown_SkColorType;
767 }
768 return SkImageInfo::Make(w, h, ct, at);
769}
770
771
772void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
773 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
774 dst->setInfo(info);
775 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
776}