djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRect.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | /* |
| 17 | * This GM exercises the flags to SkCanvas::save(). The canvas' save() and |
| 18 | * restore actions can be limited to only a portion of the canvas' state through |
| 19 | * the use of flags when calling save. |
| 20 | */ |
| 21 | class CanvasStateGM : public GM { |
| 22 | SkSize fSize; |
| 23 | enum { |
| 24 | WIDTH = 150, |
| 25 | HEIGHT = 150, |
| 26 | }; |
| 27 | |
| 28 | SkPaint fFillPaint; |
| 29 | SkPaint fStrokePaint; |
| 30 | |
| 31 | SkPath fPath; |
| 32 | |
| 33 | SkRect fOutlineRect; |
| 34 | SkRect fFillRect; |
| 35 | |
| 36 | |
| 37 | public: |
| 38 | CanvasStateGM() { |
| 39 | fSize.set(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)); |
| 40 | |
| 41 | fFillPaint.setColor(SK_ColorRED); |
| 42 | fFillPaint.setStyle(SkPaint::kFill_Style); |
| 43 | |
| 44 | fStrokePaint.setColor(SK_ColorBLUE); |
| 45 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 46 | fStrokePaint.setStrokeWidth(1); |
| 47 | |
| 48 | fPath.moveTo(25, 25); |
| 49 | fPath.lineTo(125, 25); |
| 50 | fPath.lineTo(75, 125); |
| 51 | fPath.close(); |
| 52 | |
| 53 | fOutlineRect = SkRect::MakeXYWH(1, 1, WIDTH-2, HEIGHT-2); |
| 54 | fFillRect = SkRect::MakeXYWH(10, 10, WIDTH-20, HEIGHT-20); |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | virtual SkString onShortName() SK_OVERRIDE { |
| 59 | return SkString("canvas-state"); |
| 60 | } |
| 61 | |
| 62 | virtual SkISize onISize() SK_OVERRIDE { |
| 63 | return SkISize::Make(WIDTH*3, HEIGHT*4); |
| 64 | } |
| 65 | |
| 66 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 67 | |
| 68 | SkCanvas::SaveFlags flags[] = { SkCanvas::kMatrix_SaveFlag, |
| 69 | SkCanvas::kClip_SaveFlag, |
| 70 | SkCanvas::kMatrixClip_SaveFlag }; |
| 71 | |
| 72 | // columns -- flags |
| 73 | // rows -- permutations of setting the clip and matrix |
| 74 | for (size_t i = 0; i < SK_ARRAY_COUNT(flags); ++i) { |
| 75 | for (int j = 0; j < 2; ++j) { |
| 76 | for (int k = 0; k < 2; ++k) { |
| 77 | this->drawTestPattern(i, (2*j)+k, canvas, flags[i], j, k); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
djsollen@google.com | 2b1155a | 2013-08-13 14:48:09 +0000 | [diff] [blame^] | 84 | virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipPicture_Flag; } |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | void drawTestPattern(int x, int y, SkCanvas* canvas, |
| 88 | SkCanvas::SaveFlags flags, bool doClip, bool doScale) { |
| 89 | canvas->save(); |
| 90 | canvas->translate(x*WIDTH, y*HEIGHT); |
| 91 | |
| 92 | canvas->drawRect(fOutlineRect, fStrokePaint); |
| 93 | canvas->save(flags); |
| 94 | if(doClip) { |
| 95 | canvas->clipPath(fPath); |
| 96 | } |
| 97 | if (doScale) { |
djsollen@google.com | 2b1155a | 2013-08-13 14:48:09 +0000 | [diff] [blame^] | 98 | canvas->scale(SkDoubleToScalar(0.5), SkDoubleToScalar(0.5)); |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 99 | } |
| 100 | canvas->restore(); |
| 101 | canvas->drawRect(fFillRect, fFillPaint); |
| 102 | |
| 103 | canvas->restore(); |
| 104 | } |
| 105 | |
| 106 | typedef GM INHERITED; |
| 107 | }; |
| 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | DEF_GM( return SkNEW(CanvasStateGM); ) |
| 112 | |
| 113 | } // end namespace |