OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "gm.h" |
| 9 #include "SkBitmapSource.h" |
| 10 #include "SkBlurImageFilter.h" |
| 11 #include "SkDropShadowImageFilter.h" |
| 12 #include "SkMatrixImageFilter.h" |
| 13 #include "SkOffsetImageFilter.h" |
| 14 #include "SkPictureImageFilter.h" |
| 15 #include "SkPictureRecorder.h" |
| 16 #include "SkRandom.h" |
| 17 |
| 18 namespace skiagm { |
| 19 |
| 20 // Each method of this type must draw its geometry inside 'r' using 'p' |
| 21 typedef void(*drawMth)(SkCanvas* canvas, const SkRect& r, const SkPaint& p); |
| 22 |
| 23 static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 24 canvas->drawRect(r, p); |
| 25 } |
| 26 |
| 27 static void draw_oval(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 28 canvas->drawOval(r, p); |
| 29 } |
| 30 |
| 31 static void draw_rrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 32 SkScalar xRad = r.width() / 4.0f; |
| 33 SkScalar yRad = r.height() / 4.0f; |
| 34 |
| 35 SkRRect rr; |
| 36 rr.setRectXY(r, xRad, yRad); |
| 37 canvas->drawRRect(rr, p); |
| 38 } |
| 39 |
| 40 static void draw_drrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 41 SkScalar xRad = r.width() / 4.0f; |
| 42 SkScalar yRad = r.height() / 4.0f; |
| 43 |
| 44 SkRRect outer; |
| 45 outer.setRectXY(r, xRad, yRad); |
| 46 SkRRect inner = outer; |
| 47 inner.inset(xRad, yRad); |
| 48 canvas->drawDRRect(outer, inner, p); |
| 49 } |
| 50 |
| 51 static void draw_path(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 52 SkPath path; |
| 53 |
| 54 path.moveTo(r.fLeft, r.fTop); |
| 55 path.lineTo(r.fLeft, r.fBottom); |
| 56 path.lineTo(r.fRight, r.fBottom); |
| 57 path.close(); |
| 58 |
| 59 canvas->drawPath(path, p); |
| 60 } |
| 61 |
| 62 static void draw_points(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 63 SkPoint pts0[2] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } }; |
| 64 SkPoint pts1[2] = { { r.fLeft, r.fBottom }, { r.fRight, r.fTop } }; |
| 65 |
| 66 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts0, p); |
| 67 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts1, p); |
| 68 } |
| 69 |
| 70 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 71 SkBitmap bm; |
| 72 |
| 73 bm.allocN32Pixels(64, 64); |
| 74 SkCanvas temp(bm); |
| 75 temp.clear(SK_ColorMAGENTA); |
| 76 |
| 77 canvas->drawBitmapRect(bm, r, &p); |
| 78 } |
| 79 |
| 80 static const drawMth gDrawMthds[] = { |
| 81 draw_rect, draw_oval, draw_rrect, draw_drrect, draw_path, draw_points, draw_
bitmap |
| 82 }; |
| 83 |
| 84 static void add_paint(SkImageFilter* filter, SkTArray<SkPaint>* paints) { |
| 85 SkPaint& p = paints->push_back(); |
| 86 p.setImageFilter(filter); |
| 87 SkASSERT(p.canComputeFastBounds()); |
| 88 } |
| 89 |
| 90 // Create a selection of imagefilter-based paints to test |
| 91 static void create_paints(SkImageFilter* source, SkTArray<SkPaint>* paints) { |
| 92 { |
| 93 SkMatrix scale; |
| 94 scale.setScale(2.0f, 2.0f); |
| 95 |
| 96 SkAutoTUnref<SkMatrixImageFilter> scaleMIF( |
| 97 SkMatrixImageFilter::Create(scale, SkPaint::kLow_FilterLevel, source
)); |
| 98 |
| 99 add_paint(scaleMIF, paints); |
| 100 } |
| 101 |
| 102 { |
| 103 SkMatrix rot; |
| 104 rot.setRotate(-33.3f); |
| 105 |
| 106 SkAutoTUnref<SkMatrixImageFilter> rotMIF( |
| 107 SkMatrixImageFilter::Create(rot, SkPaint::kLow_FilterLevel, source))
; |
| 108 |
| 109 add_paint(rotMIF, paints); |
| 110 } |
| 111 |
| 112 { |
| 113 static const SkDropShadowImageFilter::ShadowMode kBoth = |
| 114 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode
; |
| 115 |
| 116 SkAutoTUnref<SkDropShadowImageFilter> dsif( |
| 117 SkDropShadowImageFilter::Create(10.0f, 10.0f, |
| 118 3.0f, 3.0f, |
| 119 SK_ColorRED, kBoth, |
| 120 source, NULL, 0)); |
| 121 |
| 122 add_paint(dsif, paints); |
| 123 } |
| 124 |
| 125 { |
| 126 SkAutoTUnref<SkDropShadowImageFilter> dsif( |
| 127 SkDropShadowImageFilter::Create(27.0f, 27.0f, |
| 128 3.0f, 3.0f, |
| 129 SK_ColorRED, |
| 130 SkDropShadowImageFilter::kDrawShadow
Only_ShadowMode, |
| 131 source, NULL, 0)); |
| 132 |
| 133 add_paint(dsif, paints); |
| 134 } |
| 135 |
| 136 { |
| 137 SkAutoTUnref<SkBlurImageFilter> bif(SkBlurImageFilter::Create(3, 3, sour
ce)); |
| 138 |
| 139 add_paint(bif, paints); |
| 140 } |
| 141 |
| 142 { |
| 143 SkAutoTUnref<SkOffsetImageFilter> oif(SkOffsetImageFilter::Create(15, 15
, source)); |
| 144 |
| 145 add_paint(oif, paints); |
| 146 } |
| 147 } |
| 148 |
| 149 // This GM visualizes the fast bounds for various combinations of geometry |
| 150 // and image filter |
| 151 class ImageFilterFastBoundGM : public GM { |
| 152 public: |
| 153 ImageFilterFastBoundGM() { |
| 154 this->setBGColor(0xFFCCCCCC); |
| 155 } |
| 156 |
| 157 protected: |
| 158 static const int kTileWidth = 100; |
| 159 static const int kTileHeight = 100; |
| 160 static const int kNumVertTiles = 6; |
| 161 static const int kNumXtraCols = 2; |
| 162 |
| 163 // SkPictureImageFilter doesn't support serialization yet. |
| 164 uint32_t onGetFlags() const SK_OVERRIDE { |
| 165 return kSkipPicture_Flag | |
| 166 kSkipPipe_Flag | |
| 167 kSkipPipeCrossProcess_Flag | |
| 168 kSkipTiled_Flag | |
| 169 kSkipScaledReplay_Flag; |
| 170 } |
| 171 |
| 172 SkString onShortName() SK_OVERRIDE{ return SkString("filterfastbounds"); } |
| 173 |
| 174 SkISize onISize() SK_OVERRIDE{ |
| 175 return SkISize::Make((SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols) * kTile
Width, |
| 176 kNumVertTiles * kTileHeight); |
| 177 } |
| 178 |
| 179 static void draw_geom_with_paint(drawMth draw, const SkIPoint& off, |
| 180 SkCanvas* canvas, const SkPaint& p) { |
| 181 SkPaint redStroked; |
| 182 redStroked.setColor(SK_ColorRED); |
| 183 redStroked.setStyle(SkPaint::kStroke_Style); |
| 184 |
| 185 SkPaint blueStroked; |
| 186 blueStroked.setColor(SK_ColorBLUE); |
| 187 blueStroked.setStyle(SkPaint::kStroke_Style); |
| 188 |
| 189 const SkRect r = SkRect::MakeLTRB(20, 20, 30, 30); |
| 190 SkRect storage; |
| 191 |
| 192 canvas->save(); |
| 193 canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY)); |
| 194 canvas->scale(1.5f, 1.5f); |
| 195 |
| 196 const SkRect& fastBound = p.computeFastBounds(r, &storage); |
| 197 |
| 198 canvas->save(); |
| 199 canvas->clipRect(fastBound); |
| 200 (*draw)(canvas, r, p); |
| 201 canvas->restore(); |
| 202 |
| 203 canvas->drawRect(r, redStroked); |
| 204 canvas->drawRect(fastBound, blueStroked); |
| 205 canvas->restore(); |
| 206 } |
| 207 |
| 208 static void draw_savelayer_with_paint(const SkIPoint& off, |
| 209 SkCanvas* canvas, |
| 210 const SkPaint& p) { |
| 211 SkPaint redStroked; |
| 212 redStroked.setColor(SK_ColorRED); |
| 213 redStroked.setStyle(SkPaint::kStroke_Style); |
| 214 |
| 215 SkPaint blueStroked; |
| 216 blueStroked.setColor(SK_ColorBLUE); |
| 217 blueStroked.setStyle(SkPaint::kStroke_Style); |
| 218 |
| 219 const SkRect bounds = SkRect::MakeWH(10, 10); |
| 220 SkRect storage; |
| 221 |
| 222 canvas->save(); |
| 223 canvas->translate(30, 30); |
| 224 canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY)); |
| 225 canvas->scale(1.5f, 1.5f); |
| 226 |
| 227 const SkRect& fastBound = p.computeFastBounds(bounds, &storage); |
| 228 |
| 229 canvas->saveLayer(&fastBound, &p); |
| 230 canvas->restore(); |
| 231 |
| 232 canvas->drawRect(bounds, redStroked); |
| 233 canvas->drawRect(fastBound, blueStroked); |
| 234 canvas->restore(); |
| 235 } |
| 236 |
| 237 void onDraw(SkCanvas* canvas) SK_OVERRIDE{ |
| 238 |
| 239 SkPaint blackFill; |
| 240 |
| 241 //----------- |
| 242 // Normal paints (no source) |
| 243 SkTArray<SkPaint> paints; |
| 244 create_paints(NULL, &paints); |
| 245 |
| 246 //----------- |
| 247 // Paints with a PictureImageFilter as a source |
| 248 SkAutoTUnref<SkPicture> pic; |
| 249 |
| 250 { |
| 251 SkPictureRecorder rec; |
| 252 |
| 253 SkCanvas* c = rec.beginRecording(10, 10); |
| 254 c->drawRect(SkRect::MakeWH(10, 10), blackFill); |
| 255 pic.reset(rec.endRecording()); |
| 256 } |
| 257 |
| 258 SkAutoTUnref<SkPictureImageFilter> pif(SkPictureImageFilter::Create(pic)
); |
| 259 |
| 260 SkTArray<SkPaint> pifPaints; |
| 261 create_paints(pif, &pifPaints); |
| 262 |
| 263 //----------- |
| 264 // Paints with a BitmapSource as a source |
| 265 SkBitmap bm; |
| 266 |
| 267 { |
| 268 SkPaint p; |
| 269 bm.allocN32Pixels(10, 10); |
| 270 SkCanvas temp(bm); |
| 271 temp.clear(SK_ColorYELLOW); |
| 272 p.setColor(SK_ColorBLUE); |
| 273 temp.drawRect(SkRect::MakeLTRB(5, 5, 10, 10), p); |
| 274 p.setColor(SK_ColorGREEN); |
| 275 temp.drawRect(SkRect::MakeLTRB(5, 0, 10, 5), p); |
| 276 } |
| 277 |
| 278 SkAutoTUnref<SkBitmapSource> bms(SkBitmapSource::Create(bm)); |
| 279 |
| 280 SkTArray<SkPaint> bmsPaints; |
| 281 create_paints(bms, &bmsPaints); |
| 282 |
| 283 //----------- |
| 284 SkASSERT(paints.count() == kNumVertTiles); |
| 285 SkASSERT(paints.count() == pifPaints.count()); |
| 286 SkASSERT(paints.count() == bmsPaints.count()); |
| 287 |
| 288 // horizontal separators |
| 289 for (int i = 1; i < paints.count(); ++i) { |
| 290 canvas->drawLine(0, |
| 291 i*SkIntToScalar(kTileHeight), |
| 292 SkIntToScalar((SK_ARRAY_COUNT(gDrawMthds) + kNumXtr
aCols)*kTileWidth), |
| 293 i*SkIntToScalar(kTileHeight), |
| 294 blackFill); |
| 295 } |
| 296 // vertical separators |
| 297 for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols; ++i)
{ |
| 298 canvas->drawLine(SkIntToScalar(i * kTileWidth), |
| 299 0, |
| 300 SkIntToScalar(i * kTileWidth), |
| 301 SkIntToScalar(paints.count() * kTileWidth), |
| 302 blackFill); |
| 303 } |
| 304 |
| 305 // A column of saveLayers with PictureImageFilters |
| 306 for (int i = 0; i < pifPaints.count(); ++i) { |
| 307 draw_savelayer_with_paint(SkIPoint::Make(0, i*kTileHeight), |
| 308 canvas, pifPaints[i]); |
| 309 } |
| 310 |
| 311 // A column of saveLayers with BitmapSources |
| 312 for (int i = 0; i < pifPaints.count(); ++i) { |
| 313 draw_savelayer_with_paint(SkIPoint::Make(kTileWidth, i*kTileHeight), |
| 314 canvas, bmsPaints[i]); |
| 315 } |
| 316 |
| 317 // Multiple columns with different geometry |
| 318 for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds); ++i) { |
| 319 for (int j = 0; j < paints.count(); ++j) { |
| 320 draw_geom_with_paint(*gDrawMthds[i], |
| 321 SkIPoint::Make((i+kNumXtraCols) * kTileWidt
h, j*kTileHeight), |
| 322 canvas, paints[j]); |
| 323 } |
| 324 } |
| 325 |
| 326 } |
| 327 |
| 328 private: |
| 329 typedef GM INHERITED; |
| 330 }; |
| 331 |
| 332 ////////////////////////////////////////////////////////////////////////////// |
| 333 |
| 334 DEF_GM(return SkNEW(ImageFilterFastBoundGM);) |
| 335 |
| 336 } |
OLD | NEW |