blob: 84023ecbb6cd0c8ce8d7d1c5aa16081a0dbd6549 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reedb5d32632015-09-29 13:36:50 -07008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "SkGr.h"
bsalomon0d996862016-03-09 18:44:43 -080010#include "SkGrPriv.h"
egdaniel378092f2014-12-03 10:40:13 -080011
bsalomon76228632015-05-29 08:02:10 -070012#include "GrCaps.h"
bsalomonf276ac52015-10-09 13:36:42 -070013#include "GrContext.h"
bsalomon045802d2015-10-20 07:58:01 -070014#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
cblume55f2d2d2016-02-26 13:20:48 -080016#include "GrTextureParamsAdjuster.h"
17#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080018#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070019#include "GrYUVProvider.h"
20
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000021#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000022#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070023#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070024#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080025#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070026#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000027#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080028#include "SkMipMap.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000029#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080030#include "SkResourceCache.h"
cblume55f2d2d2016-02-26 13:20:48 -080031#include "SkTemplates.h"
krajcevski40a1e112014-08-05 14:13:36 -070032#include "SkTextureCompressor.h"
sugoi692135f2015-01-19 10:10:27 -080033#include "SkYUVPlanesCache.h"
joshualitt9bc39542015-08-12 12:57:54 -070034#include "effects/GrBicubicEffect.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070035#include "effects/GrConstColorProcessor.h"
krajcevskif461a8f2014-06-19 14:14:06 -070036#include "effects/GrDitherEffect.h"
egdaniel378092f2014-12-03 10:40:13 -080037#include "effects/GrPorterDuffXferProcessor.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070038#include "effects/GrXfermodeFragmentProcessor.h"
bsalomonf267c1e2016-02-01 13:16:14 -080039#include "effects/GrYUVEffect.h"
krajcevski9c0e6292014-06-02 07:38:14 -070040
krajcevski8c111f72014-06-02 13:51:34 -070041#ifndef SK_IGNORE_ETC1_SUPPORT
krajcevski99ffe242014-06-03 13:04:35 -070042# include "ktx.h"
krajcevski9c0e6292014-06-02 07:38:14 -070043# include "etc1.h"
44#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000045
brianosmana6359362016-03-21 06:55:37 -070046GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) {
bsalomon466c2c42015-10-16 12:01:18 -070047 GrSurfaceDesc desc;
48 desc.fFlags = kNone_GrSurfaceFlags;
49 desc.fWidth = info.width();
50 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -070051 desc.fConfig = SkImageInfo2GrPixelConfig(info, caps);
bsalomon466c2c42015-10-16 12:01:18 -070052 desc.fSampleCnt = 0;
53 return desc;
54}
55
bsalomon045802d2015-10-20 07:58:01 -070056void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
57 SkASSERT(key);
58 SkASSERT(imageID);
59 SkASSERT(!imageBounds.isEmpty());
60 static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
61 GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
bsalomon466c2c42015-10-16 12:01:18 -070062 builder[0] = imageID;
bsalomon045802d2015-10-20 07:58:01 -070063 builder[1] = imageBounds.fLeft;
64 builder[2] = imageBounds.fTop;
65 builder[3] = imageBounds.fRight;
66 builder[4] = imageBounds.fBottom;
bsalomon466c2c42015-10-16 12:01:18 -070067}
68
69GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
70 int expectedW, int expectedH,
71 const void** outStartOfDataToUpload) {
72 *outStartOfDataToUpload = nullptr;
73#ifndef SK_IGNORE_ETC1_SUPPORT
74 if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
75 return kUnknown_GrPixelConfig;
76 }
77
78 const uint8_t* bytes = data->bytes();
79 if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
80 // Does the data match the dimensions of the bitmap? If not,
81 // then we don't know how to scale the image to match it...
82 if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
83 etc1_pkm_get_height(bytes) != (unsigned)expectedH)
84 {
85 return kUnknown_GrPixelConfig;
86 }
87
88 *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
89 return kETC1_GrPixelConfig;
scroggob8e09602016-04-12 07:41:22 -070090 } else if (SkKTXFile::is_ktx(bytes, data->size())) {
bsalomon466c2c42015-10-16 12:01:18 -070091 SkKTXFile ktx(data);
92
93 // Is it actually an ETC1 texture?
94 if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
95 return kUnknown_GrPixelConfig;
96 }
97
98 // Does the data match the dimensions of the bitmap? If not,
99 // then we don't know how to scale the image to match it...
100 if (ktx.width() != expectedW || ktx.height() != expectedH) {
101 return kUnknown_GrPixelConfig;
102 }
103
104 *outStartOfDataToUpload = ktx.pixelData();
105 return kETC1_GrPixelConfig;
106 }
107#endif
108 return kUnknown_GrPixelConfig;
109}
110
bsalomon045802d2015-10-20 07:58:01 -0700111//////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112
bsalomon045802d2015-10-20 07:58:01 -0700113/**
114 * Fill out buffer with the compressed format Ganesh expects from a colortable
115 * based bitmap. [palette (colortable) + indices].
116 *
117 * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
118 * we could detect that the colortable.count is <= 16, and then repack the
119 * indices as nibbles to save RAM, but it would take more time (i.e. a lot
120 * slower than memcpy), so skipping that for now.
121 *
122 * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
123 * as the colortable.count says it is.
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 */
bsalomon0d996862016-03-09 18:44:43 -0800125static void build_index8_data(void* buffer, const SkPixmap& pixmap) {
126 SkASSERT(kIndex_8_SkColorType == pixmap.colorType());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000127
bsalomon0d996862016-03-09 18:44:43 -0800128 const SkColorTable* ctable = pixmap.ctable();
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 char* dst = (char*)buffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000130
reed@google.com7111d462014-03-25 16:20:24 +0000131 const int count = ctable->count();
132
133 SkDstPixelInfo dstPI;
134 dstPI.fColorType = kRGBA_8888_SkColorType;
135 dstPI.fAlphaType = kPremul_SkAlphaType;
136 dstPI.fPixels = buffer;
137 dstPI.fRowBytes = count * sizeof(SkPMColor);
138
139 SkSrcPixelInfo srcPI;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000140 srcPI.fColorType = kN32_SkColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000141 srcPI.fAlphaType = kPremul_SkAlphaType;
mtklein775b8192014-12-02 09:11:25 -0800142 srcPI.fPixels = ctable->readColors();
reed@google.com7111d462014-03-25 16:20:24 +0000143 srcPI.fRowBytes = count * sizeof(SkPMColor);
144
145 srcPI.convertPixelsTo(&dstPI, count, 1);
146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomond4cb9222014-08-11 14:19:09 -0700148 dst += 256 * sizeof(GrColor);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
bsalomon0d996862016-03-09 18:44:43 -0800150 if ((unsigned)pixmap.width() == pixmap.rowBytes()) {
151 memcpy(dst, pixmap.addr(), pixmap.getSafeSize());
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 } else {
153 // need to trim off the extra bytes per row
bsalomon0d996862016-03-09 18:44:43 -0800154 size_t width = pixmap.width();
155 size_t rowBytes = pixmap.rowBytes();
156 const uint8_t* src = pixmap.addr8();
157 for (int y = 0; y < pixmap.height(); y++) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 memcpy(dst, src, width);
159 src += rowBytes;
160 dst += width;
161 }
162 }
163}
164
bsalomon045802d2015-10-20 07:58:01 -0700165/**
reed43fe6182015-09-08 08:37:36 -0700166 * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
167 * be gone from SkPixelRef, and we can remove this subclass entirely.
168 */
169class PixelRef_GrYUVProvider : public GrYUVProvider {
170 SkPixelRef* fPR;
171
172public:
173 PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
174
175 uint32_t onGetID() override { return fPR->getGenerationID(); }
msarett4984c3c2016-03-10 05:44:43 -0800176 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
177 return fPR->queryYUV8(sizeInfo, colorSpace);
reed43fe6182015-09-08 08:37:36 -0700178 }
msarett4984c3c2016-03-10 05:44:43 -0800179 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
180 return fPR->getYUV8Planes(sizeInfo, planes);
reed43fe6182015-09-08 08:37:36 -0700181 }
182};
krajcevski9c0e6292014-06-02 07:38:14 -0700183
bsalomon045802d2015-10-20 07:58:01 -0700184static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
185 const GrSurfaceDesc& desc) {
sugoiff58e462014-10-16 05:19:31 -0700186 // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
sugoi518d83d2014-07-21 11:37:39 -0700187 SkPixelRef* pixelRef = bm.pixelRef();
reed43fe6182015-09-08 08:37:36 -0700188 if ((nullptr == pixelRef) ||
bsalomon045802d2015-10-20 07:58:01 -0700189 (pixelRef->info().width() != bm.info().width()) ||
sugoi692135f2015-01-19 10:10:27 -0800190 (pixelRef->info().height() != bm.info().height())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
sugoi518d83d2014-07-21 11:37:39 -0700192 }
193
reed43fe6182015-09-08 08:37:36 -0700194 PixelRef_GrYUVProvider provider(pixelRef);
reed43fe6182015-09-08 08:37:36 -0700195
bsalomon045802d2015-10-20 07:58:01 -0700196 return provider.refAsTexture(ctx, desc, !bm.isVolatile());
sugoi518d83d2014-07-21 11:37:39 -0700197}
198
bsalomon045802d2015-10-20 07:58:01 -0700199static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
bsalomon466c2c42015-10-16 12:01:18 -0700200 SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
201 if (!data) {
202 return nullptr;
203 }
204
205 const void* startOfTexData;
206 desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
207 &startOfTexData);
208 if (kUnknown_GrPixelConfig == desc.fConfig) {
209 return nullptr;
210 }
211
bsalomon5ec26ae2016-02-25 08:33:02 -0800212 return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
bsalomon466c2c42015-10-16 12:01:18 -0700213}
214
bsalomon0d996862016-03-09 18:44:43 -0800215GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) {
brianosmana6359362016-03-21 06:55:37 -0700216 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
bsalomon0d996862016-03-09 18:44:43 -0800217 if (GrTexture *texture = load_etc1_texture(ctx, bitmap, desc)) {
218 return texture;
219 }
bsalomon045802d2015-10-20 07:58:01 -0700220
bsalomon0d996862016-03-09 18:44:43 -0800221 if (GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc)) {
222 return texture;
223 }
224
225 SkAutoLockPixels alp(bitmap);
226 if (!bitmap.readyToDraw()) {
227 return nullptr;
228 }
229 SkPixmap pixmap;
230 if (!bitmap.peekPixels(&pixmap)) {
231 return nullptr;
232 }
ericrk8bea8902016-03-18 11:52:20 -0700233 return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
bsalomon0d996862016-03-09 18:44:43 -0800234}
235
ericrk8bea8902016-03-18 11:52:20 -0700236GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800237 const SkPixmap* pmap = &pixmap;
238 SkPixmap tmpPixmap;
bsalomon045802d2015-10-20 07:58:01 -0700239 SkBitmap tmpBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000240
bsalomon76228632015-05-29 08:02:10 -0700241 const GrCaps* caps = ctx->caps();
brianosmana6359362016-03-21 06:55:37 -0700242 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243
brianosmana6359362016-03-21 06:55:37 -0700244 if (caps->srgbSupport() && !GrPixelConfigIsSRGB(desc.fConfig) &&
245 kSRGB_SkColorProfileType == pixmap.info().profileType()) {
brianosman85f92692016-03-24 06:56:32 -0700246 // We were supplied sRGB as the profile type, but we don't have a suitable pixel config.
brianosmana6359362016-03-21 06:55:37 -0700247 // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't
248 // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and
249 // destination (claim they're linear):
250 SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
251 pixmap.colorType(), pixmap.alphaType());
252 SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable());
253
254 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height(),
255 kSRGB_SkColorProfileType);
256 tmpBitmap.allocPixels(dstInfo);
257
258 SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
259 if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
260 return nullptr;
261 }
262 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
263 return nullptr;
264 }
265 pmap = &tmpPixmap;
266 // must rebuild desc, since we've forced the info to be N32
267 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosman85f92692016-03-24 06:56:32 -0700268 } else if (kGray_8_SkColorType == pixmap.colorType()) {
269 // We don't have Gray8 support as a pixel config, so expand to 8888
270
271 // We should have converted sRGB Gray8 above (if we have sRGB support):
272 SkASSERT(!caps->srgbSupport() || kLinear_SkColorProfileType == pixmap.info().profileType());
273
274 SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(),
275 kOpaque_SkAlphaType);
276 tmpBitmap.allocPixels(info);
277 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
278 return nullptr;
279 }
280 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
281 return nullptr;
282 }
283 pmap = &tmpPixmap;
284 // must rebuild desc, since we've forced the info to be N32
285 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosmana6359362016-03-21 06:55:37 -0700286 } else if (kIndex_8_SkColorType == pixmap.colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700287 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700288 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bsalomon0d996862016-03-09 18:44:43 -0800289 pixmap.width(), pixmap.height());
bsalomond4cb9222014-08-11 14:19:09 -0700290 SkAutoMalloc storage(imageSize);
bsalomon0d996862016-03-09 18:44:43 -0800291 build_index8_data(storage.get(), pixmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
293 // our compressed data will be trimmed, so pass width() for its
294 // "rowBytes", since they are the same now.
ericrk8bea8902016-03-18 11:52:20 -0700295 return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(),
bsalomon0d996862016-03-09 18:44:43 -0800296 pixmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 } else {
bsalomon0d996862016-03-09 18:44:43 -0800298 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
299 tmpBitmap.allocPixels(info);
300 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
301 return nullptr;
302 }
303 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
304 return nullptr;
305 }
306 pmap = &tmpPixmap;
307 // must rebuild desc, since we've forced the info to be N32
brianosmana6359362016-03-21 06:55:37 -0700308 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
krajcevski9c0e6292014-06-02 07:38:14 -0700309 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000310 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000311
ericrk8bea8902016-03-18 11:52:20 -0700312 return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
bsalomon0d996862016-03-09 18:44:43 -0800313 pmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800314}
315
bsalomonb4d40ef2015-07-15 10:12:16 -0700316
bsalomon045802d2015-10-20 07:58:01 -0700317////////////////////////////////////////////////////////////////////////////////
318
bsalomonc55271f2015-11-09 11:55:57 -0800319void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700320 class Invalidator : public SkPixelRef::GenIDChangeListener {
321 public:
322 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
323 private:
324 GrUniqueKeyInvalidatedMessage fMsg;
325
326 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
327 };
328
329 pixelRef->addGenIDChangeListener(new Invalidator(key));
330}
331
cblume55f2d2d2016-02-26 13:20:48 -0800332GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap)
333{
brianosmana6359362016-03-21 06:55:37 -0700334 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
cblume55f2d2d2016-02-26 13:20:48 -0800335 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
336 GrTexture* texture = load_etc1_texture(ctx, bitmap, desc);
337 if (texture) {
338 return texture;
339 }
340 }
341
342 GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc);
343 if (texture) {
344 return texture;
345 }
346
brianosmaneb3429c2016-03-25 13:03:03 -0700347 // SkMipMap::Build doesn't handle sRGB data correctly (yet).
348 // Failover to the GL code-path for now.
349 if (kLinear_SkColorProfileType != bitmap.profileType()) {
350 return nullptr;
351 }
352
cblume55f2d2d2016-02-26 13:20:48 -0800353 SkASSERT(sizeof(int) <= sizeof(uint32_t));
354 if (bitmap.width() < 0 || bitmap.height() < 0) {
355 return nullptr;
356 }
357
358 SkAutoPixmapUnlock srcUnlocker;
359 if (!bitmap.requestLock(&srcUnlocker)) {
360 return nullptr;
361 }
362 const SkPixmap& pixmap = srcUnlocker.pixmap();
363 // Try to catch where we might have returned nullptr for src crbug.com/492818
364 if (nullptr == pixmap.addr()) {
365 sk_throw();
366 }
367
368 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
369 if (!mipmaps) {
370 return nullptr;
371 }
372
373 const int mipLevelCount = mipmaps->countLevels() + 1;
374 if (mipLevelCount < 1) {
375 return nullptr;
376 }
377
378 const bool isMipMapped = mipLevelCount > 1;
379 desc.fIsMipMapped = isMipMapped;
380
381 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
382
383 texels[0].fPixels = pixmap.addr();
384 texels[0].fRowBytes = pixmap.rowBytes();
385
386 for (int i = 1; i < mipLevelCount; ++i) {
387 SkMipMap::Level generatedMipLevel;
388 mipmaps->getLevel(i - 1, &generatedMipLevel);
389 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
390 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
391 }
392
393 return ctx->textureProvider()->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(),
394 mipLevelCount);
395}
396
reedb5d32632015-09-29 13:36:50 -0700397GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700398 const GrTextureParams& params) {
bsalomon89fe56b2015-10-29 10:49:28 -0700399 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800400 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700401 }
bsalomonb1b01992015-11-18 10:56:08 -0800402 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000403}
reed8f343722015-08-13 13:32:39 -0700404
rileya@google.com24f3ad12012-07-18 21:47:40 +0000405///////////////////////////////////////////////////////////////////////////////
406
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000407// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
408// alpha info, that will be considered.
brianosmana6359362016-03-21 06:55:37 -0700409GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt,
410 const GrCaps& caps) {
411 // We intentionally ignore profile type for non-8888 formats. Anything we can't support
412 // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture.
brianosmanc571c002016-03-17 13:01:26 -0700413 switch (ct) {
414 case kUnknown_SkColorType:
415 return kUnknown_GrPixelConfig;
416 case kAlpha_8_SkColorType:
417 return kAlpha_8_GrPixelConfig;
418 case kRGB_565_SkColorType:
419 return kRGB_565_GrPixelConfig;
420 case kARGB_4444_SkColorType:
421 return kRGBA_4444_GrPixelConfig;
422 case kRGBA_8888_SkColorType:
brianosmana6359362016-03-21 06:55:37 -0700423 return (kSRGB_SkColorProfileType == pt && caps.srgbSupport())
424 ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700425 case kBGRA_8888_SkColorType:
brianosmana6359362016-03-21 06:55:37 -0700426 return (kSRGB_SkColorProfileType == pt && caps.srgbSupport())
427 ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700428 case kIndex_8_SkColorType:
429 return kIndex_8_GrPixelConfig;
430 case kGray_8_SkColorType:
431 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
432 case kRGBA_F16_SkColorType:
433 return kRGBA_half_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000434 }
435 SkASSERT(0); // shouldn't get here
436 return kUnknown_GrPixelConfig;
437}
438
jvanverthfa1e8a72014-12-22 08:31:49 -0800439bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
440 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000441 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800442 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000443 switch (config) {
444 case kAlpha_8_GrPixelConfig:
445 ct = kAlpha_8_SkColorType;
446 break;
447 case kIndex_8_GrPixelConfig:
448 ct = kIndex_8_SkColorType;
449 break;
450 case kRGB_565_GrPixelConfig:
451 ct = kRGB_565_SkColorType;
452 break;
453 case kRGBA_4444_GrPixelConfig:
454 ct = kARGB_4444_SkColorType;
455 break;
456 case kRGBA_8888_GrPixelConfig:
457 ct = kRGBA_8888_SkColorType;
458 break;
459 case kBGRA_8888_GrPixelConfig:
460 ct = kBGRA_8888_SkColorType;
461 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800462 case kSRGBA_8888_GrPixelConfig:
463 ct = kRGBA_8888_SkColorType;
464 pt = kSRGB_SkColorProfileType;
465 break;
brianosmana6359362016-03-21 06:55:37 -0700466 case kSBGRA_8888_GrPixelConfig:
467 ct = kBGRA_8888_SkColorType;
468 pt = kSRGB_SkColorProfileType;
469 break;
reed@google.combf790232013-12-13 19:45:58 +0000470 default:
471 return false;
472 }
473 if (ctOut) {
474 *ctOut = ct;
475 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800476 if (ptOut) {
477 *ptOut = pt;
478 }
reed@google.combf790232013-12-13 19:45:58 +0000479 return true;
480}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000481
bsalomonf1b7a1d2015-09-28 06:26:28 -0700482////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000483
bsalomonaa48d362015-10-01 08:34:17 -0700484static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
485 if (primitiveIsSrc) {
486 return SkXfermode::kSrc_Mode != mode;
487 } else {
488 return SkXfermode::kDst_Mode != mode;
489 }
490}
491
bsalomonf1b7a1d2015-09-28 06:26:28 -0700492static inline bool skpaint_to_grpaint_impl(GrContext* context,
493 const SkPaint& skPaint,
494 const SkMatrix& viewM,
495 const GrFragmentProcessor** shaderProcessor,
496 SkXfermode::Mode* primColorMode,
497 bool primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700498 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700499 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000500 grPaint->setAntiAlias(skPaint.isAntiAlias());
brianosman898235c2016-04-06 07:38:23 -0700501 grPaint->setAllowSRGBInputs(allowSRGBInputs);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000502
bsalomonf1b7a1d2015-09-28 06:26:28 -0700503 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
504 // of per-vertex colors.
505 SkAutoTUnref<const GrFragmentProcessor> aufp;
bsalomonaa48d362015-10-01 08:34:17 -0700506 const GrFragmentProcessor* shaderFP = nullptr;
507 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
508 if (shaderProcessor) {
509 shaderFP = *shaderProcessor;
510 } else if (const SkShader* shader = skPaint.getShader()) {
511 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700512 skPaint.getFilterQuality()));
bsalomonaa48d362015-10-01 08:34:17 -0700513 shaderFP = aufp;
514 if (!shaderFP) {
515 return false;
516 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700517 }
518 }
519
520 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
521 // a known constant value. In that case we can simply apply a color filter during this
522 // conversion without converting the color filter to a GrFragmentProcessor.
523 bool applyColorFilterToPaintColor = false;
524 if (shaderFP) {
525 if (primColorMode) {
526 // There is a blend between the primitive color and the shader color. The shader sees
527 // the opaque paint color. The shader's output is blended using the provided mode by
528 // the primitive color. The blended color is then modulated by the paint's alpha.
529
530 // The geometry processor will insert the primitive color to start the color chain, so
531 // the GrPaint color will be ignored.
532
533 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
534
535 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
536 aufp.reset(shaderFP);
537
538 if (primitiveIsSrc) {
539 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
540 *primColorMode);
541 } else {
542 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
543 *primColorMode);
544 }
545 aufp.reset(shaderFP);
546 // The above may return null if compose results in a pass through of the prim color.
547 if (shaderFP) {
548 grPaint->addColorFragmentProcessor(shaderFP);
549 }
550
551 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
552 if (GrColor_WHITE != paintAlpha) {
553 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
554 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
555 }
556 } else {
557 // The shader's FP sees the paint unpremul color
558 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
559 grPaint->addColorFragmentProcessor(shaderFP);
560 }
561 } else {
562 if (primColorMode) {
563 // There is a blend between the primitive color and the paint color. The blend considers
564 // the opaque paint color. The paint's alpha is applied to the post-blended color.
565 SkAutoTUnref<const GrFragmentProcessor> processor(
566 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
567 GrConstColorProcessor::kIgnore_InputMode));
568 if (primitiveIsSrc) {
569 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
570 *primColorMode));
571 } else {
572 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
573 *primColorMode));
574
575 }
576 if (processor) {
577 grPaint->addColorFragmentProcessor(processor);
578 }
579
bsalomonaa48d362015-10-01 08:34:17 -0700580 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700581
582 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700583 if (GrColor_WHITE != paintAlpha) {
584 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
585 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
586 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700587 } else {
588 // No shader, no primitive color.
589 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
590 applyColorFilterToPaintColor = true;
591 }
592 }
593
594 SkColorFilter* colorFilter = skPaint.getColorFilter();
595 if (colorFilter) {
596 if (applyColorFilterToPaintColor) {
597 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
598 } else {
bsalomone25eea42015-09-29 06:38:55 -0700599 SkAutoTUnref<const GrFragmentProcessor> cfFP(
bsalomon4a339522015-10-06 08:40:50 -0700600 colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700601 if (cfFP) {
602 grPaint->addColorFragmentProcessor(cfFP);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700603 } else {
604 return false;
605 }
606 }
607 }
608
robertphillips4f037942016-02-09 05:09:27 -0800609 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
610 // the GrPaint to also be null (also kSrcOver).
611 SkASSERT(!grPaint->getXPFactory());
612 SkXfermode* xfermode = skPaint.getXfermode();
613 if (xfermode) {
halcanary9d524f22016-03-29 09:03:52 -0700614 // SafeUnref in case a new xfermode is added that returns null.
robertphillips4f037942016-02-09 05:09:27 -0800615 // In such cases we will fall back to kSrcOver_Mode.
616 SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory()));
617 }
mtklein775b8192014-12-02 09:11:25 -0800618
krajcevskif461a8f2014-06-19 14:14:06 -0700619#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700620 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bsalomonaca31fe2015-09-22 11:38:46 -0700621 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
krajcevskif461a8f2014-06-19 14:14:06 -0700622 }
623#endif
bsalomonbed83a62015-04-15 14:18:34 -0700624 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000625}
626
bsalomonf1b7a1d2015-09-28 06:26:28 -0700627bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
brianosman898235c2016-04-06 07:38:23 -0700628 bool allowSRGBInputs, GrPaint* grPaint) {
629 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false,
630 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700631}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000632
bsalomonf1b7a1d2015-09-28 06:26:28 -0700633/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
634bool SkPaintToGrPaintReplaceShader(GrContext* context,
635 const SkPaint& skPaint,
636 const GrFragmentProcessor* shaderFP,
brianosman898235c2016-04-06 07:38:23 -0700637 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700638 GrPaint* grPaint) {
639 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700640 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000641 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700642 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
brianosman898235c2016-04-06 07:38:23 -0700643 allowSRGBInputs, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000644}
reed8b26b992015-05-07 15:36:17 -0700645
bsalomonf1b7a1d2015-09-28 06:26:28 -0700646/** Ignores the SkShader (if any) on skPaint. */
647bool SkPaintToGrPaintNoShader(GrContext* context,
648 const SkPaint& skPaint,
brianosman898235c2016-04-06 07:38:23 -0700649 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700650 GrPaint* grPaint) {
651 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
652 static const GrFragmentProcessor* kNullShaderFP = nullptr;
653 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
654 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
brianosman898235c2016-04-06 07:38:23 -0700655 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700656}
657
658/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
659be setup as a vertex attribute using the specified SkXfermode::Mode. */
660bool SkPaintToGrPaintWithXfermode(GrContext* context,
661 const SkPaint& skPaint,
662 const SkMatrix& viewM,
663 SkXfermode::Mode primColorMode,
664 bool primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700665 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700666 GrPaint* grPaint) {
667 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700668 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700669}
670
joshualitt33a5fce2015-11-18 13:28:51 -0800671bool SkPaintToGrPaintWithTexture(GrContext* context,
672 const SkPaint& paint,
673 const SkMatrix& viewM,
674 const GrFragmentProcessor* fp,
675 bool textureIsAlphaOnly,
brianosman898235c2016-04-06 07:38:23 -0700676 bool allowSRGBInputs,
joshualitt33a5fce2015-11-18 13:28:51 -0800677 GrPaint* grPaint) {
678 SkAutoTUnref<const GrFragmentProcessor> shaderFP;
679 if (textureIsAlphaOnly) {
680 if (const SkShader* shader = paint.getShader()) {
681 shaderFP.reset(shader->asFragmentProcessor(context,
682 viewM,
683 nullptr,
684 paint.getFilterQuality()));
685 if (!shaderFP) {
686 return false;
687 }
688 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp };
689 shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
690 } else {
691 shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
692 }
693 } else {
694 shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
695 }
696
brianosman898235c2016-04-06 07:38:23 -0700697 return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), allowSRGBInputs, grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800698}
699
bsalomonf1b7a1d2015-09-28 06:26:28 -0700700
701////////////////////////////////////////////////////////////////////////////////////////////////
702
reed8b26b992015-05-07 15:36:17 -0700703SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
704#ifdef SK_DEBUG
705 const GrSurfaceDesc& desc = tex->desc();
706 SkASSERT(w <= desc.fWidth);
707 SkASSERT(h <= desc.fHeight);
708#endif
709 const GrPixelConfig config = tex->config();
710 SkColorType ct;
711 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700712 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700713 ct = kUnknown_SkColorType;
714 }
715 return SkImageInfo::Make(w, h, ct, at);
716}
717
718
719void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
720 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
721 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700722 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700723}
joshualitt9bc39542015-08-12 12:57:54 -0700724
725GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
726 const SkMatrix& viewM,
727 const SkMatrix& localM,
728 bool* doBicubic) {
729 *doBicubic = false;
730 GrTextureParams::FilterMode textureFilterMode;
731 switch (paintFilterQuality) {
732 case kNone_SkFilterQuality:
733 textureFilterMode = GrTextureParams::kNone_FilterMode;
734 break;
735 case kLow_SkFilterQuality:
736 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
737 break;
738 case kMedium_SkFilterQuality: {
739 SkMatrix matrix;
740 matrix.setConcat(viewM, localM);
741 if (matrix.getMinScale() < SK_Scalar1) {
742 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
743 } else {
744 // Don't trigger MIP level generation unnecessarily.
745 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
746 }
747 break;
748 }
749 case kHigh_SkFilterQuality: {
750 SkMatrix matrix;
751 matrix.setConcat(viewM, localM);
752 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
753 break;
754 }
755 default:
756 SkErrorInternals::SetError( kInvalidPaint_SkError,
757 "Sorry, I don't understand the filtering "
758 "mode you asked for. Falling back to "
759 "MIPMaps.");
760 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
761 break;
762
763 }
764 return textureFilterMode;
765}