blob: 9ed80b46bdf93fea8a848d8f154a4c54ea511b3a [file] [log] [blame]
Chia-I Wuf1405182017-11-27 11:29:21 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_BUFFERLAYERCONSUMER_H
18#define ANDROID_BUFFERLAYERCONSUMER_H
19
Alec Mourid7b3a8b2019-03-21 11:44:18 -070020#include <android-base/thread_annotations.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080021#include <gui/BufferQueueDefs.h>
22#include <gui/ConsumerBase.h>
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070023#include <gui/HdrMetadata.h>
Alec Mouria90a5702021-04-16 16:36:21 +000024#include <renderengine/ExternalTexture.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080025#include <ui/FenceTime.h>
26#include <ui/GraphicBuffer.h>
Peiyong Lin34beb7a2018-03-28 11:57:12 -070027#include <ui/GraphicTypes.h>
Chia-I Wu67dcc692017-11-27 14:51:06 -080028#include <ui/Region.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080029#include <utils/String8.h>
30#include <utils/Vector.h>
31#include <utils/threads.h>
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Chia-I Wuc75c44d2017-11-27 14:32:57 -080036class Layer;
Chia-I Wuf1405182017-11-27 11:29:21 -080037class String8;
38
Peiyong Lin833074a2018-08-28 11:53:54 -070039namespace renderengine {
Lloyd Pique144e1162017-12-20 16:44:52 -080040class RenderEngine;
Peiyong Lin833074a2018-08-28 11:53:54 -070041} // namespace renderengine
Lloyd Pique144e1162017-12-20 16:44:52 -080042
Chia-I Wuf1405182017-11-27 11:29:21 -080043/*
44 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080045 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080046 *
Chia-I Wu221b5922017-12-14 13:59:16 -080047 * A typical usage pattern is to call updateTexImage() when a new frame is
48 * desired. If a new frame is available, the frame is latched. If not, the
49 * previous contents are retained. The texture is attached and updated after
50 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080051 *
Chia-I Wu221b5922017-12-14 13:59:16 -080052 * All calls to updateTexImage must be made with RenderEngine being current.
53 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080054 */
55class BufferLayerConsumer : public ConsumerBase {
56public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080057 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
58
59 class BufferRejecter {
60 friend class BufferLayerConsumer;
61 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
62
63 protected:
64 virtual ~BufferRejecter() {}
65 };
66
Chia-I Wufd257f82017-11-27 14:51:06 -080067 struct ContentsChangedListener : public FrameAvailableListener {
68 virtual void onSidebandStreamChanged() = 0;
69 };
Chia-I Wuf1405182017-11-27 11:29:21 -080070
Chia-I Wu221b5922017-12-14 13:59:16 -080071 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
72 // tex parameter indicates the name of the RenderEngine texture to which
73 // images are to be streamed.
Peiyong Lin833074a2018-08-28 11:53:54 -070074 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, renderengine::RenderEngine& engine,
Lloyd Pique144e1162017-12-20 16:44:52 -080075 uint32_t tex, Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080076
Chia-I Wufd257f82017-11-27 14:51:06 -080077 // Sets the contents changed listener. This should be used instead of
78 // ConsumerBase::setFrameAvailableListener().
79 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
80
Chia-I Wuf1405182017-11-27 11:29:21 -080081 // updateTexImage acquires the most recently queued buffer, and sets the
82 // image contents of the target texture to it.
83 //
Chia-I Wu221b5922017-12-14 13:59:16 -080084 // This call may only be made while RenderEngine is current.
Chia-I Wuf1405182017-11-27 11:29:21 -080085 //
Chia-I Wu221b5922017-12-14 13:59:16 -080086 // This calls doFenceWait to ensure proper synchronization unless native
87 // fence is supported.
Chia-I Wuda5c7302017-11-27 14:51:06 -080088 //
Chia-I Wu221b5922017-12-14 13:59:16 -080089 // Unlike the GLConsumer version, this version takes a functor that may be
90 // used to reject the newly acquired buffer. It also does not bind the
91 // RenderEngine texture until bindTextureImage is called.
Ana Krulec010d2192018-10-08 06:29:54 -070092 status_t updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Alec Mouri56e538f2019-01-14 15:22:01 -080093 bool* autoRefresh, bool* queuedBuffer, uint64_t maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -080094
Chia-I Wuf1405182017-11-27 11:29:21 -080095 // setReleaseFence stores a fence that will signal when the current buffer
96 // is no longer being read. This fence will be returned to the producer
97 // when the current buffer is released by updateTexImage(). Multiple
98 // fences can be set for a given buffer; they will be merged into a single
99 // union fence.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800100 void setReleaseFence(const sp<Fence>& fence);
101
102 bool releasePendingBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800103
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800104 sp<Fence> getPrevFinalReleaseFence() const;
105
Chia-I Wu221b5922017-12-14 13:59:16 -0800106 // See GLConsumer::getTransformMatrix.
Chia-I Wuf1405182017-11-27 11:29:21 -0800107 void getTransformMatrix(float mtx[16]);
108
Chia-I Wuf1405182017-11-27 11:29:21 -0800109 // getTimestamp retrieves the timestamp associated with the texture image
110 // set by the most recent call to updateTexImage.
111 //
112 // The timestamp is in nanoseconds, and is monotonically increasing. Its
113 // other semantics (zero point, etc) are source-dependent and should be
114 // documented by the source.
115 int64_t getTimestamp();
116
117 // getDataSpace retrieves the DataSpace associated with the texture image
118 // set by the most recent call to updateTexImage.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700119 ui::Dataspace getCurrentDataSpace();
Chia-I Wuf1405182017-11-27 11:29:21 -0800120
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700121 // getCurrentHdrMetadata retrieves the HDR metadata associated with the
122 // texture image set by the most recent call to updateTexImage.
123 const HdrMetadata& getCurrentHdrMetadata() const;
124
Chia-I Wuf1405182017-11-27 11:29:21 -0800125 // getFrameNumber retrieves the frame number associated with the texture
126 // image set by the most recent call to updateTexImage.
127 //
128 // The frame number is an incrementing counter set to 0 at the creation of
129 // the BufferQueue associated with this consumer.
130 uint64_t getFrameNumber();
131
Chia-I Wu67dcc692017-11-27 14:51:06 -0800132 bool getTransformToDisplayInverse() const;
133
134 // must be called from SF main thread
135 const Region& getSurfaceDamage() const;
136
Steven Thomas44685cb2019-07-23 16:19:31 -0700137 // Merge the given damage region into the current damage region value.
138 void mergeSurfaceDamage(const Region& damage);
139
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800140 // getCurrentApi retrieves the API which queues the current buffer.
141 int getCurrentApi() const;
142
Chia-I Wu221b5922017-12-14 13:59:16 -0800143 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800144 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
145
146 // setFilteringEnabled sets whether the transform matrix should be computed
147 // for use with bilinear filtering.
148 void setFilteringEnabled(bool enabled);
149
150 // getCurrentBuffer returns the buffer associated with the current image.
151 // When outSlot is not nullptr, the current buffer slot index is also
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000152 // returned. Simiarly, when outFence is not nullptr, the current output
153 // fence is returned.
Alec Mouria90a5702021-04-16 16:36:21 +0000154 std::shared_ptr<renderengine::ExternalTexture> getCurrentBuffer(
155 int* outSlot = nullptr, sp<Fence>* outFence = nullptr) const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800156
Chia-I Wuf1405182017-11-27 11:29:21 -0800157 // getCurrentCrop returns the cropping rectangle of the current buffer.
158 Rect getCurrentCrop() const;
159
160 // getCurrentTransform returns the transform of the current buffer.
161 uint32_t getCurrentTransform() const;
162
163 // getCurrentScalingMode returns the scaling mode of the current buffer.
164 uint32_t getCurrentScalingMode() const;
165
166 // getCurrentFence returns the fence indicating when the current buffer is
167 // ready to be read from.
168 sp<Fence> getCurrentFence() const;
169
170 // getCurrentFence returns the FenceTime indicating when the current
171 // buffer is ready to be read from.
172 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
173
174 // setConsumerUsageBits overrides the ConsumerBase method to OR
175 // DEFAULT_USAGE_FLAGS to usage.
176 status_t setConsumerUsageBits(uint64_t usage);
Alec Mouri485e4c32019-04-30 18:24:05 -0700177 void onBufferAvailable(const BufferItem& item) EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800178
Chia-I Wuf1405182017-11-27 11:29:21 -0800179protected:
180 // abandonLocked overrides the ConsumerBase method to clear
181 // mCurrentTextureImage in addition to the ConsumerBase behavior.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700182 virtual void abandonLocked() EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800183
184 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
185 // specific info in addition to the ConsumerBase behavior.
186 virtual void dumpLocked(String8& result, const char* prefix) const;
187
Alec Mouri39801c02018-10-10 10:44:47 -0700188 // See ConsumerBase::acquireBufferLocked
Chia-I Wuf1405182017-11-27 11:29:21 -0800189 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700190 uint64_t maxFrameNumber = 0) override
191 EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800192
Chia-I Wu6748db42017-12-01 10:53:53 -0800193 bool canUseImageCrop(const Rect& crop) const;
194
Chia-I Wuf1405182017-11-27 11:29:21 -0800195 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800196 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800197
198 bool isPending;
199 int currentTexture;
200 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800201 };
202
203 // This releases the buffer in the slot referenced by mCurrentTexture,
204 // then updates state to refer to the BufferItem, which must be a
205 // newly-acquired buffer. If pendingRelease is not null, the parameters
206 // which would have been passed to releaseBufferLocked upon the successful
207 // completion of the method will instead be returned to the caller, so that
208 // it may call releaseBufferLocked itself later.
209 status_t updateAndReleaseLocked(const BufferItem& item,
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700210 PendingRelease* pendingRelease = nullptr)
211 EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800212
Chia-I Wuf1405182017-11-27 11:29:21 -0800213private:
Alec Mourib5c4f352019-02-19 19:46:38 -0800214 // Utility class for managing GraphicBuffer references into renderengine
215 class Image {
216 public:
Alec Mouri16a99402019-07-29 16:37:30 -0700217 Image(const sp<GraphicBuffer>& graphicBuffer, renderengine::RenderEngine& engine);
Alec Mourib5c4f352019-02-19 19:46:38 -0800218 virtual ~Image();
219 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
220
221 private:
222 // mGraphicBuffer is the buffer that was used to create this image.
223 sp<GraphicBuffer> mGraphicBuffer;
224 // Back-reference into renderengine to initiate cleanup.
225 renderengine::RenderEngine& mRE;
226 DISALLOW_COPY_AND_ASSIGN(Image);
227 };
228
Chia-I Wuf1405182017-11-27 11:29:21 -0800229 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800230 // initialized this will release the reference to the GraphicBuffer in
Alec Mouri39801c02018-10-10 10:44:47 -0700231 // that slot. Otherwise it has no effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800232 //
233 // This method must be called with mMutex locked.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700234 virtual void freeBufferLocked(int slotIndex) EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800235
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800236 // IConsumerListener interface
237 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800238 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800239 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
240 FrameEventHistoryDelta* outDelta) override;
241
Chia-I Wuf1405182017-11-27 11:29:21 -0800242 // computeCurrentTransformMatrixLocked computes the transform matrix for the
243 // current texture. It uses mCurrentTransform and the current GraphicBuffer
244 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800245 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800246 void computeCurrentTransformMatrixLocked();
247
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800248 // getCurrentCropLocked returns the cropping rectangle of the current buffer.
249 Rect getCurrentCropLocked() const;
250
Chia-I Wuf1405182017-11-27 11:29:21 -0800251 // The default consumer usage flags that BufferLayerConsumer always sets on its
252 // BufferQueue instance; these will be OR:d with any additional flags passed
253 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
254 // consume buffers as hardware textures.
255 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
256
Alec Mourib5c4f352019-02-19 19:46:38 -0800257 // mCurrentTextureBuffer is the buffer containing the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800258 // possible that this buffer is not associated with any buffer slot, so we
259 // must track it separately in order to support the getCurrentBuffer method.
Alec Mouria90a5702021-04-16 16:36:21 +0000260 std::shared_ptr<renderengine::ExternalTexture> mCurrentTextureBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000261
Chia-I Wuf1405182017-11-27 11:29:21 -0800262 // mCurrentCrop is the crop rectangle that applies to the current texture.
263 // It gets set each time updateTexImage is called.
264 Rect mCurrentCrop;
265
266 // mCurrentTransform is the transform identifier for the current texture. It
267 // gets set each time updateTexImage is called.
268 uint32_t mCurrentTransform;
269
270 // mCurrentScalingMode is the scaling mode for the current texture. It gets
271 // set each time updateTexImage is called.
272 uint32_t mCurrentScalingMode;
273
274 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
275 sp<Fence> mCurrentFence;
276
277 // The FenceTime wrapper around mCurrentFence.
278 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
279
280 // mCurrentTransformMatrix is the transform matrix for the current texture.
281 // It gets computed by computeTransformMatrix each time updateTexImage is
282 // called.
283 float mCurrentTransformMatrix[16];
284
285 // mCurrentTimestamp is the timestamp for the current texture. It
286 // gets set each time updateTexImage is called.
287 int64_t mCurrentTimestamp;
288
289 // mCurrentDataSpace is the dataspace for the current texture. It
290 // gets set each time updateTexImage is called.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700291 ui::Dataspace mCurrentDataSpace;
Chia-I Wuf1405182017-11-27 11:29:21 -0800292
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700293 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
294 // gets set each time updateTexImage is called.
295 HdrMetadata mCurrentHdrMetadata;
296
Chia-I Wuf1405182017-11-27 11:29:21 -0800297 // mCurrentFrameNumber is the frame counter for the current texture.
298 // It gets set each time updateTexImage is called.
299 uint64_t mCurrentFrameNumber;
300
Chia-I Wu67dcc692017-11-27 14:51:06 -0800301 // Indicates this buffer must be transformed by the inverse transform of the screen
302 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
303 // This must be set/read from SurfaceFlinger's main thread.
304 bool mCurrentTransformToDisplayInverse;
305
306 // The portion of this surface that has changed since the previous frame
307 Region mCurrentSurfaceDamage;
308
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800309 int mCurrentApi;
310
Chia-I Wuf1405182017-11-27 11:29:21 -0800311 uint32_t mDefaultWidth, mDefaultHeight;
312
313 // mFilteringEnabled indicates whether the transform matrix is computed for
314 // use with bilinear filtering. It defaults to true and is changed by
315 // setFilteringEnabled().
316 bool mFilteringEnabled;
317
Peiyong Lin833074a2018-08-28 11:53:54 -0700318 renderengine::RenderEngine& mRE;
Chia-I Wu9f2db772017-11-30 21:06:50 -0800319
Chia-I Wu221b5922017-12-14 13:59:16 -0800320 // mTexName is the name of the RenderEngine texture to which streamed
321 // images will be bound when bindTexImage is called. It is set at
322 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800323 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800324
chaviw1a4dba42020-05-27 15:16:15 -0700325 // The layer for this BufferLayerConsumer. Always check mAbandoned before accessing.
326 Layer* mLayer GUARDED_BY(mMutex);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800327
Chia-I Wufd257f82017-11-27 14:51:06 -0800328 wp<ContentsChangedListener> mContentsChangedListener;
329
Chia-I Wuf1405182017-11-27 11:29:21 -0800330 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800331 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800332 // indicating that no buffer slot is currently bound to the texture. Note,
333 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
334 // that no buffer is bound to the texture. A call to setBufferCount will
335 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
336 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800337
Alec Mourib5c4f352019-02-19 19:46:38 -0800338 // Shadow buffer cache for cleaning up renderengine references.
Alec Mouria90a5702021-04-16 16:36:21 +0000339 std::shared_ptr<renderengine::ExternalTexture>
340 mImages[BufferQueueDefs::NUM_BUFFER_SLOTS] GUARDED_BY(mImagesMutex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700341
342 // Separate mutex guarding the shadow buffer cache.
343 // mImagesMutex can be manipulated with binder threads (e.g. onBuffersAllocated)
344 // which is contentious enough that we can't just use mMutex.
345 mutable std::mutex mImagesMutex;
Alec Mouri39801c02018-10-10 10:44:47 -0700346
Chia-I Wuda5c7302017-11-27 14:51:06 -0800347 // A release that is pending on the receipt of a new release fence from
348 // presentDisplay
349 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800350};
351
352// ----------------------------------------------------------------------------
353}; // namespace android
354
355#endif // ANDROID_BUFFERLAYERCONSUMER_H