1/*
2 * Copyright (C) 2016 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 "tests/common/BitmapAllocationTestUtils.h"
19#include "utils/Color.h"
20
21#include <SkBitmap.h>
22
23using namespace android;
24using namespace android::uirenderer;
25
26class BitmapFillrate;
27
28static bool _BitmapFillrate(
29        BitmapAllocationTestUtils::registerBitmapAllocationScene<BitmapFillrate>(
30                "bitmapFillrate", "Draws multiple large half transparent bitmaps."));
31
32class BitmapFillrate : public TestScene {
33public:
34    BitmapFillrate(BitmapAllocationTestUtils::BitmapAllocator allocator)
35        : TestScene()
36        , mAllocator(allocator) { }
37
38    void createContent(int width, int height, Canvas& canvas) override {
39        canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
40        createNode(canvas, 0x909C27B0, 0, 0, width, height);
41        createNode(canvas, 0xA0CDDC39, width / 3, height / 3, width, height);
42        createNode(canvas, 0x90009688, width / 3, 0, width, height);
43        createNode(canvas, 0xA0FF5722, 0, height / 3, width, height);
44        createNode(canvas, 0x9000796B, width / 6, height/6, width, height);
45        createNode(canvas, 0xA0FFC107, width / 6, 0, width, height);
46    }
47
48    void doFrame(int frameNr) override {
49        for (size_t ci = 0; ci < mNodes.size(); ci++) {
50            mNodes[ci]->mutateStagingProperties().setTranslationX(frameNr % 200);
51            mNodes[ci]->mutateStagingProperties().setTranslationY(frameNr % 200);
52            mNodes[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
53        }
54    }
55private:
56    void createNode(Canvas& canvas, SkColor color, int left, int top,
57            int width, int height) {
58        int itemWidth = 2 * width / 3;
59        int itemHeight = 2 * height / 3;
60        auto card = TestUtils::createNode(left, top, left + itemWidth , top + itemHeight,
61                [this, itemWidth, itemHeight, color](RenderProperties& props, Canvas& canvas) {
62            sk_sp<Bitmap> bitmap = mAllocator(itemWidth, itemHeight, kRGBA_8888_SkColorType,
63                    [color](SkBitmap& skBitmap) {
64                 skBitmap.eraseColor(color);
65            });
66            canvas.drawBitmap(*bitmap, 0, 0, nullptr);
67        });
68        canvas.drawRenderNode(card.get());
69        mNodes.push_back(card);
70    }
71
72    BitmapAllocationTestUtils::BitmapAllocator mAllocator;
73    std::vector< sp<RenderNode> > mNodes;
74};