# Display - [Introduction](#section11660541593) - [Directory Structure](#section161941989596) - [Available APIs](#section1551164914237) - [Usage](#section129654513264) - [Repositories Involved](#section1371113476307) ## Introduction This repository defines and implements the following Hardware Driver Interfaces \(HDIs\) of the display module. The HDIs are classified into the following types: - DisplayLayer: manages display layers, including creating layers, setting display attributes, and filling and flushing layers. - DisplayGralloc: manages memory of the display module, including memory application, release, and mapping. - DisplayGfx: implements hardware acceleration, including the initialization of the hardware acceleration module, bit blit, and regular graphics drawing and filling. **Figure 1** HDI architecture of the display module ![](figures/hdi-architecture-of-the-display-module.png "hdi-architecture-of-the-display-module") ## Directory Structure The source code directory structure is as follows: ``` /drivers/peripheral/display ├── hal # HAL code │ └── default # Default HDI implementation ├── interfaces # Driver capability APIs provided for upper-layer services │ └── include # APIs exposed externally ├── test # Test code │ └── unittest # Unit test code ``` ### Available APIs The display driver provides the Window Manager Service \(WMS\) with driver capability APIs that can be classified into the **DisplayLayer**, **DisplayGralloc**, and **DisplayGfx** types. By calling these APIs, you can create or destroy a layer, set layer attributes, allocate, release, map, and buffer memory, fill in rectangles, and blit bitmaps. [Table 1](#table1513255710559) describes major HDIs provided by the display module. **Table 1** Major HDIs of the display module

Header File

API

Description

display_layer.h

int32_t (*InitDisplay)(uint32_t devId);

Initializes the layer.

int32_t (*DeinitDisplay)(uint32_t devId);

Deinitializes the layer.

int32_t (*GetDisplayInfo)(uint32_t devId, DisplayInfo *dispInfo);

Obtains the display device information.

int32_t (*OpenLayer)(uint32_t devId, const LayerInfo *layerInfo, uint32_t *layerId);

Creates a layer.

int32_t (*CloseLayer)(uint32_t devId, uint32_t layerId);

Destroys a layer.

int32_t (*SetLayerVisible)(uint32_t devId, uint32_t layerId, bool visible);

Sets whether a layer is visible.

int32_t (*SetLayerDirtyRegion)(uint32_t devId, uint32_t layerId, IRect *region);

Sets the flushing area for a layer.

int32_t (*Flush)(uint32_t devId, uint32_t layerId, LayerBuffer *buffer);

Flushes a layer.

int32_t (*GetLayerBuffer)(uint32_t devId, uint32_t layerId, LayerBuffer *buffer);

Obtains the buffer of a layer.

display_gralloc.h

int32_t AllocMem(GrallocBuffer *buffer);

Allocates memory.

void FreeMem(GrallocBuffer *buffer);

Releases memory.

void *Mmap(GrallocBuffer *buffer);

Maps memory.

void *MmapCache(GrallocBuffer *buffer);

Maps memory to memory with cache.

int32_t Unmap(GrallocBuffer *buffer);

Unmaps memory.

int32_t FlushCache(GrallocBuffer *buffer);

Flushes data from the cache to memory and invalidates the data in the cache.

int32_t FlushMCache(GrallocBuffer *buffer);

Flushes data from the cache via Mmap to memory and invalidates the data in the cache.

display_gfx.h

int32_t InitGfx(void);

Initializes hardware acceleration.

int32_t DeinitGfx(void);

Deinitializes hardware acceleration.

int32_t Blit(ISurface *srcSurface, IRect *srcRect, ISurface *dstSurface, IRect *dstRect);

Blits bitmaps.

int32_t FillRect(ISurface *surface, IRect *rect, uint32_t color, GfxOpt *opt);

Fills a rectangle with a given color on the canvas.

### Usage The core function of this repository is to provide HDIs for upper-layer display system services to implement display driver capabilities. The following sample code describes how to use the display HDIs: ``` #include "display_gfx.h" #include "display_gralloc.h" #include "display_layer.h" #include "display_type.h" #define DEVID 0 DisplayTest g_displayTest; static GrallocBuffer g_buffer; static int32_t GetDisplayInterfaces(void) { int32_t ret; ret = LayerInitialize(&g_displayTest.layerFuncs); if (ret != DISPLAY_SUCCESS || g_displayTest.layerFuncs == NULL) { HDF_LOGE("initialize layer failed"); return DISPLAY_FAILURE; } ret = GrallocInitialize(&g_displayTest.grallocFuncs); if (ret != DISPLAY_SUCCESS || g_displayTest.layerFuncs == NULL) { HDF_LOGE("initialize gralloc failed"); return DISPLAY_FAILURE; } ret = GfxInitialize(&g_displayTest.gfxFuncs); if (ret != DISPLAY_SUCCESS || g_displayTest.gfxFuncs == NULL) { HDF_LOGE("initialize gralloc failed"); return DISPLAY_FAILURE; } return DISPLAY_SUCCESS; } static int32_t DisplayUninit(void) { LayerUninitialize(g_displayTest.layerFuncs); GrallocUninitialize(g_displayTest.grallocFuncs); GfxUninitialize(g_displayTest.gfxFuncs); return DISPLAY_SUCCESS; } static void GetLayerInfo(LayerInfo *layInfo) { layInfo->width = g_displayTest.displayInfo.width; layInfo->height = g_displayTest.displayInfo.height; layInfo->bpp = LAYER_BPP; layInfo->pixFormat = PIXEL_FMT_RGBA_5551; layInfo->type = LAYER_TYPE_GRAPHIC; } static void WriteDataToBuf(int32_t width, int32_t height, uint16_t *pBuf) { int32_t x; int32_t y; for (y = ((height / LINE_WIDTH) - LINE_WIDTH); y < ((height / LINE_WIDTH) + LINE_WIDTH); y++) { for (x = 0; x < width; x++) { *((uint16_t*)pBuf + y * width + x) = HIFB_RED_1555; } } for (y = 0; y < height; y++) { for (x = ((width / LINE_WIDTH) - LINE_WIDTH); x < ((width / LINE_WIDTH) + LINE_WIDTH); x++) { *((uint16_t*)pBuf + y * width + x) = HIFB_RED_1555; } } } int DisplayServiceSample(void) { int32_t ret; g_displayTest.devId = DEVID; /* Obtain display driver APIs. */ ret = GetDisplayInterfaces(); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("get display interfaces ops failed"); return ret; } /* Initialize a display device. */ if (g_displayTest.layerFuncs->InitDisplay != NULL) { ret = g_displayTest.layerFuncs->InitDisplay(g_displayTest.devId); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("initialize display failed"); return DISPLAY_FAILURE; } } /* Obtain the display device information. */ if (g_displayTest.layerFuncs->GetDisplayInfo != NULL) { ret = g_displayTest.layerFuncs->GetDisplayInfo(g_displayTest.devId, &g_displayTest.displayInfo); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("get disp info failed"); return DISPLAY_FAILURE; } } /* Open a specific layer of the display device. */ if (g_displayTest.layerFuncs->OpenLayer != NULL) { LayerInfo layInfo; GetLayerInfo(&layInfo); ret = g_displayTest.layerFuncs->OpenLayer(g_displayTest.devId, &layInfo, &g_displayTest.layerId); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("open layer failed"); return DISPLAY_FAILURE; } } /* Obtain the buffer of the layer and add data to the buffer. */ if (g_displayTest.layerFuncs->GetLayerBuffer != NULL) { ret = g_displayTest.layerFuncs->GetLayerBuffer(g_displayTest.devId, g_displayTest.layerId, &g_displayTest.buffer); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("get layer buffer failed"); return DISPLAY_FAILURE; } uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr; WriteDataToBuf(g_displayTest.displayInfo.width, g_displayTest.displayInfo.height, pBuf); } /* Flush the layer data for display. */ if (g_displayTest.layerFuncs->Flush != NULL) { ret = g_displayTest.layerFuncs->Flush(g_displayTest.devId, g_displayTest.layerId, &g_displayTest.buffer); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("flush layer failed"); return DISPLAY_FAILURE; } } /* Close the specified layer of the display device. */ if (g_displayTest.layerFuncs->CloseLayer != NULL) { ret = g_displayTest.layerFuncs->CloseLayer(g_displayTest.devId, g_displayTest.layerId); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("close layer failed"); return DISPLAY_FAILURE; } } /* Close the display device. */ if (g_displayTest.layerFuncs->DeinitDisplay != NULL) { ret = g_displayTest.layerFuncs->DeinitDisplay(g_displayTest.devId); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("deinit display failed"); return DISPLAY_FAILURE; } } /* Unregister display driver APIs. */ ret = DisplayUninit(); if (ret != DISPLAY_SUCCESS) { HDF_LOGE("DisplayUninit fail"); return ret; } return 0; } ``` ## Repositories Involved [Driver subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md) [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) [drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md) [drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral)