1 /* 2 * Copyright (c) 2025 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 "hdf_log.h" 18 #include "ashmem.h" 19 #include "v2_1/icodec_image.h" 20 #include "v1_2/display_composer_type.h" 21 #include "v1_2/display_buffer_type.h" 22 #include "v1_2/include/idisplay_buffer.h" 23 24 #define HDF_LOG_TAG codec_heif_decode_test 25 26 namespace { 27 using namespace std; 28 using namespace testing::ext; 29 using namespace OHOS::HDI::Codec::Image::V2_1; 30 using namespace OHOS::HDI::Display::Buffer::V1_2; 31 using namespace OHOS::HDI::Display::Composer::V1_2; 32 33 static OHOS::sptr<ICodecImage> hdiHeifDecoder_ = nullptr; 34 static OHOS::HDI::Display::Buffer::V1_2::IDisplayBuffer* bufferMgr_ = nullptr; 35 36 class CodecHdiHeifDecodeTest : public testing::Test { 37 public: SetUpTestCase()38 static void SetUpTestCase() 39 { 40 hdiHeifDecoder_ = ICodecImage::Get(); 41 bufferMgr_ = OHOS::HDI::Display::Buffer::V1_2::IDisplayBuffer::Get(); 42 } TearDownTestCase()43 static void TearDownTestCase() 44 { 45 hdiHeifDecoder_ = nullptr; 46 bufferMgr_ = nullptr; 47 } SetUp()48 void SetUp() override 49 { 50 } TearDown()51 void TearDown() override 52 { 53 } 54 AllocateOutputBuffer(uint32_t width,uint32_t height,int32_t pixelFmt)55 sptr<NativeBuffer> AllocateOutputBuffer(uint32_t width, uint32_t height, int32_t pixelFmt) 56 { 57 uint64_t usage = OHOS::HDI::Display::Composer::V1_2::HBM_USE_CPU_READ | 58 OHOS::HDI::Display::Composer::V1_2::HBM_USE_CPU_WRITE | 59 OHOS::HDI::Display::Composer::V1_2::HBM_USE_MEM_DMA | 60 OHOS::HDI::Display::Composer::V1_2::HBM_USE_MEM_MMZ_CACHE; 61 AllocInfo alloc = { 62 .width = width, 63 .height = height, 64 .usage = usage, 65 .format = pixelFmt 66 }; 67 BufferHandle *handle = nullptr; 68 int32_t ret = bufferMgr_->AllocMem(alloc, handle); 69 if (ret != HDF_SUCCESS || handle == nullptr) { 70 return nullptr; 71 } 72 sptr<NativeBuffer> output = new NativeBuffer(handle); 73 return output; 74 } 75 public: 76 }; 77 78 // [fail] output is null 79 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_001, TestSize.Level1) 80 { 81 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 82 CodecHeifDecInfo decInfo {}; 83 sptr<NativeBuffer> output = nullptr; 84 vector<sptr<Ashmem>> inputs; 85 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 86 ASSERT_NE(ret, HDF_SUCCESS); 87 } 88 89 // [fail] displaySize == 0 90 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_002, TestSize.Level1) 91 { 92 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 93 CodecHeifDecInfo decInfo; 94 decInfo.gridInfo = { 95 .displayWidth = 0, 96 .displayHeight = 0, 97 .enableGrid = false, 98 .cols = 0, 99 .rows = 0, 100 .tileWidth = 0, 101 .tileHeight = 0 102 }; 103 decInfo.sampleSize = 1; 104 sptr<NativeBuffer> output = AllocateOutputBuffer(128, 128, 105 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 106 vector<sptr<Ashmem>> inputs; 107 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 108 ASSERT_NE(ret, HDF_SUCCESS); 109 } 110 111 // [fail] gridCnt == 0 112 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_003, TestSize.Level1) 113 { 114 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 115 CodecHeifDecInfo decInfo; 116 decInfo.gridInfo = { 117 .displayWidth = 128, 118 .displayHeight = 128, 119 .enableGrid = true, 120 .cols = 0, 121 .rows = 0, 122 .tileWidth = 128, 123 .tileHeight = 128 124 }; 125 decInfo.sampleSize = 1; 126 sptr<NativeBuffer> output = AllocateOutputBuffer(128, 128, 127 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 128 vector<sptr<Ashmem>> inputs; 129 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 130 ASSERT_NE(ret, HDF_SUCCESS); 131 } 132 133 // [fail] gridSize == 0 134 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_004, TestSize.Level1) 135 { 136 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 137 CodecHeifDecInfo decInfo; 138 decInfo.gridInfo = { 139 .displayWidth = 128, 140 .displayHeight = 128, 141 .enableGrid = true, 142 .cols = 1, 143 .rows = 1, 144 .tileWidth = 0, 145 .tileHeight = 0 146 }; 147 decInfo.sampleSize = 1; 148 sptr<NativeBuffer> output = AllocateOutputBuffer(128, 128, 149 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 150 vector<sptr<Ashmem>> inputs; 151 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 152 ASSERT_NE(ret, HDF_SUCCESS); 153 } 154 155 // [fail] displaySize > gridSize * gridCnt 156 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_005, TestSize.Level1) 157 { 158 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 159 CodecHeifDecInfo decInfo; 160 decInfo.gridInfo = { 161 .displayWidth = 512, 162 .displayHeight = 512, 163 .enableGrid = true, 164 .cols = 1, 165 .rows = 1, 166 .tileWidth = 256, 167 .tileHeight = 256 168 }; 169 decInfo.sampleSize = 1; 170 sptr<NativeBuffer> output = AllocateOutputBuffer(512, 512, 171 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 172 vector<sptr<Ashmem>> inputs; 173 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 174 ASSERT_NE(ret, HDF_SUCCESS); 175 } 176 177 // [fail] not enough input (no grid) 178 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_006, TestSize.Level1) 179 { 180 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 181 CodecHeifDecInfo decInfo; 182 decInfo.gridInfo = { 183 .displayWidth = 512, 184 .displayHeight = 512, 185 .enableGrid = false, 186 .cols = 1, 187 .rows = 1, 188 .tileWidth = 512, 189 .tileHeight = 512 190 }; 191 decInfo.sampleSize = 1; 192 sptr<NativeBuffer> output = AllocateOutputBuffer(512, 512, 193 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 194 vector<sptr<Ashmem>> inputs; 195 inputs.push_back(Ashmem::CreateAshmem("", 512 * 512)); 196 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 197 ASSERT_NE(ret, HDF_SUCCESS); 198 } 199 200 // [fail] not enough input (grid) 201 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_007, TestSize.Level1) 202 { 203 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 204 CodecHeifDecInfo decInfo; 205 decInfo.gridInfo = { 206 .displayWidth = 512, 207 .displayHeight = 512, 208 .enableGrid = true, 209 .cols = 1, 210 .rows = 2, 211 .tileWidth = 512, 212 .tileHeight = 256 213 }; 214 decInfo.sampleSize = 1; 215 sptr<NativeBuffer> output = AllocateOutputBuffer(512, 512, 216 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 217 vector<sptr<Ashmem>> inputs; 218 inputs.push_back(Ashmem::CreateAshmem("", 512 * 512)); 219 inputs.push_back(Ashmem::CreateAshmem("", 512 * 512)); 220 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 221 ASSERT_NE(ret, HDF_SUCCESS); 222 } 223 224 // [fail] sampleSize = 1, output is too small 225 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_008, TestSize.Level1) 226 { 227 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 228 CodecHeifDecInfo decInfo; 229 decInfo.gridInfo = { 230 .displayWidth = 512, 231 .displayHeight = 512, 232 .enableGrid = false, 233 .cols = 1, 234 .rows = 1, 235 .tileWidth = 512, 236 .tileHeight = 512 237 }; 238 decInfo.sampleSize = 1; 239 sptr<NativeBuffer> output = AllocateOutputBuffer(128, 128, 240 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 241 vector<sptr<Ashmem>> inputs; 242 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 243 ASSERT_NE(ret, HDF_SUCCESS); 244 } 245 246 // [fail] unsupported sampleSize 247 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_009, TestSize.Level1) 248 { 249 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 250 CodecHeifDecInfo decInfo; 251 decInfo.gridInfo = { 252 .displayWidth = 512, 253 .displayHeight = 512, 254 .enableGrid = false, 255 .cols = 1, 256 .rows = 1, 257 .tileWidth = 512, 258 .tileHeight = 512 259 }; 260 decInfo.sampleSize = 3; 261 sptr<NativeBuffer> output = AllocateOutputBuffer(512, 512, 262 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 263 vector<sptr<Ashmem>> inputs; 264 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 265 ASSERT_NE(ret, HDF_SUCCESS); 266 } 267 268 // [fail] displaySize % sampleSize != 0 269 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_010, TestSize.Level1) 270 { 271 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 272 CodecHeifDecInfo decInfo; 273 decInfo.gridInfo = { 274 .displayWidth = 511, 275 .displayHeight = 511, 276 .enableGrid = false, 277 .cols = 1, 278 .rows = 1, 279 .tileWidth = 512, 280 .tileHeight = 512 281 }; 282 decInfo.sampleSize = 4; 283 sptr<NativeBuffer> output = AllocateOutputBuffer(128, 128, 284 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YCBCR_420_SP); 285 vector<sptr<Ashmem>> inputs; 286 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 287 ASSERT_NE(ret, HDF_SUCCESS); 288 } 289 290 // [fail] unsupported output pixel format 291 HWTEST_F(CodecHdiHeifDecodeTest, HdfCodecHdiDoHeifDecodeTest_011, TestSize.Level1) 292 { 293 ASSERT_TRUE(hdiHeifDecoder_ != nullptr); 294 CodecHeifDecInfo decInfo; 295 decInfo.gridInfo = { 296 .displayWidth = 512, 297 .displayHeight = 512, 298 .enableGrid = false, 299 .cols = 1, 300 .rows = 1, 301 .tileWidth = 512, 302 .tileHeight = 512 303 }; 304 decInfo.sampleSize = 2; 305 sptr<NativeBuffer> output = AllocateOutputBuffer(256, 256, 306 OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_YUV_422_I); 307 vector<sptr<Ashmem>> inputs; 308 int32_t ret = hdiHeifDecoder_->DoHeifDecode(inputs, output, decInfo); 309 ASSERT_NE(ret, HDF_SUCCESS); 310 } 311 }