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 "videodisplaymanager.h" 17 #include <hilog/log.h> 18 #include <iremote_proxy.h> 19 #include "surface_buffer_impl.h" 20 #include "idisplay_layer.h" 21 22 using namespace OHOS::HDI::Display::V1_0; 23 static OHOS::sptr<IDisplayLayer> g_layerService = nullptr; 24 constexpr const char *DISPLAY_LAYER_SERVICE_NAME = "hdi_video_layer_service"; 25 26 namespace { 27 static constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0, "VideoDisplayManager" }; 28 } 29 30 #define __VLOG(func, fmt, ...) \ 31 func(LABEL, "%{public}s: " fmt , __func__, ##__VA_ARGS__) 32 33 #define VLOGFD(fmt, ...) __VLOG(::OHOS::HiviewDFX::HiLog::Debug, fmt, ##__VA_ARGS__) 34 #define VLOGFI(fmt, ...) __VLOG(::OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__) 35 #define VLOGFW(fmt, ...) __VLOG(::OHOS::HiviewDFX::HiLog::Warn, fmt, ##__VA_ARGS__) 36 #define VLOGFE(fmt, ...) __VLOG(::OHOS::HiviewDFX::HiLog::Error, fmt, ##__VA_ARGS__) 37 38 #ifndef VIDEO_DISPLAY_DEBUG 39 #define VIDEO_DISPLAY_ENTER() ((void)0) 40 #define VIDEO_DISPLAY_EXIT() ((void)0) 41 #else 42 #define VIDEO_DISPLAY_ENTER() do { \ 43 VLOGFD("enter..."); \ 44 } while (0) 45 46 #define VIDEO_DISPLAY_EXIT() do { \ 47 VLOGFD("exit..."); \ 48 } while (0) 49 #endif 50 51 namespace OHOS { ~Listener()52 Listener::~Listener() 53 { 54 if (surface_ != nullptr && preBuffer != nullptr) { 55 SurfaceError ret = surface_->ReleaseBuffer(preBuffer, -1); 56 if (ret != SURFACE_ERROR_OK) { 57 VLOGFE("release buffer fail, ret=%{public}d", ret); 58 } 59 preBuffer = nullptr; 60 } 61 } 62 OnBufferAvailable()63 void Listener::OnBufferAvailable() 64 { 65 sptr<SurfaceBuffer> buffer; 66 sptr<SurfaceBufferImpl> bufferImpl; 67 int32_t fence; 68 SurfaceError ret; 69 70 VIDEO_DISPLAY_ENTER(); 71 auto surfacTemp = surface_.promote(); 72 if (surfacTemp == nullptr) { 73 VLOGFE("surface is null"); 74 return; 75 } 76 ret = surfacTemp->AcquireBuffer(buffer, fence, timestamp, damage); 77 if (ret != SURFACE_ERROR_OK) { 78 VLOGFE("acquire buffer fail, ret=%{public}d", ret); 79 return; 80 } 81 bufferImpl = SurfaceBufferImpl::FromBase(buffer); 82 if (bufferImpl == nullptr) { 83 VLOGFE("bufferImpl is null"); 84 return; 85 } 86 if (g_layerService != nullptr) { 87 auto bufferHandle = bufferImpl->GetBufferHandle(); 88 g_layerService->SetLayerBuffer(0, layerId_, *bufferHandle, fence); 89 } 90 if (preBuffer != nullptr) { 91 ret = surfacTemp->ReleaseBuffer(preBuffer, -1); 92 if (ret != SURFACE_ERROR_OK) { 93 VLOGFE("release buffer fail, ret=%{public}d", ret); 94 } 95 } 96 preBuffer = buffer; 97 VIDEO_DISPLAY_EXIT(); 98 } 99 VideoDisplayManager()100 VideoDisplayManager::VideoDisplayManager() : surface_(nullptr), listener(nullptr) 101 { 102 VIDEO_DISPLAY_ENTER(); 103 if (g_layerService == nullptr) { 104 g_layerService = IDisplayLayer::Get(DISPLAY_LAYER_SERVICE_NAME); 105 if (g_layerService == nullptr) { 106 VLOGFE("VideoDisplayManager : get layer service failed"); 107 } 108 } 109 VIDEO_DISPLAY_EXIT(); 110 } 111 ~VideoDisplayManager()112 VideoDisplayManager::~VideoDisplayManager() 113 { 114 VIDEO_DISPLAY_ENTER(); 115 VIDEO_DISPLAY_EXIT(); 116 } 117 CreateLayer(const LayerInfo & layerInfo,uint32_t & layerId,sptr<Surface> & surface)118 int32_t VideoDisplayManager::CreateLayer(const LayerInfo &layerInfo, uint32_t &layerId, sptr<Surface>& surface) 119 { 120 VIDEO_DISPLAY_ENTER(); 121 int32_t ret; 122 std::shared_ptr<DisplayInfo> dspInfo; 123 LayerInfo info = layerInfo; 124 125 if (g_layerService == nullptr) { 126 g_layerService = IDisplayLayer::Get(DISPLAY_LAYER_SERVICE_NAME); 127 if (g_layerService == nullptr) { 128 VLOGFE("VideoDisplayManager : get layer service failed"); 129 return DISPLAY_FAILURE; 130 } 131 } 132 133 ret = g_layerService->InitDisplay(0); 134 if (ret != DISPLAY_SUCCESS) { 135 VLOGFE("init display fail, ret=%{public}d", ret); 136 return DISPLAY_FAILURE; 137 } 138 139 ret = g_layerService->GetDisplayInfo(0, dspInfo); 140 if (ret != DISPLAY_SUCCESS) { 141 VLOGFE("get display info fail, ret=%{public}d", ret); 142 (void)g_layerService->DeinitDisplay(0); 143 return DISPLAY_FAILURE; 144 } 145 146 if (info.height == 0 && info.width == 0) { 147 info.width = dspInfo->width; 148 info.height = dspInfo->height; 149 } 150 151 VLOGFI("create layer width=%{public}d height=%{public}d type=%{public}d bpp=%{public}d pixFormat=%{public}d", 152 info.width, info.height, info.type, info.bpp, info.pixFormat); 153 154 ret = g_layerService->CreateLayer(0, info, layerId); 155 if (ret != DISPLAY_SUCCESS) { 156 VLOGFE("create layer fail, ret=%{public}d", ret); 157 (void)g_layerService->DeinitDisplay(0); 158 return DISPLAY_FAILURE; 159 } 160 161 surface = Surface::CreateSurfaceAsConsumer(); 162 if (surface == nullptr) { 163 VLOGFE("create surface fail"); 164 (void)g_layerService->CloseLayer(0, layerId); 165 return DISPLAY_FAILURE; 166 } 167 168 VIDEO_DISPLAY_EXIT(); 169 return DISPLAY_SUCCESS; 170 } 171 DestroyLayer(uint32_t layerId)172 void VideoDisplayManager::DestroyLayer(uint32_t layerId) 173 { 174 VIDEO_DISPLAY_ENTER(); 175 if (g_layerService != nullptr) { 176 (void)g_layerService->CloseLayer(0, layerId); 177 } else { 178 VLOGFE("layer is not create"); 179 } 180 VIDEO_DISPLAY_EXIT(); 181 } 182 AttachLayer(sptr<Surface> & surface,uint32_t layerId)183 sptr<IBufferProducer> VideoDisplayManager::AttachLayer(sptr<Surface>& surface, uint32_t layerId) 184 { 185 VIDEO_DISPLAY_ENTER(); 186 surface_ = surface; 187 listener = new (std::nothrow) Listener(surface_, layerId); 188 if (listener == nullptr) { 189 VLOGFE("Failed to create listener"); 190 return nullptr; 191 } 192 193 surface_->RegisterConsumerListener(listener); 194 VIDEO_DISPLAY_EXIT(); 195 return surface_->GetProducer(); 196 } 197 DetachLayer(uint32_t layerId)198 void VideoDisplayManager::DetachLayer(uint32_t layerId) 199 { 200 VIDEO_DISPLAY_ENTER(); 201 if (surface_ != nullptr) { 202 surface_->UnregisterConsumerListener(); 203 } 204 VIDEO_DISPLAY_EXIT(); 205 } 206 SetRect(uint32_t layerId,IRect rect)207 int32_t VideoDisplayManager::SetRect(uint32_t layerId, IRect rect) 208 { 209 VIDEO_DISPLAY_ENTER(); 210 if (g_layerService == nullptr) { 211 VLOGFE("layer is not create or invalid"); 212 return DISPLAY_FAILURE; 213 } 214 215 int32_t ret = g_layerService->SetLayerRect(0, layerId, rect); 216 VIDEO_DISPLAY_EXIT(); 217 return ret; 218 } 219 GetRect(uint32_t layerId,IRect & rect)220 int32_t VideoDisplayManager::GetRect(uint32_t layerId, IRect &rect) 221 { 222 VIDEO_DISPLAY_ENTER(); 223 if (g_layerService == nullptr) { 224 VLOGFE("layer is not create or invalid"); 225 return DISPLAY_FAILURE; 226 } 227 228 std::shared_ptr<IRect> pRect = std::make_shared<IRect>(); 229 int32_t ret = g_layerService->GetLayerRect(0, layerId, pRect); 230 auto temp = pRect.get(); 231 rect = *temp; 232 VIDEO_DISPLAY_EXIT(); 233 return ret; 234 } 235 SetZorder(uint32_t layerId,uint32_t zorder)236 int32_t VideoDisplayManager::SetZorder(uint32_t layerId, uint32_t zorder) 237 { 238 VIDEO_DISPLAY_ENTER(); 239 if (g_layerService == nullptr) { 240 VLOGFE("layer is not create or invalid"); 241 return DISPLAY_FAILURE; 242 } 243 244 int32_t ret = g_layerService->SetLayerZorder(0, layerId, zorder); 245 VIDEO_DISPLAY_EXIT(); 246 return ret; 247 } 248 GetZorder(uint32_t layerId,uint32_t & zorder)249 int32_t VideoDisplayManager::GetZorder(uint32_t layerId, uint32_t &zorder) 250 { 251 VIDEO_DISPLAY_ENTER(); 252 if (g_layerService == nullptr) { 253 VLOGFE("layer is not create or invalid"); 254 return DISPLAY_FAILURE; 255 } 256 257 int32_t ret = g_layerService->GetLayerZorder(0, layerId, zorder); 258 VIDEO_DISPLAY_EXIT(); 259 return ret; 260 } 261 SetTransformMode(uint32_t layerId,TransformType type)262 int32_t VideoDisplayManager::SetTransformMode(uint32_t layerId, TransformType type) 263 { 264 VIDEO_DISPLAY_ENTER(); 265 if (g_layerService == nullptr) { 266 VLOGFE("layer is not create or invalid"); 267 return DISPLAY_FAILURE; 268 } 269 270 int32_t ret = g_layerService->SetTransformMode(0, layerId, type); 271 VIDEO_DISPLAY_EXIT(); 272 return ret; 273 } 274 SetVisable(uint32_t layerId,bool visible)275 int32_t VideoDisplayManager::SetVisable(uint32_t layerId, bool visible) 276 { 277 VIDEO_DISPLAY_ENTER(); 278 if (g_layerService == nullptr) { 279 VLOGFE("layer is not create or invalid"); 280 return DISPLAY_FAILURE; 281 } 282 283 int32_t ret = g_layerService->SetLayerVisible(0, layerId, visible); 284 VIDEO_DISPLAY_EXIT(); 285 return ret; 286 } 287 IsVisable(uint32_t layerId,bool & visible)288 int32_t VideoDisplayManager::IsVisable(uint32_t layerId, bool &visible) 289 { 290 VIDEO_DISPLAY_ENTER(); 291 if (g_layerService == nullptr) { 292 VLOGFE("layer is not create or invalid"); 293 return DISPLAY_FAILURE; 294 } 295 296 int32_t ret = g_layerService->GetLayerVisibleState(0, layerId, visible); 297 VIDEO_DISPLAY_EXIT(); 298 return ret; 299 } 300 } // namespace OHOS 301