Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapHeap.h" | 9 #include "SkBitmapHeap.h" |
| 10 #include "SkColor.h" | 10 #include "SkColor.h" |
| 11 #include "SkFlattenable.h" | 11 #include "SkFlattenable.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkPictureFlat.h" | 13 #include "SkPictureFlat.h" |
| 14 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
| 15 #include "SkShader.h" | 15 #include "SkShader.h" |
| 16 #include "Test.h" | 16 #include "Test.h" |
| 17 | 17 |
| 18 struct SimpleFlatController : public SkFlatController { | |
| 19 SimpleFlatController() : SkFlatController() {} | |
|
robertphillips
2014/11/12 17:14:42
SK_OVERRIDE on these two ?
| |
| 20 virtual void* allocThrow(size_t bytes) { return sk_malloc_throw(bytes); } | |
| 21 virtual void unalloc(void* ptr) { sk_free(ptr); } | |
| 22 void setBitmapStorage(SkBitmapHeap* h) { this->setBitmapHeap(h); } | |
| 23 }; | |
| 24 | |
| 18 struct SkShaderTraits { | 25 struct SkShaderTraits { |
| 19 static void Flatten(SkWriteBuffer& buffer, const SkShader& shader) { | 26 static void Flatten(SkWriteBuffer& buffer, const SkShader& shader) { |
| 20 buffer.writeFlattenable(&shader); | 27 buffer.writeFlattenable(&shader); |
| 21 } | 28 } |
| 22 }; | 29 }; |
| 23 typedef SkFlatDictionary<SkShader, SkShaderTraits> FlatDictionary; | 30 typedef SkFlatDictionary<SkShader, SkShaderTraits> FlatDictionary; |
| 24 | 31 |
| 25 class SkBitmapHeapTester { | 32 class SkBitmapHeapTester { |
| 26 | |
| 27 public: | 33 public: |
| 28 static int32_t GetRefCount(const SkBitmapHeapEntry* entry) { | 34 static int32_t GetRefCount(const SkBitmapHeapEntry* entry) { |
| 29 return entry->fRefCount; | 35 return entry->fRefCount; |
| 30 } | 36 } |
| 31 }; | 37 }; |
| 32 | 38 |
| 33 DEF_TEST(BitmapHeap, reporter) { | 39 DEF_TEST(BitmapHeap, reporter) { |
| 34 // Create a bitmap shader. | 40 // Create a bitmap shader. |
| 35 SkBitmap bm; | 41 SkBitmap bm; |
| 36 bm.allocN32Pixels(2, 2); | 42 bm.allocN32Pixels(2, 2); |
| 37 bm.eraseColor(SK_ColorRED); | 43 bm.eraseColor(SK_ColorRED); |
| 38 uint32_t* pixel = bm.getAddr32(1,0); | 44 uint32_t* pixel = bm.getAddr32(1,0); |
| 39 *pixel = SK_ColorBLUE; | 45 *pixel = SK_ColorBLUE; |
| 40 | 46 |
| 41 SkShader* bitmapShader = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_ TileMode, | 47 SkShader* bitmapShader = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_ TileMode, |
| 42 SkShader::kRepeat_Tile Mode); | 48 SkShader::kRepeat_Tile Mode); |
| 43 SkAutoTUnref<SkShader> aur(bitmapShader); | 49 SkAutoTUnref<SkShader> aur(bitmapShader); |
| 44 | 50 |
| 45 // Flatten, storing it in the bitmap heap. | 51 // Flatten, storing it in the bitmap heap. |
| 46 SkBitmapHeap heap(1, 1); | 52 SkBitmapHeap heap(1, 1); |
| 47 SkChunkFlatController controller(1024); | 53 SimpleFlatController controller; |
| 48 controller.setBitmapStorage(&heap); | 54 controller.setBitmapStorage(&heap); |
| 49 FlatDictionary dictionary(&controller); | 55 FlatDictionary dictionary(&controller); |
| 50 | 56 |
| 51 // Dictionary and heap start off empty. | 57 // Dictionary and heap start off empty. |
| 52 REPORTER_ASSERT(reporter, heap.count() == 0); | 58 REPORTER_ASSERT(reporter, heap.count() == 0); |
| 53 REPORTER_ASSERT(reporter, dictionary.count() == 0); | 59 REPORTER_ASSERT(reporter, dictionary.count() == 0); |
| 54 | 60 |
| 55 heap.deferAddingOwners(); | 61 heap.deferAddingOwners(); |
| 56 int index = dictionary.find(*bitmapShader); | 62 int index = dictionary.find(*bitmapShader); |
| 57 heap.endAddingOwnersDeferral(true); | 63 heap.endAddingOwnersDeferral(true); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 76 heap.deferAddingOwners(); | 82 heap.deferAddingOwners(); |
| 77 index = dictionary.find(*bitmapShader); | 83 index = dictionary.find(*bitmapShader); |
| 78 heap.endAddingOwnersDeferral(false); | 84 heap.endAddingOwnersDeferral(false); |
| 79 | 85 |
| 80 // The dictionary should report the same index since the new entry is identi cal. | 86 // The dictionary should report the same index since the new entry is identi cal. |
| 81 // The bitmap heap should contain the bitmap, but with no references. | 87 // The bitmap heap should contain the bitmap, but with no references. |
| 82 REPORTER_ASSERT(reporter, 1 == index); | 88 REPORTER_ASSERT(reporter, 1 == index); |
| 83 REPORTER_ASSERT(reporter, heap.count() == 1); | 89 REPORTER_ASSERT(reporter, heap.count() == 1); |
| 84 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0); | 90 REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0); |
| 85 } | 91 } |
| OLD | NEW |