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 "TestSceneBase.h"
18#include <string>
19#include <hwui/Paint.h>
20#include <minikin/Layout.h>
21
22class SaveLayer2Animation;
23
24static TestScene::Registrar _SaveLayer(TestScene::Info{
25    "savelayer2",
26    "Interleaving 20 drawText/drawRect ops with saveLayer"
27    "Tests the clipped saveLayer performance and FBO switching overhead.",
28    TestScene::simpleCreateScene<SaveLayer2Animation>
29});
30
31class SaveLayer2Animation : public TestScene {
32public:
33    Paint mBluePaint;
34    Paint mGreenPaint;
35
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.setTextSize(padding);
46        mGreenPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
47        mGreenPaint.setTextSize(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,
52                    &mBluePaint, SaveFlags::ClipToLayer | SaveFlags::MatrixClip);
53            canvas.drawColor(SkColorSetARGB(255, 255, 255, 0), SkBlendMode::kSrcOver);
54            std::string stri = std::to_string(i);
55            std::string offscreen = "offscreen line " + stri;
56            std::unique_ptr<uint16_t[]> offtext = TestUtils::asciiToUtf16(offscreen.c_str());
57            canvas.drawText(offtext.get(), 0, offscreen.length(), offscreen.length(),
58                    bounds.fLeft, top + padding, minikin::kBidi_Force_LTR, mBluePaint, nullptr);
59            canvas.restore();
60
61            canvas.drawRect(bounds.fLeft, top + padding, bounds.fRight,
62                    top + smallRectHeight - padding, mBluePaint);
63            std::string onscreen = "onscreen line " + stri;
64            std::unique_ptr<uint16_t[]> ontext = TestUtils::asciiToUtf16(onscreen.c_str());
65            canvas.drawText(ontext.get(), 0, onscreen.length(), onscreen.length(), bounds.fLeft,
66                    top + smallRectHeight - padding, minikin::kBidi_Force_LTR, mGreenPaint,
67                    nullptr);
68        }
69    }
70    void doFrame(int frameNr) override {
71    }
72};
73