1 /*
2  * Copyright (c) 2023 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 <pthread.h>
16 #include <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_2/display_composer_stub.h"
21 #ifdef DISPLAY_HICOLLIE_ENABLE
22 #include "xcollie/xcollie.h"
23 #include "xcollie/xcollie_define.h"
24 #endif
25 
26 #undef LOG_TAG
27 #define LOG_TAG "COMPOSER_DRV"
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002515
30 #define HICOLLIE_TIMEOUT 10
31 
32 struct HdfDisplayComposerHost {
33     struct IDeviceIoService ioService;
34     OHOS::sptr<OHOS::IRemoteObject> stub;
35 };
36 
37 static pthread_rwlock_t g_rwLock = PTHREAD_RWLOCK_INITIALIZER;
38 static bool g_stop = true;
39 
DisplayComposerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)40 static int32_t DisplayComposerDriverDispatch(
41     struct HdfDeviceIoClient* client, int cmdId, struct HdfSBuf* data, struct HdfSBuf* reply)
42 {
43     if ((client == nullptr) || (client->device == nullptr)) {
44         HDF_LOGE("%{public}s: param is nullptr", __func__);
45         return HDF_ERR_INVALID_PARAM;
46     }
47 
48     OHOS::MessageParcel* dataParcel = nullptr;
49     OHOS::MessageParcel* replyParcel = nullptr;
50     OHOS::MessageOption option;
51 
52     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
53         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
54         return HDF_ERR_INVALID_PARAM;
55     }
56     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
57         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
58         return HDF_ERR_INVALID_PARAM;
59     }
60 
61     pthread_rwlock_rdlock(&g_rwLock);
62     auto* hdfDisplayComposerHost = CONTAINER_OF(client->device->service, struct HdfDisplayComposerHost, ioService);
63     if (hdfDisplayComposerHost == nullptr || g_stop) {
64         pthread_rwlock_unlock(&g_rwLock);
65         HDF_LOGE("%{public}s:hdfDisplayComposerHost nullptr, stop: %{public}d", __func__, g_stop);
66         return HDF_FAILURE;
67     }
68 #ifdef DISPLAY_HICOLLIE_ENABLE
69     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer("HDI::Display::Composer::SendRequest",
70         HICOLLIE_TIMEOUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY);
71 #endif
72     int32_t ret = hdfDisplayComposerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
73 #ifdef DISPLAY_HICOLLIE_ENABLE
74     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
75 #endif
76     pthread_rwlock_unlock(&g_rwLock);
77     return ret;
78 }
79 
HdfDisplayComposerDriverInit(struct HdfDeviceObject * deviceObject)80 static int HdfDisplayComposerDriverInit(struct HdfDeviceObject* deviceObject)
81 {
82     HDF_LOGI("%{public}s: enter", __func__);
83     return HDF_SUCCESS;
84 }
85 
HdfDisplayComposerDriverBind(struct HdfDeviceObject * deviceObject)86 static int HdfDisplayComposerDriverBind(struct HdfDeviceObject* deviceObject)
87 {
88     HDF_LOGI("%{public}s: enter", __func__);
89     static auto* hdfDisplayComposerHost = new (std::nothrow) HdfDisplayComposerHost;
90     if (hdfDisplayComposerHost == nullptr) {
91         HDF_LOGE("%{public}s: failed to create HdfDisplayComposerHost object", __func__);
92         return HDF_FAILURE;
93     }
94 
95     hdfDisplayComposerHost->ioService.Dispatch = DisplayComposerDriverDispatch;
96     hdfDisplayComposerHost->ioService.Open = NULL;
97     hdfDisplayComposerHost->ioService.Release = NULL;
98 
99     auto serviceImpl = OHOS::HDI::Display::Composer::V1_2::IDisplayComposer::Get(true);
100     if (serviceImpl == nullptr) {
101         HDF_LOGE("%{public}s: failed to get the implement of service", __func__);
102         delete hdfDisplayComposerHost;
103         return HDF_FAILURE;
104     }
105 
106     hdfDisplayComposerHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
107         OHOS::HDI::Display::Composer::V1_2::IDisplayComposer::GetDescriptor());
108     if (hdfDisplayComposerHost->stub == nullptr) {
109         HDF_LOGE("%{public}s: failed to get stub object", __func__);
110         delete hdfDisplayComposerHost;
111         return HDF_FAILURE;
112     }
113 
114     deviceObject->service = &hdfDisplayComposerHost->ioService;
115     g_stop = false;
116     return HDF_SUCCESS;
117 }
118 
HdfDisplayComposerDriverRelease(struct HdfDeviceObject * deviceObject)119 static void HdfDisplayComposerDriverRelease(struct HdfDeviceObject* deviceObject)
120 {
121     HDF_LOGI("%{public}s: enter", __func__);
122     pthread_rwlock_wrlock(&g_rwLock);
123     g_stop = true;
124     pthread_rwlock_unlock(&g_rwLock);
125 }
126 
127 static struct HdfDriverEntry g_displaycomposerDriverEntry = {
128     .moduleVersion = 1,
129     .moduleName = "display_composer",
130     .Bind = HdfDisplayComposerDriverBind,
131     .Init = HdfDisplayComposerDriverInit,
132     .Release = HdfDisplayComposerDriverRelease,
133 };
134 
135 #ifdef __cplusplus
136 extern "C" {
137 #endif
138 HDF_INIT(g_displaycomposerDriverEntry);
139 #ifdef __cplusplus
140 }
141 #endif
142