rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2012 Google Inc. |
| 5 | * |
| 6 | * Use of this source code is governed by a BSD-style license that can be |
| 7 | * found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "gm.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class DistantClipGM : public GM { |
| 18 | public: |
| 19 | DistantClipGM() { } |
| 20 | |
| 21 | protected: |
| 22 | |
| 23 | SkString onShortName() { |
| 24 | return SkString("distantclip"); |
| 25 | } |
| 26 | |
| 27 | SkISize onISize() { return make_isize(100, 100); } |
| 28 | |
| 29 | virtual void onDraw(SkCanvas* canvas) { |
| 30 | int offset = 35000; |
| 31 | int extents = 1000; |
| 32 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 33 | SkPictureRecorder recorder; |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 34 | // We record a picture of huge vertical extents in which we clear the canvas to red, create |
| 35 | // a 'extents' by 'extents' round rect clip at a vertical offset of 'offset', then draw |
| 36 | // green into that. |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 37 | SkCanvas* rec = recorder.beginRecording(100, offset + extents, NULL, 0); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 38 | rec->drawColor(0xffff0000); |
| 39 | rec->save(); |
robertphillips@google.com | 968c17d | 2012-09-17 12:05:40 +0000 | [diff] [blame] | 40 | SkRect r = { |
skia.committer@gmail.com | 54339a8 | 2012-09-18 02:01:09 +0000 | [diff] [blame] | 41 | SkIntToScalar(-extents), |
| 42 | SkIntToScalar(offset - extents), |
| 43 | SkIntToScalar(extents), |
| 44 | SkIntToScalar(offset + extents) |
robertphillips@google.com | 968c17d | 2012-09-17 12:05:40 +0000 | [diff] [blame] | 45 | }; |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 46 | SkPath p; |
| 47 | p.addRoundRect(r, 5, 5); |
| 48 | rec->clipPath(p, SkRegion::kIntersect_Op, true); |
| 49 | rec->drawColor(0xff00ff00); |
| 50 | rec->restore(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 51 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 52 | |
| 53 | // Next we play that picture into another picture of the same size. |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 54 | pict->draw(recorder.beginRecording(100, offset + extents, NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 55 | SkAutoTUnref<SkPicture> pict2(recorder.endRecording()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 56 | |
| 57 | // Finally we play the part of that second picture that should be green into the canvas. |
| 58 | canvas->save(); |
skia.committer@gmail.com | 54339a8 | 2012-09-18 02:01:09 +0000 | [diff] [blame] | 59 | canvas->translate(SkIntToScalar(extents / 2), |
robertphillips@google.com | 968c17d | 2012-09-17 12:05:40 +0000 | [diff] [blame] | 60 | SkIntToScalar(-(offset - extents / 2))); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 61 | pict2->draw(canvas); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 62 | canvas->restore(); |
| 63 | |
| 64 | // If the image is red, we erroneously decided the clipPath was empty and didn't record |
| 65 | // the green drawColor, if it's green we're all good. |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | typedef GM INHERITED; |
| 70 | }; |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static GM* MyFactory(void*) { return new DistantClipGM; } |
| 75 | static GMRegistry reg(MyFactory); |
| 76 | |
| 77 | } |