OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "gm.h" | 8 #include "gm.h" |
9 #include "SkGradientShader.h" | 9 #include "SkGradientShader.h" |
10 | 10 |
11 #include "SkTypeface.h" | 11 #include "SkTypeface.h" |
12 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 | 14 |
15 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty
le) { | 15 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty
le) { |
16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); | 16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); |
17 } | 17 } |
18 | 18 |
19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { | 19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { |
20 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), | 20 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), |
21 SkIntToScalar(bm.height())); | 21 SkIntToScalar(bm.height())); |
22 mat.mapRect(&bounds); | 22 mat.mapRect(&bounds); |
23 return SkSize::Make(bounds.width(), bounds.height()); | 23 return SkSize::Make(bounds.width(), bounds.height()); |
24 } | 24 } |
25 | 25 |
26 static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, | 26 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat,
SkScalar dx) { |
27 SkScalar dx) { | |
28 SkPaint paint; | 27 SkPaint paint; |
29 | 28 |
30 SkAutoCanvasRestore acr(canvas, true); | 29 SkAutoCanvasRestore acr(canvas, true); |
31 | 30 |
32 canvas->drawBitmapMatrix(bm, mat, &paint); | 31 canvas->drawBitmapMatrix(bm, mat, &paint); |
33 | 32 |
34 paint.setFilterLevel(SkPaint::kLow_FilterLevel); | 33 paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
35 canvas->translate(dx, 0); | 34 canvas->translate(dx, 0); |
36 canvas->drawBitmapMatrix(bm, mat, &paint); | 35 canvas->drawBitmapMatrix(bm, mat, &paint); |
37 | 36 |
| 37 paint.setFilterLevel(SkPaint::kMedium_FilterLevel); |
| 38 canvas->translate(dx, 0); |
| 39 canvas->drawBitmapMatrix(bm, mat, &paint); |
| 40 |
38 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); | 41 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
39 canvas->translate(dx, 0); | 42 canvas->translate(dx, 0); |
40 canvas->drawBitmapMatrix(bm, mat, &paint); | 43 canvas->drawBitmapMatrix(bm, mat, &paint); |
41 } | 44 } |
42 | 45 |
43 class FilterBitmapGM : public skiagm::GM { | 46 class FilterBitmapGM : public skiagm::GM { |
44 void onOnceBeforeDraw() { | 47 void onOnceBeforeDraw() { |
45 | 48 |
46 make_bitmap(); | 49 this->makeBitmap(); |
47 | 50 |
48 SkScalar cx = SkScalarHalf(fBM.width()); | 51 SkScalar cx = SkScalarHalf(fBM.width()); |
49 SkScalar cy = SkScalarHalf(fBM.height()); | 52 SkScalar cy = SkScalarHalf(fBM.height()); |
50 SkScalar scale = get_scale(); | 53 SkScalar scale = this->getScale(); |
51 | 54 |
52 | 55 // these two matrices use a scale factor configured by the subclass |
53 fMatrix[0].setScale(scale, scale); | 56 fMatrix[0].setScale(scale, scale); |
54 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); | 57 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); |
| 58 |
| 59 // up/down scaling mix |
| 60 fMatrix[2].setScale(0.7f, 1.05f); |
55 } | 61 } |
56 | 62 |
57 public: | 63 public: |
58 SkBitmap fBM; | 64 SkBitmap fBM; |
59 SkMatrix fMatrix[2]; | 65 SkMatrix fMatrix[3]; |
60 SkString fName; | 66 SkString fName; |
61 | 67 |
62 FilterBitmapGM() | 68 FilterBitmapGM() |
63 { | 69 { |
64 this->setBGColor(0xFFDDDDDD); | 70 this->setBGColor(0xFFDDDDDD); |
65 } | 71 } |
66 | 72 |
67 protected: | 73 protected: |
68 virtual SkString onShortName() SK_OVERRIDE { | 74 virtual SkString onShortName() SK_OVERRIDE { |
69 return fName; | 75 return fName; |
70 } | 76 } |
71 | 77 |
72 virtual SkISize onISize() SK_OVERRIDE { | 78 virtual SkISize onISize() SK_OVERRIDE { |
73 return SkISize::Make(920, 480); | 79 return SkISize::Make(1024, 768); |
74 } | 80 } |
75 | 81 |
76 virtual void make_bitmap() = 0; | 82 virtual void makeBitmap() = 0; |
77 virtual SkScalar get_scale() = 0; | 83 virtual SkScalar getScale() = 0; |
78 | 84 |
79 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 85 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
80 | 86 |
81 canvas->translate(10, 10); | 87 canvas->translate(10, 10); |
82 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { | 88 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { |
83 SkSize size = computeSize(fBM, fMatrix[i]); | 89 SkSize size = computeSize(fBM, fMatrix[i]); |
84 size.fWidth += 20; | 90 size.fWidth += 20; |
85 size.fHeight += 20; | 91 size.fHeight += 20; |
86 | 92 |
87 draw_col(canvas, fBM, fMatrix[i], size.fWidth); | 93 draw_row(canvas, fBM, fMatrix[i], size.fWidth); |
88 canvas->translate(0, size.fHeight); | 94 canvas->translate(0, size.fHeight); |
89 } | 95 } |
90 } | 96 } |
91 | 97 |
92 private: | 98 private: |
93 typedef skiagm::GM INHERITED; | 99 typedef skiagm::GM INHERITED; |
94 }; | 100 }; |
95 | 101 |
96 class FilterBitmapTextGM: public FilterBitmapGM { | 102 class FilterBitmapTextGM: public FilterBitmapGM { |
97 public: | 103 public: |
98 FilterBitmapTextGM(float textSize) | 104 FilterBitmapTextGM(float textSize) |
99 : fTextSize(textSize) | 105 : fTextSize(textSize) |
100 { | 106 { |
101 fName.printf("filterbitmap_text_%.2fpt", fTextSize); | 107 fName.printf("filterbitmap_text_%.2fpt", fTextSize); |
102 } | 108 } |
103 | 109 |
104 protected: | 110 protected: |
105 float fTextSize; | 111 float fTextSize; |
106 | 112 |
107 SkScalar get_scale() SK_OVERRIDE { | 113 SkScalar getScale() SK_OVERRIDE { |
108 return 32.f/fTextSize; | 114 return 32.f/fTextSize; |
109 } | 115 } |
110 | 116 |
111 void make_bitmap() SK_OVERRIDE { | 117 void makeBitmap() SK_OVERRIDE { |
112 fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTe
xtSize * 6)); | 118 fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTe
xtSize * 6)); |
113 fBM.allocPixels(); | 119 fBM.allocPixels(); |
114 SkCanvas canvas(fBM); | 120 SkCanvas canvas(fBM); |
115 canvas.drawColor(SK_ColorWHITE); | 121 canvas.drawColor(SK_ColorWHITE); |
116 | 122 |
117 SkPaint paint; | 123 SkPaint paint; |
118 paint.setAntiAlias(true); | 124 paint.setAntiAlias(true); |
119 paint.setSubpixelText(true); | 125 paint.setSubpixelText(true); |
120 paint.setTextSize(fTextSize); | 126 paint.setTextSize(fTextSize); |
121 | 127 |
(...skipping 15 matching lines...) Expand all Loading... |
137 FilterBitmapCheckerboardGM(int size, int num_checks) | 143 FilterBitmapCheckerboardGM(int size, int num_checks) |
138 : fSize(size), fNumChecks(num_checks) | 144 : fSize(size), fNumChecks(num_checks) |
139 { | 145 { |
140 fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); | 146 fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); |
141 } | 147 } |
142 | 148 |
143 protected: | 149 protected: |
144 int fSize; | 150 int fSize; |
145 int fNumChecks; | 151 int fNumChecks; |
146 | 152 |
147 SkScalar get_scale() SK_OVERRIDE { | 153 SkScalar getScale() SK_OVERRIDE { |
148 return 192.f/fSize; | 154 return 192.f/fSize; |
149 } | 155 } |
150 | 156 |
151 void make_bitmap() SK_OVERRIDE { | 157 void makeBitmap() SK_OVERRIDE { |
152 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize); | 158 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize); |
153 fBM.allocPixels(); | 159 fBM.allocPixels(); |
154 SkAutoLockPixels lock(fBM); | 160 SkAutoLockPixels lock(fBM); |
155 for (int y = 0; y < fSize; y ++) { | 161 for (int y = 0; y < fSize; y ++) { |
156 for (int x = 0; x < fSize; x ++) { | 162 for (int x = 0; x < fSize; x ++) { |
157 SkPMColor* s = fBM.getAddr32(x, y); | 163 SkPMColor* s = fBM.getAddr32(x, y); |
158 int cx = (x * fNumChecks) / fSize; | 164 int cx = (x * fNumChecks) / fSize; |
159 int cy = (y * fNumChecks) / fSize; | 165 int cy = (y * fNumChecks) / fSize; |
160 if ((cx+cy)%2) { | 166 if ((cx+cy)%2) { |
161 *s = 0xFFFFFFFF; | 167 *s = 0xFFFFFFFF; |
(...skipping 12 matching lines...) Expand all Loading... |
174 FilterBitmapImageGM(const char filename[]) | 180 FilterBitmapImageGM(const char filename[]) |
175 : fFilename(filename) | 181 : fFilename(filename) |
176 { | 182 { |
177 fName.printf("filterbitmap_image_%s", filename); | 183 fName.printf("filterbitmap_image_%s", filename); |
178 } | 184 } |
179 | 185 |
180 protected: | 186 protected: |
181 SkString fFilename; | 187 SkString fFilename; |
182 int fSize; | 188 int fSize; |
183 | 189 |
184 SkScalar get_scale() SK_OVERRIDE { | 190 SkScalar getScale() SK_OVERRIDE { |
185 return 192.f/fSize; | 191 return 192.f/fSize; |
186 } | 192 } |
187 | 193 |
188 void make_bitmap() SK_OVERRIDE { | 194 void makeBitmap() SK_OVERRIDE { |
189 SkString path(skiagm::GM::gResourcePath); | 195 SkString path(skiagm::GM::gResourcePath); |
190 path.append("/"); | 196 path.append("/"); |
191 path.append(fFilename); | 197 path.append(fFilename); |
192 | 198 |
193 SkImageDecoder *codec = NULL; | 199 SkImageDecoder *codec = NULL; |
194 SkFILEStream stream(path.c_str()); | 200 SkFILEStream stream(path.c_str()); |
195 if (stream.isValid()) { | 201 if (stream.isValid()) { |
196 codec = SkImageDecoder::Factory(&stream); | 202 codec = SkImageDecoder::Factory(&stream); |
197 } | 203 } |
198 if (codec) { | 204 if (codec) { |
(...skipping 21 matching lines...) Expand all Loading... |
220 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) | 226 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) |
221 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) | 227 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) |
222 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) | 228 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) |
223 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) | 229 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) |
224 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) | 230 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) |
225 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) | 231 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) |
226 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) | 232 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) |
227 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) | 233 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) |
228 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) | 234 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) |
229 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) | 235 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) |
OLD | NEW |