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