cache private readback for gpu-images
Does not try to cache calls to readPixels at the moment:
- not triggered by drawing
- not clear if we want to perform any pixel transformations (that readPixels allows) on the GPU or CPU
Can consider that another time.
BUG=513695
Review URL: https://codereview.chromium.org/1262923003
diff --git a/src/core/SkBitmapCache.cpp b/src/core/SkBitmapCache.cpp
index aabf87a..3f1feac 100644
--- a/src/core/SkBitmapCache.cpp
+++ b/src/core/SkBitmapCache.cpp
@@ -142,6 +142,20 @@
}
}
+bool SkBitmapCache::Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache) {
+ BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeEmpty());
+
+ return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result);
+}
+
+void SkBitmapCache::Add(uint32_t genID, const SkBitmap& result, SkResourceCache* localCache) {
+ SkASSERT(result.isImmutable());
+
+ BitmapRec* rec = SkNEW_ARGS(BitmapRec, (genID, 1, 1, SkIRect::MakeEmpty(), result));
+
+ CHECK_LOCAL(localCache, add, Add, rec);
+}
+
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkBitmapCache.h b/src/core/SkBitmapCache.h
index de50aab..c758942 100644
--- a/src/core/SkBitmapCache.h
+++ b/src/core/SkBitmapCache.h
@@ -52,6 +52,10 @@
*/
static bool Add(SkPixelRef*, const SkIRect& subset, const SkBitmap& result,
SkResourceCache* localCache = NULL);
+
+ static bool Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache = NULL);
+ // todo: eliminate the need to specify ID, since it should == the bitmap's
+ static void Add(uint32_t genID, const SkBitmap&, SkResourceCache* localCache = NULL);
};
class SkMipMapCache {
diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp
index e79dfba..2a46385 100644
--- a/src/core/SkPixelRef.cpp
+++ b/src/core/SkPixelRef.cpp
@@ -356,6 +356,18 @@
void SkPixelRef::setImmutable() {
fMutability = kImmutable;
}
+
+void SkPixelRef::setImmutableWithID(uint32_t genID) {
+ /*
+ * We are forcing the genID to match an external value. The caller must ensure that this
+ * value does not conflict with other content.
+ *
+ * One use is to force this pixelref's id to match an SkImage's id
+ */
+ fMutability = kImmutable;
+ fTaggedGenID.store(genID);
+}
+
void SkPixelRef::setTemporarilyImmutable() {
SkASSERT(fMutability != kImmutable);
fMutability = kTemporarilyImmutable;
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index c89c08d..d410de8 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -5,13 +5,14 @@
* found in the LICENSE file.
*/
+#include "SkBitmapCache.h"
#include "SkImage_Gpu.h"
#include "GrContext.h"
#include "GrDrawContext.h"
#include "effects/GrYUVtoRGBEffect.h"
#include "SkCanvas.h"
#include "SkGpuDevice.h"
-
+#include "SkPixelRef.h"
SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex,
int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted)
@@ -22,6 +23,12 @@
, fBudgeted(budgeted)
{}
+SkImage_Gpu::~SkImage_Gpu() {
+ if (fAddedRasterVersionToCache.load()) {
+ SkNotifyBitmapGenIDIsStale(this->uniqueID());
+ }
+}
+
SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
GrTexture* tex = this->getTexture();
SkASSERT(tex);
@@ -49,6 +56,13 @@
}
bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
+ if (SkBitmapCache::Find(this->uniqueID(), dst)) {
+ SkASSERT(dst->getGenerationID() == this->uniqueID());
+ SkASSERT(dst->isImmutable());
+ SkASSERT(dst->getPixels());
+ return true;
+ }
+
SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) {
return false;
@@ -57,6 +71,10 @@
dst->getPixels(), dst->rowBytes())) {
return false;
}
+
+ dst->pixelRef()->setImmutableWithID(this->uniqueID());
+ SkBitmapCache::Add(this->uniqueID(), *dst);
+ fAddedRasterVersionToCache.store(true);
return true;
}
diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h
index 4c7ebd6..9481690 100644
--- a/src/image/SkImage_Gpu.h
+++ b/src/image/SkImage_Gpu.h
@@ -8,6 +8,7 @@
#ifndef SkImage_Gpu_DEFINED
#define SkImage_Gpu_DEFINED
+#include "SkAtomics.h"
#include "GrTexture.h"
#include "GrGpuResourcePriv.h"
#include "SkBitmap.h"
@@ -17,14 +18,13 @@
class SkImage_Gpu : public SkImage_Base {
public:
-
-
/**
* An "image" can be a subset/window into a larger texture, so we explicit take the
* width and height.
*/
SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType, GrTexture*,
int sampleCountForNewSurfaces, SkSurface::Budgeted);
+ ~SkImage_Gpu() override;
void applyBudgetDecision() const {
GrTexture* tex = this->getTexture();
@@ -47,10 +47,12 @@
int srcX, int srcY) const override;
private:
- SkAutoTUnref<GrTexture> fTexture;
- const int fSampleCountForNewSurfaces; // 0 if we don't know
- const SkAlphaType fAlphaType;
- SkSurface::Budgeted fBudgeted;
+ SkAutoTUnref<GrTexture> fTexture;
+ const int fSampleCountForNewSurfaces; // 0 if we don't know
+ const SkAlphaType fAlphaType;
+ const SkSurface::Budgeted fBudgeted;
+ mutable SkAtomic<bool> fAddedRasterVersionToCache;
+
typedef SkImage_Base INHERITED;
};