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 "display_test.h"
17#include <stdio.h>
18#include "securec.h"
19#include "hdf_log.h"
20#include "loadbmp_test.h"
21
22DisplayTest g_displayTest;
23static BufferHandle* g_buffer;
24
25static int32_t LoadBmp(const int8_t *fileName, BufferHandle **buffer)
26{
27    int32_t ret;
28    uint8_t *pBuf = NULL;
29    OsdSurface surface;
30    OsdBitMapFileHeader bmpFileHeader = {0};
31    OsdBitMapInfo bmpInfo = {0};
32
33    if (fileName == NULL) {
34        HDF_LOGE("%s: fileName is null", __func__);
35        return DISPLAY_FAILURE;
36    }
37    if (GetBmpInfo(fileName, &bmpFileHeader, &bmpInfo) < 0) {
38        HDF_LOGE("%s: GetBmpInfo err", __func__);
39        return DISPLAY_FAILURE;
40    }
41    AllocInfo info = {
42        .width = bmpInfo.header.width + 1,
43        .height = bmpInfo.header.height + 1,
44        .format = PIXEL_FMT_RGBA_8888,
45        .usage =  HBM_USE_MEM_MMZ
46    };
47    // alloc picture buffer
48    if (g_displayTest.grallocFuncs->AllocMem != NULL) {
49        ret = g_displayTest.grallocFuncs->AllocMem(&info, buffer);
50        if (ret != DISPLAY_SUCCESS) {
51            HDF_LOGE("%s: pictureBuf alloc failure", __func__);
52            return DISPLAY_FAILURE;
53        }
54    }
55    // load bmp picture
56    pBuf = (uint8_t *)(*buffer)->virAddr;
57    surface.colorFmt = OSD_COLOR_FMT_RGB1555;
58    CreateSurfaceByBitMap(fileName, &surface, pBuf, (*buffer)->size);
59    return DISPLAY_SUCCESS;
60}
61
62static void GetLayerInfo(LayerInfo *layInfo)
63{
64    layInfo->width = (uint32_t)g_displayTest.displayInfo.width;
65    layInfo->height = (uint32_t)g_displayTest.displayInfo.height;
66    layInfo->bpp = LAYER_BPP;
67    layInfo->pixFormat = PIXEL_FMT_RGBA_5551;
68    layInfo->type = LAYER_TYPE_GRAPHIC;
69}
70
71static void WriteDataToBuf(int32_t width, int32_t height, uint16_t *pBuf)
72{
73    int32_t x;
74    int32_t y;
75
76    for (y = ((height / LINE_WIDTH) - LINE_WIDTH); y < ((height / LINE_WIDTH) + LINE_WIDTH); y++) {
77        for (x = 0; x < width; x++) {
78            *((uint16_t*)pBuf + y * width + x) = HIFB_RED_1555;
79        }
80    }
81    for (y = 0; y < height; y++) {
82        for (x = ((width / LINE_WIDTH) - LINE_WIDTH); x < ((width / LINE_WIDTH) + LINE_WIDTH); x++) {
83            *((uint16_t*)pBuf + y * width + x) = HIFB_RED_1555;
84        }
85    }
86}
87
88static void PicSourceSurfaceInit(ISurface *surface, uint64_t phyAddr, int32_t bpp)
89{
90    surface->width = SAMPLE_IMAGE_WIDTH;
91    surface->height = SAMPLE_IMAGE_HEIGHT;
92    surface->phyAddr = phyAddr;
93    surface->enColorFmt = PIXEL_FMT_RGBA_5551;
94    surface->stride = SAMPLE_IMAGE_WIDTH * bpp / BITS_PER_BYTE;
95    surface->bAlphaExt1555 = true;
96    surface->bAlphaMax255  = true;
97    surface->alpha0 = 0XFF;
98    surface->alpha1 = 0XFF;
99}
100
101static void DestSurfaceInit(ISurface *surface, uint64_t phyAddr, int32_t bpp)
102{
103    surface->width = (uint32_t)g_displayTest.displayInfo.width;
104    surface->height = (uint32_t)g_displayTest.displayInfo.height;
105    surface->phyAddr = phyAddr;
106    surface->enColorFmt = PIXEL_FMT_RGBA_5551;
107    surface->stride = g_displayTest.displayInfo.width * bpp / BITS_PER_BYTE;
108    surface->bAlphaExt1555 = true;
109    surface->bAlphaMax255  = true;
110    surface->alpha0 = 0XFF;
111    surface->alpha1 = 0XFF;
112    surface->bYCbCrClut = 0;
113}
114
115int32_t DisplayInit(void)
116{
117    int32_t ret;
118
119    ret = LayerInitialize(&g_displayTest.layerFuncs);
120    if (ret != DISPLAY_SUCCESS || g_displayTest.layerFuncs == NULL) {
121        HDF_LOGE("%s: layer initialize failure", __func__);
122        return DISPLAY_FAILURE;
123    }
124    ret = GrallocInitialize(&g_displayTest.grallocFuncs);
125    if (ret != DISPLAY_SUCCESS || g_displayTest.layerFuncs == NULL) {
126        HDF_LOGE("%s: gralloc initialize failure", __func__);
127        return DISPLAY_FAILURE;
128    }
129    ret = GfxInitialize(&g_displayTest.gfxFuncs);
130    if (ret != DISPLAY_SUCCESS || g_displayTest.gfxFuncs == NULL) {
131        HDF_LOGE("%s: gfx initialize failure", __func__);
132        return DISPLAY_FAILURE;
133    }
134    return DISPLAY_SUCCESS;
135}
136
137int32_t DisplayUninit(void)
138{
139    LayerUninitialize(g_displayTest.layerFuncs);
140    GrallocUninitialize(g_displayTest.grallocFuncs);
141    GfxUninitialize(g_displayTest.gfxFuncs);
142    return DISPLAY_SUCCESS;
143}
144
145int32_t InitDisplayTest(void)
146{
147    int32_t ret;
148
149    g_displayTest.devId = 0;
150    // init display
151    if (g_displayTest.layerFuncs->InitDisplay != NULL) {
152        ret = g_displayTest.layerFuncs->InitDisplay(g_displayTest.devId);
153        if (ret != DISPLAY_SUCCESS) {
154            HDF_LOGE("%s: MPI VO can't be opened repeatedly, so here bypass ret check", __func__);
155            return DISPLAY_SUCCESS;
156        }
157    }
158    return DISPLAY_SUCCESS;
159}
160
161int32_t DeinitDisplayTest(void)
162{
163    int32_t ret = DISPLAY_SUCCESS;
164
165    g_displayTest.devId = 0;
166    // deinit display
167    if (g_displayTest.layerFuncs->DeinitDisplay != NULL) {
168        HDF_LOGE("%s: Here bypass call DeinitDisplay func for keep test environment continuable", __func__);
169        if (ret != DISPLAY_SUCCESS) {
170            HDF_LOGE("%s: deinit display failed", __func__);
171            return DISPLAY_FAILURE;
172        }
173    }
174    return DISPLAY_SUCCESS;
175}
176
177int32_t GetDisplayInfoTest(void)
178{
179    int32_t ret;
180
181    g_displayTest.devId = 0;
182    // get display info
183    if (g_displayTest.layerFuncs->GetDisplayInfo != NULL) {
184        ret = g_displayTest.layerFuncs->GetDisplayInfo(g_displayTest.devId, &g_displayTest.displayInfo);
185        if (ret != DISPLAY_SUCCESS) {
186            HDF_LOGE("%s: get disp info failed", __func__);
187            return DISPLAY_FAILURE;
188        }
189    }
190    HDF_LOGI("%s: dispInfo width = %d", __func__, g_displayTest.displayInfo.width);
191    HDF_LOGI("%s: dispInfo height = %d", __func__, g_displayTest.displayInfo.height);
192    HDF_LOGI("%s: dispInfo rotAngle = %d", __func__, g_displayTest.displayInfo.rotAngle);
193    return DISPLAY_SUCCESS;
194}
195
196int32_t CreateLayerTest(void)
197{
198    int32_t ret;
199    LayerInfo layInfo;
200
201    g_displayTest.devId = 0;
202    // create layer
203    GetLayerInfo(&layInfo);
204    if (g_displayTest.layerFuncs->CreateLayer != NULL) {
205        ret = g_displayTest.layerFuncs->CreateLayer(g_displayTest.devId, &layInfo, &g_displayTest.layerId);
206        if (ret != DISPLAY_SUCCESS) {
207            HDF_LOGE("%s: CreateLayer fail", __func__);
208            return DISPLAY_FAILURE;
209        }
210    }
211    return DISPLAY_SUCCESS;
212}
213
214int32_t CloseLayerTest(void)
215{
216    int32_t ret;
217
218    if (g_displayTest.layerFuncs->CloseLayer != NULL) {
219        ret = g_displayTest.layerFuncs->CloseLayer(g_displayTest.devId, g_displayTest.layerId);
220        if (ret != DISPLAY_SUCCESS) {
221            HDF_LOGE("%s: CloseLayer fail", __func__);
222            return DISPLAY_FAILURE;
223        }
224    }
225    return DISPLAY_SUCCESS;
226}
227
228int32_t SetLayerVisibleTest(void)
229{
230    int32_t ret;
231
232    // set layer visible
233    if (g_displayTest.layerFuncs->SetLayerVisible != NULL) {
234        ret = g_displayTest.layerFuncs->SetLayerVisible(g_displayTest.devId, g_displayTest.layerId, true);
235        if (ret != DISPLAY_SUCCESS) {
236            HDF_LOGE("%s: SetLayerVisible fail", __func__);
237            return DISPLAY_FAILURE;
238        }
239    }
240    return DISPLAY_SUCCESS;
241}
242
243int32_t SetLayerDirtyRegionTest(void)
244{
245    int32_t ret;
246    IRect rect = {0, 0, g_displayTest.displayInfo.width, g_displayTest.displayInfo.height};
247
248    // set refresh region
249    if (g_displayTest.layerFuncs->SetLayerDirtyRegion != NULL) {
250        ret = g_displayTest.layerFuncs->SetLayerDirtyRegion(g_displayTest.devId, g_displayTest.layerId, &rect);
251        if (ret != DISPLAY_SUCCESS) {
252            HDF_LOGE("%s: SetLayerDirtyRegion fail", __func__);
253            return DISPLAY_FAILURE;
254        }
255    }
256    return DISPLAY_SUCCESS;
257}
258
259int32_t GetLayerBufferTest(void)
260{
261    int32_t ret;
262
263    if (g_displayTest.layerFuncs->GetLayerBuffer != NULL) {
264        ret = g_displayTest.layerFuncs->GetLayerBuffer(g_displayTest.devId, g_displayTest.layerId,
265                                                       &g_displayTest.buffer);
266        if (ret != DISPLAY_SUCCESS) {
267            HDF_LOGE("%s: GetLayerBuffer fail", __func__);
268            return DISPLAY_FAILURE;
269        }
270    }
271    // write buffer data
272    uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr;
273    WriteDataToBuf(g_displayTest.displayInfo.width, g_displayTest.displayInfo.height, pBuf);
274    return DISPLAY_SUCCESS;
275}
276
277int32_t FlushTest(void)
278{
279    int32_t ret;
280
281    // refresh layer to display
282    if (g_displayTest.layerFuncs->Flush != NULL) {
283        ret = g_displayTest.layerFuncs->Flush(g_displayTest.devId, g_displayTest.layerId, &g_displayTest.buffer);
284        if (ret != DISPLAY_SUCCESS) {
285            HDF_LOGE("%s: flush fail", __func__);
286            return DISPLAY_FAILURE;
287        }
288    }
289    return DISPLAY_SUCCESS;
290}
291
292int32_t InitGfxTest(void)
293{
294    int32_t ret;
295    // init gfx
296    if (g_displayTest.gfxFuncs->InitGfx != NULL) {
297        ret = g_displayTest.gfxFuncs->InitGfx();
298        if (ret != DISPLAY_SUCCESS) {
299            HDF_LOGE("%s: init gfx fail", __func__);
300            return DISPLAY_FAILURE;
301        }
302    }
303    return DISPLAY_SUCCESS;
304}
305
306int32_t DeinitGfxTest(void)
307{
308    int32_t ret;
309    // deinit gfx
310    if (g_displayTest.gfxFuncs->DeinitGfx != NULL) {
311        ret = g_displayTest.gfxFuncs->DeinitGfx();
312        if (ret != DISPLAY_SUCCESS) {
313            HDF_LOGE("%s: deinit gfx fail", __func__);
314            return DISPLAY_FAILURE;
315        }
316    }
317    return DISPLAY_SUCCESS;
318}
319
320int32_t BlitTest(void)
321{
322    int32_t ret;
323    ISurface srcSurface = {0};
324    ISurface dstSurface = {0};
325    BufferHandle* pictureBuf = NULL;
326    uint32_t layerBufSize = g_displayTest.displayInfo.width * g_displayTest.displayInfo.height * PIXEL_BYTE;
327
328    // clean the layer buffer
329    (void)memset_s(g_displayTest.buffer.data.virAddr, layerBufSize, 0, layerBufSize);
330    // load bmp test picture
331    ret = LoadBmp((const int8_t *)PIC_RES_PATH, &pictureBuf);
332    if (ret != DISPLAY_SUCCESS) {
333        HDF_LOGE("%s: LoadBmp fail", __func__);
334        return DISPLAY_FAILURE;
335    }
336    // use picture buffer to create source surface
337    IRect srcRect = {0, 0, SAMPLE_IMAGE_WIDTH, SAMPLE_IMAGE_HEIGHT};
338    PicSourceSurfaceInit(&srcSurface, pictureBuf->phyAddr, LAYER_BPP);
339    // use layer buffer to create dest surface
340    IRect dstRect = srcRect;
341    DestSurfaceInit(&dstSurface, g_displayTest.buffer.data.phyAddr, LAYER_BPP);
342    // TDE: copy bmp picture buffer to layer buffer
343    if (g_displayTest.gfxFuncs->Blit != NULL) {
344        ret = g_displayTest.gfxFuncs->Blit(&srcSurface, &srcRect, &dstSurface, &dstRect, NULL);
345        if (ret != DISPLAY_SUCCESS) {
346            HDF_LOGE("%s: Blit fail", __func__);
347            goto EXIT;
348        }
349    }
350    return DISPLAY_SUCCESS;
351
352EXIT:
353    /* free picture buffer */
354    if (g_displayTest.grallocFuncs->FreeMem != NULL) {
355        g_displayTest.grallocFuncs->FreeMem(pictureBuf);
356    }
357    return ret;
358}
359
360int32_t FillRectTest(void)
361{
362    int32_t ret;
363    ISurface dstSurface = {0};
364    GfxOpt opt = {0};
365    IRect rect = {0, 0, SAMPLE_RECT_WIDTH, SAMPLE_RECT_HEIGHT};
366    uint32_t layerBufSize = g_displayTest.displayInfo.width * g_displayTest.displayInfo.height * PIXEL_BYTE;
367
368    opt.enGlobalAlpha = true;
369    opt.globalAlpha = MAX_GLOBLE_ALPHA;
370    // clean the layer buffer
371    (void)memset_s(g_displayTest.buffer.data.virAddr, layerBufSize, 0, layerBufSize);
372    DestSurfaceInit(&dstSurface, g_displayTest.buffer.data.phyAddr, LAYER_BPP);
373    // TDE: copy bmp picture buffer to layer buffer
374    if (g_displayTest.gfxFuncs->FillRect != NULL) {
375        ret = g_displayTest.gfxFuncs->FillRect(&dstSurface, &rect, HIFB_RED_1555, &opt);
376        if (ret != DISPLAY_SUCCESS) {
377            HDF_LOGE("%s: FillRect fail", __func__);
378            return ret;
379        }
380    }
381    return DISPLAY_SUCCESS;
382}
383
384int32_t AllocMemTest1(void)
385{
386    int32_t ret = DISPLAY_FAILURE;
387
388    AllocInfo info = {
389        .width = SAMPLE_IMAGE_WIDTH,
390        .height = SAMPLE_IMAGE_HEIGHT,
391        .format = PIXEL_FMT_RGBA_8888,
392        .usage =  HBM_USE_MEM_MMZ
393    };
394    if (g_displayTest.grallocFuncs->AllocMem != NULL) {
395        ret = g_displayTest.grallocFuncs->AllocMem(&info, &g_buffer);
396    }
397    if (ret != DISPLAY_SUCCESS) {
398        HDF_LOGE("%s: normal memory allocMem failed", __func__);
399        return ret;
400    }
401    return DISPLAY_SUCCESS;
402}
403
404int32_t MmapCacheTest(void)
405{
406    int32_t ret = DISPLAY_FAILURE;
407    void *mapCacheAddr = NULL;
408    AllocInfo info = {
409        .width = SAMPLE_RECT_WIDTH,
410        .height = SAMPLE_RECT_HEIGHT,
411        .format = PIXEL_FMT_RGBA_8888,
412        .usage =  HBM_USE_MEM_MMZ_CACHE
413    };
414
415    if (g_displayTest.grallocFuncs->AllocMem != NULL) {
416        ret = g_displayTest.grallocFuncs->AllocMem(&info, &g_buffer);
417    }
418    if (ret != DISPLAY_SUCCESS) {
419        HDF_LOGE("%s: normal memory allocMem failed", __func__);
420        return ret;
421    }
422
423    if (g_displayTest.grallocFuncs->MmapCache != NULL) {
424        mapCacheAddr = g_displayTest.grallocFuncs->MmapCache(g_buffer);
425        if (mapCacheAddr == NULL) {
426            return DISPLAY_FAILURE;
427        }
428    }
429    return DISPLAY_SUCCESS;
430}
431
432int32_t FreeMemTest(void)
433{
434    if (g_displayTest.grallocFuncs->FreeMem != NULL) {
435        g_displayTest.grallocFuncs->FreeMem(g_buffer);
436    }
437    return DISPLAY_SUCCESS;
438}
439
440int32_t AllocMemTest2(void)
441{
442    int32_t ret = DISPLAY_FAILURE;
443    AllocInfo info = {
444        .width = SAMPLE_IMAGE_WIDTH,
445        .height = SAMPLE_IMAGE_HEIGHT,
446        .format = PIXEL_FMT_RGBA_8888,
447        .usage =  HBM_USE_MEM_MMZ
448    };
449
450    if (g_displayTest.grallocFuncs->AllocMem != NULL) {
451        ret = g_displayTest.grallocFuncs->AllocMem(&info, &g_buffer);
452    }
453    if (ret != DISPLAY_SUCCESS) {
454        HDF_LOGE("%s: cache memory allocMem failed", __func__);
455        return ret;
456    }
457
458    return DISPLAY_SUCCESS;
459}
460
461int32_t FlushMCacheTest(void)
462{
463    int32_t ret;
464
465    if (g_displayTest.grallocFuncs->FlushMCache != NULL) {
466        ret = g_displayTest.grallocFuncs->FlushMCache(g_buffer);
467        if (ret != DISPLAY_SUCCESS) {
468            return ret;
469        }
470    }
471    return DISPLAY_SUCCESS;
472}
473