1010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev/*
2010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Copyright (C) 2017 The Android Open Source Project
3010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
4010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Licensed under the Apache License, Version 2.0 (the "License");
5010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * you may not use this file except in compliance with the License.
6010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * You may obtain a copy of the License at
7010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
8010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *      http://www.apache.org/licenses/LICENSE-2.0
9010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev *
10010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * Unless required by applicable law or agreed to in writing, software
11010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * distributed under the License is distributed on an "AS IS" BASIS,
12010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * See the License for the specific language governing permissions and
14010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev * limitations under the License.
15010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev */
16010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
17010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev#include "TestSceneBase.h"
18010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev#include <string>
19010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev#include <hwui/Paint.h>
20010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev#include <minikin/Layout.h>
21010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
22010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievclass SaveLayer2Animation;
23010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
24010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievstatic TestScene::Registrar _SaveLayer(TestScene::Info{
25010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    "savelayer2",
26010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    "Interleaving 20 drawText/drawRect ops with saveLayer"
27010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    "Tests the clipped saveLayer performance and FBO switching overhead.",
28010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    TestScene::simpleCreateScene<SaveLayer2Animation>
29010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev});
30010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
31010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievclass SaveLayer2Animation : public TestScene {
32010b6a58c7d19ba2ef68295819fce00b37595decStan Ilievpublic:
33010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    Paint mBluePaint;
34010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    Paint mGreenPaint;
35010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
36010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    void createContent(int width, int height, Canvas& canvas) override {
37010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        canvas.drawColor(SkColorSetARGB(255, 255, 0, 0), SkBlendMode::kSrcOver);
38010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        SkIRect bounds = SkIRect::MakeWH(width, height);
39010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        int regions = 20;
40010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        int smallRectHeight = (bounds.height()/regions);
41010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        int padding = smallRectHeight / 4;
42010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        int top = bounds.fTop;
43010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
44010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        mBluePaint.setColor(SkColorSetARGB(255, 0, 0, 255));
45010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        mBluePaint.setTextSize(padding);
46010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        mGreenPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
47010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        mGreenPaint.setTextSize(padding);
48010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
49010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        //interleave drawText and drawRect with saveLayer ops
50010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        for (int i = 0; i < regions; i++, top += smallRectHeight) {
51010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.saveLayer(bounds.fLeft, top, bounds.fRight, top + padding,
52010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev                    &mBluePaint, SaveFlags::ClipToLayer | SaveFlags::MatrixClip);
53010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.drawColor(SkColorSetARGB(255, 255, 255, 0), SkBlendMode::kSrcOver);
54010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            std::string stri = std::to_string(i);
55010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            std::string offscreen = "offscreen line " + stri;
56010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            std::unique_ptr<uint16_t[]> offtext = TestUtils::asciiToUtf16(offscreen.c_str());
57010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.drawText(offtext.get(), 0, offscreen.length(), offscreen.length(),
58010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev                    bounds.fLeft, top + padding, minikin::kBidi_Force_LTR, mBluePaint, nullptr);
59010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.restore();
60010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev
61010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.drawRect(bounds.fLeft, top + padding, bounds.fRight,
62010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev                    top + smallRectHeight - padding, mBluePaint);
63010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            std::string onscreen = "onscreen line " + stri;
64010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            std::unique_ptr<uint16_t[]> ontext = TestUtils::asciiToUtf16(onscreen.c_str());
65010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev            canvas.drawText(ontext.get(), 0, onscreen.length(), onscreen.length(), bounds.fLeft,
66010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev                    top + smallRectHeight - padding, minikin::kBidi_Force_LTR, mGreenPaint,
67010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev                    nullptr);
68010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev        }
69010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    }
70010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    void doFrame(int frameNr) override {
71010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev    }
72010b6a58c7d19ba2ef68295819fce00b37595decStan Iliev};
73