Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Unified Diff: src/core/SkImageCacherator.cpp

Issue 1313423002: make cacherator thread-safe (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkImageCacherator.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkImageCacherator.cpp
diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp
index a65e8fccbdc3b28ebbbe6f704d64c57323ffa0c3..24d28139093fb98f301a1ad7e11302c627019165 100644
--- a/src/core/SkImageCacherator.cpp
+++ b/src/core/SkImageCacherator.cpp
@@ -51,13 +51,16 @@ SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen,
SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& info,
const SkIPoint& origin, uint32_t uniqueID)
- : fGenerator(gen)
+ : fNotThreadSafeGenerator(gen)
, fInfo(info)
, fOrigin(origin)
, fUniqueID(uniqueID)
{}
-SkImageCacherator::~SkImageCacherator() { delete fGenerator; }
+SkData* SkImageCacherator::refEncoded() {
+ ScopedGenerator generator(this);
+ return generator->refEncodedData();
+}
static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
SkASSERT(bitmap.getGenerationID() == expectedID);
@@ -66,17 +69,17 @@ static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
return true;
}
-static bool generate_bitmap(SkBitmap* bitmap, const SkImageInfo& info, const SkIPoint& origin,
- SkImageGenerator* generator) {
- const size_t rowBytes = info.minRowBytes();
- if (!bitmap->tryAllocPixels(info, rowBytes)) {
+bool SkImageCacherator::generateBitmap(SkBitmap* bitmap) {
+ const size_t rowBytes = fInfo.minRowBytes();
+ if (!bitmap->tryAllocPixels(fInfo, rowBytes)) {
return false;
}
SkASSERT(bitmap->rowBytes() == rowBytes);
+ ScopedGenerator generator(this);
const SkImageInfo& genInfo = generator->getInfo();
- if (info.dimensions() == genInfo.dimensions()) {
- SkASSERT(origin.x() == 0 && origin.y() == 0);
+ if (fInfo.dimensions() == genInfo.dimensions()) {
+ SkASSERT(fOrigin.x() == 0 && fOrigin.y() == 0);
// fast-case, no copy needed
if (!generator->getPixels(bitmap->info(), bitmap->getPixels(), rowBytes)) {
bitmap->reset();
@@ -93,7 +96,7 @@ static bool generate_bitmap(SkBitmap* bitmap, const SkImageInfo& info, const SkI
return false;
}
full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(),
- origin.x(), origin.y());
+ fOrigin.x(), fOrigin.y());
}
return true;
}
@@ -104,7 +107,8 @@ bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) {
if (SkBitmapCache::Find(fUniqueID, bitmap)) {
return check_output_bitmap(*bitmap, fUniqueID);
}
- if (!generate_bitmap(bitmap, fInfo, fOrigin, fGenerator)) {
+
+ if (!this->generateBitmap(bitmap)) {
return false;
}
@@ -120,10 +124,13 @@ bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) {
#if SK_SUPPORT_GPU
// Try to get a texture and read it back to raster (and then cache that with our ID)
+ SkAutoTUnref<GrTexture> tex;
- SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
- SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_SkImageUsageType,
- &subset));
+ {
+ ScopedGenerator generator(this);
+ SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
+ tex.reset(generator->generateTexture(nullptr, kUntiled_SkImageUsageType, &subset));
+ }
if (!tex) {
bitmap->reset();
return false;
@@ -151,40 +158,50 @@ bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) {
//////////////////////////////////////////////////////////////////////////////////////////////////
-GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType usage) {
+/*
+ * We have a 5 ways to try to return a texture (in sorted order)
+ *
+ * 1. Check the cache for a pre-existing one
+ * 2. Ask the genreator to natively create one
+ * 3. Ask the generator to return a compressed form that the GPU might support
+ * 4. Ask the generator to return YUV planes, which the GPU can convert
+ * 5. Ask the generator to return RGB(A) data, which the GPU can convert
+ */
+GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usage) {
#if SK_SUPPORT_GPU
+ if (!ctx) {
+ return nullptr;
+ }
+
GrUniqueKey key;
GrMakeKeyFromImageID(&key, fUniqueID, fInfo.width(), fInfo.height(), SkIPoint::Make(0, 0),
*ctx->caps(), usage);
- GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key);
- if (tex) {
- return tex; // we got a cache hit!
- }
- SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
- tex = fGenerator->generateTexture(ctx, usage, &subset);
- if (tex) {
- tex->resourcePriv().setUniqueKey(key);
+ // 1. Check the cache for a pre-existing one
+ if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key)) {
+ return tex;
}
- return tex;
-#else
- return nullptr;
-#endif
-}
-GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usage) {
-#if SK_SUPPORT_GPU
- if (!ctx) {
- return nullptr;
- }
- if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) {
- return tex;
+ // 2. Ask the genreator to natively create one
mtklein 2015/08/26 21:20:57 Out of curiosity, will each of these 2-5 lock then
+ {
+ ScopedGenerator generator(this);
+ SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
+ if (GrTexture* tex = generator->generateTexture(ctx, usage, &subset)) {
+ tex->resourcePriv().setUniqueKey(key);
+ return tex;
+ }
}
- // Try to get a bitmap and then upload/cache it as a texture
+ // 3. Ask the generator to return a compressed form that the GPU might support
+ // TODO
+
+ // 4. Ask the generator to return YUV planes, which the GPU can convert
+ // TODO
+
+ // 5. Ask the generator to return RGB(A) data, which the GPU can convert
SkBitmap bitmap;
- if (!generate_bitmap(&bitmap, fInfo, fOrigin, fGenerator)) {
+ if (!this->generateBitmap(&bitmap)) {
return nullptr;
}
return GrRefCachedBitmapTexture(ctx, bitmap, usage);
« no previous file with comments | « src/core/SkImageCacherator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698