14d949f91Sopenharmony_ci/*
24d949f91Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
34d949f91Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44d949f91Sopenharmony_ci * you may not use this file except in compliance with the License.
54d949f91Sopenharmony_ci * You may obtain a copy of the License at
64d949f91Sopenharmony_ci *
74d949f91Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84d949f91Sopenharmony_ci *
94d949f91Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104d949f91Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114d949f91Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124d949f91Sopenharmony_ci * See the License for the specific language governing permissions and
134d949f91Sopenharmony_ci * limitations under the License.
144d949f91Sopenharmony_ci */
154d949f91Sopenharmony_ci
164d949f91Sopenharmony_ci#include "gtest/gtest.h"
174d949f91Sopenharmony_ci
184d949f91Sopenharmony_ci#include "render_environment.h"
194d949f91Sopenharmony_ci#include "effect_context.h"
204d949f91Sopenharmony_ci
214d949f91Sopenharmony_ciusing namespace testing::ext;
224d949f91Sopenharmony_ci
234d949f91Sopenharmony_cinamespace OHOS {
244d949f91Sopenharmony_cinamespace Media {
254d949f91Sopenharmony_cinamespace Effect {
264d949f91Sopenharmony_cinamespace Test {
274d949f91Sopenharmony_ci
284d949f91Sopenharmony_ciconstexpr uint32_t WIDTH = 960;
294d949f91Sopenharmony_ciconstexpr uint32_t HEIGHT = 1280;
304d949f91Sopenharmony_ciconstexpr IEffectFormat FORMATE_TYPE = IEffectFormat::RGBA8888;
314d949f91Sopenharmony_ciconstexpr uint32_t ROW_STRIDE = WIDTH * 4;
324d949f91Sopenharmony_ciconstexpr uint32_t LEN = ROW_STRIDE * HEIGHT;
334d949f91Sopenharmony_ciconstexpr uint32_t EXTRA_LEN = 10;
344d949f91Sopenharmony_ci
354d949f91Sopenharmony_ciclass TestRenderEnvironment : public testing::Test {
364d949f91Sopenharmony_cipublic:
374d949f91Sopenharmony_ci    TestRenderEnvironment() = default;
384d949f91Sopenharmony_ci
394d949f91Sopenharmony_ci    ~TestRenderEnvironment() override = default;
404d949f91Sopenharmony_ci    static void SetUpTestCase() {}
414d949f91Sopenharmony_ci
424d949f91Sopenharmony_ci    static void TearDownTestCase() {}
434d949f91Sopenharmony_ci
444d949f91Sopenharmony_ci    void SetUp() override
454d949f91Sopenharmony_ci    {
464d949f91Sopenharmony_ci        renderEnvironment = std::make_shared<RenderEnvironment>();
474d949f91Sopenharmony_ci        renderEnvironment->Init();
484d949f91Sopenharmony_ci        renderEnvironment->Prepare();
494d949f91Sopenharmony_ci
504d949f91Sopenharmony_ci        std::shared_ptr<BufferInfo> bufferInfo = std::make_unique<BufferInfo>();
514d949f91Sopenharmony_ci        bufferInfo->width_ = WIDTH;
524d949f91Sopenharmony_ci        bufferInfo->height_ = HEIGHT;
534d949f91Sopenharmony_ci        bufferInfo->rowStride_ = ROW_STRIDE;
544d949f91Sopenharmony_ci        bufferInfo->len_ = LEN;
554d949f91Sopenharmony_ci        bufferInfo->formatType_ = FORMATE_TYPE;
564d949f91Sopenharmony_ci        void *addr = malloc(bufferInfo->len_);
574d949f91Sopenharmony_ci
584d949f91Sopenharmony_ci        std::shared_ptr<ExtraInfo> extraInfo = std::make_unique<ExtraInfo>();
594d949f91Sopenharmony_ci        extraInfo->dataType = DataType::PIXEL_MAP;
604d949f91Sopenharmony_ci        extraInfo->bufferType = BufferType::HEAP_MEMORY;
614d949f91Sopenharmony_ci        extraInfo->pixelMap = nullptr;
624d949f91Sopenharmony_ci        extraInfo->surfaceBuffer = nullptr;
634d949f91Sopenharmony_ci        effectBuffer = std::make_shared<EffectBuffer>(bufferInfo, addr, extraInfo);
644d949f91Sopenharmony_ci
654d949f91Sopenharmony_ci        free(addr);
664d949f91Sopenharmony_ci        addr = nullptr;
674d949f91Sopenharmony_ci    }
684d949f91Sopenharmony_ci
694d949f91Sopenharmony_ci    void TearDown() override
704d949f91Sopenharmony_ci    {
714d949f91Sopenharmony_ci        effectBuffer = nullptr;
724d949f91Sopenharmony_ci        if (renderEnvironment == nullptr) {
734d949f91Sopenharmony_ci            return;
744d949f91Sopenharmony_ci        }
754d949f91Sopenharmony_ci        renderEnvironment->ReleaseParam();
764d949f91Sopenharmony_ci        renderEnvironment->Release();
774d949f91Sopenharmony_ci    }
784d949f91Sopenharmony_ci
794d949f91Sopenharmony_ci    std::shared_ptr<EffectBuffer> effectBuffer;
804d949f91Sopenharmony_ci    std::shared_ptr<RenderEnvironment> renderEnvironment;
814d949f91Sopenharmony_ci};
824d949f91Sopenharmony_ci
834d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment001, TestSize.Level1)
844d949f91Sopenharmony_ci{
854d949f91Sopenharmony_ci    std::shared_ptr<EffectBuffer> output;
864d949f91Sopenharmony_ci    renderEnvironment->outType_ = DataType::NATIVE_WINDOW;
874d949f91Sopenharmony_ci    effectBuffer->bufferInfo_->width_ = 2 * WIDTH;
884d949f91Sopenharmony_ci    renderEnvironment->GenMainTex(effectBuffer, output);
894d949f91Sopenharmony_ci
904d949f91Sopenharmony_ci    effectBuffer->bufferInfo_->formatType_ = IEffectFormat::DEFAULT;
914d949f91Sopenharmony_ci    renderEnvironment->GenMainTex(effectBuffer, output);
924d949f91Sopenharmony_ci
934d949f91Sopenharmony_ci    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
944d949f91Sopenharmony_ci    EXPECT_NE(texptr, nullptr);
954d949f91Sopenharmony_ci    renderEnvironment->ConvertTextureToBuffer(texptr, effectBuffer.get());
964d949f91Sopenharmony_ci
974d949f91Sopenharmony_ci    GLenum internalFormat = GL_RG8;
984d949f91Sopenharmony_ci    size_t ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
994d949f91Sopenharmony_ci    EXPECT_EQ(ret, 0);
1004d949f91Sopenharmony_ci
1014d949f91Sopenharmony_ci    internalFormat = GL_R8;
1024d949f91Sopenharmony_ci    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
1034d949f91Sopenharmony_ci    EXPECT_EQ(ret, 1);
1044d949f91Sopenharmony_ci
1054d949f91Sopenharmony_ci    internalFormat = GL_RGB565;
1064d949f91Sopenharmony_ci    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
1074d949f91Sopenharmony_ci    EXPECT_EQ(ret, 2);
1084d949f91Sopenharmony_ci
1094d949f91Sopenharmony_ci    internalFormat = GL_RGBA4;
1104d949f91Sopenharmony_ci    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
1114d949f91Sopenharmony_ci    EXPECT_EQ(ret, 2);
1124d949f91Sopenharmony_ci
1134d949f91Sopenharmony_ci    internalFormat = GL_RGBA16F;
1144d949f91Sopenharmony_ci    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
1154d949f91Sopenharmony_ci    EXPECT_EQ(ret, 8);
1164d949f91Sopenharmony_ci}
1174d949f91Sopenharmony_ci
1184d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment002, TestSize.Level1)
1194d949f91Sopenharmony_ci{
1204d949f91Sopenharmony_ci    IEffectFormat format = IEffectFormat::YUVNV12;
1214d949f91Sopenharmony_ci    GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
1224d949f91Sopenharmony_ci    EXPECT_NE(tex, 0);
1234d949f91Sopenharmony_ci
1244d949f91Sopenharmony_ci    GraphicTransformType type = GraphicTransformType::GRAPHIC_ROTATE_NONE;
1254d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1264d949f91Sopenharmony_ci    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
1274d949f91Sopenharmony_ci    effectBuffer->tex = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
1284d949f91Sopenharmony_ci    EXPECT_NE(effectBuffer->tex, nullptr);
1294d949f91Sopenharmony_ci
1304d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_ROTATE_90;
1314d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1324d949f91Sopenharmony_ci    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
1334d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT90;
1344d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1354d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT90;
1364d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1374d949f91Sopenharmony_ci
1384d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_ROTATE_180;
1394d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1404d949f91Sopenharmony_ci    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
1414d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT180;
1424d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1434d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT180;
1444d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1454d949f91Sopenharmony_ci
1464d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_ROTATE_270;
1474d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1484d949f91Sopenharmony_ci    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
1494d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT270;
1504d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1514d949f91Sopenharmony_ci    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT270;
1524d949f91Sopenharmony_ci    renderEnvironment->DrawFrame(tex, type);
1534d949f91Sopenharmony_ci}
1544d949f91Sopenharmony_ci
1554d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment003, TestSize.Level1)
1564d949f91Sopenharmony_ci{
1574d949f91Sopenharmony_ci    std::shared_ptr<EffectBuffer> input;
1584d949f91Sopenharmony_ci    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
1594d949f91Sopenharmony_ci    EXPECT_NE(texptr, nullptr);
1604d949f91Sopenharmony_ci
1614d949f91Sopenharmony_ci    effectBuffer->tex = texptr;
1624d949f91Sopenharmony_ci    renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
1634d949f91Sopenharmony_ci    effectBuffer->extraInfo_->surfaceBuffer = nullptr;
1644d949f91Sopenharmony_ci    std::shared_ptr<EffectBuffer> buffer =
1654d949f91Sopenharmony_ci        renderEnvironment->ConvertBufferToTexture(effectBuffer.get());
1664d949f91Sopenharmony_ci    EXPECT_NE(buffer, nullptr);
1674d949f91Sopenharmony_ci
1684d949f91Sopenharmony_ci    bool result = renderEnvironment->IfNeedGenMainTex();
1694d949f91Sopenharmony_ci    EXPECT_EQ(result, true);
1704d949f91Sopenharmony_ci
1714d949f91Sopenharmony_ci    RenderContext *context = renderEnvironment->GetContext();
1724d949f91Sopenharmony_ci    EXPECT_NE(context, nullptr);
1734d949f91Sopenharmony_ci
1744d949f91Sopenharmony_ci    ResourceCache *ceCache = renderEnvironment->GetResourceCache();
1754d949f91Sopenharmony_ci    EXPECT_NE(ceCache, nullptr);
1764d949f91Sopenharmony_ci}
1774d949f91Sopenharmony_ci
1784d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment004, TestSize.Level1)
1794d949f91Sopenharmony_ci{
1804d949f91Sopenharmony_ci    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
1814d949f91Sopenharmony_ci    EXPECT_NE(texptr, nullptr);
1824d949f91Sopenharmony_ci
1834d949f91Sopenharmony_ci    IEffectFormat format = IEffectFormat::YUVNV21;
1844d949f91Sopenharmony_ci    GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
1854d949f91Sopenharmony_ci    EXPECT_NE(tex, 0);
1864d949f91Sopenharmony_ci    renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
1874d949f91Sopenharmony_ci
1884d949f91Sopenharmony_ci    format = IEffectFormat::YUVNV12;
1894d949f91Sopenharmony_ci    renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
1904d949f91Sopenharmony_ci}
1914d949f91Sopenharmony_ci
1924d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment005, TestSize.Level1)
1934d949f91Sopenharmony_ci{
1944d949f91Sopenharmony_ci    std::shared_ptr<RenderContext> renderContext = std::make_shared<RenderContext>();
1954d949f91Sopenharmony_ci    bool result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
1964d949f91Sopenharmony_ci    EXPECT_EQ(result, false);
1974d949f91Sopenharmony_ci
1984d949f91Sopenharmony_ci    result = renderContext->Init();
1994d949f91Sopenharmony_ci    EXPECT_EQ(result, true);
2004d949f91Sopenharmony_ci
2014d949f91Sopenharmony_ci    result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
2024d949f91Sopenharmony_ci    EXPECT_EQ(result, false);
2034d949f91Sopenharmony_ci}
2044d949f91Sopenharmony_ci
2054d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment006, TestSize.Level1)
2064d949f91Sopenharmony_ci{
2074d949f91Sopenharmony_ci    std::shared_ptr<EffectContext> effectContext = std::make_shared<EffectContext>();
2084d949f91Sopenharmony_ci    effectContext->renderEnvironment_ = renderEnvironment;
2094d949f91Sopenharmony_ci    effectContext->memoryManager_ = std::make_shared<EffectMemoryManager>();
2104d949f91Sopenharmony_ci    MemoryInfo memInfo = {
2114d949f91Sopenharmony_ci        .bufferInfo = {
2124d949f91Sopenharmony_ci            .width_ = effectBuffer->bufferInfo_->width_,
2134d949f91Sopenharmony_ci            .height_ = effectBuffer->bufferInfo_->height_,
2144d949f91Sopenharmony_ci            .len_ = effectBuffer->bufferInfo_->len_,
2154d949f91Sopenharmony_ci            .formatType_ = effectBuffer->bufferInfo_->formatType_,
2164d949f91Sopenharmony_ci        },
2174d949f91Sopenharmony_ci        .bufferType = BufferType::DMA_BUFFER,
2184d949f91Sopenharmony_ci    };
2194d949f91Sopenharmony_ci    MemoryData *memoryData = effectContext->memoryManager_->AllocMemory(effectBuffer->buffer_, memInfo);
2204d949f91Sopenharmony_ci    MemoryInfo &allocMemInfo = memoryData->memoryInfo;
2214d949f91Sopenharmony_ci    SurfaceBuffer *buffer = (allocMemInfo.bufferType == BufferType::DMA_BUFFER) ?
2224d949f91Sopenharmony_ci        static_cast<SurfaceBuffer *>(allocMemInfo.extra) : nullptr;
2234d949f91Sopenharmony_ci
2244d949f91Sopenharmony_ci    std::shared_ptr<EffectBuffer> input;
2254d949f91Sopenharmony_ci    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
2264d949f91Sopenharmony_ci    EXPECT_NE(texptr, nullptr);
2274d949f91Sopenharmony_ci
2284d949f91Sopenharmony_ci    effectBuffer->tex = texptr;
2294d949f91Sopenharmony_ci    effectBuffer->extraInfo_->surfaceBuffer = buffer;
2304d949f91Sopenharmony_ci    effectBuffer->bufferInfo_->formatType_ = IEffectFormat::YUVNV12;
2314d949f91Sopenharmony_ci
2324d949f91Sopenharmony_ci    renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
2334d949f91Sopenharmony_ci    renderEnvironment->DrawTexFromSurfaceBuffer(texptr, buffer);
2344d949f91Sopenharmony_ci
2354d949f91Sopenharmony_ci    IEffectFormat format = IEffectFormat::RGBA8888;
2364d949f91Sopenharmony_ci    renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
2374d949f91Sopenharmony_ci
2384d949f91Sopenharmony_ci    format = IEffectFormat::YUVNV12;
2394d949f91Sopenharmony_ci    renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
2404d949f91Sopenharmony_ci    renderEnvironment->DrawSurfaceBufferFromTex(texptr, buffer, format);
2414d949f91Sopenharmony_ci}
2424d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment007, TestSize.Level1) {
2434d949f91Sopenharmony_ci    std::string tag = "TestTag";
2444d949f91Sopenharmony_ci    std::shared_ptr<RenderSurface> renderSurface = std::make_shared<RenderSurface>(tag);
2454d949f91Sopenharmony_ci
2464d949f91Sopenharmony_ci    bool result = renderSurface->Init();
2474d949f91Sopenharmony_ci    EXPECT_EQ(result, true);
2484d949f91Sopenharmony_ci
2494d949f91Sopenharmony_ci    void *window = nullptr;
2504d949f91Sopenharmony_ci    result = renderSurface->Create(window);
2514d949f91Sopenharmony_ci    EXPECT_EQ(result, false);
2524d949f91Sopenharmony_ci
2534d949f91Sopenharmony_ci    result = renderSurface->Release();
2544d949f91Sopenharmony_ci    EXPECT_EQ(result, true);
2554d949f91Sopenharmony_ci
2564d949f91Sopenharmony_ci    RenderSurface::SurfaceType type = renderSurface->GetSurfaceType();
2574d949f91Sopenharmony_ci    EXPECT_EQ(type, RenderSurface::SurfaceType::SURFACE_TYPE_NULL);
2584d949f91Sopenharmony_ci}
2594d949f91Sopenharmony_ciHWTEST_F(TestRenderEnvironment, TestRenderEnvironment008, TestSize.Level1) {
2604d949f91Sopenharmony_ci    unsigned char *bitmap = new unsigned char[LEN];
2614d949f91Sopenharmony_ci
2624d949f91Sopenharmony_ci    int temp = renderEnvironment->GenTextureWithPixels(bitmap, WIDTH, HEIGHT, WIDTH);
2634d949f91Sopenharmony_ci    EXPECT_NE(temp, 0);
2644d949f91Sopenharmony_ci
2654d949f91Sopenharmony_ci    temp = renderEnvironment->GenTextureWithPixels(bitmap, WIDTH - EXTRA_LEN, HEIGHT, WIDTH);
2664d949f91Sopenharmony_ci    EXPECT_NE(temp, 0);
2674d949f91Sopenharmony_ci    delete[] bitmap;
2684d949f91Sopenharmony_ci}
2694d949f91Sopenharmony_ci}
2704d949f91Sopenharmony_ci}
2714d949f91Sopenharmony_ci}
2724d949f91Sopenharmony_ci}