blob: 98a00ca95211ccb7ff87a826245184e0c8b883fd [file] [log] [blame]
Mike Reed113fd342017-01-14 13:45:02 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Mike Reed113fd342017-01-14 13:45:02 -050010
Yuqian Lid196cbe2017-02-23 10:28:33 -050011
12static const uint32_t SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31;
13
Robert Phillips42f30942017-01-17 10:48:52 -050014// This GM tests out the deprecated Android-specific unclipped saveLayer "feature".
15// In particular, it attempts to compare the performance of unclipped saveLayers with alternatives.
16
17static void save_layer_unclipped(SkCanvas* canvas,
18 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
Mike Reed113fd342017-01-14 13:45:02 -050019 SkRect rect = SkRect::MakeLTRB(l, t, r, b);
Yuqian Lid196cbe2017-02-23 10:28:33 -050020 canvas->saveLayer({ &rect, nullptr, nullptr, SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag });
Mike Reed113fd342017-01-14 13:45:02 -050021}
22
23static void do_draw(SkCanvas* canvas) {
24 SkPaint paint;
25 SkRandom rand;
26
Mike Reed113fd342017-01-14 13:45:02 -050027 for (int i = 0; i < 20; ++i) {
Robert Phillips52b346e2017-01-19 08:37:31 -050028 paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24)));
Mike Reed113fd342017-01-14 13:45:02 -050029 canvas->drawRect({ 15, 15, 290, 40 }, paint);
30 canvas->translate(0, 30);
31 }
32}
33
Robert Phillips42f30942017-01-17 10:48:52 -050034class UnclippedSaveLayerGM : public skiagm::GM {
35public:
36 enum class Mode {
37 kClipped,
38 kUnclipped
39 };
Mike Reed113fd342017-01-14 13:45:02 -050040
Robert Phillips42f30942017-01-17 10:48:52 -050041 UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); }
Mike Reed113fd342017-01-14 13:45:02 -050042
Robert Phillips42f30942017-01-17 10:48:52 -050043protected:
44 bool runAsBench() const override { return true; }
45
46 SkString onShortName() override {
47 if (Mode::kClipped == fMode) {
48 return SkString("savelayer_unclipped");
49 } else {
50 SkASSERT(Mode::kUnclipped == fMode);
51 return SkString("savelayer_clipped");
52 }
Mike Reed113fd342017-01-14 13:45:02 -050053 }
Robert Phillips42f30942017-01-17 10:48:52 -050054
55 SkISize onISize() override { return SkISize::Make(320, 640); }
56
57 void onDraw(SkCanvas* canvas) override {
58 const SkScalar L = 10;
59 const SkScalar T = 10;
60 const SkScalar R = 310;
61 const SkScalar B = 630;
62
63 canvas->clipRect({ L, T, R, B });
64
65 for (int i = 0; i < 100; ++i) {
66 SkAutoCanvasRestore acr(canvas, true);
67 if (Mode::kClipped == fMode) {
68 save_layer_unclipped(canvas, L, T, R, T + 20);
69 save_layer_unclipped(canvas, L, B - 20, R, B);
70 } else {
71 SkASSERT(Mode::kUnclipped == fMode);
72 canvas->saveLayer({ L, T, R, B }, nullptr);
Yuqian Lid196cbe2017-02-23 10:28:33 -050073 }
Robert Phillips42f30942017-01-17 10:48:52 -050074
75 do_draw(canvas);
76 }
77 }
78
79private:
80 Mode fMode;
81
82 typedef skiagm::GM INHERITED;
83};
Mike Reedc61abee2017-02-28 17:45:27 -050084DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);)
85DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);)
Robert Phillips42f30942017-01-17 10:48:52 -050086
Yuqian Lid196cbe2017-02-23 10:28:33 -050087DEF_SIMPLE_GM(picture_savelayer, canvas, 320, 640) {
88 SkPaint paint1, paint2, paint3;
89 paint1.setAlpha(0x7f);
90 paint2.setAlpha(0x3f);
91 paint3.setColor(0xFFFF0000);
92 SkRect rect1{40, 5, 80, 70}, rect2{5, 40, 70, 80}, rect3{10, 10, 70, 70};
93 // In the future, we might also test the clipped case by allowing i = 0
94 for(int i = 1; i < 2; ++i) {
95 canvas->translate(100 * i, 0);
96 auto flag = i ? SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag : 0;
97 canvas->saveLayer({ &rect1, &paint1, nullptr, flag});
98 canvas->saveLayer({ &rect2, &paint2, nullptr, flag});
99 canvas->drawRect(rect3, paint3);
100 canvas->restore();
101 canvas->restore();
102 }
103};
104
Mike Reedc61abee2017-02-28 17:45:27 -0500105#include "Resources.h"
Robert Phillips42f30942017-01-17 10:48:52 -0500106
Mike Reedc61abee2017-02-28 17:45:27 -0500107// Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which
108// should seed the layer with the image (from below). Then we punch a hole in the layer and
109// restore with kPlus mode, which should show the mandrill super-bright on the outside, but
110// normal where we punched the hole.
111DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
112 canvas->drawImage(GetResourceAsImage("mandrill_256.png"), 0, 0, nullptr);
113
114 SkCanvas::SaveLayerRec rec;
115 SkPaint paint;
116 paint.setBlendMode(SkBlendMode::kPlus);
117 rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag;
118 rec.fPaint = &paint;
119 canvas->saveLayer(rec);
120 paint.setBlendMode(SkBlendMode::kClear);
121 canvas->drawCircle(128, 128, 96, paint);
122 canvas->restore();
123};
Robert Phillips42f30942017-01-17 10:48:52 -0500124