1 /*
2  * Copyright (c) 2022 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 #include <iremote_proxy.h>
16 #include "display_layer.h"
17 #include "idisplay_layer.h"
18 #include "display_common.h"
19 using namespace OHOS::HDI::Display::V1_0;
20 static OHOS::sptr<IDisplayLayer> g_layerService = nullptr;
21 constexpr const char *DISPLAY_LAYER_SERVICE_NAME = "hdi_video_layer_service";
22 
InitDisplayIfNeed(void)23 static int32_t InitDisplayIfNeed(void)
24 {
25     DISPLAY_LOGD();
26     if (g_layerService == nullptr) {
27         g_layerService = IDisplayLayer::Get(DISPLAY_LAYER_SERVICE_NAME);
28         if (g_layerService == nullptr) {
29             DISPLAY_LOGE("get layer service failed");
30             return DISPLAY_FAILURE;
31         }
32         int32_t ret = g_layerService->InitDisplay(0);
33         if (ret != DISPLAY_SUCCESS) {
34             DISPLAY_LOGE("init display fail, ret=%{public}d",  ret);
35             return DISPLAY_FAILURE;
36         }
37     }
38     return DISPLAY_SUCCESS;
39 }
40 
DeInitDisplay(void)41 static int32_t DeInitDisplay(void)
42 {
43     if (g_layerService != nullptr) {
44         return g_layerService->DeinitDisplay(0);
45     }
46     return DISPLAY_SUCCESS;
47 }
48 
49 
CreateLayer(uint32_t devId,const LayerInfo * layerInfo,uint32_t * layerId)50 static int32_t CreateLayer(uint32_t devId, const LayerInfo *layerInfo, uint32_t *layerId)
51 {
52     DISPLAY_LOGD();
53     DISPLAY_CHK_RETURN((layerInfo == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("layerInfo is nullptr"));
54     DISPLAY_CHK_RETURN((layerId == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("layerId is nullptr"));
55     LayerInfo info = *layerInfo;
56     int32_t ret = InitDisplayIfNeed();
57     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("display init failed"));
58     return g_layerService->CreateLayer(devId, info, *layerId);
59 }
60 
CloseLayer(uint32_t devId,uint32_t layerId)61 static int32_t CloseLayer(uint32_t devId, uint32_t layerId)
62 {
63     DISPLAY_LOGD();
64     g_layerService->CloseLayer(devId, layerId);
65     return DISPLAY_SUCCESS;
66 }
67 
SetLayerSize(uint32_t devId,uint32_t layerId,IRect * rect)68 static int32_t SetLayerSize(uint32_t devId, uint32_t layerId, IRect *rect)
69 {
70     DISPLAY_LOGD();
71     DISPLAY_CHK_RETURN((rect == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("rect is nullptr"));
72     g_layerService->SetLayerRect(devId, layerId, *rect);
73     return DISPLAY_SUCCESS;
74 }
75 
SetLayerCrop(uint32_t devId,uint32_t layerId,IRect * rect)76 static int32_t SetLayerCrop(uint32_t devId, uint32_t layerId, IRect *rect)
77 {
78     DISPLAY_LOGD();
79     return DISPLAY_SUCCESS;
80 }
81 
SetLayerZorder(uint32_t devId,uint32_t layerId,uint32_t zorder)82 static int32_t SetLayerZorder(uint32_t devId, uint32_t layerId, uint32_t zorder)
83 {
84     DISPLAY_LOGD();
85     g_layerService->SetLayerZorder(devId, layerId, zorder);
86     return DISPLAY_SUCCESS;
87 }
88 
SetTransformMode(uint32_t devId,uint32_t layerId,TransformType type)89 static int32_t SetTransformMode(uint32_t devId, uint32_t layerId, TransformType type)
90 {
91     DISPLAY_LOGD();
92     g_layerService->SetTransformMode(devId, layerId, type);
93     return DISPLAY_SUCCESS;
94 }
95 
SetLayerBuffer(uint32_t devId,uint32_t layerId,const BufferHandle * buffer,int32_t fence)96 static int32_t SetLayerBuffer(uint32_t devId, uint32_t layerId, const BufferHandle *buffer, int32_t fence)
97 {
98     DISPLAY_LOGD();
99     DISPLAY_CHK_RETURN((buffer == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("buffer is nullptr"));
100     g_layerService->SetLayerBuffer(devId, layerId, *buffer, fence);
101     return DISPLAY_SUCCESS;
102 }
103 extern "C" {
LayerInitialize(LayerFuncs ** funcs)104 int32_t LayerInitialize(LayerFuncs **funcs)
105 {
106     DISPLAY_LOGD();
107     DISPLAY_CHK_RETURN((funcs == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("funcs is nullptr"));
108     LayerFuncs *lFunc = reinterpret_cast<LayerFuncs *>(calloc(1, sizeof(LayerFuncs)));
109     if (lFunc == nullptr) {
110         DISPLAY_LOGE("can not calloc LayerFuncs");
111         return DISPLAY_FAILURE;
112     }
113     lFunc->CreateLayer = CreateLayer;
114     lFunc->CloseLayer = CloseLayer;
115     lFunc->SetLayerSize = SetLayerSize;
116     lFunc->SetLayerCrop = SetLayerCrop;
117     lFunc->SetLayerZorder = SetLayerZorder;
118     lFunc->SetTransformMode = SetTransformMode;
119     lFunc->SetLayerBuffer = SetLayerBuffer;
120     *funcs = lFunc;
121     DISPLAY_LOGD("LayerInitialize success");
122     return DISPLAY_SUCCESS;
123 }
124 
LayerUninitialize(LayerFuncs * funcs)125 int32_t LayerUninitialize(LayerFuncs *funcs)
126 {
127     DISPLAY_CHK_RETURN((funcs == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("funcs is nullptr"));
128     DISPLAY_LOGD();
129     free(funcs);
130     int32_t ret = DeInitDisplay();
131     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_LOGE("uninitialize failed"));
132     return DISPLAY_SUCCESS;
133 }
134 }
135