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 
19 #include <SkBlendMode.h>
20 
21 class SaveLayerAnimation;
22 
23 static TestScene::Registrar _SaveLayer(TestScene::Info{
24         "savelayer",
25         "A nested pair of clipped saveLayer operations. "
26         "Tests the clipped saveLayer codepath. Draws content into offscreen buffers and back "
27         "again.",
28         TestScene::simpleCreateScene<SaveLayerAnimation>});
29 
30 class SaveLayerAnimation : public TestScene {
31 public:
32     sp<RenderNode> card;
createContent(int width,int height,Canvas & canvas)33     void createContent(int width, int height, Canvas& canvas) override {
34         canvas.drawColor(Color::White, SkBlendMode::kSrcOver);  // background
35 
36         card = TestUtils::createNode(0, 0, 400, 800, [](RenderProperties& props, Canvas& canvas) {
37             // nested clipped saveLayers
38             canvas.saveLayerAlpha(0, 0, 400, 400, 200);
39             canvas.drawColor(Color::Green_700, SkBlendMode::kSrcOver);
40             canvas.clipRect(50, 50, 350, 350, SkClipOp::kIntersect);
41             canvas.saveLayerAlpha(100, 100, 300, 300, 128);
42             canvas.drawColor(Color::Blue_500, SkBlendMode::kSrcOver);
43             canvas.restore();
44             canvas.restore();
45 
46             // single unclipped saveLayer
47             canvas.save(SaveFlags::MatrixClip);
48             canvas.translate(0, 400);
49             int unclippedSaveLayer = canvas.saveUnclippedLayer(100, 100, 300, 300);
50             Paint paint;
51             paint.setAntiAlias(true);
52             paint.setColor(Color::Green_700);
53             canvas.drawCircle(200, 200, 200, paint);
54             Paint alphaPaint;
55             alphaPaint.setAlpha(128);
56             canvas.restoreUnclippedLayer(unclippedSaveLayer, alphaPaint);
57             canvas.restore();
58         });
59 
60         canvas.drawRenderNode(card.get());
61     }
doFrame(int frameNr)62     void doFrame(int frameNr) override {
63         int curFrame = frameNr % 150;
64         card->mutateStagingProperties().setTranslationX(curFrame);
65         card->mutateStagingProperties().setTranslationY(curFrame);
66         card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
67     }
68 };
69