• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRAMEWORKS_SURFACE_INCLUDE_CONSUMER_SURFACE_H
17 #define FRAMEWORKS_SURFACE_INCLUDE_CONSUMER_SURFACE_H
18 
19 #include <map>
20 #include <string>
21 
22 #include <iconsumer_surface.h>
23 
24 #include "buffer_queue.h"
25 #include "buffer_queue_producer.h"
26 #include "buffer_queue_consumer.h"
27 
28 namespace OHOS {
29 class ConsumerSurface : public IConsumerSurface {
30 public:
31     ConsumerSurface(const std::string &name);
32     virtual ~ConsumerSurface();
33     GSError Init();
34 
35     bool IsConsumer() const override;
36     sptr<IBufferProducer> GetProducer() const override;
37 
38     GSError AcquireBuffer(sptr<SurfaceBuffer>& buffer, int32_t &fence,
39                           int64_t &timestamp, Rect &damage) override;
40 
41     GSError ReleaseBuffer(sptr<SurfaceBuffer>& buffer, int32_t fence) override;
42 
43     GSError AcquireBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence,
44                           int64_t &timestamp, Rect &damage) override;
45     GSError AcquireBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence,
46                           int64_t &timestamp, std::vector<Rect> &damages) override;
47     GSError AcquireBuffer(AcquireBufferReturnValue &returnValue, int64_t expectPresentTimestamp,
48                           bool isUsingAutoTimestamp) override;
49     GSError ReleaseBuffer(sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& fence) override;
50 
51     GSError AttachBuffer(sptr<SurfaceBuffer>& buffer) override;
52     GSError DetachBuffer(sptr<SurfaceBuffer>& buffer) override;
53 
54     bool QueryIfBufferAvailable() override;
55 
56     uint32_t GetQueueSize() override;
57     GSError SetQueueSize(uint32_t queueSize) override;
58 
59     const std::string& GetName() override;
60 
61     GSError SetDefaultWidthAndHeight(int32_t width, int32_t height) override;
62     int32_t GetDefaultWidth() override;
63     int32_t GetDefaultHeight() override;
64     GSError SetDefaultUsage(uint64_t usage) override;
65     uint64_t GetDefaultUsage() override;
66 
67     GSError SetUserData(const std::string &key, const std::string &val) override;
68     std::string GetUserData(const std::string &key) override;
69 
70     GSError RegisterConsumerListener(sptr<IBufferConsumerListener>& listener) override;
71     GSError RegisterConsumerListener(IBufferConsumerListenerClazz *listener) override;
72     GSError RegisterReleaseListener(OnReleaseFunc func) override;
UnRegisterReleaseListener()73     GSError UnRegisterReleaseListener() override
74     {
75         return GSERROR_OK;
76     }
77     GSError RegisterDeleteBufferListener(OnDeleteBufferFunc func, bool isForUniRedraw = false) override;
78     GSError UnregisterConsumerListener() override;
79 
80     uint64_t GetUniqueId() const override;
81 
82     void Dump(std::string &result) const override;
83 
84     GSError GoBackground() override;
85 
86     GSError SetTransform(GraphicTransformType transform) override;
87     GraphicTransformType GetTransform() const override;
88     GSError SetScalingMode(uint32_t sequence, ScalingMode scalingMode) override;
89     GSError GetScalingMode(uint32_t sequence, ScalingMode &scalingMode) override;
90     GSError SetMetaData(uint32_t sequence, const std::vector<GraphicHDRMetaData> &metaData) override;
91     GSError SetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey key, const std::vector<uint8_t> &metaData) override;
92     GSError QueryMetaDataType(uint32_t sequence, HDRMetaDataType &type) const override;
93     GSError GetMetaData(uint32_t sequence, std::vector<GraphicHDRMetaData> &metaData) const override;
94     GSError GetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey &key,
95                            std::vector<uint8_t> &metaData) const override;
96     GSError SetTunnelHandle(const GraphicExtDataHandle *handle) override;
97     sptr<SurfaceTunnelHandle> GetTunnelHandle() const override;
98     GSError SetPresentTimestamp(uint32_t sequence, const GraphicPresentTimestamp &timestamp) override;
99 
100     GSError AttachBuffer(sptr<SurfaceBuffer>& buffer, int32_t timeOut) override;
101     GSError RegisterSurfaceDelegator(sptr<IRemoteObject> client) override;
RegisterReleaseListener(OnReleaseFuncWithFence func)102     GSError RegisterReleaseListener(OnReleaseFuncWithFence func) override
103     {
104         return GSERROR_NOT_SUPPORT;
105     }
106     GSError RegisterUserDataChangeListener(const std::string &funcName, OnUserDataChangeFunc func) override;
107     GSError UnRegisterUserDataChangeListener(const std::string &funcName) override;
108     GSError ClearUserDataChangeListener() override;
109     void ConsumerRequestCpuAccess(bool on) override;
110     GSError AttachBufferToQueue(sptr<SurfaceBuffer> buffer) override;
111     /**
112      * @brief if isReserveSlot is true, a slot in the bufferqueue will be kept
113      * empty until attachbuffer is used to fill the slot.
114      * if isReserveSlot is true, it must used with AttachBufferToQueue together.
115      */
116     GSError DetachBufferFromQueue(sptr<SurfaceBuffer> buffer, bool isReserveSlot = false) override;
117     GraphicTransformType GetTransformHint() const override;
118     GSError SetTransformHint(GraphicTransformType transformHint) override;
IsBufferHold()119     inline bool IsBufferHold() override
120     {
121         if (consumer_ == nullptr) {
122             return false;
123         }
124         return consumer_->IsBufferHold();
125     }
126     void SetBufferHold(bool hold) override;
127     GSError SetScalingMode(ScalingMode scalingMode) override;
128     GSError SetSurfaceSourceType(OHSurfaceSource sourceType) override;
129     OHSurfaceSource GetSurfaceSourceType() const override;
130     GSError SetSurfaceAppFrameworkType(std::string appFrameworkType) override;
131     std::string GetSurfaceAppFrameworkType() const override;
132     float GetHdrWhitePointBrightness() const override;
133     float GetSdrWhitePointBrightness() const override;
134 
135     GSError GetSurfaceBufferTransformType(sptr<SurfaceBuffer> buffer, GraphicTransformType *transformType) override;
136     GSError IsSurfaceBufferInCache(uint32_t seqNum, bool &isInCache) override;
137     GSError GetGlobalAlpha(int32_t &alpha) override;
138     uint32_t GetAvailableBufferCount() const override;
139     GSError GetLastFlushedDesiredPresentTimeStamp(int64_t &lastFlushedDesiredPresentTimeStamp) const override;
140     GSError GetBufferSupportFastCompose(bool &bufferSupportFastCompose) override;
141     GSError GetBufferCacheConfig(const sptr<SurfaceBuffer>& buffer, BufferRequestConfig& config) override;
142     GSError GetCycleBuffersNumber(uint32_t& cycleBuffersNumber) override;
SetCycleBuffersNumber(uint32_t cycleBuffersNumber)143     GSError SetCycleBuffersNumber(uint32_t cycleBuffersNumber) override
144     {
145         (void)cycleBuffersNumber;
146         return GSERROR_NOT_SUPPORT;
147     }
148     GSError GetLastConsumeTime(int64_t &lastConsumeTime) const override;
149 private:
150     std::map<std::string, std::string> userData_;
151     sptr<BufferQueueProducer> producer_ = nullptr;
152     sptr<BufferQueueConsumer> consumer_ = nullptr;
153     std::string name_ = "not init";
154     std::map<std::string, OnUserDataChangeFunc> onUserDataChange_;
155     std::mutex lockMutex_;
156     uint64_t uniqueId_ = 0;
157     std::atomic<bool> hasRegistercallBackForRT_ = false;
158     std::atomic<bool> hasRegistercallBackForRedraw_ = false;
159     std::atomic<bool> isFirstBuffer_ = true;
160     std::atomic<bool> supportFastCompose_ = false;
161 };
162 } // namespace OHOS
163 
164 #endif // FRAMEWORKS_SURFACE_INCLUDE_CONSUMER_SURFACE_H
165