blob: bcfec22fd1663a6ab9e2e61bde0eb449164480b5 [file] [log] [blame]
scroggo@google.com013c5d92012-11-16 20:34:37 +00001/*
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 "SkOrderedWriteBuffer.h"
13#include "SkPictureFlat.h"
14#include "SkRefCnt.h"
15#include "SkShader.h"
16#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000017#include "TestClassDef.h"
scroggo@google.com013c5d92012-11-16 20:34:37 +000018
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000019struct SkShaderTraits {
20 static void flatten(SkOrderedWriteBuffer& buffer, const SkShader& shader) {
21 buffer.writeFlattenable(&shader);
scroggo@google.com013c5d92012-11-16 20:34:37 +000022 }
23};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000024typedef SkFlatDictionary<SkShader, SkShaderTraits> FlatDictionary;
scroggo@google.com013c5d92012-11-16 20:34:37 +000025
26class SkBitmapHeapTester {
27
28public:
29 static int32_t GetRefCount(const SkBitmapHeapEntry* entry) {
30 return entry->fRefCount;
31 }
32};
33
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000034DEF_TEST(BitmapHeap, reporter) {
scroggo@google.com013c5d92012-11-16 20:34:37 +000035 // Create a bitmap shader.
36 SkBitmap bm;
37 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
38 bm.allocPixels();
39 bm.eraseColor(SK_ColorRED);
40 uint32_t* pixel = bm.getAddr32(1,0);
41 *pixel = SK_ColorBLUE;
42
43 SkShader* bitmapShader = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
44 SkShader::kRepeat_TileMode);
45 SkAutoTUnref<SkShader> aur(bitmapShader);
46
47 // Flatten, storing it in the bitmap heap.
48 SkBitmapHeap heap(1, 1);
49 SkChunkFlatController controller(1024);
50 controller.setBitmapStorage(&heap);
51 FlatDictionary dictionary(&controller);
52
53 // Dictionary and heap start off empty.
54 REPORTER_ASSERT(reporter, heap.count() == 0);
55 REPORTER_ASSERT(reporter, dictionary.count() == 0);
56
57 heap.deferAddingOwners();
58 int index = dictionary.find(*bitmapShader);
59 heap.endAddingOwnersDeferral(true);
60
61 // The dictionary and heap should now each have one entry.
62 REPORTER_ASSERT(reporter, 1 == index);
63 REPORTER_ASSERT(reporter, heap.count() == 1);
64 REPORTER_ASSERT(reporter, dictionary.count() == 1);
65
66 // The bitmap entry's refcount should be 1, then 0 after release.
67 SkBitmapHeapEntry* entry = heap.getEntry(0);
68 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 1);
69
70 entry->releaseRef();
71 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(entry) == 0);
72
73 // Now clear out the heap, after which it should be empty.
74 heap.freeMemoryIfPossible(~0U);
75 REPORTER_ASSERT(reporter, heap.count() == 0);
76
77 // Now attempt to flatten the shader again.
78 heap.deferAddingOwners();
79 index = dictionary.find(*bitmapShader);
80 heap.endAddingOwnersDeferral(false);
81
82 // The dictionary should report the same index since the new entry is identical.
83 // The bitmap heap should contain the bitmap, but with no references.
84 REPORTER_ASSERT(reporter, 1 == index);
85 REPORTER_ASSERT(reporter, heap.count() == 1);
86 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0);
87}