Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 1 | #Topic Automatic_Canvas_Restore |
| 2 | |
| 3 | #Class SkAutoCanvasRestore |
| 4 | |
Cary Clark | 61313f3 | 2018-10-08 14:57:48 -0400 | [diff] [blame] | 5 | #Code |
| 6 | #Populate |
| 7 | ## |
| 8 | |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 9 | Stack helper class calls SkCanvas::restoreToCount when SkAutoCanvasRestore |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 10 | goes out of scope. Use this to guarantee that the canvas is restored to a known |
| 11 | state. |
| 12 | |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 13 | #Method SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) |
| 14 | |
Cary Clark | 77b3f3a | 2018-11-07 14:59:03 -0500 | [diff] [blame] | 15 | #Line # restores Canvas when out of scope ## |
Cary Clark | 09d80c0 | 2018-10-31 12:14:03 -0400 | [diff] [blame] | 16 | #Populate |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 17 | |
| 18 | #Example |
| 19 | #Height 128 |
| 20 | SkPaint p; |
Mike Reed | 729bb19 | 2019-02-13 16:56:13 -0500 | [diff] [blame] | 21 | SkFont font(nullptr, 64); |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 22 | for (SkScalar sx : { -1, 1 } ) { |
| 23 | for (SkScalar sy : { -1, 1 } ) { |
| 24 | SkAutoCanvasRestore autoRestore(canvas, true); |
| 25 | SkMatrix m = SkMatrix::MakeAll(sx, 1, 96, 0, sy, 64, 0, 0, 1); |
| 26 | canvas->concat(m); |
Mike Reed | 729bb19 | 2019-02-13 16:56:13 -0500 | [diff] [blame] | 27 | canvas->drawString("R", 0, 0, font, p); |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | ## |
| 31 | |
| 32 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 33 | |
| 34 | ## |
| 35 | |
| 36 | #Method ~SkAutoCanvasRestore() |
| 37 | |
Cary Clark | ab2621d | 2018-01-30 10:08:57 -0500 | [diff] [blame] | 38 | #Line # restores Canvas to saved state ## |
Cary Clark | 09d80c0 | 2018-10-31 12:14:03 -0400 | [diff] [blame] | 39 | #Populate |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 40 | |
| 41 | #NoExample |
| 42 | ## |
| 43 | |
| 44 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 45 | |
| 46 | ## |
| 47 | |
| 48 | #Method void restore() |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 49 | #In Member_Function |
Cary Clark | ab2621d | 2018-01-30 10:08:57 -0500 | [diff] [blame] | 50 | #Line # restores Canvas to saved state ## |
Cary Clark | 09d80c0 | 2018-10-31 12:14:03 -0400 | [diff] [blame] | 51 | #Populate |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 52 | |
| 53 | #Example |
| 54 | for (bool callRestore : { false, true } ) { |
| 55 | for (bool saveCanvas : {false, true} ) { |
| 56 | SkAutoCanvasRestore autoRestore(canvas, saveCanvas); |
| 57 | if (!saveCanvas) { |
| 58 | canvas->save(); |
| 59 | } |
| 60 | SkDebugf("saveCanvas: %s before restore: %d\n", |
| 61 | saveCanvas ? "true" : "false", canvas->getSaveCount()); |
| 62 | if (callRestore) autoRestore.restore(); |
| 63 | SkDebugf("saveCanvas: %s after restore: %d\n", |
| 64 | saveCanvas ? "true" : "false", canvas->getSaveCount()); |
| 65 | } |
| 66 | } |
| 67 | SkDebugf("final count: %d\n", canvas->getSaveCount()); |
| 68 | #StdOut |
| 69 | saveCanvas: false before restore: 2 |
| 70 | saveCanvas: false after restore: 2 |
| 71 | saveCanvas: true before restore: 2 |
| 72 | saveCanvas: true after restore: 2 |
| 73 | saveCanvas: false before restore: 2 |
| 74 | saveCanvas: false after restore: 1 |
| 75 | saveCanvas: true before restore: 2 |
| 76 | saveCanvas: true after restore: 1 |
| 77 | final count: 1 |
| 78 | ## |
| 79 | ## |
| 80 | |
| 81 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 82 | |
| 83 | ## |
| 84 | |
| 85 | #Class SkAutoCanvasRestore ## |
| 86 | |
| 87 | #Topic Automatic_Canvas_Restore ## |