1/*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "gtest/gtest.h"
17
18#include "render_environment.h"
19#include "effect_context.h"
20
21using namespace testing::ext;
22
23namespace OHOS {
24namespace Media {
25namespace Effect {
26namespace Test {
27
28constexpr uint32_t WIDTH = 960;
29constexpr uint32_t HEIGHT = 1280;
30constexpr IEffectFormat FORMATE_TYPE = IEffectFormat::RGBA8888;
31constexpr uint32_t ROW_STRIDE = WIDTH * 4;
32constexpr uint32_t LEN = ROW_STRIDE * HEIGHT;
33constexpr uint32_t EXTRA_LEN = 10;
34
35class TestRenderEnvironment : public testing::Test {
36public:
37    TestRenderEnvironment() = default;
38
39    ~TestRenderEnvironment() override = default;
40    static void SetUpTestCase() {}
41
42    static void TearDownTestCase() {}
43
44    void SetUp() override
45    {
46        renderEnvironment = std::make_shared<RenderEnvironment>();
47        renderEnvironment->Init();
48        renderEnvironment->Prepare();
49
50        std::shared_ptr<BufferInfo> bufferInfo = std::make_unique<BufferInfo>();
51        bufferInfo->width_ = WIDTH;
52        bufferInfo->height_ = HEIGHT;
53        bufferInfo->rowStride_ = ROW_STRIDE;
54        bufferInfo->len_ = LEN;
55        bufferInfo->formatType_ = FORMATE_TYPE;
56        void *addr = malloc(bufferInfo->len_);
57
58        std::shared_ptr<ExtraInfo> extraInfo = std::make_unique<ExtraInfo>();
59        extraInfo->dataType = DataType::PIXEL_MAP;
60        extraInfo->bufferType = BufferType::HEAP_MEMORY;
61        extraInfo->pixelMap = nullptr;
62        extraInfo->surfaceBuffer = nullptr;
63        effectBuffer = std::make_shared<EffectBuffer>(bufferInfo, addr, extraInfo);
64
65        free(addr);
66        addr = nullptr;
67    }
68
69    void TearDown() override
70    {
71        effectBuffer = nullptr;
72        if (renderEnvironment == nullptr) {
73            return;
74        }
75        renderEnvironment->ReleaseParam();
76        renderEnvironment->Release();
77    }
78
79    std::shared_ptr<EffectBuffer> effectBuffer;
80    std::shared_ptr<RenderEnvironment> renderEnvironment;
81};
82
83HWTEST_F(TestRenderEnvironment, TestRenderEnvironment001, TestSize.Level1)
84{
85    std::shared_ptr<EffectBuffer> output;
86    renderEnvironment->outType_ = DataType::NATIVE_WINDOW;
87    effectBuffer->bufferInfo_->width_ = 2 * WIDTH;
88    renderEnvironment->GenMainTex(effectBuffer, output);
89
90    effectBuffer->bufferInfo_->formatType_ = IEffectFormat::DEFAULT;
91    renderEnvironment->GenMainTex(effectBuffer, output);
92
93    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
94    EXPECT_NE(texptr, nullptr);
95    renderEnvironment->ConvertTextureToBuffer(texptr, effectBuffer.get());
96
97    GLenum internalFormat = GL_RG8;
98    size_t ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
99    EXPECT_EQ(ret, 0);
100
101    internalFormat = GL_R8;
102    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
103    EXPECT_EQ(ret, 1);
104
105    internalFormat = GL_RGB565;
106    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
107    EXPECT_EQ(ret, 2);
108
109    internalFormat = GL_RGBA4;
110    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
111    EXPECT_EQ(ret, 2);
112
113    internalFormat = GL_RGBA16F;
114    ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
115    EXPECT_EQ(ret, 8);
116}
117
118HWTEST_F(TestRenderEnvironment, TestRenderEnvironment002, TestSize.Level1)
119{
120    IEffectFormat format = IEffectFormat::YUVNV12;
121    GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
122    EXPECT_NE(tex, 0);
123
124    GraphicTransformType type = GraphicTransformType::GRAPHIC_ROTATE_NONE;
125    renderEnvironment->DrawFrame(tex, type);
126    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
127    effectBuffer->tex = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
128    EXPECT_NE(effectBuffer->tex, nullptr);
129
130    type = GraphicTransformType::GRAPHIC_ROTATE_90;
131    renderEnvironment->DrawFrame(tex, type);
132    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
133    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT90;
134    renderEnvironment->DrawFrame(tex, type);
135    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT90;
136    renderEnvironment->DrawFrame(tex, type);
137
138    type = GraphicTransformType::GRAPHIC_ROTATE_180;
139    renderEnvironment->DrawFrame(tex, type);
140    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
141    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT180;
142    renderEnvironment->DrawFrame(tex, type);
143    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT180;
144    renderEnvironment->DrawFrame(tex, type);
145
146    type = GraphicTransformType::GRAPHIC_ROTATE_270;
147    renderEnvironment->DrawFrame(tex, type);
148    renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
149    type = GraphicTransformType::GRAPHIC_FLIP_H_ROT270;
150    renderEnvironment->DrawFrame(tex, type);
151    type = GraphicTransformType::GRAPHIC_FLIP_V_ROT270;
152    renderEnvironment->DrawFrame(tex, type);
153}
154
155HWTEST_F(TestRenderEnvironment, TestRenderEnvironment003, TestSize.Level1)
156{
157    std::shared_ptr<EffectBuffer> input;
158    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
159    EXPECT_NE(texptr, nullptr);
160
161    effectBuffer->tex = texptr;
162    renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
163    effectBuffer->extraInfo_->surfaceBuffer = nullptr;
164    std::shared_ptr<EffectBuffer> buffer =
165        renderEnvironment->ConvertBufferToTexture(effectBuffer.get());
166    EXPECT_NE(buffer, nullptr);
167
168    bool result = renderEnvironment->IfNeedGenMainTex();
169    EXPECT_EQ(result, true);
170
171    RenderContext *context = renderEnvironment->GetContext();
172    EXPECT_NE(context, nullptr);
173
174    ResourceCache *ceCache = renderEnvironment->GetResourceCache();
175    EXPECT_NE(ceCache, nullptr);
176}
177
178HWTEST_F(TestRenderEnvironment, TestRenderEnvironment004, TestSize.Level1)
179{
180    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
181    EXPECT_NE(texptr, nullptr);
182
183    IEffectFormat format = IEffectFormat::YUVNV21;
184    GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
185    EXPECT_NE(tex, 0);
186    renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
187
188    format = IEffectFormat::YUVNV12;
189    renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
190}
191
192HWTEST_F(TestRenderEnvironment, TestRenderEnvironment005, TestSize.Level1)
193{
194    std::shared_ptr<RenderContext> renderContext = std::make_shared<RenderContext>();
195    bool result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
196    EXPECT_EQ(result, false);
197
198    result = renderContext->Init();
199    EXPECT_EQ(result, true);
200
201    result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
202    EXPECT_EQ(result, false);
203}
204
205HWTEST_F(TestRenderEnvironment, TestRenderEnvironment006, TestSize.Level1)
206{
207    std::shared_ptr<EffectContext> effectContext = std::make_shared<EffectContext>();
208    effectContext->renderEnvironment_ = renderEnvironment;
209    effectContext->memoryManager_ = std::make_shared<EffectMemoryManager>();
210    MemoryInfo memInfo = {
211        .bufferInfo = {
212            .width_ = effectBuffer->bufferInfo_->width_,
213            .height_ = effectBuffer->bufferInfo_->height_,
214            .len_ = effectBuffer->bufferInfo_->len_,
215            .formatType_ = effectBuffer->bufferInfo_->formatType_,
216        },
217        .bufferType = BufferType::DMA_BUFFER,
218    };
219    MemoryData *memoryData = effectContext->memoryManager_->AllocMemory(effectBuffer->buffer_, memInfo);
220    MemoryInfo &allocMemInfo = memoryData->memoryInfo;
221    SurfaceBuffer *buffer = (allocMemInfo.bufferType == BufferType::DMA_BUFFER) ?
222        static_cast<SurfaceBuffer *>(allocMemInfo.extra) : nullptr;
223
224    std::shared_ptr<EffectBuffer> input;
225    RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
226    EXPECT_NE(texptr, nullptr);
227
228    effectBuffer->tex = texptr;
229    effectBuffer->extraInfo_->surfaceBuffer = buffer;
230    effectBuffer->bufferInfo_->formatType_ = IEffectFormat::YUVNV12;
231
232    renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
233    renderEnvironment->DrawTexFromSurfaceBuffer(texptr, buffer);
234
235    IEffectFormat format = IEffectFormat::RGBA8888;
236    renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
237
238    format = IEffectFormat::YUVNV12;
239    renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
240    renderEnvironment->DrawSurfaceBufferFromTex(texptr, buffer, format);
241}
242HWTEST_F(TestRenderEnvironment, TestRenderEnvironment007, TestSize.Level1) {
243    std::string tag = "TestTag";
244    std::shared_ptr<RenderSurface> renderSurface = std::make_shared<RenderSurface>(tag);
245
246    bool result = renderSurface->Init();
247    EXPECT_EQ(result, true);
248
249    void *window = nullptr;
250    result = renderSurface->Create(window);
251    EXPECT_EQ(result, false);
252
253    result = renderSurface->Release();
254    EXPECT_EQ(result, true);
255
256    RenderSurface::SurfaceType type = renderSurface->GetSurfaceType();
257    EXPECT_EQ(type, RenderSurface::SurfaceType::SURFACE_TYPE_NULL);
258}
259HWTEST_F(TestRenderEnvironment, TestRenderEnvironment008, TestSize.Level1) {
260    unsigned char *bitmap = new unsigned char[LEN];
261
262    int temp = renderEnvironment->GenTextureWithPixels(bitmap, WIDTH, HEIGHT, WIDTH);
263    EXPECT_NE(temp, 0);
264
265    temp = renderEnvironment->GenTextureWithPixels(bitmap, WIDTH - EXTRA_LEN, HEIGHT, WIDTH);
266    EXPECT_NE(temp, 0);
267    delete[] bitmap;
268}
269}
270}
271}
272}