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 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 27 | SkISize onISize() { return SkISize::Make(100, 100); } |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 28 | |
| 29 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 30 | static const SkScalar kOffset = 35000.0f; |
| 31 | static const SkScalar kExtents = 1000.0f; |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 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. |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 37 | SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, NULL, 0); |
| 38 | rec->drawColor(SK_ColorRED); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 39 | rec->save(); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 40 | SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 41 | SkPath p; |
| 42 | p.addRoundRect(r, 5, 5); |
| 43 | rec->clipPath(p, SkRegion::kIntersect_Op, true); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 44 | rec->drawColor(SK_ColorGREEN); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 45 | rec->restore(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 46 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 47 | |
| 48 | // Next we play that picture into another picture of the same size. |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 49 | pict->draw(recorder.beginRecording(pict->cullRect().width(), |
| 50 | pict->cullRect().height(), |
| 51 | NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 52 | SkAutoTUnref<SkPicture> pict2(recorder.endRecording()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 53 | |
| 54 | // Finally we play the part of that second picture that should be green into the canvas. |
| 55 | canvas->save(); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 56 | canvas->translate(kExtents / 2, -(kOffset - kExtents / 2)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 57 | pict2->draw(canvas); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 58 | canvas->restore(); |
| 59 | |
| 60 | // If the image is red, we erroneously decided the clipPath was empty and didn't record |
| 61 | // the green drawColor, if it's green we're all good. |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | typedef GM INHERITED; |
| 66 | }; |
| 67 | |
| 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | static GM* MyFactory(void*) { return new DistantClipGM; } |
| 71 | static GMRegistry reg(MyFactory); |
| 72 | |
| 73 | } |