Use SkBitmapCache to optimize readPixels on a texture-backed bitmap
BUG=skia:2786 R=junov@chromium.org, reed@google.com, bsalomon@google.com Author: piotaixr@chromium.org Review URL: https://codereview.chromium.org/533323002
This commit is contained in:
parent
4bcc2021ff
commit
0e9770515c
@ -145,6 +145,7 @@ void* SkBitmapDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include "SkConfig8888.h"
|
#include "SkConfig8888.h"
|
||||||
|
#include "SkPixelRef.h"
|
||||||
|
|
||||||
bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
|
bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
|
||||||
size_t srcRowBytes, int x, int y) {
|
size_t srcRowBytes, int x, int y) {
|
||||||
@ -261,8 +262,14 @@ void SkBitmapDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
|
|||||||
// the bitmap, we extract a subset.
|
// the bitmap, we extract a subset.
|
||||||
SkIRect srcIR;
|
SkIRect srcIR;
|
||||||
tmpSrc.roundOut(&srcIR);
|
tmpSrc.roundOut(&srcIR);
|
||||||
if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
|
if(bitmap.pixelRef()->getTexture()) {
|
||||||
return;
|
// Accelerated source canvas, don't use extractSubset but readPixels to get the subset.
|
||||||
|
// This way, the pixels are copied in CPU memory instead of GPU memory.
|
||||||
|
bitmap.pixelRef()->readPixels(&tmpBitmap, &srcIR);
|
||||||
|
} else {
|
||||||
|
if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bitmapPtr = &tmpBitmap;
|
bitmapPtr = &tmpBitmap;
|
||||||
|
|
||||||
|
@ -9,8 +9,10 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "SkGrPixelRef.h"
|
#include "SkGrPixelRef.h"
|
||||||
|
|
||||||
#include "GrContext.h"
|
#include "GrContext.h"
|
||||||
#include "GrTexture.h"
|
#include "GrTexture.h"
|
||||||
|
#include "SkBitmapCache.h"
|
||||||
#include "SkGr.h"
|
#include "SkGr.h"
|
||||||
#include "SkRect.h"
|
#include "SkRect.h"
|
||||||
|
|
||||||
@ -143,7 +145,7 @@ SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) {
|
|||||||
if (NULL == fSurface) {
|
if (NULL == fSurface) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that when copying a render-target-backed pixel ref, we
|
// Note that when copying a render-target-backed pixel ref, we
|
||||||
// return a texture-backed pixel ref instead. This is because
|
// return a texture-backed pixel ref instead. This is because
|
||||||
// render-target pixel refs are usually created in conjunction with
|
// render-target pixel refs are usually created in conjunction with
|
||||||
@ -153,30 +155,59 @@ SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) {
|
|||||||
return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset);
|
return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool tryAllocBitmapPixels(SkBitmap* bitmap) {
|
||||||
|
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
|
||||||
|
if (NULL != allocator) {
|
||||||
|
return allocator->allocPixelRef(bitmap, 0);
|
||||||
|
} else {
|
||||||
|
// DiscardableMemory is not available, fallback to default allocator
|
||||||
|
return bitmap->tryAllocPixels();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
|
bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
|
||||||
if (NULL == fSurface || fSurface->wasDestroyed()) {
|
if (NULL == fSurface || fSurface->wasDestroyed()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int left, top, width, height;
|
SkIRect bounds;
|
||||||
if (subset) {
|
if (subset) {
|
||||||
left = subset->fLeft;
|
bounds = *subset;
|
||||||
width = subset->width();
|
|
||||||
top = subset->fTop;
|
|
||||||
height = subset->height();
|
|
||||||
} else {
|
} else {
|
||||||
left = 0;
|
bounds = SkIRect::MakeWH(this->info().width(), this->info().height());
|
||||||
width = this->info().width();
|
|
||||||
top = 0;
|
|
||||||
height = this->info().height();
|
|
||||||
}
|
}
|
||||||
if (!dst->tryAllocN32Pixels(width, height)) {
|
|
||||||
SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n");
|
//Check the cache
|
||||||
return false;
|
if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) {
|
||||||
}
|
//Cache miss
|
||||||
SkAutoLockPixels al(*dst);
|
|
||||||
void* buffer = dst->getPixels();
|
SkBitmap cachedBitmap;
|
||||||
return fSurface->readPixels(left, top, width, height,
|
cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height()));
|
||||||
|
|
||||||
|
// If we can't alloc the pixels, then fail
|
||||||
|
if (!tryAllocBitmapPixels(&cachedBitmap)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to read the pixels from the surface
|
||||||
|
void* buffer = cachedBitmap.getPixels();
|
||||||
|
bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop,
|
||||||
|
bounds.width(), bounds.height(),
|
||||||
kSkia8888_GrPixelConfig,
|
kSkia8888_GrPixelConfig,
|
||||||
buffer, dst->rowBytes());
|
buffer, cachedBitmap.rowBytes());
|
||||||
|
|
||||||
|
if (!readPixelsOk) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we are here, pixels were read correctly from the surface.
|
||||||
|
cachedBitmap.setImmutable();
|
||||||
|
//Add to the cache
|
||||||
|
SkBitmapCache::Add(this->getGenerationID(), bounds, cachedBitmap);
|
||||||
|
|
||||||
|
dst->swap(cachedBitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user