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" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 15 | #include "GrImageIDTextureAdjuster.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 16 | #include "GrTextureParamsAdjuster.h" |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 17 | #include "GrTexturePriv.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 18 | #include "GrTypes.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 19 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 20 | #include "GrYUVProvider.h" |
| 21 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 22 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 23 | #include "SkConfig8888.h" |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 24 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 25 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 26 | #include "SkErrorInternals.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 27 | #include "SkGrPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 28 | #include "SkMessageBus.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 29 | #include "SkMipMap.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 30 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 31 | #include "SkResourceCache.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 32 | #include "SkTemplates.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 33 | #include "SkYUVPlanesCache.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 34 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 35 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 36 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 37 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 38 | #include "effects/GrXfermodeFragmentProcessor.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 39 | #include "effects/GrYUVEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 40 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 41 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 42 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 43 | # include "etc1.h" |
| 44 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 46 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info, const GrCaps& caps) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 47 | GrSurfaceDesc desc; |
| 48 | desc.fFlags = kNone_GrSurfaceFlags; |
| 49 | desc.fWidth = info.width(); |
| 50 | desc.fHeight = info.height(); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 51 | desc.fConfig = SkImageInfo2GrPixelConfig(info, caps); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 52 | desc.fSampleCnt = 0; |
| 53 | return desc; |
| 54 | } |
| 55 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 56 | void 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); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 62 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 63 | builder[1] = imageBounds.fLeft; |
| 64 | builder[2] = imageBounds.fTop; |
| 65 | builder[3] = imageBounds.fRight; |
| 66 | builder[4] = imageBounds.fBottom; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | GrPixelConfig 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; |
scroggo | b8e0960 | 2016-04-12 07:41:22 -0700 | [diff] [blame] | 90 | } else if (SkKTXFile::is_ktx(bytes, data->size())) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 91 | 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 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 111 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 112 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 113 | /** |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | */ |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 125 | static void build_index8_data(void* buffer, const SkPixmap& pixmap) { |
| 126 | SkASSERT(kIndex_8_SkColorType == pixmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 127 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 128 | const SkColorTable* ctable = pixmap.ctable(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 130 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 131 | 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.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 140 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 141 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 142 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 143 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 144 | |
| 145 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 146 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | // 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] | 148 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 150 | if ((unsigned)pixmap.width() == pixmap.rowBytes()) { |
| 151 | memcpy(dst, pixmap.addr(), pixmap.getSafeSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | } else { |
| 153 | // need to trim off the extra bytes per row |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 154 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | memcpy(dst, src, width); |
| 159 | src += rowBytes; |
| 160 | dst += width; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 165 | /** |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 166 | * 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 | */ |
| 169 | class PixelRef_GrYUVProvider : public GrYUVProvider { |
| 170 | SkPixelRef* fPR; |
| 171 | |
| 172 | public: |
| 173 | PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {} |
| 174 | |
| 175 | uint32_t onGetID() override { return fPR->getGenerationID(); } |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 176 | bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override { |
| 177 | return fPR->queryYUV8(sizeInfo, colorSpace); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 178 | } |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 179 | bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override { |
| 180 | return fPR->getYUV8Planes(sizeInfo, planes); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 181 | } |
| 182 | }; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 183 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 184 | static sk_sp<GrTexture> create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm, |
| 185 | const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 186 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 187 | SkPixelRef* pixelRef = bm.pixelRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 188 | if ((nullptr == pixelRef) || |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 189 | (pixelRef->info().width() != bm.info().width()) || |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 190 | (pixelRef->info().height() != bm.info().height())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 191 | return nullptr; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 194 | PixelRef_GrYUVProvider provider(pixelRef); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 195 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 196 | return provider.refAsTexture(ctx, desc, !bm.isVolatile()); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 199 | static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 200 | 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 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 212 | return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 213 | } |
| 214 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 215 | GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bitmap) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 216 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps()); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 217 | if (GrTexture *texture = load_etc1_texture(ctx, bitmap, desc)) { |
| 218 | return texture; |
| 219 | } |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 220 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 221 | sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc)); |
| 222 | if (texture) { |
| 223 | return texture.release(); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 224 | } |
| 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 | } |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 234 | return GrUploadPixmapToTexture(ctx, pixmap, SkBudgeted::kYes); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 235 | } |
| 236 | |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 237 | GrTexture* GrUploadPixmapToTexture(GrContext* ctx, const SkPixmap& pixmap, SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 238 | const SkPixmap* pmap = &pixmap; |
| 239 | SkPixmap tmpPixmap; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 240 | SkBitmap tmpBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 241 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 242 | const GrCaps* caps = ctx->caps(); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 243 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info(), *caps); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 244 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 245 | if (caps->srgbSupport() && !GrPixelConfigIsSRGB(desc.fConfig) && |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 246 | pixmap.info().colorSpace() && pixmap.info().colorSpace()->gammaCloseToSRGB()) { |
| 247 | // 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] | 248 | // 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 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 255 | SkImageInfo dstInfo = SkImageInfo::Make(pixmap.width(), pixmap.height(), |
| 256 | kN32_SkColorType, kPremul_SkAlphaType, |
| 257 | sk_ref_sp(pixmap.info().colorSpace())); |
| 258 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 259 | tmpBitmap.allocPixels(dstInfo); |
| 260 | |
| 261 | SkImageInfo linDstInfo = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
| 262 | if (!linSrcPixmap.readPixels(linDstInfo, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 266 | return nullptr; |
| 267 | } |
| 268 | pmap = &tmpPixmap; |
| 269 | // must rebuild desc, since we've forced the info to be N32 |
| 270 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
brianosman | 85f9269 | 2016-03-24 06:56:32 -0700 | [diff] [blame] | 271 | } else if (kGray_8_SkColorType == pixmap.colorType()) { |
| 272 | // We don't have Gray8 support as a pixel config, so expand to 8888 |
| 273 | |
| 274 | // We should have converted sRGB Gray8 above (if we have sRGB support): |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 275 | SkASSERT(!caps->srgbSupport() || !pixmap.info().colorSpace() || |
| 276 | !pixmap.info().colorSpace()->gammaCloseToSRGB()); |
brianosman | 85f9269 | 2016-03-24 06:56:32 -0700 | [diff] [blame] | 277 | |
| 278 | SkImageInfo info = SkImageInfo::MakeN32(pixmap.width(), pixmap.height(), |
| 279 | kOpaque_SkAlphaType); |
| 280 | tmpBitmap.allocPixels(info); |
| 281 | if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | pmap = &tmpPixmap; |
| 288 | // must rebuild desc, since we've forced the info to be N32 |
| 289 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 290 | } else if (kIndex_8_SkColorType == pixmap.colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 291 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 292 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 293 | pixmap.width(), pixmap.height()); |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 294 | SkAutoMalloc storage(imageSize); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 295 | build_index8_data(storage.get(), pixmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 296 | |
| 297 | // our compressed data will be trimmed, so pass width() for its |
| 298 | // "rowBytes", since they are the same now. |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 299 | return ctx->textureProvider()->createTexture(desc, budgeted, storage.get(), |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 300 | pixmap.width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | } else { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 302 | SkImageInfo info = SkImageInfo::MakeN32Premul(pixmap.width(), pixmap.height()); |
| 303 | tmpBitmap.allocPixels(info); |
| 304 | if (!pixmap.readPixels(info, tmpBitmap.getPixels(), tmpBitmap.rowBytes())) { |
| 305 | return nullptr; |
| 306 | } |
| 307 | if (!tmpBitmap.peekPixels(&tmpPixmap)) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | pmap = &tmpPixmap; |
| 311 | // must rebuild desc, since we've forced the info to be N32 |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 312 | desc = GrImageInfoToSurfaceDesc(pmap->info(), *caps); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 313 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 314 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 315 | |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 316 | return ctx->textureProvider()->createTexture(desc, budgeted, pmap->addr(), |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 317 | pmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 318 | } |
| 319 | |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 320 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 321 | //////////////////////////////////////////////////////////////////////////////// |
| 322 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 323 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 324 | class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 325 | public: |
| 326 | explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 327 | private: |
| 328 | GrUniqueKeyInvalidatedMessage fMsg; |
| 329 | |
| 330 | void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 331 | }; |
| 332 | |
| 333 | pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 334 | } |
| 335 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 336 | GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap, |
| 337 | SkSourceGammaTreatment gammaTreatment) |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 338 | { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 339 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info(), *ctx->caps()); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 340 | if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) { |
| 341 | GrTexture* texture = load_etc1_texture(ctx, bitmap, desc); |
| 342 | if (texture) { |
| 343 | return texture; |
| 344 | } |
| 345 | } |
| 346 | |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 347 | sk_sp<GrTexture> texture(create_texture_from_yuv(ctx, bitmap, desc)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 348 | if (texture) { |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 349 | return texture.release(); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | SkASSERT(sizeof(int) <= sizeof(uint32_t)); |
| 353 | if (bitmap.width() < 0 || bitmap.height() < 0) { |
| 354 | return nullptr; |
| 355 | } |
| 356 | |
| 357 | SkAutoPixmapUnlock srcUnlocker; |
| 358 | if (!bitmap.requestLock(&srcUnlocker)) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | const SkPixmap& pixmap = srcUnlocker.pixmap(); |
| 362 | // Try to catch where we might have returned nullptr for src crbug.com/492818 |
| 363 | if (nullptr == pixmap.addr()) { |
| 364 | sk_throw(); |
| 365 | } |
| 366 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 367 | SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 368 | if (!mipmaps) { |
| 369 | return nullptr; |
| 370 | } |
| 371 | |
| 372 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 373 | if (mipLevelCount < 1) { |
| 374 | return nullptr; |
| 375 | } |
| 376 | |
| 377 | const bool isMipMapped = mipLevelCount > 1; |
| 378 | desc.fIsMipMapped = isMipMapped; |
| 379 | |
| 380 | SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); |
| 381 | |
| 382 | texels[0].fPixels = pixmap.addr(); |
| 383 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 384 | |
| 385 | for (int i = 1; i < mipLevelCount; ++i) { |
| 386 | SkMipMap::Level generatedMipLevel; |
| 387 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 388 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 389 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 390 | } |
| 391 | |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 392 | { |
| 393 | GrTexture* texture = ctx->textureProvider()->createMipMappedTexture(desc, |
| 394 | SkBudgeted::kYes, |
| 395 | texels.get(), |
| 396 | mipLevelCount); |
brianosman | 7d2f607 | 2016-06-17 07:03:45 -0700 | [diff] [blame] | 397 | if (texture) { |
| 398 | texture->texturePriv().setGammaTreatment(gammaTreatment); |
| 399 | } |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 400 | return texture; |
| 401 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 402 | } |
| 403 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 404 | GrTexture* GrUploadMipMapToTexture(GrContext* ctx, const SkImageInfo& info, |
| 405 | const GrMipLevel* texels, int mipLevelCount) { |
| 406 | const GrCaps* caps = ctx->caps(); |
| 407 | return ctx->textureProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps), |
| 408 | SkBudgeted::kYes, texels, |
| 409 | mipLevelCount); |
| 410 | } |
| 411 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 412 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 413 | const GrTextureParams& params, |
| 414 | SkSourceGammaTreatment gammaTreatment) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 415 | if (bitmap.getTexture()) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 416 | return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, gammaTreatment, |
| 417 | nullptr); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 418 | } |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 419 | return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params, gammaTreatment); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 420 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 421 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 422 | /////////////////////////////////////////////////////////////////////////////// |
| 423 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 424 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 425 | // alpha info, that will be considered. |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 426 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, const SkColorSpace* cs, |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 427 | const GrCaps& caps) { |
| 428 | // We intentionally ignore profile type for non-8888 formats. Anything we can't support |
| 429 | // in hardware will be expanded to sRGB 8888 in GrUploadPixmapToTexture. |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 430 | switch (ct) { |
| 431 | case kUnknown_SkColorType: |
| 432 | return kUnknown_GrPixelConfig; |
| 433 | case kAlpha_8_SkColorType: |
| 434 | return kAlpha_8_GrPixelConfig; |
| 435 | case kRGB_565_SkColorType: |
| 436 | return kRGB_565_GrPixelConfig; |
| 437 | case kARGB_4444_SkColorType: |
| 438 | return kRGBA_4444_GrPixelConfig; |
| 439 | case kRGBA_8888_SkColorType: |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 440 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 441 | ? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 442 | case kBGRA_8888_SkColorType: |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 443 | return (caps.srgbSupport() && cs && cs->gammaCloseToSRGB()) |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 444 | ? kSBGRA_8888_GrPixelConfig : kBGRA_8888_GrPixelConfig; |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 445 | case kIndex_8_SkColorType: |
| 446 | return kIndex_8_GrPixelConfig; |
| 447 | case kGray_8_SkColorType: |
| 448 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
| 449 | case kRGBA_F16_SkColorType: |
| 450 | return kRGBA_half_GrPixelConfig; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 451 | } |
| 452 | SkASSERT(0); // shouldn't get here |
| 453 | return kUnknown_GrPixelConfig; |
| 454 | } |
| 455 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 456 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 457 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 458 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 459 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 460 | switch (config) { |
| 461 | case kAlpha_8_GrPixelConfig: |
| 462 | ct = kAlpha_8_SkColorType; |
| 463 | break; |
| 464 | case kIndex_8_GrPixelConfig: |
| 465 | ct = kIndex_8_SkColorType; |
| 466 | break; |
| 467 | case kRGB_565_GrPixelConfig: |
| 468 | ct = kRGB_565_SkColorType; |
| 469 | break; |
| 470 | case kRGBA_4444_GrPixelConfig: |
| 471 | ct = kARGB_4444_SkColorType; |
| 472 | break; |
| 473 | case kRGBA_8888_GrPixelConfig: |
| 474 | ct = kRGBA_8888_SkColorType; |
| 475 | break; |
| 476 | case kBGRA_8888_GrPixelConfig: |
| 477 | ct = kBGRA_8888_SkColorType; |
| 478 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 479 | case kSRGBA_8888_GrPixelConfig: |
| 480 | ct = kRGBA_8888_SkColorType; |
| 481 | pt = kSRGB_SkColorProfileType; |
| 482 | break; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 483 | case kSBGRA_8888_GrPixelConfig: |
| 484 | ct = kBGRA_8888_SkColorType; |
| 485 | pt = kSRGB_SkColorProfileType; |
| 486 | break; |
brianosman | 9ac5b91 | 2016-04-12 13:49:53 -0700 | [diff] [blame] | 487 | case kRGBA_half_GrPixelConfig: |
| 488 | ct = kRGBA_F16_SkColorType; |
| 489 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 490 | default: |
| 491 | return false; |
| 492 | } |
| 493 | if (ctOut) { |
| 494 | *ctOut = ct; |
| 495 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 496 | if (ptOut) { |
| 497 | *ptOut = pt; |
| 498 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 499 | return true; |
| 500 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 501 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 502 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 503 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 504 | static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) { |
| 505 | if (primitiveIsSrc) { |
| 506 | return SkXfermode::kSrc_Mode != mode; |
| 507 | } else { |
| 508 | return SkXfermode::kDst_Mode != mode; |
| 509 | } |
| 510 | } |
| 511 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 512 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
| 513 | const SkPaint& skPaint, |
| 514 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 515 | sk_sp<GrFragmentProcessor>* shaderProcessor, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 516 | SkXfermode::Mode* primColorMode, |
| 517 | bool primitiveIsSrc, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 518 | bool allowSRGBInputs, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 519 | GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 520 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 521 | grPaint->setAllowSRGBInputs(allowSRGBInputs); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 522 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 523 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 524 | // of per-vertex colors. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 525 | sk_sp<GrFragmentProcessor> shaderFP; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 526 | if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { |
| 527 | if (shaderProcessor) { |
| 528 | shaderFP = *shaderProcessor; |
| 529 | } else if (const SkShader* shader = skPaint.getShader()) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 530 | SkSourceGammaTreatment gammaTreatment = allowSRGBInputs |
| 531 | ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 532 | shaderFP = shader->asFragmentProcessor(context, viewM, nullptr, |
| 533 | skPaint.getFilterQuality(), gammaTreatment); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 534 | if (!shaderFP) { |
| 535 | return false; |
| 536 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 541 | // a known constant value. In that case we can simply apply a color filter during this |
| 542 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 543 | bool applyColorFilterToPaintColor = false; |
| 544 | if (shaderFP) { |
| 545 | if (primColorMode) { |
| 546 | // There is a blend between the primitive color and the shader color. The shader sees |
| 547 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 548 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 549 | |
| 550 | // The geometry processor will insert the primitive color to start the color chain, so |
| 551 | // the GrPaint color will be ignored. |
| 552 | |
| 553 | GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor()); |
| 554 | |
| 555 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 556 | if (primitiveIsSrc) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 557 | shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(shaderFP), |
| 558 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 559 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 560 | shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(shaderFP), |
| 561 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 562 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 563 | // The above may return null if compose results in a pass through of the prim color. |
| 564 | if (shaderFP) { |
| 565 | grPaint->addColorFragmentProcessor(shaderFP); |
| 566 | } |
| 567 | |
| 568 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 569 | if (GrColor_WHITE != paintAlpha) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 570 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
| 571 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 572 | } |
| 573 | } else { |
| 574 | // The shader's FP sees the paint unpremul color |
| 575 | grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor())); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 576 | grPaint->addColorFragmentProcessor(std::move(shaderFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 577 | } |
| 578 | } else { |
| 579 | if (primColorMode) { |
| 580 | // There is a blend between the primitive color and the paint color. The blend considers |
| 581 | // 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] | 582 | sk_sp<GrFragmentProcessor> processor( |
| 583 | GrConstColorProcessor::Make(SkColorToOpaqueGrColor(skPaint.getColor()), |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 584 | GrConstColorProcessor::kIgnore_InputMode)); |
| 585 | if (primitiveIsSrc) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 586 | processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std::move(processor), |
| 587 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 588 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 589 | processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(processor), |
| 590 | *primColorMode); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 591 | } |
| 592 | if (processor) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 593 | grPaint->addColorFragmentProcessor(std::move(processor)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 594 | } |
| 595 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 596 | grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor())); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 597 | |
| 598 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 599 | if (GrColor_WHITE != paintAlpha) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 600 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make( |
| 601 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 602 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 603 | } else { |
| 604 | // No shader, no primitive color. |
| 605 | grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor())); |
| 606 | applyColorFilterToPaintColor = true; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 611 | if (colorFilter) { |
| 612 | if (applyColorFilterToPaintColor) { |
| 613 | grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor()))); |
| 614 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 615 | sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(context)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 616 | if (cfFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 617 | grPaint->addColorFragmentProcessor(std::move(cfFP)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 618 | } else { |
| 619 | return false; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 624 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 625 | // the GrPaint to also be null (also kSrcOver). |
| 626 | SkASSERT(!grPaint->getXPFactory()); |
| 627 | SkXfermode* xfermode = skPaint.getXfermode(); |
| 628 | if (xfermode) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 629 | grPaint->setXPFactory(xfermode->asXPFactory()); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 630 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 631 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 632 | #ifndef SK_IGNORE_GPU_DITHER |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 633 | if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 634 | grPaint->addColorFragmentProcessor(GrDitherEffect::Make()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 635 | } |
| 636 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 637 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 638 | } |
| 639 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 640 | bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 641 | bool allowSRGBInputs, GrPaint* grPaint) { |
| 642 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, |
| 643 | allowSRGBInputs, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 644 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 645 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 646 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 647 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
| 648 | const SkPaint& skPaint, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 649 | sk_sp<GrFragmentProcessor> shaderFP, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 650 | bool allowSRGBInputs, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 651 | GrPaint* grPaint) { |
| 652 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 653 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 654 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 655 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 656 | allowSRGBInputs, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 657 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 658 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 659 | /** Ignores the SkShader (if any) on skPaint. */ |
| 660 | bool SkPaintToGrPaintNoShader(GrContext* context, |
| 661 | const SkPaint& skPaint, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 662 | bool allowSRGBInputs, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 663 | GrPaint* grPaint) { |
| 664 | // 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] | 665 | static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr); |
| 666 | static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 667 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 668 | allowSRGBInputs, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
| 672 | be setup as a vertex attribute using the specified SkXfermode::Mode. */ |
| 673 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
| 674 | const SkPaint& skPaint, |
| 675 | const SkMatrix& viewM, |
| 676 | SkXfermode::Mode primColorMode, |
| 677 | bool primitiveIsSrc, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 678 | bool allowSRGBInputs, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 679 | GrPaint* grPaint) { |
| 680 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 681 | allowSRGBInputs, grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 682 | } |
| 683 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 684 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
| 685 | const SkPaint& paint, |
| 686 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 687 | sk_sp<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 688 | bool textureIsAlphaOnly, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 689 | bool allowSRGBInputs, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 690 | GrPaint* grPaint) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 691 | sk_sp<GrFragmentProcessor> shaderFP; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 692 | if (textureIsAlphaOnly) { |
| 693 | if (const SkShader* shader = paint.getShader()) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 694 | SkSourceGammaTreatment gammaTreatment = allowSRGBInputs |
| 695 | ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 696 | shaderFP = shader->asFragmentProcessor(context, |
| 697 | viewM, |
| 698 | nullptr, |
| 699 | paint.getFilterQuality(), |
| 700 | gammaTreatment); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 701 | if (!shaderFP) { |
| 702 | return false; |
| 703 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 704 | sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std::move(fp) }; |
| 705 | shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 706 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 707 | shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 708 | } |
| 709 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 710 | shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 711 | } |
| 712 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 713 | return SkPaintToGrPaintReplaceShader(context, paint, std::move(shaderFP), allowSRGBInputs, |
| 714 | grPaint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 715 | } |
| 716 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 717 | |
| 718 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 719 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 720 | SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { |
| 721 | #ifdef SK_DEBUG |
| 722 | const GrSurfaceDesc& desc = tex->desc(); |
| 723 | SkASSERT(w <= desc.fWidth); |
| 724 | SkASSERT(h <= desc.fHeight); |
| 725 | #endif |
| 726 | const GrPixelConfig config = tex->config(); |
| 727 | SkColorType ct; |
| 728 | SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 729 | if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 730 | ct = kUnknown_SkColorType; |
| 731 | } |
| 732 | return SkImageInfo::Make(w, h, ct, at); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) { |
| 737 | const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque); |
| 738 | dst->setInfo(info); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 739 | dst->setPixelRef(new SkGrPixelRef(info, src))->unref(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 740 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 741 | |
| 742 | GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 743 | const SkMatrix& viewM, |
| 744 | const SkMatrix& localM, |
| 745 | bool* doBicubic) { |
| 746 | *doBicubic = false; |
| 747 | GrTextureParams::FilterMode textureFilterMode; |
| 748 | switch (paintFilterQuality) { |
| 749 | case kNone_SkFilterQuality: |
| 750 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 751 | break; |
| 752 | case kLow_SkFilterQuality: |
| 753 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 754 | break; |
| 755 | case kMedium_SkFilterQuality: { |
| 756 | SkMatrix matrix; |
| 757 | matrix.setConcat(viewM, localM); |
| 758 | if (matrix.getMinScale() < SK_Scalar1) { |
| 759 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 760 | } else { |
| 761 | // Don't trigger MIP level generation unnecessarily. |
| 762 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 763 | } |
| 764 | break; |
| 765 | } |
| 766 | case kHigh_SkFilterQuality: { |
| 767 | SkMatrix matrix; |
| 768 | matrix.setConcat(viewM, localM); |
| 769 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 770 | break; |
| 771 | } |
| 772 | default: |
| 773 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 774 | "Sorry, I don't understand the filtering " |
| 775 | "mode you asked for. Falling back to " |
| 776 | "MIPMaps."); |
| 777 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 778 | break; |
| 779 | |
| 780 | } |
| 781 | return textureFilterMode; |
| 782 | } |