reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 8 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "SkGr.h" |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 10 | #include "SkGrPriv.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 11 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 15 | #include "GrGpuResourcePriv.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 16 | #include "GrImageIDTextureAdjuster.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 17 | #include "GrTextureParamsAdjuster.h" |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 18 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 19 | #include "GrTypes.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 20 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 21 | #include "GrYUVProvider.h" |
| 22 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 23 | #include "SkBlendModePriv.h" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 24 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 25 | #include "SkConfig8888.h" |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 26 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 27 | #include "SkData.h" |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 28 | #include "SkMaskFilter.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 29 | #include "SkMessageBus.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 30 | #include "SkMipMap.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 31 | #include "SkPixelRef.h" |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 32 | #include "SkPM4fPriv.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 33 | #include "SkResourceCache.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 34 | #include "SkTemplates.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 35 | #include "SkYUVPlanesCache.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 36 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 37 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 38 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 39 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 40 | #include "effects/GrXfermodeFragmentProcessor.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 41 | #include "effects/GrYUVEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 42 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 43 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 44 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 45 | # include "etc1.h" |
| 46 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 48 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 49 | GrSurfaceDesc desc; |
| 50 | desc.fFlags = kNone_GrSurfaceFlags; |
| 51 | desc.fWidth = info.width(); |
| 52 | desc.fHeight = info.height(); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 53 | desc.fConfig = SkImageInfo2GrPixelConfig(info, caps); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 54 | desc.fSampleCnt = 0; |
| 55 | return desc; |
| 56 | } |
| 57 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 58 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) { |
| 59 | SkASSERT(key); |
| 60 | SkASSERT(imageID); |
| 61 | SkASSERT(!imageBounds.isEmpty()); |
| 62 | static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain(); |
| 63 | GrUniqueKey::Builder builder(key, kImageIDDomain, 5); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 64 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 65 | builder[1] = imageBounds.fLeft; |
| 66 | builder[2] = imageBounds.fTop; |
| 67 | builder[3] = imageBounds.fRight; |
| 68 | builder[4] = imageBounds.fBottom; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
| 72 | int expectedW, int expectedH, |
| 73 | const void** outStartOfDataToUpload) { |
| 74 | *outStartOfDataToUpload = nullptr; |
| 75 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 76 | if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { |
| 77 | return kUnknown_GrPixelConfig; |
| 78 | } |
| 79 | |
| 80 | const uint8_t* bytes = data->bytes(); |
| 81 | if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) { |
| 82 | // Does the data match the dimensions of the bitmap? If not, |
| 83 | // then we don't know how to scale the image to match it... |
| 84 | if (etc1_pkm_get_width(bytes) != (unsigned)expectedW || |
| 85 | etc1_pkm_get_height(bytes) != (unsigned)expectedH) |
| 86 | { |
| 87 | return kUnknown_GrPixelConfig; |
| 88 | } |
| 89 | |
| 90 | *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE; |
| 91 | return kETC1_GrPixelConfig; |
scroggo | b8e0960 | 2016-04-12 07:41:22 -0700 | [diff] [blame] | 92 | } else if (SkKTXFile::is_ktx(bytes, data->size())) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 93 | SkKTXFile ktx(data); |
| 94 | |
| 95 | // Is it actually an ETC1 texture? |
| 96 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
| 97 | return kUnknown_GrPixelConfig; |
| 98 | } |
| 99 | |
| 100 | // Does the data match the dimensions of the bitmap? If not, |
| 101 | // then we don't know how to scale the image to match it... |
| 102 | if (ktx.width() != expectedW || ktx.height() != expectedH) { |
| 103 | return kUnknown_GrPixelConfig; |
| 104 | } |
| 105 | |
| 106 | *outStartOfDataToUpload = ktx.pixelData(); |
| 107 | return kETC1_GrPixelConfig; |
| 108 | } |
| 109 | #endif |
| 110 | return kUnknown_GrPixelConfig; |
| 111 | } |
| 112 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 113 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 114 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Fill out buffer with the compressed format Ganesh expects from a colortable |
| 117 | * based bitmap. [palette (colortable) + indices]. |
| 118 | * |
| 119 | * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
| 120 | * we could detect that the colortable.count is <= 16, and then repack the |
| 121 | * indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 122 | * slower than memcpy), so skipping that for now. |
| 123 | * |
| 124 | * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 125 | * as the colortable.count says it is. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | */ |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 127 | static void build_index8_data(void* buffer, const SkPixmap& pixmap) { |
| 128 | SkASSERT(kIndex_8_SkColorType == pixmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 129 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 130 | const SkColorTable* ctable = pixmap.ctable(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 132 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 133 | const int count = ctable->count(); |
| 134 | |
| 135 | SkDstPixelInfo dstPI; |
| 136 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 137 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 138 | dstPI.fPixels = buffer; |
| 139 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 140 | |
| 141 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 142 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 143 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 144 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 145 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 146 | |
| 147 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 148 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | // always skip a full 256 number of entries, even if we memcpy'd fewer |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 150 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 152 | if ((unsigned)pixmap.width() == pixmap.rowBytes()) { |
| 153 | memcpy(dst, pixmap.addr(), pixmap.getSafeSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | } else { |
| 155 | // need to trim off the extra bytes per row |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 156 | size_t width = pixmap.width(); |
| 157 | size_t rowBytes = pixmap.rowBytes(); |
| 158 | const uint8_t* src = pixmap.addr8(); |
| 159 | for (int y = 0; y < pixmap.height(); y++) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | memcpy(dst, src, width); |
| 161 | src += rowBytes; |
| 162 | dst += width; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 167 | /** |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 168 | * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will |
| 169 | * be gone from SkPixelRef, and we can remove this subclass entirely. |
| 170 | */ |
| 171 | class PixelRef_GrYUVProvider : public GrYUVProvider { |
| 172 | SkPixelRef* fPR; |
| 173 | |
| 174 | public: |
| 175 | PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {} |
| 176 | |
| 177 | uint32_t onGetID() override { return fPR->getGenerationID(); } |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 178 | bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override { |
| 179 | return fPR->queryYUV8(sizeInfo, colorSpace); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 180 | } |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 181 | bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override { |
| 182 | return fPR->getYUV8Planes(sizeInfo, planes); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 183 | } |
| 184 | }; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 185 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 186 | static sk_sp<GrTexture> create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm, |
| 187 | const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 188 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 189 | SkPixelRef* pixelRef = bm.pixelRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 190 | if ((nullptr == pixelRef) || |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 191 | (pixelRef->info().width() != bm.info().width()) || |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 192 | (pixelRef->info().height() != bm.info().height())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | return nullptr; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 194 | } |
| 195 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 196 | PixelRef_GrYUVProvider provider(pixelRef); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 197 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 198 | return provider.refAsTexture(ctx, desc, !bm.isVolatile()); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 199 | } |
| 200 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 201 | static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) { |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 202 | sk_sp<SkData> data(bm.pixelRef()->refEncodedData()); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 203 | if (!data) { |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | const void* startOfTexData; |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 208 | desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data.get(), bm.width(), bm.height(), |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 209 | &startOfTexData); |
| 210 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 214 | return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 217 | GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 218 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps()); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 219 | if (GrTexture *texture = load_etc1_texture(ctx, bitmap, desc)) { |
| 220 | return texture; |
| 221 | } |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 222 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 223 | sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc)); |
| 224 | if (texture) { |
| 225 | return texture.release(); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | SkAutoLockPixels alp(bitmap); |
| 229 | if (!bitmap.readyToDraw()) { |
| 230 | return nullptr; |
| 231 | } |
| 232 | SkPixmap pixmap; |
| 233 | if (!bitmap.peekPixels(&pixmap)) { |
| 234 | return nullptr; |
| 235 | } |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 236 | return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 237 | } |
| 238 | |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 239 | GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 240 | const SkPixmap* pmap = &pixmap; |
| 241 | SkPixmap tmpPixmap; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 242 | SkBitmap tmpBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 243 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 244 | const GrCaps* caps = ctx->caps(); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 245 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 246 | |
brianosman | efded51 | 2016-07-26 08:11:50 -0700 | [diff] [blame] | 247 | if (caps->srgbSupport() && |
| 248 | pixmap.info().colorSpace() && pixmap.info().colorSpace()->gammaCloseToSRGB() && |
| 249 | !(GrPixelConfigIsSRGB(desc.fConfig) || |
| 250 | kRGBA_half_GrPixelConfig == desc.fConfig || |
| 251 | kRGBA_float_GrPixelConfig == desc.fConfig)) { |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 252 | // We were supplied an sRGB-like color space, but we don't have a suitable pixel config. |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 253 | // Convert to 8888 sRGB so we can handle the data correctly. The raster backend doesn't |
| 254 | // handle sRGB Index8 -> sRGB 8888 correctly (yet), so lie about both the source and |
| 255 | // destination (claim they're linear): |
| 256 | SkImageInfo linSrcInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(), |
| 257 | pixmap.colorType(), pixmap.alphaType()); |
| 258 | SkPixmap linSrcPixmap(linSrcInfo, pixmap.addr(), pixmap.rowBytes(), pixmap.ctable()); |
| 259 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 260 | SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(), |
| 261 | kN32_SkColorType, kPremul_SkAlphaType, |
| 262 | sk_ref_sp(pixmap.info().colorSpace())); |
| 263 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 264 | tmpBitmap.allocPixels(dstInfo); |
| 265 | |
| 266 | SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
| 267 | if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 268 | return nullptr; |
| 269 | } |
| 270 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | pmap = &tmpPixmap; |
| 274 | // must rebuild desc, since we've forced the info to be N32 |
| 275 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
brianosman | 85f9269 | 2016-03-24 06:56:32 -0700 | [diff] [blame] | 276 | } else if (kGray_8_SkColorType == pixmap.colorType()) { |
| 277 | // We don't have Gray8 support as a pixel config, so expand to 8888 |
| 278 | |
| 279 | // We should have converted sRGB Gray8 above (if we have sRGB support): |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 280 | SkASSERT(!caps->srgbSupport() || !pixmap.info().colorSpace() || |
| 281 | !pixmap.info().colorSpace()->gammaCloseToSRGB()); |
brianosman | 85f9269 | 2016-03-24 06:56:32 -0700 | [diff] [blame] | 282 | |
| 283 | SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(), |
| 284 | kOpaque_SkAlphaType); |
| 285 | tmpBitmap.allocPixels(info); |
| 286 | if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | pmap = &tmpPixmap; |
| 293 | // must rebuild desc, since we've forced the info to be N32 |
| 294 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 295 | } else if (kIndex_8_SkColorType == pixmap.colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 296 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 297 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 298 | pixmap.width(), pixmap.height()); |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 299 | SkAutoMalloc storage(imageSize); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 300 | build_index8_data(storage.get(), pixmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | |
| 302 | // our compressed data will be trimmed, so pass width() for its |
| 303 | // "rowBytes", since they are the same now. |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 304 | return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(), |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 305 | pixmap.width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 306 | } else { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 307 | SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
| 308 | tmpBitmap.allocPixels(info); |
| 309 | if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 310 | return nullptr; |
| 311 | } |
| 312 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 313 | return nullptr; |
| 314 | } |
| 315 | pmap = &tmpPixmap; |
| 316 | // must rebuild desc, since we've forced the info to be N32 |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 317 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 318 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 319 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 321 | return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(), |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 322 | pmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 325 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 326 | //////////////////////////////////////////////////////////////////////////////// |
| 327 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 328 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 329 | class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 330 | public: |
| 331 | explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 332 | private: |
| 333 | GrUniqueKeyInvalidatedMessage fMsg; |
| 334 | |
| 335 | void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 336 | }; |
| 337 | |
| 338 | pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 339 | } |
| 340 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 341 | GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 342 | SkDestinationSurfaceColorMode colorMode) |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 343 | { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 344 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 345 | if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) { |
| 346 | GrTexture* texture = load_etc1_texture(ctx, bitmap, desc); |
| 347 | if (texture) { |
| 348 | return texture; |
| 349 | } |
| 350 | } |
| 351 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 352 | sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 353 | if (texture) { |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 354 | return texture.release(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 355 | } |
| 356 | |
brianosman | 7e5e594 | 2016-06-21 12:08:24 -0700 | [diff] [blame] | 357 | // We don't support Gray8 directly in the GL backend, so fail-over to GrUploadBitmapToTexture. |
| 358 | // That will transform the Gray8 to 8888, then use the driver/GPU to build mipmaps. If we build |
| 359 | // the mips on the CPU here, they'll all be Gray8, which isn't useful. (They get treated as A8). |
| 360 | // TODO: A better option might be to transform the initial bitmap here to 8888, then run the |
| 361 | // CPU mip-mapper on that data before uploading. This is much less code for a rare case though: |
| 362 | if (kGray_8_SkColorType == bitmap.colorType()) { |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 366 | SkASSERT(sizeof(int) <= sizeof(uint32_t)); |
| 367 | if (bitmap.width() < 0 || bitmap.height() < 0) { |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
| 371 | SkAutoPixmapUnlock srcUnlocker; |
| 372 | if (!bitmap.requestLock(&srcUnlocker)) { |
| 373 | return nullptr; |
| 374 | } |
| 375 | const SkPixmap& pixmap = srcUnlocker.pixmap(); |
| 376 | // Try to catch where we might have returned nullptr for src crbug.com/492818 |
| 377 | if (nullptr == pixmap.addr()) { |
| 378 | sk_throw(); |
| 379 | } |
| 380 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 381 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 382 | if (!mipmaps) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | |
| 386 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 387 | if (mipLevelCount < 1) { |
| 388 | return nullptr; |
| 389 | } |
| 390 | |
| 391 | const bool isMipMapped = mipLevelCount > 1; |
| 392 | desc.fIsMipMapped = isMipMapped; |
| 393 | |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 394 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 395 | |
| 396 | texels[0].fPixels = pixmap.addr(); |
| 397 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 398 | |
| 399 | for (int i = 1; i < mipLevelCount; ++i) { |
| 400 | SkMipMap::Level generatedMipLevel; |
| 401 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 402 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 403 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 404 | } |
| 405 | |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 406 | { |
| 407 | GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc, |
| 408 | SkBudgeted::kYes, |
| 409 | texels.get(), |
| 410 | mipLevelCount); |
brianosman | 7d2f607 | 2016-06-17 07:03:45 -0700 | [diff] [blame] | 411 | if (texture) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 412 | texture->texturePriv().setMipColorMode(colorMode); |
brianosman | 7d2f607 | 2016-06-17 07:03:45 -0700 | [diff] [blame] | 413 | } |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 414 | return texture; |
| 415 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 416 | } |
| 417 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 418 | GrTexture* GrUploadMipMapToTexture(GrContext* ctx, const SkImageInfo& info, |
| 419 | const GrMipLevel* texels, int mipLevelCount) { |
| 420 | const GrCaps* caps = ctx->caps(); |
| 421 | return ctx->textureProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps), |
| 422 | SkBudgeted::kYes, texels, |
| 423 | mipLevelCount); |
| 424 | } |
| 425 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 426 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 427 | const GrTextureParams& params, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 428 | SkDestinationSurfaceColorMode colorMode) { |
| 429 | return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, colorMode); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 430 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 431 | |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 432 | sk_sp<GrTexture> GrMakeCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
| 433 | const GrTextureParams& params, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 434 | SkDestinationSurfaceColorMode colorMode) { |
| 435 | GrTexture* tex = GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, colorMode); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 436 | return sk_sp<GrTexture>(tex); |
| 437 | } |
| 438 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 439 | /////////////////////////////////////////////////////////////////////////////// |
| 440 | |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 441 | GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) { |
| 442 | // We want to premultiply after linearizing, so this is easy: |
| 443 | return SkColorToUnpremulGrColor4f(c, dstColorSpace).premul(); |
| 444 | } |
| 445 | |
| 446 | GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace) { |
| 447 | if (dstColorSpace) { |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 448 | auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
Brian Osman | 72ae431 | 2016-10-20 16:53:45 -0400 | [diff] [blame] | 449 | auto gamutXform = GrColorSpaceXform::Make(srgbColorSpace.get(), dstColorSpace); |
| 450 | return SkColorToUnpremulGrColor4f(c, true, gamutXform.get()); |
| 451 | } else { |
| 452 | return SkColorToUnpremulGrColor4f(c, false, nullptr); |
| 453 | } |
| 454 | } |
| 455 | |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 456 | GrColor4f SkColorToPremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) { |
| 457 | // We want to premultiply after linearizing, so this is easy: |
| 458 | return SkColorToUnpremulGrColor4f(c, gammaCorrect, gamutXform).premul(); |
| 459 | } |
| 460 | |
| 461 | GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) { |
| 462 | // You can't be color-space aware in legacy mode |
| 463 | SkASSERT(gammaCorrect || !gamutXform); |
| 464 | |
| 465 | GrColor4f color; |
| 466 | if (gammaCorrect) { |
| 467 | // SkColor4f::FromColor does sRGB -> Linear |
| 468 | color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c)); |
| 469 | } else { |
| 470 | // GrColor4f::FromGrColor just multiplies by 1/255 |
| 471 | color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c)); |
| 472 | } |
| 473 | |
| 474 | if (gamutXform) { |
| 475 | color = gamutXform->apply(color); |
| 476 | } |
| 477 | |
| 478 | return color; |
| 479 | } |
| 480 | |
| 481 | /////////////////////////////////////////////////////////////////////////////// |
| 482 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 483 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 484 | // alpha info, that will be considered. |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 485 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, const SkColorSpace* cs, |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 486 | const GrCaps& caps) { |
| 487 | // We intentionally ignore profile type for non-8888 formats. Anything we can't support |
| 488 | // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture. |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 489 | switch (ct) { |
| 490 | case kUnknown_SkColorType: |
| 491 | return kUnknown_GrPixelConfig; |
| 492 | case kAlpha_8_SkColorType: |
| 493 | return kAlpha_8_GrPixelConfig; |
| 494 | case kRGB_565_SkColorType: |
| 495 | return kRGB_565_GrPixelConfig; |
| 496 | case kARGB_4444_SkColorType: |
| 497 | return kRGBA_4444_GrPixelConfig; |
| 498 | case kRGBA_8888_SkColorType: |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 499 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 500 | ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 501 | case kBGRA_8888_SkColorType: |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 502 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 503 | ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 504 | case kIndex_8_SkColorType: |
| 505 | return kIndex_8_GrPixelConfig; |
| 506 | case kGray_8_SkColorType: |
| 507 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
| 508 | case kRGBA_F16_SkColorType: |
| 509 | return kRGBA_half_GrPixelConfig; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 510 | } |
| 511 | SkASSERT(0); // shouldn't get here |
| 512 | return kUnknown_GrPixelConfig; |
| 513 | } |
| 514 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 515 | bool GrPixelConfigToColorType(GrPixelConfig config, SkColorType* ctOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 516 | SkColorType ct; |
| 517 | switch (config) { |
| 518 | case kAlpha_8_GrPixelConfig: |
| 519 | ct = kAlpha_8_SkColorType; |
| 520 | break; |
| 521 | case kIndex_8_GrPixelConfig: |
| 522 | ct = kIndex_8_SkColorType; |
| 523 | break; |
| 524 | case kRGB_565_GrPixelConfig: |
| 525 | ct = kRGB_565_SkColorType; |
| 526 | break; |
| 527 | case kRGBA_4444_GrPixelConfig: |
| 528 | ct = kARGB_4444_SkColorType; |
| 529 | break; |
| 530 | case kRGBA_8888_GrPixelConfig: |
| 531 | ct = kRGBA_8888_SkColorType; |
| 532 | break; |
| 533 | case kBGRA_8888_GrPixelConfig: |
| 534 | ct = kBGRA_8888_SkColorType; |
| 535 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 536 | case kSRGBA_8888_GrPixelConfig: |
| 537 | ct = kRGBA_8888_SkColorType; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 538 | break; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 539 | case kSBGRA_8888_GrPixelConfig: |
| 540 | ct = kBGRA_8888_SkColorType; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 541 | break; |
brianosman | 9ac5b91 | 2016-04-12 13:49:53 -0700 | [diff] [blame] | 542 | case kRGBA_half_GrPixelConfig: |
| 543 | ct = kRGBA_F16_SkColorType; |
| 544 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 545 | default: |
| 546 | return false; |
| 547 | } |
| 548 | if (ctOut) { |
| 549 | *ctOut = ct; |
| 550 | } |
| 551 | return true; |
| 552 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 553 | |
brianosman | 27a3aa5 | 2016-09-23 07:11:34 -0700 | [diff] [blame] | 554 | GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace* colorSpace) { |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 555 | if (!colorSpace) { |
| 556 | return kRGBA_8888_GrPixelConfig; |
| 557 | } else if (colorSpace->gammaIsLinear()) { |
| 558 | return kRGBA_half_GrPixelConfig; |
| 559 | } else if (colorSpace->gammaCloseToSRGB()) { |
| 560 | return kSRGBA_8888_GrPixelConfig; |
| 561 | } else { |
| 562 | SkDEBUGFAIL("No renderable config exists for color space with strange gamma"); |
| 563 | return kUnknown_GrPixelConfig; |
| 564 | } |
| 565 | } |
| 566 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 567 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 568 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 569 | static inline bool blend_requires_shader(const SkBlendMode mode, bool primitiveIsSrc) { |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 570 | if (primitiveIsSrc) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 571 | return SkBlendMode::kSrc != mode; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 572 | } else { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 573 | return SkBlendMode::kDst != mode; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 577 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 578 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 579 | const SkPaint& skPaint, |
| 580 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 581 | sk_sp<GrFragmentProcessor>* shaderProcessor, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 582 | SkBlendMode* primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 583 | bool primitiveIsSrc, |
| 584 | GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 585 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 586 | grPaint->setAllowSRGBInputs(rtc->isGammaCorrect()); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 587 | |
Brian Osman | c68d4aa | 2016-09-30 11:41:59 -0400 | [diff] [blame] | 588 | // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 589 | GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), rtc->isGammaCorrect(), |
| 590 | rtc->getColorXformFromSRGB()); |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 591 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 592 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 593 | // of per-vertex colors. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 594 | sk_sp<GrFragmentProcessor> shaderFP; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 595 | if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { |
| 596 | if (shaderProcessor) { |
| 597 | shaderFP = *shaderProcessor; |
| 598 | } else if (const SkShader* shader = skPaint.getShader()) { |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 599 | shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context, &viewM, nullptr, |
| 600 | skPaint.getFilterQuality(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 601 | rtc->getColorSpace(), |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 602 | rtc->colorMode())); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 603 | if (!shaderFP) { |
| 604 | return false; |
| 605 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 610 | // a known constant value. In that case we can simply apply a color filter during this |
| 611 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 612 | bool applyColorFilterToPaintColor = false; |
| 613 | if (shaderFP) { |
| 614 | if (primColorMode) { |
| 615 | // There is a blend between the primitive color and the shader color. The shader sees |
| 616 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 617 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 618 | |
| 619 | // The geometry processor will insert the primitive color to start the color chain, so |
| 620 | // the GrPaint color will be ignored. |
| 621 | |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 622 | GrColor4f shaderInput = origColor.opaque(); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 623 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 624 | if (primitiveIsSrc) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 625 | shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP), |
| 626 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 627 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 628 | shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP), |
| 629 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 630 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 631 | // The above may return null if compose results in a pass through of the prim color. |
| 632 | if (shaderFP) { |
| 633 | grPaint->addColorFragmentProcessor(shaderFP); |
| 634 | } |
| 635 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 636 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 637 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 638 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 639 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 640 | // color channels. It's value should be treated as the same in ANY color space. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 641 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 642 | GrColor4f::FromGrColor(paintAlpha), |
| 643 | GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 644 | } |
| 645 | } else { |
| 646 | // The shader's FP sees the paint unpremul color |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 647 | grPaint->setColor4f(origColor); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 648 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 649 | } |
| 650 | } else { |
| 651 | if (primColorMode) { |
| 652 | // There is a blend between the primitive color and the paint color. The blend considers |
| 653 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 654 | sk_sp<GrFragmentProcessor> processor( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 655 | GrConstColorProcessor::Make(origColor.opaque(), |
| 656 | GrConstColorProcessor::kIgnore_InputMode)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 657 | if (primitiveIsSrc) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 658 | processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(processor), |
| 659 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 660 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 661 | processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor), |
| 662 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 663 | } |
| 664 | if (processor) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 665 | grPaint->addColorFragmentProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 666 | } |
| 667 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 668 | grPaint->setColor4f(origColor.opaque()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 669 | |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 670 | // We can ignore origColor here - alpha is unchanged by gamma |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 671 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 672 | if (GrColor_WHITE != paintAlpha) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 673 | // No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 674 | // color channels. It's value should be treated as the same in ANY color space. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 675 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 676 | GrColor4f::FromGrColor(paintAlpha), |
| 677 | GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 678 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 679 | } else { |
| 680 | // No shader, no primitive color. |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 681 | grPaint->setColor4f(origColor.premul()); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 682 | applyColorFilterToPaintColor = true; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 687 | if (colorFilter) { |
| 688 | if (applyColorFilterToPaintColor) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 689 | // If we're in legacy mode, we *must* avoid using the 4f version of the color filter, |
| 690 | // because that will combine with the linearized version of the stored color. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 691 | if (rtc->isGammaCorrect()) { |
Brian Osman | 8bf4e67 | 2016-10-17 16:54:49 -0400 | [diff] [blame] | 692 | grPaint->setColor4f(GrColor4f::FromSkColor4f( |
| 693 | colorFilter->filterColor4f(origColor.toSkColor4f())).premul()); |
| 694 | } else { |
| 695 | grPaint->setColor4f(SkColorToPremulGrColor4f( |
| 696 | colorFilter->filterColor(skPaint.getColor()), false, nullptr)); |
| 697 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 698 | } else { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 699 | sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 700 | rtc->getColorSpace())); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 701 | if (cfFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 702 | grPaint->addColorFragmentProcessor(std::move(cfFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 703 | } else { |
| 704 | return false; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 709 | SkMaskFilter* maskFilter = skPaint.getMaskFilter(); |
| 710 | if (maskFilter) { |
| 711 | GrFragmentProcessor* mfFP; |
| 712 | if (maskFilter->asFragmentProcessor(&mfFP, nullptr, viewM)) { |
| 713 | grPaint->addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor>(mfFP)); |
| 714 | } |
| 715 | } |
| 716 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 717 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 718 | // the GrPaint to also be null (also kSrcOver). |
| 719 | SkASSERT(!grPaint->getXPFactory()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 720 | if (!skPaint.isSrcOver()) { |
| 721 | grPaint->setXPFactory(SkBlendMode_AsXPFactory(skPaint.getBlendMode())); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 722 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 723 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 724 | #ifndef SK_IGNORE_GPU_DITHER |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 725 | if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0 && !rtc->isGammaCorrect()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 726 | grPaint->addColorFragmentProcessor(GrDitherEffect::Make()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 727 | } |
| 728 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 729 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 732 | bool SkPaintToGrPaint(GrContext* context, GrRenderTargetContext* rtc, const SkPaint& skPaint, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 733 | const SkMatrix& viewM, GrPaint* grPaint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 734 | return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, nullptr, false, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 735 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 736 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 737 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 738 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 739 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 740 | const SkPaint& skPaint, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 741 | sk_sp<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 742 | GrPaint* grPaint) { |
| 743 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 744 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 745 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 746 | return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), &shaderFP, nullptr, false, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 747 | grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 748 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 749 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 750 | /** Ignores the SkShader (if any) on skPaint. */ |
| 751 | bool SkPaintToGrPaintNoShader(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 752 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 753 | const SkPaint& skPaint, |
| 754 | GrPaint* grPaint) { |
| 755 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 756 | static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr); |
| 757 | static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 758 | return skpaint_to_grpaint_impl(context, rtc, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 759 | false, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 763 | be setup as a vertex attribute using the specified SkBlendMode. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 764 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 765 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 766 | const SkPaint& skPaint, |
| 767 | const SkMatrix& viewM, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 768 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 769 | bool primitiveIsSrc, |
| 770 | GrPaint* grPaint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 771 | return skpaint_to_grpaint_impl(context, rtc, skPaint, viewM, nullptr, &primColorMode, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 772 | primitiveIsSrc, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 773 | } |
| 774 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 775 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 776 | GrRenderTargetContext* rtc, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 777 | const SkPaint& paint, |
| 778 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 779 | sk_sp<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 780 | bool textureIsAlphaOnly, |
| 781 | GrPaint* grPaint) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 782 | sk_sp<GrFragmentProcessor> shaderFP; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 783 | if (textureIsAlphaOnly) { |
| 784 | if (const SkShader* shader = paint.getShader()) { |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 785 | shaderFP = shader->asFragmentProcessor(SkShader::AsFPArgs(context, |
| 786 | &viewM, |
| 787 | nullptr, |
| 788 | paint.getFilterQuality(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 789 | rtc->getColorSpace(), |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 790 | rtc->colorMode())); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 791 | if (!shaderFP) { |
| 792 | return false; |
| 793 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 794 | sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) }; |
| 795 | shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 796 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 797 | shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 798 | } |
| 799 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 800 | shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 803 | return SkPaintToGrPaintReplaceShader(context, rtc, paint, std::move(shaderFP), grPaint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 804 | } |
| 805 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 806 | |
| 807 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 808 | |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 809 | GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 810 | const SkMatrix& viewM, |
| 811 | const SkMatrix& localM, |
| 812 | bool* doBicubic) { |
| 813 | *doBicubic = false; |
| 814 | GrTextureParams::FilterMode textureFilterMode; |
| 815 | switch (paintFilterQuality) { |
| 816 | case kNone_SkFilterQuality: |
| 817 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 818 | break; |
| 819 | case kLow_SkFilterQuality: |
| 820 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 821 | break; |
| 822 | case kMedium_SkFilterQuality: { |
| 823 | SkMatrix matrix; |
| 824 | matrix.setConcat(viewM, localM); |
| 825 | if (matrix.getMinScale() < SK_Scalar1) { |
| 826 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 827 | } else { |
| 828 | // Don't trigger MIP level generation unnecessarily. |
| 829 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 830 | } |
| 831 | break; |
| 832 | } |
| 833 | case kHigh_SkFilterQuality: { |
| 834 | SkMatrix matrix; |
| 835 | matrix.setConcat(viewM, localM); |
| 836 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 837 | break; |
| 838 | } |
| 839 | default: |
Mike Klein | e54c75f | 2016-10-13 14:18:09 -0400 | [diff] [blame] | 840 | // Should be unreachable. If not, fall back to mipmaps. |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 841 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 842 | break; |
| 843 | |
| 844 | } |
| 845 | return textureFilterMode; |
| 846 | } |