1 /*
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
22 using namespace android;
23 using namespace android::uirenderer;
24
allocHardwareBuffer()25 AHardwareBuffer* allocHardwareBuffer() {
26 AHardwareBuffer* buffer;
27 AHardwareBuffer_Desc desc = {
28 .width = 16,
29 .height = 16,
30 .layers = 1,
31 .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
32 .usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
33 };
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
RENDERTHREAD_TEST(AutoBackendTextureRelease,makeImage_invalid)42 RENDERTHREAD_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
49 // SkImages::BorrowTextureFrom should fail if given null GrDirectContext.
50 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
RENDERTHREAD_TEST(AutoBackendTextureRelease,makeImage_valid)60 RENDERTHREAD_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 }
74