• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "video_layer_service.h"
17 #include <hdf_log.h>
18 #include <hdf_base.h>
19 #include "display_gralloc.h"
20 #include "display_layer.h"
21 
22 namespace OHOS {
23 namespace HDI {
24 namespace Display {
25 namespace V1_0 {
26 static LayerFuncs *g_layerFuncs = nullptr;
27 static GrallocFuncs *g_grallocFuncs = nullptr;
28 
InitDisplay(unsigned int devId)29 DispErrCode VideoLayerService::InitDisplay(unsigned int devId)
30 {
31     if (g_layerFuncs != nullptr) {
32         return DISPLAY_SUCCESS;
33     }
34 
35     int32_t ret = LayerInitialize(&g_layerFuncs);
36     if (ret != DISPLAY_SUCCESS || g_layerFuncs == nullptr) {
37         HDF_LOGE("layer init fail, ret=%{public}d",  ret);
38         return DISPLAY_FAILURE;
39     }
40 
41     if (g_layerFuncs->InitDisplay == nullptr || g_layerFuncs->CreateLayer == nullptr ||
42         g_layerFuncs->GetDisplayInfo == nullptr) {
43         HDF_LOGE("layer func is invalid");
44         return DISPLAY_FAILURE;
45     }
46 
47     ret = g_layerFuncs->InitDisplay(devId);
48     if (ret != DISPLAY_SUCCESS) {
49         HDF_LOGE("init display fail, ret=%{public}d",  ret);
50         return DISPLAY_FAILURE;
51     }
52 
53     if (g_grallocFuncs == nullptr) {
54         ret = GrallocInitialize(&g_grallocFuncs);
55         if (ret != DISPLAY_SUCCESS || g_grallocFuncs == nullptr) {
56             LayerUninitialize(g_layerFuncs);
57             g_layerFuncs = nullptr;
58             HDF_LOGE("%{public}s: GrallocInitialize fail, ret=%{public}d", __func__, ret);
59             return DISPLAY_FAILURE;
60         }
61     }
62     return DISPLAY_SUCCESS;
63 }
64 
DeinitDisplay(unsigned int devId)65 DispErrCode VideoLayerService::DeinitDisplay(unsigned int devId)
66 {
67     if (g_layerFuncs != nullptr && g_layerFuncs->DeinitDisplay != nullptr) {
68         (void)g_layerFuncs->DeinitDisplay(devId);
69     }
70     return DISPLAY_SUCCESS;
71 }
72 
GetDisplayInfo(unsigned int devId,std::shared_ptr<DisplayInfo> & dispInfo)73 DispErrCode VideoLayerService::GetDisplayInfo(unsigned int devId, std::shared_ptr<DisplayInfo> &dispInfo)
74 {
75     if (g_layerFuncs == nullptr) {
76         HDF_LOGE("video display not inited");
77         return DISPLAY_FAILURE;
78     }
79 
80     int32_t ret = g_layerFuncs->GetDisplayInfo(devId, dispInfo.get());
81     if (ret != DISPLAY_SUCCESS) {
82         HDF_LOGE("get display info fail, ret=%{public}d",  ret);
83         return DISPLAY_FAILURE;
84     }
85 
86     return DISPLAY_SUCCESS;
87 }
88 
CreateLayer(unsigned int devId,LayerInfo & layerInfo,unsigned int & layerId)89 DispErrCode VideoLayerService::CreateLayer(unsigned int devId, LayerInfo &layerInfo, unsigned int &layerId)
90 {
91     int32_t ret;
92     LayerInfo info = layerInfo;
93     if (g_layerFuncs == nullptr) {
94         ret = InitDisplay(devId);
95         if (ret != DISPLAY_SUCCESS) {
96             HDF_LOGE("InitDisplay fail, ret=%{public}d",  ret);
97             return DISPLAY_FAILURE;
98         }
99         std::shared_ptr<DisplayInfo> dispInfo = std::make_shared<DisplayInfo>();
100         ret = GetDisplayInfo(devId, dispInfo);
101         if (ret != DISPLAY_SUCCESS) {
102             HDF_LOGE("InitDisplay fail, ret=%{public}d",  ret);
103             return DISPLAY_FAILURE;
104         }
105         if (info.height == 0 && info.width == 0) {
106             info.width = dispInfo->width;
107             info.height = dispInfo->height;
108         }
109     }
110 
111     ret = g_layerFuncs->CreateLayer(devId, &info, &layerId);
112     if (ret != DISPLAY_SUCCESS) {
113         HDF_LOGE("create layer fail, ret=%{public}d",  ret);
114         return DISPLAY_FAILURE;
115     }
116     return DISPLAY_SUCCESS;
117 }
118 
CloseLayer(unsigned int devId,unsigned int layerId)119 DispErrCode VideoLayerService::CloseLayer(unsigned int devId, unsigned int layerId)
120 {
121     if (g_layerFuncs == nullptr || g_layerFuncs->CloseLayer == nullptr) {
122         HDF_LOGE("may not inited or CloseLayer nullptr");
123         return DISPLAY_FAILURE;
124     }
125     (void)g_layerFuncs->CloseLayer(devId, layerId);
126     return DISPLAY_SUCCESS;
127 }
128 
SetLayerVisible(unsigned int devId,unsigned int layerId,bool visible)129 DispErrCode VideoLayerService::SetLayerVisible(unsigned int devId, unsigned int layerId, bool visible)
130 {
131     if (g_layerFuncs == nullptr || g_layerFuncs->SetLayerVisible == nullptr) {
132         HDF_LOGE("may not inited or SetLayerVisible nullptr");
133         return DISPLAY_FAILURE;
134     }
135     int32_t ret = g_layerFuncs->SetLayerVisible(devId, layerId, visible);
136     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
137 }
138 
GetLayerVisibleState(unsigned int devId,unsigned int layerId,bool & visible)139 DispErrCode VideoLayerService::GetLayerVisibleState(unsigned int devId, unsigned int layerId, bool &visible)
140 {
141     if (g_layerFuncs == nullptr || g_layerFuncs->GetLayerVisibleState == nullptr) {
142         HDF_LOGE("may not inited or GetLayerVisibleState nullptr");
143         return DISPLAY_FAILURE;
144     }
145     int32_t ret = g_layerFuncs->GetLayerVisibleState(devId, layerId, &visible);
146     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
147 }
148 
SetLayerRect(unsigned int devId,unsigned int layerId,IRect & rect)149 DispErrCode VideoLayerService::SetLayerRect(unsigned int devId, unsigned int layerId, IRect &rect)
150 {
151     if (g_layerFuncs == nullptr || g_layerFuncs->SetLayerSize == nullptr) {
152         HDF_LOGE("may not inited or SetLayerSize nullptr");
153         return DISPLAY_FAILURE;
154     }
155     int32_t ret = g_layerFuncs->SetLayerSize(devId, layerId, &rect);
156     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
157 }
158 
GetLayerRect(unsigned int devId,unsigned int layerId,std::shared_ptr<IRect> & rect)159 DispErrCode VideoLayerService::GetLayerRect(unsigned int devId, unsigned int layerId, std::shared_ptr<IRect> &rect)
160 {
161     if (g_layerFuncs == nullptr || g_layerFuncs->GetLayerSize == nullptr) {
162         HDF_LOGE("may not inited or GetLayerSize nullptr");
163         return DISPLAY_FAILURE;
164     }
165     int32_t ret = g_layerFuncs->GetLayerSize(devId, layerId, rect.get());
166 
167     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
168 }
169 
SetLayerZorder(unsigned int devId,unsigned int layerId,unsigned int zorder)170 DispErrCode VideoLayerService::SetLayerZorder(unsigned int devId, unsigned int layerId, unsigned int zorder)
171 {
172     if (g_layerFuncs == nullptr || g_layerFuncs->SetLayerZorder == nullptr) {
173         HDF_LOGE("may not inited or SetLayerZorder nullptr");
174         return DISPLAY_FAILURE;
175     }
176     int32_t ret = g_layerFuncs->SetLayerZorder(devId, layerId, zorder);
177     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
178 }
179 
GetLayerZorder(unsigned int devId,unsigned int layerId,unsigned int & zorder)180 DispErrCode VideoLayerService::GetLayerZorder(unsigned int devId, unsigned int layerId, unsigned int &zorder)
181 {
182     if (g_layerFuncs == nullptr || g_layerFuncs->GetLayerZorder == nullptr) {
183         HDF_LOGE("may not inited or GetLayerZorder nullptr");
184         return DISPLAY_FAILURE;
185     }
186     int32_t ret = g_layerFuncs->GetLayerZorder(devId, layerId, &zorder);
187     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
188 }
189 
SetTransformMode(unsigned int devId,unsigned int layerId,TransformType & type)190 DispErrCode VideoLayerService::SetTransformMode(unsigned int devId, unsigned int layerId, TransformType &type)
191 {
192     if (g_layerFuncs == nullptr || g_layerFuncs->SetTransformMode == nullptr) {
193         HDF_LOGE("may not inited or SetTransformMode nullptr");
194         return DISPLAY_FAILURE;
195     }
196     int32_t ret = g_layerFuncs->SetTransformMode(devId, layerId, type);
197     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
198 }
199 
SetLayerBuffer(unsigned int devId,unsigned int layerId,const BufferHandle & buffer,int fence)200 DispErrCode VideoLayerService::SetLayerBuffer(unsigned int devId, unsigned int layerId, const BufferHandle &buffer,
201     int fence)
202 {
203     if (g_layerFuncs == nullptr || g_layerFuncs->SetLayerBuffer == nullptr) {
204         HDF_LOGE("may not inited or SetLayerBuffer nullptr");
205         return DISPLAY_FAILURE;
206     }
207     BufferHandle bufferTemp = buffer;
208     bufferTemp.virAddr = g_grallocFuncs->Mmap(&bufferTemp);
209     int32_t ret = g_layerFuncs->SetLayerBuffer(devId, layerId, &bufferTemp, fence);
210     (void)g_grallocFuncs->Unmap(&bufferTemp);
211     return (ret == DISPLAY_SUCCESS) ? DISPLAY_SUCCESS : DISPLAY_FAILURE;
212 }
213 } // namespace V1_0
214 } // namespace Display
215 } // namespace HDI
216 } // namespace OHOS
217