1 /*
2 * Copyright (c) 2022-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
16 #ifndef DISPLAY_DEVICE_COMMON_H
17 #define DISPLAY_DEVICE_COMMON_H
18
19 #include <buffer_handle_parcel.h>
20 #include <display_type.h>
21 #include <fcntl.h>
22 #include <hdf_base.h>
23 #include <hdf_log.h>
24 #include <iservmgr_hdi.h>
25 #include <map>
26 #include <message_option.h>
27 #include <message_parcel.h>
28 #include <osal_mem.h>
29 #include <securec.h>
30 #include <unistd.h>
31 #include "display_device.h"
32 #include "display_layer.h"
33
34 #define DISPLAY_LOG(fmt, args...) HDF_LOGD("DispDevDbg %{public}s / %{public}d :" fmt, __func__, __LINE__, ##args)
35 #define DISPLAY_START DISPLAY_LOG("START")
36 #define DISPLAY_END DISPLAY_LOG("END")
37
38 const int32_t HDF_DISPLAY_DRIVER_FUNC_NUM_MAX = 50;
39 /*****cmdId bit status****
40 ** 0000 0000 0000 1111 0000 0000 0000 0000 functionType
41 ** 0000 0000 0000 0000 0000 0000 1111 1111 functionNum
42 ** 0000 0000 0000 0000 0001 0000 0000 0000 isBatchCommand
43 ** 0000 0000 0000 0000 0010 0000 0000 0000 BatchCommandEndFlag
44 ** 1111 1111 1111 0000 1100 1111 0000 0000 reserved */
45
46 /* single command data : ************ * mean parameter, E mean batch command end flag
47 ** batch command data : CL*CL*CL*N|E N mean command number with batch flag
48 ** common receive reply: CL*CL*CL*N|E C mean command, L mean parameter length **/
49 const uint32_t BATCH_CMD_FLAG = 0x1000;
50 const int32_t COMPOSER_CMD_MAX_NUM = 128;
51 const int32_t STRMAXLEN = 100;
52 const int32_t COMPOSER_SERVER_ARRAY_NUMBER_MAX = 256;
53
54 enum {
55 HDF_DISPLAY_DRIVER_FUNC_TYPE_INVALID,
56 HDF_DISPLAY_DRIVER_FUNC_TYPE_DEVICE,
57 HDF_DISPLAY_DRIVER_FUNC_TYPE_LAYER,
58 HDF_DISPLAY_DRIVER_FUNC_TYPE_MAX
59 };
60 using DisplayDeviceCommandId = enum {
61 DSP_CMD_INVALID = 0x00000,
62 DSP_CMD_EXECUTECMD = 0x00001,
63 // device
64 DSP_CMD_RESERVED_1001 = 0x10000 | 1,
65 DSP_CMD_RESERVED_1002 = 0x10000 | 2,
66 DSP_CMD_REGHOTPLUGCALLBACK = 0x10000 | 3,
67 DSP_CMD_GETDISPLAYCAPABILITY = 0x10000 | 4,
68 DSP_CMD_GETDISPLAYSUPPORTEDMODES = 0x10000 | 5,
69 DSP_CMD_GETDISPLAYMODE = 0x10000 | 6,
70 DSP_CMD_SETDISPLAYMODE = 0x10000 | 7,
71 DSP_CMD_GETDISPLAYPOWERSTATUS = 0x10000 | 8,
72 DSP_CMD_SETDISPLAYPOWERSTATUS = 0x10000 | 9,
73 DSP_CMD_GETDISPLAYBACKLIGHT = 0x10000 | 10,
74 DSP_CMD_SETDISPLAYBACKLIGHT = 0x10000 | 11,
75 DSP_CMD_GETDISPLAYPROPERTY = 0x10000 | 12,
76 DSP_CMD_SETDISPLAYPROPERTY = 0x10000 | 13,
77 DSP_CMD_PREPAREDISPLAYLAYERS = 0x10000 | 14 | 0x1000,
78 DSP_CMD_RESERVED_1015 = 0x10000 | 15,
79 DSP_CMD_GETDISPLAYCOMPCHANGE = 0x10000 | 16,
80 DSP_CMD_RESERVED_1017 = 0x10000 | 17,
81 DSP_CMD_SETDISPLAYCLIENTCROP = 0x10000 | 18,
82 DSP_CMD_SETDISPLAYCLIENTDESTRECT = 0x10000 | 19,
83 DSP_CMD_SETDISPLAYCLIENTBUFFER = 0x10000 | 20 | 0x1000,
84 DSP_CMD_SETDISPLAYCLIENTDAMAGE = 0x10000 | 21 | 0x1000,
85 DSP_CMD_SETDISPLAYVSYNCENABLED = 0x10000 | 22,
86 DSP_CMD_RESERVED_1023 = 0x10000 | 23,
87 DSP_CMD_RESERVED_1024 = 0x10000 | 24,
88 DSP_CMD_REGDISPLAYVBLANKCALLBACK = 0x10000 | 25,
89 DSP_CMD_RESERVED_1026 = 0x10000 | 26,
90 DSP_CMD_GETDISPLAYRELEASEFENCE = 0x10000 | 27,
91 DSP_CMD_COMMIT = 0x10000 | 28 | 0x1000,
92 DSP_CMD_INVOKEDISPLAYCMD = 0x10000 | 29 | 0x1000,
93 DSP_CMD_CREATEVIRTUALDISPLAY = 0x10000 | 30,
94 DSP_CMD_DESTROYVIRTUALDISPLAY = 0x10000 | 31,
95 DSP_CMD_SETVIRTUALDISPLAYBUFFER = 0x10000 | 32,
96 DSP_CMD_REGDISPLAYREFRESHCALLBACK = 0x10000 | 33,
97 DSP_CMD_GETWRITEBACKFRAME = 0x10000 | 34,
98 DSP_CMD_CREATEWRITEBACK = 0x10000 | 35,
99 DSP_CMD_DESTROYWRITEBACK = 0x10000 | 36,
100 DSP_CMD_SET_PROXY_REMOTE_CALLBACK = 0x10000 | 37,
101 // layer
102 DSP_CMD_RESERVED_2001 = 0x20000 | 1,
103 DSP_CMD_RESERVED_2002 = 0x20000 | 2,
104 DSP_CMD_CREATELAYER = 0x20000 | 3,
105 DSP_CMD_RESERVED_2004 = 0x20000 | 4,
106 DSP_CMD_SETLAYERVISIBLE = 0x20000 | 5 | 0x1000,
107 DSP_CMD_GETLAYERVISIBLESTATE = 0x20000 | 6,
108 DSP_CMD_RESERVED_2007 = 0x20000 | 7,
109 DSP_CMD_RESERVED_2008 = 0x20000 | 8,
110 DSP_CMD_SETLAYERCROP = 0x20000 | 9 | 0x1000,
111 DSP_CMD_SETLAYERZORDER = 0x20000 | 10 | 0x1000,
112 DSP_CMD_GETLAYERZORDER = 0x20000 | 11,
113 DSP_CMD_SETLAYERPREMULTI = 0x20000 | 12 | 0x1000,
114 DSP_CMD_GETLAYERPREMULTI = 0x20000 | 13,
115 DSP_CMD_SETLAYERALPHA = 0x20000 | 14 | 0x1000,
116 DSP_CMD_GETLAYERALPHA = 0x20000 | 15,
117 DSP_CMD_SETLAYERCOLORKEY = 0x20000 | 16 | 0x1000,
118 DSP_CMD_GETLAYERCOLORKEY = 0x20000 | 17,
119 DSP_CMD_SETLAYERPALETTE = 0x20000 | 18 | 0x1000,
120 DSP_CMD_GETLAYERPALETTE = 0x20000 | 19,
121 DSP_CMD_RESERVED_2020 = 0x20000 | 20,
122 DSP_CMD_SETLAYERCOMPRESSION = 0x20000 | 21 | 0x1000,
123 DSP_CMD_GETLAYERCOMPRESSION = 0x20000 | 22,
124 DSP_CMD_RESERVED_2023 = 0x20000 | 23,
125 DSP_CMD_FLUSH = 0x20000 | 24 | 0x1000,
126 DSP_CMD_SETLAYERVISIBLEREGION = 0x20000 | 25 | 0x1000,
127 DSP_CMD_SETLAYERDIRTYREGION = 0x20000 | 26 | 0x1000,
128 DSP_CMD_GETLAYERBUFFER = 0x20000 | 27,
129 DSP_CMD_SETLAYERBUFFER = 0x20000 | 28 | 0x1000,
130 DSP_CMD_INVOKELAYERCMD = 0x20000 | 29 | 0x1000,
131 DSP_CMD_SETLAYERCOMPOSITIONTYPE = 0x20000 | 30 | 0x1000,
132 DSP_CMD_RESERVED_2031 = 0x20000 | 31,
133 DSP_CMD_INITDISPLAY = 0x20000 | 32,
134 DSP_CMD_DEINITDISPLAY = 0x20000 | 33,
135 DSP_CMD_GETDISPLAYINFO = 0x20000 | 34,
136 DSP_CMD_CLOSELAYER = 0x20000 | 35,
137 DSP_CMD_SETLAYERSIZE = 0x20000 | 36 | 0x1000,
138 DSP_CMD_GETLAYERSIZE = 0x20000 | 37,
139 DSP_CMD_SETTRANSFORMMODE = 0x20000 | 38 | 0x1000,
140 DSP_CMD_WAITFORVBLANK = 0x20000 | 39,
141 DSP_CMD_SNAPSHOT = 0x20000 | 40,
142 DSP_CMD_SETLAYERBLENDTYPE = 0x20000 | 41 | 0x1000
143 };
144 using HotPlugCallback = void (*)(uint32_t outputId, bool connected, void *data);
145 using VBlankCallback = void (*)(unsigned int sequence, uint64_t ns, void *data);
146 using PageFlipCallback = void (*)(unsigned int sequence, uint64_t ns, void *data);
147 using RefreshCallback = void (*)(uint32_t devId, void *data);
148
149 using CommandId = int32_t;
150 using BlendOpt = int32_t;
151
152 DispPowerStatus Convert2PowerStatus(int32_t x);
153 CompositionType Convert2CompositionType(int32_t x);
154 TransformType Convert2TransformType(int32_t x);
155 BlendType Convert2BlendTypeType(int32_t x);
156
157 bool DisplayDeviceWriteCmdId(OHOS::MessageParcel *parcel, DisplayDeviceCommandId cmdId);
158 DisplayDeviceCommandId DisplayDeviceReadCmdId(OHOS::MessageParcel *parcel);
159 bool DisplayDeviceWriteUint32(OHOS::MessageParcel *parcel, uint32_t data);
160 bool DisplayDeviceReadUint32(uint32_t *data, OHOS::MessageParcel *parcel);
161 bool DisplayDeviceWriteInt32(OHOS::MessageParcel *parcel, int32_t data);
162 bool DisplayDeviceReadInt32(int32_t *data, OHOS::MessageParcel *parcel);
163 bool DisplayDeviceWriteBool(OHOS::MessageParcel *parcel, bool data);
164 bool DisplayDeviceReadBool(bool *data, OHOS::MessageParcel *parcel);
165 bool DisplayDeviceWriteUint64(OHOS::MessageParcel *parcel, uint64_t data);
166 bool DisplayDeviceReadUint64(uint64_t *data, OHOS::MessageParcel *parcel);
167 bool DisplayDeviceWritePtr(OHOS::MessageParcel *parcel, uintptr_t data);
168 bool DisplayDeviceReadPtr(uintptr_t *&data, OHOS::MessageParcel *parcel);
169 bool DisplayDeviceWriteBufHdl(OHOS::MessageParcel *parcel, const BufferHandle *data);
170 bool DisplayDeviceReadBufHdl(BufferHandle *&data, OHOS::MessageParcel *parcel);
171 bool DisplayDeviceWriteFileDescriptor(OHOS::MessageParcel *parcel, const int fd);
172 bool DisplayDeviceReadFileDescriptor(int *fd, OHOS::MessageParcel *parcel);
173 bool DisplayDeviceWriteFileDescriptorArray(OHOS::MessageParcel *parcel, const int *fd, uint32_t num = 1);
174 bool DisplayDeviceReadFileDescriptorArray(int *fd, OHOS::MessageParcel *parcel, uint32_t num = 1);
175
176 template <typename T>
DisplayDeviceWriteData(OHOS::MessageParcel *parcel, const T *data, uint32_t num = 1)177 bool DisplayDeviceWriteData(OHOS::MessageParcel *parcel, const T *data, uint32_t num = 1)
178 {
179 if (nullptr == parcel || nullptr == data || num == 0) {
180 DISPLAY_LOG("error: OHOS::MessageParcel %{public}s nullptr, data %{public}s nullptr, num %{public}s 0",
181 nullptr == parcel ? "is" : "is not", nullptr == data ? "is" : "is not", 0 == num ? "is" : "is not");
182 return false;
183 }
184
185 if (!parcel->WriteUint32(sizeof(T) * num)) {
186 DISPLAY_LOG("error: write data length into parcel failed");
187 return false;
188 }
189
190 if (!parcel->WriteBuffer(data, sizeof(T) * num)) {
191 DISPLAY_LOG("error: write data into parcel failed");
192 return false;
193 }
194 return true;
195 }
196 template <typename T>
DisplayDeviceReadData(T *data, OHOS::MessageParcel *parcel, uint32_t num = 1)197 bool DisplayDeviceReadData(T *data, OHOS::MessageParcel *parcel, uint32_t num = 1)
198 {
199 if (nullptr == parcel || nullptr == data || num == 0) {
200 DISPLAY_LOG("error: OHOS::MessageParcel %{public}s nullptr, data %{public}s nullptr, num %{public}s 0",
201 nullptr == parcel ? "is" : "is not", nullptr == data ? "is" : "is not", 0 == num ? "is" : "is not");
202 return false;
203 }
204
205 uint32_t retValueLen = 0;
206 if (sizeof(T) * num != (retValueLen = parcel->ReadUint32())) {
207 DISPLAY_LOG(
208 "error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen, sizeof(T));
209 return false;
210 }
211
212 T *dataTmp = (T *)parcel->ReadUnpadBuffer(retValueLen);
213 if (dataTmp == nullptr) {
214 DISPLAY_LOG("read buffer data failed");
215 return false;
216 }
217 if (memcpy_s(data, num * sizeof(T), dataTmp, retValueLen) != EOK) {
218 return HDF_ERR_INVALID_PARAM;
219 }
220 return true;
221 }
222
223 #endif // DISPLAY_DEVICE_COMMON_H
224