rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 1 | |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "gm.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 12 | #include "SkPictureRecorder.h" |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class DistantClipGM : public GM { |
| 17 | public: |
| 18 | DistantClipGM() { } |
| 19 | |
| 20 | protected: |
| 21 | |
| 22 | SkString onShortName() { |
| 23 | return SkString("distantclip"); |
| 24 | } |
| 25 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 26 | SkISize onISize() { return SkISize::Make(100, 100); } |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 27 | |
| 28 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 29 | constexpr SkScalar kOffset = 35000.0f; |
| 30 | constexpr SkScalar kExtents = 1000.0f; |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 31 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 32 | SkPictureRecorder recorder; |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 33 | // We record a picture of huge vertical extents in which we clear the canvas to red, create |
| 34 | // a 'extents' by 'extents' round rect clip at a vertical offset of 'offset', then draw |
| 35 | // green into that. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, nullptr, 0); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 37 | rec->drawColor(SK_ColorRED); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 38 | rec->save(); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 39 | SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 40 | SkPath p; |
| 41 | p.addRoundRect(r, 5, 5); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 42 | rec->clipPath(p, true); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 43 | rec->drawColor(SK_ColorGREEN); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 44 | rec->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 45 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 46 | |
| 47 | // Next we play that picture into another picture of the same size. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 48 | pict->playback(recorder.beginRecording(pict->cullRect().width(), |
| 49 | pict->cullRect().height(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 51 | sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 52 | |
| 53 | // Finally we play the part of that second picture that should be green into the canvas. |
| 54 | canvas->save(); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 55 | canvas->translate(kExtents / 2, -(kOffset - kExtents / 2)); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 56 | pict2->playback(canvas); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 57 | canvas->restore(); |
| 58 | |
| 59 | // If the image is red, we erroneously decided the clipPath was empty and didn't record |
| 60 | // the green drawColor, if it's green we're all good. |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | typedef GM INHERITED; |
| 65 | }; |
| 66 | |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | static GM* MyFactory(void*) { return new DistantClipGM; } |
| 70 | static GMRegistry reg(MyFactory); |
| 71 | |
| 72 | } |