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 "SkColorFilter.h" | 13 #include "SkColorFilter.h" |
14 | 14 |
15 // effects | 15 // effects |
16 #include "SkGradientShader.h" | 16 #include "SkGradientShader.h" |
17 #include "SkBlurDrawLooper.h" | 17 #include "SkBlurDrawLooper.h" |
18 | 18 |
19 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { | 19 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
20 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); | 20 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
21 bm->eraseColor(SK_ColorTRANSPARENT); | 21 bm->eraseColor(SK_ColorTRANSPARENT); |
22 | 22 |
23 SkCanvas canvas(*bm); | 23 SkCanvas canvas(*bm); |
24 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; | 24 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; |
25 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; | 25 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
26 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | 26 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
27 SkPaint paint; | 27 SkPaint paint; |
28 | 28 |
29 paint.setDither(true); | 29 paint.setDither(true); |
30 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, | 30 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUN
T(colors), |
31 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref(); | 31 SkShader::kClamp_TileMode)); |
32 canvas.drawPaint(paint); | 32 canvas.drawPaint(paint); |
33 } | 33 } |
34 | 34 |
35 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, | 35 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
36 SkShader::TileMode tmx, SkShader::TileMode tmy) { | 36 SkShader::TileMode tmx, SkShader::TileMode tmy) { |
37 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); | 37 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy)); |
38 paint->setShader(shader)->unref(); | |
39 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQualit
y); | 38 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQualit
y); |
40 } | 39 } |
41 | 40 |
42 static const SkColorType gColorTypes[] = { | 41 static const SkColorType gColorTypes[] = { |
43 kN32_SkColorType, | 42 kN32_SkColorType, |
44 kRGB_565_SkColorType, | 43 kRGB_565_SkColorType, |
45 }; | 44 }; |
46 | 45 |
47 class TilingGM : public skiagm::GM { | 46 class TilingGM : public skiagm::GM { |
48 public: | 47 public: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 148 } |
150 | 149 |
151 private: | 150 private: |
152 bool fPowerOfTwoSize; | 151 bool fPowerOfTwoSize; |
153 typedef skiagm::GM INHERITED; | 152 typedef skiagm::GM INHERITED; |
154 }; | 153 }; |
155 | 154 |
156 static const int gWidth = 32; | 155 static const int gWidth = 32; |
157 static const int gHeight = 32; | 156 static const int gHeight = 32; |
158 | 157 |
159 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { | 158 static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
160 SkBitmap bm; | 159 SkBitmap bm; |
161 makebm(&bm, kN32_SkColorType, gWidth, gHeight); | 160 makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
162 return SkShader::CreateBitmapShader(bm, tx, ty); | 161 return SkShader::MakeBitmapShader(bm, tx, ty); |
163 } | 162 } |
164 | 163 |
165 static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { | 164 static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
166 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)}
}; | 165 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)}
}; |
167 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; | 166 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
168 SkScalar rad = SkIntToScalar(gWidth)/2; | 167 SkScalar rad = SkIntToScalar(gWidth)/2; |
169 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) }; | 168 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) }; |
170 | 169 |
171 int index = (int)ty; | 170 int index = (int)ty; |
172 switch (index % 3) { | 171 switch (index % 3) { |
173 case 0: | 172 case 0: |
174 return SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY
_COUNT(colors), tx); | 173 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_C
OUNT(colors), tx); |
175 case 1: | 174 case 1: |
176 return SkGradientShader::CreateRadial(center, rad, colors, nullptr,
SK_ARRAY_COUNT(colors), tx); | 175 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK
_ARRAY_COUNT(colors), tx); |
177 case 2: | 176 case 2: |
178 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, n
ullptr, SK_ARRAY_COUNT(colors)); | 177 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nul
lptr, SK_ARRAY_COUNT(colors)); |
179 } | 178 } |
180 | |
181 return nullptr; | 179 return nullptr; |
182 } | 180 } |
183 | 181 |
184 typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); | 182 typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
185 | 183 |
186 class Tiling2GM : public skiagm::GM { | 184 class Tiling2GM : public skiagm::GM { |
187 ShaderProc fProc; | 185 ShaderProc fProc; |
188 SkString fName; | 186 SkString fName; |
189 public: | 187 public: |
190 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { | 188 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
191 fName.printf("tilemode_%s", name); | 189 fName.printf("tilemode_%s", name); |
192 } | 190 } |
193 | 191 |
194 protected: | 192 protected: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 230 |
233 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { | 231 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
234 x = SkIntToScalar(16) + w; | 232 x = SkIntToScalar(16) + w; |
235 | 233 |
236 SkString str(gModeNames[ky]); | 234 SkString str(gModeNames[ky]); |
237 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); | 235 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); |
238 | 236 |
239 x += SkIntToScalar(50); | 237 x += SkIntToScalar(50); |
240 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { | 238 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
241 SkPaint paint; | 239 SkPaint paint; |
242 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); | 240 paint.setShader(fProc(gModes[kx], gModes[ky])); |
243 | 241 |
244 canvas->save(); | 242 canvas->save(); |
245 canvas->translate(x, y); | 243 canvas->translate(x, y); |
246 canvas->drawRect(r, paint); | 244 canvas->drawRect(r, paint); |
247 canvas->restore(); | 245 canvas->restore(); |
248 | 246 |
249 x += r.width() * 4 / 3; | 247 x += r.width() * 4 / 3; |
250 } | 248 } |
251 y += r.height() * 4 / 3; | 249 y += r.height() * 4 / 3; |
252 } | 250 } |
253 } | 251 } |
254 | 252 |
255 private: | 253 private: |
256 typedef skiagm::GM INHERITED; | 254 typedef skiagm::GM INHERITED; |
257 }; | 255 }; |
258 | 256 |
259 ////////////////////////////////////////////////////////////////////////////// | 257 ////////////////////////////////////////////////////////////////////////////// |
260 | 258 |
261 DEF_GM( return new TilingGM(true); ) | 259 DEF_GM( return new TilingGM(true); ) |
262 DEF_GM( return new TilingGM(false); ) | 260 DEF_GM( return new TilingGM(false); ) |
263 DEF_GM( return new Tiling2GM(make_bm, "bitmap"); ) | 261 DEF_GM( return new Tiling2GM(make_bm, "bitmap"); ) |
264 DEF_GM( return new Tiling2GM(make_grad, "gradient"); ) | 262 DEF_GM( return new Tiling2GM(make_grad, "gradient"); ) |
OLD | NEW |