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 "gm.h" | 8 #include "gm.h" |
9 #include "SkPath.h" | 9 #include "SkPath.h" |
10 #include "SkRegion.h" | 10 #include "SkRegion.h" |
11 #include "SkShader.h" | 11 #include "SkShader.h" |
12 #include "SkUtils.h" | 12 #include "SkUtils.h" |
13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
14 #include "SkColorFilter.h" | 14 #include "SkColorFilter.h" |
15 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
16 | 16 |
17 // effects | 17 // effects |
18 #include "SkGradientShader.h" | 18 #include "SkGradientShader.h" |
19 #include "SkUnitMappers.h" | 19 #include "SkUnitMappers.h" |
20 #include "SkBlurDrawLooper.h" | 20 #include "SkBlurDrawLooper.h" |
21 | 21 |
22 static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { | 22 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
23 bm->allocConfigPixels(config, w, h); | 23 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
24 bm->eraseColor(SK_ColorTRANSPARENT); | 24 bm->eraseColor(SK_ColorTRANSPARENT); |
25 | 25 |
26 SkCanvas canvas(*bm); | 26 SkCanvas canvas(*bm); |
27 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; | 27 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; |
28 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; | 28 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
29 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | 29 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
30 SkPaint paint; | 30 SkPaint paint; |
31 | 31 |
32 SkUnitMapper* um = NULL; | 32 SkUnitMapper* um = NULL; |
33 | 33 |
34 um = new SkCosineMapper; | 34 um = new SkCosineMapper; |
35 // um = new SkDiscreteMapper(12); | 35 // um = new SkDiscreteMapper(12); |
36 | 36 |
37 SkAutoUnref au(um); | 37 SkAutoUnref au(um); |
38 | 38 |
39 paint.setDither(true); | 39 paint.setDither(true); |
40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, | 40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
41 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref()
; | 41 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref()
; |
42 canvas.drawPaint(paint); | 42 canvas.drawPaint(paint); |
43 } | 43 } |
44 | 44 |
45 static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filte
r_level, | 45 static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filte
r_level, |
46 SkShader::TileMode tmx, SkShader::TileMode tmy) { | 46 SkShader::TileMode tmx, SkShader::TileMode tmy) { |
47 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); | 47 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
48 paint->setShader(shader)->unref(); | 48 paint->setShader(shader)->unref(); |
49 paint->setFilterLevel(filter_level); | 49 paint->setFilterLevel(filter_level); |
50 } | 50 } |
51 | 51 |
52 static const SkBitmap::Config gConfigs[] = { | 52 static const SkColorType gColorTypes[] = { |
53 SkBitmap::kARGB_8888_Config, | 53 kPMColor_SkColorType, |
54 SkBitmap::kRGB_565_Config, | 54 kRGB_565_SkColorType, |
55 }; | 55 }; |
56 | 56 |
57 class ScaledTilingGM : public skiagm::GM { | 57 class ScaledTilingGM : public skiagm::GM { |
58 SkBlurDrawLooper fLooper; | 58 SkBlurDrawLooper fLooper; |
59 public: | 59 public: |
60 ScaledTilingGM(bool powerOfTwoSize) | 60 ScaledTilingGM(bool powerOfTwoSize) |
61 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 0x88
000000) | 61 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 0x88
000000) |
62 , fPowerOfTwoSize(powerOfTwoSize) { | 62 , fPowerOfTwoSize(powerOfTwoSize) { |
63 } | 63 } |
64 | 64 |
65 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; | 65 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
66 | 66 |
67 protected: | 67 protected: |
68 | 68 |
69 enum { | 69 enum { |
70 kPOTSize = 4, | 70 kPOTSize = 4, |
71 kNPOTSize = 3, | 71 kNPOTSize = 3, |
72 }; | 72 }; |
73 | 73 |
74 SkString onShortName() { | 74 SkString onShortName() { |
75 SkString name("scaled_tilemodes"); | 75 SkString name("scaled_tilemodes"); |
76 if (!fPowerOfTwoSize) { | 76 if (!fPowerOfTwoSize) { |
77 name.append("_npot"); | 77 name.append("_npot"); |
78 } | 78 } |
79 return name; | 79 return name; |
80 } | 80 } |
81 | 81 |
82 SkISize onISize() { return SkISize::Make(880, 760); } | 82 SkISize onISize() { return SkISize::Make(880, 760); } |
83 | 83 |
84 virtual void onOnceBeforeDraw() SK_OVERRIDE { | 84 virtual void onOnceBeforeDraw() SK_OVERRIDE { |
85 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; | 85 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
86 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { | 86 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
87 makebm(&fTexture[i], gConfigs[i], size, size); | 87 makebm(&fTexture[i], gColorTypes[i], size, size); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 91 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
92 | 92 |
93 float scale = 32.f/kPOTSize; | 93 float scale = 32.f/kPOTSize; |
94 | 94 |
95 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; | 95 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
96 | 96 |
97 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; | 97 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
98 | 98 |
99 static const char* gConfigNames[] = { "8888" , "565", "4444" }; | 99 static const char* gColorTypeNames[] = { "8888" , "565", "4444" }; |
100 | 100 |
101 static const SkPaint::FilterLevel gFilterLevels[] = | 101 static const SkPaint::FilterLevel gFilterLevels[] = |
102 { SkPaint::kNone_FilterLevel, | 102 { SkPaint::kNone_FilterLevel, |
103 SkPaint::kLow_FilterLevel, | 103 SkPaint::kLow_FilterLevel, |
104 SkPaint::kMedium_FilterLevel, | 104 SkPaint::kMedium_FilterLevel, |
105 SkPaint::kHigh_FilterLevel }; | 105 SkPaint::kHigh_FilterLevel }; |
106 static const char* gFilterNames[] = { "None", "Low", "Medium",
"High" }; | 106 static const char* gFilterNames[] = { "None", "Low", "Medium",
"High" }; |
107 | 107 |
108 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode,
SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; | 108 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode,
SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
109 static const char* gModeNames[] = { "C",
"R", "M" }; | 109 static const char* gModeNames[] = { "C",
"R", "M" }; |
(...skipping 12 matching lines...) Expand all Loading... |
122 | 122 |
123 p.setTextAlign(SkPaint::kCenter_Align); | 123 p.setTextAlign(SkPaint::kCenter_Align); |
124 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2
), y, p); | 124 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2
), y, p); |
125 | 125 |
126 x += r.width() * 4 / 3; | 126 x += r.width() * 4 / 3; |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 y = SkIntToScalar(40) / scale; | 130 y = SkIntToScalar(40) / scale; |
131 | 131 |
132 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { | 132 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
133 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterLevels); j++) { | 133 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterLevels); j++) { |
134 x = SkIntToScalar(10)/scale; | 134 x = SkIntToScalar(10)/scale; |
135 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { | 135 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
136 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { | 136 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
137 SkPaint paint; | 137 SkPaint paint; |
138 #if 1 // Temporary change to regen bitmap before each draw. This may help tracki
ng down an issue | 138 #if 1 // Temporary change to regen bitmap before each draw. This may help tracki
ng down an issue |
139 // on SGX where resizing NPOT textures to POT textures exhibits a driver b
ug. | 139 // on SGX where resizing NPOT textures to POT textures exhibits a driver b
ug. |
140 if (!fPowerOfTwoSize) { | 140 if (!fPowerOfTwoSize) { |
141 makebm(&fTexture[i], gConfigs[i], size, size); | 141 makebm(&fTexture[i], gColorTypes[i], size, size); |
142 } | 142 } |
143 #endif | 143 #endif |
144 setup(&paint, fTexture[i], gFilterLevels[j], gModes[kx],
gModes[ky]); | 144 setup(&paint, fTexture[i], gFilterLevels[j], gModes[kx],
gModes[ky]); |
145 paint.setDither(true); | 145 paint.setDither(true); |
146 | 146 |
147 canvas->save(); | 147 canvas->save(); |
148 canvas->scale(scale,scale); | 148 canvas->scale(scale,scale); |
149 canvas->translate(x, y); | 149 canvas->translate(x, y); |
150 canvas->drawRect(r, paint); | 150 canvas->drawRect(r, paint); |
151 canvas->restore(); | 151 canvas->restore(); |
152 | 152 |
153 x += r.width() * 4 / 3; | 153 x += r.width() * 4 / 3; |
154 } | 154 } |
155 } | 155 } |
156 { | 156 { |
157 SkPaint p; | 157 SkPaint p; |
158 SkString str; | 158 SkString str; |
159 p.setAntiAlias(true); | 159 p.setAntiAlias(true); |
160 p.setLooper(&fLooper); | 160 p.setLooper(&fLooper); |
161 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); | 161 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]); |
162 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y
+ r.height() * 2 / 3), p); | 162 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y
+ r.height() * 2 / 3), p); |
163 } | 163 } |
164 | 164 |
165 y += r.height() * 4 / 3; | 165 y += r.height() * 4 / 3; |
166 } | 166 } |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 private: | 170 private: |
171 bool fPowerOfTwoSize; | 171 bool fPowerOfTwoSize; |
172 typedef skiagm::GM INHERITED; | 172 typedef skiagm::GM INHERITED; |
173 }; | 173 }; |
174 | 174 |
175 static const int gWidth = 32; | 175 static const int gWidth = 32; |
176 static const int gHeight = 32; | 176 static const int gHeight = 32; |
177 | 177 |
178 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { | 178 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
179 SkBitmap bm; | 179 SkBitmap bm; |
180 makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight); | 180 makebm(&bm, kPMColor_SkColorType, gWidth, gHeight); |
181 return SkShader::CreateBitmapShader(bm, tx, ty); | 181 return SkShader::CreateBitmapShader(bm, tx, ty); |
182 } | 182 } |
183 | 183 |
184 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { | 184 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
185 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)}
}; | 185 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)}
}; |
186 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; | 186 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
187 SkScalar rad = SkIntToScalar(gWidth)/2; | 187 SkScalar rad = SkIntToScalar(gWidth)/2; |
188 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; | 188 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; |
189 | 189 |
190 int index = (int)ty; | 190 int index = (int)ty; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 private: | 272 private: |
273 typedef skiagm::GM INHERITED; | 273 typedef skiagm::GM INHERITED; |
274 }; | 274 }; |
275 | 275 |
276 ////////////////////////////////////////////////////////////////////////////// | 276 ////////////////////////////////////////////////////////////////////////////// |
277 | 277 |
278 DEF_GM( return new ScaledTilingGM(true); ) | 278 DEF_GM( return new ScaledTilingGM(true); ) |
279 DEF_GM( return new ScaledTilingGM(false); ) | 279 DEF_GM( return new ScaledTilingGM(false); ) |
280 DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) | 280 DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) |
281 DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) | 281 DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) |
OLD | NEW |