skia2/tests/GrUnitTests.cpp
commit-bot@chromium.org 742058f0ca Speed up GrResourceCache lookup by inlining GrBinHashKey comparisons
The GCC compilers for Android and Ubuntu do not seem to be able to
inline the memcmp operations on GrBinHashKey data. Write the comparisons
manually. Also shortcut GrBinHashKey::EQ to skip comparison when hashes
do not match.

Speeds up grresourcecache_find test on ARM and x86_64. Speeds up
grresourcecache_add on x86_64.

In order to test the change, moves ad hoc Gr unit tests from
src/gr_unittest.cpp to tests/GrUnitTests to be consistent with other
tests and enables GrUnitTests.

Fixes a regression from r2863 with where re-setting GrBinHashKey data
would not set the hash correctly. This should also improve the hash
function itself. The regression caused many of the hash operations be
no-ops. This is caught by the unit test.

Renames the comparison functions that GrHashTable needs from EQ, LT to
Equals, LessThan.

Renames GrTBinHashKey to GrBinHashKey. The GrTBinHashKey used to
forward comparison functions to an ENTRY template class, which would
extract the key and call back to the GrTBinHashKey. This would save
the user from writing one comparison function when comparison was done
with int ENTRY::compare(). There's no real benefit in this now. Also
this was used only for one class (GrTextureStripAtlas). The other use
in GrResourceKey was not actually using the provided "shortcut". The
new GrBinHashKey is not templated with the entry, rather just provides
== and < functions. The users of GrTHashTable provide the needed
functions now.

Adds explicit documentation of functions that are actually needed
GrTHashTable for the Key template. Adds SK_DEBUG guards according to
the contract.

R=bsalomon@google.com, mtklein@google.com

Author: kkinnunen@nvidia.com

Review URL: https://codereview.chromium.org/88113002

git-svn-id: http://skia.googlecode.com/svn/trunk@12426 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-11-28 08:24:29 +00:00

78 lines
2.3 KiB
C++

/*
* Copyright 2010 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 "TestClassDef.h"
// This is a GPU-backend specific test
#if SK_SUPPORT_GPU
#include "GrBinHashKey.h"
#include "GrDrawTarget.h"
#include "SkMatrix.h"
#include "GrRedBlackTree.h"
// If we aren't inheriting these as #defines from elsewhere,
// clang demands they be declared before we #include the template
// that relies on them.
static bool LT(const int& elem, int value) {
return elem < value;
}
static bool EQ(const int& elem, int value) {
return elem == value;
}
#include "GrTBSearch.h"
DEF_TEST(GrUnitTests_bsearch, reporter) {
const int array[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
};
for (int n = 0; n < static_cast<int>(GR_ARRAY_COUNT(array)); ++n) {
for (int i = 0; i < n; i++) {
int index = GrTBSearch<int, int>(array, n, array[i]);
REPORTER_ASSERT(reporter, index == (int) i);
index = GrTBSearch<int, int>(array, n, -array[i]);
REPORTER_ASSERT(reporter, index < 0);
}
}
}
DEF_TEST(GrUnitTests_binHashKey, reporter) {
const char* testStringA_ = "abcdABCD";
const char* testStringB_ = "abcdBBCD";
const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
enum {
kDataLenUsedForKey = 8
};
GrBinHashKey<kDataLenUsedForKey> keyA;
keyA.setKeyData(testStringA);
// test copy constructor and comparison
GrBinHashKey<kDataLenUsedForKey> keyA2(keyA);
REPORTER_ASSERT(reporter, keyA == keyA2);
REPORTER_ASSERT(reporter, keyA.getHash() == keyA2.getHash());
// test re-init
keyA2.setKeyData(testStringA);
REPORTER_ASSERT(reporter, keyA == keyA2);
REPORTER_ASSERT(reporter, keyA.getHash() == keyA2.getHash());
// test sorting
GrBinHashKey<kDataLenUsedForKey> keyB;
keyB.setKeyData(testStringB);
REPORTER_ASSERT(reporter, keyA < keyB);
REPORTER_ASSERT(reporter, keyA.getHash() != keyB.getHash());
}
DEF_TEST(GrUnitTests_redBlackTree, reporter) {
// TODO(mtklein): unwrap this and use reporter.
GrRedBlackTree<int>::UnitTest();
}
#endif