blob: 5e3044fd9889214cf2d4dc9c22bf3ad96f62090a [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>
Chia-I Wuf1405182017-11-27 11:29:21 -080024
25#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
30#include <utils/String8.h>
31#include <utils/Vector.h>
32#include <utils/threads.h>
33
34namespace android {
35// ----------------------------------------------------------------------------
36
Chia-I Wuda5c7302017-11-27 14:51:06 -080037class DispSync;
Chia-I Wuc75c44d2017-11-27 14:32:57 -080038class Layer;
Chia-I Wuf1405182017-11-27 11:29:21 -080039class String8;
40
Peiyong Lin833074a2018-08-28 11:53:54 -070041namespace renderengine {
Lloyd Pique144e1162017-12-20 16:44:52 -080042class RenderEngine;
43class Image;
Peiyong Lin833074a2018-08-28 11:53:54 -070044} // namespace renderengine
Lloyd Pique144e1162017-12-20 16:44:52 -080045
Chia-I Wuf1405182017-11-27 11:29:21 -080046/*
47 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080048 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080049 *
Chia-I Wu221b5922017-12-14 13:59:16 -080050 * A typical usage pattern is to call updateTexImage() when a new frame is
51 * desired. If a new frame is available, the frame is latched. If not, the
52 * previous contents are retained. The texture is attached and updated after
53 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080054 *
Chia-I Wu221b5922017-12-14 13:59:16 -080055 * All calls to updateTexImage must be made with RenderEngine being current.
56 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080057 */
58class BufferLayerConsumer : public ConsumerBase {
59public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080060 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
61
62 class BufferRejecter {
63 friend class BufferLayerConsumer;
64 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
65
66 protected:
67 virtual ~BufferRejecter() {}
68 };
69
Chia-I Wufd257f82017-11-27 14:51:06 -080070 struct ContentsChangedListener : public FrameAvailableListener {
71 virtual void onSidebandStreamChanged() = 0;
72 };
Chia-I Wuf1405182017-11-27 11:29:21 -080073
Chia-I Wu221b5922017-12-14 13:59:16 -080074 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
75 // tex parameter indicates the name of the RenderEngine texture to which
76 // images are to be streamed.
Peiyong Lin833074a2018-08-28 11:53:54 -070077 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, renderengine::RenderEngine& engine,
Lloyd Pique144e1162017-12-20 16:44:52 -080078 uint32_t tex, Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080079
Chia-I Wufd257f82017-11-27 14:51:06 -080080 // Sets the contents changed listener. This should be used instead of
81 // ConsumerBase::setFrameAvailableListener().
82 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
83
Chia-I Wuf1405182017-11-27 11:29:21 -080084 // updateTexImage acquires the most recently queued buffer, and sets the
85 // image contents of the target texture to it.
86 //
Chia-I Wu221b5922017-12-14 13:59:16 -080087 // This call may only be made while RenderEngine is current.
Chia-I Wuf1405182017-11-27 11:29:21 -080088 //
Chia-I Wu221b5922017-12-14 13:59:16 -080089 // This calls doFenceWait to ensure proper synchronization unless native
90 // fence is supported.
Chia-I Wuda5c7302017-11-27 14:51:06 -080091 //
Chia-I Wu221b5922017-12-14 13:59:16 -080092 // Unlike the GLConsumer version, this version takes a functor that may be
93 // used to reject the newly acquired buffer. It also does not bind the
94 // RenderEngine texture until bindTextureImage is called.
Ana Krulec010d2192018-10-08 06:29:54 -070095 status_t updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Alec Mouri56e538f2019-01-14 15:22:01 -080096 bool* autoRefresh, bool* queuedBuffer, uint64_t maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -080097
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080098 // See BufferLayerConsumer::bindTextureImageLocked().
99 status_t bindTextureImage();
100
Chia-I Wuf1405182017-11-27 11:29:21 -0800101 // setReleaseFence stores a fence that will signal when the current buffer
102 // is no longer being read. This fence will be returned to the producer
103 // when the current buffer is released by updateTexImage(). Multiple
104 // fences can be set for a given buffer; they will be merged into a single
105 // union fence.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800106 void setReleaseFence(const sp<Fence>& fence);
107
108 bool releasePendingBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800109
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800110 sp<Fence> getPrevFinalReleaseFence() const;
111
Chia-I Wu221b5922017-12-14 13:59:16 -0800112 // See GLConsumer::getTransformMatrix.
Chia-I Wuf1405182017-11-27 11:29:21 -0800113 void getTransformMatrix(float mtx[16]);
114
Chia-I Wuf1405182017-11-27 11:29:21 -0800115 // getTimestamp retrieves the timestamp associated with the texture image
116 // set by the most recent call to updateTexImage.
117 //
118 // The timestamp is in nanoseconds, and is monotonically increasing. Its
119 // other semantics (zero point, etc) are source-dependent and should be
120 // documented by the source.
121 int64_t getTimestamp();
122
123 // getDataSpace retrieves the DataSpace associated with the texture image
124 // set by the most recent call to updateTexImage.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700125 ui::Dataspace getCurrentDataSpace();
Chia-I Wuf1405182017-11-27 11:29:21 -0800126
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700127 // getCurrentHdrMetadata retrieves the HDR metadata associated with the
128 // texture image set by the most recent call to updateTexImage.
129 const HdrMetadata& getCurrentHdrMetadata() const;
130
Chia-I Wuf1405182017-11-27 11:29:21 -0800131 // getFrameNumber retrieves the frame number associated with the texture
132 // image set by the most recent call to updateTexImage.
133 //
134 // The frame number is an incrementing counter set to 0 at the creation of
135 // the BufferQueue associated with this consumer.
136 uint64_t getFrameNumber();
137
Chia-I Wu67dcc692017-11-27 14:51:06 -0800138 bool getTransformToDisplayInverse() const;
139
140 // must be called from SF main thread
141 const Region& getSurfaceDamage() const;
142
Steven Thomas44685cb2019-07-23 16:19:31 -0700143 // Merge the given damage region into the current damage region value.
144 void mergeSurfaceDamage(const Region& damage);
145
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800146 // getCurrentApi retrieves the API which queues the current buffer.
147 int getCurrentApi() const;
148
Chia-I Wu221b5922017-12-14 13:59:16 -0800149 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800150 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
151
152 // setFilteringEnabled sets whether the transform matrix should be computed
153 // for use with bilinear filtering.
154 void setFilteringEnabled(bool enabled);
155
156 // getCurrentBuffer returns the buffer associated with the current image.
157 // When outSlot is not nullptr, the current buffer slot index is also
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000158 // returned. Simiarly, when outFence is not nullptr, the current output
159 // fence is returned.
160 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr, sp<Fence>* outFence = nullptr) const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800161
Chia-I Wuf1405182017-11-27 11:29:21 -0800162 // getCurrentCrop returns the cropping rectangle of the current buffer.
163 Rect getCurrentCrop() const;
164
165 // getCurrentTransform returns the transform of the current buffer.
166 uint32_t getCurrentTransform() const;
167
168 // getCurrentScalingMode returns the scaling mode of the current buffer.
169 uint32_t getCurrentScalingMode() const;
170
171 // getCurrentFence returns the fence indicating when the current buffer is
172 // ready to be read from.
173 sp<Fence> getCurrentFence() const;
174
175 // getCurrentFence returns the FenceTime indicating when the current
176 // buffer is ready to be read from.
177 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
178
179 // setConsumerUsageBits overrides the ConsumerBase method to OR
180 // DEFAULT_USAGE_FLAGS to usage.
181 status_t setConsumerUsageBits(uint64_t usage);
Alec Mouri485e4c32019-04-30 18:24:05 -0700182 void onBufferAvailable(const BufferItem& item) EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800183
Chia-I Wuf1405182017-11-27 11:29:21 -0800184protected:
185 // abandonLocked overrides the ConsumerBase method to clear
186 // mCurrentTextureImage in addition to the ConsumerBase behavior.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700187 virtual void abandonLocked() EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800188
189 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
190 // specific info in addition to the ConsumerBase behavior.
191 virtual void dumpLocked(String8& result, const char* prefix) const;
192
Alec Mouri39801c02018-10-10 10:44:47 -0700193 // See ConsumerBase::acquireBufferLocked
Chia-I Wuf1405182017-11-27 11:29:21 -0800194 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700195 uint64_t maxFrameNumber = 0) override
196 EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800197
Chia-I Wu6748db42017-12-01 10:53:53 -0800198 bool canUseImageCrop(const Rect& crop) const;
199
Chia-I Wuf1405182017-11-27 11:29:21 -0800200 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800201 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800202
203 bool isPending;
204 int currentTexture;
205 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800206 };
207
208 // This releases the buffer in the slot referenced by mCurrentTexture,
209 // then updates state to refer to the BufferItem, which must be a
210 // newly-acquired buffer. If pendingRelease is not null, the parameters
211 // which would have been passed to releaseBufferLocked upon the successful
212 // completion of the method will instead be returned to the caller, so that
213 // it may call releaseBufferLocked itself later.
214 status_t updateAndReleaseLocked(const BufferItem& item,
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700215 PendingRelease* pendingRelease = nullptr)
216 EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800217
Alec Mouri39801c02018-10-10 10:44:47 -0700218 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target.
219 // If the bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800220 status_t bindTextureImageLocked();
221
Chia-I Wuf1405182017-11-27 11:29:21 -0800222private:
Alec Mourib5c4f352019-02-19 19:46:38 -0800223 // Utility class for managing GraphicBuffer references into renderengine
224 class Image {
225 public:
Alec Mouri16a99402019-07-29 16:37:30 -0700226 Image(const sp<GraphicBuffer>& graphicBuffer, renderengine::RenderEngine& engine);
Alec Mourib5c4f352019-02-19 19:46:38 -0800227 virtual ~Image();
228 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
229
230 private:
231 // mGraphicBuffer is the buffer that was used to create this image.
232 sp<GraphicBuffer> mGraphicBuffer;
233 // Back-reference into renderengine to initiate cleanup.
234 renderengine::RenderEngine& mRE;
235 DISALLOW_COPY_AND_ASSIGN(Image);
236 };
237
Chia-I Wuf1405182017-11-27 11:29:21 -0800238 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800239 // initialized this will release the reference to the GraphicBuffer in
Alec Mouri39801c02018-10-10 10:44:47 -0700240 // that slot. Otherwise it has no effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800241 //
242 // This method must be called with mMutex locked.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700243 virtual void freeBufferLocked(int slotIndex) EXCLUDES(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800244
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800245 // IConsumerListener interface
246 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800247 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800248 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
249 FrameEventHistoryDelta* outDelta) override;
250
Chia-I Wuf1405182017-11-27 11:29:21 -0800251 // computeCurrentTransformMatrixLocked computes the transform matrix for the
252 // current texture. It uses mCurrentTransform and the current GraphicBuffer
253 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800254 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800255 void computeCurrentTransformMatrixLocked();
256
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800257 // getCurrentCropLocked returns the cropping rectangle of the current buffer.
258 Rect getCurrentCropLocked() const;
259
Chia-I Wuf1405182017-11-27 11:29:21 -0800260 // The default consumer usage flags that BufferLayerConsumer always sets on its
261 // BufferQueue instance; these will be OR:d with any additional flags passed
262 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
263 // consume buffers as hardware textures.
264 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
265
Alec Mourib5c4f352019-02-19 19:46:38 -0800266 // mCurrentTextureBuffer is the buffer containing the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800267 // possible that this buffer is not associated with any buffer slot, so we
268 // must track it separately in order to support the getCurrentBuffer method.
Alec Mourib5c4f352019-02-19 19:46:38 -0800269 std::shared_ptr<Image> mCurrentTextureBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000270
Chia-I Wuf1405182017-11-27 11:29:21 -0800271 // mCurrentCrop is the crop rectangle that applies to the current texture.
272 // It gets set each time updateTexImage is called.
273 Rect mCurrentCrop;
274
275 // mCurrentTransform is the transform identifier for the current texture. It
276 // gets set each time updateTexImage is called.
277 uint32_t mCurrentTransform;
278
279 // mCurrentScalingMode is the scaling mode for the current texture. It gets
280 // set each time updateTexImage is called.
281 uint32_t mCurrentScalingMode;
282
283 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
284 sp<Fence> mCurrentFence;
285
286 // The FenceTime wrapper around mCurrentFence.
287 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
288
289 // mCurrentTransformMatrix is the transform matrix for the current texture.
290 // It gets computed by computeTransformMatrix each time updateTexImage is
291 // called.
292 float mCurrentTransformMatrix[16];
293
294 // mCurrentTimestamp is the timestamp for the current texture. It
295 // gets set each time updateTexImage is called.
296 int64_t mCurrentTimestamp;
297
298 // mCurrentDataSpace is the dataspace for the current texture. It
299 // gets set each time updateTexImage is called.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700300 ui::Dataspace mCurrentDataSpace;
Chia-I Wuf1405182017-11-27 11:29:21 -0800301
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700302 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
303 // gets set each time updateTexImage is called.
304 HdrMetadata mCurrentHdrMetadata;
305
Chia-I Wuf1405182017-11-27 11:29:21 -0800306 // mCurrentFrameNumber is the frame counter for the current texture.
307 // It gets set each time updateTexImage is called.
308 uint64_t mCurrentFrameNumber;
309
Chia-I Wu67dcc692017-11-27 14:51:06 -0800310 // Indicates this buffer must be transformed by the inverse transform of the screen
311 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
312 // This must be set/read from SurfaceFlinger's main thread.
313 bool mCurrentTransformToDisplayInverse;
314
315 // The portion of this surface that has changed since the previous frame
316 Region mCurrentSurfaceDamage;
317
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800318 int mCurrentApi;
319
Chia-I Wuf1405182017-11-27 11:29:21 -0800320 uint32_t mDefaultWidth, mDefaultHeight;
321
322 // mFilteringEnabled indicates whether the transform matrix is computed for
323 // use with bilinear filtering. It defaults to true and is changed by
324 // setFilteringEnabled().
325 bool mFilteringEnabled;
326
Peiyong Lin833074a2018-08-28 11:53:54 -0700327 renderengine::RenderEngine& mRE;
Chia-I Wu9f2db772017-11-30 21:06:50 -0800328
Chia-I Wu221b5922017-12-14 13:59:16 -0800329 // mTexName is the name of the RenderEngine texture to which streamed
330 // images will be bound when bindTexImage is called. It is set at
331 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800332 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800333
chaviw1a4dba42020-05-27 15:16:15 -0700334 // The layer for this BufferLayerConsumer. Always check mAbandoned before accessing.
335 Layer* mLayer GUARDED_BY(mMutex);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800336
Chia-I Wufd257f82017-11-27 14:51:06 -0800337 wp<ContentsChangedListener> mContentsChangedListener;
338
Chia-I Wuf1405182017-11-27 11:29:21 -0800339 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800340 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800341 // indicating that no buffer slot is currently bound to the texture. Note,
342 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
343 // that no buffer is bound to the texture. A call to setBufferCount will
344 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
345 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800346
Alec Mourib5c4f352019-02-19 19:46:38 -0800347 // Shadow buffer cache for cleaning up renderengine references.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700348 std::shared_ptr<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS] GUARDED_BY(mImagesMutex);
349
350 // Separate mutex guarding the shadow buffer cache.
351 // mImagesMutex can be manipulated with binder threads (e.g. onBuffersAllocated)
352 // which is contentious enough that we can't just use mMutex.
353 mutable std::mutex mImagesMutex;
Alec Mouri39801c02018-10-10 10:44:47 -0700354
Chia-I Wuda5c7302017-11-27 14:51:06 -0800355 // A release that is pending on the receipt of a new release fence from
356 // presentDisplay
357 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800358};
359
360// ----------------------------------------------------------------------------
361}; // namespace android
362
363#endif // ANDROID_BUFFERLAYERCONSUMER_H