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_1_DISPLAY_BUFFER_HDI_IMPL_H
17 #define OHOS_HDI_DISPLAY_V1_1_DISPLAY_BUFFER_HDI_IMPL_H
18 
19 #include <iproxy_broker.h>
20 #include <unistd.h>
21 #include "hdf_log.h"
22 #include "hilog/log.h"
23 #include "v1_0/hdi_impl/display_buffer_hdi_impl.h"
24 #include "v1_1/imetadata.h"
25 #include "v1_1/include/idisplay_buffer.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "DISP_HDI_BUFF"
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002515
31 
32 namespace OHOS {
33 namespace HDI {
34 namespace Display {
35 namespace Buffer {
36 namespace V1_1 {
37 template<typename Interface>
38 class DisplayBufferHdiImpl : public V1_0::DisplayBufferHdiImpl<Interface> {
39 public:
40     explicit DisplayBufferHdiImpl(bool isAllocLocal = false) : BaseType1_0(isAllocLocal), metadata_(nullptr)
41     {
42         while ((metadata_ = IMetadata::Get(true)) == nullptr) {
43             // Waiting for metadata service ready
44             usleep(WAIT_TIME_INTERVAL);
45         }
46     }
~DisplayBufferHdiImpl()47     virtual ~DisplayBufferHdiImpl() {};
48 
RegisterBuffer(const BufferHandle & handle)49     int32_t RegisterBuffer(const BufferHandle& handle) override
50     {
51         CHECK_NULLPOINTER_RETURN_VALUE(metadata_, HDF_FAILURE);
52         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
53         CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
54         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle));
55         int32_t ret = metadata_->RegisterBuffer(hdiBuffer);
56         return ret;
57     }
58 
SetMetadata(const BufferHandle & handle,uint32_t key,const std::vector<uint8_t> & value)59     int32_t SetMetadata(const BufferHandle& handle, uint32_t key, const std::vector<uint8_t>& value) override
60     {
61         CHECK_NULLPOINTER_RETURN_VALUE(metadata_, HDF_FAILURE);
62         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
63         CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
64         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle));
65         int32_t ret = metadata_->SetMetadata(hdiBuffer, key, value);
66         return ret;
67     }
68 
GetMetadata(const BufferHandle & handle,uint32_t key,std::vector<uint8_t> & value)69     int32_t GetMetadata(const BufferHandle& handle, uint32_t key, std::vector<uint8_t>& value) override
70     {
71         CHECK_NULLPOINTER_RETURN_VALUE(metadata_, HDF_FAILURE);
72         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
73         CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
74         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle));
75         int32_t ret = metadata_->GetMetadata(hdiBuffer, key, value);
76         return ret;
77     }
78 
ListMetadataKeys(const BufferHandle & handle,std::vector<uint32_t> & keys)79     int32_t ListMetadataKeys(const BufferHandle& handle, std::vector<uint32_t>& keys) override
80     {
81         CHECK_NULLPOINTER_RETURN_VALUE(metadata_, HDF_FAILURE);
82         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
83         CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
84         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle));
85         int32_t ret = metadata_->ListMetadataKeys(hdiBuffer, keys);
86         return ret;
87     }
88 
EraseMetadataKey(const BufferHandle & handle,uint32_t key)89     int32_t EraseMetadataKey(const BufferHandle& handle, uint32_t key) override
90     {
91         CHECK_NULLPOINTER_RETURN_VALUE(metadata_, HDF_FAILURE);
92         sptr<NativeBuffer> hdiBuffer = new NativeBuffer();
93         CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
94         hdiBuffer->SetBufferHandle(const_cast<BufferHandle*>(&handle));
95         int32_t ret = metadata_->EraseMetadataKey(hdiBuffer, key);
96         return ret;
97     }
98 private:
99     using BaseType1_0 = V1_0::DisplayBufferHdiImpl<Interface>;
100 protected:
101     using BaseType1_0::WAIT_TIME_INTERVAL;
102     sptr<IMetadata> metadata_;
103 };
104 using HdiDisplayBufferImpl = DisplayBufferHdiImpl<V1_1::IDisplayBuffer>;
105 } // namespace V1_1
106 } // namespace Buffer
107 } // namespace Display
108 } // namespace HDI
109 } // namespace OHOS
110 
111 #endif // OHOS_HDI_DISPLAY_V1_1_DISPLAY_BUFFER_HDI_IMPL_H
112