blob: bbdd98e0c0d18da6ccaa5efd1b5be796d5060ce0 [file] [log] [blame]
sergeyv694d4992016-10-27 10:23:13 -07001/*
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:
John Reck1bcacfd2017-11-03 10:12:19 -070030 static sk_sp<Bitmap> allocateHeapBitmap(int width, int height, SkColorType colorType,
31 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;
sergeyv694d4992016-10-27 10:23:13 -070037 }
38
John Reck1bcacfd2017-11-03 10:12:19 -070039 static sk_sp<Bitmap> allocateHardwareBitmap(int width, int height, SkColorType colorType,
40 std::function<void(SkBitmap& bitmap)> setup) {
sergeyv694d4992016-10-27 10:23:13 -070041 SkBitmap skBitmap;
sergeyv91d63542016-12-14 16:34:55 -080042 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
43 skBitmap.setInfo(info);
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -040044 sk_sp<Bitmap> heapBitmap(Bitmap::allocateHeapBitmap(&skBitmap));
sergeyv694d4992016-10-27 10:23:13 -070045 setup(skBitmap);
46 return Bitmap::allocateHardwareBitmap(skBitmap);
47 }
48
John Reck1bcacfd2017-11-03 10:12:19 -070049 typedef sk_sp<Bitmap> (*BitmapAllocator)(int, int, SkColorType,
50 std::function<void(SkBitmap& bitmap)> setup);
sergeyv694d4992016-10-27 10:23:13 -070051
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>
John Reck1bcacfd2017-11-03 10:12:19 -070058 static bool registerBitmapAllocationScene(std::string name, std::string description) {
John Reck2ed52402022-09-22 16:02:23 -040059 TestScene::registerScene({name + "Texture", description + " (Texture version).",
John Reck1bcacfd2017-11-03 10:12:19 -070060 createBitmapAllocationScene<BaseScene, &allocateHeapBitmap>});
sergeyv694d4992016-10-27 10:23:13 -070061
John Reck2ed52402022-09-22 16:02:23 -040062 TestScene::registerScene({name + "HardwareBuffer",
63 description + " (HardwareBuffer version).",
John Reck1bcacfd2017-11-03 10:12:19 -070064 createBitmapAllocationScene<BaseScene, &allocateHardwareBitmap>});
sergeyv694d4992016-10-27 10:23:13 -070065 return true;
66 }
67};
68
John Reck1bcacfd2017-11-03 10:12:19 -070069} // namespace test
70} // namespace uirenderer
71} // namespace android