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