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