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" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 10 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 14 | #include "GrImageIDTextureAdjuster.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 15 | #include "GrTextureParamsAdjuster.h" |
| 16 | #include "GrTypes.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 17 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 18 | #include "GrYUVProvider.h" |
| 19 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 20 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 21 | #include "SkConfig8888.h" |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 22 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 23 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 24 | #include "SkErrorInternals.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 25 | #include "SkGrPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 26 | #include "SkMessageBus.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 27 | #include "SkMipMap.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 28 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 29 | #include "SkResourceCache.h" |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 30 | #include "SkTemplates.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 31 | #include "SkTextureCompressor.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 32 | #include "SkYUVPlanesCache.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 33 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 34 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 35 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 36 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 37 | #include "effects/GrXfermodeFragmentProcessor.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 38 | #include "effects/GrYUVEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 39 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 40 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 41 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 42 | # include "etc1.h" |
| 43 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 45 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) { |
| 46 | GrSurfaceDesc desc; |
| 47 | desc.fFlags = kNone_GrSurfaceFlags; |
| 48 | desc.fWidth = info.width(); |
| 49 | desc.fHeight = info.height(); |
| 50 | desc.fConfig = SkImageInfo2GrPixelConfig(info); |
| 51 | desc.fSampleCnt = 0; |
| 52 | return desc; |
| 53 | } |
| 54 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 55 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) { |
| 56 | SkASSERT(key); |
| 57 | SkASSERT(imageID); |
| 58 | SkASSERT(!imageBounds.isEmpty()); |
| 59 | static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain(); |
| 60 | GrUniqueKey::Builder builder(key, kImageIDDomain, 5); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 61 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 62 | builder[1] = imageBounds.fLeft; |
| 63 | builder[2] = imageBounds.fTop; |
| 64 | builder[3] = imageBounds.fRight; |
| 65 | builder[4] = imageBounds.fBottom; |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
| 69 | int expectedW, int expectedH, |
| 70 | const void** outStartOfDataToUpload) { |
| 71 | *outStartOfDataToUpload = nullptr; |
| 72 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 73 | if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { |
| 74 | return kUnknown_GrPixelConfig; |
| 75 | } |
| 76 | |
| 77 | const uint8_t* bytes = data->bytes(); |
| 78 | if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) { |
| 79 | // Does the data match the dimensions of the bitmap? If not, |
| 80 | // then we don't know how to scale the image to match it... |
| 81 | if (etc1_pkm_get_width(bytes) != (unsigned)expectedW || |
| 82 | etc1_pkm_get_height(bytes) != (unsigned)expectedH) |
| 83 | { |
| 84 | return kUnknown_GrPixelConfig; |
| 85 | } |
| 86 | |
| 87 | *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE; |
| 88 | return kETC1_GrPixelConfig; |
| 89 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 90 | SkKTXFile ktx(data); |
| 91 | |
| 92 | // Is it actually an ETC1 texture? |
| 93 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
| 94 | return kUnknown_GrPixelConfig; |
| 95 | } |
| 96 | |
| 97 | // Does the data match the dimensions of the bitmap? If not, |
| 98 | // then we don't know how to scale the image to match it... |
| 99 | if (ktx.width() != expectedW || ktx.height() != expectedH) { |
| 100 | return kUnknown_GrPixelConfig; |
| 101 | } |
| 102 | |
| 103 | *outStartOfDataToUpload = ktx.pixelData(); |
| 104 | return kETC1_GrPixelConfig; |
| 105 | } |
| 106 | #endif |
| 107 | return kUnknown_GrPixelConfig; |
| 108 | } |
| 109 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 110 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 111 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Fill out buffer with the compressed format Ganesh expects from a colortable |
| 114 | * based bitmap. [palette (colortable) + indices]. |
| 115 | * |
| 116 | * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
| 117 | * we could detect that the colortable.count is <= 16, and then repack the |
| 118 | * indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 119 | * slower than memcpy), so skipping that for now. |
| 120 | * |
| 121 | * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 122 | * as the colortable.count says it is. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | */ |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 124 | static void build_index8_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 125 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 126 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 127 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 129 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | SkColorTable* ctable = bitmap.getColorTable(); |
| 134 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 135 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 136 | const int count = ctable->count(); |
| 137 | |
| 138 | SkDstPixelInfo dstPI; |
| 139 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 140 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 141 | dstPI.fPixels = buffer; |
| 142 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 143 | |
| 144 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 145 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 146 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 147 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 148 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 149 | |
| 150 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 151 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | // 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] | 153 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 155 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 157 | } else { |
| 158 | // need to trim off the extra bytes per row |
| 159 | size_t width = bitmap.width(); |
| 160 | size_t rowBytes = bitmap.rowBytes(); |
| 161 | const char* src = (const char*)bitmap.getPixels(); |
| 162 | for (int y = 0; y < bitmap.height(); y++) { |
| 163 | memcpy(dst, src, width); |
| 164 | src += rowBytes; |
| 165 | dst += width; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 170 | /** |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 171 | * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will |
| 172 | * be gone from SkPixelRef, and we can remove this subclass entirely. |
| 173 | */ |
| 174 | class PixelRef_GrYUVProvider : public GrYUVProvider { |
| 175 | SkPixelRef* fPR; |
| 176 | |
| 177 | public: |
| 178 | PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {} |
| 179 | |
| 180 | uint32_t onGetID() override { return fPR->getGenerationID(); } |
| 181 | bool onGetYUVSizes(SkISize sizes[3]) override { |
| 182 | return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr); |
| 183 | } |
| 184 | bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
| 185 | SkYUVColorSpace* space) override { |
| 186 | return fPR->getYUV8Planes(sizes, planes, rowBytes, space); |
| 187 | } |
| 188 | }; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 189 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 190 | static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm, |
| 191 | const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 192 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 193 | SkPixelRef* pixelRef = bm.pixelRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 194 | if ((nullptr == pixelRef) || |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 195 | (pixelRef->info().width() != bm.info().width()) || |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 196 | (pixelRef->info().height() != bm.info().height())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | return nullptr; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 198 | } |
| 199 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 200 | PixelRef_GrYUVProvider provider(pixelRef); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 201 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 202 | return provider.refAsTexture(ctx, desc, !bm.isVolatile()); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 203 | } |
| 204 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 205 | static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) { |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 206 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
| 207 | if (!data) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | const void* startOfTexData; |
| 212 | desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(), |
| 213 | &startOfTexData); |
| 214 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 218 | return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0); |
bsalomon | 466c2c4 | 2015-10-16 12:01:18 -0700 | [diff] [blame] | 219 | } |
| 220 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 221 | GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) { |
| 222 | SkASSERT(!bmp.getTexture()); |
| 223 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 224 | SkBitmap tmpBitmap; |
| 225 | const SkBitmap* bitmap = &bmp; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 226 | |
reed | 3322a81 | 2015-09-16 10:09:24 -0700 | [diff] [blame] | 227 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info()); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 228 | const GrCaps* caps = ctx->caps(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 229 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 230 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 231 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 232 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 233 | bitmap->width(), bitmap->height()); |
| 234 | SkAutoMalloc storage(imageSize); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 235 | build_index8_data(storage.get(), bmp); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 236 | |
| 237 | // our compressed data will be trimmed, so pass width() for its |
| 238 | // "rowBytes", since they are the same now. |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 239 | return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, storage.get(), |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 240 | bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 241 | } else { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 242 | bmp.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | // now bitmap points to our temp, which has been promoted to 32bits |
| 244 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 245 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 247 | } else if (!bitmap->readyToDraw()) { |
| 248 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 249 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 250 | // the bitmap has available pixels, then they might not be what the decompressed |
| 251 | // data is. |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 252 | |
| 253 | // Really?? We aren't doing this with YUV. |
| 254 | |
| 255 | GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 256 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 257 | return texture; |
| 258 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 259 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 260 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 261 | GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc); |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 262 | if (texture) { |
| 263 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 264 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 265 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 266 | SkAutoLockPixels alp(*bitmap); |
| 267 | if (!bitmap->readyToDraw()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 268 | return nullptr; |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 269 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 270 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 271 | return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, bitmap->getPixels(), |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 272 | bitmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 273 | } |
| 274 | |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 275 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 276 | //////////////////////////////////////////////////////////////////////////////// |
| 277 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 278 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 279 | class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 280 | public: |
| 281 | explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 282 | private: |
| 283 | GrUniqueKeyInvalidatedMessage fMsg; |
| 284 | |
| 285 | void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 286 | }; |
| 287 | |
| 288 | pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 289 | } |
| 290 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 291 | GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& bitmap) |
| 292 | { |
| 293 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info()); |
| 294 | if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) { |
| 295 | GrTexture* texture = load_etc1_texture(ctx, bitmap, desc); |
| 296 | if (texture) { |
| 297 | return texture; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | GrTexture* texture = create_texture_from_yuv(ctx, bitmap, desc); |
| 302 | if (texture) { |
| 303 | return texture; |
| 304 | } |
| 305 | |
| 306 | SkASSERT(sizeof(int) <= sizeof(uint32_t)); |
| 307 | if (bitmap.width() < 0 || bitmap.height() < 0) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | SkAutoPixmapUnlock srcUnlocker; |
| 312 | if (!bitmap.requestLock(&srcUnlocker)) { |
| 313 | return nullptr; |
| 314 | } |
| 315 | const SkPixmap& pixmap = srcUnlocker.pixmap(); |
| 316 | // Try to catch where we might have returned nullptr for src crbug.com/492818 |
| 317 | if (nullptr == pixmap.addr()) { |
| 318 | sk_throw(); |
| 319 | } |
| 320 | |
| 321 | SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr)); |
| 322 | if (!mipmaps) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
| 326 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 327 | if (mipLevelCount < 1) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
| 331 | const bool isMipMapped = mipLevelCount > 1; |
| 332 | desc.fIsMipMapped = isMipMapped; |
| 333 | |
| 334 | SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); |
| 335 | |
| 336 | texels[0].fPixels = pixmap.addr(); |
| 337 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 338 | |
| 339 | for (int i = 1; i < mipLevelCount; ++i) { |
| 340 | SkMipMap::Level generatedMipLevel; |
| 341 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 342 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 343 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 344 | } |
| 345 | |
| 346 | return ctx->textureProvider()->createMipMappedTexture(desc, SkBudgeted::kYes, texels.get(), |
| 347 | mipLevelCount); |
| 348 | } |
| 349 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 350 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 351 | const GrTextureParams& params) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 352 | if (bitmap.getTexture()) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 353 | return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 354 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 355 | return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 356 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 357 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 358 | /////////////////////////////////////////////////////////////////////////////// |
| 359 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 360 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 361 | // alpha info, that will be considered. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 362 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 363 | switch (ct) { |
| 364 | case kUnknown_SkColorType: |
| 365 | return kUnknown_GrPixelConfig; |
| 366 | case kAlpha_8_SkColorType: |
| 367 | return kAlpha_8_GrPixelConfig; |
| 368 | case kRGB_565_SkColorType: |
| 369 | return kRGB_565_GrPixelConfig; |
| 370 | case kARGB_4444_SkColorType: |
| 371 | return kRGBA_4444_GrPixelConfig; |
| 372 | case kRGBA_8888_SkColorType: |
jvanverth | 99babf2 | 2015-05-22 06:06:40 -0700 | [diff] [blame] | 373 | //if (kSRGB_SkColorProfileType == pt) { |
| 374 | // return kSRGBA_8888_GrPixelConfig; |
| 375 | //} |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 376 | return kRGBA_8888_GrPixelConfig; |
| 377 | case kBGRA_8888_SkColorType: |
| 378 | return kBGRA_8888_GrPixelConfig; |
| 379 | case kIndex_8_SkColorType: |
| 380 | return kIndex_8_GrPixelConfig; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 381 | case kGray_8_SkColorType: |
| 382 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 383 | case kRGBA_F16_SkColorType: |
| 384 | return kRGBA_half_GrPixelConfig; |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 385 | } |
| 386 | SkASSERT(0); // shouldn't get here |
| 387 | return kUnknown_GrPixelConfig; |
| 388 | } |
| 389 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 390 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 391 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 392 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 393 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 394 | switch (config) { |
| 395 | case kAlpha_8_GrPixelConfig: |
| 396 | ct = kAlpha_8_SkColorType; |
| 397 | break; |
| 398 | case kIndex_8_GrPixelConfig: |
| 399 | ct = kIndex_8_SkColorType; |
| 400 | break; |
| 401 | case kRGB_565_GrPixelConfig: |
| 402 | ct = kRGB_565_SkColorType; |
| 403 | break; |
| 404 | case kRGBA_4444_GrPixelConfig: |
| 405 | ct = kARGB_4444_SkColorType; |
| 406 | break; |
| 407 | case kRGBA_8888_GrPixelConfig: |
| 408 | ct = kRGBA_8888_SkColorType; |
| 409 | break; |
| 410 | case kBGRA_8888_GrPixelConfig: |
| 411 | ct = kBGRA_8888_SkColorType; |
| 412 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 413 | case kSRGBA_8888_GrPixelConfig: |
| 414 | ct = kRGBA_8888_SkColorType; |
| 415 | pt = kSRGB_SkColorProfileType; |
| 416 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 417 | default: |
| 418 | return false; |
| 419 | } |
| 420 | if (ctOut) { |
| 421 | *ctOut = ct; |
| 422 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 423 | if (ptOut) { |
| 424 | *ptOut = pt; |
| 425 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 426 | return true; |
| 427 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 428 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 429 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 430 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 431 | static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) { |
| 432 | if (primitiveIsSrc) { |
| 433 | return SkXfermode::kSrc_Mode != mode; |
| 434 | } else { |
| 435 | return SkXfermode::kDst_Mode != mode; |
| 436 | } |
| 437 | } |
| 438 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 439 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
| 440 | const SkPaint& skPaint, |
| 441 | const SkMatrix& viewM, |
| 442 | const GrFragmentProcessor** shaderProcessor, |
| 443 | SkXfermode::Mode* primColorMode, |
| 444 | bool primitiveIsSrc, |
| 445 | GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 446 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 447 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 448 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 449 | // of per-vertex colors. |
| 450 | SkAutoTUnref<const GrFragmentProcessor> aufp; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 451 | const GrFragmentProcessor* shaderFP = nullptr; |
| 452 | if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { |
| 453 | if (shaderProcessor) { |
| 454 | shaderFP = *shaderProcessor; |
| 455 | } else if (const SkShader* shader = skPaint.getShader()) { |
| 456 | aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 457 | skPaint.getFilterQuality())); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 458 | shaderFP = aufp; |
| 459 | if (!shaderFP) { |
| 460 | return false; |
| 461 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 466 | // a known constant value. In that case we can simply apply a color filter during this |
| 467 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 468 | bool applyColorFilterToPaintColor = false; |
| 469 | if (shaderFP) { |
| 470 | if (primColorMode) { |
| 471 | // There is a blend between the primitive color and the shader color. The shader sees |
| 472 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 473 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 474 | |
| 475 | // The geometry processor will insert the primitive color to start the color chain, so |
| 476 | // the GrPaint color will be ignored. |
| 477 | |
| 478 | GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor()); |
| 479 | |
| 480 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
| 481 | aufp.reset(shaderFP); |
| 482 | |
| 483 | if (primitiveIsSrc) { |
| 484 | shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP, |
| 485 | *primColorMode); |
| 486 | } else { |
| 487 | shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP, |
| 488 | *primColorMode); |
| 489 | } |
| 490 | aufp.reset(shaderFP); |
| 491 | // The above may return null if compose results in a pass through of the prim color. |
| 492 | if (shaderFP) { |
| 493 | grPaint->addColorFragmentProcessor(shaderFP); |
| 494 | } |
| 495 | |
| 496 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 497 | if (GrColor_WHITE != paintAlpha) { |
| 498 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 499 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 500 | } |
| 501 | } else { |
| 502 | // The shader's FP sees the paint unpremul color |
| 503 | grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor())); |
| 504 | grPaint->addColorFragmentProcessor(shaderFP); |
| 505 | } |
| 506 | } else { |
| 507 | if (primColorMode) { |
| 508 | // There is a blend between the primitive color and the paint color. The blend considers |
| 509 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
| 510 | SkAutoTUnref<const GrFragmentProcessor> processor( |
| 511 | GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()), |
| 512 | GrConstColorProcessor::kIgnore_InputMode)); |
| 513 | if (primitiveIsSrc) { |
| 514 | processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor, |
| 515 | *primColorMode)); |
| 516 | } else { |
| 517 | processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor, |
| 518 | *primColorMode)); |
| 519 | |
| 520 | } |
| 521 | if (processor) { |
| 522 | grPaint->addColorFragmentProcessor(processor); |
| 523 | } |
| 524 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 525 | grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor())); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 526 | |
| 527 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 528 | if (GrColor_WHITE != paintAlpha) { |
| 529 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 530 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 531 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 532 | } else { |
| 533 | // No shader, no primitive color. |
| 534 | grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor())); |
| 535 | applyColorFilterToPaintColor = true; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 540 | if (colorFilter) { |
| 541 | if (applyColorFilterToPaintColor) { |
| 542 | grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor()))); |
| 543 | } else { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 544 | SkAutoTUnref<const GrFragmentProcessor> cfFP( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 545 | colorFilter->asFragmentProcessor(context)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 546 | if (cfFP) { |
| 547 | grPaint->addColorFragmentProcessor(cfFP); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 548 | } else { |
| 549 | return false; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 554 | // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on |
| 555 | // the GrPaint to also be null (also kSrcOver). |
| 556 | SkASSERT(!grPaint->getXPFactory()); |
| 557 | SkXfermode* xfermode = skPaint.getXfermode(); |
| 558 | if (xfermode) { |
| 559 | // SafeUnref in case a new xfermode is added that returns null. |
| 560 | // In such cases we will fall back to kSrcOver_Mode. |
| 561 | SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory())); |
| 562 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 563 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 564 | #ifndef SK_IGNORE_GPU_DITHER |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 565 | if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) { |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 566 | grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 567 | } |
| 568 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 569 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 570 | } |
| 571 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 572 | bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM, |
| 573 | GrPaint* grPaint) { |
| 574 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint); |
| 575 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 576 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 577 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 578 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
| 579 | const SkPaint& skPaint, |
| 580 | const GrFragmentProcessor* shaderFP, |
| 581 | GrPaint* grPaint) { |
| 582 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 583 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 584 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 585 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false, |
| 586 | grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 587 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 588 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 589 | /** Ignores the SkShader (if any) on skPaint. */ |
| 590 | bool SkPaintToGrPaintNoShader(GrContext* context, |
| 591 | const SkPaint& skPaint, |
| 592 | GrPaint* grPaint) { |
| 593 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
| 594 | static const GrFragmentProcessor* kNullShaderFP = nullptr; |
| 595 | static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP; |
| 596 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false, |
| 597 | grPaint); |
| 598 | } |
| 599 | |
| 600 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
| 601 | be setup as a vertex attribute using the specified SkXfermode::Mode. */ |
| 602 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
| 603 | const SkPaint& skPaint, |
| 604 | const SkMatrix& viewM, |
| 605 | SkXfermode::Mode primColorMode, |
| 606 | bool primitiveIsSrc, |
| 607 | GrPaint* grPaint) { |
| 608 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc, |
| 609 | grPaint); |
| 610 | } |
| 611 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 612 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
| 613 | const SkPaint& paint, |
| 614 | const SkMatrix& viewM, |
| 615 | const GrFragmentProcessor* fp, |
| 616 | bool textureIsAlphaOnly, |
| 617 | GrPaint* grPaint) { |
| 618 | SkAutoTUnref<const GrFragmentProcessor> shaderFP; |
| 619 | if (textureIsAlphaOnly) { |
| 620 | if (const SkShader* shader = paint.getShader()) { |
| 621 | shaderFP.reset(shader->asFragmentProcessor(context, |
| 622 | viewM, |
| 623 | nullptr, |
| 624 | paint.getFilterQuality())); |
| 625 | if (!shaderFP) { |
| 626 | return false; |
| 627 | } |
| 628 | const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp }; |
| 629 | shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2)); |
| 630 | } else { |
| 631 | shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp)); |
| 632 | } |
| 633 | } else { |
| 634 | shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); |
| 635 | } |
| 636 | |
| 637 | return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), grPaint); |
| 638 | } |
| 639 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 640 | |
| 641 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 642 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 643 | SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { |
| 644 | #ifdef SK_DEBUG |
| 645 | const GrSurfaceDesc& desc = tex->desc(); |
| 646 | SkASSERT(w <= desc.fWidth); |
| 647 | SkASSERT(h <= desc.fHeight); |
| 648 | #endif |
| 649 | const GrPixelConfig config = tex->config(); |
| 650 | SkColorType ct; |
| 651 | SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 652 | if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 653 | ct = kUnknown_SkColorType; |
| 654 | } |
| 655 | return SkImageInfo::Make(w, h, ct, at); |
| 656 | } |
| 657 | |
| 658 | |
| 659 | void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) { |
| 660 | const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque); |
| 661 | dst->setInfo(info); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 662 | dst->setPixelRef(new SkGrPixelRef(info, src))->unref(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 663 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 664 | |
| 665 | GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 666 | const SkMatrix& viewM, |
| 667 | const SkMatrix& localM, |
| 668 | bool* doBicubic) { |
| 669 | *doBicubic = false; |
| 670 | GrTextureParams::FilterMode textureFilterMode; |
| 671 | switch (paintFilterQuality) { |
| 672 | case kNone_SkFilterQuality: |
| 673 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 674 | break; |
| 675 | case kLow_SkFilterQuality: |
| 676 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 677 | break; |
| 678 | case kMedium_SkFilterQuality: { |
| 679 | SkMatrix matrix; |
| 680 | matrix.setConcat(viewM, localM); |
| 681 | if (matrix.getMinScale() < SK_Scalar1) { |
| 682 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 683 | } else { |
| 684 | // Don't trigger MIP level generation unnecessarily. |
| 685 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 686 | } |
| 687 | break; |
| 688 | } |
| 689 | case kHigh_SkFilterQuality: { |
| 690 | SkMatrix matrix; |
| 691 | matrix.setConcat(viewM, localM); |
| 692 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 693 | break; |
| 694 | } |
| 695 | default: |
| 696 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 697 | "Sorry, I don't understand the filtering " |
| 698 | "mode you asked for. Falling back to " |
| 699 | "MIPMaps."); |
| 700 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 701 | break; |
| 702 | |
| 703 | } |
| 704 | return textureFilterMode; |
| 705 | } |