blob: 1fcded05bff783bc159c2f059530c62a90b35c7c [file] [log] [blame]
rileya@google.comffadfb52012-09-14 13:53:36 +00001
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"
13
14namespace skiagm {
15
16class DistantClipGM : public GM {
17public:
18 DistantClipGM() { }
19
20protected:
21
22 SkString onShortName() {
23 return SkString("distantclip");
24 }
25
26 SkISize onISize() { return make_isize(100, 100); }
27
28 virtual void onDraw(SkCanvas* canvas) {
29 int offset = 35000;
30 int extents = 1000;
31
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.
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000036 SkCanvas* rec = recorder.beginRecording(100, offset + extents, NULL, 0);
rileya@google.comffadfb52012-09-14 13:53:36 +000037 rec->drawColor(0xffff0000);
38 rec->save();
robertphillips@google.com968c17d2012-09-17 12:05:40 +000039 SkRect r = {
skia.committer@gmail.com54339a82012-09-18 02:01:09 +000040 SkIntToScalar(-extents),
41 SkIntToScalar(offset - extents),
42 SkIntToScalar(extents),
43 SkIntToScalar(offset + extents)
robertphillips@google.com968c17d2012-09-17 12:05:40 +000044 };
rileya@google.comffadfb52012-09-14 13:53:36 +000045 SkPath p;
46 p.addRoundRect(r, 5, 5);
47 rec->clipPath(p, SkRegion::kIntersect_Op, true);
48 rec->drawColor(0xff00ff00);
49 rec->restore();
robertphillips@google.com84b18c72014-04-13 19:09:42 +000050 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
rileya@google.comffadfb52012-09-14 13:53:36 +000051
52 // Next we play that picture into another picture of the same size.
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000053 pict->draw(recorder.beginRecording(100, offset + extents, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +000054 SkAutoTUnref<SkPicture> pict2(recorder.endRecording());
rileya@google.comffadfb52012-09-14 13:53:36 +000055
56 // Finally we play the part of that second picture that should be green into the canvas.
57 canvas->save();
skia.committer@gmail.com54339a82012-09-18 02:01:09 +000058 canvas->translate(SkIntToScalar(extents / 2),
robertphillips@google.com968c17d2012-09-17 12:05:40 +000059 SkIntToScalar(-(offset - extents / 2)));
robertphillips@google.com84b18c72014-04-13 19:09:42 +000060 pict2->draw(canvas);
rileya@google.comffadfb52012-09-14 13:53:36 +000061 canvas->restore();
62
63 // If the image is red, we erroneously decided the clipPath was empty and didn't record
64 // the green drawColor, if it's green we're all good.
65 }
66
67private:
68 typedef GM INHERITED;
69};
70
71///////////////////////////////////////////////////////////////////////////////
72
73static GM* MyFactory(void*) { return new DistantClipGM; }
74static GMRegistry reg(MyFactory);
75
76}