BitmapHeapTest.cpp revision 8b0e8ac5f582de80356019406e2975079bf0829d
1/* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "SkBitmap.h" 9#include "SkBitmapHeap.h" 10#include "SkColor.h" 11#include "SkFlattenable.h" 12#include "SkWriteBuffer.h" 13#include "SkPictureFlat.h" 14#include "SkRefCnt.h" 15#include "SkShader.h" 16#include "Test.h" 17 18struct SkShaderTraits { 19 static void flatten(SkWriteBuffer& buffer, const SkShader& shader) { 20 buffer.writeFlattenable(&shader); 21 } 22}; 23typedef SkFlatDictionary<SkShader, SkShaderTraits> FlatDictionary; 24 25class SkBitmapHeapTester { 26 27public: 28 static int32_t GetRefCount(const SkBitmapHeapEntry* entry) { 29 return entry->fRefCount; 30 } 31}; 32 33DEF_TEST(BitmapHeap, reporter) { 34 // Create a bitmap shader. 35 SkBitmap bm; 36 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); 37 bm.allocPixels(); 38 bm.eraseColor(SK_ColorRED); 39 uint32_t* pixel = bm.getAddr32(1,0); 40 *pixel = SK_ColorBLUE; 41 42 SkShader* bitmapShader = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, 43 SkShader::kRepeat_TileMode); 44 SkAutoTUnref<SkShader> aur(bitmapShader); 45 46 // Flatten, storing it in the bitmap heap. 47 SkBitmapHeap heap(1, 1); 48 SkChunkFlatController controller(1024); 49 controller.setBitmapStorage(&heap); 50 FlatDictionary dictionary(&controller); 51 52 // Dictionary and heap start off empty. 53 REPORTER_ASSERT(reporter, heap.count() == 0); 54 REPORTER_ASSERT(reporter, dictionary.count() == 0); 55 56 heap.deferAddingOwners(); 57 int index = dictionary.find(*bitmapShader); 58 heap.endAddingOwnersDeferral(true); 59 60 // The dictionary and heap should now each have one entry. 61 REPORTER_ASSERT(reporter, 1 == index); 62 REPORTER_ASSERT(reporter, heap.count() == 1); 63 REPORTER_ASSERT(reporter, dictionary.count() == 1); 64 65 // The bitmap entry's refcount should be 1, then 0 after release. 66 SkBitmapHeapEntry* entry = heap.getEntry(0); 67 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 1); 68 69 entry->releaseRef(); 70 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 0); 71 72 // Now clear out the heap, after which it should be empty. 73 heap.freeMemoryIfPossible(~0U); 74 REPORTER_ASSERT(reporter, heap.count() == 0); 75 76 // Now attempt to flatten the shader again. 77 heap.deferAddingOwners(); 78 index = dictionary.find(*bitmapShader); 79 heap.endAddingOwnersDeferral(false); 80 81 // The dictionary should report the same index since the new entry is identical. 82 // The bitmap heap should contain the bitmap, but with no references. 83 REPORTER_ASSERT(reporter, 1 == index); 84 REPORTER_ASSERT(reporter, heap.count() == 1); 85 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0); 86} 87