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