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 "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkView.h" | 9 #include "SkView.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkPaint.h" | 11 #include "SkPaint.h" |
12 #include "SkPath.h" | 12 #include "SkPath.h" |
13 #include "SkRegion.h" | 13 #include "SkRegion.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
15 #include "SkUtils.h" | 15 #include "SkUtils.h" |
16 #include "SkColorPriv.h" | 16 #include "SkColorPriv.h" |
17 #include "SkColorFilter.h" | 17 #include "SkColorFilter.h" |
18 #include "SkPicture.h" | 18 #include "SkPicture.h" |
19 #include "SkTypeface.h" | 19 #include "SkTypeface.h" |
20 | 20 |
21 // effects | 21 // effects |
22 #include "SkGradientShader.h" | 22 #include "SkGradientShader.h" |
23 #include "SkUnitMappers.h" | 23 #include "SkUnitMappers.h" |
24 #include "SkBlurMask.h" | 24 #include "SkBlurMask.h" |
25 #include "SkBlurDrawLooper.h" | 25 #include "SkBlurDrawLooper.h" |
26 | 26 |
27 static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { | 27 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
28 bm->setConfig(config, w, h); | 28 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
29 bm->allocPixels(); | |
30 bm->eraseColor(SK_ColorTRANSPARENT); | 29 bm->eraseColor(SK_ColorTRANSPARENT); |
31 | 30 |
32 SkCanvas canvas(*bm); | 31 SkCanvas canvas(*bm); |
33 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } }; | 32 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } }; |
34 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; | 33 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
35 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | 34 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
36 SkPaint paint; | 35 SkPaint paint; |
37 | 36 |
38 SkUnitMapper* um = NULL; | 37 SkUnitMapper* um = NULL; |
39 | 38 |
40 um = new SkCosineMapper; | 39 um = new SkCosineMapper; |
41 // um = new SkDiscreteMapper(12); | 40 // um = new SkDiscreteMapper(12); |
42 | 41 |
43 SkAutoUnref au(um); | 42 SkAutoUnref au(um); |
44 | 43 |
45 paint.setDither(true); | 44 paint.setDither(true); |
46 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, | 45 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
47 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref()
; | 46 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref()
; |
48 canvas.drawPaint(paint); | 47 canvas.drawPaint(paint); |
49 } | 48 } |
50 | 49 |
51 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, | 50 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
52 SkShader::TileMode tmx, SkShader::TileMode tmy) { | 51 SkShader::TileMode tmx, SkShader::TileMode tmy) { |
53 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); | 52 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
54 paint->setShader(shader)->unref(); | 53 paint->setShader(shader)->unref(); |
55 paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_Fi
lterLevel); | 54 paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_Fi
lterLevel); |
56 } | 55 } |
57 | 56 |
58 static const SkBitmap::Config gConfigs[] = { | 57 static const SkColorType gColorTypes[] = { |
59 SkBitmap::kARGB_8888_Config, | 58 kPMColor_SkColorType, |
60 SkBitmap::kRGB_565_Config, | 59 kRGB_565_SkColorType, |
61 }; | 60 }; |
62 static const int gWidth = 32; | 61 static const int gWidth = 32; |
63 static const int gHeight = 32; | 62 static const int gHeight = 32; |
64 | 63 |
65 class TilingView : public SampleView { | 64 class TilingView : public SampleView { |
66 SkPicture* fTextPicture; | 65 SkPicture* fTextPicture; |
67 SkBlurDrawLooper fLooper; | 66 SkBlurDrawLooper fLooper; |
68 public: | 67 public: |
69 TilingView() | 68 TilingView() |
70 : fLooper(0x88000000, | 69 : fLooper(0x88000000, |
71 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), | 70 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), |
72 SkIntToScalar(2), SkIntToScalar(2)) { | 71 SkIntToScalar(2), SkIntToScalar(2)) { |
73 fTextPicture = new SkPicture(); | 72 fTextPicture = new SkPicture(); |
74 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { | 73 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
75 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight); | 74 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight); |
76 } | 75 } |
77 } | 76 } |
78 | 77 |
79 ~TilingView() { | 78 ~TilingView() { |
80 fTextPicture->unref(); | 79 fTextPicture->unref(); |
81 } | 80 } |
82 | 81 |
83 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; | 82 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
84 | 83 |
85 protected: | 84 protected: |
86 // overrides from SkEventSink | 85 // overrides from SkEventSink |
87 virtual bool onQuery(SkEvent* evt) { | 86 virtual bool onQuery(SkEvent* evt) { |
88 if (SampleCode::TitleQ(*evt)) { | 87 if (SampleCode::TitleQ(*evt)) { |
89 SampleCode::TitleR(evt, "Tiling"); | 88 SampleCode::TitleR(evt, "Tiling"); |
90 return true; | 89 return true; |
91 } | 90 } |
92 return this->INHERITED::onQuery(evt); | 91 return this->INHERITED::onQuery(evt); |
93 } | 92 } |
(...skipping 30 matching lines...) Expand all Loading... |
124 p.setTextAlign(SkPaint::kCenter_Align); | 123 p.setTextAlign(SkPaint::kCenter_Align); |
125 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/
2, y, p); | 124 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/
2, y, p); |
126 | 125 |
127 x += r.width() * 4 / 3; | 126 x += r.width() * 4 / 3; |
128 } | 127 } |
129 } | 128 } |
130 } | 129 } |
131 | 130 |
132 y += SkIntToScalar(16); | 131 y += SkIntToScalar(16); |
133 | 132 |
134 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { | 133 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
135 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { | 134 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
136 x = SkIntToScalar(10); | 135 x = SkIntToScalar(10); |
137 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { | 136 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
138 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { | 137 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
139 SkPaint paint; | 138 SkPaint paint; |
140 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gMod
es[ky]); | 139 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gMod
es[ky]); |
141 paint.setDither(true); | 140 paint.setDither(true); |
142 | 141 |
143 canvas->save(); | 142 canvas->save(); |
144 canvas->translate(x, y); | 143 canvas->translate(x, y); |
(...skipping 20 matching lines...) Expand all Loading... |
165 } | 164 } |
166 | 165 |
167 private: | 166 private: |
168 typedef SampleView INHERITED; | 167 typedef SampleView INHERITED; |
169 }; | 168 }; |
170 | 169 |
171 ////////////////////////////////////////////////////////////////////////////// | 170 ////////////////////////////////////////////////////////////////////////////// |
172 | 171 |
173 static SkView* MyFactory() { return new TilingView; } | 172 static SkView* MyFactory() { return new TilingView; } |
174 static SkViewRegister reg(MyFactory); | 173 static SkViewRegister reg(MyFactory); |
OLD | NEW |