skia2/tests/BitmapHeapTest.cpp
commit-bot@chromium.org 8b0e8ac5f5 Refactor read and write buffers.
Eliminates SkFlattenable{Read,Write}Buffer, promoting SkOrdered{Read,Write}Buffer
a step each in the hierarchy.

What used to be this:

SkFlattenableWriteBuffer -> SkOrderedWriteBuffer
SkFlattenableReadBuffer  -> SkOrderedReadBuffer
SkFlattenableReadBuffer  -> SkValidatingReadBuffer

is now

SkWriteBuffer
SkReadBuffer -> SkValidatingReadBuffer

Benefits:
  - code is simpler, names are less wordy
  - the generic SkFlattenableFooBuffer code in SkPaint was incorrect; removed
  - write buffers are completely devirtualized, important for record speed

This refactoring was mostly mechanical.  You aren't going to find anything
interesting in files with less than 10 lines changed.

BUG=skia:
R=reed@google.com, scroggo@google.com, djsollen@google.com, mtklein@google.com

Author: mtklein@chromium.org

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13245 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-30 18:58:24 +00:00

87 lines
2.8 KiB
C++

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkBitmapHeap.h"
#include "SkColor.h"
#include "SkFlattenable.h"
#include "SkWriteBuffer.h"
#include "SkPictureFlat.h"
#include "SkRefCnt.h"
#include "SkShader.h"
#include "Test.h"
struct SkShaderTraits {
static void flatten(SkWriteBuffer& buffer, const SkShader& shader) {
buffer.writeFlattenable(&shader);
}
};
typedef SkFlatDictionary<SkShader, SkShaderTraits> FlatDictionary;
class SkBitmapHeapTester {
public:
static int32_t GetRefCount(const SkBitmapHeapEntry* entry) {
return entry->fRefCount;
}
};
DEF_TEST(BitmapHeap, reporter) {
// Create a bitmap shader.
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
bm.allocPixels();
bm.eraseColor(SK_ColorRED);
uint32_t* pixel = bm.getAddr32(1,0);
*pixel = SK_ColorBLUE;
SkShader* bitmapShader = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkAutoTUnref<SkShader> aur(bitmapShader);
// Flatten, storing it in the bitmap heap.
SkBitmapHeap heap(1, 1);
SkChunkFlatController controller(1024);
controller.setBitmapStorage(&heap);
FlatDictionary dictionary(&controller);
// Dictionary and heap start off empty.
REPORTER_ASSERT(reporter, heap.count() == 0);
REPORTER_ASSERT(reporter, dictionary.count() == 0);
heap.deferAddingOwners();
int index = dictionary.find(*bitmapShader);
heap.endAddingOwnersDeferral(true);
// The dictionary and heap should now each have one entry.
REPORTER_ASSERT(reporter, 1 == index);
REPORTER_ASSERT(reporter, heap.count() == 1);
REPORTER_ASSERT(reporter, dictionary.count() == 1);
// The bitmap entry's refcount should be 1, then 0 after release.
SkBitmapHeapEntry* entry = heap.getEntry(0);
REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 1);
entry->releaseRef();
REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 0);
// Now clear out the heap, after which it should be empty.
heap.freeMemoryIfPossible(~0U);
REPORTER_ASSERT(reporter, heap.count() == 0);
// Now attempt to flatten the shader again.
heap.deferAddingOwners();
index = dictionary.find(*bitmapShader);
heap.endAddingOwnersDeferral(false);
// The dictionary should report the same index since the new entry is identical.
// The bitmap heap should contain the bitmap, but with no references.
REPORTER_ASSERT(reporter, 1 == index);
REPORTER_ASSERT(reporter, heap.count() == 1);
REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0);
}