Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 17 | #include <cstdint> |
| 18 | #include "android/hardware_buffer.h" |
| 19 | #include "gmock/gmock.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 21 | #include "system/graphics.h" |
| 22 | #include "ui/GraphicBuffer.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 23 | #include "util/EglDisplayContext.h" |
| 24 | #include "util/EglProgram.h" |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 25 | #include "util/EglSurfaceTexture.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 26 | #include "util/EglUtil.h" |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 27 | #include "utils/Errors.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace companion { |
| 31 | namespace virtualcamera { |
| 32 | namespace { |
| 33 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 34 | using ::testing::Eq; |
| 35 | using ::testing::NotNull; |
| 36 | |
| 37 | constexpr int kWidth = 64; |
| 38 | constexpr int kHeight = 64; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 39 | constexpr char kGlExtYuvTarget[] = "GL_EXT_YUV_target"; |
| 40 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 41 | uint8_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 | |
| 46 | uint8_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 | |
| 51 | uint8_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 Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 56 | TEST(EglDisplayContextTest, SuccessfulInitialization) { |
| 57 | EglDisplayContext displayContext; |
| 58 | |
| 59 | EXPECT_TRUE(displayContext.isInitialized()); |
| 60 | } |
| 61 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 62 | class EglTest : public ::testing::Test { |
| 63 | public: |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 64 | void SetUp() override { |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 65 | ASSERT_TRUE(mEglDisplayContext.isInitialized()); |
| 66 | ASSERT_TRUE(mEglDisplayContext.makeCurrent()); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 69 | private: |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 70 | EglDisplayContext mEglDisplayContext; |
| 71 | }; |
| 72 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 73 | TEST_F(EglTest, EglTestPatternProgramSuccessfulInit) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 74 | EglTestPatternProgram eglTestPatternProgram; |
| 75 | |
| 76 | // Verify the shaders compiled and linked successfully. |
| 77 | EXPECT_TRUE(eglTestPatternProgram.isInitialized()); |
| 78 | } |
| 79 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 80 | TEST_F(EglTest, EglTextureProgramSuccessfulInit) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 81 | if (!isGlExtensionSupported(kGlExtYuvTarget)) { |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 82 | GTEST_SKIP() << "Skipping test because of missing required GL extension " << kGlExtYuvTarget; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | EglTextureProgram eglTextureProgram; |
| 86 | |
| 87 | // Verify the shaders compiled and linked successfully. |
| 88 | EXPECT_TRUE(eglTextureProgram.isInitialized()); |
| 89 | } |
| 90 | |
Jan Sebechlebsky | 9677240 | 2023-11-23 15:56:58 +0100 | [diff] [blame] | 91 | TEST_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 Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 120 | } // namespace |
| 121 | } // namespace virtualcamera |
| 122 | } // namespace companion |
| 123 | } // namespace android |