Use SkImage_Raster's unique ID to cache textures.

SkImages can share SkBitmaps and have different unique IDs/mipmap
status. Currently we cache the texture version using the bitmap's
unique ID. Instead use the image's ID so different images produce
different textures (e.g. mipped and nonmipped).

Bug: skia:11983
Change-Id: Ic37564186f675277e5a9de1bcf36b40a19c3a3de
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407356
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Brian Salomon 2021-05-12 16:59:46 -04:00 committed by Skia Commit-Bot
parent 9b984bb149
commit 8005007e98
7 changed files with 63 additions and 21 deletions

View File

@ -12,7 +12,6 @@
#include "include/core/SkPicture.h"
#include "include/core/SkSurface.h"
#include "src/core/SkTLazy.h"
#include "src/gpu/SkGr.h"
#include "src/image/SkImage_Base.h"
class SkPictureImageGenerator : public SkImageGenerator {
@ -93,6 +92,7 @@ bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels,
#if SK_SUPPORT_GPU
#include "include/gpu/GrRecordingContext.h"
#include "src/gpu/GrRecordingContextPriv.h"
#include "src/gpu/SkGr.h"
GrSurfaceProxyView SkPictureImageGenerator::onGenerateTexture(GrRecordingContext* ctx,
const SkImageInfo& info,

View File

@ -16,11 +16,11 @@
#include "src/core/SkReadBuffer.h"
#include "src/core/SkSpecialImage.h"
#include "src/core/SkWriteBuffer.h"
#include "src/gpu/SkGr.h"
#if SK_SUPPORT_GPU
#include "src/gpu/GrRecordingContextPriv.h"
#include "src/gpu/GrTextureProxy.h"
#include "src/gpu/SkGr.h"
#include "src/gpu/effects/GrMatrixConvolutionEffect.h"
#endif

View File

@ -8,6 +8,7 @@
#ifndef GrTextureProxy_DEFINED
#define GrTextureProxy_DEFINED
#include "include/gpu/GrBackendSurface.h"
#include "src/gpu/GrSamplerState.h"
#include "src/gpu/GrSurfaceProxy.h"

View File

@ -163,10 +163,14 @@ static sk_sp<GrTextureProxy> make_bmp_proxy(GrProxyProvider* proxyProvider,
return proxy;
}
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeCachedBitmapProxyView(GrRecordingContext* rContext,
static std::tuple<GrSurfaceProxyView, GrColorType>
make_cached_bitmap_view(GrRecordingContext* rContext,
const SkBitmap& bitmap,
GrMipmapped mipmapped) {
GrMipmapped mipmapped,
uint32_t cacheID,
bool addListenerToBitmap) {
SkASSERT(cacheID != SK_InvalidUniqueID);
if (!bitmap.peekPixels(nullptr)) {
return {};
}
@ -177,15 +181,20 @@ GrMakeCachedBitmapProxyView(GrRecordingContext* rContext,
GrUniqueKey key;
SkIPoint origin = bitmap.pixelRefOrigin();
SkIRect subset = SkIRect::MakePtSize(origin, bitmap.dimensions());
GrMakeKeyFromImageID(&key, bitmap.pixelRef()->getGenerationID(), subset);
GrMakeKeyFromImageID(&key, cacheID, subset);
mipmapped = adjust_mipmapped(mipmapped, bitmap, caps);
GrColorType ct = choose_bmp_texture_colortype(caps, bitmap);
auto installKey = [&](GrTextureProxy* proxy) {
auto listener = GrMakeUniqueKeyInvalidationListener(&key, proxyProvider->contextID());
if (cacheID == bitmap.getGenerationID()) {
if (addListenerToBitmap) {
auto listener = GrMakeUniqueKeyInvalidationListener(&key,
proxyProvider->contextID());
bitmap.pixelRef()->addGenIDChangeListener(std::move(listener));
}
proxyProvider->assignUniqueKeyToProxy(key, proxy);
}
};
sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
@ -229,6 +238,22 @@ GrMakeCachedBitmapProxyView(GrRecordingContext* rContext,
return {{std::move(mippedProxy), kTopLeft_GrSurfaceOrigin, swizzle}, ct};
}
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeCachedBitmapProxyViewWithID(GrRecordingContext* rContext,
const SkBitmap& bitmap,
GrMipmapped mipmapped,
uint32_t cacheID) {
return make_cached_bitmap_view(rContext, bitmap, mipmapped, cacheID, /*add listener*/ false);
}
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeCachedBitmapProxyView(GrRecordingContext* rContext,
const SkBitmap& bitmap,
GrMipmapped mipmapped) {
uint32_t cacheID = bitmap.getGenerationID();
return make_cached_bitmap_view(rContext, bitmap, mipmapped, cacheID, /*add listener*/ true);
}
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeUncachedBitmapProxyView(GrRecordingContext* rContext,
const SkBitmap& bitmap,

View File

@ -199,7 +199,19 @@ GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext*,
* if mipmapping isn't supported or for a 1x1 bitmap. If GrMipmapped is kNo it indicates mipmaps
* aren't required but a previously created mipmapped texture may still be returned. A color type is
* returned as color type conversion may be performed if there isn't a texture format equivalent of
* the bitmap's color type.
* the bitmap's color type. cacheID is used in the cache key and is from the shared namespace of
* SkBitmap generation IDs and SkImage unique IDs. It must be valid.
*/
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeCachedBitmapProxyViewWithID(GrRecordingContext*,
const SkBitmap&,
GrMipmapped,
uint32_t cacheID);
/**
* Like GrMakeCachedBitmapProxyViewWithID but uses the bitmap's generation ID as the cache ID and
* also may add a listener to the SkBitmap that will invalidate the cached texture if the bitmap
* is deleted or its contents change.
*/
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeCachedBitmapProxyView(GrRecordingContext*,
@ -207,8 +219,8 @@ GrMakeCachedBitmapProxyView(GrRecordingContext*,
GrMipmapped = GrMipmapped::kNo);
/**
* Like above but always uploads the bitmap and never inserts into the cache. Unlike above, the
* texture may be approx or scratch and budgeted or not.
* Like GrMakeCachedBitmapProxyView but always uploads the bitmap and never inserts into the cache.
* Unlike GrMakeCachedBitmapProxyView, the texture may be approx or scratch and budgeted or not.
*/
std::tuple<GrSurfaceProxyView, GrColorType>
GrMakeUncachedBitmapProxyView(GrRecordingContext*,

View File

@ -212,6 +212,7 @@ bool SkImage_Raster::onPinAsTexture(GrRecordingContext* rContext) const {
} else {
SkASSERT(fPinnedCount == 0);
SkASSERT(fPinnedUniqueID == 0);
// It's important that we use the bitmap's gen ID here and not the image's unique ID.
std::tie(fPinnedView, fPinnedColorType) = GrMakeCachedBitmapProxyView(rContext,
fBitmap,
GrMipmapped::kNo);
@ -428,7 +429,7 @@ std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Raster::onAsView(
return {fPinnedView, fPinnedColorType};
}
if (policy == GrImageTexGenPolicy::kDraw) {
return GrMakeCachedBitmapProxyView(rContext, fBitmap, mipmapped);
return GrMakeCachedBitmapProxyViewWithID(rContext, fBitmap, mipmapped, this->uniqueID());
}
auto budgeted = (policy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted)
? SkBudgeted::kNo

View File

@ -51,7 +51,9 @@ static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContex
sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
SkCanvas* canvas = gpuSurface->getCanvas();
// w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
// w/o pinning - the gpu caches the contents of the image and assumes the bitmap is immutable.
// This is actually undefined but we're assuming the GPU backend will cache the original and
// that it won't be purged before this test ends.
{
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
@ -59,28 +61,29 @@ static void basic_test(skiatest::Reporter* reporter, GrRecordingContext* rContex
bmCanvas.clear(SK_ColorGREEN);
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
}
// w/ pinning - the gpu draw is stuck at the pinned state
{
bmCanvas.clear(SK_ColorBLUE);
SkImage_pinAsTexture(img.get(), rContext); // pin at blue
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
bmCanvas.clear(SK_ColorBLUE);
bmCanvas.clear(SK_ColorGREEN);
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
SkImage_unpinAsTexture(img.get(), rContext);
}
// once unpinned local changes will be picked up
// once unpinned revert to original cached texture
{
canvas->drawImage(img, 0, 0);
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
}
}