blob: 2fc0996d84cf2d42bb6c913c3ebe622807eaac75 [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
egdaniel378092f2014-12-03 10:40:13 -080010#include "GrXferProcessor.h"
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000011#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000012#include "SkConfig8888.h"
krajcevski9c0e6292014-06-02 07:38:14 -070013#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080014#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070015#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000016#include "SkMessageBus.h"
17#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080018#include "SkResourceCache.h"
krajcevski40a1e112014-08-05 14:13:36 -070019#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080020#include "SkYUVPlanesCache.h"
krajcevskif461a8f2014-06-19 14:14:06 -070021#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080022#include "effects/GrPorterDuffXferProcessor.h"
sugoi518d83d2014-07-21 11:37:39 -070023#include "effects/GrYUVtoRGBEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070024
krajcevski8c111f72014-06-02 13:51:34 -070025#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070026# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070027# include "etc1.h"
28#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30/* Fill out buffer with the compressed format Ganesh expects from a colortable
31 based bitmap. [palette (colortable) + indices].
bsalomon@google.com5782d712011-01-21 21:03:59 +000032
33 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000034 we could detect that the colortable.count is <= 16, and then repack the
35 indices as nibbles to save RAM, but it would take more time (i.e. a lot
36 slower than memcpy), so skipping that for now.
bsalomon@google.com5782d712011-01-21 21:03:59 +000037
reed@google.comac10a2d2010-12-22 21:39:39 +000038 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
39 as the colortable.count says it is.
40 */
bsalomone79a2da2014-10-24 12:42:51 -070041static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070042 SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +000043
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +000044 SkAutoLockPixels alp(bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +000045 if (!bitmap.readyToDraw()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000046 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000047 return;
48 }
49
50 SkColorTable* ctable = bitmap.getColorTable();
51 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +000052
reed@google.com7111d462014-03-25 16:20:24 +000053 const int count = ctable->count();
54
55 SkDstPixelInfo dstPI;
56 dstPI.fColorType = kRGBA_8888_SkColorType;
57 dstPI.fAlphaType = kPremul_SkAlphaType;
58 dstPI.fPixels = buffer;
59 dstPI.fRowBytes = count * sizeof(SkPMColor);
60
61 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000062 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +000063 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -080064 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +000065 srcPI.fRowBytes = count * sizeof(SkPMColor);
66
67 srcPI.convertPixelsTo(&dstPI, count, 1);
68
reed@google.comac10a2d2010-12-22 21:39:39 +000069 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -070070 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +000071
scroggo@google.com0ba4bf42013-02-25 16:02:36 +000072 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
74 } else {
75 // need to trim off the extra bytes per row
76 size_t width = bitmap.width();
77 size_t rowBytes = bitmap.rowBytes();
78 const char* src = (const char*)bitmap.getPixels();
79 for (int y = 0; y < bitmap.height(); y++) {
80 memcpy(dst, src, width);
81 src += rowBytes;
82 dst += width;
83 }
84 }
85}
86
87////////////////////////////////////////////////////////////////////////////////
88
bsalomon37f9a262015-02-02 13:00:10 -080089enum Stretch {
90 kNo_Stretch,
91 kBilerp_Stretch,
92 kNearest_Stretch
93};
94
95static Stretch get_stretch_type(const GrContext* ctx, int width, int height,
96 const GrTextureParams* params) {
97 if (params && params->isTiled()) {
bsalomond2a6f4e2015-02-04 10:55:54 -080098 if (!ctx->npotTextureTileSupport() && (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon37f9a262015-02-02 13:00:10 -080099 switch(params->filterMode()) {
100 case GrTextureParams::kNone_FilterMode:
101 return kNearest_Stretch;
102 case GrTextureParams::kBilerp_FilterMode:
103 case GrTextureParams::kMipMap_FilterMode:
104 return kBilerp_Stretch;
105 }
106 }
107 }
108 return kNo_Stretch;
109}
110
bsalomon8718aaf2015-02-19 07:24:21 -0800111static bool make_stretched_key(const GrUniqueKey& origKey, Stretch stretch,
112 GrUniqueKey* stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800113 if (origKey.isValid() && kNo_Stretch != stretch) {
bsalomon8718aaf2015-02-19 07:24:21 -0800114 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
115 GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 1);
bsalomon37f9a262015-02-02 13:00:10 -0800116 builder[0] = stretch;
117 builder.finish();
118 return true;
119 }
bsalomon23e619c2015-02-06 11:54:28 -0800120 SkASSERT(!stretchedKey->isValid());
bsalomon37f9a262015-02-02 13:00:10 -0800121 return false;
122}
123
bsalomon8718aaf2015-02-19 07:24:21 -0800124static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000125 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
126 // are unique.
127 uint32_t genID = bitmap.getGenerationID();
reed@google.com672588b2014-01-08 15:42:01 +0000128 SkIPoint origin = bitmap.pixelRefOrigin();
bsalomon24db3b12015-01-23 04:24:04 -0800129 uint32_t width = SkToU16(bitmap.width());
130 uint32_t height = SkToU16(bitmap.height());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000131
bsalomon8718aaf2015-02-19 07:24:21 -0800132 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
133 GrUniqueKey::Builder builder(key, kDomain, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800134 builder[0] = genID;
135 builder[1] = origin.fX;
136 builder[2] = origin.fY;
137 builder[3] = width | (height << 16);
bsalomon23e619c2015-02-06 11:54:28 -0800138}
bsalomon37f9a262015-02-02 13:00:10 -0800139
bsalomon23e619c2015-02-06 11:54:28 -0800140static void make_bitmap_keys(const SkBitmap& bitmap,
141 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800142 GrUniqueKey* key,
143 GrUniqueKey* stretchedKey) {
bsalomon23e619c2015-02-06 11:54:28 -0800144 make_unstretched_key(bitmap, key);
bsalomon37f9a262015-02-02 13:00:10 -0800145 if (kNo_Stretch != stretch) {
bsalomon23e619c2015-02-06 11:54:28 -0800146 make_stretched_key(*key, stretch, stretchedKey);
bsalomon37f9a262015-02-02 13:00:10 -0800147 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000148}
149
bsalomonf2703d82014-10-28 14:33:06 -0700150static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
151 desc->fFlags = kNone_GrSurfaceFlags;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000152 desc->fWidth = bitmap.width();
153 desc->fHeight = bitmap.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000154 desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155 desc->fSampleCnt = 0;
156}
157
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000158namespace {
159
160// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
bsalomon23e619c2015-02-06 11:54:28 -0800161class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000162public:
bsalomon8718aaf2015-02-19 07:24:21 -0800163 explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000164private:
bsalomon8718aaf2015-02-19 07:24:21 -0800165 GrUniqueKeyInvalidatedMessage fMsg;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000166
mtklein36352bf2015-03-25 18:17:31 -0700167 void onChange() override {
bsalomon8718aaf2015-02-19 07:24:21 -0800168 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000169 }
170};
171
172} // namespace
173
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000174
bsalomon37f9a262015-02-02 13:00:10 -0800175static GrTexture* create_texture_for_bmp(GrContext* ctx,
bsalomon8718aaf2015-02-19 07:24:21 -0800176 const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700177 GrSurfaceDesc desc,
bsalomon23e619c2015-02-06 11:54:28 -0800178 SkPixelRef* pixelRefForInvalidationNotification,
sugoi0249ec22014-09-09 08:12:34 -0700179 const void* pixels,
180 size_t rowBytes) {
bsalomond309e7a2015-04-30 14:18:54 -0700181 GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
bsalomond0423582015-02-06 08:49:24 -0800182 if (result && optionalKey.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800183 BitmapInvalidator* listener = SkNEW_ARGS(BitmapInvalidator, (optionalKey));
184 pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
bsalomond309e7a2015-04-30 14:18:54 -0700185 ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
sugoi0249ec22014-09-09 08:12:34 -0700186 }
187 return result;
188}
189
bsalomon37f9a262015-02-02 13:00:10 -0800190// creates a new texture that is the input texture scaled up to the next power of two in
191// width or height. If optionalKey is valid it will be set on the new texture. stretch
192// controls whether the scaling is done using nearest or bilerp filtering.
bsalomon23e619c2015-02-06 11:54:28 -0800193GrTexture* stretch_texture_to_next_pot(GrTexture* inputTexture, Stretch stretch,
194 SkPixelRef* pixelRef,
bsalomon8718aaf2015-02-19 07:24:21 -0800195 const GrUniqueKey& optionalKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800196 SkASSERT(kNo_Stretch != stretch);
197
198 GrContext* context = inputTexture->getContext();
199 SkASSERT(context);
200
201 // Either it's a cache miss or the original wasn't cached to begin with.
202 GrSurfaceDesc rtDesc = inputTexture->desc();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800203 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
bsalomon37f9a262015-02-02 13:00:10 -0800204 rtDesc.fWidth = GrNextPow2(rtDesc.fWidth);
205 rtDesc.fHeight = GrNextPow2(rtDesc.fHeight);
206 rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
207
208 // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
209 // fail.
210 if (!context->isConfigRenderable(rtDesc.fConfig, false)) {
211 if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
212 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
213 rtDesc.fConfig = kAlpha_8_GrPixelConfig;
214 } else if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
215 rtDesc.fConfig = kSkia8888_GrPixelConfig;
216 } else {
217 return NULL;
218 }
219 } else if (kRGB_GrColorComponentFlags ==
220 (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
221 if (context->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
222 rtDesc.fConfig = kSkia8888_GrPixelConfig;
223 } else {
224 return NULL;
225 }
226 } else {
227 return NULL;
228 }
229 }
230
bsalomon23e619c2015-02-06 11:54:28 -0800231 GrTexture* stretched = create_texture_for_bmp(context, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800232
bsalomon23e619c2015-02-06 11:54:28 -0800233 if (!stretched) {
bsalomon37f9a262015-02-02 13:00:10 -0800234 return NULL;
235 }
236 GrPaint paint;
237
238 // If filtering is not desired then we want to ensure all texels in the resampled image are
239 // copies of texels from the original.
240 GrTextureParams params(SkShader::kClamp_TileMode,
241 kBilerp_Stretch == stretch ? GrTextureParams::kBilerp_FilterMode :
242 GrTextureParams::kNone_FilterMode);
243 paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
244
245 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
246 SkRect localRect = SkRect::MakeWH(1.f, 1.f);
247
joshualitt570d2f82015-02-25 13:19:48 -0800248 context->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
249 SkMatrix::I(), rect, localRect);
bsalomon37f9a262015-02-02 13:00:10 -0800250
bsalomon23e619c2015-02-06 11:54:28 -0800251 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800252}
253
krajcevski8c111f72014-06-02 13:51:34 -0700254#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomon8718aaf2015-02-19 07:24:21 -0800255static GrTexture *load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700256 const SkBitmap &bm, GrSurfaceDesc desc) {
krajcevski99ffe242014-06-03 13:04:35 -0700257 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
krajcevski9c0e6292014-06-02 07:38:14 -0700258
259 // Is this even encoded data?
260 if (NULL == data) {
261 return NULL;
262 }
263
264 // Is this a valid PKM encoded data?
265 const uint8_t *bytes = data->bytes();
krajcevski99ffe242014-06-03 13:04:35 -0700266 if (etc1_pkm_is_valid(bytes)) {
267 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
268 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
269
270 // Does the data match the dimensions of the bitmap? If not,
271 // then we don't know how to scale the image to match it...
272 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
273 encodedHeight != static_cast<uint32_t>(bm.height())) {
274 return NULL;
275 }
276
277 // Everything seems good... skip ahead to the data.
278 bytes += ETC_PKM_HEADER_SIZE;
279 desc.fConfig = kETC1_GrPixelConfig;
280 } else if (SkKTXFile::is_ktx(bytes)) {
281 SkKTXFile ktx(data);
282
283 // Is it actually an ETC1 texture?
krajcevski40a1e112014-08-05 14:13:36 -0700284 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
krajcevski99ffe242014-06-03 13:04:35 -0700285 return NULL;
286 }
287
288 // Does the data match the dimensions of the bitmap? If not,
289 // then we don't know how to scale the image to match it...
290 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
291 return NULL;
mtklein775b8192014-12-02 09:11:25 -0800292 }
krajcevski99ffe242014-06-03 13:04:35 -0700293
294 bytes = ktx.pixelData();
295 desc.fConfig = kETC1_GrPixelConfig;
296 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700297 return NULL;
298 }
299
bsalomon23e619c2015-02-06 11:54:28 -0800300 return create_texture_for_bmp(ctx, optionalKey, desc, bm.pixelRef(), bytes, 0);
krajcevski9c0e6292014-06-02 07:38:14 -0700301}
krajcevski8c111f72014-06-02 13:51:34 -0700302#endif // SK_IGNORE_ETC1_SUPPORT
krajcevski9c0e6292014-06-02 07:38:14 -0700303
bsalomon8718aaf2015-02-19 07:24:21 -0800304static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
bsalomonf2703d82014-10-28 14:33:06 -0700305 const SkBitmap& bm, const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700306 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700307 SkPixelRef* pixelRef = bm.pixelRef();
sugoi692135f2015-01-19 10:10:27 -0800308 if ((NULL == pixelRef) ||
309 (pixelRef->info().width() != bm.info().width()) ||
310 (pixelRef->info().height() != bm.info().height())) {
sugoi518d83d2014-07-21 11:37:39 -0700311 return NULL;
312 }
313
sugoiba18f912015-02-04 10:53:03 -0800314 const bool useCache = optionalKey.isValid();
sugoi692135f2015-01-19 10:10:27 -0800315 SkYUVPlanesCache::Info yuvInfo;
sugoiba18f912015-02-04 10:53:03 -0800316 SkAutoTUnref<SkCachedData> cachedData;
317 SkAutoMalloc storage;
318 if (useCache) {
319 cachedData.reset(SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo));
320 }
sugoi692135f2015-01-19 10:10:27 -0800321
sugoi518d83d2014-07-21 11:37:39 -0700322 void* planes[3];
sugoiba18f912015-02-04 10:53:03 -0800323 if (cachedData.get()) {
sugoi692135f2015-01-19 10:10:27 -0800324 planes[0] = (void*)cachedData->data();
325 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
326 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
327 } else {
328 // Fetch yuv plane sizes for memory allocation. Here, width and height can be
329 // rounded up to JPEG block size and be larger than the image's width and height.
330 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) {
331 return NULL;
332 }
sugoi518d83d2014-07-21 11:37:39 -0700333
sugoi692135f2015-01-19 10:10:27 -0800334 // Allocate the memory for YUV
335 size_t totalSize(0);
336 for (int i = 0; i < 3; ++i) {
337 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth;
338 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].fHeight;
339 totalSize += yuvInfo.fSizeInMemory[i];
340 }
sugoiba18f912015-02-04 10:53:03 -0800341 if (useCache) {
342 cachedData.reset(SkResourceCache::NewCachedData(totalSize));
343 planes[0] = cachedData->writable_data();
344 } else {
345 storage.reset(totalSize);
346 planes[0] = storage.get();
347 }
sugoi692135f2015-01-19 10:10:27 -0800348 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0];
349 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1];
rileyaabaef862014-09-12 17:45:58 -0700350
sugoi692135f2015-01-19 10:10:27 -0800351 // Get the YUV planes and update plane sizes to actual image size
352 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes,
353 &yuvInfo.fColorSpace)) {
354 return NULL;
355 }
356
sugoiba18f912015-02-04 10:53:03 -0800357 if (useCache) {
358 // Decoding is done, cache the resulting YUV planes
359 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo);
360 }
sugoi518d83d2014-07-21 11:37:39 -0700361 }
362
bsalomonf2703d82014-10-28 14:33:06 -0700363 GrSurfaceDesc yuvDesc;
sugoi518d83d2014-07-21 11:37:39 -0700364 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
bsalomone3059732014-10-14 11:47:22 -0700365 SkAutoTUnref<GrTexture> yuvTextures[3];
sugoi518d83d2014-07-21 11:37:39 -0700366 for (int i = 0; i < 3; ++i) {
sugoi692135f2015-01-19 10:10:27 -0800367 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
368 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
sugoi4ab3dbb2015-03-06 05:16:52 -0800369 bool needsExactTexture =
370 (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
371 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bsalomond309e7a2015-04-30 14:18:54 -0700372 yuvTextures[i].reset(ctx->textureProvider()->refScratchTexture(yuvDesc,
373 needsExactTexture ? GrTextureProvider::kExact_ScratchTexMatch :
374 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700375 if (!yuvTextures[i] ||
376 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
sugoi692135f2015-01-19 10:10:27 -0800377 yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
sugoi518d83d2014-07-21 11:37:39 -0700378 return NULL;
379 }
380 }
381
bsalomonf2703d82014-10-28 14:33:06 -0700382 GrSurfaceDesc rtDesc = desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800383 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
sugoi518d83d2014-07-21 11:37:39 -0700384
bsalomon23e619c2015-02-06 11:54:28 -0800385 GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0);
bsalomon37f9a262015-02-02 13:00:10 -0800386 if (!result) {
387 return NULL;
sugoi518d83d2014-07-21 11:37:39 -0700388 }
389
bsalomon37f9a262015-02-02 13:00:10 -0800390 GrRenderTarget* renderTarget = result->asRenderTarget();
391 SkASSERT(renderTarget);
392
393 SkAutoTUnref<GrFragmentProcessor>
394 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2],
sugoi4ccce7e2015-02-13 13:57:09 -0800395 yuvInfo.fSize, yuvInfo.fColorSpace));
bsalomon37f9a262015-02-02 13:00:10 -0800396 GrPaint paint;
397 paint.addColorProcessor(yuvToRgbProcessor);
398 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth),
399 SkIntToScalar(yuvInfo.fSize[0].fHeight));
joshualitt570d2f82015-02-25 13:19:48 -0800400
401 ctx->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r);
bsalomon37f9a262015-02-02 13:00:10 -0800402
sugoi518d83d2014-07-21 11:37:39 -0700403 return result;
404}
405
bsalomon37f9a262015-02-02 13:00:10 -0800406static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
407 const SkBitmap& origBitmap,
bsalomon8718aaf2015-02-19 07:24:21 -0800408 const GrUniqueKey& optionalKey) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 SkBitmap tmpBitmap;
410
411 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000412
bsalomonf2703d82014-10-28 14:33:06 -0700413 GrSurfaceDesc desc;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000414 generate_bitmap_texture_desc(*bitmap, &desc);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000415
reed0689d7b2014-06-14 05:30:20 -0700416 if (kIndex_8_SkColorType == bitmap->colorType()) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800417 if (ctx->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700418 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
419 bitmap->width(), bitmap->height());
420 SkAutoMalloc storage(imageSize);
bsalomone79a2da2014-10-24 12:42:51 -0700421 build_index8_data(storage.get(), origBitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
423 // our compressed data will be trimmed, so pass width() for its
424 // "rowBytes", since they are the same now.
bsalomon23e619c2015-02-06 11:54:28 -0800425 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
426 storage.get(), bitmap->width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000428 origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 // now bitmap points to our temp, which has been promoted to 32bits
430 bitmap = &tmpBitmap;
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000431 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 }
krajcevski309e8692014-06-02 08:02:45 -0700433 }
krajcevski9c0e6292014-06-02 07:38:14 -0700434
435 // Is this an ETC1 encoded texture?
krajcevski8c111f72014-06-02 13:51:34 -0700436#ifndef SK_IGNORE_ETC1_SUPPORT
bsalomond2a6f4e2015-02-04 10:55:54 -0800437 // Make sure that the underlying device supports ETC1 textures before we go ahead
438 // and check the data.
439 else if (ctx->isConfigTexturable(kETC1_GrPixelConfig)
440 // If the bitmap had compressed data and was then uncompressed, it'll still return
441 // compressed data on 'refEncodedData' and upload it. Probably not good, since if
442 // the bitmap has available pixels, then they might not be what the decompressed
443 // data is.
444 && !(bitmap->readyToDraw())) {
bsalomon37f9a262015-02-02 13:00:10 -0800445 GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
bsalomon49f085d2014-09-05 13:34:00 -0700446 if (texture) {
krajcevski9c0e6292014-06-02 07:38:14 -0700447 return texture;
448 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 }
krajcevski8c111f72014-06-02 13:51:34 -0700450#endif // SK_IGNORE_ETC1_SUPPORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000451
bsalomond2a6f4e2015-02-04 10:55:54 -0800452 GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
453 if (texture) {
454 return texture;
sugoi518d83d2014-07-21 11:37:39 -0700455 }
bsalomond2a6f4e2015-02-04 10:55:54 -0800456
bsalomon@google.com7f4ad5a2013-05-07 19:36:43 +0000457 SkAutoLockPixels alp(*bitmap);
458 if (!bitmap->readyToDraw()) {
459 return NULL;
460 }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000461
bsalomon23e619c2015-02-06 11:54:28 -0800462 return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(),
463 bitmap->getPixels(), bitmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800464}
465
466static GrTexture* create_bitmap_texture(GrContext* ctx,
467 const SkBitmap& bmp,
468 Stretch stretch,
bsalomon8718aaf2015-02-19 07:24:21 -0800469 const GrUniqueKey& unstretchedKey,
470 const GrUniqueKey& stretchedKey) {
bsalomon37f9a262015-02-02 13:00:10 -0800471 if (kNo_Stretch != stretch) {
472 SkAutoTUnref<GrTexture> unstretched;
473 // Check if we have the unstretched version in the cache, if not create it.
474 if (unstretchedKey.isValid()) {
bsalomond309e7a2015-04-30 14:18:54 -0700475 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
bsalomon37f9a262015-02-02 13:00:10 -0800476 }
477 if (!unstretched) {
478 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
479 if (!unstretched) {
480 return NULL;
481 }
482 }
bsalomon23e619c2015-02-06 11:54:28 -0800483 GrTexture* stretched = stretch_texture_to_next_pot(unstretched, stretch, bmp.pixelRef(),
484 stretchedKey);
485 return stretched;
bsalomon37f9a262015-02-02 13:00:10 -0800486 }
487
488 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490}
491
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000492bool GrIsBitmapInCache(const GrContext* ctx,
493 const SkBitmap& bitmap,
494 const GrTextureParams* params) {
bsalomon88425562015-02-04 09:12:46 -0800495 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
496
497 // Handle the case where the bitmap is explicitly texture backed.
498 GrTexture* texture = bitmap.getTexture();
499 if (texture) {
500 if (kNo_Stretch == stretch) {
501 return true;
502 }
503 // No keys for volatile bitmaps.
504 if (bitmap.isVolatile()) {
505 return false;
506 }
bsalomon8718aaf2015-02-19 07:24:21 -0800507 const GrUniqueKey& key = texture->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800508 if (!key.isValid()) {
509 return false;
510 }
bsalomon8718aaf2015-02-19 07:24:21 -0800511 GrUniqueKey stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800512 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700513 return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
bsalomon9ed7f572014-12-19 12:26:37 -0800514 }
515
bsalomon37f9a262015-02-02 13:00:10 -0800516 // We don't cache volatile bitmaps
517 if (bitmap.isVolatile()) {
518 return false;
519 }
520
bsalomon8718aaf2015-02-19 07:24:21 -0800521 GrUniqueKey key, stretchedKey;
bsalomon23e619c2015-02-06 11:54:28 -0800522 make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700523 return ctx->textureProvider()->existsTextureWithUniqueKey(
524 (kNo_Stretch == stretch) ? key : stretchedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000525}
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
bsalomonbcf0a522014-10-08 08:40:09 -0700527GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
528 const SkBitmap& bitmap,
529 const GrTextureParams* params) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000530
bsalomon37f9a262015-02-02 13:00:10 -0800531 Stretch stretch = get_stretch_type(ctx, bitmap.width(), bitmap.height(), params);
bsalomon88425562015-02-04 09:12:46 -0800532
533 GrTexture* result = bitmap.getTexture();
534 if (result) {
535 if (kNo_Stretch == stretch) {
536 return SkRef(result);
537 }
bsalomon8718aaf2015-02-19 07:24:21 -0800538 GrUniqueKey stretchedKey;
bsalomon88425562015-02-04 09:12:46 -0800539 // Don't create a key for the resized version if the bmp is volatile.
540 if (!bitmap.isVolatile()) {
bsalomon8718aaf2015-02-19 07:24:21 -0800541 const GrUniqueKey& key = result->getUniqueKey();
bsalomon88425562015-02-04 09:12:46 -0800542 if (key.isValid()) {
bsalomon23e619c2015-02-06 11:54:28 -0800543 make_stretched_key(key, stretch, &stretchedKey);
bsalomond309e7a2015-04-30 14:18:54 -0700544 GrTexture* stretched =
545 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800546 if (stretched) {
547 return stretched;
548 }
549 }
550 }
bsalomon23e619c2015-02-06 11:54:28 -0800551 return stretch_texture_to_next_pot(result, stretch, bitmap.pixelRef(), stretchedKey);
bsalomon88425562015-02-04 09:12:46 -0800552 }
553
bsalomon8718aaf2015-02-19 07:24:21 -0800554 GrUniqueKey key, resizedKey;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000555
bsalomon37f9a262015-02-02 13:00:10 -0800556 if (!bitmap.isVolatile()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000557 // If the bitmap isn't changing try to find a cached copy first.
bsalomon23e619c2015-02-06 11:54:28 -0800558 make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000559
bsalomond309e7a2015-04-30 14:18:54 -0700560 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
561 resizedKey.isValid() ? resizedKey : key);
bsalomon37f9a262015-02-02 13:00:10 -0800562 if (result) {
563 return result;
564 }
565 }
bsalomone137db82015-01-31 20:10:56 -0800566
bsalomon37f9a262015-02-02 13:00:10 -0800567 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
568 if (result) {
569 return result;
570 }
bsalomone137db82015-01-31 20:10:56 -0800571
joshualitt5f5a8d72015-02-25 14:09:45 -0800572 SkErrorInternals::SetError( kInternalError_SkError,
573 "---- failed to create texture for cache [%d %d]\n",
574 bitmap.width(), bitmap.height());
bsalomon37f9a262015-02-02 13:00:10 -0800575
576 return NULL;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000577}
rileya@google.com24f3ad12012-07-18 21:47:40 +0000578///////////////////////////////////////////////////////////////////////////////
579
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000580// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
581// alpha info, that will be considered.
jvanverthfa1e8a72014-12-22 08:31:49 -0800582GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000583 switch (ct) {
584 case kUnknown_SkColorType:
585 return kUnknown_GrPixelConfig;
586 case kAlpha_8_SkColorType:
587 return kAlpha_8_GrPixelConfig;
588 case kRGB_565_SkColorType:
589 return kRGB_565_GrPixelConfig;
590 case kARGB_4444_SkColorType:
591 return kRGBA_4444_GrPixelConfig;
592 case kRGBA_8888_SkColorType:
jvanverthfe43c402014-12-22 10:29:30 -0800593// if (kSRGB_SkColorProfileType == pt) {
594// return kSRGBA_8888_GrPixelConfig;
595// }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000596 return kRGBA_8888_GrPixelConfig;
597 case kBGRA_8888_SkColorType:
598 return kBGRA_8888_GrPixelConfig;
599 case kIndex_8_SkColorType:
600 return kIndex_8_GrPixelConfig;
reed0c9b1a82015-03-17 17:44:06 -0700601 case kGray_8_SkColorType:
602 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000603 }
604 SkASSERT(0); // shouldn't get here
605 return kUnknown_GrPixelConfig;
606}
607
jvanverthfa1e8a72014-12-22 08:31:49 -0800608bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
609 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000610 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800611 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000612 switch (config) {
613 case kAlpha_8_GrPixelConfig:
614 ct = kAlpha_8_SkColorType;
615 break;
616 case kIndex_8_GrPixelConfig:
617 ct = kIndex_8_SkColorType;
618 break;
619 case kRGB_565_GrPixelConfig:
620 ct = kRGB_565_SkColorType;
621 break;
622 case kRGBA_4444_GrPixelConfig:
623 ct = kARGB_4444_SkColorType;
624 break;
625 case kRGBA_8888_GrPixelConfig:
626 ct = kRGBA_8888_SkColorType;
627 break;
628 case kBGRA_8888_GrPixelConfig:
629 ct = kBGRA_8888_SkColorType;
630 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800631 case kSRGBA_8888_GrPixelConfig:
632 ct = kRGBA_8888_SkColorType;
633 pt = kSRGB_SkColorProfileType;
634 break;
reed@google.combf790232013-12-13 19:45:58 +0000635 default:
636 return false;
637 }
638 if (ctOut) {
639 *ctOut = ct;
640 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800641 if (ptOut) {
642 *ptOut = pt;
643 }
reed@google.combf790232013-12-13 19:45:58 +0000644 return true;
645}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000646
647///////////////////////////////////////////////////////////////////////////////
648
bsalomonbed83a62015-04-15 14:18:34 -0700649bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800650 GrColor paintColor, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000651
652 grPaint->setDither(skPaint.isDither());
653 grPaint->setAntiAlias(skPaint.isAntiAlias());
654
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000655 SkXfermode* mode = skPaint.getXfermode();
egdaniel378092f2014-12-03 10:40:13 -0800656 GrXPFactory* xpFactory = NULL;
egdaniel58136162015-01-20 10:19:22 -0800657 if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000658 // Fall back to src-over
bsalomonbed83a62015-04-15 14:18:34 -0700659 // return false here?
egdanielc016fb82014-12-03 11:41:54 -0800660 xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000661 }
egdaniel378092f2014-12-03 10:40:13 -0800662 SkASSERT(xpFactory);
663 grPaint->setXPFactory(xpFactory)->unref();
mtklein775b8192014-12-02 09:11:25 -0800664
dandov9de5b512014-06-10 14:38:28 -0700665 //set the color of the paint to the one of the parameter
bsalomon83d081a2014-07-08 09:56:10 -0700666 grPaint->setColor(paintColor);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000667
668 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700669 if (colorFilter) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000670 // if the source color is a constant then apply the filter here once rather than per pixel
671 // in a shader.
672 if (constantColor) {
673 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
674 grPaint->setColor(SkColor2GrColor(filtered));
675 } else {
reedcff10b22015-03-03 06:41:45 -0800676 SkTDArray<GrFragmentProcessor*> array;
bsalomonbed83a62015-04-15 14:18:34 -0700677 // return false if failed?
reedcff10b22015-03-03 06:41:45 -0800678 if (colorFilter->asFragmentProcessors(context, &array)) {
679 for (int i = 0; i < array.count(); ++i) {
680 grPaint->addColorProcessor(array[i]);
681 array[i]->unref();
682 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000683 }
684 }
685 }
krajcevskif461a8f2014-06-19 14:14:06 -0700686
687#ifndef SK_IGNORE_GPU_DITHER
688 // If the dither flag is set, then we need to see if the underlying context
689 // supports it. If not, then install a dither effect.
690 if (skPaint.isDither() && grPaint->numColorStages() > 0) {
691 // What are we rendering into?
joshualitt25d9c152015-02-18 12:29:52 -0800692 SkASSERT(rt);
krajcevskif461a8f2014-06-19 14:14:06 -0700693
694 // Suspect the dithering flag has no effect on these configs, otherwise
695 // fall back on setting the appropriate state.
joshualitt25d9c152015-02-18 12:29:52 -0800696 if (GrPixelConfigIs8888(rt->config()) ||
697 GrPixelConfigIs8888(rt->config())) {
krajcevskif461a8f2014-06-19 14:14:06 -0700698 // The dither flag is set and the target is likely
699 // not going to be dithered by the GPU.
joshualittb0a8a372014-09-23 09:50:21 -0700700 SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
701 if (fp.get()) {
702 grPaint->addColorProcessor(fp);
krajcevskif461a8f2014-06-19 14:14:06 -0700703 grPaint->setDither(false);
704 }
705 }
706 }
707#endif
bsalomonbed83a62015-04-15 14:18:34 -0700708 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000709}
710
bsalomonbed83a62015-04-15 14:18:34 -0700711bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
712 const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000713 SkShader* shader = skPaint.getShader();
714 if (NULL == shader) {
bsalomonbed83a62015-04-15 14:18:34 -0700715 return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
716 constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000717 }
718
bsalomon83d081a2014-07-08 09:56:10 -0700719 GrColor paintColor = SkColor2GrColor(skPaint.getColor());
krajcevskif461a8f2014-06-19 14:14:06 -0700720
721 // Start a new block here in order to preserve our context state after calling
joshualittb0a8a372014-09-23 09:50:21 -0700722 // asFragmentProcessor(). Since these calls get passed back to the client, we don't really
krajcevskif461a8f2014-06-19 14:14:06 -0700723 // want them messing around with the context.
724 {
bsalomon83d081a2014-07-08 09:56:10 -0700725 // Allow the shader to modify paintColor and also create an effect to be installed as
726 // the first color effect on the GrPaint.
joshualittb0a8a372014-09-23 09:50:21 -0700727 GrFragmentProcessor* fp = NULL;
bsalomonbed83a62015-04-15 14:18:34 -0700728 if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) {
729 return false;
730 }
731 if (fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700732 grPaint->addColorProcessor(fp)->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700733 constantColor = false;
734 }
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000735 }
krajcevskif461a8f2014-06-19 14:14:06 -0700736
joshualittb0a8a372014-09-23 09:50:21 -0700737 // The grcolor is automatically set when calling asFragmentProcessor.
dandov9de5b512014-06-10 14:38:28 -0700738 // 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 -0700739 return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000740}
reed8b26b992015-05-07 15:36:17 -0700741
742SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
743#ifdef SK_DEBUG
744 const GrSurfaceDesc& desc = tex->desc();
745 SkASSERT(w <= desc.fWidth);
746 SkASSERT(h <= desc.fHeight);
747#endif
748 const GrPixelConfig config = tex->config();
749 SkColorType ct;
750 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
751 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
752 ct = kUnknown_SkColorType;
753 }
754 return SkImageInfo::Make(w, h, ct, at);
755}
756
757
758void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
759 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
760 dst->setInfo(info);
761 dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref();
762}