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 again.",
25    TestScene::simpleCreateScene<SaveLayerAnimation>
26});
27
28class SaveLayerAnimation : public TestScene {
29public:
30    sp<RenderNode> card;
31    void createContent(int width, int height, TestCanvas& canvas) override {
32        canvas.drawColor(Color::White, SkXfermode::kSrcOver_Mode); // background
33
34        card = TestUtils::createNode(0, 0, 400, 800,
35                [](RenderProperties& props, TestCanvas& canvas) {
36            // nested clipped saveLayers
37            canvas.saveLayerAlpha(0, 0, 400, 400, 200, SaveFlags::ClipToLayer);
38            canvas.drawColor(Color::Green_700, SkXfermode::kSrcOver_Mode);
39            canvas.clipRect(50, 50, 350, 350, SkRegion::kIntersect_Op);
40            canvas.saveLayerAlpha(100, 100, 300, 300, 128, SaveFlags::ClipToLayer);
41            canvas.drawColor(Color::Blue_500, SkXfermode::kSrcOver_Mode);
42            canvas.restore();
43            canvas.restore();
44
45            // single unclipped saveLayer
46            canvas.save(SaveFlags::MatrixClip);
47            canvas.translate(0, 400);
48            canvas.saveLayerAlpha(100, 100, 300, 300, 128, SaveFlags::Flags(0)); // unclipped
49            SkPaint paint;
50            paint.setAntiAlias(true);
51            paint.setColor(Color::Green_700);
52            canvas.drawCircle(200, 200, 200, paint);
53            canvas.restore();
54            canvas.restore();
55        });
56
57        canvas.drawRenderNode(card.get());
58    }
59    void doFrame(int frameNr) override {
60        int curFrame = frameNr % 150;
61        card->mutateStagingProperties().setTranslationX(curFrame);
62        card->mutateStagingProperties().setTranslationY(curFrame);
63        card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
64    }
65};
66