OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
13 #include "SkShader.h" | 13 #include "SkShader.h" |
14 #include "SkString.h" | 14 #include "SkString.h" |
15 | 15 #include "sk_tool_utils.h" |
16 static const char* gConfigName[] = { | |
17 "ERROR", "a1", "a8", "index8", "565", "4444", "8888" | |
18 }; | |
19 | 16 |
20 static void draw_into_bitmap(const SkBitmap& bm) { | 17 static void draw_into_bitmap(const SkBitmap& bm) { |
21 const int w = bm.width(); | 18 const int w = bm.width(); |
22 const int h = bm.height(); | 19 const int h = bm.height(); |
23 | 20 |
24 SkCanvas canvas(bm); | 21 SkCanvas canvas(bm); |
25 SkPaint p; | 22 SkPaint p; |
26 p.setAntiAlias(true); | 23 p.setAntiAlias(true); |
27 p.setColor(SK_ColorRED); | 24 p.setColor(SK_ColorRED); |
28 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, | 25 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, |
(...skipping 16 matching lines...) Expand all Loading... |
45 } | 42 } |
46 | 43 |
47 static uint8_t compute_666_index(SkPMColor c) { | 44 static uint8_t compute_666_index(SkPMColor c) { |
48 int r = SkGetPackedR32(c); | 45 int r = SkGetPackedR32(c); |
49 int g = SkGetPackedG32(c); | 46 int g = SkGetPackedG32(c); |
50 int b = SkGetPackedB32(c); | 47 int b = SkGetPackedB32(c); |
51 | 48 |
52 return conv_byte_to_6(r) * 36 + conv_byte_to_6(g) * 6 + conv_byte_to_6(b); | 49 return conv_byte_to_6(r) * 36 + conv_byte_to_6(g) * 6 + conv_byte_to_6(b); |
53 } | 50 } |
54 | 51 |
55 static void convert_to_index666(const SkBitmap& src, SkBitmap* dst, | 52 static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) { |
56 bool isOpaque) { | |
57 SkPMColor storage[216]; | 53 SkPMColor storage[216]; |
58 SkPMColor* colors = storage; | 54 SkPMColor* colors = storage; |
59 // rrr ggg bbb | 55 // rrr ggg bbb |
60 for (int r = 0; r < 6; r++) { | 56 for (int r = 0; r < 6; r++) { |
61 int rr = conv_6_to_byte(r); | 57 int rr = conv_6_to_byte(r); |
62 for (int g = 0; g < 6; g++) { | 58 for (int g = 0; g < 6; g++) { |
63 int gg = conv_6_to_byte(g); | 59 int gg = conv_6_to_byte(g); |
64 for (int b = 0; b < 6; b++) { | 60 for (int b = 0; b < 6; b++) { |
65 int bb = conv_6_to_byte(b); | 61 int bb = conv_6_to_byte(b); |
66 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb); | 62 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb); |
67 } | 63 } |
68 } | 64 } |
69 } | 65 } |
70 SkAlphaType aType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | 66 SkColorTable* ctable = new SkColorTable(storage, 216, kOpaque_SkAlphaType); |
71 SkColorTable* ctable = new SkColorTable(storage, 216, aType); | 67 dst->allocPixels(SkImageInfo::Make(src.width(), src.height(), |
72 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height()); | 68 kIndex_8_SkColorType, kOpaque_SkAlphaType
), |
73 dst->allocPixels(ctable); | 69 NULL, ctable); |
74 ctable->unref(); | 70 ctable->unref(); |
75 | 71 |
76 SkAutoLockPixels alps(src); | 72 SkAutoLockPixels alps(src); |
77 SkAutoLockPixels alpd(*dst); | 73 SkAutoLockPixels alpd(*dst); |
78 | 74 |
79 for (int y = 0; y < src.height(); y++) { | 75 for (int y = 0; y < src.height(); y++) { |
80 const SkPMColor* srcP = src.getAddr32(0, y); | 76 const SkPMColor* srcP = src.getAddr32(0, y); |
81 uint8_t* dstP = dst->getAddr8(0, y); | 77 uint8_t* dstP = dst->getAddr8(0, y); |
82 for (int x = src.width() - 1; x >= 0; --x) { | 78 for (int x = src.width() - 1; x >= 0; --x) { |
83 *dstP++ = compute_666_index(*srcP++); | 79 *dstP++ = compute_666_index(*srcP++); |
84 } | 80 } |
85 } | 81 } |
86 } | 82 } |
87 | 83 |
88 class RepeatTileBench : public SkBenchmark { | 84 class RepeatTileBench : public SkBenchmark { |
89 SkPaint fPaint; | 85 const SkColorType fColorType; |
90 SkString fName; | 86 const SkAlphaType fAlphaType; |
91 SkBitmap fBitmap; | 87 SkPaint fPaint; |
92 bool fIsOpaque; | 88 SkString fName; |
93 SkBitmap::Config fConfig; | 89 SkBitmap fBitmap; |
94 public: | 90 public: |
95 RepeatTileBench(SkBitmap::Config c, bool isOpaque = false) { | 91 RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) |
| 92 : fColorType(ct), fAlphaType(at) |
| 93 { |
96 const int w = 50; | 94 const int w = 50; |
97 const int h = 50; | 95 const int h = 50; |
98 fConfig = c; | |
99 fIsOpaque = isOpaque; | |
100 | 96 |
101 if (SkBitmap::kIndex8_Config == fConfig) { | 97 if (kIndex_8_SkColorType == ct) { |
102 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); | 98 fBitmap.setInfo(SkImageInfo::MakeN32(w, h, at)); |
103 } else { | 99 } else { |
104 fBitmap.setConfig(fConfig, w, h); | 100 fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at)); |
105 } | 101 } |
106 fName.printf("repeatTile_%s_%c", | 102 fName.printf("repeatTile_%s_%c", |
107 gConfigName[fBitmap.config()], isOpaque ? 'X' : 'A'); | 103 sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == a
t ? 'X' : 'A'); |
108 } | 104 } |
109 | 105 |
110 protected: | 106 protected: |
111 virtual const char* onGetName() SK_OVERRIDE { | 107 virtual const char* onGetName() SK_OVERRIDE { |
112 return fName.c_str(); | 108 return fName.c_str(); |
113 } | 109 } |
114 | 110 |
115 virtual void onPreDraw() SK_OVERRIDE { | 111 virtual void onPreDraw() SK_OVERRIDE { |
116 fBitmap.allocPixels(); | 112 fBitmap.allocPixels(); |
117 fBitmap.eraseColor(fIsOpaque ? SK_ColorWHITE : 0); | 113 fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0
); |
118 fBitmap.setAlphaType(fIsOpaque ? | |
119 kOpaque_SkAlphaType : kPremul_SkAlphaType); | |
120 | 114 |
121 draw_into_bitmap(fBitmap); | 115 draw_into_bitmap(fBitmap); |
122 | 116 |
123 if (SkBitmap::kIndex8_Config == fConfig) { | 117 if (kIndex_8_SkColorType == fColorType) { |
124 SkBitmap tmp; | 118 SkBitmap tmp; |
125 convert_to_index666(fBitmap, &tmp, fIsOpaque); | 119 convert_to_index666(fBitmap, &tmp); |
126 fBitmap = tmp; | 120 fBitmap = tmp; |
127 } | 121 } |
128 | 122 |
129 SkShader* s = SkShader::CreateBitmapShader(fBitmap, | 123 SkShader* s = SkShader::CreateBitmapShader(fBitmap, |
130 SkShader::kRepeat_TileMode, | 124 SkShader::kRepeat_TileMode, |
131 SkShader::kRepeat_TileMode); | 125 SkShader::kRepeat_TileMode); |
132 fPaint.setShader(s)->unref(); | 126 fPaint.setShader(s)->unref(); |
133 } | 127 } |
134 | 128 |
135 | 129 |
136 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 130 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
137 SkPaint paint(fPaint); | 131 SkPaint paint(fPaint); |
138 this->setupPaint(&paint); | 132 this->setupPaint(&paint); |
139 | 133 |
140 for (int i = 0; i < loops; i++) { | 134 for (int i = 0; i < loops; i++) { |
141 canvas->drawPaint(paint); | 135 canvas->drawPaint(paint); |
142 } | 136 } |
143 } | 137 } |
144 | 138 |
145 private: | 139 private: |
146 typedef SkBenchmark INHERITED; | 140 typedef SkBenchmark INHERITED; |
147 }; | 141 }; |
148 | 142 |
149 DEF_BENCH(return new RepeatTileBench(SkBitmap::kARGB_8888_Config, true)) | 143 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType)) |
150 DEF_BENCH(return new RepeatTileBench(SkBitmap::kARGB_8888_Config, false)) | 144 DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType)) |
151 DEF_BENCH(return new RepeatTileBench(SkBitmap::kRGB_565_Config)) | 145 DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType)) |
152 DEF_BENCH(return new RepeatTileBench(SkBitmap::kIndex8_Config)) | 146 DEF_BENCH(return new RepeatTileBench(kIndex_8_SkColorType, kPremul_SkAlphaType)) |
OLD | NEW |