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 | #include "GrTextureMaker.h" |
| 9 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "SkGr.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" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 13 | #include "GrDrawContext.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 15 | #include "GrYUVProvider.h" |
| 16 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 17 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 18 | #include "SkConfig8888.h" |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 19 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 20 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 21 | #include "SkErrorInternals.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 22 | #include "SkGrPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 23 | #include "SkMessageBus.h" |
| 24 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 25 | #include "SkResourceCache.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 26 | #include "SkTextureCompressor.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 27 | #include "SkYUVPlanesCache.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 28 | #include "effects/GrBicubicEffect.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 29 | #include "effects/GrConstColorProcessor.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 30 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 31 | #include "effects/GrPorterDuffXferProcessor.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 32 | #include "effects/GrXfermodeFragmentProcessor.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 33 | #include "effects/GrYUVtoRGBEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 34 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 35 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 36 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 37 | # include "etc1.h" |
| 38 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 40 | bool GrTextureUsageSupported(const GrCaps& caps, int width, int height, SkImageUsageType usage) { |
| 41 | if (caps.npotTextureTileSupport()) { |
| 42 | return true; |
| 43 | } |
| 44 | const bool is_pow2 = SkIsPow2(width) && SkIsPow2(height); |
| 45 | return is_pow2 || kUntiled_SkImageUsageType == usage; |
| 46 | } |
| 47 | |
| 48 | GrTextureParams GrImageUsageToTextureParams(SkImageUsageType usage) { |
| 49 | // Just need a params that will trigger the correct cache key / etc, since the usage doesn't |
| 50 | // tell us the specifics about filter level or specific tiling. |
| 51 | |
| 52 | const SkShader::TileMode tiles[] = { |
| 53 | SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType |
| 54 | SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType |
| 55 | SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType |
| 56 | }; |
| 57 | |
| 58 | const GrTextureParams::FilterMode filters[] = { |
| 59 | GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType |
| 60 | GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType |
| 61 | GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType |
| 62 | }; |
| 63 | |
| 64 | return GrTextureParams(tiles[usage], filters[usage]); |
| 65 | } |
| 66 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 68 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 69 | |
| 70 | At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | we could detect that the colortable.count is <= 16, and then repack the |
| 72 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 73 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 74 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 76 | as the colortable.count says it is. |
| 77 | */ |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 78 | static void build_index8_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 79 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 81 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 83 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | SkColorTable* ctable = bitmap.getColorTable(); |
| 88 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 89 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 90 | const int count = ctable->count(); |
| 91 | |
| 92 | SkDstPixelInfo dstPI; |
| 93 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 94 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 95 | dstPI.fPixels = buffer; |
| 96 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 97 | |
| 98 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 99 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 100 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 101 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 102 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 103 | |
| 104 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 105 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | // 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] | 107 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 109 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 111 | } else { |
| 112 | // need to trim off the extra bytes per row |
| 113 | size_t width = bitmap.width(); |
| 114 | size_t rowBytes = bitmap.rowBytes(); |
| 115 | const char* src = (const char*)bitmap.getPixels(); |
| 116 | for (int y = 0; y < bitmap.height(); y++) { |
| 117 | memcpy(dst, src, width); |
| 118 | src += rowBytes; |
| 119 | dst += width; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | //////////////////////////////////////////////////////////////////////////////// |
| 125 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 126 | static void get_stretch(const GrContext* ctx, int width, int height, |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 127 | const GrTextureParams* params, SkGrStretch* stretch) { |
| 128 | stretch->fType = SkGrStretch::kNone_Type; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 129 | bool doStretch = false; |
| 130 | if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() && |
| 131 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
| 132 | doStretch = true; |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 133 | stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize())); |
| 134 | stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize())); |
| 135 | } else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 136 | // The small texture issues appear to be with tiling. Hence it seems ok to scale them |
| 137 | // up using the GPU. If issues persist we may need to CPU-stretch. |
| 138 | doStretch = true; |
| 139 | stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize()); |
| 140 | stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 141 | } |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 142 | if (doStretch) { |
bsalomon | 0513c83 | 2015-06-01 10:22:48 -0700 | [diff] [blame] | 143 | if (params) { |
| 144 | switch(params->filterMode()) { |
| 145 | case GrTextureParams::kNone_FilterMode: |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 146 | stretch->fType = SkGrStretch::kNearest_Type; |
bsalomon | 0513c83 | 2015-06-01 10:22:48 -0700 | [diff] [blame] | 147 | break; |
| 148 | case GrTextureParams::kBilerp_FilterMode: |
| 149 | case GrTextureParams::kMipMap_FilterMode: |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 150 | stretch->fType = SkGrStretch::kBilerp_Type; |
bsalomon | 0513c83 | 2015-06-01 10:22:48 -0700 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } else { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 154 | stretch->fType = SkGrStretch::kBilerp_Type; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 155 | } |
| 156 | } else { |
| 157 | stretch->fWidth = -1; |
| 158 | stretch->fHeight = -1; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 159 | stretch->fType = SkGrStretch::kNone_Type; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 160 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 161 | } |
| 162 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 163 | bool GrMakeStretchedKey(const GrUniqueKey& origKey, const SkGrStretch& stretch, |
| 164 | GrUniqueKey* stretchedKey) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 165 | if (origKey.isValid() && SkGrStretch::kNone_Type != stretch.fType) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 166 | uint32_t width = SkToU16(stretch.fWidth); |
| 167 | uint32_t height = SkToU16(stretch.fHeight); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 168 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 169 | GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2); |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 170 | builder[0] = stretch.fType; |
| 171 | builder[1] = width | (height << 16); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 172 | builder.finish(); |
| 173 | return true; |
| 174 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 175 | SkASSERT(!stretchedKey->isValid()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 179 | static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) { |
| 180 | SkASSERT(SkIsU16(subset.width())); |
| 181 | SkASSERT(SkIsU16(subset.height())); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 182 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 183 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 184 | GrUniqueKey::Builder builder(key, kDomain, 4); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 185 | builder[0] = imageID; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 186 | builder[1] = subset.x(); |
| 187 | builder[2] = subset.y(); |
| 188 | builder[3] = subset.width() | (subset.height() << 16); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 189 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 190 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 191 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset, |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 192 | const GrCaps& caps, SkImageUsageType usage) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 193 | const SkGrStretch::Type stretches[] = { |
| 194 | SkGrStretch::kNone_Type, // kUntiled_SkImageUsageType |
| 195 | SkGrStretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType |
| 196 | SkGrStretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 199 | if (!GrTextureUsageSupported(caps, subset.width(), subset.height(), usage)) { |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 200 | GrUniqueKey tmpKey; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 201 | make_unstretched_key(&tmpKey, imageID, subset); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 202 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 203 | SkGrStretch stretch; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 204 | stretch.fType = stretches[usage]; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 205 | stretch.fWidth = SkNextPow2(subset.width()); |
| 206 | stretch.fHeight = SkNextPow2(subset.height()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 207 | if (!GrMakeStretchedKey(tmpKey, stretch, key)) { |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 208 | goto UNSTRETCHED; |
| 209 | } |
| 210 | } else { |
| 211 | UNSTRETCHED: |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 212 | make_unstretched_key(key, imageID, subset); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 216 | static void make_image_keys(uint32_t imageID, const SkIRect& subset, const SkGrStretch& stretch, |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 217 | GrUniqueKey* key, GrUniqueKey* stretchedKey) { |
| 218 | make_unstretched_key(key, imageID, subset); |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 219 | if (SkGrStretch::kNone_Type != stretch.fType) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 220 | GrMakeStretchedKey(*key, stretch, stretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 221 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
reed | 3322a81 | 2015-09-16 10:09:24 -0700 | [diff] [blame] | 224 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) { |
| 225 | GrSurfaceDesc desc; |
| 226 | desc.fFlags = kNone_GrSurfaceFlags; |
| 227 | desc.fWidth = info.width(); |
| 228 | desc.fHeight = info.height(); |
| 229 | desc.fConfig = SkImageInfo2GrPixelConfig(info); |
| 230 | desc.fSampleCnt = 0; |
| 231 | return desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 232 | } |
| 233 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 234 | namespace { |
| 235 | |
| 236 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 237 | class BitmapInvalidator : public SkPixelRef::GenIDChangeListener { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 238 | public: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 239 | explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 240 | private: |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 241 | GrUniqueKeyInvalidatedMessage fMsg; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 242 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 243 | void onChange() override { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 244 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 245 | } |
| 246 | }; |
| 247 | |
| 248 | } // namespace |
| 249 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 250 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 251 | GrTexture* GrCreateTextureForPixels(GrContext* ctx, |
| 252 | const GrUniqueKey& optionalKey, |
| 253 | GrSurfaceDesc desc, |
| 254 | SkPixelRef* pixelRefForInvalidationNotification, |
| 255 | const void* pixels, |
| 256 | size_t rowBytes) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 257 | GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes); |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 258 | if (result && optionalKey.isValid()) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 259 | if (pixelRefForInvalidationNotification) { |
| 260 | BitmapInvalidator* listener = new BitmapInvalidator(optionalKey); |
| 261 | pixelRefForInvalidationNotification->addGenIDChangeListener(listener); |
| 262 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 263 | ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result); |
sugoi | 0249ec2 | 2014-09-09 08:12:34 -0700 | [diff] [blame] | 264 | } |
| 265 | return result; |
| 266 | } |
| 267 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 268 | // creates a new texture that is the input texture scaled up. If optionalKey is valid it will be |
| 269 | // set on the new texture. stretch controls whether the scaling is done using nearest or bilerp |
| 270 | // filtering and the size to stretch the texture to. |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 271 | GrTexture* stretch_texture(GrTexture* inputTexture, const SkGrStretch& stretch, |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 272 | SkPixelRef* pixelRef, |
| 273 | const GrUniqueKey& optionalKey) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 274 | SkASSERT(SkGrStretch::kNone_Type != stretch.fType); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 275 | |
| 276 | GrContext* context = inputTexture->getContext(); |
| 277 | SkASSERT(context); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 278 | const GrCaps* caps = context->caps(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 279 | |
| 280 | // Either it's a cache miss or the original wasn't cached to begin with. |
| 281 | GrSurfaceDesc rtDesc = inputTexture->desc(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 282 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 283 | rtDesc.fWidth = stretch.fWidth; |
| 284 | rtDesc.fHeight = stretch.fHeight; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 285 | rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig); |
| 286 | |
| 287 | // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise, |
| 288 | // fail. |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 289 | if (!caps->isConfigRenderable(rtDesc.fConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 290 | if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 291 | if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 292 | rtDesc.fConfig = kAlpha_8_GrPixelConfig; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 293 | } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 294 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 295 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 296 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 297 | } |
| 298 | } else if (kRGB_GrColorComponentFlags == |
| 299 | (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 300 | if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 301 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 302 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 303 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 304 | } |
| 305 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 306 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 310 | SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc, |
| 311 | pixelRef, nullptr,0)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 312 | if (!stretched) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 313 | return nullptr; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 314 | } |
| 315 | GrPaint paint; |
| 316 | |
| 317 | // If filtering is not desired then we want to ensure all texels in the resampled image are |
| 318 | // copies of texels from the original. |
| 319 | GrTextureParams params(SkShader::kClamp_TileMode, |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 320 | SkGrStretch::kBilerp_Type == stretch.fType ? |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 321 | GrTextureParams::kBilerp_FilterMode : |
| 322 | GrTextureParams::kNone_FilterMode); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 323 | paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params); |
| 324 | |
| 325 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight)); |
| 326 | SkRect localRect = SkRect::MakeWH(1.f, 1.f); |
| 327 | |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 328 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 329 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 330 | return nullptr; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint, |
| 334 | SkMatrix::I(), rect, localRect); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 335 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 336 | return stretched.detach(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 337 | } |
| 338 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 339 | GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
| 340 | int expectedW, int expectedH, |
| 341 | const void** outStartOfDataToUpload) { |
| 342 | *outStartOfDataToUpload = nullptr; |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 343 | #ifndef SK_IGNORE_ETC1_SUPPORT |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 344 | if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) { |
| 345 | return kUnknown_GrPixelConfig; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 346 | } |
| 347 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 348 | const uint8_t* bytes = data->bytes(); |
| 349 | if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 350 | // Does the data match the dimensions of the bitmap? If not, |
| 351 | // then we don't know how to scale the image to match it... |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 352 | if (etc1_pkm_get_width(bytes) != (unsigned)expectedW || |
| 353 | etc1_pkm_get_height(bytes) != (unsigned)expectedH) |
| 354 | { |
| 355 | return kUnknown_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 356 | } |
| 357 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 358 | *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE; |
| 359 | return kETC1_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 360 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 361 | SkKTXFile ktx(data); |
| 362 | |
| 363 | // Is it actually an ETC1 texture? |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 364 | if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 365 | return kUnknown_GrPixelConfig; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Does the data match the dimensions of the bitmap? If not, |
| 369 | // then we don't know how to scale the image to match it... |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 370 | if (ktx.width() != expectedW || ktx.height() != expectedH) { |
| 371 | return kUnknown_GrPixelConfig; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 372 | } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 373 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 374 | *outStartOfDataToUpload = ktx.pixelData(); |
| 375 | return kETC1_GrPixelConfig; |
| 376 | } |
| 377 | #endif |
| 378 | return kUnknown_GrPixelConfig; |
| 379 | } |
| 380 | |
| 381 | static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
| 382 | const SkBitmap &bm, GrSurfaceDesc desc) { |
| 383 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
| 384 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 385 | return nullptr; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 386 | } |
| 387 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 388 | const void* startOfTexData; |
| 389 | desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(), |
| 390 | &startOfTexData); |
| 391 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
| 395 | return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 396 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will |
| 400 | * be gone from SkPixelRef, and we can remove this subclass entirely. |
| 401 | */ |
| 402 | class PixelRef_GrYUVProvider : public GrYUVProvider { |
| 403 | SkPixelRef* fPR; |
| 404 | |
| 405 | public: |
| 406 | PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {} |
| 407 | |
| 408 | uint32_t onGetID() override { return fPR->getGenerationID(); } |
| 409 | bool onGetYUVSizes(SkISize sizes[3]) override { |
| 410 | return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr); |
| 411 | } |
| 412 | bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
| 413 | SkYUVColorSpace* space) override { |
| 414 | return fPR->getYUV8Planes(sizes, planes, rowBytes, space); |
| 415 | } |
| 416 | }; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 417 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 418 | static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 419 | const SkBitmap& bm, const GrSurfaceDesc& desc) { |
sugoi | ff58e46 | 2014-10-16 05:19:31 -0700 | [diff] [blame] | 420 | // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 421 | SkPixelRef* pixelRef = bm.pixelRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 422 | if ((nullptr == pixelRef) || |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 423 | (pixelRef->info().width() != bm.info().width()) || |
| 424 | (pixelRef->info().height() != bm.info().height())) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 425 | return nullptr; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 426 | } |
| 427 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 428 | const bool useCache = optionalKey.isValid(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 429 | PixelRef_GrYUVProvider provider(pixelRef); |
| 430 | GrTexture* texture = provider.refAsTexture(ctx, desc, useCache); |
| 431 | if (!texture) { |
| 432 | return nullptr; |
| 433 | } |
| 434 | |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 435 | if (useCache) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 436 | BitmapInvalidator* listener = new BitmapInvalidator(optionalKey); |
| 437 | pixelRef->addGenIDChangeListener(listener); |
| 438 | ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture); |
sugoi | ba18f91 | 2015-02-04 10:53:03 -0800 | [diff] [blame] | 439 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 440 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 441 | } |
| 442 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 443 | static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx, |
| 444 | const SkBitmap& origBitmap, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 445 | const GrUniqueKey& optionalKey) { |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 446 | if (origBitmap.width() < ctx->caps()->minTextureSize() || |
| 447 | origBitmap.height() < ctx->caps()->minTextureSize()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 448 | return nullptr; |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 449 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | SkBitmap tmpBitmap; |
| 451 | |
| 452 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 453 | |
reed | 3322a81 | 2015-09-16 10:09:24 -0700 | [diff] [blame] | 454 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info()); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 455 | const GrCaps* caps = ctx->caps(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 456 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 457 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 458 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 459 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 460 | bitmap->width(), bitmap->height()); |
| 461 | SkAutoMalloc storage(imageSize); |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 462 | build_index8_data(storage.get(), origBitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 463 | |
| 464 | // our compressed data will be trimmed, so pass width() for its |
| 465 | // "rowBytes", since they are the same now. |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 466 | return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 467 | storage.get(), bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 469 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | // now bitmap points to our temp, which has been promoted to 32bits |
| 471 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 472 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 474 | } else if (!bitmap->readyToDraw()) { |
| 475 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 476 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 477 | // the bitmap has available pixels, then they might not be what the decompressed |
| 478 | // data is. |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 479 | GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 480 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 481 | return texture; |
| 482 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 483 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 485 | GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc); |
| 486 | if (texture) { |
| 487 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 488 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 489 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 490 | SkAutoLockPixels alp(*bitmap); |
| 491 | if (!bitmap->readyToDraw()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 492 | return nullptr; |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 493 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 494 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 495 | return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 496 | bitmap->getPixels(), bitmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 497 | } |
| 498 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 499 | static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const SkGrStretch& stretch) { |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 500 | SkBitmap stretched; |
| 501 | stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight); |
| 502 | SkCanvas canvas(stretched); |
| 503 | SkPaint paint; |
| 504 | switch (stretch.fType) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 505 | case SkGrStretch::kNearest_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 506 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 507 | break; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 508 | case SkGrStretch::kBilerp_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 509 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 510 | break; |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 511 | case SkGrStretch::kNone_Type: |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 512 | SkDEBUGFAIL("Shouldn't get here."); |
| 513 | break; |
| 514 | } |
| 515 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight)); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 516 | canvas.drawBitmapRect(bmp, dstRect, &paint); |
bsalomon | b4d40ef | 2015-07-15 10:12:16 -0700 | [diff] [blame] | 517 | return stretched; |
| 518 | } |
| 519 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 520 | bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& subset, |
| 521 | GrTexture* nativeTexture, const GrTextureParams* params) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 522 | SkGrStretch stretch; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 523 | get_stretch(ctx, subset.width(), subset.height(), params, &stretch); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 524 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 525 | // Handle the case where the bitmap/image is explicitly texture backed. |
| 526 | if (nativeTexture) { |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 527 | if (SkGrStretch::kNone_Type == stretch.fType) { |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 528 | return true; |
| 529 | } |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 530 | const GrUniqueKey& key = nativeTexture->getUniqueKey(); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 531 | if (!key.isValid()) { |
| 532 | return false; |
| 533 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 534 | GrUniqueKey stretchedKey; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 535 | GrMakeStretchedKey(key, stretch, &stretchedKey); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 536 | return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey); |
bsalomon | 9ed7f57 | 2014-12-19 12:26:37 -0800 | [diff] [blame] | 537 | } |
| 538 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 539 | GrUniqueKey key, stretchedKey; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 540 | make_image_keys(imageID, subset, stretch, &key, &stretchedKey); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 541 | return ctx->textureProvider()->existsTextureWithUniqueKey( |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 542 | (SkGrStretch::kNone_Type == stretch.fType) ? key : stretchedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 543 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 544 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 545 | class Bitmap_GrTextureMaker : public GrTextureMaker { |
| 546 | public: |
| 547 | Bitmap_GrTextureMaker(const SkBitmap& bitmap) |
| 548 | : INHERITED(bitmap.width(), bitmap.height()) |
| 549 | , fBitmap(bitmap) |
| 550 | {} |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 551 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 552 | protected: |
| 553 | GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { |
| 554 | GrTexture* tex = fBitmap.getTexture(); |
| 555 | if (tex) { |
| 556 | return SkRef(tex); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 557 | } |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 558 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 559 | GrUniqueKey unstretchedKey; |
| 560 | make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 561 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 562 | GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 563 | if (result) { |
| 564 | return result; |
| 565 | } |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 566 | return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 567 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 568 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 569 | bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKey) override { |
| 570 | if (fBitmap.isVolatile()) { |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | GrUniqueKey unstretchedKey; |
| 575 | make_unstretched_key(&unstretchedKey, fBitmap.getGenerationID(), fBitmap.getSubset()); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 576 | return GrMakeStretchedKey(unstretchedKey, stretch, stretchedKey); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 577 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 578 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 579 | void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { |
| 580 | fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretchedKey)); |
| 581 | } |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 582 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 583 | bool onGetROBitmap(SkBitmap* bitmap) override { |
| 584 | *bitmap = fBitmap; |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | private: |
| 589 | const SkBitmap fBitmap; |
| 590 | |
| 591 | typedef GrTextureMaker INHERITED; |
| 592 | }; |
| 593 | |
| 594 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
| 595 | const GrTextureParams* params) { |
| 596 | return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 597 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 598 | |
| 599 | // TODO: make this be the canonical signature, and turn the version that takes GrTextureParams* |
| 600 | // into a wrapper that contains the inverse of these tables. |
| 601 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, |
| 602 | const SkBitmap& bitmap, |
| 603 | SkImageUsageType usage) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 604 | GrTextureParams params = GrImageUsageToTextureParams(usage); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 605 | return GrRefCachedBitmapTexture(ctx, bitmap, ¶ms); |
| 606 | } |
| 607 | |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 608 | /////////////////////////////////////////////////////////////////////////////// |
| 609 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 610 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 611 | // alpha info, that will be considered. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 612 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 613 | switch (ct) { |
| 614 | case kUnknown_SkColorType: |
| 615 | return kUnknown_GrPixelConfig; |
| 616 | case kAlpha_8_SkColorType: |
| 617 | return kAlpha_8_GrPixelConfig; |
| 618 | case kRGB_565_SkColorType: |
| 619 | return kRGB_565_GrPixelConfig; |
| 620 | case kARGB_4444_SkColorType: |
| 621 | return kRGBA_4444_GrPixelConfig; |
| 622 | case kRGBA_8888_SkColorType: |
jvanverth | 99babf2 | 2015-05-22 06:06:40 -0700 | [diff] [blame] | 623 | //if (kSRGB_SkColorProfileType == pt) { |
| 624 | // return kSRGBA_8888_GrPixelConfig; |
| 625 | //} |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 626 | return kRGBA_8888_GrPixelConfig; |
| 627 | case kBGRA_8888_SkColorType: |
| 628 | return kBGRA_8888_GrPixelConfig; |
| 629 | case kIndex_8_SkColorType: |
| 630 | return kIndex_8_GrPixelConfig; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 631 | case kGray_8_SkColorType: |
| 632 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 633 | } |
| 634 | SkASSERT(0); // shouldn't get here |
| 635 | return kUnknown_GrPixelConfig; |
| 636 | } |
| 637 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 638 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 639 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 640 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 641 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 642 | switch (config) { |
| 643 | case kAlpha_8_GrPixelConfig: |
| 644 | ct = kAlpha_8_SkColorType; |
| 645 | break; |
| 646 | case kIndex_8_GrPixelConfig: |
| 647 | ct = kIndex_8_SkColorType; |
| 648 | break; |
| 649 | case kRGB_565_GrPixelConfig: |
| 650 | ct = kRGB_565_SkColorType; |
| 651 | break; |
| 652 | case kRGBA_4444_GrPixelConfig: |
| 653 | ct = kARGB_4444_SkColorType; |
| 654 | break; |
| 655 | case kRGBA_8888_GrPixelConfig: |
| 656 | ct = kRGBA_8888_SkColorType; |
| 657 | break; |
| 658 | case kBGRA_8888_GrPixelConfig: |
| 659 | ct = kBGRA_8888_SkColorType; |
| 660 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 661 | case kSRGBA_8888_GrPixelConfig: |
| 662 | ct = kRGBA_8888_SkColorType; |
| 663 | pt = kSRGB_SkColorProfileType; |
| 664 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 665 | default: |
| 666 | return false; |
| 667 | } |
| 668 | if (ctOut) { |
| 669 | *ctOut = ct; |
| 670 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 671 | if (ptOut) { |
| 672 | *ptOut = pt; |
| 673 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 674 | return true; |
| 675 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 676 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 677 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 678 | //////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 679 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame^] | 680 | static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) { |
| 681 | if (primitiveIsSrc) { |
| 682 | return SkXfermode::kSrc_Mode != mode; |
| 683 | } else { |
| 684 | return SkXfermode::kDst_Mode != mode; |
| 685 | } |
| 686 | } |
| 687 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 688 | static inline bool skpaint_to_grpaint_impl(GrContext* context, |
| 689 | const SkPaint& skPaint, |
| 690 | const SkMatrix& viewM, |
| 691 | const GrFragmentProcessor** shaderProcessor, |
| 692 | SkXfermode::Mode* primColorMode, |
| 693 | bool primitiveIsSrc, |
| 694 | GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 695 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 696 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 697 | // Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 698 | // of per-vertex colors. |
| 699 | SkAutoTUnref<const GrFragmentProcessor> aufp; |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame^] | 700 | const GrFragmentProcessor* shaderFP = nullptr; |
| 701 | if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { |
| 702 | if (shaderProcessor) { |
| 703 | shaderFP = *shaderProcessor; |
| 704 | } else if (const SkShader* shader = skPaint.getShader()) { |
| 705 | aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr, |
| 706 | skPaint.getFilterQuality(), |
| 707 | grPaint->getProcessorDataManager())); |
| 708 | shaderFP = aufp; |
| 709 | if (!shaderFP) { |
| 710 | return false; |
| 711 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 716 | // a known constant value. In that case we can simply apply a color filter during this |
| 717 | // conversion without converting the color filter to a GrFragmentProcessor. |
| 718 | bool applyColorFilterToPaintColor = false; |
| 719 | if (shaderFP) { |
| 720 | if (primColorMode) { |
| 721 | // There is a blend between the primitive color and the shader color. The shader sees |
| 722 | // the opaque paint color. The shader's output is blended using the provided mode by |
| 723 | // the primitive color. The blended color is then modulated by the paint's alpha. |
| 724 | |
| 725 | // The geometry processor will insert the primitive color to start the color chain, so |
| 726 | // the GrPaint color will be ignored. |
| 727 | |
| 728 | GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor()); |
| 729 | |
| 730 | shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput); |
| 731 | aufp.reset(shaderFP); |
| 732 | |
| 733 | if (primitiveIsSrc) { |
| 734 | shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP, |
| 735 | *primColorMode); |
| 736 | } else { |
| 737 | shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP, |
| 738 | *primColorMode); |
| 739 | } |
| 740 | aufp.reset(shaderFP); |
| 741 | // The above may return null if compose results in a pass through of the prim color. |
| 742 | if (shaderFP) { |
| 743 | grPaint->addColorFragmentProcessor(shaderFP); |
| 744 | } |
| 745 | |
| 746 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
| 747 | if (GrColor_WHITE != paintAlpha) { |
| 748 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 749 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 750 | } |
| 751 | } else { |
| 752 | // The shader's FP sees the paint unpremul color |
| 753 | grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor())); |
| 754 | grPaint->addColorFragmentProcessor(shaderFP); |
| 755 | } |
| 756 | } else { |
| 757 | if (primColorMode) { |
| 758 | // There is a blend between the primitive color and the paint color. The blend considers |
| 759 | // the opaque paint color. The paint's alpha is applied to the post-blended color. |
| 760 | SkAutoTUnref<const GrFragmentProcessor> processor( |
| 761 | GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()), |
| 762 | GrConstColorProcessor::kIgnore_InputMode)); |
| 763 | if (primitiveIsSrc) { |
| 764 | processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor, |
| 765 | *primColorMode)); |
| 766 | } else { |
| 767 | processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor, |
| 768 | *primColorMode)); |
| 769 | |
| 770 | } |
| 771 | if (processor) { |
| 772 | grPaint->addColorFragmentProcessor(processor); |
| 773 | } |
| 774 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame^] | 775 | grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor())); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 776 | |
| 777 | GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame^] | 778 | if (GrColor_WHITE != paintAlpha) { |
| 779 | grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create( |
| 780 | paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref(); |
| 781 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 782 | } else { |
| 783 | // No shader, no primitive color. |
| 784 | grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor())); |
| 785 | applyColorFilterToPaintColor = true; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 790 | if (colorFilter) { |
| 791 | if (applyColorFilterToPaintColor) { |
| 792 | grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor()))); |
| 793 | } else { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 794 | SkAutoTUnref<const GrFragmentProcessor> cfFP( |
| 795 | colorFilter->asFragmentProcessor(context, grPaint->getProcessorDataManager())); |
| 796 | if (cfFP) { |
| 797 | grPaint->addColorFragmentProcessor(cfFP); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 798 | } else { |
| 799 | return false; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 804 | SkXfermode* mode = skPaint.getXfermode(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 805 | GrXPFactory* xpFactory = nullptr; |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame] | 806 | if (!SkXfermode::AsXPFactory(mode, &xpFactory)) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 807 | // Fall back to src-over |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 808 | // return false here? |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 809 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 810 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 811 | SkASSERT(xpFactory); |
| 812 | grPaint->setXPFactory(xpFactory)->unref(); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 813 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 814 | #ifndef SK_IGNORE_GPU_DITHER |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 815 | if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) { |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 816 | grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 817 | } |
| 818 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 819 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 820 | } |
| 821 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 822 | bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM, |
| 823 | GrPaint* grPaint) { |
| 824 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint); |
| 825 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 826 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 827 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 828 | bool SkPaintToGrPaintReplaceShader(GrContext* context, |
| 829 | const SkPaint& skPaint, |
| 830 | const GrFragmentProcessor* shaderFP, |
| 831 | GrPaint* grPaint) { |
| 832 | if (!shaderFP) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 833 | return false; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 834 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 835 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false, |
| 836 | grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 837 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 838 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 839 | /** Ignores the SkShader (if any) on skPaint. */ |
| 840 | bool SkPaintToGrPaintNoShader(GrContext* context, |
| 841 | const SkPaint& skPaint, |
| 842 | GrPaint* grPaint) { |
| 843 | // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced. |
| 844 | static const GrFragmentProcessor* kNullShaderFP = nullptr; |
| 845 | static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP; |
| 846 | return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false, |
| 847 | grPaint); |
| 848 | } |
| 849 | |
| 850 | /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
| 851 | be setup as a vertex attribute using the specified SkXfermode::Mode. */ |
| 852 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
| 853 | const SkPaint& skPaint, |
| 854 | const SkMatrix& viewM, |
| 855 | SkXfermode::Mode primColorMode, |
| 856 | bool primitiveIsSrc, |
| 857 | GrPaint* grPaint) { |
| 858 | return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc, |
| 859 | grPaint); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 864 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 865 | SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { |
| 866 | #ifdef SK_DEBUG |
| 867 | const GrSurfaceDesc& desc = tex->desc(); |
| 868 | SkASSERT(w <= desc.fWidth); |
| 869 | SkASSERT(h <= desc.fHeight); |
| 870 | #endif |
| 871 | const GrPixelConfig config = tex->config(); |
| 872 | SkColorType ct; |
| 873 | SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 874 | if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 875 | ct = kUnknown_SkColorType; |
| 876 | } |
| 877 | return SkImageInfo::Make(w, h, ct, at); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) { |
| 882 | const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque); |
| 883 | dst->setInfo(info); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 884 | dst->setPixelRef(new SkGrPixelRef(info, src))->unref(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 885 | } |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 886 | |
| 887 | GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 888 | const SkMatrix& viewM, |
| 889 | const SkMatrix& localM, |
| 890 | bool* doBicubic) { |
| 891 | *doBicubic = false; |
| 892 | GrTextureParams::FilterMode textureFilterMode; |
| 893 | switch (paintFilterQuality) { |
| 894 | case kNone_SkFilterQuality: |
| 895 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 896 | break; |
| 897 | case kLow_SkFilterQuality: |
| 898 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 899 | break; |
| 900 | case kMedium_SkFilterQuality: { |
| 901 | SkMatrix matrix; |
| 902 | matrix.setConcat(viewM, localM); |
| 903 | if (matrix.getMinScale() < SK_Scalar1) { |
| 904 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 905 | } else { |
| 906 | // Don't trigger MIP level generation unnecessarily. |
| 907 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 908 | } |
| 909 | break; |
| 910 | } |
| 911 | case kHigh_SkFilterQuality: { |
| 912 | SkMatrix matrix; |
| 913 | matrix.setConcat(viewM, localM); |
| 914 | *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode); |
| 915 | break; |
| 916 | } |
| 917 | default: |
| 918 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 919 | "Sorry, I don't understand the filtering " |
| 920 | "mode you asked for. Falling back to " |
| 921 | "MIPMaps."); |
| 922 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 923 | break; |
| 924 | |
| 925 | } |
| 926 | return textureFilterMode; |
| 927 | } |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 928 | |
| 929 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 930 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 931 | GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, SkImageUsageType usage) { |
| 932 | GrTextureParams params = GrImageUsageToTextureParams(usage); |
| 933 | return this->refCachedTexture(ctx, ¶ms); |
| 934 | } |
| 935 | |
reed | b5d3263 | 2015-09-29 13:36:50 -0700 | [diff] [blame] | 936 | GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParams* params) { |
| 937 | SkGrStretch stretch; |
| 938 | get_stretch(ctx, this->width(), this->height(), params, &stretch); |
| 939 | |
| 940 | if (SkGrStretch::kNone_Type == stretch.fType) { |
| 941 | return this->onRefUnstretchedTexture(ctx); |
| 942 | } |
| 943 | |
| 944 | GrUniqueKey stretchedKey; |
| 945 | if (this->onMakeStretchedKey(stretch, &stretchedKey)) { |
| 946 | GrTexture* result = ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey); |
| 947 | if (result) { |
| 948 | return result; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | GrTexture* result = this->onGenerateStretchedTexture(ctx, stretch); |
| 953 | if (!result) { |
| 954 | return nullptr; |
| 955 | } |
| 956 | |
| 957 | if (stretchedKey.isValid()) { |
| 958 | ctx->textureProvider()->assignUniqueKeyToTexture(stretchedKey, result); |
| 959 | this->onNotifyStretchCached(stretchedKey); |
| 960 | } |
| 961 | return result; |
| 962 | } |
| 963 | |
| 964 | GrTexture* GrTextureMaker::onGenerateStretchedTexture(GrContext* ctx, const SkGrStretch& stretch) { |
| 965 | if (this->width() < ctx->caps()->minTextureSize() || |
| 966 | this->height() < ctx->caps()->minTextureSize()) |
| 967 | { |
| 968 | // we can't trust our ability to use HW to perform the stretch, so we request |
| 969 | // a raster instead, and perform the stretch on the CPU. |
| 970 | SkBitmap bitmap; |
| 971 | if (!this->onGetROBitmap(&bitmap)) { |
| 972 | return nullptr; |
| 973 | } |
| 974 | SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch); |
| 975 | return create_unstretched_bitmap_texture(ctx, stretchedBmp, GrUniqueKey()); |
| 976 | } else { |
| 977 | SkAutoTUnref<GrTexture> unstretched(this->onRefUnstretchedTexture(ctx)); |
| 978 | if (!unstretched) { |
| 979 | return nullptr; |
| 980 | } |
| 981 | return stretch_texture(unstretched, stretch, nullptr, GrUniqueKey()); |
| 982 | } |
| 983 | } |