blob: e2a6d2e922900f35614d7363d856fbf341e80404 [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
Chia-I Wuf1405182017-11-27 11:29:21 -080020#include <gui/BufferQueueDefs.h>
21#include <gui/ConsumerBase.h>
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070022#include <gui/HdrMetadata.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080023
24#include <ui/FenceTime.h>
25#include <ui/GraphicBuffer.h>
Peiyong Lin34beb7a2018-03-28 11:57:12 -070026#include <ui/GraphicTypes.h>
Chia-I Wu67dcc692017-11-27 14:51:06 -080027#include <ui/Region.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080028
29#include <utils/String8.h>
30#include <utils/Vector.h>
31#include <utils/threads.h>
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Chia-I Wuda5c7302017-11-27 14:51:06 -080036class DispSync;
Chia-I Wuc75c44d2017-11-27 14:32:57 -080037class Layer;
Chia-I Wuf1405182017-11-27 11:29:21 -080038class String8;
39
Peiyong Lin833074a2018-08-28 11:53:54 -070040namespace renderengine {
Lloyd Pique144e1162017-12-20 16:44:52 -080041class RenderEngine;
42class Image;
Peiyong Lin833074a2018-08-28 11:53:54 -070043} // namespace renderengine
Lloyd Pique144e1162017-12-20 16:44:52 -080044
Chia-I Wuf1405182017-11-27 11:29:21 -080045/*
46 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080047 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080048 *
Chia-I Wu221b5922017-12-14 13:59:16 -080049 * A typical usage pattern is to call updateTexImage() when a new frame is
50 * desired. If a new frame is available, the frame is latched. If not, the
51 * previous contents are retained. The texture is attached and updated after
52 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080053 *
Chia-I Wu221b5922017-12-14 13:59:16 -080054 * All calls to updateTexImage must be made with RenderEngine being current.
55 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080056 */
57class BufferLayerConsumer : public ConsumerBase {
58public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080059 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
60
61 class BufferRejecter {
62 friend class BufferLayerConsumer;
63 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
64
65 protected:
66 virtual ~BufferRejecter() {}
67 };
68
Chia-I Wufd257f82017-11-27 14:51:06 -080069 struct ContentsChangedListener : public FrameAvailableListener {
70 virtual void onSidebandStreamChanged() = 0;
71 };
Chia-I Wuf1405182017-11-27 11:29:21 -080072
Chia-I Wu221b5922017-12-14 13:59:16 -080073 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
74 // tex parameter indicates the name of the RenderEngine texture to which
75 // images are to be streamed.
Peiyong Lin833074a2018-08-28 11:53:54 -070076 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, renderengine::RenderEngine& engine,
Lloyd Pique144e1162017-12-20 16:44:52 -080077 uint32_t tex, Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080078
Chia-I Wufd257f82017-11-27 14:51:06 -080079 // Sets the contents changed listener. This should be used instead of
80 // ConsumerBase::setFrameAvailableListener().
81 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
82
Chia-I Wuf1405182017-11-27 11:29:21 -080083 // updateTexImage acquires the most recently queued buffer, and sets the
84 // image contents of the target texture to it.
85 //
Chia-I Wu221b5922017-12-14 13:59:16 -080086 // This call may only be made while RenderEngine is current.
Chia-I Wuf1405182017-11-27 11:29:21 -080087 //
Chia-I Wu221b5922017-12-14 13:59:16 -080088 // This calls doFenceWait to ensure proper synchronization unless native
89 // fence is supported.
Chia-I Wuda5c7302017-11-27 14:51:06 -080090 //
Chia-I Wu221b5922017-12-14 13:59:16 -080091 // Unlike the GLConsumer version, this version takes a functor that may be
92 // used to reject the newly acquired buffer. It also does not bind the
93 // RenderEngine texture until bindTextureImage is called.
Ana Krulec010d2192018-10-08 06:29:54 -070094 status_t updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Alec Mouri56e538f2019-01-14 15:22:01 -080095 bool* autoRefresh, bool* queuedBuffer, uint64_t maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -080096
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080097 // See BufferLayerConsumer::bindTextureImageLocked().
98 status_t bindTextureImage();
99
Chia-I Wuf1405182017-11-27 11:29:21 -0800100 // setReleaseFence stores a fence that will signal when the current buffer
101 // is no longer being read. This fence will be returned to the producer
102 // when the current buffer is released by updateTexImage(). Multiple
103 // fences can be set for a given buffer; they will be merged into a single
104 // union fence.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800105 void setReleaseFence(const sp<Fence>& fence);
106
107 bool releasePendingBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800108
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800109 sp<Fence> getPrevFinalReleaseFence() const;
110
Chia-I Wu221b5922017-12-14 13:59:16 -0800111 // See GLConsumer::getTransformMatrix.
Chia-I Wuf1405182017-11-27 11:29:21 -0800112 void getTransformMatrix(float mtx[16]);
113
Chia-I Wuf1405182017-11-27 11:29:21 -0800114 // getTimestamp retrieves the timestamp associated with the texture image
115 // set by the most recent call to updateTexImage.
116 //
117 // The timestamp is in nanoseconds, and is monotonically increasing. Its
118 // other semantics (zero point, etc) are source-dependent and should be
119 // documented by the source.
120 int64_t getTimestamp();
121
122 // getDataSpace retrieves the DataSpace associated with the texture image
123 // set by the most recent call to updateTexImage.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700124 ui::Dataspace getCurrentDataSpace();
Chia-I Wuf1405182017-11-27 11:29:21 -0800125
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700126 // getCurrentHdrMetadata retrieves the HDR metadata associated with the
127 // texture image set by the most recent call to updateTexImage.
128 const HdrMetadata& getCurrentHdrMetadata() const;
129
Chia-I Wuf1405182017-11-27 11:29:21 -0800130 // getFrameNumber retrieves the frame number associated with the texture
131 // image set by the most recent call to updateTexImage.
132 //
133 // The frame number is an incrementing counter set to 0 at the creation of
134 // the BufferQueue associated with this consumer.
135 uint64_t getFrameNumber();
136
Chia-I Wu67dcc692017-11-27 14:51:06 -0800137 bool getTransformToDisplayInverse() const;
138
139 // must be called from SF main thread
140 const Region& getSurfaceDamage() const;
141
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800142 // getCurrentApi retrieves the API which queues the current buffer.
143 int getCurrentApi() const;
144
Chia-I Wu221b5922017-12-14 13:59:16 -0800145 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800146 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
147
148 // setFilteringEnabled sets whether the transform matrix should be computed
149 // for use with bilinear filtering.
150 void setFilteringEnabled(bool enabled);
151
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000152 // Sets mCurrentTextureBufferStaleForGpu to true to indicate that the
153 // buffer is now "stale" for GPU composition, and returns the old staleness
154 // bit as a caching hint.
155 bool getAndSetCurrentBufferCacheHint();
156
Chia-I Wuf1405182017-11-27 11:29:21 -0800157 // getCurrentBuffer returns the buffer associated with the current image.
158 // When outSlot is not nullptr, the current buffer slot index is also
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159 // returned. Simiarly, when outFence is not nullptr, the current output
160 // fence is returned.
161 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr, sp<Fence>* outFence = nullptr) const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800162
Chia-I Wuf1405182017-11-27 11:29:21 -0800163 // getCurrentCrop returns the cropping rectangle of the current buffer.
164 Rect getCurrentCrop() const;
165
166 // getCurrentTransform returns the transform of the current buffer.
167 uint32_t getCurrentTransform() const;
168
169 // getCurrentScalingMode returns the scaling mode of the current buffer.
170 uint32_t getCurrentScalingMode() const;
171
172 // getCurrentFence returns the fence indicating when the current buffer is
173 // ready to be read from.
174 sp<Fence> getCurrentFence() const;
175
176 // getCurrentFence returns the FenceTime indicating when the current
177 // buffer is ready to be read from.
178 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
179
180 // setConsumerUsageBits overrides the ConsumerBase method to OR
181 // DEFAULT_USAGE_FLAGS to usage.
182 status_t setConsumerUsageBits(uint64_t usage);
183
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.
187 virtual void abandonLocked();
188
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,
195 uint64_t maxFrameNumber = 0) override;
196
Chia-I Wu6748db42017-12-01 10:53:53 -0800197 bool canUseImageCrop(const Rect& crop) const;
198
Chia-I Wuf1405182017-11-27 11:29:21 -0800199 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800200 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800201
202 bool isPending;
203 int currentTexture;
204 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800205 };
206
207 // This releases the buffer in the slot referenced by mCurrentTexture,
208 // then updates state to refer to the BufferItem, which must be a
209 // newly-acquired buffer. If pendingRelease is not null, the parameters
210 // which would have been passed to releaseBufferLocked upon the successful
211 // completion of the method will instead be returned to the caller, so that
212 // it may call releaseBufferLocked itself later.
213 status_t updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800214 PendingRelease* pendingRelease = nullptr);
Chia-I Wuf1405182017-11-27 11:29:21 -0800215
Alec Mouri39801c02018-10-10 10:44:47 -0700216 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target.
217 // If the bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800218 status_t bindTextureImageLocked();
219
Chia-I Wuf1405182017-11-27 11:29:21 -0800220private:
Chia-I Wuf1405182017-11-27 11:29:21 -0800221 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800222 // initialized this will release the reference to the GraphicBuffer in
Alec Mouri39801c02018-10-10 10:44:47 -0700223 // that slot. Otherwise it has no effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800224 //
225 // This method must be called with mMutex locked.
226 virtual void freeBufferLocked(int slotIndex);
227
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800228 // IConsumerListener interface
229 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800230 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800231 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
232 FrameEventHistoryDelta* outDelta) override;
233
Chia-I Wuf1405182017-11-27 11:29:21 -0800234 // computeCurrentTransformMatrixLocked computes the transform matrix for the
235 // current texture. It uses mCurrentTransform and the current GraphicBuffer
236 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800237 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800238 void computeCurrentTransformMatrixLocked();
239
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800240 // doFenceWaitLocked inserts a wait command into the RenderEngine command
241 // stream to ensure that it is safe for future RenderEngine commands to
Chia-I Wuf1405182017-11-27 11:29:21 -0800242 // access the current texture buffer.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800243 status_t doFenceWaitLocked() const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800244
Chia-I Wuf1405182017-11-27 11:29:21 -0800245 // The default consumer usage flags that BufferLayerConsumer always sets on its
246 // BufferQueue instance; these will be OR:d with any additional flags passed
247 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
248 // consume buffers as hardware textures.
249 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
250
Alec Mouri39801c02018-10-10 10:44:47 -0700251 // mCurrentTextureImage is the buffer containing the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800252 // possible that this buffer is not associated with any buffer slot, so we
253 // must track it separately in order to support the getCurrentBuffer method.
Alec Mouri39801c02018-10-10 10:44:47 -0700254 sp<GraphicBuffer> mCurrentTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800255
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000256 // True if the buffer was used for the previous client composition frame,
257 // and false otherwise.
258 bool mCurrentTextureBufferStaleForGpu;
259
Chia-I Wuf1405182017-11-27 11:29:21 -0800260 // mCurrentCrop is the crop rectangle that applies to the current texture.
261 // It gets set each time updateTexImage is called.
262 Rect mCurrentCrop;
263
264 // mCurrentTransform is the transform identifier for the current texture. It
265 // gets set each time updateTexImage is called.
266 uint32_t mCurrentTransform;
267
268 // mCurrentScalingMode is the scaling mode for the current texture. It gets
269 // set each time updateTexImage is called.
270 uint32_t mCurrentScalingMode;
271
272 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
273 sp<Fence> mCurrentFence;
274
275 // The FenceTime wrapper around mCurrentFence.
276 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
277
278 // mCurrentTransformMatrix is the transform matrix for the current texture.
279 // It gets computed by computeTransformMatrix each time updateTexImage is
280 // called.
281 float mCurrentTransformMatrix[16];
282
283 // mCurrentTimestamp is the timestamp for the current texture. It
284 // gets set each time updateTexImage is called.
285 int64_t mCurrentTimestamp;
286
287 // mCurrentDataSpace is the dataspace for the current texture. It
288 // gets set each time updateTexImage is called.
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700289 ui::Dataspace mCurrentDataSpace;
Chia-I Wuf1405182017-11-27 11:29:21 -0800290
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700291 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
292 // gets set each time updateTexImage is called.
293 HdrMetadata mCurrentHdrMetadata;
294
Chia-I Wuf1405182017-11-27 11:29:21 -0800295 // mCurrentFrameNumber is the frame counter for the current texture.
296 // It gets set each time updateTexImage is called.
297 uint64_t mCurrentFrameNumber;
298
Chia-I Wu67dcc692017-11-27 14:51:06 -0800299 // Indicates this buffer must be transformed by the inverse transform of the screen
300 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
301 // This must be set/read from SurfaceFlinger's main thread.
302 bool mCurrentTransformToDisplayInverse;
303
304 // The portion of this surface that has changed since the previous frame
305 Region mCurrentSurfaceDamage;
306
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800307 int mCurrentApi;
308
Chia-I Wuf1405182017-11-27 11:29:21 -0800309 uint32_t mDefaultWidth, mDefaultHeight;
310
311 // mFilteringEnabled indicates whether the transform matrix is computed for
312 // use with bilinear filtering. It defaults to true and is changed by
313 // setFilteringEnabled().
314 bool mFilteringEnabled;
315
Peiyong Lin833074a2018-08-28 11:53:54 -0700316 renderengine::RenderEngine& mRE;
Chia-I Wu9f2db772017-11-30 21:06:50 -0800317
Chia-I Wu221b5922017-12-14 13:59:16 -0800318 // mTexName is the name of the RenderEngine texture to which streamed
319 // images will be bound when bindTexImage is called. It is set at
320 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800321 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800322
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800323 // The layer for this BufferLayerConsumer
324 const wp<Layer> mLayer;
325
Chia-I Wufd257f82017-11-27 14:51:06 -0800326 wp<ContentsChangedListener> mContentsChangedListener;
327
Chia-I Wuf1405182017-11-27 11:29:21 -0800328 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800329 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800330 // indicating that no buffer slot is currently bound to the texture. Note,
331 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
332 // that no buffer is bound to the texture. A call to setBufferCount will
333 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
334 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800335
Alec Mouri39801c02018-10-10 10:44:47 -0700336 // Cached image used for rendering the current texture through GPU
337 // composition, which contains the cached image after freeBufferLocked is
338 // called on the current buffer. Whenever latchBuffer is called, this is
339 // expected to be cleared. Then, if bindTexImage is called before the next
340 // buffer is acquired, then this image is bound.
341 std::unique_ptr<renderengine::Image> mCurrentTextureImageFreed;
342
343 // Cached images used for rendering the current texture through GPU
344 // composition.
345 std::unique_ptr<renderengine::Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS];
346
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