blob: d387ebff6b48670452d23a1d9f20e9f5f670df59 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
2 * Copyright (C) 2023 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
Jan Sebechlebsky96772402023-11-23 15:56:58 +010017#include <cstdint>
18#include "android/hardware_buffer.h"
19#include "gmock/gmock.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010020#include "gtest/gtest.h"
Jan Sebechlebsky96772402023-11-23 15:56:58 +010021#include "system/graphics.h"
22#include "ui/GraphicBuffer.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010023#include "util/EglDisplayContext.h"
24#include "util/EglProgram.h"
Jan Sebechlebsky96772402023-11-23 15:56:58 +010025#include "util/EglSurfaceTexture.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010026#include "util/EglUtil.h"
Jan Sebechlebsky96772402023-11-23 15:56:58 +010027#include "utils/Errors.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010028
29namespace android {
30namespace companion {
31namespace virtualcamera {
32namespace {
33
Jan Sebechlebsky96772402023-11-23 15:56:58 +010034using ::testing::Eq;
35using ::testing::NotNull;
36
37constexpr int kWidth = 64;
38constexpr int kHeight = 64;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010039constexpr char kGlExtYuvTarget[] = "GL_EXT_YUV_target";
40
Jan Sebechlebsky96772402023-11-23 15:56:58 +010041uint8_t getY(const android_ycbcr& ycbcr, const int x, const int y) {
42 uint8_t* yPtr = reinterpret_cast<uint8_t*>(ycbcr.y);
43 return *(yPtr + ycbcr.ystride * y + x);
44}
45
46uint8_t getCb(const android_ycbcr& ycbcr, const int x, const int y) {
47 uint8_t* cbPtr = reinterpret_cast<uint8_t*>(ycbcr.cb);
48 return *(cbPtr + ycbcr.cstride * (y / 2) + (x / 2) * ycbcr.chroma_step);
49}
50
51uint8_t getCr(const android_ycbcr& ycbcr, const int x, const int y) {
52 uint8_t* crPtr = reinterpret_cast<uint8_t*>(ycbcr.cr);
53 return *(crPtr + ycbcr.cstride * (y / 2) + (x / 2) * ycbcr.chroma_step);
54}
55
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010056TEST(EglDisplayContextTest, SuccessfulInitialization) {
57 EglDisplayContext displayContext;
58
59 EXPECT_TRUE(displayContext.isInitialized());
60}
61
Jan Sebechlebsky96772402023-11-23 15:56:58 +010062class EglTest : public ::testing::Test {
63public:
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010064 void SetUp() override {
Jan Sebechlebsky96772402023-11-23 15:56:58 +010065 ASSERT_TRUE(mEglDisplayContext.isInitialized());
66 ASSERT_TRUE(mEglDisplayContext.makeCurrent());
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010067 }
68
Jan Sebechlebsky96772402023-11-23 15:56:58 +010069private:
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010070 EglDisplayContext mEglDisplayContext;
71};
72
Jan Sebechlebsky96772402023-11-23 15:56:58 +010073TEST_F(EglTest, EglTestPatternProgramSuccessfulInit) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010074 EglTestPatternProgram eglTestPatternProgram;
75
76 // Verify the shaders compiled and linked successfully.
77 EXPECT_TRUE(eglTestPatternProgram.isInitialized());
78}
79
Jan Sebechlebsky96772402023-11-23 15:56:58 +010080TEST_F(EglTest, EglTextureProgramSuccessfulInit) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010081 if (!isGlExtensionSupported(kGlExtYuvTarget)) {
Jan Sebechlebsky96772402023-11-23 15:56:58 +010082 GTEST_SKIP() << "Skipping test because of missing required GL extension " << kGlExtYuvTarget;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010083 }
84
85 EglTextureProgram eglTextureProgram;
86
87 // Verify the shaders compiled and linked successfully.
88 EXPECT_TRUE(eglTextureProgram.isInitialized());
89}
90
Jan Sebechlebsky96772402023-11-23 15:56:58 +010091TEST_F(EglTest, EglSurfaceTextureBlackAfterInit) {
92 if (!isGlExtensionSupported(kGlExtYuvTarget)) {
93 GTEST_SKIP() << "Skipping test because of missing required GL extension " << kGlExtYuvTarget;
94 }
95
96 EglSurfaceTexture surfaceTexture(kWidth, kHeight);
97 surfaceTexture.updateTexture();
98 sp<GraphicBuffer> buffer = surfaceTexture.getCurrentBuffer();
99
100 ASSERT_THAT(buffer, NotNull());
101 const int width = buffer->getWidth();
102 const int height = buffer->getHeight();
103 ASSERT_THAT(width, Eq(kWidth));
104 ASSERT_THAT(height, Eq(kHeight));
105
106 android_ycbcr ycbcr;
107 status_t ret = buffer->lockYCbCr(AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, &ycbcr);
108 ASSERT_THAT(ret, Eq(NO_ERROR));
109 for (int i = 0; i < width; ++i) {
110 for (int j = 0; j < height; ++j) {
111 EXPECT_THAT(getY(ycbcr, i, j), Eq(0x00));
112 EXPECT_THAT(getCb(ycbcr, i, j), Eq(0x7f));
113 EXPECT_THAT(getCr(ycbcr, i, j), Eq(0x7f));
114 }
115 }
116
117 buffer->unlock();
118}
119
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100120} // namespace
121} // namespace virtualcamera
122} // namespace companion
123} // namespace android