blob: 23ad2a3f9153c43665e978aca2fee8de05496adf [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;
Alec Mouri8977ce92022-05-07 00:34:50 +0000239 void addAndGetFrameTimestamps(const NewFrameEventsEntry*, FrameEventHistoryDelta*) override {}
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800240
Chia-I Wuf1405182017-11-27 11:29:21 -0800241 // computeCurrentTransformMatrixLocked computes the transform matrix for the
242 // current texture. It uses mCurrentTransform and the current GraphicBuffer
243 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800244 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800245 void computeCurrentTransformMatrixLocked();
246
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800247 // getCurrentCropLocked returns the cropping rectangle of the current buffer.
248 Rect getCurrentCropLocked() const;
249
Chia-I Wuf1405182017-11-27 11:29:21 -0800250 // The default consumer usage flags that BufferLayerConsumer always sets on its
251 // BufferQueue instance; these will be OR:d with any additional flags passed
252 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
253 // consume buffers as hardware textures.
254 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
255
Alec Mourib5c4f352019-02-19 19:46:38 -0800256 // mCurrentTextureBuffer is the buffer containing the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800257 // possible that this buffer is not associated with any buffer slot, so we
258 // must track it separately in order to support the getCurrentBuffer method.
Alec Mouria90a5702021-04-16 16:36:21 +0000259 std::shared_ptr<renderengine::ExternalTexture> mCurrentTextureBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000260
Chia-I Wuf1405182017-11-27 11:29:21 -0800261 // mCurrentCrop is the crop rectangle that applies to the current texture.
262 // It gets set each time updateTexImage is called.
263 Rect mCurrentCrop;
264
265 // mCurrentTransform is the transform identifier for the current texture. It
266 // gets set each time updateTexImage is called.
267 uint32_t mCurrentTransform;
268
269 // mCurrentScalingMode is the scaling mode for the current texture. It gets
270 // set each time updateTexImage is called.
271 uint32_t mCurrentScalingMode;
272
273 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
274 sp<Fence> mCurrentFence;
275
276 // The FenceTime wrapper around mCurrentFence.
277 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
278
279 // mCurrentTransformMatrix is the transform matrix for the current texture.
280 // It gets computed by computeTransformMatrix each time updateTexImage is
281 // called.
282 float mCurrentTransformMatrix[16];
283
284 // mCurrentTimestamp is the timestamp for the current texture. It
285 // gets set each time updateTexImage is called.
286 int64_t mCurrentTimestamp;
287
288 // mCurrentDataSpace is the dataspace for the current texture. It
289 // gets set each time updateTexImage is called.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700290 ui::Dataspace mCurrentDataSpace;
Chia-I Wuf1405182017-11-27 11:29:21 -0800291
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700292 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
293 // gets set each time updateTexImage is called.
294 HdrMetadata mCurrentHdrMetadata;
295
Chia-I Wuf1405182017-11-27 11:29:21 -0800296 // mCurrentFrameNumber is the frame counter for the current texture.
297 // It gets set each time updateTexImage is called.
298 uint64_t mCurrentFrameNumber;
299
Chia-I Wu67dcc692017-11-27 14:51:06 -0800300 // Indicates this buffer must be transformed by the inverse transform of the screen
301 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
302 // This must be set/read from SurfaceFlinger's main thread.
303 bool mCurrentTransformToDisplayInverse;
304
305 // The portion of this surface that has changed since the previous frame
306 Region mCurrentSurfaceDamage;
307
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800308 int mCurrentApi;
309
Chia-I Wuf1405182017-11-27 11:29:21 -0800310 uint32_t mDefaultWidth, mDefaultHeight;
311
312 // mFilteringEnabled indicates whether the transform matrix is computed for
313 // use with bilinear filtering. It defaults to true and is changed by
314 // setFilteringEnabled().
315 bool mFilteringEnabled;
316
Peiyong Lin833074a2018-08-28 11:53:54 -0700317 renderengine::RenderEngine& mRE;
Chia-I Wu9f2db772017-11-30 21:06:50 -0800318
Chia-I Wu221b5922017-12-14 13:59:16 -0800319 // mTexName is the name of the RenderEngine texture to which streamed
320 // images will be bound when bindTexImage is called. It is set at
321 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800322 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800323
chaviw1a4dba42020-05-27 15:16:15 -0700324 // The layer for this BufferLayerConsumer. Always check mAbandoned before accessing.
325 Layer* mLayer GUARDED_BY(mMutex);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800326
Chia-I Wufd257f82017-11-27 14:51:06 -0800327 wp<ContentsChangedListener> mContentsChangedListener;
328
Chia-I Wuf1405182017-11-27 11:29:21 -0800329 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800330 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800331 // indicating that no buffer slot is currently bound to the texture. Note,
332 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
333 // that no buffer is bound to the texture. A call to setBufferCount will
334 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
335 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800336
Alec Mourib5c4f352019-02-19 19:46:38 -0800337 // Shadow buffer cache for cleaning up renderengine references.
Alec Mouria90a5702021-04-16 16:36:21 +0000338 std::shared_ptr<renderengine::ExternalTexture>
339 mImages[BufferQueueDefs::NUM_BUFFER_SLOTS] GUARDED_BY(mImagesMutex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700340
341 // Separate mutex guarding the shadow buffer cache.
342 // mImagesMutex can be manipulated with binder threads (e.g. onBuffersAllocated)
343 // which is contentious enough that we can't just use mMutex.
344 mutable std::mutex mImagesMutex;
Alec Mouri39801c02018-10-10 10:44:47 -0700345
Chia-I Wuda5c7302017-11-27 14:51:06 -0800346 // A release that is pending on the receipt of a new release fence from
347 // presentDisplay
348 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800349};
350
351// ----------------------------------------------------------------------------
352}; // namespace android
353
354#endif // ANDROID_BUFFERLAYERCONSUMER_H