| Index: tests/LayerAllocatorTest.cpp
|
| diff --git a/tests/LayerAllocatorTest.cpp b/tests/LayerAllocatorTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e74f6d6ffcf9dbc53bd9a494faf226b84440d251
|
| --- /dev/null
|
| +++ b/tests/LayerAllocatorTest.cpp
|
| @@ -0,0 +1,140 @@
|
| +/*
|
| + * Copyright 2016 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 "SkCanvas.h"
|
| +#include "SkRasterCanvasLayerAllocator.h"
|
| +
|
| +#include <unordered_map>
|
| +
|
| +class TestAllocator : public SkRasterCanvasLayerAllocator {
|
| +public:
|
| + TestAllocator() : fAllocationCount (0) { }
|
| + ~TestAllocator() override {
|
| + for (int i = 0; i < fAllocationCount; i++) {
|
| + delete [] fBuffers[i];
|
| + }
|
| + }
|
| +
|
| + void* getNativeContext(void* buffer,
|
| + const SkMatrix& transform,
|
| + const SkIRect& clip_bounds) override;
|
| +
|
| +protected:
|
| + void* allocateLayer(const SkImageInfo& info,
|
| + size_t* rowBytes,
|
| + void (**deallocator)(void*),
|
| + void** deallocatorPayload,
|
| + void* initialData) override;
|
| + typedef std::unordered_map<void*, int*> context_map_t;
|
| + context_map_t fContexts;
|
| +
|
| +public:
|
| + int fAllocationCount;
|
| + char* fBuffers[10];
|
| + int fContextSentinel[10];
|
| +};
|
| +
|
| +bool sDeallocated = false;
|
| +
|
| +void TestDeallocator (void* payload) {
|
| + sDeallocated = true;
|
| +}
|
| +
|
| +void* TestAllocator::getNativeContext(void* buffer,
|
| + const SkMatrix& transform,
|
| + const SkIRect& clip_bounds) {
|
| + context_map_t::iterator it = fContexts.find(buffer);
|
| + if (it == fContexts.end()) {
|
| + //SkDebugf("native context not found");
|
| + return nullptr;
|
| + }
|
| + //SkDebugf("native context %lx", it->second);
|
| + return it->second;
|
| +}
|
| +
|
| +void* TestAllocator::allocateLayer(const SkImageInfo& info,
|
| + size_t* rowBytes,
|
| + void (**deallocator)(void*),
|
| + void** deallocatorPayload,
|
| + void* initialData) {
|
| + char* buffer = new char [info.height() * info.minRowBytes()];
|
| + fBuffers[fAllocationCount] = buffer;
|
| + int* context = &fContextSentinel[fAllocationCount];
|
| + // Some parts of Skia will fill in the correct value if
|
| + // you pass 0, but then that information is lost before
|
| + // we get to allocating pixel refs.
|
| + *rowBytes = info.minRowBytes();
|
| + *deallocator = TestDeallocator;
|
| + *deallocatorPayload = buffer;
|
| + fAllocationCount++;
|
| + fContexts.insert({buffer, context});
|
| + return buffer;
|
| +}
|
| +
|
| +void should_use_slot(skiatest::Reporter* reporter,
|
| + SkCanvas* canvas,
|
| + TestAllocator* allocator,
|
| + SkIRect bounds,
|
| + int i) {
|
| + SkMatrix m = SkMatrix::I();
|
| + void* buffer = canvas->accessTopLayerPixels(nullptr, nullptr);
|
| + REPORTER_ASSERT(reporter, allocator->fAllocationCount >= i);
|
| + REPORTER_ASSERT(reporter, buffer == allocator->fBuffers[i]);
|
| + REPORTER_ASSERT(reporter, allocator->getNativeContext(buffer, m, bounds) ==
|
| + &allocator->fContextSentinel[i]);
|
| +}
|
| +
|
| +DEF_TEST(TestSaveAndRestore, reporter) {
|
| + TestAllocator* allocator = new TestAllocator;
|
| +
|
| + SkIRect bounds = SkIRect::MakeWH(20, 20);
|
| + SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.size());
|
| + SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
|
| +
|
| + // The result of createCanvas() uses memory and context provided by
|
| + // the allocator.
|
| + sk_sp<SkCanvas> canvas = allocator->createCanvas(info, props);
|
| + REPORTER_ASSERT(reporter, canvas);
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, 0);
|
| +
|
| + // After saveLayer() we've made another allocation and it's backing
|
| + // the bitmap.
|
| + canvas->saveLayer(nullptr, nullptr);
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, 1);
|
| + REPORTER_ASSERT(reporter, !sDeallocated);
|
| +
|
| + // After restore(), the deallocator callback has been triggered and the
|
| + // previous buffer and context are now active.
|
| + canvas->restore();
|
| + REPORTER_ASSERT(reporter, sDeallocated);
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, 0);
|
| +
|
| + // Multiple saves continue to allocate (or reuse) buffers & contexts.
|
| +
|
| + canvas->saveLayer(nullptr, nullptr);
|
| + int latestIndex = allocator->fAllocationCount - 1;
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, latestIndex);
|
| +
|
| + canvas->saveLayer(nullptr, nullptr);
|
| + int previousIndex = latestIndex;
|
| + latestIndex = allocator->fAllocationCount - 1;
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, latestIndex);
|
| +
|
| + // Even after several saves and restores, restore backs us up the stack.
|
| + canvas->restore();
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, previousIndex);
|
| + canvas->restore();
|
| + should_use_slot(reporter, canvas.get(), allocator, bounds, 0);
|
| +}
|
| +
|
| +DEF_TEST(TestInitialData, reporter) {
|
| +
|
| +}
|
| +
|
| +
|
| +
|
|
|