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 "Test.h" | 8 #include "Test.h" |
9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkColor.h" | 12 #include "SkColor.h" |
13 #include "SkColorFilter.h" | 13 #include "SkColorFilter.h" |
14 #include "SkGradientShader.h" | 14 #include "SkGradientShader.h" |
15 #include "SkPaint.h" | 15 #include "SkPaint.h" |
16 #include "SkPictureFlat.h" | 16 #include "SkPictureFlat.h" |
17 #include "SkShader.h" | 17 #include "SkShader.h" |
18 #include "SkXfermode.h" | 18 #include "SkXfermode.h" |
19 | 19 |
20 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer, | 20 struct SkFlattenableTraits { |
21 const void* obj) { | 21 static void flatten(SkOrderedWriteBuffer& buffer, const SkFlattenable& flatt
enable) { |
22 buffer.writeFlattenable((SkFlattenable*)obj); | 22 buffer.writeFlattenable(&flattenable); |
23 } | 23 } |
| 24 }; |
| 25 struct SkBitmapTraits { |
| 26 static void flatten(SkOrderedWriteBuffer& buffer, const SkBitmap& bitmap) { |
| 27 bitmap.flatten(buffer); |
| 28 } |
| 29 }; |
24 | 30 |
25 class Controller : public SkChunkFlatController { | 31 class Controller : public SkChunkFlatController { |
26 public: | 32 public: |
27 Controller() : INHERITED(1024) { | 33 Controller() : INHERITED(1024) { |
28 this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref(); | 34 this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref(); |
29 } | 35 } |
30 private: | 36 private: |
31 typedef SkChunkFlatController INHERITED; | 37 typedef SkChunkFlatController INHERITED; |
32 }; | 38 }; |
33 | 39 |
34 /** | 40 /** |
35 * Verify that two SkFlatData objects that created from the same object are | 41 * Verify that two SkFlatData objects that created from the same object are |
36 * identical when using an SkNamedFactorySet. | 42 * identical when using an SkNamedFactorySet. |
37 * @param reporter Object to report failures. | 43 * @param reporter Object to report failures. |
38 * @param obj Flattenable object to be flattened. | 44 * @param obj Flattenable object to be flattened. |
39 * @param flattenProc Function that flattens objects with the same type as obj. | 45 * @param flattenProc Function that flattens objects with the same type as obj. |
40 */ | 46 */ |
41 static void testCreate(skiatest::Reporter* reporter, const void* obj, | 47 template <typename Traits, typename T> |
42 void (*flattenProc)(SkOrderedWriteBuffer&, const void*))
{ | 48 static void testCreate(skiatest::Reporter* reporter, const T& obj) { |
43 Controller controller; | 49 Controller controller; |
44 // No need to delete data because that will be taken care of by the | 50 // No need to delete data because that will be taken care of by the controll
er. |
45 // controller. | 51 SkFlatData* data1 = SkFlatData::Create<Traits>(&controller, obj, 0); |
46 SkFlatData* data1 = SkFlatData::Create(&controller, obj, 0, flattenProc); | 52 SkFlatData* data2 = SkFlatData::Create<Traits>(&controller, obj, 1); |
47 SkFlatData* data2 = SkFlatData::Create(&controller, obj, 1, flattenProc); | |
48 REPORTER_ASSERT(reporter, *data1 == *data2); | 53 REPORTER_ASSERT(reporter, *data1 == *data2); |
49 } | 54 } |
50 | 55 |
51 DEF_TEST(FlatData, reporter) { | 56 DEF_TEST(FlatData, reporter) { |
52 // Test flattening SkShader | 57 // Test flattening SkShader |
53 SkPoint points[2]; | 58 SkPoint points[2]; |
54 points[0].set(0, 0); | 59 points[0].set(0, 0); |
55 points[1].set(SkIntToScalar(20), SkIntToScalar(20)); | 60 points[1].set(SkIntToScalar(20), SkIntToScalar(20)); |
56 SkColor colors[2]; | 61 SkColor colors[2]; |
57 colors[0] = SK_ColorRED; | 62 colors[0] = SK_ColorRED; |
58 colors[1] = SK_ColorBLUE; | 63 colors[1] = SK_ColorBLUE; |
59 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL, | 64 |
60 2, SkShader::kRepeat_TileMode); | 65 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors,
NULL, 2, |
61 SkAutoUnref aur(shader); | 66 SkShader::kRepe
at_TileMode)); |
62 testCreate(reporter, shader, flattenFlattenableProc); | 67 testCreate<SkFlattenableTraits>(reporter, *shader); |
63 | 68 |
64 // Test SkBitmap | 69 // Test SkBitmap |
65 { | 70 { |
66 SkBitmap bm; | 71 SkBitmap bm; |
67 bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50); | 72 bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50); |
68 bm.allocPixels(); | 73 bm.allocPixels(); |
69 SkCanvas canvas(bm); | 74 SkCanvas canvas(bm); |
70 SkPaint paint; | 75 SkPaint paint; |
71 paint.setShader(shader); | 76 paint.setShader(shader); |
72 canvas.drawPaint(paint); | 77 canvas.drawPaint(paint); |
73 testCreate(reporter, &bm, &SkFlattenObjectProc<SkBitmap>); | 78 testCreate<SkBitmapTraits>(reporter, bm); |
74 } | 79 } |
75 | 80 |
76 // Test SkColorFilter | 81 // Test SkColorFilter |
77 SkColorFilter* cf = SkColorFilter::CreateLightingFilter(SK_ColorBLUE, | 82 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorB
LUE, SK_ColorRED)); |
78 SK_ColorRED); | 83 testCreate<SkFlattenableTraits>(reporter, *cf); |
79 SkAutoUnref aurcf(cf); | |
80 testCreate(reporter, cf, &flattenFlattenableProc); | |
81 | 84 |
82 // Test SkXfermode | 85 // Test SkXfermode |
83 SkXfermode* xfer = SkXfermode::Create(SkXfermode::kDstOver_Mode); | 86 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode))
; |
84 SkAutoUnref aurxf(xfer); | 87 testCreate<SkFlattenableTraits>(reporter, *xfer); |
85 testCreate(reporter, xfer, &flattenFlattenableProc); | |
86 } | 88 } |
OLD | NEW |