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" |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 9 | #include "SkColorFilter.h" |
commit-bot@chromium.org | ea476e1 | 2013-10-14 18:29:23 +0000 | [diff] [blame] | 10 | #include "SkConfig8888.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 12 | #include "SkMessageBus.h" |
| 13 | #include "SkPixelRef.h" |
| 14 | #include "GrResourceCache.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame^] | 16 | #include "effects/GrDitherEffect.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 17 | #include "GrDrawTargetCaps.h" |
| 18 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 19 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 20 | # include "ktx.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 21 | # include "etc1.h" |
| 22 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
| 24 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 25 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 26 | |
| 27 | 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] | 28 | we could detect that the colortable.count is <= 16, and then repack the |
| 29 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 30 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 31 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 33 | as the colortable.count says it is. |
| 34 | */ |
| 35 | static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 36 | SkASSERT(kIndex_8_SkColorType == bitmap.colorType()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 38 | SkAutoLockPixels alp(bitmap); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | if (!bitmap.readyToDraw()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 40 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
| 44 | SkColorTable* ctable = bitmap.getColorTable(); |
| 45 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 46 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 47 | const int count = ctable->count(); |
| 48 | |
| 49 | SkDstPixelInfo dstPI; |
| 50 | dstPI.fColorType = kRGBA_8888_SkColorType; |
| 51 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 52 | dstPI.fPixels = buffer; |
| 53 | dstPI.fRowBytes = count * sizeof(SkPMColor); |
| 54 | |
| 55 | SkSrcPixelInfo srcPI; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 56 | srcPI.fColorType = kN32_SkColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 57 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 58 | srcPI.fPixels = ctable->lockColors(); |
| 59 | srcPI.fRowBytes = count * sizeof(SkPMColor); |
| 60 | |
| 61 | srcPI.convertPixelsTo(&dstPI, count, 1); |
| 62 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 63 | ctable->unlockColors(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 64 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | // always skip a full 256 number of entries, even if we memcpy'd fewer |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 66 | dst += kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 68 | if ((unsigned)bitmap.width() == bitmap.rowBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 70 | } else { |
| 71 | // need to trim off the extra bytes per row |
| 72 | size_t width = bitmap.width(); |
| 73 | size_t rowBytes = bitmap.rowBytes(); |
| 74 | const char* src = (const char*)bitmap.getPixels(); |
| 75 | for (int y = 0; y < bitmap.height(); y++) { |
| 76 | memcpy(dst, src, width); |
| 77 | src += rowBytes; |
| 78 | dst += width; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | //////////////////////////////////////////////////////////////////////////////// |
| 84 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 85 | static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { |
| 86 | // Our id includes the offset, width, and height so that bitmaps created by extractSubset() |
| 87 | // are unique. |
| 88 | uint32_t genID = bitmap.getGenerationID(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 89 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 90 | int16_t width = SkToS16(bitmap.width()); |
| 91 | int16_t height = SkToS16(bitmap.height()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 92 | |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 93 | GrCacheID::Key key; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 94 | memcpy(key.fData8 + 0, &genID, 4); |
| 95 | memcpy(key.fData8 + 4, &origin.fX, 4); |
| 96 | memcpy(key.fData8 + 8, &origin.fY, 4); |
| 97 | memcpy(key.fData8 + 12, &width, 2); |
| 98 | memcpy(key.fData8 + 14, &height, 2); |
| 99 | static const size_t kKeyDataSize = 16; |
bsalomon@google.com | 10a9fb8 | 2013-01-02 19:29:57 +0000 | [diff] [blame] | 100 | memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 101 | GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 102 | static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); |
| 103 | id->reset(gBitmapTextureDomain, key); |
| 104 | } |
| 105 | |
| 106 | static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { |
| 107 | desc->fFlags = kNone_GrTextureFlags; |
| 108 | desc->fWidth = bitmap.width(); |
| 109 | desc->fHeight = bitmap.height(); |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 110 | desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info()); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 111 | desc->fSampleCnt = 0; |
| 112 | } |
| 113 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 114 | namespace { |
| 115 | |
| 116 | // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. |
| 117 | class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { |
| 118 | public: |
| 119 | explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} |
| 120 | private: |
| 121 | GrResourceKey fKey; |
| 122 | |
| 123 | virtual void onChange() SK_OVERRIDE { |
| 124 | const GrResourceInvalidatedMessage message = { fKey }; |
| 125 | SkMessageBus<GrResourceInvalidatedMessage>::Post(message); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | } // namespace |
| 130 | |
| 131 | static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { |
| 132 | SkASSERT(NULL != pixelRef); |
| 133 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); |
| 134 | } |
| 135 | |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 136 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 137 | static GrTexture *load_etc1_texture(GrContext* ctx, |
| 138 | const GrTextureParams* params, |
| 139 | const SkBitmap &bm, GrTextureDesc desc) { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 140 | SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData()); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 141 | |
| 142 | // Is this even encoded data? |
| 143 | if (NULL == data) { |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | // Is this a valid PKM encoded data? |
| 148 | const uint8_t *bytes = data->bytes(); |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 149 | if (etc1_pkm_is_valid(bytes)) { |
| 150 | uint32_t encodedWidth = etc1_pkm_get_width(bytes); |
| 151 | uint32_t encodedHeight = etc1_pkm_get_height(bytes); |
| 152 | |
| 153 | // Does the data match the dimensions of the bitmap? If not, |
| 154 | // then we don't know how to scale the image to match it... |
| 155 | if (encodedWidth != static_cast<uint32_t>(bm.width()) || |
| 156 | encodedHeight != static_cast<uint32_t>(bm.height())) { |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | // Everything seems good... skip ahead to the data. |
| 161 | bytes += ETC_PKM_HEADER_SIZE; |
| 162 | desc.fConfig = kETC1_GrPixelConfig; |
| 163 | } else if (SkKTXFile::is_ktx(bytes)) { |
| 164 | SkKTXFile ktx(data); |
| 165 | |
| 166 | // Is it actually an ETC1 texture? |
| 167 | if (!ktx.isETC1()) { |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | // Does the data match the dimensions of the bitmap? If not, |
| 172 | // then we don't know how to scale the image to match it... |
| 173 | if (ktx.width() != bm.width() || ktx.height() != bm.height()) { |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | bytes = ktx.pixelData(); |
| 178 | desc.fConfig = kETC1_GrPixelConfig; |
| 179 | } else { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
| 182 | |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 183 | // This texture is likely to be used again so leave it in the cache |
| 184 | GrCacheID cacheID; |
| 185 | generate_bitmap_cache_id(bm, &cacheID); |
| 186 | |
| 187 | GrResourceKey key; |
| 188 | GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key); |
| 189 | if (NULL != result) { |
| 190 | add_genID_listener(key, bm.pixelRef()); |
| 191 | } |
| 192 | return result; |
| 193 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 194 | #endif // SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 195 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 196 | static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 197 | bool cache, |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 198 | const GrTextureParams* params, |
| 199 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | SkBitmap tmpBitmap; |
| 201 | |
| 202 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 203 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 204 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 205 | generate_bitmap_texture_desc(*bitmap, &desc); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 206 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 207 | if (kIndex_8_SkColorType == bitmap->colorType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | // build_compressed_data doesn't do npot->pot expansion |
| 209 | // and paletted textures can't be sub-updated |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 210 | if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { |
| 211 | size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | SkAutoMalloc storage(imagesize); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 213 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | build_compressed_data(storage.get(), origBitmap); |
| 215 | |
| 216 | // our compressed data will be trimmed, so pass width() for its |
| 217 | // "rowBytes", since they are the same now. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 218 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 219 | if (cache) { |
| 220 | GrCacheID cacheID; |
| 221 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 222 | |
| 223 | GrResourceKey key; |
| 224 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 225 | storage.get(), bitmap->width(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 226 | if (NULL != result) { |
| 227 | add_genID_listener(key, origBitmap.pixelRef()); |
| 228 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 229 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 230 | } else { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 231 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 232 | GrContext::kExact_ScratchTexMatch); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 233 | result->writePixels(0, 0, bitmap->width(), |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 234 | bitmap->height(), desc.fConfig, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 235 | storage.get()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 236 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 237 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 239 | origBitmap.copyTo(&tmpBitmap, kN32_SkColorType); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | // now bitmap points to our temp, which has been promoted to 32bits |
| 241 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | 3adcc34 | 2014-04-23 19:18:09 +0000 | [diff] [blame] | 242 | desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | } |
krajcevski | 309e869 | 2014-06-02 08:02:45 -0700 | [diff] [blame] | 244 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 245 | |
| 246 | // Is this an ETC1 encoded texture? |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 247 | #ifndef SK_IGNORE_ETC1_SUPPORT |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 248 | else if ( |
| 249 | // We do not support scratch ETC1 textures, hence they should all be at least |
| 250 | // trying to go to the cache. |
| 251 | cache |
| 252 | // Make sure that the underlying device supports ETC1 textures before we go ahead |
| 253 | // and check the data. |
| 254 | && ctx->getGpu()->caps()->isConfigTexturable(kETC1_GrPixelConfig) |
| 255 | // If the bitmap had compressed data and was then uncompressed, it'll still return |
| 256 | // compressed data on 'refEncodedData' and upload it. Probably not good, since if |
| 257 | // the bitmap has available pixels, then they might not be what the decompressed |
| 258 | // data is. |
| 259 | && !(bitmap->readyToDraw())) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 260 | GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc); |
| 261 | if (NULL != texture) { |
| 262 | return texture; |
| 263 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 264 | } |
krajcevski | 8c111f7 | 2014-06-02 13:51:34 -0700 | [diff] [blame] | 265 | #endif // SK_IGNORE_ETC1_SUPPORT |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 266 | |
bsalomon@google.com | 7f4ad5a | 2013-05-07 19:36:43 +0000 | [diff] [blame] | 267 | SkAutoLockPixels alp(*bitmap); |
| 268 | if (!bitmap->readyToDraw()) { |
| 269 | return NULL; |
| 270 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 271 | if (cache) { |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 272 | // This texture is likely to be used again so leave it in the cache |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 273 | GrCacheID cacheID; |
| 274 | generate_bitmap_cache_id(origBitmap, &cacheID); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 275 | |
| 276 | GrResourceKey key; |
| 277 | GrTexture* result = ctx->createTexture(params, desc, cacheID, |
| 278 | bitmap->getPixels(), bitmap->rowBytes(), &key); |
commit-bot@chromium.org | 3843f3f | 2013-10-31 20:22:47 +0000 | [diff] [blame] | 279 | if (NULL != result) { |
| 280 | add_genID_listener(key, origBitmap.pixelRef()); |
| 281 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 282 | return result; |
| 283 | } else { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 284 | // This texture is unlikely to be used again (in its present form) so |
| 285 | // just use a scratch texture. This will remove the texture from the |
| 286 | // cache so no one else can find it. Additionally, once unlocked, the |
| 287 | // scratch texture will go to the end of the list for purging so will |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 288 | // likely be available for this volatile bitmap the next time around. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 289 | GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 290 | result->writePixels(0, 0, |
| 291 | bitmap->width(), bitmap->height(), |
| 292 | desc.fConfig, |
| 293 | bitmap->getPixels(), |
| 294 | bitmap->rowBytes()); |
| 295 | return result; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 296 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 299 | bool GrIsBitmapInCache(const GrContext* ctx, |
| 300 | const SkBitmap& bitmap, |
| 301 | const GrTextureParams* params) { |
| 302 | GrCacheID cacheID; |
| 303 | generate_bitmap_cache_id(bitmap, &cacheID); |
| 304 | |
| 305 | GrTextureDesc desc; |
| 306 | generate_bitmap_texture_desc(bitmap, &desc); |
| 307 | return ctx->isTextureInCache(desc, cacheID, params); |
| 308 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 310 | GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx, |
| 311 | const SkBitmap& bitmap, |
| 312 | const GrTextureParams* params) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 313 | GrTexture* result = NULL; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 314 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 315 | bool cache = !bitmap.isVolatile(); |
| 316 | |
| 317 | if (cache) { |
| 318 | // If the bitmap isn't changing try to find a cached copy first. |
| 319 | |
| 320 | GrCacheID cacheID; |
| 321 | generate_bitmap_cache_id(bitmap, &cacheID); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 322 | |
| 323 | GrTextureDesc desc; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 324 | generate_bitmap_texture_desc(bitmap, &desc); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 326 | result = ctx->findAndRefTexture(desc, cacheID, params); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 327 | } |
| 328 | if (NULL == result) { |
| 329 | result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 330 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 331 | if (NULL == result) { |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 332 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 333 | bitmap.width(), bitmap.height()); |
| 334 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 335 | return result; |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 336 | } |
| 337 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 338 | void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 339 | SkASSERT(NULL != texture->getContext()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 340 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 341 | texture->getContext()->unlockScratchTexture(texture); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 342 | texture->unref(); |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /////////////////////////////////////////////////////////////////////////////// |
| 346 | |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 347 | #ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG |
rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 348 | GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 349 | switch (config) { |
| 350 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 351 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 352 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 353 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 355 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 356 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 357 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 358 | case SkBitmap::kARGB_8888_Config: |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 359 | return kSkia8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | default: |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 361 | // kNo_Config, kA1_Config missing |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 362 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 365 | #endif |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 366 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 367 | // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
| 368 | // alpha info, that will be considered. |
| 369 | GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) { |
| 370 | switch (ct) { |
| 371 | case kUnknown_SkColorType: |
| 372 | return kUnknown_GrPixelConfig; |
| 373 | case kAlpha_8_SkColorType: |
| 374 | return kAlpha_8_GrPixelConfig; |
| 375 | case kRGB_565_SkColorType: |
| 376 | return kRGB_565_GrPixelConfig; |
| 377 | case kARGB_4444_SkColorType: |
| 378 | return kRGBA_4444_GrPixelConfig; |
| 379 | case kRGBA_8888_SkColorType: |
| 380 | return kRGBA_8888_GrPixelConfig; |
| 381 | case kBGRA_8888_SkColorType: |
| 382 | return kBGRA_8888_GrPixelConfig; |
| 383 | case kIndex_8_SkColorType: |
| 384 | return kIndex_8_GrPixelConfig; |
| 385 | } |
| 386 | SkASSERT(0); // shouldn't get here |
| 387 | return kUnknown_GrPixelConfig; |
| 388 | } |
| 389 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 390 | bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) { |
| 391 | SkColorType ct; |
| 392 | switch (config) { |
| 393 | case kAlpha_8_GrPixelConfig: |
| 394 | ct = kAlpha_8_SkColorType; |
| 395 | break; |
| 396 | case kIndex_8_GrPixelConfig: |
| 397 | ct = kIndex_8_SkColorType; |
| 398 | break; |
| 399 | case kRGB_565_GrPixelConfig: |
| 400 | ct = kRGB_565_SkColorType; |
| 401 | break; |
| 402 | case kRGBA_4444_GrPixelConfig: |
| 403 | ct = kARGB_4444_SkColorType; |
| 404 | break; |
| 405 | case kRGBA_8888_GrPixelConfig: |
| 406 | ct = kRGBA_8888_SkColorType; |
| 407 | break; |
| 408 | case kBGRA_8888_GrPixelConfig: |
| 409 | ct = kBGRA_8888_SkColorType; |
| 410 | break; |
| 411 | default: |
| 412 | return false; |
| 413 | } |
| 414 | if (ctOut) { |
| 415 | *ctOut = ct; |
| 416 | } |
| 417 | return true; |
| 418 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 419 | |
| 420 | /////////////////////////////////////////////////////////////////////////////// |
| 421 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 422 | void SkPaint2GrPaintNoShader(GrContext* context, const SkPaint& skPaint, GrColor grColor, |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 423 | bool constantColor, GrPaint* grPaint) { |
| 424 | |
| 425 | grPaint->setDither(skPaint.isDither()); |
| 426 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 427 | |
| 428 | SkXfermode::Coeff sm; |
| 429 | SkXfermode::Coeff dm; |
| 430 | |
| 431 | SkXfermode* mode = skPaint.getXfermode(); |
| 432 | GrEffectRef* xferEffect = NULL; |
| 433 | if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) { |
| 434 | if (NULL != xferEffect) { |
| 435 | grPaint->addColorEffect(xferEffect)->unref(); |
| 436 | sm = SkXfermode::kOne_Coeff; |
| 437 | dm = SkXfermode::kZero_Coeff; |
| 438 | } |
| 439 | } else { |
| 440 | //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
| 441 | // Fall back to src-over |
| 442 | sm = SkXfermode::kOne_Coeff; |
| 443 | dm = SkXfermode::kISA_Coeff; |
| 444 | } |
| 445 | grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 446 | |
| 447 | //set the color of the paint to the one of the parameter |
| 448 | grPaint->setColor(grColor); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 449 | |
| 450 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 451 | if (NULL != colorFilter) { |
| 452 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 453 | // in a shader. |
| 454 | if (constantColor) { |
| 455 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 456 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 457 | } else { |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 458 | SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(context)); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 459 | if (NULL != effect.get()) { |
| 460 | grPaint->addColorEffect(effect); |
| 461 | } |
| 462 | } |
| 463 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame^] | 464 | |
| 465 | #ifndef SK_IGNORE_GPU_DITHER |
| 466 | // If the dither flag is set, then we need to see if the underlying context |
| 467 | // supports it. If not, then install a dither effect. |
| 468 | if (skPaint.isDither() && grPaint->numColorStages() > 0) { |
| 469 | // What are we rendering into? |
| 470 | const GrRenderTarget *target = context->getRenderTarget(); |
| 471 | SkASSERT(NULL != target); |
| 472 | |
| 473 | // Suspect the dithering flag has no effect on these configs, otherwise |
| 474 | // fall back on setting the appropriate state. |
| 475 | if (target->config() == kRGBA_8888_GrPixelConfig || |
| 476 | target->config() == kBGRA_8888_GrPixelConfig) { |
| 477 | // The dither flag is set and the target is likely |
| 478 | // not going to be dithered by the GPU. |
| 479 | SkAutoTUnref<GrEffectRef> effect(GrDitherEffect::Create()); |
| 480 | if (NULL != effect.get()) { |
| 481 | grPaint->addColorEffect(effect); |
| 482 | grPaint->setDither(false); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | #endif |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 487 | } |
| 488 | |
commit-bot@chromium.org | 9e967ad | 2014-05-20 15:06:29 +0000 | [diff] [blame] | 489 | /** |
| 490 | * Unlike GrContext::AutoMatrix, this doesn't require setting a new matrix. GrContext::AutoMatrix |
| 491 | * likes to set the new matrix in its constructor because it is usually necessary to simulataneously |
| 492 | * update a GrPaint. This AutoMatrix is used while initially setting up GrPaint, however. |
| 493 | */ |
| 494 | class AutoMatrix { |
| 495 | public: |
| 496 | AutoMatrix(GrContext* context) { |
| 497 | fMatrix = context->getMatrix(); |
| 498 | fContext = context; |
| 499 | } |
| 500 | ~AutoMatrix() { |
| 501 | SkASSERT(NULL != fContext); |
| 502 | fContext->setMatrix(fMatrix); |
| 503 | } |
| 504 | private: |
| 505 | GrContext* fContext; |
| 506 | SkMatrix fMatrix; |
| 507 | }; |
| 508 | |
commit-bot@chromium.org | 3595f88 | 2014-05-19 19:35:57 +0000 | [diff] [blame] | 509 | void SkPaint2GrPaintShader(GrContext* context, const SkPaint& skPaint, |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 510 | bool constantColor, GrPaint* grPaint) { |
| 511 | SkShader* shader = skPaint.getShader(); |
| 512 | if (NULL == shader) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 513 | SkPaint2GrPaintNoShader(context, skPaint, SkColor2GrColor(skPaint.getColor()), |
| 514 | constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | |
commit-bot@chromium.org | 9e967ad | 2014-05-20 15:06:29 +0000 | [diff] [blame] | 518 | // SkShader::asNewEffect() may do offscreen rendering. Save off the current RT, clip, and |
| 519 | // matrix. We don't reset the matrix on the context because SkShader::asNewEffect may use |
| 520 | // GrContext::getMatrix() to know the transformation from local coords to device space. |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 521 | GrColor grColor = SkColor2GrColor(skPaint.getColor()); |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame^] | 522 | |
| 523 | // Start a new block here in order to preserve our context state after calling |
| 524 | // asNewEffect(). Since these calls get passed back to the client, we don't really |
| 525 | // want them messing around with the context. |
| 526 | { |
| 527 | GrContext::AutoRenderTarget art(context, NULL); |
| 528 | GrContext::AutoClip ac(context, GrContext::AutoClip::kWideOpen_InitialClip); |
| 529 | AutoMatrix am(context); |
| 530 | |
| 531 | // setup the shader as the first color effect on the paint |
| 532 | // the default grColor is the paint's color |
| 533 | GrEffectRef* grEffect = NULL; |
| 534 | if (shader->asNewEffect(context, skPaint, NULL, &grColor, &grEffect) && NULL != grEffect) { |
| 535 | SkAutoTUnref<GrEffectRef> effect(grEffect); |
| 536 | grPaint->addColorEffect(effect); |
| 537 | constantColor = false; |
| 538 | } |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 539 | } |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame^] | 540 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 541 | // The grcolor is automatically set when calling asneweffect. |
| 542 | // If the shader can be seen as an effect it returns true and adds its effect to the grpaint. |
| 543 | SkPaint2GrPaintNoShader(context, skPaint, grColor, constantColor, grPaint); |
commit-bot@chromium.org | 8dcff64 | 2014-05-15 20:32:48 +0000 | [diff] [blame] | 544 | } |