Actually test the direct-context's thread-safe proxy cache

This CL:
  connects the tests to the live cache
  switches the cache over to using an SkTDynamicHash

Bug: 1108408
Change-Id: I1876baf13a8d12a1ec398f49e2b2d51f434d6d0e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/317764
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Robert Phillips 2020-09-18 12:28:59 -04:00 committed by Skia Commit-Bot
parent 87e6ccde21
commit 752f7e1692
3 changed files with 46 additions and 29 deletions

View File

@ -10,7 +10,7 @@
GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache() {}
GrThreadSafeUniquelyKeyedProxyViewCache::~GrThreadSafeUniquelyKeyedProxyViewCache() {
fUniquelyKeyedProxyViews.foreach([](const GrUniqueKey&k, Entry** v) { delete *v; });
fUniquelyKeyedProxyViews.foreach([](Entry* v) { delete v; });
}
#if GR_TEST_UTILS
@ -20,23 +20,24 @@ int GrThreadSafeUniquelyKeyedProxyViewCache::numEntries() const {
return fUniquelyKeyedProxyViews.count();
}
size_t GrThreadSafeUniquelyKeyedProxyViewCache::approxBytesUsed() const {
int GrThreadSafeUniquelyKeyedProxyViewCache::count() const {
SkAutoSpinlock lock{fSpinLock};
return fUniquelyKeyedProxyViews.approxBytesUsed();
return fUniquelyKeyedProxyViews.count();
}
#endif
void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllRefs() {
SkAutoSpinlock lock{fSpinLock};
fUniquelyKeyedProxyViews.foreach([](Entry* v) { delete v; });
fUniquelyKeyedProxyViews.reset();
}
void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllUniqueRefs() {
SkAutoSpinlock lock{fSpinLock};
fUniquelyKeyedProxyViews.foreach([](const GrUniqueKey&k, Entry** v) {
fUniquelyKeyedProxyViews.foreach([](Entry* v) {
// problematic
});
}
@ -44,9 +45,9 @@ void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllUniqueRefs() {
GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::find(const GrUniqueKey& key) {
SkAutoSpinlock lock{fSpinLock};
Entry** tmp = fUniquelyKeyedProxyViews.find(const_cast<GrUniqueKey&>(key));
Entry* tmp = fUniquelyKeyedProxyViews.find(key);
if (tmp) {
return (*tmp)->fView;
return tmp->fView;
}
return {};
@ -55,14 +56,14 @@ GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::find(const GrUniqueK
GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::internalAdd(
const GrUniqueKey& key,
const GrSurfaceProxyView& view) {
Entry** tmp = fUniquelyKeyedProxyViews.find(const_cast<GrUniqueKey&>(key));
Entry* tmp = fUniquelyKeyedProxyViews.find(key);
if (!tmp) {
// TODO: block allocate here?
Entry* newT = new Entry(key, view);
tmp = fUniquelyKeyedProxyViews.set(newT->fKey, newT);
tmp = new Entry(key, view);
fUniquelyKeyedProxyViews.add(tmp);
}
return (*tmp)->fView;
return tmp->fView;
}
GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::add(const GrUniqueKey& key,

View File

@ -9,7 +9,7 @@
#define GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED
#include "include/private/SkSpinlock.h"
#include "include/private/SkTHash.h"
#include "src/core/SkTDynamicHash.h"
#include "src/gpu/GrSurfaceProxyView.h"
// Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
@ -43,11 +43,11 @@ public:
#if GR_TEST_UTILS
int numEntries() const SK_EXCLUDES(fSpinLock);
size_t approxBytesUsed() const SK_EXCLUDES(fSpinLock);
int count() const SK_EXCLUDES(fSpinLock);
#endif
void dropAllRefs() SK_EXCLUDES(fSpinLock);
void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock);
void dropAllRefs() SK_EXCLUDES(fSpinLock);
void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock);
GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
@ -58,8 +58,12 @@ private:
Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
// Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
GrUniqueKey fKey;
const GrUniqueKey fKey;
GrSurfaceProxyView fView;
// for SkTDynamicHash
static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
};
GrSurfaceProxyView internalAdd(const GrUniqueKey&,
@ -67,12 +71,7 @@ private:
mutable SkSpinlock fSpinLock;
struct KeyHash {
uint32_t operator()(const GrUniqueKey& key) { return key.hash(); }
};
// TODO: it sure would be cool if the key could be a const& to the version stored in 'Entry'
SkTHashMap<GrUniqueKey, Entry*, KeyHash> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock);
SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock);
};
#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED

View File

@ -85,8 +85,6 @@ public:
SkBitmap tmp = create_up_arrow_bitmap(kImageWH);
SkAssertResult(CreateBackendTexture(fDContext, &fBETex, tmp));
fThreadSafeViewCache = std::make_unique<GrThreadSafeUniquelyKeyedProxyViewCache>();
}
~TestHelper() {
@ -95,9 +93,7 @@ public:
Stats* stats() { return &fStats; }
int numCacheEntries() const {
return fThreadSafeViewCache->numEntries();
}
int numCacheEntries() const { return this->threadSafeViewCache()->numEntries(); }
GrDirectContext* dContext() { return fDContext; }
@ -106,7 +102,11 @@ public:
SkCanvas* ddlCanvas2() { return fRecorder2 ? fRecorder2->getCanvas() : nullptr; }
GrThreadSafeUniquelyKeyedProxyViewCache* threadSafeViewCache() {
return fThreadSafeViewCache.get();
return fDContext->priv().threadSafeViewCache();
}
const GrThreadSafeUniquelyKeyedProxyViewCache* threadSafeViewCache() const {
return fDContext->priv().threadSafeViewCache();
}
// Add a draw on 'canvas' that will introduce a ref on the 'wh' view
@ -137,6 +137,7 @@ public:
nullptr);
}
// TODO: make a static version and pass in rContext & resourceCache
bool checkView(SkCanvas* canvas, int wh, int hits, int misses, int numRefs) {
if (fStats.fCacheHits != hits || fStats.fCacheMisses != misses) {
return false;
@ -197,8 +198,6 @@ private:
Stats fStats;
GrDirectContext* fDContext = nullptr;
std::unique_ptr<GrThreadSafeUniquelyKeyedProxyViewCache> fThreadSafeViewCache;
sk_sp<SkSurface> fDst;
std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder1;
std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder2;
@ -337,3 +336,21 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeViewCache4, reporter, ctxInfo) {
REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0);
REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 2);
}
// Case 5: ensure that expanding the map works
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeViewCache5, reporter, ctxInfo) {
TestHelper helper(ctxInfo.directContext());
auto threadSafeViewCache = helper.threadSafeViewCache();
int size = 16;
helper.accessCachedView(helper.ddlCanvas1(), size);
int initialCount = threadSafeViewCache->count();
while (initialCount == threadSafeViewCache->count()) {
size *= 2;
helper.accessCachedView(helper.ddlCanvas1(), size);
}
}