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