• 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_PRODUCER_SURFACE_H
17 #define FRAMEWORKS_SURFACE_INCLUDE_PRODUCER_SURFACE_H
18 
19 #include <atomic>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include <surface.h>
25 #include <ibuffer_producer.h>
26 
27 #include "buffer_queue.h"
28 #include "buffer_queue_consumer.h"
29 #include "surface_buffer.h"
30 #include "producer_surface_delegator.h"
31 
32 struct NativeWindow;
33 namespace OHOS {
34 class ProducerSurface : public Surface {
35 public:
36     ProducerSurface(sptr<IBufferProducer>& producer);
37     virtual ~ProducerSurface();
38 
39     // thread unsafe
40     GSError Init();
41 
42     bool IsConsumer() const override;
43     sptr<IBufferProducer> GetProducer() const override;
44     GSError RequestBuffer(sptr<SurfaceBuffer>& buffer,
45                           int32_t &fence, BufferRequestConfig &config) override;
46 
47     GSError RequestBuffers(std::vector<sptr<SurfaceBuffer>> &buffers,
48         std::vector<sptr<SyncFence>> &fences, BufferRequestConfig &config) override;
49 
50     GSError CancelBuffer(sptr<SurfaceBuffer>& buffer) override;
51 
52     GSError FlushBuffer(sptr<SurfaceBuffer>& buffer,
53                         int32_t fence, BufferFlushConfig &config) override;
54 
55     GSError FlushBuffers(const std::vector<sptr<SurfaceBuffer>> &buffers,
56         const std::vector<sptr<SyncFence>> &fences, const std::vector<BufferFlushConfigWithDamages> &config) override;
57 
58     GSError RequestBuffer(sptr<SurfaceBuffer>& buffer,
59                           sptr<SyncFence>& fence, BufferRequestConfig &config) override;
60     GSError FlushBuffer(sptr<SurfaceBuffer>& buffer,
61                         const sptr<SyncFence>& fence, BufferFlushConfig &config) override;
62     GSError GetLastFlushedBuffer(sptr<SurfaceBuffer>& buffer,
63         sptr<SyncFence>& fence, float matrix[16], bool isUseNewMatrix = false) override;
64     GSError FlushBuffer(sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& fence,
65                         BufferFlushConfigWithDamages &config) override;
66 
67     GSError AttachBuffer(sptr<SurfaceBuffer>& buffer) override;
68     GSError AttachBuffer(sptr<SurfaceBuffer>& buffer, int32_t timeOut) override;
69 
70     GSError DetachBuffer(sptr<SurfaceBuffer>& buffer) override;
71 
72     uint32_t GetQueueSize() override;
73     GSError SetQueueSize(uint32_t queueSize) override;
74 
75     const std::string& GetName() override;
76     uint64_t GetUniqueId() const override;
77     int32_t GetDefaultWidth() override;
78     int32_t GetDefaultHeight() override;
79     GSError SetDefaultUsage(uint64_t usage) override;
80     uint64_t GetDefaultUsage() override;
81     void SetBufferHold(bool hold) override;
82 
83     GSError SetUserData(const std::string &key, const std::string &val) override;
84     std::string GetUserData(const std::string &key) override;
85 
86     GSError RegisterReleaseListener(OnReleaseFunc func) override;
87     GSError RegisterReleaseListener(OnReleaseFuncWithFence func) override;
88     GSError RegisterReleaseListenerBackup(OnReleaseFuncWithFence func) override;
89     GSError UnRegisterReleaseListener() override;
90     GSError UnRegisterReleaseListenerBackup() override;
91 
Dump(std::string & result)92     void Dump(std::string &result) const override {};
93 
94     // Call carefully. This interface will empty all caches of the current process
95     GSError CleanCache(bool cleanAll = false) override;
96     GSError GoBackground() override;
97 
98     GSError SetTransform(GraphicTransformType transform) override;
99     GraphicTransformType GetTransform() const override;
100 
101     GSError Connect() override;
102     GSError Disconnect() override;
103     GSError SetScalingMode(uint32_t sequence, ScalingMode scalingMode) override;
104     GSError SetMetaData(uint32_t sequence, const std::vector<GraphicHDRMetaData> &metaData) override;
105     GSError SetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey key, const std::vector<uint8_t> &metaData) override;
106     GSError SetTunnelHandle(const GraphicExtDataHandle *handle) override;
107     GSError GetPresentTimestamp(uint32_t sequence, GraphicPresentTimestampType type, int64_t &time) const override;
108     GSError SetWptrNativeWindowToPSurface(void* nativeWindow) override;
109     virtual GSError RegisterSurfaceDelegator(sptr<IRemoteObject> client) override;
110     GSError RegisterUserDataChangeListener(const std::string &funcName, OnUserDataChangeFunc func) override;
111     GSError UnRegisterUserDataChangeListener(const std::string &funcName) override;
112     GSError ClearUserDataChangeListener() override;
113     GSError AttachBufferToQueue(sptr<SurfaceBuffer> buffer) override;
114     GSError DetachBufferFromQueue(sptr<SurfaceBuffer> buffer, bool isReserveSlot = false) override;
115     GraphicTransformType GetTransformHint() const override;
116     GSError SetTransformHint(GraphicTransformType transformHint) override;
117     GSError SetBufferName(const std::string &name) override;
118 
119     void SetRequestWidthAndHeight(int32_t width, int32_t height) override;
120     int32_t GetRequestWidth() override;
121     int32_t GetRequestHeight() override;
122     GSError SetScalingMode(ScalingMode scalingMode) override;
123     GSError SetSurfaceSourceType(OHSurfaceSource sourceType) override;
124     OHSurfaceSource GetSurfaceSourceType() const override;
125     GSError SetSurfaceAppFrameworkType(std::string appFrameworkType) override;
126     std::string GetSurfaceAppFrameworkType() const override;
127 
128     void SetWindowConfig(const BufferRequestConfig& config) override;
129     void SetWindowConfigWidthAndHeight(int32_t width, int32_t height) override;
130     void SetWindowConfigStride(int32_t stride) override;
131     void SetWindowConfigFormat(int32_t format) override;
132     void SetWindowConfigUsage(uint64_t usage) override;
133     void SetWindowConfigTimeout(int32_t timeout) override;
134     void SetWindowConfigColorGamut(GraphicColorGamut colorGamut) override;
135     void SetWindowConfigTransform(GraphicTransformType transform) override;
136     BufferRequestConfig GetWindowConfig() override;
137     GSError SetHdrWhitePointBrightness(float brightness) override;
138     GSError SetSdrWhitePointBrightness(float brightness) override;
139     GSError GetProducerInitInfo(ProducerInitInfo &info) override;
140     GSError AcquireLastFlushedBuffer(sptr<SurfaceBuffer> &buffer, sptr<SyncFence> &fence,
141         float matrix[16], uint32_t matrixSize, bool isUseNewMatrix) override;
142     GSError ReleaseLastFlushedBuffer(sptr<SurfaceBuffer> buffer) override;
143     GSError SetGlobalAlpha(int32_t alpha) override;
IsInHebcList()144     virtual bool IsInHebcList() override
145     {
146         return initInfo_.isInHebcList;
147     }
148     GSError RequestAndDetachBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence,
149                                    BufferRequestConfig& config) override;
150     GSError AttachAndFlushBuffer(sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& fence,
151                                  BufferFlushConfig& config, bool needMap) override;
152     GSError GetCycleBuffersNumber(uint32_t& cycleBuffersNumber) override;
153     GSError SetCycleBuffersNumber(uint32_t cycleBuffersNumber) override;
154     /**
155     * @brief In the strictly disconnected state, the producer must call the ConnectStrictly() interface before request
156     *        buffer. Unlike Connect(), ConnectStrictly() does not distinguish between process IDs (PIDs) and is
157     *        suitable for stricter connection management scenarios.
158     */
159     GSError ConnectStrictly() override;
160     /**
161     * @brief After calling DisconnectStrictly(), the consumer (server) enter the strictly disconnected state.
162     *        In this state, any attempt by the producer (client) to request buffer will fail and return the error code
163     *        GSERROR_CONSUMER_DISCONNECTED.
164     */
165     GSError DisconnectStrictly() override;
166 private:
167     GSError PropertyChangeCallback(const SurfaceProperty& property);
168     GSError RegisterPropertyListenerInner(OnPropertyChangeFunc func, uint64_t producerId);
169     GSError UnRegisterPropertyListenerInner(uint64_t producerId);
170     bool IsRemote();
171     void CleanAllLocked(uint32_t *bufSeqNum);
172     GSError AddCacheLocked(sptr<BufferExtraData> &bedataimpl,
173         IBufferProducer::RequestBufferReturnValue &retval, BufferRequestConfig &config);
174     GSError SetMetadataValue(sptr<SurfaceBuffer>& buffer);
175     GSError CleanCacheLocked(bool cleanAll);
176     void SetBufferConfigLocked(sptr<BufferExtraData>& bedataimpl,
177         IBufferProducer::RequestBufferReturnValue& retval, BufferRequestConfig& config);
178     void DeleteCacheBufferLocked(sptr<BufferExtraData>& bedataimpl,
179         IBufferProducer::RequestBufferReturnValue& retval, BufferRequestConfig& config);
180     GSError UpdateCacheLocked(sptr<BufferExtraData>& bedataimpl,
181         IBufferProducer::RequestBufferReturnValue& retval, BufferRequestConfig& config);
182     void ReleasePreCacheBuffer(int bufferCacheSize);
183 
184     mutable std::mutex mutex_;
185     std::atomic_bool inited_ = false;
186     std::map<int32_t, sptr<SurfaceBuffer>> bufferProducerCache_;
187     std::map<std::string, std::string> userData_;
188     sptr<IBufferProducer> producer_ = nullptr;
189     std::string name_ = "not init";
190     uint64_t queueId_ = 0;
191     bool isDisconnected_ = true;
192     sptr<IProducerListener> listener_;
193     sptr<IProducerListener> propertyListener_;
194     sptr<IProducerListener> listenerBackup_;
195     std::mutex listenerMutex_;
196     wptr<NativeWindow> wpNativeWindow_ = nullptr;
197     wptr<ProducerSurfaceDelegator> wpPSurfaceDelegator_ = nullptr;
198     std::mutex delegatorMutex_;
199     std::map<std::string, OnUserDataChangeFunc> onUserDataChange_;
200     std::mutex lockMutex_;
201     std::string bufferName_ = "";
202     int32_t requestWidth_ = 0;
203     int32_t requestHeight_ = 0;
204     GraphicTransformType lastSetTransformHint_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
205     BufferRequestConfig windowConfig_ = {0};
206     ProducerInitInfo initInfo_ = {0};
207     sptr<SurfaceBuffer> preCacheBuffer_ = nullptr;
208 };
209 } // namespace OHOS
210 
211 #endif // FRAMEWORKS_SURFACE_INCLUDE_PRODUCER_SURFACE_H
212