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/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;
}