blob: a21567ee69d61fbe3cb233963e2305637e8402e3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "gm/gm.h"
10#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
13#include "include/core/SkPicture.h"
14#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#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.comffadfb52012-09-14 13:53:36 +000020
21namespace skiagm {
22
23class DistantClipGM : public GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -040024 SkString onShortName() override { return SkString("distantclip"); }
rileya@google.comffadfb52012-09-14 13:53:36 +000025
Hal Canaryfa3305a2019-07-18 12:36:54 -040026 SkISize onISize() override { return {100, 100}; }
rileya@google.comffadfb52012-09-14 13:53:36 +000027
Hal Canaryfa3305a2019-07-18 12:36:54 -040028 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070029 constexpr SkScalar kOffset = 35000.0f;
30 constexpr SkScalar kExtents = 1000.0f;
rileya@google.comffadfb52012-09-14 13:53:36 +000031
robertphillips@google.com84b18c72014-04-13 19:09:42 +000032 SkPictureRecorder recorder;
rileya@google.comffadfb52012-09-14 13:53:36 +000033 // 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 Reed457c6dd2020-08-21 13:42:01 -040036 SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070037 rec->drawColor(SK_ColorRED);
rileya@google.comffadfb52012-09-14 13:53:36 +000038 rec->save();
robertphillipsa8d7f0b2014-08-29 08:03:56 -070039 SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents);
Mike Reed92f6eb12020-08-25 11:48:41 -040040 rec->clipPath(SkPath::RRect(r, 5, 5), true);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070041 rec->drawColor(SK_ColorGREEN);
rileya@google.comffadfb52012-09-14 13:53:36 +000042 rec->restore();
reedca2622b2016-03-18 07:25:55 -070043 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
rileya@google.comffadfb52012-09-14 13:53:36 +000044
45 // Next we play that picture into another picture of the same size.
halcanary9d524f22016-03-29 09:03:52 -070046 pict->playback(recorder.beginRecording(pict->cullRect().width(),
Mike Reed457c6dd2020-08-21 13:42:01 -040047 pict->cullRect().height()));
reedca2622b2016-03-18 07:25:55 -070048 sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture());
rileya@google.comffadfb52012-09-14 13:53:36 +000049
50 // Finally we play the part of that second picture that should be green into the canvas.
51 canvas->save();
robertphillipsa8d7f0b2014-08-29 08:03:56 -070052 canvas->translate(kExtents / 2, -(kOffset - kExtents / 2));
robertphillipsc5ba71d2014-09-04 08:42:50 -070053 pict2->playback(canvas);
rileya@google.comffadfb52012-09-14 13:53:36 +000054 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.comffadfb52012-09-14 13:53:36 +000059};
60
61///////////////////////////////////////////////////////////////////////////////
62
Hal Canarye964c182019-01-23 10:22:01 -050063DEF_GM( return new DistantClipGM; )
rileya@google.comffadfb52012-09-14 13:53:36 +000064
John Stilesa6841be2020-08-06 14:11:56 -040065} // namespace skiagm