Port GrGLCaps over to use SkTHash.
I've written some new hashtable interfaces that should be easier to use,
and I've been trying to roll them out bit by bit, hopefully replacing
SkTDynamicHash, SkTMultiMap, SkTHashCache, etc.
This turns the cache in GrGLCaps::readPixelsSupported() into an SkTHashMap,
mapping the format key to a bool. Functionally, it's the same.
BUG=skia:
Review URL: https://codereview.chromium.org/948473002
diff --git a/gyp/core.gypi b/gyp/core.gypi
index ead1d99..4421f5d 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -203,7 +203,6 @@
'<(skia_src_path)/core/SkTextFormatParams.h',
'<(skia_src_path)/core/SkTextMapStateProc.h',
'<(skia_src_path)/core/SkTDPQueue.h',
- '<(skia_src_path)/core/SkTHashCache.h',
'<(skia_src_path)/core/SkTLList.h',
'<(skia_src_path)/core/SkTLS.cpp',
'<(skia_src_path)/core/SkTraceEvent.h',
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index 93468a6..6b4a9e4 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -205,7 +205,6 @@
'../tests/SurfaceTest.cpp',
'../tests/TArrayTest.cpp',
'../tests/TDPQueueTest.cpp',
- '../tests/THashCache.cpp',
'../tests/Time.cpp',
'../tests/TLSTest.cpp',
'../tests/TSetTest.cpp',
diff --git a/src/core/SkTHash.h b/src/core/SkTHash.h
index c7917ac..b47f8fa 100644
--- a/src/core/SkTHash.h
+++ b/src/core/SkTHash.h
@@ -18,6 +18,12 @@
public:
SkTHashTable() : fCount(0), fCapacity(0) {}
+ // Clear the table.
+ void reset() {
+ this->~SkTHashTable();
+ SkNEW_PLACEMENT(this, SkTHashTable);
+ }
+
// How many entries are in the table?
int count() const { return fCount; }
@@ -144,6 +150,9 @@
public:
SkTHashMap() {}
+ // Clear the map.
+ void reset() { fTable.reset(); }
+
// How many key/value pairs are in the table?
int count() const { return fTable.count(); }
@@ -187,6 +196,9 @@
public:
SkTHashSet() {}
+ // Clear the set.
+ void reset() { fTable.reset(); }
+
// How many items are in the set?
int count() const { return fTable.count(); }
diff --git a/src/core/SkTHashCache.h b/src/core/SkTHashCache.h
deleted file mode 100644
index cfee972..0000000
--- a/src/core/SkTHashCache.h
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkTHASHCACHE_DEFINED
-#define SkTHASHCACHE_DEFINED
-
-#include "SkTypes.h"
-#include "SkTDynamicHash.h"
-
-template <typename T,
-typename Key,
-typename Traits = T,
-int kGrowPercent = 75 >
-class SkTHashCache : public SkNoncopyable {
-public:
-
- SkTHashCache() {
- this->reset();
- }
-
- ~SkTHashCache() {
- this->clear();
- }
-
- T* find(const Key& key) const {
- return fDict->find(key);
- }
-
- /**
- * If element already in cache, return immediately the cached value
- */
- T& add(const T& add) {
- Key key = Traits::GetKey(add);
- if (T* val = this->find(key)) {
- return *val;
- }
-
- T* element = SkNEW_ARGS(T, (add));
-
- fDict->add(element);
-
- return *element;
- }
-
- int size() const {
- return fDict->count();
- }
-
- void reset() {
- this->clear();
-
- fDict.reset(SkNEW(DictType));
- }
-
-private:
- typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType;
-
- void clear() {
- if (fDict.get()) {
- typename DictType::Iter it(fDict.get());
-
- while (!it.done()) {
- SkDELETE(&(*it));
- ++it;
- }
- }
- }
-
- SkAutoTDelete<DictType> fDict;
-};
-
-#endif /* SkHASHCACHE_DEFINED */
-
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index 1f50f8d..9911d53 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -749,20 +749,13 @@
GrGLenum format,
GrGLenum type,
GrGLenum currFboFormat) const {
-
- ReadPixelsSupportedFormats::Key key = {format, type, currFboFormat};
-
- ReadPixelsSupportedFormats* cachedValue = fReadPixelsSupportedCache.find(key);
-
- if (NULL == cachedValue) {
- bool value = doReadPixelsSupported(intf, format, type);
- ReadPixelsSupportedFormats newValue(key, value);
- fReadPixelsSupportedCache.add(newValue);
-
- return newValue.value();
+ ReadPixelsSupportedFormat key = {format, type, currFboFormat};
+ if (const bool* supported = fReadPixelsSupportedCache.find(key)) {
+ return *supported;
}
-
- return cachedValue->value();
+ bool supported = this->doReadPixelsSupported(intf, format, type);
+ fReadPixelsSupportedCache.set(key, supported);
+ return supported;
}
void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
diff --git a/src/gpu/gl/GrGLCaps.h b/src/gpu/gl/GrGLCaps.h
index 527cd33..1b77ed0 100644
--- a/src/gpu/gl/GrGLCaps.h
+++ b/src/gpu/gl/GrGLCaps.h
@@ -12,7 +12,7 @@
#include "GrDrawTargetCaps.h"
#include "GrGLStencilBuffer.h"
#include "SkChecksum.h"
-#include "SkTHashCache.h"
+#include "SkTHash.h"
#include "SkTArray.h"
class GrGLContextInfo;
@@ -393,45 +393,23 @@
const char* fFBFetchColorName;
const char* fFBFetchExtensionString;
- class ReadPixelsSupportedFormats {
- public:
- struct Key {
- GrGLenum fFormat;
- GrGLenum fType;
- GrGLenum fFboFormat;
+ struct ReadPixelsSupportedFormat {
+ GrGLenum fFormat;
+ GrGLenum fType;
+ GrGLenum fFboFormat;
- bool operator==(const Key& rhs) const {
- return fFormat == rhs.fFormat
- && fType == rhs.fType
- && fFboFormat == rhs.fFboFormat;
- }
-
- uint32_t getHash() const {
- return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(this), sizeof(*this));
- }
- };
-
- ReadPixelsSupportedFormats(Key key, bool value) : fKey(key), fValue(value) {
+ bool operator==(const ReadPixelsSupportedFormat& rhs) const {
+ return fFormat == rhs.fFormat
+ && fType == rhs.fType
+ && fFboFormat == rhs.fFboFormat;
}
-
- static const Key& GetKey(const ReadPixelsSupportedFormats& element) {
- return element.fKey;
+ static uint32_t Hash(const ReadPixelsSupportedFormat& r) {
+ return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&r), sizeof(r));
}
-
- static uint32_t Hash(const Key& key) {
- return key.getHash();
- }
-
- bool value() const {
- return fValue;
- }
- private:
- Key fKey;
- bool fValue;
};
- mutable SkTHashCache<ReadPixelsSupportedFormats,
- ReadPixelsSupportedFormats::Key> fReadPixelsSupportedCache;
+ mutable SkTHashMap<ReadPixelsSupportedFormat, bool, ReadPixelsSupportedFormat::Hash>
+ fReadPixelsSupportedCache;
typedef GrDrawTargetCaps INHERITED;
};
diff --git a/tests/HashTest.cpp b/tests/HashTest.cpp
index 00857f6..623e597 100644
--- a/tests/HashTest.cpp
+++ b/tests/HashTest.cpp
@@ -39,6 +39,9 @@
}
REPORTER_ASSERT(r, map.count() == N);
+
+ map.reset();
+ REPORTER_ASSERT(r, map.count() == 0);
}
namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size()); } }
@@ -54,6 +57,9 @@
REPORTER_ASSERT(r, set.contains(SkString("Hello")));
REPORTER_ASSERT(r, set.contains(SkString("World")));
REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
+
+ set.reset();
+ REPORTER_ASSERT(r, set.count() == 0);
}
namespace {
diff --git a/tests/THashCache.cpp b/tests/THashCache.cpp
deleted file mode 100644
index c35df6c..0000000
--- a/tests/THashCache.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "Test.h"
-#include "SkTHashCache.h"
-
-
-// Tests the SkTHashCache<T> class template.
-
-struct Tint {
- uint32_t value;
-
- bool operator==(const Tint& rhs) const {
- return value == rhs.value;
- }
-};
-
-class Element {
-public:
-
- bool operator==(const Element& rhs) const {
- return value == rhs.value && key == rhs.key;
- }
-
- static const Tint& GetKey(const Element& element) {
- return element.key;
- }
-
- static uint32_t Hash(const Tint& key) {
- return key.value;
- }
-
- Element(Tint key, int value) : key(key), value(value) {
- }
-
- Tint key;
- int value;
-};
-
-typedef SkTHashCache<Element, Tint> CacheType;
-
-DEF_TEST(THashCache, reporter) {
- Tint k11 = {11};
- Element e11(k11, 22);
-
- Element e11Collision(k11, 0);
- // Element e42(4, 2);
-
- //Some tests for the class Element
- REPORTER_ASSERT(reporter, Element::GetKey(e11) == k11);
- REPORTER_ASSERT(reporter, Element::Hash(k11) == 11);
-
- CacheType cache;
-
- // Is the cahce correctly initialized ?
- REPORTER_ASSERT(reporter, 0 == cache.size());
- REPORTER_ASSERT(reporter, NULL == cache.find(k11));
-
- Element& e11_c = cache.add(e11);
-
- // Tests for simple insertion, verifying that the returned element
- // has the same values as the original one
- REPORTER_ASSERT(reporter, 1 == cache.size());
- REPORTER_ASSERT(reporter, NULL != cache.find(k11));
- REPORTER_ASSERT(reporter, e11_c == e11);
-
- Element& e11Collision_c = cache.add(e11Collision);
- // Verifying that, in case of collision, the element alerady in the cache is not removed
- REPORTER_ASSERT(reporter, e11Collision_c == e11);
- REPORTER_ASSERT(reporter, 1 == cache.size());
-
- Tint k42 = {42};
- Element e42(k42, 2);
- cache.add(e42);
- // Can we add more than one element?
- REPORTER_ASSERT(reporter, NULL != cache.find(k11));
- REPORTER_ASSERT(reporter, NULL != cache.find(k42));
- REPORTER_ASSERT(reporter, 2 == cache.size());
-
- cache.reset();
- // Does clear do its job?
- REPORTER_ASSERT(reporter, 0 == cache.size());
- REPORTER_ASSERT(reporter, NULL == cache.find(k11));
- REPORTER_ASSERT(reporter, NULL == cache.find(k42));
-}