blob: c2e284154a584dea54f71f323114a4e7391b5b12 [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"
brianosmanfe199872016-06-13 07:59:48 -070017#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080018#include "GrTypes.h"
egdaniel378092f2014-12-03 10:40:13 -080019#include "GrXferProcessor.h"
reed43fe6182015-09-08 08:37:36 -070020#include "GrYUVProvider.h"
21
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +000022#include "SkColorFilter.h"
commit-bot@chromium.orgea476e12013-10-14 18:29:23 +000023#include "SkConfig8888.h"
bsalomonb4d40ef2015-07-15 10:12:16 -070024#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070025#include "SkData.h"
joshualitt5f5a8d72015-02-25 14:09:45 -080026#include "SkErrorInternals.h"
reed8b26b992015-05-07 15:36:17 -070027#include "SkGrPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000028#include "SkMessageBus.h"
cblume55f2d2d2016-02-26 13:20:48 -080029#include "SkMipMap.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000030#include "SkPixelRef.h"
sugoi692135f2015-01-19 10:10:27 -080031#include "SkResourceCache.h"
cblume55f2d2d2016-02-26 13:20:48 -080032#include "SkTemplates.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
robertphillips677da9d2016-05-11 05:15:55 -0700184static sk_sp<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
robertphillips677da9d2016-05-11 05:15:55 -0700221 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
222 if (texture) {
223 return texture.release();
bsalomon0d996862016-03-09 18:44:43 -0800224 }
225
226 SkAutoLockPixels alp(bitmap);
227 if (!bitmap.readyToDraw()) {
228 return nullptr;
229 }
230 SkPixmap pixmap;
231 if (!bitmap.peekPixels(&pixmap)) {
232 return nullptr;
233 }
ericrk8bea8902016-03-18 11:52:20 -0700234 return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes);
bsalomon0d996862016-03-09 18:44:43 -0800235}
236
ericrk8bea8902016-03-18 11:52:20 -0700237GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800238 const SkPixmap* pmap = &pixmap;
239 SkPixmap tmpPixmap;
bsalomon045802d2015-10-20 07:58:01 -0700240 SkBitmap tmpBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000241
bsalomon76228632015-05-29 08:02:10 -0700242 const GrCaps* caps = ctx->caps();
brianosmana6359362016-03-21 06:55:37 -0700243 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000244
brianosmana6359362016-03-21 06:55:37 -0700245 if (caps->srgbSupport() && !GrPixelConfigIsSRGB(desc.fConfig) &&
brianosmanab824182016-06-16 11:41:44 -0700246 kSRGB_SkColorProfileType == pixmap.info().profileType()) {
brianosman85f92692016-03-24 06:56:32 -0700247 // We were supplied sRGB as the profile type, but we don't have a suitable pixel config.
brianosmana6359362016-03-21 06:55:37 -0700248 // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't
249 // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and
250 // destination (claim they're linear):
251 SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(),
252 pixmap.colorType(), pixmap.alphaType());
253 SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable());
254
255 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height(),
256 kSRGB_SkColorProfileType);
257 tmpBitmap.allocPixels(dstInfo);
258
259 SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
260 if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
261 return nullptr;
262 }
263 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
264 return nullptr;
265 }
266 pmap = &tmpPixmap;
267 // must rebuild desc, since we've forced the info to be N32
268 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosman85f92692016-03-24 06:56:32 -0700269 } else if (kGray_8_SkColorType == pixmap.colorType()) {
270 // We don't have Gray8 support as a pixel config, so expand to 8888
271
272 // We should have converted sRGB Gray8 above (if we have sRGB support):
273 SkASSERT(!caps->srgbSupport() || kLinear_SkColorProfileType == pixmap.info().profileType());
274
275 SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(),
276 kOpaque_SkAlphaType);
277 tmpBitmap.allocPixels(info);
278 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
279 return nullptr;
280 }
281 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
282 return nullptr;
283 }
284 pmap = &tmpPixmap;
285 // must rebuild desc, since we've forced the info to be N32
286 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
brianosmana6359362016-03-21 06:55:37 -0700287 } else if (kIndex_8_SkColorType == pixmap.colorType()) {
bsalomon76228632015-05-29 08:02:10 -0700288 if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomond4cb9222014-08-11 14:19:09 -0700289 size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bsalomon0d996862016-03-09 18:44:43 -0800290 pixmap.width(), pixmap.height());
bsalomond4cb9222014-08-11 14:19:09 -0700291 SkAutoMalloc storage(imageSize);
bsalomon0d996862016-03-09 18:44:43 -0800292 build_index8_data(storage.get(), pixmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000293
294 // our compressed data will be trimmed, so pass width() for its
295 // "rowBytes", since they are the same now.
ericrk8bea8902016-03-18 11:52:20 -0700296 return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(),
bsalomon0d996862016-03-09 18:44:43 -0800297 pixmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 } else {
bsalomon0d996862016-03-09 18:44:43 -0800299 SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height());
300 tmpBitmap.allocPixels(info);
301 if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) {
302 return nullptr;
303 }
304 if (!tmpBitmap.peekPixels(&tmpPixmap)) {
305 return nullptr;
306 }
307 pmap = &tmpPixmap;
308 // must rebuild desc, since we've forced the info to be N32
brianosmana6359362016-03-21 06:55:37 -0700309 desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps);
krajcevski9c0e6292014-06-02 07:38:14 -0700310 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000311 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
ericrk8bea8902016-03-18 11:52:20 -0700313 return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(),
bsalomon0d996862016-03-09 18:44:43 -0800314 pmap->rowBytes());
bsalomon37f9a262015-02-02 13:00:10 -0800315}
316
bsalomonb4d40ef2015-07-15 10:12:16 -0700317
bsalomon045802d2015-10-20 07:58:01 -0700318////////////////////////////////////////////////////////////////////////////////
319
bsalomonc55271f2015-11-09 11:55:57 -0800320void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
bsalomon89fe56b2015-10-29 10:49:28 -0700321 class Invalidator : public SkPixelRef::GenIDChangeListener {
322 public:
323 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
324 private:
325 GrUniqueKeyInvalidatedMessage fMsg;
326
327 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
328 };
329
330 pixelRef->addGenIDChangeListener(new Invalidator(key));
331}
332
brianosman982eb7f2016-06-06 13:10:58 -0700333GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap,
334 SkSourceGammaTreatment gammaTreatment)
cblume55f2d2d2016-02-26 13:20:48 -0800335{
brianosmana6359362016-03-21 06:55:37 -0700336 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps());
cblume55f2d2d2016-02-26 13:20:48 -0800337 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
338 GrTexture* texture = load_etc1_texture(ctx, bitmap, desc);
339 if (texture) {
340 return texture;
341 }
342 }
343
robertphillips677da9d2016-05-11 05:15:55 -0700344 sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc));
cblume55f2d2d2016-02-26 13:20:48 -0800345 if (texture) {
robertphillips677da9d2016-05-11 05:15:55 -0700346 return texture.release();
cblume55f2d2d2016-02-26 13:20:48 -0800347 }
348
349 SkASSERT(sizeof(int) <= sizeof(uint32_t));
350 if (bitmap.width() < 0 || bitmap.height() < 0) {
351 return nullptr;
352 }
353
354 SkAutoPixmapUnlock srcUnlocker;
355 if (!bitmap.requestLock(&srcUnlocker)) {
356 return nullptr;
357 }
358 const SkPixmap& pixmap = srcUnlocker.pixmap();
359 // Try to catch where we might have returned nullptr for src crbug.com/492818
360 if (nullptr == pixmap.addr()) {
361 sk_throw();
362 }
363
reed6644d932016-06-10 11:41:47 -0700364 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr));
cblume55f2d2d2016-02-26 13:20:48 -0800365 if (!mipmaps) {
366 return nullptr;
367 }
368
369 const int mipLevelCount = mipmaps->countLevels() + 1;
370 if (mipLevelCount < 1) {
371 return nullptr;
372 }
373
374 const bool isMipMapped = mipLevelCount > 1;
375 desc.fIsMipMapped = isMipMapped;
376
377 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
378
379 texels[0].fPixels = pixmap.addr();
380 texels[0].fRowBytes = pixmap.rowBytes();
381
382 for (int i = 1; i < mipLevelCount; ++i) {
383 SkMipMap::Level generatedMipLevel;
384 mipmaps->getLevel(i - 1, &generatedMipLevel);
385 texels[i].fPixels = generatedMipLevel.fPixmap.addr();
386 texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
387 }
388
brianosmanfe199872016-06-13 07:59:48 -0700389 {
390 GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc,
391 SkBudgeted::kYes,
392 texels.get(),
393 mipLevelCount);
394 texture->texturePriv().setGammaTreatment(gammaTreatment);
395 return texture;
396 }
cblume55f2d2d2016-02-26 13:20:48 -0800397}
398
cblume186d2d42016-06-03 11:17:42 -0700399GrTexture* GrUploadMipMapToTexture(GrContext* ctx, const SkImageInfo& info,
400 const GrMipLevel* texels, int mipLevelCount) {
401 const GrCaps* caps = ctx->caps();
402 return ctx->textureProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps),
403 SkBudgeted::kYes, texels,
404 mipLevelCount);
405}
406
reedb5d32632015-09-29 13:36:50 -0700407GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
brianosman982eb7f2016-06-06 13:10:58 -0700408 const GrTextureParams& params,
409 SkSourceGammaTreatment gammaTreatment) {
bsalomon89fe56b2015-10-29 10:49:28 -0700410 if (bitmap.getTexture()) {
brianosman982eb7f2016-06-06 13:10:58 -0700411 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, gammaTreatment,
412 nullptr);
bsalomon89fe56b2015-10-29 10:49:28 -0700413 }
brianosman982eb7f2016-06-06 13:10:58 -0700414 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, gammaTreatment);
rileya@google.com24f3ad12012-07-18 21:47:40 +0000415}
reed8f343722015-08-13 13:32:39 -0700416
rileya@google.com24f3ad12012-07-18 21:47:40 +0000417///////////////////////////////////////////////////////////////////////////////
418
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000419// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
420// alpha info, that will be considered.
brianosmanab824182016-06-16 11:41:44 -0700421GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt,
brianosmana6359362016-03-21 06:55:37 -0700422 const GrCaps& caps) {
423 // We intentionally ignore profile type for non-8888 formats. Anything we can't support
424 // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture.
brianosmanc571c002016-03-17 13:01:26 -0700425 switch (ct) {
426 case kUnknown_SkColorType:
427 return kUnknown_GrPixelConfig;
428 case kAlpha_8_SkColorType:
429 return kAlpha_8_GrPixelConfig;
430 case kRGB_565_SkColorType:
431 return kRGB_565_GrPixelConfig;
432 case kARGB_4444_SkColorType:
433 return kRGBA_4444_GrPixelConfig;
434 case kRGBA_8888_SkColorType:
brianosmanab824182016-06-16 11:41:44 -0700435 return (kSRGB_SkColorProfileType == pt && caps.srgbSupport())
brianosmana6359362016-03-21 06:55:37 -0700436 ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700437 case kBGRA_8888_SkColorType:
brianosmanab824182016-06-16 11:41:44 -0700438 return (kSRGB_SkColorProfileType == pt && caps.srgbSupport())
brianosmana6359362016-03-21 06:55:37 -0700439 ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig;
brianosmanc571c002016-03-17 13:01:26 -0700440 case kIndex_8_SkColorType:
441 return kIndex_8_GrPixelConfig;
442 case kGray_8_SkColorType:
443 return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
444 case kRGBA_F16_SkColorType:
445 return kRGBA_half_GrPixelConfig;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000446 }
447 SkASSERT(0); // shouldn't get here
448 return kUnknown_GrPixelConfig;
449}
450
jvanverthfa1e8a72014-12-22 08:31:49 -0800451bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
452 SkColorProfileType* ptOut) {
reed@google.combf790232013-12-13 19:45:58 +0000453 SkColorType ct;
jvanverthfa1e8a72014-12-22 08:31:49 -0800454 SkColorProfileType pt = kLinear_SkColorProfileType;
reed@google.combf790232013-12-13 19:45:58 +0000455 switch (config) {
456 case kAlpha_8_GrPixelConfig:
457 ct = kAlpha_8_SkColorType;
458 break;
459 case kIndex_8_GrPixelConfig:
460 ct = kIndex_8_SkColorType;
461 break;
462 case kRGB_565_GrPixelConfig:
463 ct = kRGB_565_SkColorType;
464 break;
465 case kRGBA_4444_GrPixelConfig:
466 ct = kARGB_4444_SkColorType;
467 break;
468 case kRGBA_8888_GrPixelConfig:
469 ct = kRGBA_8888_SkColorType;
470 break;
471 case kBGRA_8888_GrPixelConfig:
472 ct = kBGRA_8888_SkColorType;
473 break;
jvanverthfa1e8a72014-12-22 08:31:49 -0800474 case kSRGBA_8888_GrPixelConfig:
475 ct = kRGBA_8888_SkColorType;
476 pt = kSRGB_SkColorProfileType;
477 break;
brianosmana6359362016-03-21 06:55:37 -0700478 case kSBGRA_8888_GrPixelConfig:
479 ct = kBGRA_8888_SkColorType;
480 pt = kSRGB_SkColorProfileType;
481 break;
brianosman9ac5b912016-04-12 13:49:53 -0700482 case kRGBA_half_GrPixelConfig:
483 ct = kRGBA_F16_SkColorType;
484 break;
reed@google.combf790232013-12-13 19:45:58 +0000485 default:
486 return false;
487 }
488 if (ctOut) {
489 *ctOut = ct;
490 }
jvanverthfa1e8a72014-12-22 08:31:49 -0800491 if (ptOut) {
492 *ptOut = pt;
493 }
reed@google.combf790232013-12-13 19:45:58 +0000494 return true;
495}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000496
bsalomonf1b7a1d2015-09-28 06:26:28 -0700497////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000498
bsalomonaa48d362015-10-01 08:34:17 -0700499static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
500 if (primitiveIsSrc) {
501 return SkXfermode::kSrc_Mode != mode;
502 } else {
503 return SkXfermode::kDst_Mode != mode;
504 }
505}
506
bsalomonf1b7a1d2015-09-28 06:26:28 -0700507static inline bool skpaint_to_grpaint_impl(GrContext* context,
508 const SkPaint& skPaint,
509 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -0700510 sk_sp<GrFragmentProcessor>* shaderProcessor,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700511 SkXfermode::Mode* primColorMode,
512 bool primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700513 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700514 GrPaint* grPaint) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000515 grPaint->setAntiAlias(skPaint.isAntiAlias());
brianosman898235c2016-04-06 07:38:23 -0700516 grPaint->setAllowSRGBInputs(allowSRGBInputs);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000517
bsalomonf1b7a1d2015-09-28 06:26:28 -0700518 // Setup the initial color considering the shader, the SkPaint color, and the presence or not
519 // of per-vertex colors.
bungeman06ca8ec2016-06-09 08:01:03 -0700520 sk_sp<GrFragmentProcessor> shaderFP;
bsalomonaa48d362015-10-01 08:34:17 -0700521 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
522 if (shaderProcessor) {
523 shaderFP = *shaderProcessor;
524 } else if (const SkShader* shader = skPaint.getShader()) {
brianosman982eb7f2016-06-06 13:10:58 -0700525 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs
526 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
bungeman06ca8ec2016-06-09 08:01:03 -0700527 shaderFP = shader->asFragmentProcessor(context, viewM, nullptr,
528 skPaint.getFilterQuality(), gammaTreatment);
bsalomonaa48d362015-10-01 08:34:17 -0700529 if (!shaderFP) {
530 return false;
531 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700532 }
533 }
534
535 // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
536 // a known constant value. In that case we can simply apply a color filter during this
537 // conversion without converting the color filter to a GrFragmentProcessor.
538 bool applyColorFilterToPaintColor = false;
539 if (shaderFP) {
540 if (primColorMode) {
541 // There is a blend between the primitive color and the shader color. The shader sees
542 // the opaque paint color. The shader's output is blended using the provided mode by
543 // the primitive color. The blended color is then modulated by the paint's alpha.
544
545 // The geometry processor will insert the primitive color to start the color chain, so
546 // the GrPaint color will be ignored.
547
548 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
549
550 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700551 if (primitiveIsSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700552 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP),
553 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700554 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700555 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP),
556 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700557 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700558 // The above may return null if compose results in a pass through of the prim color.
559 if (shaderFP) {
560 grPaint->addColorFragmentProcessor(shaderFP);
561 }
562
563 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
564 if (GrColor_WHITE != paintAlpha) {
bungeman06ca8ec2016-06-09 08:01:03 -0700565 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
566 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700567 }
568 } else {
569 // The shader's FP sees the paint unpremul color
570 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
bungeman06ca8ec2016-06-09 08:01:03 -0700571 grPaint->addColorFragmentProcessor(std::move(shaderFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700572 }
573 } else {
574 if (primColorMode) {
575 // There is a blend between the primitive color and the paint color. The blend considers
576 // the opaque paint color. The paint's alpha is applied to the post-blended color.
bungeman06ca8ec2016-06-09 08:01:03 -0700577 sk_sp<GrFragmentProcessor> processor(
578 GrConstColorProcessor::Make(SkColorToOpaqueGrColor(skPaint.getColor()),
bsalomonf1b7a1d2015-09-28 06:26:28 -0700579 GrConstColorProcessor::kIgnore_InputMode));
580 if (primitiveIsSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700581 processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(processor),
582 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700583 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700584 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor),
585 *primColorMode);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700586 }
587 if (processor) {
bungeman06ca8ec2016-06-09 08:01:03 -0700588 grPaint->addColorFragmentProcessor(std::move(processor));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700589 }
590
bsalomonaa48d362015-10-01 08:34:17 -0700591 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700592
593 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
bsalomonaa48d362015-10-01 08:34:17 -0700594 if (GrColor_WHITE != paintAlpha) {
bungeman06ca8ec2016-06-09 08:01:03 -0700595 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
596 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonaa48d362015-10-01 08:34:17 -0700597 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700598 } else {
599 // No shader, no primitive color.
600 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
601 applyColorFilterToPaintColor = true;
602 }
603 }
604
605 SkColorFilter* colorFilter = skPaint.getColorFilter();
606 if (colorFilter) {
607 if (applyColorFilterToPaintColor) {
608 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
609 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700610 sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context));
bsalomone25eea42015-09-29 06:38:55 -0700611 if (cfFP) {
bungeman06ca8ec2016-06-09 08:01:03 -0700612 grPaint->addColorFragmentProcessor(std::move(cfFP));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700613 } else {
614 return false;
615 }
616 }
617 }
618
robertphillips4f037942016-02-09 05:09:27 -0800619 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
620 // the GrPaint to also be null (also kSrcOver).
621 SkASSERT(!grPaint->getXPFactory());
622 SkXfermode* xfermode = skPaint.getXfermode();
623 if (xfermode) {
bungeman06ca8ec2016-06-09 08:01:03 -0700624 grPaint->setXPFactory(xfermode->asXPFactory());
robertphillips4f037942016-02-09 05:09:27 -0800625 }
mtklein775b8192014-12-02 09:11:25 -0800626
krajcevskif461a8f2014-06-19 14:14:06 -0700627#ifndef SK_IGNORE_GPU_DITHER
bsalomonac856c92015-08-27 06:30:17 -0700628 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
bungeman06ca8ec2016-06-09 08:01:03 -0700629 grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
krajcevskif461a8f2014-06-19 14:14:06 -0700630 }
631#endif
bsalomonbed83a62015-04-15 14:18:34 -0700632 return true;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000633}
634
bsalomonf1b7a1d2015-09-28 06:26:28 -0700635bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
brianosman898235c2016-04-06 07:38:23 -0700636 bool allowSRGBInputs, GrPaint* grPaint) {
637 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false,
638 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700639}
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000640
bsalomonf1b7a1d2015-09-28 06:26:28 -0700641/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
642bool SkPaintToGrPaintReplaceShader(GrContext* context,
643 const SkPaint& skPaint,
bungeman06ca8ec2016-06-09 08:01:03 -0700644 sk_sp<GrFragmentProcessor> shaderFP,
brianosman898235c2016-04-06 07:38:23 -0700645 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700646 GrPaint* grPaint) {
647 if (!shaderFP) {
bsalomonc21b09e2015-08-28 18:46:56 -0700648 return false;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000649 }
bsalomonf1b7a1d2015-09-28 06:26:28 -0700650 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
brianosman898235c2016-04-06 07:38:23 -0700651 allowSRGBInputs, grPaint);
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000652}
reed8b26b992015-05-07 15:36:17 -0700653
bsalomonf1b7a1d2015-09-28 06:26:28 -0700654/** Ignores the SkShader (if any) on skPaint. */
655bool SkPaintToGrPaintNoShader(GrContext* context,
656 const SkPaint& skPaint,
brianosman898235c2016-04-06 07:38:23 -0700657 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700658 GrPaint* grPaint) {
659 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
bungeman06ca8ec2016-06-09 08:01:03 -0700660 static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr);
661 static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700662 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
brianosman898235c2016-04-06 07:38:23 -0700663 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700664}
665
666/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
667be setup as a vertex attribute using the specified SkXfermode::Mode. */
668bool SkPaintToGrPaintWithXfermode(GrContext* context,
669 const SkPaint& skPaint,
670 const SkMatrix& viewM,
671 SkXfermode::Mode primColorMode,
672 bool primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700673 bool allowSRGBInputs,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700674 GrPaint* grPaint) {
675 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
brianosman898235c2016-04-06 07:38:23 -0700676 allowSRGBInputs, grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700677}
678
joshualitt33a5fce2015-11-18 13:28:51 -0800679bool SkPaintToGrPaintWithTexture(GrContext* context,
680 const SkPaint& paint,
681 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -0700682 sk_sp<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800683 bool textureIsAlphaOnly,
brianosman898235c2016-04-06 07:38:23 -0700684 bool allowSRGBInputs,
joshualitt33a5fce2015-11-18 13:28:51 -0800685 GrPaint* grPaint) {
bungeman06ca8ec2016-06-09 08:01:03 -0700686 sk_sp<GrFragmentProcessor> shaderFP;
joshualitt33a5fce2015-11-18 13:28:51 -0800687 if (textureIsAlphaOnly) {
688 if (const SkShader* shader = paint.getShader()) {
brianosman982eb7f2016-06-06 13:10:58 -0700689 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs
690 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
bungeman06ca8ec2016-06-09 08:01:03 -0700691 shaderFP = shader->asFragmentProcessor(context,
692 viewM,
693 nullptr,
694 paint.getFilterQuality(),
695 gammaTreatment);
joshualitt33a5fce2015-11-18 13:28:51 -0800696 if (!shaderFP) {
697 return false;
698 }
bungeman06ca8ec2016-06-09 08:01:03 -0700699 sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) };
700 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
joshualitt33a5fce2015-11-18 13:28:51 -0800701 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700702 shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
joshualitt33a5fce2015-11-18 13:28:51 -0800703 }
704 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700705 shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
joshualitt33a5fce2015-11-18 13:28:51 -0800706 }
707
bungeman06ca8ec2016-06-09 08:01:03 -0700708 return SkPaintToGrPaintReplaceShader(context, paint, std::move(shaderFP), allowSRGBInputs,
709 grPaint);
joshualitt33a5fce2015-11-18 13:28:51 -0800710}
711
bsalomonf1b7a1d2015-09-28 06:26:28 -0700712
713////////////////////////////////////////////////////////////////////////////////////////////////
714
reed8b26b992015-05-07 15:36:17 -0700715SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
716#ifdef SK_DEBUG
717 const GrSurfaceDesc& desc = tex->desc();
718 SkASSERT(w <= desc.fWidth);
719 SkASSERT(h <= desc.fHeight);
720#endif
721 const GrPixelConfig config = tex->config();
722 SkColorType ct;
723 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
halcanary96fcdcc2015-08-27 07:41:13 -0700724 if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
reed8b26b992015-05-07 15:36:17 -0700725 ct = kUnknown_SkColorType;
726 }
727 return SkImageInfo::Make(w, h, ct, at);
728}
729
730
731void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
732 const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
733 dst->setInfo(info);
halcanary385fe4d2015-08-26 13:07:48 -0700734 dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
reed8b26b992015-05-07 15:36:17 -0700735}
joshualitt9bc39542015-08-12 12:57:54 -0700736
737GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
738 const SkMatrix& viewM,
739 const SkMatrix& localM,
740 bool* doBicubic) {
741 *doBicubic = false;
742 GrTextureParams::FilterMode textureFilterMode;
743 switch (paintFilterQuality) {
744 case kNone_SkFilterQuality:
745 textureFilterMode = GrTextureParams::kNone_FilterMode;
746 break;
747 case kLow_SkFilterQuality:
748 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
749 break;
750 case kMedium_SkFilterQuality: {
751 SkMatrix matrix;
752 matrix.setConcat(viewM, localM);
753 if (matrix.getMinScale() < SK_Scalar1) {
754 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
755 } else {
756 // Don't trigger MIP level generation unnecessarily.
757 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
758 }
759 break;
760 }
761 case kHigh_SkFilterQuality: {
762 SkMatrix matrix;
763 matrix.setConcat(viewM, localM);
764 *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
765 break;
766 }
767 default:
768 SkErrorInternals::SetError( kInvalidPaint_SkError,
769 "Sorry, I don't understand the filtering "
770 "mode you asked for. Falling back to "
771 "MIPMaps.");
772 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
773 break;
774
775 }
776 return textureFilterMode;
777}