blob: fa752659a58a7f2b42243380e578e587348163bb [file] [log] [blame]
rileya@google.comffadfb52012-09-14 13:53:36 +00001
rileya@google.comffadfb52012-09-14 13:53:36 +00002/*
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"
Mike Reedc090c642017-05-16 10:39:06 -040011#include "SkPath.h"
rileya@google.comffadfb52012-09-14 13:53:36 +000012#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
rileya@google.comffadfb52012-09-14 13:53:36 +000014
15namespace skiagm {
16
17class DistantClipGM : public GM {
18public:
19 DistantClipGM() { }
20
21protected:
22
23 SkString onShortName() {
24 return SkString("distantclip");
25 }
26
tfarinaf5393182014-06-09 23:59:03 -070027 SkISize onISize() { return SkISize::Make(100, 100); }
rileya@google.comffadfb52012-09-14 13:53:36 +000028
29 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -070030 constexpr SkScalar kOffset = 35000.0f;
31 constexpr SkScalar kExtents = 1000.0f;
rileya@google.comffadfb52012-09-14 13:53:36 +000032
robertphillips@google.com84b18c72014-04-13 19:09:42 +000033 SkPictureRecorder recorder;
rileya@google.comffadfb52012-09-14 13:53:36 +000034 // 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.
halcanary96fcdcc2015-08-27 07:41:13 -070037 SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, nullptr, 0);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070038 rec->drawColor(SK_ColorRED);
rileya@google.comffadfb52012-09-14 13:53:36 +000039 rec->save();
robertphillipsa8d7f0b2014-08-29 08:03:56 -070040 SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents);
rileya@google.comffadfb52012-09-14 13:53:36 +000041 SkPath p;
42 p.addRoundRect(r, 5, 5);
reed66998382016-09-21 11:15:07 -070043 rec->clipPath(p, true);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070044 rec->drawColor(SK_ColorGREEN);
rileya@google.comffadfb52012-09-14 13:53:36 +000045 rec->restore();
reedca2622b2016-03-18 07:25:55 -070046 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
rileya@google.comffadfb52012-09-14 13:53:36 +000047
48 // Next we play that picture into another picture of the same size.
halcanary9d524f22016-03-29 09:03:52 -070049 pict->playback(recorder.beginRecording(pict->cullRect().width(),
50 pict->cullRect().height(),
halcanary96fcdcc2015-08-27 07:41:13 -070051 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -070052 sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture());
rileya@google.comffadfb52012-09-14 13:53:36 +000053
54 // Finally we play the part of that second picture that should be green into the canvas.
55 canvas->save();
robertphillipsa8d7f0b2014-08-29 08:03:56 -070056 canvas->translate(kExtents / 2, -(kOffset - kExtents / 2));
robertphillipsc5ba71d2014-09-04 08:42:50 -070057 pict2->playback(canvas);
rileya@google.comffadfb52012-09-14 13:53:36 +000058 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
64private:
65 typedef GM INHERITED;
66};
67
68///////////////////////////////////////////////////////////////////////////////
69
70static GM* MyFactory(void*) { return new DistantClipGM; }
71static GMRegistry reg(MyFactory);
72
73}