sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 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 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | namespace test { |
| 27 | |
| 28 | class BitmapAllocationTestUtils { |
| 29 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | 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; |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 37 | } |
| 38 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | static sk_sp<Bitmap> allocateHardwareBitmap(int width, int height, SkColorType colorType, |
| 40 | std::function<void(SkBitmap& bitmap)> setup) { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 41 | SkBitmap skBitmap; |
sergeyv | 91d6354 | 2016-12-14 16:34:55 -0800 | [diff] [blame] | 42 | SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType); |
| 43 | skBitmap.setInfo(info); |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 44 | sk_sp<Bitmap> heapBitmap(Bitmap::allocateHeapBitmap(&skBitmap)); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 45 | setup(skBitmap); |
| 46 | return Bitmap::allocateHardwareBitmap(skBitmap); |
| 47 | } |
| 48 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | typedef sk_sp<Bitmap> (*BitmapAllocator)(int, int, SkColorType, |
| 50 | std::function<void(SkBitmap& bitmap)> setup); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 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> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 58 | static bool registerBitmapAllocationScene(std::string name, std::string description) { |
John Reck | 2ed5240 | 2022-09-22 16:02:23 -0400 | [diff] [blame^] | 59 | TestScene::registerScene({name + "Texture", description + " (Texture version).", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | createBitmapAllocationScene<BaseScene, &allocateHeapBitmap>}); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 61 | |
John Reck | 2ed5240 | 2022-09-22 16:02:23 -0400 | [diff] [blame^] | 62 | TestScene::registerScene({name + "HardwareBuffer", |
| 63 | description + " (HardwareBuffer version).", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | createBitmapAllocationScene<BaseScene, &allocateHardwareBitmap>}); |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | }; |
| 68 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | } // namespace test |
| 70 | } // namespace uirenderer |
| 71 | } // namespace android |