blob: b8b3f0aa5229f85e23a945634ff6c81f5031e864 [file] [log] [blame]
Nolan Scobie2163e412022-10-24 19:57:43 -04001/*
2 * Copyright (C) 2022 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 <gtest/gtest.h>
18
19#include "AutoBackendTextureRelease.h"
20#include "tests/common/TestUtils.h"
21
22using namespace android;
23using namespace android::uirenderer;
24
25AHardwareBuffer* allocHardwareBuffer() {
26 AHardwareBuffer* buffer;
27 AHardwareBuffer_Desc desc = {
28 .width = 16,
29 .height = 16,
30 .layers = 1,
31 .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
John Reck0c3e7f62022-12-21 16:40:45 -050032 .usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
Nolan Scobie2163e412022-10-24 19:57:43 -040033 };
34 constexpr int kSucceeded = 0;
35 int status = AHardwareBuffer_allocate(&desc, &buffer);
36 EXPECT_EQ(kSucceeded, status);
37 return buffer;
38}
39
40// Expands to AutoBackendTextureRelease_makeImage_invalid_RenderThreadTest,
41// set as friend in AutoBackendTextureRelease.h
42RENDERTHREAD_TEST(AutoBackendTextureRelease, makeImage_invalid) {
43 AHardwareBuffer* buffer = allocHardwareBuffer();
44 AutoBackendTextureRelease* textureRelease =
45 new AutoBackendTextureRelease(renderThread.getGrContext(), buffer);
46
47 EXPECT_EQ(1, TestUtils::getUsageCount(textureRelease));
48
Kevin Lubickdd8b2ea2023-03-17 19:55:58 +000049 // SkImages::BorrowTextureFrom should fail if given null GrDirectContext.
Nolan Scobie2163e412022-10-24 19:57:43 -040050 textureRelease->makeImage(buffer, HAL_DATASPACE_UNKNOWN, /*context = */ nullptr);
51
52 EXPECT_EQ(1, TestUtils::getUsageCount(textureRelease));
53
54 textureRelease->unref(true);
55 AHardwareBuffer_release(buffer);
56}
57
58// Expands to AutoBackendTextureRelease_makeImage_valid_RenderThreadTest,
59// set as friend in AutoBackendTextureRelease.h
60RENDERTHREAD_TEST(AutoBackendTextureRelease, makeImage_valid) {
61 AHardwareBuffer* buffer = allocHardwareBuffer();
62 AutoBackendTextureRelease* textureRelease =
63 new AutoBackendTextureRelease(renderThread.getGrContext(), buffer);
64
65 EXPECT_EQ(1, TestUtils::getUsageCount(textureRelease));
66
67 textureRelease->makeImage(buffer, HAL_DATASPACE_UNKNOWN, renderThread.getGrContext());
68
69 EXPECT_EQ(2, TestUtils::getUsageCount(textureRelease));
70
71 textureRelease->unref(true);
72 AHardwareBuffer_release(buffer);
73}