| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
| 10 #include "SkImage_Base.h" | 10 #include "SkImage_Base.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (!bitmap->tryAllocPixels(fInfo, nullptr, full.getColorTable())) { | 103 if (!bitmap->tryAllocPixels(fInfo, nullptr, full.getColorTable())) { |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| 106 return full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowB
ytes(), | 106 return full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowB
ytes(), |
| 107 fOrigin.x(), fOrigin.y()); | 107 fOrigin.x(), fOrigin.y()); |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 ////////////////////////////////////////////////////////////////////////////////
////////////////// | 111 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
| 112 | 112 |
| 113 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap, const SkImage* client)
{ | 113 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap, const SkImage* client, |
| 114 SkImage::CachingHint chint) { |
| 114 if (SkBitmapCache::Find(fUniqueID, bitmap)) { | 115 if (SkBitmapCache::Find(fUniqueID, bitmap)) { |
| 115 return check_output_bitmap(*bitmap, fUniqueID); | 116 return check_output_bitmap(*bitmap, fUniqueID); |
| 116 } | 117 } |
| 117 | 118 |
| 118 if (!this->generateBitmap(bitmap)) { | 119 if (!this->generateBitmap(bitmap)) { |
| 119 return false; | 120 return false; |
| 120 } | 121 } |
| 121 | 122 |
| 122 bitmap->pixelRef()->setImmutableWithID(fUniqueID); | 123 bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
| 123 SkBitmapCache::Add(fUniqueID, *bitmap); | 124 if (SkImage::kAllow_CachingHint == chint) { |
| 124 if (client) { | 125 SkBitmapCache::Add(fUniqueID, *bitmap); |
| 125 as_IB(client)->notifyAddedToCache(); | 126 if (client) { |
| 127 as_IB(client)->notifyAddedToCache(); |
| 128 } |
| 126 } | 129 } |
| 127 | |
| 128 return true; | 130 return true; |
| 129 } | 131 } |
| 130 | 132 |
| 131 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap, const SkImage* client) { | 133 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap, const SkImage* client, |
| 132 if (this->tryLockAsBitmap(bitmap, client)) { | 134 SkImage::CachingHint chint) { |
| 135 if (this->tryLockAsBitmap(bitmap, client, chint)) { |
| 133 return check_output_bitmap(*bitmap, fUniqueID); | 136 return check_output_bitmap(*bitmap, fUniqueID); |
| 134 } | 137 } |
| 135 | 138 |
| 136 #if SK_SUPPORT_GPU | 139 #if SK_SUPPORT_GPU |
| 137 // Try to get a texture and read it back to raster (and then cache that with
our ID) | 140 // Try to get a texture and read it back to raster (and then cache that with
our ID) |
| 138 SkAutoTUnref<GrTexture> tex; | 141 SkAutoTUnref<GrTexture> tex; |
| 139 | 142 |
| 140 { | 143 { |
| 141 ScopedGenerator generator(this); | 144 ScopedGenerator generator(this); |
| 142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 145 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 153 } | 156 } |
| 154 | 157 |
| 155 const uint32_t pixelOpsFlags = 0; | 158 const uint32_t pixelOpsFlags = 0; |
| 156 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr
PixelConfig(fInfo), | 159 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr
PixelConfig(fInfo), |
| 157 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags)
) { | 160 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags)
) { |
| 158 bitmap->reset(); | 161 bitmap->reset(); |
| 159 return false; | 162 return false; |
| 160 } | 163 } |
| 161 | 164 |
| 162 bitmap->pixelRef()->setImmutableWithID(fUniqueID); | 165 bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
| 163 SkBitmapCache::Add(fUniqueID, *bitmap); | 166 if (SkImage::kAllow_CachingHint == chint) { |
| 164 if (client) { | 167 SkBitmapCache::Add(fUniqueID, *bitmap); |
| 165 as_IB(client)->notifyAddedToCache(); | 168 if (client) { |
| 169 as_IB(client)->notifyAddedToCache(); |
| 170 } |
| 166 } | 171 } |
| 167 | |
| 168 return check_output_bitmap(*bitmap, fUniqueID); | 172 return check_output_bitmap(*bitmap, fUniqueID); |
| 169 #else | 173 #else |
| 170 return false; | 174 return false; |
| 171 #endif | 175 #endif |
| 172 } | 176 } |
| 173 | 177 |
| 174 ////////////////////////////////////////////////////////////////////////////////
////////////////// | 178 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
| 175 | 179 |
| 176 #if SK_SUPPORT_GPU | 180 #if SK_SUPPORT_GPU |
| 177 | 181 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 /* | 217 /* |
| 214 * We have a 5 ways to try to return a texture (in sorted order) | 218 * We have a 5 ways to try to return a texture (in sorted order) |
| 215 * | 219 * |
| 216 * 1. Check the cache for a pre-existing one | 220 * 1. Check the cache for a pre-existing one |
| 217 * 2. Ask the generator to natively create one | 221 * 2. Ask the generator to natively create one |
| 218 * 3. Ask the generator to return a compressed form that the GPU might support | 222 * 3. Ask the generator to return a compressed form that the GPU might support |
| 219 * 4. Ask the generator to return YUV planes, which the GPU can convert | 223 * 4. Ask the generator to return YUV planes, which the GPU can convert |
| 220 * 5. Ask the generator to return RGB(A) data, which the GPU can convert | 224 * 5. Ask the generator to return RGB(A) data, which the GPU can convert |
| 221 */ | 225 */ |
| 222 GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& key
, | 226 GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& key
, |
| 223 const SkImage* client) { | 227 const SkImage* client, SkImage::Cachin
gHint chint) { |
| 224 // 1. Check the cache for a pre-existing one | 228 // 1. Check the cache for a pre-existing one |
| 225 if (key.isValid()) { | 229 if (key.isValid()) { |
| 226 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKe
y(key)) { | 230 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKe
y(key)) { |
| 227 return tex; | 231 return tex; |
| 228 } | 232 } |
| 229 } | 233 } |
| 230 | 234 |
| 231 // 2. Ask the generator to natively create one | 235 // 2. Ask the generator to natively create one |
| 232 { | 236 { |
| 233 ScopedGenerator generator(this); | 237 ScopedGenerator generator(this); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 253 ScopedGenerator generator(this); | 257 ScopedGenerator generator(this); |
| 254 Generator_GrYUVProvider provider(generator); | 258 Generator_GrYUVProvider provider(generator); |
| 255 GrTexture* tex = provider.refAsTexture(ctx, desc, true); | 259 GrTexture* tex = provider.refAsTexture(ctx, desc, true); |
| 256 if (tex) { | 260 if (tex) { |
| 257 return set_key_and_return(tex, key); | 261 return set_key_and_return(tex, key); |
| 258 } | 262 } |
| 259 } | 263 } |
| 260 | 264 |
| 261 // 5. Ask the generator to return RGB(A) data, which the GPU can convert | 265 // 5. Ask the generator to return RGB(A) data, which the GPU can convert |
| 262 SkBitmap bitmap; | 266 SkBitmap bitmap; |
| 263 if (this->tryLockAsBitmap(&bitmap, client)) { | 267 if (this->tryLockAsBitmap(&bitmap, client, chint)) { |
| 264 GrTexture* tex = GrUploadBitmapToTexture(ctx, bitmap); | 268 GrTexture* tex = GrUploadBitmapToTexture(ctx, bitmap); |
| 265 if (tex) { | 269 if (tex) { |
| 266 return set_key_and_return(tex, key); | 270 return set_key_and_return(tex, key); |
| 267 } | 271 } |
| 268 } | 272 } |
| 269 return nullptr; | 273 return nullptr; |
| 270 } | 274 } |
| 271 | 275 |
| 272 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 276 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 273 | 277 |
| 274 #include "GrTextureParamsAdjuster.h" | 278 #include "GrTextureParamsAdjuster.h" |
| 275 | 279 |
| 276 class Cacherator_GrTextureMaker : public GrTextureMaker { | 280 class Cacherator_GrTextureMaker : public GrTextureMaker { |
| 277 public: | 281 public: |
| 278 Cacherator_GrTextureMaker(GrContext* context, SkImageCacherator* cacher, con
st SkImage* client) | 282 Cacherator_GrTextureMaker(GrContext* context, SkImageCacherator* cacher, con
st SkImage* client, |
| 283 SkImage::CachingHint chint) |
| 279 : INHERITED(context, cacher->info().width(), cacher->info().height()) | 284 : INHERITED(context, cacher->info().width(), cacher->info().height()) |
| 280 , fCacher(cacher) | 285 , fCacher(cacher) |
| 281 , fClient(client) { | 286 , fClient(client) |
| 287 , fCachingHint(chint) |
| 288 { |
| 282 if (client) { | 289 if (client) { |
| 283 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), | 290 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 284 SkIRect::MakeWH(this->width(), this->height()))
; | 291 SkIRect::MakeWH(this->width(), this->height()))
; |
| 285 } | 292 } |
| 286 } | 293 } |
| 287 | 294 |
| 288 protected: | 295 protected: |
| 289 // TODO: consider overriding this, for the case where the underlying generat
or might be | 296 // TODO: consider overriding this, for the case where the underlying generat
or might be |
| 290 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) | 297 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) |
| 291 // GrTexture* generateTextureForParams(const CopyParams&) override; | 298 // GrTexture* generateTextureForParams(const CopyParams&) override; |
| 292 | 299 |
| 293 GrTexture* refOriginalTexture() override { | 300 GrTexture* refOriginalTexture() override { |
| 294 return fCacher->lockTexture(this->context(), fOriginalKey, fClient); | 301 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCac
hingHint); |
| 295 } | 302 } |
| 296 | 303 |
| 297 void makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) over
ride { | 304 void makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) over
ride { |
| 298 if (fOriginalKey.isValid()) { | 305 if (fOriginalKey.isValid()) { |
| 299 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey); | 306 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey); |
| 300 } | 307 } |
| 301 } | 308 } |
| 302 | 309 |
| 303 void didCacheCopy(const GrUniqueKey& copyKey) override { | 310 void didCacheCopy(const GrUniqueKey& copyKey) override { |
| 304 if (fClient) { | 311 if (fClient) { |
| 305 as_IB(fClient)->notifyAddedToCache(); | 312 as_IB(fClient)->notifyAddedToCache(); |
| 306 } | 313 } |
| 307 } | 314 } |
| 308 | 315 |
| 309 private: | 316 private: |
| 310 SkImageCacherator* fCacher; | 317 SkImageCacherator* fCacher; |
| 311 const SkImage* fClient; | 318 const SkImage* fClient; |
| 312 GrUniqueKey fOriginalKey; | 319 GrUniqueKey fOriginalKey; |
| 320 SkImage::CachingHint fCachingHint; |
| 313 | 321 |
| 314 typedef GrTextureMaker INHERITED; | 322 typedef GrTextureMaker INHERITED; |
| 315 }; | 323 }; |
| 316 | 324 |
| 317 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, | 325 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, |
| 318 const SkImage* client) { | 326 const SkImage* client, SkImage::Cach
ingHint chint) { |
| 319 if (!ctx) { | 327 if (!ctx) { |
| 320 return nullptr; | 328 return nullptr; |
| 321 } | 329 } |
| 322 | 330 |
| 323 return Cacherator_GrTextureMaker(ctx, this, client).refTextureForParams(para
ms); | 331 return Cacherator_GrTextureMaker(ctx, this, client, chint).refTextureForPara
ms(params); |
| 324 } | 332 } |
| 325 | 333 |
| 326 #else | 334 #else |
| 327 | 335 |
| 328 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, | 336 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, |
| 329 const SkImage* client) { | 337 const SkImage* client, SkImage::Cach
ingHint) { |
| 330 return nullptr; | 338 return nullptr; |
| 331 } | 339 } |
| 332 | 340 |
| 333 #endif | 341 #endif |
| OLD | NEW |