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