Chromium Code Reviews| 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 "SkSurface.h" | 9 #include "SkSurface.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 | 189 |
| 190 uint32_t onGetFlags() const SK_OVERRIDE { | 190 uint32_t onGetFlags() const SK_OVERRIDE { |
| 191 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; | 191 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; |
| 192 } | 192 } |
| 193 | 193 |
| 194 private: | 194 private: |
| 195 typedef skiagm::GM INHERITED; | 195 typedef skiagm::GM INHERITED; |
| 196 }; | 196 }; |
| 197 DEF_GM( return new ImageGM; ) | 197 DEF_GM( return new ImageGM; ) |
| 198 | 198 |
| 199 class ImageResizeGM : public skiagm::GM { | |
| 200 enum { | |
| 201 W = 100, | |
| 202 H = 100, | |
| 203 }; | |
| 204 public: | |
| 205 ImageResizeGM() {} | |
| 206 | |
| 207 protected: | |
|
robertphillips
2015/01/22 15:50:02
one line ?
reed1
2015/01/22 18:25:51
Done.
| |
| 208 SkString onShortName() SK_OVERRIDE { | |
| 209 return SkString("image-resize"); | |
| 210 } | |
| 211 | |
|
robertphillips
2015/01/22 15:50:02
one line ?
reed1
2015/01/22 18:25:51
Done.
| |
| 212 SkISize onISize() SK_OVERRIDE { | |
| 213 return SkISize::Make(510, 480); | |
| 214 } | |
| 215 | |
|
robertphillips
2015/01/22 15:50:02
drawIntoImage or make static (and then DrawIntoIma
reed1
2015/01/22 18:25:51
Done.
| |
| 216 void draw_into_image(SkCanvas* canvas) { | |
| 217 SkPaint paint; | |
| 218 paint.setAntiAlias(true); | |
| 219 paint.setStyle(SkPaint::kStroke_Style); | |
| 220 paint.setStrokeWidth(3); | |
| 221 SkRandom rand; | |
| 222 for (int i = 0; i < 60; ++i) { | |
| 223 paint.setColor(rand.nextU()); | |
| 224 SkScalar x = rand.nextUScalar1() * W; | |
| 225 SkScalar y = rand.nextUScalar1() * H; | |
| 226 SkScalar r = rand.nextUScalar1() * W / 2; | |
| 227 canvas->drawCircle(x, y, r, paint); | |
| 228 } | |
| 229 } | |
| 230 | |
|
robertphillips
2015/01/22 15:50:02
makeImage or make static ?
reed1
2015/01/22 18:25:51
Done.
| |
| 231 SkImage* make_image(SkCanvas* canvas) { | |
| 232 const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); | |
| 233 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); | |
| 234 if (!surface) { | |
| 235 surface.reset(SkSurface::NewRaster(info)); | |
| 236 } | |
| 237 draw_into_image(surface->getCanvas()); | |
| 238 return surface->newImageSnapshot(); | |
| 239 } | |
| 240 | |
|
robertphillips
2015/01/22 15:50:02
same
reed1
2015/01/22 18:25:51
Done.
| |
| 241 void draw_resized(SkCanvas* canvas, SkImage* image, int newW, int newH, cons t SkIRect* subset, | |
| 242 SkFilterQuality fq) { | |
| 243 // canvas method | |
| 244 SkPaint paint; | |
| 245 paint.setFilterQuality(fq); | |
| 246 SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH)); | |
| 247 SkRect srcR; | |
| 248 if (subset) { | |
| 249 srcR.set(*subset); | |
| 250 } | |
| 251 canvas->drawImageRect(image, subset ? &srcR : NULL, dstR, &paint); | |
| 252 canvas->translate(newW + 20.0f, 0); | |
| 253 | |
| 254 // image method | |
| 255 SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq)); | |
| 256 canvas->drawImage(image2, 0, 0, NULL); | |
| 257 canvas->translate(image2->width() + 20.0f, 0); | |
| 258 } | |
| 259 | |
|
robertphillips
2015/01/22 15:50:02
same
reed1
2015/01/22 18:25:51
Done.
| |
| 260 void draw_image(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) { | |
| 261 | |
| 262 canvas->drawImage(image, 0, 0, NULL); | |
| 263 canvas->translate(image->width() + 20.0f, 0); | |
| 264 this->draw_resized(canvas, image, image->width() * 0.4f, image->height() * 0.4f, NULL, fq); | |
| 265 | |
| 266 SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2); | |
|
robertphillips
2015/01/22 15:50:02
this-> ?
reed1
2015/01/22 18:25:51
Done.
| |
| 267 this->draw_resized(canvas, image, W, H, &subset, fq); | |
| 268 } | |
| 269 | |
| 270 void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
| 271 canvas->translate(10, 10); | |
| 272 | |
|
robertphillips
2015/01/22 15:50:02
SkAutoTUnref ?
reed1
2015/01/22 18:25:51
Done.
| |
| 273 SkImage* image = make_image(canvas); | |
| 274 | |
| 275 const SkFilterQuality fq[] = { | |
| 276 kNone_SkFilterQuality, | |
| 277 kLow_SkFilterQuality, | |
| 278 kMedium_SkFilterQuality, | |
| 279 kHigh_SkFilterQuality, | |
| 280 }; | |
| 281 for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) { | |
| 282 { | |
| 283 SkAutoCanvasRestore acr(canvas, true); | |
| 284 draw_image(canvas, image, fq[i]); | |
| 285 } | |
| 286 canvas->translate(0, image->height() + 20.0f); | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 private: | |
| 291 typedef skiagm::GM INHERITED; | |
| 292 }; | |
| 293 DEF_GM( return new ImageResizeGM; ) | |
| 294 | |
| OLD | NEW |