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