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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "gm/gm.h" |
| 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkPicture.h" |
| 14 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 20 | |
| 21 | namespace skiagm { |
| 22 | |
| 23 | class DistantClipGM : public GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 24 | SkString onShortName() override { return SkString("distantclip"); } |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 25 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 26 | SkISize onISize() override { return {100, 100}; } |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 27 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 28 | void onDraw(SkCanvas* canvas) override { |
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. |
Mike Reed | 457c6dd | 2020-08-21 13:42:01 -0400 | [diff] [blame] | 36 | SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents); |
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); |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 40 | rec->clipPath(SkPath::RRect(r, 5, 5), true); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 41 | rec->drawColor(SK_ColorGREEN); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 42 | rec->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 43 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 44 | |
| 45 | // Next we play that picture into another picture of the same size. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 46 | pict->playback(recorder.beginRecording(pict->cullRect().width(), |
Mike Reed | 457c6dd | 2020-08-21 13:42:01 -0400 | [diff] [blame] | 47 | pict->cullRect().height())); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 48 | sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture()); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 49 | |
| 50 | // Finally we play the part of that second picture that should be green into the canvas. |
| 51 | canvas->save(); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 52 | canvas->translate(kExtents / 2, -(kOffset - kExtents / 2)); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 53 | pict2->playback(canvas); |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 54 | canvas->restore(); |
| 55 | |
| 56 | // If the image is red, we erroneously decided the clipPath was empty and didn't record |
| 57 | // the green drawColor, if it's green we're all good. |
| 58 | } |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 63 | DEF_GM( return new DistantClipGM; ) |
rileya@google.com | ffadfb5 | 2012-09-14 13:53:36 +0000 | [diff] [blame] | 64 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 65 | } // namespace skiagm |