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" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 15 | #include "SkData.h" |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 16 | #include "SkErrorInternals.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 17 | #include "SkGrPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 18 | #include "SkMessageBus.h" |
| 19 | #include "SkPixelRef.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 20 | #include "SkResourceCache.h" |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 21 | #include "SkTextureCompressor.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 22 | #include "SkYUVPlanesCache.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 23 | #include "effects/GrDitherEffect.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 24 | #include "effects/GrPorterDuffXferProcessor.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 25 | #include "effects/GrYUVtoRGBEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 26 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 27 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 28 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 29 | # include "etc1.h" |
| 30 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | |
| 32 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 33 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 34 | |
| 35 | 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] | 36 | we could detect that the colortable.count is <= 16, and then repack the |
| 37 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 38 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 39 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 41 | as the colortable.count says it is. |
| 42 | */ |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 43 | static void build_index8_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 44 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 46 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 48 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | SkColorTable* ctable = bitmap.getColorTable(); |
| 53 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 54 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 55 | const int count = ctable->count(); |
| 56 | |
| 57 | SkDstPixelInfo dstPI; |
| 58 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 59 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 60 | dstPI.fPixels = buffer; |
| 61 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 62 | |
| 63 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 64 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 65 | srcPI.fAlphaType = kPremul_SkAlphaType; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 66 | srcPI.fPixels = ctable->readColors(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 67 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 68 | |
| 69 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 70 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | // 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] | 72 | dst += 256 * sizeof(GrColor); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 74 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 76 | } else { |
| 77 | // need to trim off the extra bytes per row |
| 78 | size_t width = bitmap.width(); |
| 79 | size_t rowBytes = bitmap.rowBytes(); |
| 80 | const char* src = (const char*)bitmap.getPixels(); |
| 81 | for (int y = 0; y < bitmap.height(); y++) { |
| 82 | memcpy(dst, src, width); |
| 83 | src += rowBytes; |
| 84 | dst += width; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | //////////////////////////////////////////////////////////////////////////////// |
| 90 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 91 | struct Stretch { |
| 92 | enum Type { |
| 93 | kNone_Type, |
| 94 | kBilerp_Type, |
| 95 | kNearest_Type |
| 96 | } fType; |
| 97 | int fWidth; |
| 98 | int fHeight; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 101 | static void get_stretch(const GrContext* ctx, int width, int height, |
| 102 | const GrTextureParams* params, Stretch* stretch) { |
| 103 | stretch->fType = Stretch::kNone_Type; |
| 104 | bool doStretch = false; |
| 105 | if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() && |
| 106 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
| 107 | doStretch = true; |
| 108 | stretch->fWidth = GrNextPow2(width); |
| 109 | stretch->fHeight = GrNextPow2(height); |
| 110 | } else if (width < ctx->caps()->minTextureSize() || |
| 111 | height < ctx->caps()->minTextureSize()) { |
| 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 | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 410 | yuvTextures[i].reset(ctx->textureProvider()->refScratchTexture(yuvDesc, |
| 411 | needsExactTexture ? GrTextureProvider::kExact_ScratchTexMatch : |
| 412 | GrTextureProvider::kApprox_ScratchTexMatch)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 413 | if (!yuvTextures[i] || |
| 414 | !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 415 | yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) { |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 416 | return NULL; |
| 417 | } |
| 418 | } |
| 419 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 420 | GrSurfaceDesc rtDesc = desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 421 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 422 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 423 | GrTexture* result = create_texture_for_bmp(ctx, optionalKey, rtDesc, pixelRef, NULL, 0); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 424 | if (!result) { |
| 425 | return NULL; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 426 | } |
| 427 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 428 | GrRenderTarget* renderTarget = result->asRenderTarget(); |
| 429 | SkASSERT(renderTarget); |
| 430 | |
| 431 | SkAutoTUnref<GrFragmentProcessor> |
| 432 | yuvToRgbProcessor(GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures[2], |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 433 | yuvInfo.fSize, yuvInfo.fColorSpace)); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 434 | GrPaint paint; |
| 435 | paint.addColorProcessor(yuvToRgbProcessor); |
| 436 | SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth), |
| 437 | SkIntToScalar(yuvInfo.fSize[0].fHeight)); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 438 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 439 | GrDrawContext* drawContext = ctx->drawContext(); |
| 440 | if (!drawContext) { |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I(), r); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 445 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 446 | return result; |
| 447 | } |
| 448 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 449 | static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx, |
| 450 | const SkBitmap& origBitmap, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 451 | const GrUniqueKey& optionalKey) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | SkBitmap tmpBitmap; |
| 453 | |
| 454 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 455 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 456 | GrSurfaceDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 457 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 458 | const GrCaps* caps = ctx->caps(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 459 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 460 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 461 | if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 462 | size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig, |
| 463 | bitmap->width(), bitmap->height()); |
| 464 | SkAutoMalloc storage(imageSize); |
bsalomon | e79a2da | 2014-10-24 12:42:51 -0700 | [diff] [blame] | 465 | build_index8_data(storage.get(), origBitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 466 | |
| 467 | // our compressed data will be trimmed, so pass width() for its |
| 468 | // "rowBytes", since they are the same now. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 469 | return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 470 | storage.get(), bitmap->width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 471 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 472 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | // now bitmap points to our temp, which has been promoted to 32bits |
| 474 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 475 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 476 | } |
krajcevski | 309e869 | 2014-06-02 08:02:45 -0700 | [diff] [blame] | 477 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 478 | |
| 479 | // Is this an ETC1 encoded texture? |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 480 | #ifndef SK_IGNORE_ETC1_SUPPORT |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 481 | // Make sure that the underlying device supports ETC1 textures before we go ahead |
| 482 | // and check the data. |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 483 | else if (caps->isConfigTexturable(kETC1_GrPixelConfig) |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 484 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 485 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 486 | // the bitmap has available pixels, then they might not be what the decompressed |
| 487 | // data is. |
| 488 | && !(bitmap->readyToDraw())) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 489 | GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 490 | if (texture) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 491 | return texture; |
| 492 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 493 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 494 | #endif // SK_IGNORE_ETC1_SUPPORT |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 495 | |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 496 | GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc); |
| 497 | if (texture) { |
| 498 | return texture; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 499 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 500 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 501 | SkAutoLockPixels alp(*bitmap); |
| 502 | if (!bitmap->readyToDraw()) { |
| 503 | return NULL; |
| 504 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 505 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 506 | return create_texture_for_bmp(ctx, optionalKey, desc, origBitmap.pixelRef(), |
| 507 | bitmap->getPixels(), bitmap->rowBytes()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static GrTexture* create_bitmap_texture(GrContext* ctx, |
| 511 | const SkBitmap& bmp, |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 512 | const Stretch& stretch, |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 513 | const GrUniqueKey& unstretchedKey, |
| 514 | const GrUniqueKey& stretchedKey) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 515 | if (Stretch::kNone_Type != stretch.fType) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 516 | SkAutoTUnref<GrTexture> unstretched; |
| 517 | // Check if we have the unstretched version in the cache, if not create it. |
| 518 | if (unstretchedKey.isValid()) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 519 | unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey)); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 520 | } |
| 521 | if (!unstretched) { |
| 522 | unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey)); |
| 523 | if (!unstretched) { |
| 524 | return NULL; |
| 525 | } |
| 526 | } |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 527 | GrTexture* stretched = stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 528 | return stretched; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey); |
| 532 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 533 | } |
| 534 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 535 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 536 | const SkBitmap& bitmap, |
| 537 | const GrTextureParams* params) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 538 | Stretch stretch; |
| 539 | get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 540 | |
| 541 | // Handle the case where the bitmap is explicitly texture backed. |
| 542 | GrTexture* texture = bitmap.getTexture(); |
| 543 | if (texture) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 544 | if (Stretch::kNone_Type == stretch.fType) { |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 545 | return true; |
| 546 | } |
| 547 | // No keys for volatile bitmaps. |
| 548 | if (bitmap.isVolatile()) { |
| 549 | return false; |
| 550 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 551 | const GrUniqueKey& key = texture->getUniqueKey(); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 552 | if (!key.isValid()) { |
| 553 | return false; |
| 554 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 555 | GrUniqueKey stretchedKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 556 | make_stretched_key(key, stretch, &stretchedKey); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 557 | return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey); |
bsalomon | 9ed7f57 | 2014-12-19 12:26:37 -0800 | [diff] [blame] | 558 | } |
| 559 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 560 | // We don't cache volatile bitmaps |
| 561 | if (bitmap.isVolatile()) { |
| 562 | return false; |
| 563 | } |
| 564 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 565 | GrUniqueKey key, stretchedKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 566 | make_bitmap_keys(bitmap, stretch, &key, &stretchedKey); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 567 | return ctx->textureProvider()->existsTextureWithUniqueKey( |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 568 | (Stretch::kNone_Type == stretch.fType) ? key : stretchedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 569 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 570 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 571 | GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, |
| 572 | const SkBitmap& bitmap, |
| 573 | const GrTextureParams* params) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 574 | |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 575 | Stretch stretch; |
| 576 | get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 577 | |
| 578 | GrTexture* result = bitmap.getTexture(); |
| 579 | if (result) { |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 580 | if (Stretch::kNone_Type == stretch.fType) { |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 581 | return SkRef(result); |
| 582 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 583 | GrUniqueKey stretchedKey; |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 584 | // Don't create a key for the resized version if the bmp is volatile. |
| 585 | if (!bitmap.isVolatile()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 586 | const GrUniqueKey& key = result->getUniqueKey(); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 587 | if (key.isValid()) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 588 | make_stretched_key(key, stretch, &stretchedKey); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 589 | GrTexture* stretched = |
| 590 | ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 591 | if (stretched) { |
| 592 | return stretched; |
| 593 | } |
| 594 | } |
| 595 | } |
bsalomon | c59a1df | 2015-06-01 07:13:42 -0700 | [diff] [blame] | 596 | return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey); |
bsalomon | 8842556 | 2015-02-04 09:12:46 -0800 | [diff] [blame] | 597 | } |
| 598 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 599 | GrUniqueKey key, resizedKey; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 600 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 601 | if (!bitmap.isVolatile()) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 602 | // If the bitmap isn't changing try to find a cached copy first. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 603 | make_bitmap_keys(bitmap, stretch, &key, &resizedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 604 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 605 | result = ctx->textureProvider()->findAndRefTextureByUniqueKey( |
| 606 | resizedKey.isValid() ? resizedKey : key); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 607 | if (result) { |
| 608 | return result; |
| 609 | } |
| 610 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 611 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 612 | result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey); |
| 613 | if (result) { |
| 614 | return result; |
| 615 | } |
bsalomon | e137db8 | 2015-01-31 20:10:56 -0800 | [diff] [blame] | 616 | |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 617 | SkErrorInternals::SetError( kInternalError_SkError, |
| 618 | "---- failed to create texture for cache [%d %d]\n", |
| 619 | bitmap.width(), bitmap.height()); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 620 | |
| 621 | return NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 622 | } |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 623 | /////////////////////////////////////////////////////////////////////////////// |
| 624 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 625 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 626 | // alpha info, that will be considered. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 627 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 628 | switch (ct) { |
| 629 | case kUnknown_SkColorType: |
| 630 | return kUnknown_GrPixelConfig; |
| 631 | case kAlpha_8_SkColorType: |
| 632 | return kAlpha_8_GrPixelConfig; |
| 633 | case kRGB_565_SkColorType: |
| 634 | return kRGB_565_GrPixelConfig; |
| 635 | case kARGB_4444_SkColorType: |
| 636 | return kRGBA_4444_GrPixelConfig; |
| 637 | case kRGBA_8888_SkColorType: |
jvanverth | 99babf2 | 2015-05-22 06:06:40 -0700 | [diff] [blame] | 638 | //if (kSRGB_SkColorProfileType == pt) { |
| 639 | // return kSRGBA_8888_GrPixelConfig; |
| 640 | //} |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 641 | return kRGBA_8888_GrPixelConfig; |
| 642 | case kBGRA_8888_SkColorType: |
| 643 | return kBGRA_8888_GrPixelConfig; |
| 644 | case kIndex_8_SkColorType: |
| 645 | return kIndex_8_GrPixelConfig; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 646 | case kGray_8_SkColorType: |
| 647 | return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 648 | } |
| 649 | SkASSERT(0); // shouldn't get here |
| 650 | return kUnknown_GrPixelConfig; |
| 651 | } |
| 652 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 653 | bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut, |
| 654 | SkColorProfileType* ptOut) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 655 | SkColorType ct; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 656 | SkColorProfileType pt = kLinear_SkColorProfileType; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 657 | switch (config) { |
| 658 | case kAlpha_8_GrPixelConfig: |
| 659 | ct = kAlpha_8_SkColorType; |
| 660 | break; |
| 661 | case kIndex_8_GrPixelConfig: |
| 662 | ct = kIndex_8_SkColorType; |
| 663 | break; |
| 664 | case kRGB_565_GrPixelConfig: |
| 665 | ct = kRGB_565_SkColorType; |
| 666 | break; |
| 667 | case kRGBA_4444_GrPixelConfig: |
| 668 | ct = kARGB_4444_SkColorType; |
| 669 | break; |
| 670 | case kRGBA_8888_GrPixelConfig: |
| 671 | ct = kRGBA_8888_SkColorType; |
| 672 | break; |
| 673 | case kBGRA_8888_GrPixelConfig: |
| 674 | ct = kBGRA_8888_SkColorType; |
| 675 | break; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 676 | case kSRGBA_8888_GrPixelConfig: |
| 677 | ct = kRGBA_8888_SkColorType; |
| 678 | pt = kSRGB_SkColorProfileType; |
| 679 | break; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 680 | default: |
| 681 | return false; |
| 682 | } |
| 683 | if (ctOut) { |
| 684 | *ctOut = ct; |
| 685 | } |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 686 | if (ptOut) { |
| 687 | *ptOut = pt; |
| 688 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 689 | return true; |
| 690 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 691 | |
| 692 | /////////////////////////////////////////////////////////////////////////////// |
| 693 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 694 | bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 695 | GrColor paintColor, bool constantColor, GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 696 | |
| 697 | grPaint->setDither(skPaint.isDither()); |
| 698 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 699 | |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 700 | SkXfermode* mode = skPaint.getXfermode(); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 701 | GrXPFactory* xpFactory = NULL; |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame] | 702 | if (!SkXfermode::AsXPFactory(mode, &xpFactory)) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 703 | // Fall back to src-over |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 704 | // return false here? |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 705 | xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 706 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 707 | SkASSERT(xpFactory); |
| 708 | grPaint->setXPFactory(xpFactory)->unref(); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 709 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 710 | //set the color of the paint to the one of the parameter |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 711 | grPaint->setColor(paintColor); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 712 | |
| 713 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 714 | if (colorFilter) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 715 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 716 | // in a shader. |
| 717 | if (constantColor) { |
| 718 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 719 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 720 | } else { |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 721 | SkTDArray<GrFragmentProcessor*> array; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 722 | // return false if failed? |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 723 | if (colorFilter->asFragmentProcessors(context, &array)) { |
| 724 | for (int i = 0; i < array.count(); ++i) { |
| 725 | grPaint->addColorProcessor(array[i]); |
| 726 | array[i]->unref(); |
| 727 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 731 | |
| 732 | #ifndef SK_IGNORE_GPU_DITHER |
| 733 | // If the dither flag is set, then we need to see if the underlying context |
| 734 | // supports it. If not, then install a dither effect. |
| 735 | if (skPaint.isDither() && grPaint->numColorStages() > 0) { |
| 736 | // What are we rendering into? |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 737 | SkASSERT(rt); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 738 | |
| 739 | // Suspect the dithering flag has no effect on these configs, otherwise |
| 740 | // fall back on setting the appropriate state. |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 741 | if (GrPixelConfigIs8888(rt->config()) || |
| 742 | GrPixelConfigIs8888(rt->config())) { |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 743 | // The dither flag is set and the target is likely |
| 744 | // not going to be dithered by the GPU. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 745 | SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create()); |
| 746 | if (fp.get()) { |
| 747 | grPaint->addColorProcessor(fp); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 748 | grPaint->setDither(false); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | #endif |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 753 | return true; |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 754 | } |
| 755 | |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 756 | bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint, |
| 757 | const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) { |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 758 | SkShader* shader = skPaint.getShader(); |
| 759 | if (NULL == shader) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 760 | return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()), |
| 761 | constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 762 | } |
| 763 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 764 | GrColor paintColor = SkColor2GrColor(skPaint.getColor()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 765 | |
| 766 | // 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] | 767 | // 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] | 768 | // want them messing around with the context. |
| 769 | { |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 770 | // Allow the shader to modify paintColor and also create an effect to be installed as |
| 771 | // the first color effect on the GrPaint. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 772 | GrFragmentProcessor* fp = NULL; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 773 | if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, &fp)) { |
| 774 | return false; |
| 775 | } |
| 776 | if (fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 777 | grPaint->addColorProcessor(fp)->unref(); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 778 | constantColor = false; |
| 779 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 780 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 781 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 782 | // The grcolor is automatically set when calling asFragmentProcessor. |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 783 | // 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] | 784 | return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 785 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 786 | |
| 787 | SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { |
| 788 | #ifdef SK_DEBUG |
| 789 | const GrSurfaceDesc& desc = tex->desc(); |
| 790 | SkASSERT(w <= desc.fWidth); |
| 791 | SkASSERT(h <= desc.fHeight); |
| 792 | #endif |
| 793 | const GrPixelConfig config = tex->config(); |
| 794 | SkColorType ct; |
| 795 | SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 796 | if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) { |
| 797 | ct = kUnknown_SkColorType; |
| 798 | } |
| 799 | return SkImageInfo::Make(w, h, ct, at); |
| 800 | } |
| 801 | |
| 802 | |
| 803 | void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) { |
| 804 | const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque); |
| 805 | dst->setInfo(info); |
| 806 | dst->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, src)))->unref(); |
| 807 | } |