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 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Effect {
26 namespace Test {
27 
28 constexpr uint32_t WIDTH = 960;
29 constexpr uint32_t HEIGHT = 1280;
30 constexpr IEffectFormat FORMATE_TYPE = IEffectFormat::RGBA8888;
31 constexpr uint32_t ROW_STRIDE = WIDTH * 4;
32 constexpr uint32_t LEN = ROW_STRIDE * HEIGHT;
33 
34 class TestRenderEnvironment : public testing::Test {
35 public:
36     TestRenderEnvironment() = default;
37 
38     ~TestRenderEnvironment() override = default;
SetUpTestCase()39     static void SetUpTestCase() {}
40 
TearDownTestCase()41     static void TearDownTestCase() {}
42 
SetUp()43     void SetUp() override
44     {
45         renderEnvironment = std::make_shared<RenderEnvironment>();
46         renderEnvironment->Init();
47         renderEnvironment->Prepare();
48 
49         std::shared_ptr<BufferInfo> bufferInfo = std::make_unique<BufferInfo>();
50         bufferInfo->width_ = WIDTH;
51         bufferInfo->height_ = HEIGHT;
52         bufferInfo->rowStride_ = ROW_STRIDE;
53         bufferInfo->len_ = LEN;
54         bufferInfo->formatType_ = FORMATE_TYPE;
55         void *addr = malloc(bufferInfo->len_);
56 
57         std::shared_ptr<ExtraInfo> extraInfo = std::make_unique<ExtraInfo>();
58         extraInfo->dataType = DataType::PIXEL_MAP;
59         extraInfo->bufferType = BufferType::HEAP_MEMORY;
60         extraInfo->pixelMap = nullptr;
61         extraInfo->surfaceBuffer = nullptr;
62         effectBuffer = std::make_shared<EffectBuffer>(bufferInfo, addr, extraInfo);
63     }
64 
TearDown()65     void TearDown() override
66     {
67         effectBuffer = nullptr;
68         if (renderEnvironment == nullptr) {
69             return;
70         }
71         renderEnvironment->ReleaseParam();
72         renderEnvironment->Release();
73     }
74 
75     std::shared_ptr<EffectBuffer> effectBuffer;
76     std::shared_ptr<RenderEnvironment> renderEnvironment;
77 };
78 
79 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment001, TestSize.Level1)
80 {
81     std::shared_ptr<EffectBuffer> output;
82     renderEnvironment->outType_ = DataType::NATIVE_WINDOW;
83     effectBuffer->bufferInfo_->width_ = 2 * WIDTH;
84     renderEnvironment->GenMainTex(effectBuffer, output);
85 
86     effectBuffer->bufferInfo_->formatType_ = IEffectFormat::DEFAULT;
87     renderEnvironment->GenMainTex(effectBuffer, output);
88 
89     RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
90     EXPECT_NE(texptr, nullptr);
91     renderEnvironment->ConvertTextureToBuffer(texptr, effectBuffer.get());
92 
93     GLuint tex = renderEnvironment->GenTexFromEffectBuffer(effectBuffer.get());
94     EXPECT_NE(tex, 0);
95 
96     GLenum internalFormat = GL_RG8;
97     size_t ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
98     EXPECT_EQ(ret, 0);
99 
100     internalFormat = GL_R8;
101     ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
102     EXPECT_EQ(ret, 1);
103 
104     internalFormat = GL_RGB565;
105     ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
106     EXPECT_EQ(ret, 2);
107 
108     internalFormat = GL_RGBA4;
109     ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
110     EXPECT_EQ(ret, 2);
111 
112     internalFormat = GL_RGBA16F;
113     ret = GLUtils::GetInternalFormatPixelByteSize(internalFormat);
114     EXPECT_EQ(ret, 8);
115 }
116 
117 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment002, TestSize.Level1)
118 {
119     IEffectFormat format = IEffectFormat::YUVNV12;
120     GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
121     EXPECT_NE(tex, 0);
122 
123     GraphicTransformType type = GraphicTransformType::GRAPHIC_ROTATE_NONE;
124     renderEnvironment->DrawFrame(tex, type);
125     renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
126     effectBuffer->tex = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
127     EXPECT_NE(effectBuffer->tex, nullptr);
128 
129     type = GraphicTransformType::GRAPHIC_ROTATE_90;
130     renderEnvironment->DrawFrame(tex, type);
131     renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
132     type = GraphicTransformType::GRAPHIC_FLIP_H_ROT90;
133     renderEnvironment->DrawFrame(tex, type);
134     type = GraphicTransformType::GRAPHIC_FLIP_V_ROT90;
135     renderEnvironment->DrawFrame(tex, type);
136 
137     type = GraphicTransformType::GRAPHIC_ROTATE_180;
138     renderEnvironment->DrawFrame(tex, type);
139     renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
140     type = GraphicTransformType::GRAPHIC_FLIP_H_ROT180;
141     renderEnvironment->DrawFrame(tex, type);
142     type = GraphicTransformType::GRAPHIC_FLIP_V_ROT180;
143     renderEnvironment->DrawFrame(tex, type);
144 
145     type = GraphicTransformType::GRAPHIC_ROTATE_270;
146     renderEnvironment->DrawFrame(tex, type);
147     renderEnvironment->DrawFrameWithTransform(effectBuffer, type);
148     type = GraphicTransformType::GRAPHIC_FLIP_H_ROT270;
149     renderEnvironment->DrawFrame(tex, type);
150     type = GraphicTransformType::GRAPHIC_FLIP_V_ROT270;
151     renderEnvironment->DrawFrame(tex, type);
152 }
153 
154 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment003, TestSize.Level1)
155 {
156     std::shared_ptr<EffectBuffer> input;
157     RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
158     EXPECT_NE(texptr, nullptr);
159 
160     effectBuffer->tex = texptr;
161     renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
162     effectBuffer->extraInfo_->surfaceBuffer = nullptr;
163     std::shared_ptr<EffectBuffer> buffer =
164         renderEnvironment->ConvertBufferToTexture(effectBuffer.get());
165     EXPECT_NE(buffer, nullptr);
166 
167     bool result = renderEnvironment->IfNeedGenMainTex();
168     EXPECT_EQ(result, true);
169 
170     RenderContext *context = renderEnvironment->GetContext();
171     EXPECT_NE(context, nullptr);
172 
173     ResourceCache *ceCache = renderEnvironment->GetResourceCache();
174     EXPECT_NE(ceCache, nullptr);
175 }
176 
177 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment004, TestSize.Level1)
178 {
179     RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
180     EXPECT_NE(texptr, nullptr);
181 
182     IEffectFormat format = IEffectFormat::YUVNV21;
183     GLuint tex = renderEnvironment->ConvertFromYUVToRGB(effectBuffer.get(), format);
184     EXPECT_NE(tex, 0);
185     renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
186 
187     format = IEffectFormat::YUVNV12;
188     renderEnvironment->ConvertFromRGBToYUV(texptr, format, effectBuffer->buffer_);
189 }
190 
191 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment005, TestSize.Level1)
192 {
193     std::shared_ptr<RenderContext> renderContext = std::make_shared<RenderContext>();
194     bool result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
195     EXPECT_EQ(result, false);
196 
197     result = renderContext->Init();
198     EXPECT_EQ(result, true);
199 
200     result = renderContext->SwapBuffers(renderEnvironment->screenSurface_);
201     EXPECT_EQ(result, false);
202 }
203 
204 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment006, TestSize.Level1)
205 {
206     std::shared_ptr<EffectContext> effectContext = std::make_shared<EffectContext>();
207     effectContext->renderEnvironment_ = renderEnvironment;
208     effectContext->memoryManager_ = std::make_shared<EffectMemoryManager>();
209     MemoryInfo memInfo = {
210         .bufferInfo = {
211             .width_ = effectBuffer->bufferInfo_->width_,
212             .height_ = effectBuffer->bufferInfo_->height_,
213             .len_ = effectBuffer->bufferInfo_->len_,
214             .formatType_ = effectBuffer->bufferInfo_->formatType_,
215         },
216         .bufferType = BufferType::DMA_BUFFER,
217     };
218     MemoryData *memoryData = effectContext->memoryManager_->AllocMemory(effectBuffer->buffer_, memInfo);
219     MemoryInfo &allocMemInfo = memoryData->memoryInfo;
220     SurfaceBuffer *buffer = (allocMemInfo.bufferType == BufferType::DMA_BUFFER) ?
221         static_cast<SurfaceBuffer *>(allocMemInfo.extra) : nullptr;
222 
223     std::shared_ptr<EffectBuffer> input;
224     RenderTexturePtr texptr = renderEnvironment->RequestBuffer(WIDTH, HEIGHT);
225     EXPECT_NE(texptr, nullptr);
226 
227     effectBuffer->tex = texptr;
228     effectBuffer->extraInfo_->surfaceBuffer = buffer;
229     effectBuffer->bufferInfo_->formatType_ = IEffectFormat::YUVNV12;
230 
231     renderEnvironment->ConvertYUV2RGBA(effectBuffer, input);
232     renderEnvironment->DrawTexFromSurfaceBuffer(texptr, buffer);
233 
234     GLuint tex = renderEnvironment->GenTexFromEffectBuffer(effectBuffer.get());
235     EXPECT_NE(tex, 0);
236 
237     IEffectFormat format = IEffectFormat::RGBA8888;
238     renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
239 
240     format = IEffectFormat::YUVNV12;
241     renderEnvironment->DrawFlipSurfaceBufferFromTex(texptr, buffer, format);
242     renderEnvironment->DrawSurfaceBufferFromTex(texptr, buffer, format);
243 }
244 HWTEST_F(TestRenderEnvironment, TestRenderEnvironment007, TestSize.Level1) {
245     std::string tag = "TestTag";
246     std::shared_ptr<RenderSurface> renderSurface = std::make_shared<RenderSurface>(tag);
247 
248     bool result = renderSurface->Init();
249     EXPECT_EQ(result, true);
250 
251     void *window = nullptr;
252     result = renderSurface->Create(window);
253     EXPECT_EQ(result, false);
254 
255     result = renderSurface->Release();
256     EXPECT_EQ(result, true);
257 
258     RenderSurface::SurfaceType type = renderSurface->GetSurfaceType();
259     EXPECT_EQ(type, RenderSurface::SurfaceType::SURFACE_TYPE_NULL);
260 }
261 }
262 }
263 }
264 }