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