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