1 /*
2  * Copyright (c) 2021 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 #include "hardware/heif_hw_decoder.h"
18 #include "image_system_properties.h"
19 #include "mock_heif_hw_decode_flow.h"
20 #include "media_errors.h" // foundation/multimedia/image_framework/interfaces/innerkits/include/
21 
22 namespace OHOS::Media {
23 using namespace testing::ext;
24 using namespace OHOS::ImagePlugin;
25 
26 class HeifHwDecoderTest : public testing::Test {
27 public:
28     static constexpr char TEST_HEIF_IMG_NO_GRID[] = "/data/local/tmp/image/heif_test/1024x1024_nogrid";
29     static constexpr char TEST_HEIF_IMG_WITH_GRID[] = "/data/local/tmp/image/heif_test/1024x1024_grid_512x512_2x2";
30 };
31 
32 HWTEST_F(HeifHwDecoderTest, AllocOutputBufferWithInvalidSize, TestSize.Level1)
33 {
34     HeifHardwareDecoder testObj;
35     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(
36         0, 512, static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
37     ASSERT_TRUE(output == nullptr);
38 }
39 
40 HWTEST_F(HeifHwDecoderTest, AllocOutputBufferWithInvalidPixelFmt, TestSize.Level1)
41 {
42     HeifHardwareDecoder testObj;
43     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(
44         512, 512, static_cast<int32_t>(GRAPHIC_PIXEL_FMT_RGBA_8888));
45     ASSERT_TRUE(output == nullptr);
46 }
47 
48 HWTEST_F(HeifHwDecoderTest, AllocOutputBufferOk, TestSize.Level1)
49 {
50     HeifHardwareDecoder testObj;
51     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(
52         1024, 512, static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
53     ASSERT_TRUE(output != nullptr);
54 }
55 
56 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidInputs, TestSize.Level1)
57 {
58     HeifHardwareDecoder testObj;
59     GridInfo gridInfo = {
60         .displayWidth = 1024,
61         .displayHeight = 512,
62         .enableGrid = false,
63         .cols = 0,
64         .rows = 0,
65         .tileWidth = 0,
66         .tileHeight = 0
67     };
68     std::vector<std::vector<uint8_t>> inputs;
69     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(gridInfo.displayWidth, gridInfo.displayHeight,
70                                                               static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
71     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
72     ASSERT_TRUE(ret != Media::SUCCESS);
73 }
74 
75 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidOutputBuffer, TestSize.Level1)
76 {
77     HeifHardwareDecoder testObj;
78     GridInfo gridInfo = {
79         .displayWidth = 1024,
80         .displayHeight = 512,
81         .enableGrid = false,
82         .cols = 0,
83         .rows = 0,
84         .tileWidth = 0,
85         .tileHeight = 0
86     };
87     std::vector<std::vector<uint8_t>> inputs;
88     inputs.emplace_back(std::vector<uint8_t>(1));
89     inputs.emplace_back(std::vector<uint8_t>(1));
90     sptr<SurfaceBuffer> output = nullptr;
91     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
92     ASSERT_TRUE(ret != Media::SUCCESS);
93 }
94 
95 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidGridInfo1, TestSize.Level1)
96 {
97     HeifHardwareDecoder testObj;
98     GridInfo gridInfo = {
99         .displayWidth = 512,
100         .displayHeight = 512,
101         .enableGrid = true,
102         .cols = 0,
103         .rows = 0,
104         .tileWidth = 0,
105         .tileHeight = 0
106     };
107     std::vector<std::vector<uint8_t>> inputs;
108     inputs.emplace_back(std::vector<uint8_t>(1));
109     inputs.emplace_back(std::vector<uint8_t>(1));
110     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(gridInfo.displayWidth, gridInfo.displayHeight,
111                                                               static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
112     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
113     ASSERT_TRUE(ret != Media::SUCCESS);
114 }
115 
116 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidGridInfo2, TestSize.Level1)
117 {
118     HeifHardwareDecoder testObj;
119     GridInfo gridInfo = {
120         .displayWidth = 1024,
121         .displayHeight = 1024,
122         .enableGrid = true,
123         .cols = 1,
124         .rows = 2,
125         .tileWidth = 512,
126         .tileHeight = 512
127     };
128     std::vector<std::vector<uint8_t>> inputs;
129     inputs.emplace_back(std::vector<uint8_t>(1));
130     inputs.emplace_back(std::vector<uint8_t>(1));
131     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(gridInfo.displayWidth, gridInfo.displayHeight,
132                                                               static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
133     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
134     ASSERT_TRUE(ret != Media::SUCCESS);
135 }
136 
137 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidGridInfo3, TestSize.Level1)
138 {
139     HeifHardwareDecoder testObj;
140     GridInfo gridInfo = {
141         .displayWidth = 512,
142         .displayHeight = 512,
143         .enableGrid = true,
144         .cols = 1,
145         .rows = 1,
146         .tileWidth = 512,
147         .tileHeight = 0
148     };
149     std::vector<std::vector<uint8_t>> inputs;
150     inputs.emplace_back(std::vector<uint8_t>(1));
151     inputs.emplace_back(std::vector<uint8_t>(1));
152     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(gridInfo.displayWidth, gridInfo.displayHeight,
153                                                               static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
154     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
155     ASSERT_TRUE(ret != Media::SUCCESS);
156 }
157 
158 HWTEST_F(HeifHwDecoderTest, DoDecodeWithInvalidGridInfo4, TestSize.Level1)
159 {
160     HeifHardwareDecoder testObj;
161     GridInfo gridInfo = {
162         .displayWidth = 513,
163         .displayHeight = 512,
164         .enableGrid = true,
165         .cols = 1,
166         .rows = 1,
167         .tileWidth = 512,
168         .tileHeight = 512
169     };
170     std::vector<std::vector<uint8_t>> inputs;
171     inputs.emplace_back(std::vector<uint8_t>(1));
172     inputs.emplace_back(std::vector<uint8_t>(1));
173     sptr<SurfaceBuffer> output = testObj.AllocateOutputBuffer(gridInfo.displayWidth, gridInfo.displayHeight,
174                                                               static_cast<int32_t>(GRAPHIC_PIXEL_FMT_YCBCR_420_SP));
175     uint32_t ret = testObj.DoDecode(gridInfo, inputs, output);
176     ASSERT_TRUE(ret != Media::SUCCESS);
177 }
178 
179 HWTEST_F(HeifHwDecoderTest, DoDecodeOkNoGrid, TestSize.Level1)
180 {
181     bool ret = true;
182     if (ImageSystemProperties::GetHardWareDecodeEnabled()) {
183         CommandOpt opt = {
184             .pixelFormat = UserPixelFormat::NV21,
185             .inputPath = TEST_HEIF_IMG_NO_GRID
186         };
187         HeifHwDecoderFlow testObj;
188         ret = testObj.Run(opt);
189     }
190     ASSERT_TRUE(ret);
191 }
192 
193 HWTEST_F(HeifHwDecoderTest, DoDecodeOkWithGrid, TestSize.Level1)
194 {
195     bool ret = true;
196     if (ImageSystemProperties::GetHardWareDecodeEnabled()) {
197         CommandOpt opt = {
198             .pixelFormat = UserPixelFormat::NV21,
199             .inputPath = TEST_HEIF_IMG_WITH_GRID
200         };
201         HeifHwDecoderFlow testObj;
202         ret = testObj.Run(opt);
203     }
204     ASSERT_TRUE(ret);
205 }
206 } // namespace OHOS::Media