1 /*
2  * Copyright (C) 2017 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 <hwui/Paint.h>
18 #include <minikin/Layout.h>
19 #include <SkBlendMode.h>
20 #include <string>
21 #include "TestSceneBase.h"
22 
23 class SaveLayer2Animation;
24 
25 static TestScene::Registrar _SaveLayer(TestScene::Info{
26         "savelayer2",
27         "Interleaving 20 drawText/drawRect ops with saveLayer"
28         "Tests the clipped saveLayer performance and FBO switching overhead.",
29         TestScene::simpleCreateScene<SaveLayer2Animation>});
30 
31 class SaveLayer2Animation : public TestScene {
32 public:
33     Paint mBluePaint;
34     Paint mGreenPaint;
35 
createContent(int width,int height,Canvas & canvas)36     void createContent(int width, int height, Canvas& canvas) override {
37         canvas.drawColor(SkColorSetARGB(255, 255, 0, 0), SkBlendMode::kSrcOver);
38         SkIRect bounds = SkIRect::MakeWH(width, height);
39         int regions = 20;
40         int smallRectHeight = (bounds.height() / regions);
41         int padding = smallRectHeight / 4;
42         int top = bounds.fTop;
43 
44         mBluePaint.setColor(SkColorSetARGB(255, 0, 0, 255));
45         mBluePaint.getSkFont().setSize(padding);
46         mGreenPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
47         mGreenPaint.getSkFont().setSize(padding);
48 
49         // interleave drawText and drawRect with saveLayer ops
50         for (int i = 0; i < regions; i++, top += smallRectHeight) {
51             canvas.saveLayer(bounds.fLeft, top, bounds.fRight, top + padding, &mBluePaint);
52             canvas.drawColor(SkColorSetARGB(255, 255, 255, 0), SkBlendMode::kSrcOver);
53             std::string stri = std::to_string(i);
54             std::string offscreen = "offscreen line " + stri;
55             TestUtils::drawUtf8ToCanvas(&canvas, offscreen.c_str(), mBluePaint, bounds.fLeft,
56                     top + padding);
57             canvas.restore();
58 
59             canvas.drawRect(bounds.fLeft, top + padding, bounds.fRight,
60                             top + smallRectHeight - padding, mBluePaint);
61             std::string onscreen = "onscreen line " + stri;
62             TestUtils::drawUtf8ToCanvas(&canvas, onscreen.c_str(), mGreenPaint, bounds.fLeft,
63                     top + smallRectHeight - padding);
64         }
65     }
doFrame(int frameNr)66     void doFrame(int frameNr) override {}
67 };
68