• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef OHOS_HDI_DISPLAY_V1_0_DISPLAY_CMD_REQUESTER_H
17 #define OHOS_HDI_DISPLAY_V1_0_DISPLAY_CMD_REQUESTER_H
18 
19 #include <fstream>
20 #include <poll.h>
21 #include <securec.h>
22 #include <sstream>
23 #include <string>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <unordered_map>
27 #include <queue>
28 
29 #include "base/hdi_smq.h"
30 #include "buffer_handle_utils.h"
31 #include "command_pack/command_data_packer.h"
32 #include "command_pack/command_data_unpacker.h"
33 #include "display_cmd_utils.h"
34 #include "hdf_base.h"
35 #include "hdf_trace.h"
36 #include "hdifd_parcelable.h"
37 #include "hilog/log.h"
38 #include "idisplay_composer_vdi.h"
39 #include "parameter.h"
40 #include "v1_0/display_composer_type.h"
41 #include "v1_0/mapper_stub.h"
42 
43 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
44 
45 namespace OHOS {
46 namespace HDI {
47 namespace Display {
48 namespace Composer {
49 namespace V1_0 {
50 using namespace OHOS::HDI::Base;
51 using namespace OHOS::HDI::Display::Composer::V1_0;
52 using namespace OHOS::HDI::Display::Buffer::V1_0;
53 using HdifdSet = std::vector<std::shared_ptr<HdifdParcelable>>;
54 
55 static constexpr uint32_t TIME_BUFFER_MAX_LEN = 15;
56 static constexpr uint32_t BUFFER_QUEUE_MAX_SIZE = 6;
57 
58 static sptr<IMapper> g_bufferServiceImpl = nullptr;
59 
60 static constexpr uint32_t COMMIT_PRINT_INTERVAL = 1200;
61 
62 template <typename Transfer, typename VdiImpl>
63 class DisplayCmdResponser {
64 public:
Create(VdiImpl * impl,std::shared_ptr<DeviceCacheManager> cacheMgr)65     static std::unique_ptr<DisplayCmdResponser> Create(VdiImpl* impl, std::shared_ptr<DeviceCacheManager> cacheMgr)
66     {
67         DISPLAY_CHK_RETURN(impl == nullptr, nullptr,
68             HDF_LOGE("%{public}s: error, VdiImpl is nullptr", __func__));
69         DISPLAY_CHK_RETURN(cacheMgr == nullptr, nullptr,
70             HDF_LOGE("%{public}s: error, VdiImpl is nullptr", __func__));
71         return std::make_unique<DisplayCmdResponser>(impl, cacheMgr);
72     }
73 
DisplayCmdResponser(VdiImpl * impl,std::shared_ptr<DeviceCacheManager> cacheMgr)74     DisplayCmdResponser(VdiImpl* impl, std::shared_ptr<DeviceCacheManager> cacheMgr)
75         : impl_(impl),
76         cacheMgr_(cacheMgr),
77         request_(nullptr),
78         isReplyUpdated_(false),
79         reply_(nullptr),
80         replyCommandCnt_(0),
81         replyPacker_(nullptr) {}
82 
~DisplayCmdResponser()83     virtual ~DisplayCmdResponser()
84     {
85         while (delayFreeQueue_.size() > 0) {
86             BufferHandle *temp = delayFreeQueue_.front();
87             delayFreeQueue_.pop();
88             FreeBufferHandle(temp);
89             temp = nullptr;
90         }
91     }
92 
InitCmdRequest(const std::shared_ptr<Transfer> & request)93     int32_t InitCmdRequest(const std::shared_ptr<Transfer>& request)
94     {
95         DISPLAY_CHK_RETURN(request == nullptr, HDF_FAILURE,
96             HDF_LOGE("%{public}s: error, request is nullptr", __func__));
97         if (request_ != nullptr) {
98             request_.reset();
99         }
100         request_ = request;
101 
102         return HDF_SUCCESS;
103     }
104 
GetCmdReply(std::shared_ptr<Transfer> & reply)105     int32_t GetCmdReply(std::shared_ptr<Transfer>& reply)
106     {
107         int32_t ret = HDF_SUCCESS;
108         if (isReplyUpdated_ == false) {
109             ret = InitReply(CmdUtils::INIT_ELEMENT_COUNT);
110         }
111         if (ret == HDF_SUCCESS) {
112             if (reply_ != nullptr) {
113                 reply = reply_;
114             } else {
115                 ret = HDF_FAILURE;
116             }
117         }
118         isReplyUpdated_ = false;
119         if (ret != HDF_SUCCESS) {
120             HDF_LOGE("error: GetCmdReply failure");
121         }
122 
123         return ret;
124     }
125 
ProcessRequestCmd(std::shared_ptr<CommandDataUnpacker> unpacker,int32_t cmd,const std::vector<HdifdInfo> & inFds,std::vector<HdifdInfo> & outFds)126     int32_t ProcessRequestCmd(std::shared_ptr<CommandDataUnpacker> unpacker, int32_t cmd,
127         const std::vector<HdifdInfo>& inFds, std::vector<HdifdInfo>& outFds)
128     {
129         int32_t ret = HDF_SUCCESS;
130         HDF_LOGD("%{public}s: PackSection, cmd-[%{public}d] = %{public}s",
131             __func__, cmd, CmdUtils::CommandToString(cmd));
132         switch (cmd) {
133             case REQUEST_CMD_PREPARE_DISPLAY_LAYERS:
134                 OnPrepareDisplayLayers(unpacker);
135                 break;
136             case REQUEST_CMD_SET_DISPLAY_CLIENT_BUFFER:
137                 OnSetDisplayClientBuffer(unpacker, inFds);
138                 break;
139             case REQUEST_CMD_SET_DISPLAY_CLIENT_DAMAGE:
140                 OnSetDisplayClientDamage(unpacker);
141                 break;
142             case REQUEST_CMD_COMMIT:
143                 OnCommit(unpacker, outFds);
144                 break;
145             case REQUEST_CMD_SET_LAYER_ALPHA:
146                 OnSetLayerAlpha(unpacker);
147                 break;
148             case REQUEST_CMD_SET_LAYER_REGION:
149                 OnSetLayerRegion(unpacker);
150                 break;
151             case REQUEST_CMD_SET_LAYER_CROP:
152                 OnSetLayerCrop(unpacker);
153                 break;
154             case REQUEST_CMD_SET_LAYER_ZORDER:
155                 OnSetLayerZorder(unpacker);
156                 break;
157             case REQUEST_CMD_SET_LAYER_PREMULTI:
158                 OnSetLayerPreMulti(unpacker);
159                 break;
160             case REQUEST_CMD_SET_LAYER_TRANSFORM_MODE:
161                 OnSetLayerTransformMode(unpacker);
162                 break;
163             case REQUEST_CMD_SET_LAYER_DIRTY_REGION:
164                 OnSetLayerDirtyRegion(unpacker);
165                 break;
166             case REQUEST_CMD_SET_LAYER_VISIBLE_REGION:
167                 OnSetLayerVisibleRegion(unpacker);
168                 break;
169             case REQUEST_CMD_SET_LAYER_BUFFER:
170                 OnSetLayerBuffer(unpacker, inFds);
171                 break;
172             case REQUEST_CMD_SET_LAYER_COMPOSITION_TYPE:
173                 OnSetLayerCompositionType(unpacker);
174                 break;
175             case REQUEST_CMD_SET_LAYER_BLEND_TYPE:
176                 OnSetLayerBlendType(unpacker);
177                 break;
178             case REQUEST_CMD_SET_LAYER_MASK_INFO:
179                 OnSetLayerMaskInfo(unpacker);
180                 break;
181             case CONTROL_CMD_REQUEST_END:
182                 ret = OnRequestEnd(unpacker);
183                 break;
184             case REQUEST_CMD_SET_LAYER_COLOR:
185                 OnSetLayerColor(unpacker);
186                 break;
187             default:
188                 HDF_LOGE("%{public}s: not support this cmd, unpacked cmd = %{public}d", __func__, cmd);
189                 ret = HDF_FAILURE;
190                 break;
191         }
192         return ret;
193     }
194 
CmdRequest(uint32_t inEleCnt,const std::vector<HdifdInfo> & inFds,uint32_t & outEleCnt,std::vector<HdifdInfo> & outFds)195     int32_t CmdRequest(uint32_t inEleCnt, const std::vector<HdifdInfo>& inFds, uint32_t& outEleCnt,
196         std::vector<HdifdInfo>& outFds)
197     {
198         std::shared_ptr<char> requestData(new char[inEleCnt * CmdUtils::ELEMENT_SIZE], std::default_delete<char[]>());
199         int32_t ret = request_->Read(reinterpret_cast<int32_t *>(requestData.get()), inEleCnt,
200             CmdUtils::TRANSFER_WAIT_TIME);
201 
202         std::shared_ptr<CommandDataUnpacker> unpacker = std::make_shared<CommandDataUnpacker>();
203         DISPLAY_CHK_RETURN(unpacker == nullptr, HDF_FAILURE,
204             HDF_LOGE("%{public}s: unpacker construct failed", __func__));
205 
206         unpacker->Init(requestData.get(), inEleCnt * CmdUtils::ELEMENT_SIZE);
207 #ifdef DEBUG_DISPLAY_CMD_RAW_DATA
208         unpacker->Dump();
209 #endif // DEBUG_DISPLAY_CMD_RAW_DATA
210 
211         int32_t unpackCmd = -1;
212         bool retBool = unpacker->PackBegin(unpackCmd);
213         DISPLAY_CHK_RETURN(retBool == false, HDF_FAILURE,
214             HDF_LOGE("%{public}s: error: Check RequestBegin failed", __func__));
215         DISPLAY_CHK_RETURN(unpackCmd != CONTROL_CMD_REQUEST_BEGIN, HDF_FAILURE,
216             HDF_LOGI("error: unpacker PackBegin cmd not match, cmd(%{public}d)=%{public}s.", unpackCmd,
217             CmdUtils::CommandToString(unpackCmd)));
218 
219         while (ret == HDF_SUCCESS && unpacker->NextSection()) {
220             if (!unpacker->BeginSection(unpackCmd)) {
221                 HDF_LOGE("error: PackSection failed, unpackCmd=%{public}s.",
222                     CmdUtils::CommandToString(unpackCmd));
223                 ret = HDF_FAILURE;
224             }
225             ret = ProcessRequestCmd(unpacker, unpackCmd, inFds, outFds);
226         }
227 
228         DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret,
229             HDF_LOGE("%{public}s: ProcessRequestCmd failed", __func__));
230         /* pack request end commands */
231         replyPacker_->PackEnd(CONTROL_CMD_REPLY_END);
232 
233 #ifdef DEBUG_DISPLAY_CMD_RAW_DATA
234         /* just for debug */
235         replyPacker_->Dump();
236         HDF_LOGI("CmdReply command cnt=%{public}d", replyCommandCnt_);
237 #endif // DEBUG_DISPLAY_CMD_RAW_DATA
238 
239         /*  Write reply pack */
240         outEleCnt = replyPacker_->ValidSize() / CmdUtils::ELEMENT_SIZE;
241         ret = reply_->Write(reinterpret_cast<int32_t *>(replyPacker_->GetDataPtr()), outEleCnt,
242             CmdUtils::TRANSFER_WAIT_TIME);
243         if (ret != HDF_SUCCESS) {
244             HDF_LOGE("Reply write failure, ret=%{public}d", ret);
245             outEleCnt = 0;
246         }
247         int32_t ec = PeriodDataReset();
248         return (ret == HDF_SUCCESS ? ec : ret);
249     }
250 
251 protected:
InitReply(uint32_t size)252     int32_t InitReply(uint32_t size)
253     {
254         reply_ = std::make_shared<Transfer>(size, SmqType::SYNCED_SMQ);
255         DISPLAY_CHK_RETURN(reply_ == nullptr, HDF_FAILURE,
256             HDF_LOGE("%{public}s: reply_ construct failed", __func__));
257 
258         replyPacker_ = std::make_shared<CommandDataPacker>();
259         DISPLAY_CHK_RETURN(replyPacker_ == nullptr, HDF_FAILURE,
260             HDF_LOGE("%{public}s: replyPacker_ construct failed", __func__));
261 
262         bool retBool = replyPacker_->Init(reply_->GetSize() * CmdUtils::ELEMENT_SIZE);
263         DISPLAY_CHK_RETURN(retBool == false, HDF_FAILURE,
264             HDF_LOGE("%{public}s: replyPacker_ init failed", __func__));
265 
266         return CmdUtils::StartPack(CONTROL_CMD_REPLY_BEGIN, replyPacker_);
267     }
268 
OnRequestEnd(std::shared_ptr<CommandDataUnpacker> unpacker)269     int32_t OnRequestEnd(std::shared_ptr<CommandDataUnpacker> unpacker)
270     {
271         DISPLAY_TRACE;
272 
273         size_t errCnt = errMaps_.size();
274         if (errCnt >= 0) {
275             int32_t ret = CmdUtils::StartSection(REPLY_CMD_SET_ERROR, replyPacker_);
276             DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret,
277                 HDF_LOGE("%{public}s: StartSection failed", __func__));
278 
279             bool result = replyPacker_->WriteUint32(errCnt);
280             DISPLAY_CHK_RETURN(result == false, HDF_FAILURE,
281                 HDF_LOGE("%{public}s: write errCnt failed", __func__));
282             for (auto it = errMaps_.begin(); it != errMaps_.end(); ++it) {
283                 result = replyPacker_->WriteInt32(it->first);
284                 DISPLAY_CHK_RETURN(result == false, HDF_FAILURE,
285                     HDF_LOGE("%{public}s: write err-cmd failed, cmdId:%{public}s",
286                     __func__, CmdUtils::CommandToString(it->first)));
287 
288                 result = replyPacker_->WriteInt32(it->second);
289                 DISPLAY_CHK_RETURN(result == false, HDF_FAILURE,
290                     HDF_LOGE("%{public}s: write errNo failed, errNo:%{public}d", __func__, it->second));
291             }
292             result = CmdUtils::EndSection(replyPacker_);
293             DISPLAY_CHK_RETURN(result == false, HDF_FAILURE,
294                 HDF_LOGE("%{public}s: write replyPacker_ EndSection failed", __func__));
295             replyCommandCnt_++;
296         }
297         return HDF_SUCCESS;
298     }
299 
OnPrepareDisplayLayers(std::shared_ptr<CommandDataUnpacker> unpacker)300     void OnPrepareDisplayLayers(std::shared_ptr<CommandDataUnpacker> unpacker)
301     {
302         DISPLAY_TRACE;
303 
304         uint32_t devId = 0;
305         bool needFlush = false;
306         uint32_t vectSize = 0;
307         std::vector<uint32_t> layers;
308         std::vector<int32_t> types;
309 
310         int32_t ret = unpacker->ReadUint32(devId) ? HDF_SUCCESS : HDF_FAILURE;
311         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
312 
313         ret = impl_->PrepareDisplayLayers(devId, needFlush);
314         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
315 
316         ret = impl_->GetDisplayCompChange(devId, layers, types);
317         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
318 
319         ret = CmdUtils::StartSection(REPLY_CMD_PREPARE_DISPLAY_LAYERS, replyPacker_);
320         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
321 
322         DISPLAY_CHECK(replyPacker_->WriteUint32(devId) == false, goto EXIT);
323 
324         DISPLAY_CHECK(replyPacker_->WriteBool(needFlush) == false, goto EXIT);
325         // Write layers vector
326         vectSize = static_cast<uint32_t>(layers.size());
327         DISPLAY_CHECK(replyPacker_->WriteUint32(vectSize) == false, goto EXIT);
328 
329         for (uint32_t i = 0; i < vectSize; i++) {
330             DISPLAY_CHECK(replyPacker_->WriteUint32(layers[i]) == false, goto EXIT);
331         }
332         // Write composer types vector
333         vectSize = static_cast<uint32_t>(types.size());
334         DISPLAY_CHECK(replyPacker_->WriteUint32(vectSize) == false, goto EXIT);
335 
336         for (uint32_t i = 0; i < vectSize; i++) {
337             DISPLAY_CHECK(replyPacker_->WriteUint32(types[i]) == false, goto EXIT);
338         }
339         // End this cmd section
340         ret = CmdUtils::EndSection(replyPacker_);
341         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
342         replyCommandCnt_++;
343 EXIT:
344         if (ret != HDF_SUCCESS) {
345             errMaps_.emplace(REQUEST_CMD_PREPARE_DISPLAY_LAYERS, ret);
346         }
347         return;
348     }
349 
350     typedef struct ClientBufferData {
351         uint32_t devId;
352         uint32_t seqNo;
353         int32_t fence;
354         BufferHandle *buffer;
355         bool isValidBuffer;
356     } ClientBufferData;
357 
UnpackDisplayClientBufferInfo(std::shared_ptr<CommandDataUnpacker> unpacker,const std::vector<HdifdInfo> & inFds,ClientBufferData & data)358     int32_t UnpackDisplayClientBufferInfo(std::shared_ptr<CommandDataUnpacker> unpacker,
359         const std::vector<HdifdInfo>& inFds, ClientBufferData &data)
360     {
361         if (!unpacker->ReadUint32(data.devId)) {
362             return HDF_FAILURE;
363         }
364 
365         if (CmdUtils::BufferHandleUnpack(unpacker, inFds, data.buffer) != HDF_SUCCESS) {
366             data.isValidBuffer = false;
367             HDF_LOGE("%{public}s, read buffer handle error", __func__);
368             return HDF_FAILURE;
369         }
370         data.isValidBuffer = true;
371 
372         if (!unpacker->ReadUint32(data.seqNo)) {
373             HDF_LOGE("%{public}s, read seqNo error", __func__);
374             return HDF_FAILURE;
375         }
376 
377         if (CmdUtils::FileDescriptorUnpack(unpacker, inFds, data.fence) != HDF_SUCCESS) {
378             HDF_LOGE("%{public}s, FileDescriptorUnpack error", __func__);
379             return HDF_FAILURE;
380         }
381 
382         return HDF_SUCCESS;
383     }
384 
OnSetDisplayClientBuffer(std::shared_ptr<CommandDataUnpacker> unpacker,const std::vector<HdifdInfo> & inFds)385     void OnSetDisplayClientBuffer(std::shared_ptr<CommandDataUnpacker> unpacker, const std::vector<HdifdInfo>& inFds)
386     {
387         DISPLAY_TRACE;
388 
389         ClientBufferData data = {0};
390         data.fence = -1;
391         bool needFreeBuffer = false;
392         int32_t ret = UnpackDisplayClientBufferInfo(unpacker, inFds, data);
393         HdifdParcelable fdParcel(data.fence);
394 
395         if (ret != HDF_SUCCESS) {
396             goto EXIT;
397         }
398 
399         {
400             if (cacheMgr_ == nullptr) {
401                 ret = HDF_FAILURE;
402                 HDF_LOGE("%{public}s, get cache manager error", __func__);
403                 goto EXIT;
404             }
405             std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
406 
407             DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(data.devId);
408             if (devCache == nullptr) {
409                 ret = HDF_FAILURE;
410                 HDF_LOGE("%{public}s, get device cache error", __func__);
411                 goto EXIT;
412             }
413 
414             ret = devCache->SetDisplayClientBuffer(data.buffer, data.seqNo, needFreeBuffer,
415                 [&](const BufferHandle& handle)->int32_t {
416                 int rc = impl_->SetDisplayClientBuffer(data.devId, handle, fdParcel.GetFd());
417                 DISPLAY_CHK_RETURN(rc != HDF_SUCCESS, HDF_FAILURE, HDF_LOGE(" fail"));
418                 return HDF_SUCCESS;
419             });
420         }
421 #ifndef DISPLAY_COMMUNITY
422         // fix fd leak
423         if (data.buffer != nullptr && needFreeBuffer) {
424             FreeBufferWithDelay(data.buffer);
425             data.buffer = nullptr;
426             data.isValidBuffer = false;
427         }
428         fdParcel.Move();
429 #endif // DISPLAY_COMMUNITY
430 EXIT:
431         if (ret != HDF_SUCCESS) {
432             HDF_LOGE("%{public}s, SetDisplayClientBuffer error", __func__);
433             if (data.isValidBuffer) {
434                 FreeBufferHandle(data.buffer);
435             }
436             errMaps_.emplace(REQUEST_CMD_SET_DISPLAY_CLIENT_BUFFER, ret);
437         }
438     }
439 
OnSetDisplayClientDamage(std::shared_ptr<CommandDataUnpacker> unpacker)440     void OnSetDisplayClientDamage(std::shared_ptr<CommandDataUnpacker> unpacker)
441     {
442         DISPLAY_TRACE;
443 
444         uint32_t devId = 0;
445         uint32_t vectSize = 0;
446         bool retBool = true;
447         DISPLAY_CHK_CONDITION(retBool, true, unpacker->ReadUint32(devId),
448             HDF_LOGE("%{public}s, read devId error", __func__));
449 
450         DISPLAY_CHK_CONDITION(retBool, true, unpacker->ReadUint32(vectSize),
451             HDF_LOGE("%{public}s, read vectSize error", __func__));
452 
453         int32_t ret = (retBool ? HDF_SUCCESS : HDF_FAILURE);
454         std::vector<IRect> rects(vectSize);
455         if (ret == HDF_SUCCESS) {
456             for (uint32_t i = 0; i < vectSize; i++) {
457                 DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::RectUnpack(unpacker, rects[i]),
458                     HDF_LOGE("%{public}s, read vect error at i = %{public}d", __func__, i));
459                 if (ret != HDF_SUCCESS) {
460                     break;
461                 }
462             }
463         }
464         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, impl_->SetDisplayClientDamage(devId, rects),
465             HDF_LOGE("%{public}s, SetDisplayClientDamage error", __func__));
466 
467         if (ret != HDF_SUCCESS) {
468             errMaps_.emplace(REQUEST_CMD_SET_DISPLAY_CLIENT_DAMAGE, ret);
469         }
470         return;
471     }
472 
OnCommit(std::shared_ptr<CommandDataUnpacker> unpacker,std::vector<HdifdInfo> & outFds)473     void OnCommit(std::shared_ptr<CommandDataUnpacker> unpacker, std::vector<HdifdInfo>& outFds)
474     {
475         DISPLAY_TRACE;
476 
477         uint32_t devId = 0;
478         int32_t fence = -1;
479 #ifdef DISPLAY_COMSPOER_DEBUG_DUMP
480         const std::string SWITCH_ON = "on";
481         const uint32_t DUMP_CACHE_SWITCH_LEN = 4;
482         char dumpSwitch[DUMP_CACHE_SWITCH_LEN] = {0};
483         GetParameter("hdi.composer.dumpcache", "off", dumpSwitch, DUMP_CACHE_SWITCH_LEN);
484 
485         if (SWITCH_ON.compare(dumpSwitch) == 0) {
486             cacheMgr_->Dump();
487         }
488 #endif
489         int32_t ret = HDF_SUCCESS;
490         if (!unpacker->ReadUint32(devId)) {
491             HDF_LOGE("%{public}s, read devId error", __func__);
492             ret = HDF_FAILURE;
493             goto REPLY;
494         }
495 
496         ret = impl_->Commit(devId, fence);
497         if (ret != HDF_SUCCESS) {
498             HDF_LOGE("%{public}s, commit failed with ret = %{public}d", __func__, ret);
499         }
500 
501 REPLY:
502         HdifdParcelable fdParcel(fence);
503         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::StartSection(REPLY_CMD_COMMIT, replyPacker_),
504             HDF_LOGE("%{public}s, StartSection error", __func__));
505 
506         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::FileDescriptorPack(fdParcel.GetFd(), replyPacker_, outFds),
507             HDF_LOGE("%{public}s, FileDescriptorPack error", __func__));
508 
509         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::EndSection(replyPacker_),
510             HDF_LOGE("%{public}s, EndSection error", __func__));
511 
512         replyCommandCnt_++;
513 
514 #ifndef DISPLAY_COMMUNITY
515         fdParcel.Move();
516 #endif // DISPLAY_COMMUNITY
517 
518         if (ret != HDF_SUCCESS) {
519             errMaps_.emplace(REQUEST_CMD_COMMIT, ret);
520         }
521 
522         return;
523     }
524 
OnSetLayerAlpha(std::shared_ptr<CommandDataUnpacker> unpacker)525     void OnSetLayerAlpha(std::shared_ptr<CommandDataUnpacker> unpacker)
526     {
527         DISPLAY_TRACE;
528 
529         uint32_t devId = 0;
530         uint32_t layerId = 0;
531         LayerAlpha alpha = {0};
532         bool retBool = true;
533 
534         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
535         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
536 
537         retBool = unpacker->ReadBool(alpha.enGlobalAlpha);
538         DISPLAY_CHECK(retBool == false, goto EXIT);
539 
540         retBool = unpacker->ReadBool(alpha.enPixelAlpha);
541         DISPLAY_CHECK(retBool == false, goto EXIT);
542 
543         retBool = unpacker->ReadUint8(alpha.alpha0);
544         DISPLAY_CHECK(retBool == false, goto EXIT);
545 
546         retBool = unpacker->ReadUint8(alpha.alpha1);
547         DISPLAY_CHECK(retBool == false, goto EXIT);
548 
549         retBool = unpacker->ReadUint8(alpha.gAlpha);
550         DISPLAY_CHECK(retBool == false, goto EXIT);
551 
552         ret = impl_->SetLayerAlpha(devId, layerId, alpha);
553         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
554 
555 EXIT:
556         if (ret != HDF_SUCCESS || retBool == false) {
557             errMaps_.emplace(REQUEST_CMD_SET_LAYER_ALPHA, ret);
558         }
559         return;
560     }
561 
OnSetLayerRegion(std::shared_ptr<CommandDataUnpacker> unpacker)562     void OnSetLayerRegion(std::shared_ptr<CommandDataUnpacker> unpacker)
563     {
564         DISPLAY_TRACE;
565 
566         uint32_t devId = 0;
567         uint32_t layerId = 0;
568         IRect rect = {0};
569 
570         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
571         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
572 
573         ret = CmdUtils::RectUnpack(unpacker, rect);
574         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
575 
576         ret = impl_->SetLayerRegion(devId, layerId, rect);
577         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
578 EXIT:
579         if (ret != HDF_SUCCESS) {
580             errMaps_.emplace(REQUEST_CMD_SET_LAYER_REGION, ret);
581         }
582         return;
583     }
584 
OnSetLayerCrop(std::shared_ptr<CommandDataUnpacker> unpacker)585     void OnSetLayerCrop(std::shared_ptr<CommandDataUnpacker> unpacker)
586     {
587         DISPLAY_TRACE;
588 
589         uint32_t devId = 0;
590         uint32_t layerId = 0;
591         IRect rect = {0};
592 
593         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
594         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
595 
596         ret = CmdUtils::RectUnpack(unpacker, rect);
597         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
598 
599         ret = impl_->SetLayerCrop(devId, layerId, rect);
600         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
601 EXIT:
602         if (ret != HDF_SUCCESS) {
603             errMaps_.emplace(REQUEST_CMD_SET_LAYER_CROP, ret);
604         }
605         return;
606     }
607 
OnSetLayerZorder(std::shared_ptr<CommandDataUnpacker> unpacker)608     void OnSetLayerZorder(std::shared_ptr<CommandDataUnpacker> unpacker)
609     {
610         DISPLAY_TRACE;
611 
612         uint32_t devId = 0;
613         uint32_t layerId = 0;
614         uint32_t zorder = 0;
615 
616         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
617         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
618 
619         ret = unpacker->ReadUint32(zorder) ? HDF_SUCCESS : HDF_FAILURE;
620         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
621 
622         ret = impl_->SetLayerZorder(devId, layerId, zorder);
623         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
624 EXIT:
625         if (ret != HDF_SUCCESS) {
626             errMaps_.emplace(REQUEST_CMD_SET_LAYER_ZORDER, ret);
627         }
628         return;
629     }
630 
OnSetLayerPreMulti(std::shared_ptr<CommandDataUnpacker> unpacker)631     void OnSetLayerPreMulti(std::shared_ptr<CommandDataUnpacker> unpacker)
632     {
633         DISPLAY_TRACE;
634 
635         uint32_t devId = 0;
636         uint32_t layerId = 0;
637         bool preMulti = false;
638 
639         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
640         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
641 
642         ret = unpacker->ReadBool(preMulti) ? HDF_SUCCESS : HDF_FAILURE;
643         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
644 
645         ret = impl_->SetLayerPreMulti(devId, layerId, preMulti);
646         DISPLAY_CHECK(ret != HDF_SUCCESS && ret != DISPLAY_NOT_SUPPORT && ret != HDF_ERR_NOT_SUPPORT, goto EXIT);
647 EXIT:
648         if (ret != HDF_SUCCESS) {
649             errMaps_.emplace(REQUEST_CMD_SET_LAYER_PREMULTI, ret);
650         }
651         return;
652     }
653 
OnSetLayerTransformMode(std::shared_ptr<CommandDataUnpacker> unpacker)654     void OnSetLayerTransformMode(std::shared_ptr<CommandDataUnpacker> unpacker)
655     {
656         DISPLAY_TRACE;
657 
658         uint32_t devId = 0;
659         uint32_t layerId = 0;
660         int32_t type = 0;
661 
662         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
663         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
664 
665         ret = unpacker->ReadInt32(type) ? HDF_SUCCESS : HDF_FAILURE;
666         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
667 
668         ret = impl_->SetLayerTransformMode(devId, layerId, static_cast<TransformType>(type));
669         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
670 EXIT:
671         if (ret != HDF_SUCCESS) {
672             errMaps_.emplace(REQUEST_CMD_SET_LAYER_TRANSFORM_MODE, ret);
673         }
674 
675         return;
676     }
677 
OnSetLayerDirtyRegion(std::shared_ptr<CommandDataUnpacker> unpacker)678     void OnSetLayerDirtyRegion(std::shared_ptr<CommandDataUnpacker> unpacker)
679     {
680         DISPLAY_TRACE;
681 
682         uint32_t devId = 0;
683         uint32_t layerId = 0;
684         uint32_t vectSize = 0;
685         int32_t ret = HDF_SUCCESS;
686 
687         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId),
688             HDF_LOGE("%{public}s, read devId error", __func__));
689 
690         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, unpacker->ReadUint32(vectSize) ? HDF_SUCCESS : HDF_FAILURE,
691             HDF_LOGE("%{public}s, read vectSize error", __func__));
692 
693         std::vector<IRect> rects(vectSize);
694         if (ret == HDF_SUCCESS) {
695             for (uint32_t i = 0; i < vectSize; i++) {
696                 DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::RectUnpack(unpacker, rects[i]),
697                     HDF_LOGE("%{public}s, read vect error, at i = %{public}d", __func__, i));
698                 if (ret != HDF_SUCCESS) {
699                     break;
700                 }
701             }
702         }
703 
704         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, impl_->SetLayerDirtyRegion(devId, layerId, rects),
705             HDF_LOGE("%{public}s, SetLayerDirtyRegion error", __func__));
706 
707         if (ret != HDF_SUCCESS) {
708             errMaps_.emplace(REQUEST_CMD_SET_LAYER_DIRTY_REGION, ret);
709         }
710         return;
711     }
712 
OnSetLayerVisibleRegion(std::shared_ptr<CommandDataUnpacker> unpacker)713     void OnSetLayerVisibleRegion(std::shared_ptr<CommandDataUnpacker> unpacker)
714     {
715         DISPLAY_TRACE;
716 
717         uint32_t devId = 0;
718         uint32_t layerId = 0;
719         uint32_t vectSize = 0;
720         int32_t ret = HDF_SUCCESS;
721 
722         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId),
723             HDF_LOGE("%{public}s, read devId error", __func__));
724 
725         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, unpacker->ReadUint32(vectSize) ? HDF_SUCCESS : HDF_FAILURE,
726             HDF_LOGE("%{public}s, read vectSize error", __func__));
727 
728         std::vector<IRect> rects(vectSize);
729         if (ret == HDF_SUCCESS) {
730             for (uint32_t i = 0; i < vectSize; i++) {
731                 DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, CmdUtils::RectUnpack(unpacker, rects[i]),
732                     HDF_LOGE("%{public}s, read vect error, at i = %{public}d", __func__, i));
733                 if (ret != HDF_SUCCESS) {
734                     break;
735                 }
736             }
737         }
738 
739         DISPLAY_CHK_CONDITION(ret, HDF_SUCCESS, impl_->SetLayerVisibleRegion(devId, layerId, rects),
740             HDF_LOGE("%{public}s, SetLayerDirtyRegion error", __func__));
741 
742         if (ret != HDF_SUCCESS) {
743             errMaps_.emplace(REQUEST_CMD_SET_LAYER_VISIBLE_REGION, ret);
744         }
745         return;
746     }
747 
748     typedef struct LayerBufferData {
749         bool isValidBuffer;
750         uint32_t devId;
751         uint32_t layerId;
752         uint32_t seqNo;
753         int32_t fence;
754         BufferHandle *buffer;
755     } LayerBufferData;
756 
UnPackLayerBufferInfo(std::shared_ptr<CommandDataUnpacker> unpacker,const std::vector<HdifdInfo> & inFds,struct LayerBufferData * data,std::vector<uint32_t> & deletingList)757     int32_t  UnPackLayerBufferInfo(std::shared_ptr<CommandDataUnpacker> unpacker, const std::vector<HdifdInfo>& inFds,
758         struct LayerBufferData *data, std::vector<uint32_t> &deletingList)
759     {
760         DISPLAY_CHK_RETURN(HDF_SUCCESS != CmdUtils::SetupDeviceUnpack(unpacker, data->devId, data->layerId),
761             HDF_FAILURE, HDF_LOGE("%{public}s, read devId error", __func__));
762 
763         DISPLAY_CHK_RETURN(HDF_SUCCESS != CmdUtils::BufferHandleUnpack(unpacker, inFds, data->buffer), HDF_FAILURE,
764             HDF_LOGE("%{public}s, read BufferHandleUnpack error", __func__));
765 
766         data->isValidBuffer = true;
767 
768         DISPLAY_CHK_RETURN(true != unpacker->ReadUint32(data->seqNo), HDF_FAILURE,
769             HDF_LOGE("%{public}s, read seqNo error", __func__));
770 
771         DISPLAY_CHK_RETURN(HDF_SUCCESS != CmdUtils::FileDescriptorUnpack(unpacker, inFds, data->fence), HDF_FAILURE,
772             HDF_LOGE("%{public}s, FileDescriptorUnpack error", __func__));
773 
774         // unpack deletingList
775         uint32_t vectSize = 0;
776         DISPLAY_CHK_RETURN(true != unpacker->ReadUint32(vectSize), HDF_FAILURE,
777             HDF_LOGE("%{public}s, read vectSize error", __func__));
778         deletingList.resize(vectSize);
779         for (uint32_t i = 0; i < vectSize; i++) {
780             DISPLAY_CHK_RETURN(true != unpacker->ReadUint32(deletingList[i]), HDF_FAILURE,
781                 HDF_LOGE("%{public}s, read seqNo error, at i = %{public}d", __func__, i));
782         }
783         return HDF_SUCCESS;
784     }
785 
OnSetLayerBuffer(std::shared_ptr<CommandDataUnpacker> unpacker,const std::vector<HdifdInfo> & inFds)786     void OnSetLayerBuffer(std::shared_ptr<CommandDataUnpacker> unpacker, const std::vector<HdifdInfo>& inFds)
787     {
788         DISPLAY_TRACE;
789 
790         struct LayerBufferData data;
791         std::vector<uint32_t> deletingList;
792 
793         int32_t ret = UnPackLayerBufferInfo(unpacker, inFds, &data, deletingList);
794         HdifdParcelable fdParcel(data.fence);
795         bool needFreeBuffer = false;
796 
797         if (ret != HDF_SUCCESS) {
798             goto EXIT;
799         }
800         {
801             DISPLAY_CHECK(cacheMgr_ == nullptr, goto EXIT);
802             std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
803             DeviceCache* devCache = nullptr;
804             LayerCache* layerCache = nullptr;
805             devCache = cacheMgr_->DeviceCacheInstance(data.devId);
806             DISPLAY_CHECK(devCache == nullptr, goto EXIT);
807             layerCache = devCache->LayerCacheInstance(data.layerId);
808             DISPLAY_CHECK(layerCache == nullptr, goto EXIT);
809 
810             ret = layerCache->SetLayerBuffer(data.buffer, data.seqNo, needFreeBuffer, deletingList,
811                 [&](const BufferHandle& handle)->int32_t {
812 #ifdef DISPLAY_COMSPOER_DEBUG_DUMP
813                 DumpLayerBuffer(data.devId, data.layerId, data.fence, handle);
814 #endif
815                 int rc = impl_->SetLayerBuffer(data.devId, data.layerId, handle, fdParcel.GetFd());
816                 DISPLAY_CHK_RETURN(rc != HDF_SUCCESS, HDF_FAILURE, HDF_LOGE(" fail"));
817                 return HDF_SUCCESS;
818             });
819         }
820 
821 #ifndef DISPLAY_COMMUNITY
822         // fix fd leak
823         if (data.buffer != nullptr && needFreeBuffer) {
824             FreeBufferWithDelay(data.buffer);
825             data.buffer = nullptr;
826             data.isValidBuffer = false;
827         }
828         fdParcel.Move();
829 #endif // DISPLAY_COMMUNITY
830 EXIT:
831         if (ret != HDF_SUCCESS) {
832             HDF_LOGE("%{public}s, SetLayerBuffer error", __func__);
833             if (data.isValidBuffer) {
834                 FreeBufferHandle(data.buffer);
835             }
836             errMaps_.emplace(REQUEST_CMD_SET_DISPLAY_CLIENT_BUFFER, ret);
837         }
838 
839         return;
840     }
841 
OnSetLayerCompositionType(std::shared_ptr<CommandDataUnpacker> unpacker)842     void OnSetLayerCompositionType(std::shared_ptr<CommandDataUnpacker> unpacker)
843     {
844         DISPLAY_TRACE;
845 
846         uint32_t devId = 0;
847         uint32_t layerId = 0;
848         int32_t type;
849         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
850         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
851 
852         ret = unpacker->ReadInt32(type) ? HDF_SUCCESS : HDF_FAILURE;
853         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
854 
855         ret = impl_->SetLayerCompositionType(devId, layerId, static_cast<CompositionType>(type));
856         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
857 EXIT:
858         if (ret != HDF_SUCCESS) {
859             errMaps_.emplace(REQUEST_CMD_SET_LAYER_COMPOSITION_TYPE, ret);
860         }
861         return;
862     }
863 
OnSetLayerBlendType(std::shared_ptr<CommandDataUnpacker> unpacker)864     void OnSetLayerBlendType(std::shared_ptr<CommandDataUnpacker> unpacker)
865     {
866         DISPLAY_TRACE;
867 
868         uint32_t devId = 0;
869         uint32_t layerId = 0;
870         int32_t type;
871         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
872         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
873 
874         ret = unpacker->ReadInt32(type) ? HDF_SUCCESS : HDF_FAILURE;
875         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
876 
877         ret = impl_->SetLayerBlendType(devId, layerId, static_cast<BlendType>(type));
878         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
879 EXIT:
880         if (ret != HDF_SUCCESS) {
881             errMaps_.emplace(REQUEST_CMD_SET_LAYER_BLEND_TYPE, ret);
882         }
883         return;
884     }
885 
OnSetLayerMaskInfo(std::shared_ptr<CommandDataUnpacker> unpacker)886     void OnSetLayerMaskInfo(std::shared_ptr<CommandDataUnpacker> unpacker)
887     {
888         DISPLAY_TRACE;
889 
890         uint32_t devId = 0;
891         uint32_t layerId = 0;
892         uint32_t maskInfo = 0;
893 
894         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
895         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
896 
897         ret = unpacker->ReadUint32(maskInfo) ? HDF_SUCCESS : HDF_FAILURE;
898         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
899 
900         ret = impl_->SetLayerMaskInfo(devId, layerId, static_cast<MaskInfo>(maskInfo));
901         DISPLAY_CHECK(ret != HDF_SUCCESS && ret != DISPLAY_NOT_SUPPORT && ret != HDF_ERR_NOT_SUPPORT, goto EXIT);
902 EXIT:
903         if (ret != HDF_SUCCESS) {
904             errMaps_.emplace(REQUEST_CMD_SET_LAYER_MASK_INFO, ret);
905         }
906         return;
907     }
908 
OnSetLayerColor(std::shared_ptr<CommandDataUnpacker> unpacker)909     void OnSetLayerColor(std::shared_ptr<CommandDataUnpacker> unpacker)
910     {
911         DISPLAY_TRACE;
912 
913         uint32_t devId = 0;
914         uint32_t layerId = 0;
915         LayerColor layerColor = {0};
916 
917         int32_t ret = CmdUtils::SetupDeviceUnpack(unpacker, devId, layerId);
918         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
919 
920         ret = CmdUtils::LayerColorUnpack(unpacker, layerColor);
921         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
922 
923         ret = impl_->SetLayerColor(devId, layerId, layerColor);
924         DISPLAY_CHECK(ret != HDF_SUCCESS, goto EXIT);
925 EXIT:
926         if (ret != HDF_SUCCESS) {
927             errMaps_.emplace(REQUEST_CMD_SET_LAYER_COLOR, ret);
928         }
929         return;
930     }
931 
PeriodDataReset()932     int32_t PeriodDataReset()
933     {
934         replyCommandCnt_ = 0;
935         errMaps_.clear();
936 
937         int32_t ret = CmdUtils::StartPack(CONTROL_CMD_REPLY_BEGIN, replyPacker_);
938         if (ret != HDF_SUCCESS) {
939             HDF_LOGE("PackBegin failure, ret=%{public}d", ret);
940         }
941         return ret;
942     }
943 
GetFileName(uint32_t devId,uint32_t layerId,const BufferHandle & buffer)944     static std::string GetFileName(uint32_t devId, uint32_t layerId, const BufferHandle& buffer)
945     {
946         struct timeval tv;
947         char nowStr[TIME_BUFFER_MAX_LEN] = {0};
948 
949         gettimeofday(&tv, nullptr);
950         if (strftime(nowStr, sizeof(nowStr), "%m-%d-%H-%M-%S", localtime(&tv.tv_sec)) == 0) {
951             HDF_LOGE("strftime failed");
952             return "";
953         };
954 
955         std::ostringstream strStream;
956         const int32_t PIXEL_BYTES = 4;
957         strStream << "hdi_layer_" << devId << "_" << layerId << "_" << buffer.stride / PIXEL_BYTES << "x" <<
958             buffer.height << "_" << nowStr << "-" << tv.tv_usec;
959         return strStream.str();
960     }
961 #ifdef DISPLAY_COMSPOER_DEBUG_DUMP
DumpLayerBuffer(uint32_t devId,uint32_t layerId,int32_t fence,const BufferHandle & buffer)962     static void DumpLayerBuffer(uint32_t devId, uint32_t layerId, int32_t fence, const BufferHandle& buffer)
963     {
964         const std::string SWITCH_ON = "on";
965         const uint32_t DUMP_BUFFER_SWITCH_LEN = 4;
966         char dumpSwitch[DUMP_BUFFER_SWITCH_LEN] = {0};
967         GetParameter("hdi.composer.dumpbuffer", "off", dumpSwitch, DUMP_BUFFER_SWITCH_LEN);
968 
969         if (SWITCH_ON.compare(dumpSwitch) != 0) {
970             return;
971         }
972 
973         const uint32_t FENCE_TIMEOUT = 3000;
974         int32_t retCode = WaitFence(fence, FENCE_TIMEOUT);
975         if (retCode != HDF_SUCCESS) {
976             return;
977         }
978 
979         if (g_bufferServiceImpl == nullptr) {
980             g_bufferServiceImpl = IMapper::Get(true);
981             DISPLAY_CHECK((g_bufferServiceImpl == nullptr), HDF_LOGE("get IMapper failed"));
982         }
983 
984         std::string fileName = GetFileName(devId, layerId, buffer);
985         DISPLAY_CHECK((fileName == ""), HDF_LOGE("GetFileName failed"));
986         HDF_LOGI("fileName = %{public}s", fileName.c_str());
987 
988         const std::string PATH_PREFIX = "/data/local/traces/";
989         std::stringstream filePath;
990         filePath << PATH_PREFIX << fileName;
991         std::ofstream rawDataFile(filePath.str(), std::ofstream::binary);
992         DISPLAY_CHECK((!rawDataFile.good()), HDF_LOGE("open file failed, %{public}s",
993             std::strerror(errno)));
994 
995         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
996         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&buffer));
997 
998         int32_t ret = 0;
999         ret = g_bufferServiceImpl->Mmap(hdiBuffer);
1000         DISPLAY_CHECK((ret != HDF_SUCCESS), HDF_LOGE("Mmap buffer failed"));
1001 
1002         std::chrono::milliseconds time_before = std::chrono::duration_cast<std::chrono::milliseconds> (
1003             std::chrono::system_clock::now().time_since_epoch()
1004         );
1005         rawDataFile.write(static_cast<const char *>(buffer.virAddr), buffer.size);
1006         std::chrono::milliseconds time_after = std::chrono::duration_cast<std::chrono::milliseconds> (
1007             std::chrono::system_clock::now().time_since_epoch()
1008         );
1009         HDF_LOGI("wirte file take time %{public}lld", time_after.count() - time_before.count());
1010         rawDataFile.close();
1011 
1012         ret = g_bufferServiceImpl->Unmap(hdiBuffer);
1013         DISPLAY_CHECK((ret != HDF_SUCCESS), HDF_LOGE("Unmap buffer failed"));
1014     }
1015 #endif
1016 
WaitFence(int32_t fence,uint32_t timeout)1017     static int32_t WaitFence(int32_t fence, uint32_t timeout)
1018     {
1019         int retCode = -1;
1020         if (fence < 0) {
1021             HDF_LOGE("The fence id is invalid.");
1022             return retCode;
1023         }
1024 
1025         struct pollfd pollfds = {0};
1026         pollfds.fd = fence;
1027         pollfds.events = POLLIN;
1028 
1029         do {
1030             retCode = poll(&pollfds, 1, timeout);
1031         } while (retCode == -1 && (errno == EINTR || errno == EAGAIN));
1032 
1033         if (retCode == 0) {
1034             retCode = -1;
1035             errno = ETIME;
1036         } else if (retCode > 0) {
1037             retCode = 0;
1038             if (pollfds.revents & (POLLERR | POLLNVAL)) {
1039                 retCode = -1;
1040                 errno = EINVAL;
1041             }
1042         }
1043 
1044         return retCode < 0 ? -errno : HDF_SUCCESS;
1045     }
1046 
FreeBufferWithDelay(BufferHandle * handle)1047     void FreeBufferWithDelay(BufferHandle *handle)
1048     {
1049         delayFreeQueue_.push(handle);
1050         if (delayFreeQueue_.size() >= BUFFER_QUEUE_MAX_SIZE) {
1051             BufferHandle *temp = delayFreeQueue_.front();
1052             delayFreeQueue_.pop();
1053             FreeBufferHandle(temp);
1054             temp = nullptr;
1055         }
1056     }
1057 
1058 protected:
1059     VdiImpl* impl_ = nullptr;
1060     std::shared_ptr<DeviceCacheManager> cacheMgr_;
1061     std::shared_ptr<Transfer> request_;
1062     bool isReplyUpdated_;
1063     std::shared_ptr<Transfer> reply_;
1064     /* period data */
1065     uint32_t replyCommandCnt_;
1066     std::shared_ptr<CommandDataPacker> replyPacker_;
1067     std::unordered_map<int32_t, int32_t> errMaps_;
1068     /* fix fd leak */
1069     std::queue<BufferHandle *> delayFreeQueue_;
1070 };
1071 using HdiDisplayCmdResponser = DisplayCmdResponser<SharedMemQueue<int32_t>, IDisplayComposerVdi>;
1072 } // namespace V1_0
1073 } // namespace Composer
1074 } // namespace Display
1075 } // namespace HDI
1076 } // namespace OHOS
1077 #endif // OHOS_HDI_DISPLAY_V1_0_DISPLAY_CMD_REQUESTER_H