OLD | NEW |
1 | 1 |
2 | 2 |
3 /* | 3 /* |
4 * Copyright 2012 Google Inc. | 4 * Copyright 2012 Google Inc. |
5 * | 5 * |
6 * Use of this source code is governed by a BSD-style license that can be | 6 * Use of this source code is governed by a BSD-style license that can be |
7 * found in the LICENSE file. | 7 * found in the LICENSE file. |
8 */ | 8 */ |
9 | 9 |
10 #include "gm.h" | 10 #include "gm.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkPicture.h" | 12 #include "SkPicture.h" |
13 #include "SkPictureRecorder.h" | 13 #include "SkPictureRecorder.h" |
14 | 14 |
15 namespace skiagm { | 15 namespace skiagm { |
16 | 16 |
17 class DistantClipGM : public GM { | 17 class DistantClipGM : public GM { |
18 public: | 18 public: |
19 DistantClipGM() { } | 19 DistantClipGM() { } |
20 | 20 |
21 protected: | 21 protected: |
22 | 22 |
23 SkString onShortName() { | 23 SkString onShortName() { |
24 return SkString("distantclip"); | 24 return SkString("distantclip"); |
25 } | 25 } |
26 | 26 |
27 SkISize onISize() { return SkISize::Make(100, 100); } | 27 SkISize onISize() { return SkISize::Make(100, 100); } |
28 | 28 |
29 virtual void onDraw(SkCanvas* canvas) { | 29 virtual void onDraw(SkCanvas* canvas) { |
30 int offset = 35000; | 30 static const SkScalar kOffset = 35000.0f; |
31 int extents = 1000; | 31 static const SkScalar kExtents = 1000.0f; |
32 | 32 |
33 SkPictureRecorder recorder; | 33 SkPictureRecorder recorder; |
34 // We record a picture of huge vertical extents in which we clear the ca
nvas to red, create | 34 // We record a picture of huge vertical extents in which we clear the ca
nvas to red, create |
35 // a 'extents' by 'extents' round rect clip at a vertical offset of 'off
set', then draw | 35 // a 'extents' by 'extents' round rect clip at a vertical offset of 'off
set', then draw |
36 // green into that. | 36 // green into that. |
37 SkCanvas* rec = recorder.beginRecording(100, offset + extents, NULL, 0); | 37 SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, NU
LL, 0); |
38 rec->drawColor(0xffff0000); | 38 rec->drawColor(SK_ColorRED); |
39 rec->save(); | 39 rec->save(); |
40 SkRect r = { | 40 SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents,
2 * kExtents); |
41 SkIntToScalar(-extents), | |
42 SkIntToScalar(offset - extents), | |
43 SkIntToScalar(extents), | |
44 SkIntToScalar(offset + extents) | |
45 }; | |
46 SkPath p; | 41 SkPath p; |
47 p.addRoundRect(r, 5, 5); | 42 p.addRoundRect(r, 5, 5); |
48 rec->clipPath(p, SkRegion::kIntersect_Op, true); | 43 rec->clipPath(p, SkRegion::kIntersect_Op, true); |
49 rec->drawColor(0xff00ff00); | 44 rec->drawColor(SK_ColorGREEN); |
50 rec->restore(); | 45 rec->restore(); |
51 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); | 46 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
52 | 47 |
53 // Next we play that picture into another picture of the same size. | 48 // Next we play that picture into another picture of the same size. |
54 pict->draw(recorder.beginRecording(100, offset + extents, NULL, 0)); | 49 pict->draw(recorder.beginRecording(pict->cullRect().width(), |
| 50 pict->cullRect().height(), |
| 51 NULL, 0)); |
55 SkAutoTUnref<SkPicture> pict2(recorder.endRecording()); | 52 SkAutoTUnref<SkPicture> pict2(recorder.endRecording()); |
56 | 53 |
57 // Finally we play the part of that second picture that should be green
into the canvas. | 54 // Finally we play the part of that second picture that should be green
into the canvas. |
58 canvas->save(); | 55 canvas->save(); |
59 canvas->translate(SkIntToScalar(extents / 2), | 56 canvas->translate(kExtents / 2, -(kOffset - kExtents / 2)); |
60 SkIntToScalar(-(offset - extents / 2))); | |
61 pict2->draw(canvas); | 57 pict2->draw(canvas); |
62 canvas->restore(); | 58 canvas->restore(); |
63 | 59 |
64 // If the image is red, we erroneously decided the clipPath was empty an
d didn't record | 60 // If the image is red, we erroneously decided the clipPath was empty an
d didn't record |
65 // the green drawColor, if it's green we're all good. | 61 // the green drawColor, if it's green we're all good. |
66 } | 62 } |
67 | 63 |
68 private: | 64 private: |
69 typedef GM INHERITED; | 65 typedef GM INHERITED; |
70 }; | 66 }; |
71 | 67 |
72 /////////////////////////////////////////////////////////////////////////////// | 68 /////////////////////////////////////////////////////////////////////////////// |
73 | 69 |
74 static GM* MyFactory(void*) { return new DistantClipGM; } | 70 static GM* MyFactory(void*) { return new DistantClipGM; } |
75 static GMRegistry reg(MyFactory); | 71 static GMRegistry reg(MyFactory); |
76 | 72 |
77 } | 73 } |
OLD | NEW |