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#pragma once
18
19#include "TestScene.h"
20
21#include <SkBitmap.h>
22#include <string>
23
24namespace android {
25namespace uirenderer {
26namespace test {
27
28class BitmapAllocationTestUtils {
29public:
30    static sk_sp<Bitmap> allocateHeapBitmap(int width, int height,
31            SkColorType colorType, std::function<void(SkBitmap& bitmap)> setup) {
32         sk_sp<Bitmap> bitmap = TestUtils::createBitmap(width, height, colorType);
33         SkBitmap skBitmap;
34         bitmap->getSkBitmap(&skBitmap);
35         setup(skBitmap);
36         return bitmap;
37    }
38
39    static sk_sp<Bitmap> allocateHardwareBitmap(int width, int height,
40            SkColorType colorType, std::function<void(SkBitmap& bitmap)> setup) {
41        SkBitmap skBitmap;
42        SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
43        skBitmap.setInfo(info);
44        sk_sp<Bitmap> heapBitmap(Bitmap::allocateHeapBitmap(&skBitmap));
45        setup(skBitmap);
46        return Bitmap::allocateHardwareBitmap(skBitmap);
47    }
48
49    typedef sk_sp<Bitmap> (*BitmapAllocator) (int, int, SkColorType,
50            std::function<void(SkBitmap& bitmap)> setup);
51
52    template <class T, BitmapAllocator allocator>
53    static test::TestScene* createBitmapAllocationScene(const TestScene::Options&) {
54        return new T(allocator);
55    }
56
57    template <class BaseScene>
58    static bool registerBitmapAllocationScene(std::string name, std::string  description) {
59        TestScene::registerScene({
60            name + "GlTex",
61            description + " (GlTex version).",
62            createBitmapAllocationScene<BaseScene, &allocateHeapBitmap>
63        });
64
65        TestScene::registerScene({
66            name + "EglImage",
67            description + " (EglImage version).",
68            createBitmapAllocationScene<BaseScene, &allocateHardwareBitmap>
69        });
70        return true;
71    }
72};
73
74} // namespace test
75} // namespace uirenderer
76} // namespace android
77