1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "TestSceneBase.h"
18
19class SaveLayerAnimation;
20
21static TestScene::Registrar _SaveLayer(TestScene::Info{
22        "savelayer",
23        "A nested pair of clipped saveLayer operations. "
24        "Tests the clipped saveLayer codepath. Draws content into offscreen buffers and back "
25        "again.",
26        TestScene::simpleCreateScene<SaveLayerAnimation>});
27
28class SaveLayerAnimation : public TestScene {
29public:
30    sp<RenderNode> card;
31    void createContent(int width, int height, Canvas& canvas) override {
32        canvas.drawColor(Color::White, SkBlendMode::kSrcOver);  // background
33
34        card = TestUtils::createNode(0, 0, 400, 800, [](RenderProperties& props, Canvas& canvas) {
35            // nested clipped saveLayers
36            canvas.saveLayerAlpha(0, 0, 400, 400, 200, SaveFlags::ClipToLayer);
37            canvas.drawColor(Color::Green_700, SkBlendMode::kSrcOver);
38            canvas.clipRect(50, 50, 350, 350, SkClipOp::kIntersect);
39            canvas.saveLayerAlpha(100, 100, 300, 300, 128, SaveFlags::ClipToLayer);
40            canvas.drawColor(Color::Blue_500, SkBlendMode::kSrcOver);
41            canvas.restore();
42            canvas.restore();
43
44            // single unclipped saveLayer
45            canvas.save(SaveFlags::MatrixClip);
46            canvas.translate(0, 400);
47            canvas.saveLayerAlpha(100, 100, 300, 300, 128, SaveFlags::Flags(0));  // unclipped
48            SkPaint paint;
49            paint.setAntiAlias(true);
50            paint.setColor(Color::Green_700);
51            canvas.drawCircle(200, 200, 200, paint);
52            canvas.restore();
53            canvas.restore();
54        });
55
56        canvas.drawRenderNode(card.get());
57    }
58    void doFrame(int frameNr) override {
59        int curFrame = frameNr % 150;
60        card->mutateStagingProperties().setTranslationX(curFrame);
61        card->mutateStagingProperties().setTranslationY(curFrame);
62        card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
63    }
64};
65