]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ImageEx.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ImageEx.c
1 /** @file
2 Implementation for EFI_HII_IMAGE_EX_PROTOCOL.
3
4
5 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "HiiDatabase.h"
11
12 /**
13 The prototype of this extension function is the same with EFI_HII_IMAGE_PROTOCOL.NewImage().
14 This protocol invokes EFI_HII_IMAGE_PROTOCOL.NewImage() implicitly.
15
16 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
17 @param PackageList Handle of the package list where this image will
18 be added.
19 @param ImageId On return, contains the new image id, which is
20 unique within PackageList.
21 @param Image Points to the image.
22
23 @retval EFI_SUCCESS The new image was added successfully.
24 @retval EFI_NOT_FOUND The PackageList could not be found.
25 @retval EFI_OUT_OF_RESOURCES Could not add the image due to lack of resources.
26 @retval EFI_INVALID_PARAMETER Image is NULL or ImageId is NULL.
27 **/
28 EFI_STATUS
29 EFIAPI
30 HiiNewImageEx (
31 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
32 IN EFI_HII_HANDLE PackageList,
33 OUT EFI_IMAGE_ID *ImageId,
34 IN CONST EFI_IMAGE_INPUT *Image
35 )
36 {
37 HII_DATABASE_PRIVATE_DATA *Private;
38
39 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);
40 return HiiNewImage (&Private->HiiImage, PackageList, ImageId, Image);
41 }
42
43 /**
44 Return the information about the image, associated with the package list.
45 The prototype of this extension function is the same with EFI_HII_IMAGE_PROTOCOL.GetImage().
46
47 This function is similar to EFI_HII_IMAGE_PROTOCOL.GetImage().The difference is that
48 this function will locate all EFI_HII_IMAGE_DECODER_PROTOCOL instances installed in the
49 system if the decoder of the certain image type is not supported by the
50 EFI_HII_IMAGE_EX_PROTOCOL. The function will attempt to decode the image to the
51 EFI_IMAGE_INPUT using the first EFI_HII_IMAGE_DECODER_PROTOCOL instance that
52 supports the requested image type.
53
54 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
55 @param PackageList The package list in the HII database to search for the
56 specified image.
57 @param ImageId The image's id, which is unique within PackageList.
58 @param Image Points to the image.
59
60 @retval EFI_SUCCESS The new image was returned successfully.
61 @retval EFI_NOT_FOUND The image specified by ImageId is not available. The specified
62 PackageList is not in the Database.
63 @retval EFI_INVALID_PARAMETER Image was NULL or ImageId was 0.
64 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there
65 was not enough memory.
66
67 **/
68 EFI_STATUS
69 EFIAPI
70 HiiGetImageEx (
71 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
72 IN EFI_HII_HANDLE PackageList,
73 IN EFI_IMAGE_ID ImageId,
74 OUT EFI_IMAGE_INPUT *Image
75 )
76 {
77 HII_DATABASE_PRIVATE_DATA *Private;
78
79 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);
80 return IGetImage (&Private->DatabaseList, PackageList, ImageId, Image, FALSE);
81 }
82
83 /**
84 Change the information about the image.
85
86 Same with EFI_HII_IMAGE_PROTOCOL.SetImage(),this protocol invokes
87 EFI_HII_IMAGE_PROTOCOL.SetImage()implicitly.
88
89 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
90 @param PackageList The package list containing the images.
91 @param ImageId The image's id, which is unique within PackageList.
92 @param Image Points to the image.
93
94 @retval EFI_SUCCESS The new image was successfully updated.
95 @retval EFI_NOT_FOUND The image specified by ImageId is not in the
96 database. The specified PackageList is not in
97 the database.
98 @retval EFI_INVALID_PARAMETER The Image was NULL, the ImageId was 0 or
99 the Image->Bitmap was NULL.
100
101 **/
102 EFI_STATUS
103 EFIAPI
104 HiiSetImageEx (
105 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
106 IN EFI_HII_HANDLE PackageList,
107 IN EFI_IMAGE_ID ImageId,
108 IN CONST EFI_IMAGE_INPUT *Image
109 )
110 {
111 HII_DATABASE_PRIVATE_DATA *Private;
112
113 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);
114 return HiiSetImage (&Private->HiiImage, PackageList, ImageId, Image);
115 }
116
117 /**
118 Renders an image to a bitmap or to the display.
119
120 The prototype of this extension function is the same with
121 EFI_HII_IMAGE_PROTOCOL.DrawImage(). This protocol invokes
122 EFI_HII_IMAGE_PROTOCOL.DrawImage() implicitly.
123
124 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
125 @param Flags Describes how the image is to be drawn.
126 @param Image Points to the image to be displayed.
127 @param Blt If this points to a non-NULL on entry, this points
128 to the image, which is Width pixels wide and
129 Height pixels high. The image will be drawn onto
130 this image and EFI_HII_DRAW_FLAG_CLIP is implied.
131 If this points to a NULL on entry, then a buffer
132 will be allocated to hold the generated image and
133 the pointer updated on exit. It is the caller's
134 responsibility to free this buffer.
135 @param BltX Specifies the offset from the left and top edge of
136 the output image of the first pixel in the image.
137 @param BltY Specifies the offset from the left and top edge of
138 the output image of the first pixel in the image.
139
140 @retval EFI_SUCCESS The image was successfully drawn.
141 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.
142 @retval EFI_INVALID_PARAMETER The Image or Blt was NULL.
143
144 **/
145 EFI_STATUS
146 EFIAPI
147 HiiDrawImageEx (
148 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
149 IN EFI_HII_DRAW_FLAGS Flags,
150 IN CONST EFI_IMAGE_INPUT *Image,
151 IN OUT EFI_IMAGE_OUTPUT **Blt,
152 IN UINTN BltX,
153 IN UINTN BltY
154 )
155 {
156 HII_DATABASE_PRIVATE_DATA *Private;
157
158 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);
159 return HiiDrawImage (&Private->HiiImage, Flags, Image, Blt, BltX, BltY);
160 }
161
162 /**
163 Renders an image to a bitmap or the screen containing the contents of the specified
164 image.
165
166 This function is similar to EFI_HII_IMAGE_PROTOCOL.DrawImageId(). The difference is that
167 this function will locate all EFI_HII_IMAGE_DECODER_PROTOCOL instances installed in the
168 system if the decoder of the certain image type is not supported by the
169 EFI_HII_IMAGE_EX_PROTOCOL. The function will attempt to decode the image to the
170 EFI_IMAGE_INPUT using the first EFI_HII_IMAGE_DECODER_PROTOCOL instance that
171 supports the requested image type.
172
173 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
174 @param Flags Describes how the image is to be drawn.
175 @param PackageList The package list in the HII database to search for
176 the specified image.
177 @param ImageId The image's id, which is unique within PackageList.
178 @param Blt If this points to a non-NULL on entry, this points
179 to the image, which is Width pixels wide and
180 Height pixels high. The image will be drawn onto
181 this image and EFI_HII_DRAW_FLAG_CLIP is implied.
182 If this points to a NULL on entry, then a buffer
183 will be allocated to hold the generated image
184 and the pointer updated on exit. It is the caller's
185 responsibility to free this buffer.
186 @param BltX Specifies the offset from the left and top edge of
187 the output image of the first pixel in the image.
188 @param BltY Specifies the offset from the left and top edge of
189 the output image of the first pixel in the image.
190
191 @retval EFI_SUCCESS The image was successfully drawn.
192 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.
193 @retval EFI_INVALID_PARAMETER The Blt was NULL or ImageId was 0.
194 @retval EFI_NOT_FOUND The image specified by ImageId is not in the database.
195 The specified PackageList is not in the database.
196
197 **/
198 EFI_STATUS
199 EFIAPI
200 HiiDrawImageIdEx (
201 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
202 IN EFI_HII_DRAW_FLAGS Flags,
203 IN EFI_HII_HANDLE PackageList,
204 IN EFI_IMAGE_ID ImageId,
205 IN OUT EFI_IMAGE_OUTPUT **Blt,
206 IN UINTN BltX,
207 IN UINTN BltY
208 )
209 {
210 EFI_STATUS Status;
211 EFI_IMAGE_INPUT Image;
212
213 //
214 // Check input parameter.
215 //
216 if ((This == NULL) || (Blt == NULL)) {
217 return EFI_INVALID_PARAMETER;
218 }
219
220 //
221 // Get the specified Image.
222 //
223 Status = HiiGetImageEx (This, PackageList, ImageId, &Image);
224 if (EFI_ERROR (Status)) {
225 return Status;
226 }
227
228 //
229 // Draw this image.
230 //
231 Status = HiiDrawImageEx (This, Flags, &Image, Blt, BltX, BltY);
232 if (Image.Bitmap != NULL) {
233 FreePool (Image.Bitmap);
234 }
235
236 return Status;
237 }
238
239 /**
240 Return the first HII image decoder instance which supports the DecoderName.
241
242 @param BlockType The image block type.
243
244 @retval Pointer to the HII image decoder instance.
245 **/
246 EFI_HII_IMAGE_DECODER_PROTOCOL *
247 LocateHiiImageDecoder (
248 UINT8 BlockType
249 )
250 {
251 EFI_STATUS Status;
252 EFI_HII_IMAGE_DECODER_PROTOCOL *Decoder;
253 EFI_HANDLE *Handles;
254 UINTN HandleNum;
255 UINTN Index;
256 EFI_GUID *DecoderNames;
257 UINT16 NumberOfDecoderName;
258 UINT16 DecoderNameIndex;
259 EFI_GUID *DecoderName;
260
261 switch (BlockType) {
262 case EFI_HII_IIBT_IMAGE_JPEG:
263 DecoderName = &gEfiHiiImageDecoderNameJpegGuid;
264 break;
265
266 case EFI_HII_IIBT_IMAGE_PNG:
267 DecoderName = &gEfiHiiImageDecoderNamePngGuid;
268 break;
269
270 default:
271 ASSERT (FALSE);
272 return NULL;
273 }
274
275 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiHiiImageDecoderProtocolGuid, NULL, &HandleNum, &Handles);
276 if (EFI_ERROR (Status)) {
277 return NULL;
278 }
279
280 for (Index = 0; Index < HandleNum; Index++) {
281 Status = gBS->HandleProtocol (Handles[Index], &gEfiHiiImageDecoderProtocolGuid, (VOID **)&Decoder);
282 if (EFI_ERROR (Status)) {
283 continue;
284 }
285
286 Status = Decoder->GetImageDecoderName (Decoder, &DecoderNames, &NumberOfDecoderName);
287 if (EFI_ERROR (Status)) {
288 continue;
289 }
290
291 for (DecoderNameIndex = 0; DecoderNameIndex < NumberOfDecoderName; DecoderNameIndex++) {
292 if (CompareGuid (DecoderName, &DecoderNames[DecoderNameIndex])) {
293 return Decoder;
294 }
295 }
296 }
297
298 return NULL;
299 }
300
301 /**
302 This function returns the image information to EFI_IMAGE_OUTPUT. Only the width
303 and height are returned to the EFI_IMAGE_OUTPUT instead of decoding the image
304 to the buffer. This function is used to get the geometry of the image. This function
305 will try to locate all of the EFI_HII_IMAGE_DECODER_PROTOCOL installed on the
306 system if the decoder of image type is not supported by the EFI_HII_IMAGE_EX_PROTOCOL.
307
308 @param This A pointer to the EFI_HII_IMAGE_EX_PROTOCOL instance.
309 @param PackageList Handle of the package list where this image will
310 be searched.
311 @param ImageId The image's id, which is unique within PackageList.
312 @param Image Points to the image.
313
314 @retval EFI_SUCCESS The new image was returned successfully.
315 @retval EFI_NOT_FOUND The image specified by ImageId is not in the
316 database. The specified PackageList is not in the database.
317 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to
318 hold the image.
319 @retval EFI_INVALID_PARAMETER The Image was NULL or the ImageId was 0.
320 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there
321 was not enough memory.
322
323 **/
324 EFI_STATUS
325 EFIAPI
326 HiiGetImageInfo (
327 IN CONST EFI_HII_IMAGE_EX_PROTOCOL *This,
328 IN EFI_HII_HANDLE PackageList,
329 IN EFI_IMAGE_ID ImageId,
330 OUT EFI_IMAGE_OUTPUT *Image
331 )
332 {
333 EFI_STATUS Status;
334 HII_DATABASE_PRIVATE_DATA *Private;
335 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
336 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;
337 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;
338 EFI_HII_IMAGE_DECODER_PROTOCOL *Decoder;
339 EFI_HII_IMAGE_DECODER_IMAGE_INFO_HEADER *ImageInfo;
340
341 if ((Image == NULL) || (ImageId == 0)) {
342 return EFI_INVALID_PARAMETER;
343 }
344
345 Private = HII_IMAGE_EX_DATABASE_PRIVATE_DATA_FROM_THIS (This);
346 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);
347 if (PackageListNode == NULL) {
348 return EFI_NOT_FOUND;
349 }
350
351 ImagePackage = PackageListNode->ImagePkg;
352 if (ImagePackage == NULL) {
353 return EFI_NOT_FOUND;
354 }
355
356 //
357 // Find the image block specified by ImageId
358 //
359 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);
360 if (CurrentImageBlock == NULL) {
361 return EFI_NOT_FOUND;
362 }
363
364 switch (CurrentImageBlock->BlockType) {
365 case EFI_HII_IIBT_IMAGE_JPEG:
366 case EFI_HII_IIBT_IMAGE_PNG:
367 Decoder = LocateHiiImageDecoder (CurrentImageBlock->BlockType);
368 if (Decoder == NULL) {
369 return EFI_UNSUPPORTED;
370 }
371
372 //
373 // Use the common block code since the definition of two structures is the same.
374 //
375 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data));
376 ASSERT (
377 sizeof (((EFI_HII_IIBT_JPEG_BLOCK *)CurrentImageBlock)->Data) ==
378 sizeof (((EFI_HII_IIBT_PNG_BLOCK *)CurrentImageBlock)->Data)
379 );
380 ASSERT (OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Size) == OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Size));
381 ASSERT (
382 sizeof (((EFI_HII_IIBT_JPEG_BLOCK *)CurrentImageBlock)->Size) ==
383 sizeof (((EFI_HII_IIBT_PNG_BLOCK *)CurrentImageBlock)->Size)
384 );
385 Status = Decoder->GetImageInfo (
386 Decoder,
387 ((EFI_HII_IIBT_JPEG_BLOCK *)CurrentImageBlock)->Data,
388 ((EFI_HII_IIBT_JPEG_BLOCK *)CurrentImageBlock)->Size,
389 &ImageInfo
390 );
391
392 //
393 // Spec requires to use the first capable image decoder instance.
394 // The first image decoder instance may fail to decode the image.
395 //
396 if (!EFI_ERROR (Status)) {
397 Image->Height = ImageInfo->ImageHeight;
398 Image->Width = ImageInfo->ImageWidth;
399 Image->Image.Bitmap = NULL;
400 FreePool (ImageInfo);
401 }
402
403 return Status;
404
405 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:
406 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:
407 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:
408 case EFI_HII_IIBT_IMAGE_1BIT:
409 case EFI_HII_IIBT_IMAGE_4BIT:
410 case EFI_HII_IIBT_IMAGE_8BIT:
411 //
412 // Use the common block code since the definition of these structures is the same.
413 //
414 Image->Width = ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *)CurrentImageBlock)->Bitmap.Width);
415 Image->Height = ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *)CurrentImageBlock)->Bitmap.Height);
416 Image->Image.Bitmap = NULL;
417 return EFI_SUCCESS;
418
419 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:
420 case EFI_HII_IIBT_IMAGE_24BIT:
421 Image->Width = ReadUnaligned16 ((VOID *)&((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *)CurrentImageBlock)->Bitmap.Width);
422 Image->Height = ReadUnaligned16 ((VOID *)&((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *)CurrentImageBlock)->Bitmap.Height);
423 Image->Image.Bitmap = NULL;
424 return EFI_SUCCESS;
425
426 default:
427 return EFI_NOT_FOUND;
428 }
429 }