aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar bsalomon <bsalomon@google.com>2014-09-05 06:13:43 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-09-05 06:13:43 -0700
commita1ae66d252edf6da932caed1fe43d11216e56c0e (patch)
tree8247f3d0f63830970f71bd1bc5c9b7e70fac1864
parent7675fb23a0448448662567bf1d100e39bf7b5e65 (diff)
Add pop_back() to GrAllocator and add unit test.
BUG=skia:2889 R=robertphillips@google.com Author: bsalomon@google.com Review URL: https://codereview.chromium.org/538183002
-rw-r--r--gyp/tests.gypi1
-rw-r--r--src/gpu/GrAllocator.h27
-rw-r--r--tests/GrAllocatorTest.cpp103
3 files changed, 130 insertions, 1 deletions
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index e37083706f..cf78e41f63 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -108,6 +108,7 @@
'../tests/GrBinHashKeyTest.cpp',
'../tests/GrContextFactoryTest.cpp',
'../tests/GrDrawTargetTest.cpp',
+ '../tests/GrAllocatorTest.cpp',
'../tests/GrMemoryPoolTest.cpp',
'../tests/GrOrderedSetTest.cpp',
'../tests/GrGLSLPrettyPrintTest.cpp',
diff --git a/src/gpu/GrAllocator.h b/src/gpu/GrAllocator.h
index 933be9f655..37c123f793 100644
--- a/src/gpu/GrAllocator.h
+++ b/src/gpu/GrAllocator.h
@@ -61,6 +61,24 @@ public:
}
/**
+ * Remove the last item, only call if count() != 0
+ */
+ void pop_back() {
+ SkASSERT(fCount);
+ SkASSERT(fInsertionIndexInBlock > 0);
+ --fInsertionIndexInBlock;
+ --fCount;
+ if (0 == fInsertionIndexInBlock) {
+ // Never delete the first block
+ if (fBlocks.count() > 1) {
+ sk_free(fBlocks.back());
+ fBlocks.pop_back();
+ fInsertionIndexInBlock = fItemsPerBlock;
+ }
+ }
+ }
+
+ /**
* Removes all added items.
*/
void reset() {
@@ -109,7 +127,6 @@ public:
return (const char*)(fBlocks.back()) + (fInsertionIndexInBlock - 1) * fItemSize;
}
-
/**
* Iterates through the allocator. This is faster than using operator[] when walking linearly
* through the allocator.
@@ -240,6 +257,14 @@ public:
}
/**
+ * Remove the last item, only call if count() != 0
+ */
+ void pop_back() {
+ this->back().~T();
+ fAllocator.pop_back();
+ }
+
+ /**
* Removes all added items.
*/
void reset() {
diff --git a/tests/GrAllocatorTest.cpp b/tests/GrAllocatorTest.cpp
new file mode 100644
index 0000000000..a05da8fc6c
--- /dev/null
+++ b/tests/GrAllocatorTest.cpp
@@ -0,0 +1,103 @@
+/*
+ * 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"
+// This is a GPU-backend specific test
+#if SK_SUPPORT_GPU
+#include "GrAllocator.h"
+
+namespace {
+struct C {
+ C() : fID(-1) { ++gInstCnt; }
+ C(int id) : fID(id) { ++gInstCnt; }
+ ~C() { --gInstCnt; }
+ int fID;
+
+ static int gInstCnt;
+};
+
+int C::gInstCnt = 0;
+}
+
+static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
+ skiatest::Reporter* reporter);
+
+// Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt items and checks
+// again. Finally it resets the allocator and checks again.
+static void check_allocator(GrTAllocator<C>* allocator, int cnt, int popCnt,
+ skiatest::Reporter* reporter) {
+ SkASSERT(NULL != allocator);
+ SkASSERT(allocator->empty());
+ for (int i = 0; i < cnt; ++i) {
+ // Try both variations of push_back().
+ if (i % 1) {
+ allocator->push_back(C(i));
+ } else {
+ allocator->push_back() = C(i);
+ }
+ }
+ check_allocator_helper(allocator, cnt, popCnt, reporter);
+ allocator->reset();
+ check_allocator_helper(allocator, 0, 0, reporter);
+}
+
+// Checks that the allocator has the correct count, etc and that the element IDs are correct.
+// Then pops popCnt items and checks again.
+static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
+ skiatest::Reporter* reporter) {
+ REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty());
+ REPORTER_ASSERT(reporter, cnt == allocator->count());
+ REPORTER_ASSERT(reporter, cnt == C::gInstCnt);
+
+ GrTAllocator<C>::Iter iter(allocator);
+ for (int i = 0; i < cnt; ++i) {
+ REPORTER_ASSERT(reporter, iter.next() && i == iter.get()->fID);
+ }
+ REPORTER_ASSERT(reporter, !iter.next());
+ if (cnt > 0) {
+ REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
+ }
+
+ if (popCnt > 0) {
+ for (int i = 0; i < popCnt; ++i) {
+ allocator->pop_back();
+ }
+ check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
+ }
+}
+
+DEF_TEST(GrAllocator, reporter) {
+
+ // Test combinations of allocators with and without stack storage and with different block
+ // sizes.
+ SkTArray<GrTAllocator<C>*> allocators;
+ GrTAllocator<C> a1(1);
+ allocators.push_back(&a1);
+ GrTAllocator<C> a2(2);
+ allocators.push_back(&a2);
+ GrTAllocator<C> a5(5);
+ allocators.push_back(&a5);
+
+ GrSTAllocator<1, C> sa1;
+ allocators.push_back(&a1);
+ GrSTAllocator<3, C> sa3;
+ allocators.push_back(&sa3);
+ GrSTAllocator<4, C> sa4;
+ allocators.push_back(&sa4);
+
+ for (int i = 0; i < allocators.count(); ++i) {
+ check_allocator(allocators[i], 0, 0, reporter);
+ check_allocator(allocators[i], 1, 1, reporter);
+ check_allocator(allocators[i], 2, 2, reporter);
+ check_allocator(allocators[i], 10, 1, reporter);
+ check_allocator(allocators[i], 10, 5, reporter);
+ check_allocator(allocators[i], 10, 10, reporter);
+ check_allocator(allocators[i], 100, 10, reporter);
+ }
+}
+
+#endif