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 |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 74 | for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(flags)); ++i) { |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 75 | for (int j = 0; j < 2; ++j) { |
| 76 | for (int k = 0; k < 2; ++k) { |
reed@google.com | 75e9dfe | 2013-08-13 15:33:17 +0000 | [diff] [blame] | 77 | this->drawTestPattern(i, (2*j)+k, canvas, flags[i], |
| 78 | SkToBool(j), SkToBool(k)); |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
djsollen@google.com | 2b1155a | 2013-08-13 14:48:09 +0000 | [diff] [blame] | 85 | virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipPicture_Flag; } |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | void drawTestPattern(int x, int y, SkCanvas* canvas, |
| 89 | SkCanvas::SaveFlags flags, bool doClip, bool doScale) { |
| 90 | canvas->save(); |
reed@google.com | 75e9dfe | 2013-08-13 15:33:17 +0000 | [diff] [blame] | 91 | canvas->translate(SkIntToScalar(x*WIDTH), SkIntToScalar(y*HEIGHT)); |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 92 | |
| 93 | canvas->drawRect(fOutlineRect, fStrokePaint); |
| 94 | canvas->save(flags); |
| 95 | if(doClip) { |
| 96 | canvas->clipPath(fPath); |
| 97 | } |
| 98 | if (doScale) { |
djsollen@google.com | 2b1155a | 2013-08-13 14:48:09 +0000 | [diff] [blame] | 99 | canvas->scale(SkDoubleToScalar(0.5), SkDoubleToScalar(0.5)); |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 100 | } |
| 101 | canvas->restore(); |
| 102 | canvas->drawRect(fFillRect, fFillPaint); |
| 103 | |
| 104 | canvas->restore(); |
| 105 | } |
| 106 | |
| 107 | typedef GM INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 112 | class CanvasLayerStateGM : public GM { |
| 113 | public: |
| 114 | CanvasLayerStateGM() { |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 115 | fBluePaint.setColor(SK_ColorBLUE); |
| 116 | fBluePaint.setStyle(SkPaint::kFill_Style); |
| 117 | |
| 118 | fRect = SkRect::MakeXYWH(SPACER, SPACER, WIDTH-(2*SPACER), (HEIGHT-(2*SPACER)) / 7); |
| 119 | } |
| 120 | |
| 121 | protected: |
| 122 | virtual SkString onShortName() SK_OVERRIDE { |
| 123 | return SkString("canvas-layer-state"); |
| 124 | } |
| 125 | |
| 126 | virtual SkISize onISize() SK_OVERRIDE { |
| 127 | return SkISize::Make(WIDTH, HEIGHT); |
| 128 | } |
| 129 | |
| 130 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 131 | |
| 132 | // clear the canvas to red |
| 133 | canvas->drawColor(SK_ColorRED); |
| 134 | |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 135 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 136 | // both rects should appear |
| 137 | drawTestPattern(canvas, 255, SkCanvas::kARGB_NoClipLayer_SaveFlag); |
| 138 | |
| 139 | canvas->translate(0, 2*(fRect.height() + 10)); |
| 140 | |
| 141 | // only the top rect should appear |
| 142 | drawTestPattern(canvas, 255, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 143 | |
| 144 | canvas->translate(0, 2*(fRect.height() + 10)); |
| 145 | |
| 146 | // only the bottom rect should appear |
| 147 | drawTestPattern(canvas, 0, SkCanvas::kARGB_NoClipLayer_SaveFlag); |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 148 | #endif |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipGPU_Flag; } |
| 152 | |
| 153 | private: |
| 154 | // draw a rect within the layer's bounds and again outside the layer's bounds |
| 155 | void drawTestPattern(SkCanvas* canvas, U8CPU layerAlpha, SkCanvas::SaveFlags flags) { |
| 156 | canvas->saveLayerAlpha(&fRect, layerAlpha, flags); |
| 157 | canvas->drawRect(fRect, fBluePaint); |
| 158 | canvas->translate(0, fRect.height() + 10); |
| 159 | canvas->drawRect(fRect, fBluePaint); |
| 160 | canvas->restore(); |
| 161 | } |
| 162 | |
| 163 | enum { |
| 164 | WIDTH = 400, |
| 165 | HEIGHT = 400, |
| 166 | SPACER = 10, |
| 167 | }; |
| 168 | |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 169 | SkPaint fBluePaint; |
| 170 | SkRect fRect; |
| 171 | |
| 172 | typedef GM INHERITED; |
| 173 | }; |
| 174 | |
| 175 | ////////////////////////////////////////////////////////////////////////////// |
| 176 | |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 177 | DEF_GM( return SkNEW(CanvasStateGM); ) |
djsollen@google.com | 9615fed | 2013-08-20 17:54:33 +0000 | [diff] [blame] | 178 | DEF_GM( return SkNEW(CanvasLayerStateGM); ) |
djsollen@google.com | d423657 | 2013-08-13 14:29:06 +0000 | [diff] [blame] | 179 | |
| 180 | } // end namespace |