blob: a0272b3622432231cb5685fa1f95419d3b78a726 [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 Wu6748db42017-12-01 10:53:53 -080020#include "RenderEngine/Image.h"
Chia-I Wuf1405182017-11-27 11:29:21 -080021
22#include <gui/BufferQueueDefs.h>
23#include <gui/ConsumerBase.h>
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -070024#include <gui/HdrMetadata.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080025
26#include <ui/FenceTime.h>
27#include <ui/GraphicBuffer.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 Wu9f2db772017-11-30 21:06:50 -080039class RenderEngine;
Chia-I Wuf1405182017-11-27 11:29:21 -080040class String8;
41
42/*
43 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080044 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080045 *
Chia-I Wu221b5922017-12-14 13:59:16 -080046 * A typical usage pattern is to call updateTexImage() when a new frame is
47 * desired. If a new frame is available, the frame is latched. If not, the
48 * previous contents are retained. The texture is attached and updated after
49 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080050 *
Chia-I Wu221b5922017-12-14 13:59:16 -080051 * All calls to updateTexImage must be made with RenderEngine being current.
52 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080053 */
54class BufferLayerConsumer : public ConsumerBase {
55public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080056 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
57
58 class BufferRejecter {
59 friend class BufferLayerConsumer;
60 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
61
62 protected:
63 virtual ~BufferRejecter() {}
64 };
65
Chia-I Wufd257f82017-11-27 14:51:06 -080066 struct ContentsChangedListener : public FrameAvailableListener {
67 virtual void onSidebandStreamChanged() = 0;
68 };
Chia-I Wuf1405182017-11-27 11:29:21 -080069
Chia-I Wu221b5922017-12-14 13:59:16 -080070 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
71 // tex parameter indicates the name of the RenderEngine texture to which
72 // images are to be streamed.
Chia-I Wu9f2db772017-11-30 21:06:50 -080073 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, RenderEngine& engine, uint32_t tex,
74 Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080075
Chia-I Wufd257f82017-11-27 14:51:06 -080076 // Sets the contents changed listener. This should be used instead of
77 // ConsumerBase::setFrameAvailableListener().
78 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
79
Chia-I Wuda5c7302017-11-27 14:51:06 -080080 nsecs_t computeExpectedPresent(const DispSync& dispSync);
81
Chia-I Wuf1405182017-11-27 11:29:21 -080082 // updateTexImage acquires the most recently queued buffer, and sets the
83 // image contents of the target texture to it.
84 //
Chia-I Wu221b5922017-12-14 13:59:16 -080085 // This call may only be made while RenderEngine is current.
Chia-I Wuf1405182017-11-27 11:29:21 -080086 //
Chia-I Wu221b5922017-12-14 13:59:16 -080087 // This calls doFenceWait to ensure proper synchronization unless native
88 // fence is supported.
Chia-I Wuda5c7302017-11-27 14:51:06 -080089 //
Chia-I Wu221b5922017-12-14 13:59:16 -080090 // Unlike the GLConsumer version, this version takes a functor that may be
91 // used to reject the newly acquired buffer. It also does not bind the
92 // RenderEngine texture until bindTextureImage is called.
Chia-I Wuda5c7302017-11-27 14:51:06 -080093 status_t updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync, bool* autoRefresh,
94 bool* queuedBuffer, uint64_t maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -080095
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080096 // See BufferLayerConsumer::bindTextureImageLocked().
97 status_t bindTextureImage();
98
Chia-I Wuf1405182017-11-27 11:29:21 -080099 // setReleaseFence stores a fence that will signal when the current buffer
100 // is no longer being read. This fence will be returned to the producer
101 // when the current buffer is released by updateTexImage(). Multiple
102 // fences can be set for a given buffer; they will be merged into a single
103 // union fence.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800104 void setReleaseFence(const sp<Fence>& fence);
105
106 bool releasePendingBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800107
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800108 sp<Fence> getPrevFinalReleaseFence() const;
109
Chia-I Wu221b5922017-12-14 13:59:16 -0800110 // See GLConsumer::getTransformMatrix.
Chia-I Wuf1405182017-11-27 11:29:21 -0800111 void getTransformMatrix(float mtx[16]);
112
Chia-I Wuf1405182017-11-27 11:29:21 -0800113 // getTimestamp retrieves the timestamp associated with the texture image
114 // set by the most recent call to updateTexImage.
115 //
116 // The timestamp is in nanoseconds, and is monotonically increasing. Its
117 // other semantics (zero point, etc) are source-dependent and should be
118 // documented by the source.
119 int64_t getTimestamp();
120
121 // getDataSpace retrieves the DataSpace associated with the texture image
122 // set by the most recent call to updateTexImage.
123 android_dataspace getCurrentDataSpace();
124
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700125 // getCurrentHdrMetadata retrieves the HDR metadata associated with the
126 // texture image set by the most recent call to updateTexImage.
127 const HdrMetadata& getCurrentHdrMetadata() const;
128
Chia-I Wuf1405182017-11-27 11:29:21 -0800129 // getFrameNumber retrieves the frame number associated with the texture
130 // image set by the most recent call to updateTexImage.
131 //
132 // The frame number is an incrementing counter set to 0 at the creation of
133 // the BufferQueue associated with this consumer.
134 uint64_t getFrameNumber();
135
Chia-I Wu67dcc692017-11-27 14:51:06 -0800136 bool getTransformToDisplayInverse() const;
137
138 // must be called from SF main thread
139 const Region& getSurfaceDamage() const;
140
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800141 // getCurrentApi retrieves the API which queues the current buffer.
142 int getCurrentApi() const;
143
Chia-I Wu221b5922017-12-14 13:59:16 -0800144 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800145 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
146
147 // setFilteringEnabled sets whether the transform matrix should be computed
148 // for use with bilinear filtering.
149 void setFilteringEnabled(bool enabled);
150
151 // getCurrentBuffer returns the buffer associated with the current image.
152 // When outSlot is not nullptr, the current buffer slot index is also
153 // returned.
154 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr) const;
155
Chia-I Wuf1405182017-11-27 11:29:21 -0800156 // getCurrentCrop returns the cropping rectangle of the current buffer.
157 Rect getCurrentCrop() const;
158
159 // getCurrentTransform returns the transform of the current buffer.
160 uint32_t getCurrentTransform() const;
161
162 // getCurrentScalingMode returns the scaling mode of the current buffer.
163 uint32_t getCurrentScalingMode() const;
164
165 // getCurrentFence returns the fence indicating when the current buffer is
166 // ready to be read from.
167 sp<Fence> getCurrentFence() const;
168
169 // getCurrentFence returns the FenceTime indicating when the current
170 // buffer is ready to be read from.
171 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
172
173 // setConsumerUsageBits overrides the ConsumerBase method to OR
174 // DEFAULT_USAGE_FLAGS to usage.
175 status_t setConsumerUsageBits(uint64_t usage);
176
Chia-I Wuf1405182017-11-27 11:29:21 -0800177protected:
178 // abandonLocked overrides the ConsumerBase method to clear
179 // mCurrentTextureImage in addition to the ConsumerBase behavior.
180 virtual void abandonLocked();
181
182 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
183 // specific info in addition to the ConsumerBase behavior.
184 virtual void dumpLocked(String8& result, const char* prefix) const;
185
186 // acquireBufferLocked overrides the ConsumerBase method to update the
Chia-I Wu6748db42017-12-01 10:53:53 -0800187 // mImages array in addition to the ConsumerBase behavior.
Chia-I Wuf1405182017-11-27 11:29:21 -0800188 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
189 uint64_t maxFrameNumber = 0) override;
190
Chia-I Wu6748db42017-12-01 10:53:53 -0800191 bool canUseImageCrop(const Rect& crop) const;
192
Chia-I Wuf1405182017-11-27 11:29:21 -0800193 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800194 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800195
196 bool isPending;
197 int currentTexture;
198 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800199 };
200
201 // This releases the buffer in the slot referenced by mCurrentTexture,
202 // then updates state to refer to the BufferItem, which must be a
203 // newly-acquired buffer. If pendingRelease is not null, the parameters
204 // which would have been passed to releaseBufferLocked upon the successful
205 // completion of the method will instead be returned to the caller, so that
206 // it may call releaseBufferLocked itself later.
207 status_t updateAndReleaseLocked(const BufferItem& item,
208 PendingRelease* pendingRelease = nullptr);
209
Chia-I Wu6748db42017-12-01 10:53:53 -0800210 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target. Uses
Chia-I Wuf1405182017-11-27 11:29:21 -0800211 // mCurrentTexture if it's set, mCurrentTextureImage if not. If the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800212 // bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800213 status_t bindTextureImageLocked();
214
Chia-I Wuf1405182017-11-27 11:29:21 -0800215private:
Chia-I Wu6748db42017-12-01 10:53:53 -0800216 // Image is a utility class for tracking and creating RE::Images. There
Chia-I Wuf1405182017-11-27 11:29:21 -0800217 // is primarily just one image per slot, but there is also special cases:
Chia-I Wuf1405182017-11-27 11:29:21 -0800218 // - After freeBuffer, we must still keep the current image/buffer
Chia-I Wu6748db42017-12-01 10:53:53 -0800219 // Reference counting RE::Images lets us handle all these cases easily while
220 // also only creating new RE::Images from buffers when required.
221 class Image : public LightRefBase<Image> {
Chia-I Wuf1405182017-11-27 11:29:21 -0800222 public:
Chia-I Wu6748db42017-12-01 10:53:53 -0800223 Image(sp<GraphicBuffer> graphicBuffer, const RenderEngine& engine);
Chia-I Wuf1405182017-11-27 11:29:21 -0800224
Chia-I Wu6748db42017-12-01 10:53:53 -0800225 Image(const Image& rhs) = delete;
226 Image& operator=(const Image& rhs) = delete;
Chia-I Wuf1405182017-11-27 11:29:21 -0800227
Chia-I Wu6748db42017-12-01 10:53:53 -0800228 // createIfNeeded creates an RE::Image if required (we haven't created
229 // one yet, or the crop-rect has changed).
230 status_t createIfNeeded(const Rect& imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800231
232 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
233 const native_handle* graphicBufferHandle() {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800234 return mGraphicBuffer == nullptr ? nullptr : mGraphicBuffer->handle;
Chia-I Wuf1405182017-11-27 11:29:21 -0800235 }
236
Chia-I Wu6748db42017-12-01 10:53:53 -0800237 const RE::Image& image() const { return mImage; }
238
Chia-I Wuf1405182017-11-27 11:29:21 -0800239 private:
240 // Only allow instantiation using ref counting.
Chia-I Wu6748db42017-12-01 10:53:53 -0800241 friend class LightRefBase<Image>;
242 virtual ~Image() = default;
Chia-I Wuf1405182017-11-27 11:29:21 -0800243
244 // mGraphicBuffer is the buffer that was used to create this image.
245 sp<GraphicBuffer> mGraphicBuffer;
246
Chia-I Wu6748db42017-12-01 10:53:53 -0800247 // mImage is the image created from mGraphicBuffer.
248 RE::Image mImage;
249 bool mCreated;
250 int32_t mCropWidth;
251 int32_t mCropHeight;
Chia-I Wuf1405182017-11-27 11:29:21 -0800252 };
253
254 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800255 // initialized this will release the reference to the GraphicBuffer in
256 // that slot and destroy the RE::Image in that slot. Otherwise it has no
257 // effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800258 //
259 // This method must be called with mMutex locked.
260 virtual void freeBufferLocked(int slotIndex);
261
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800262 // IConsumerListener interface
263 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800264 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800265 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
266 FrameEventHistoryDelta* outDelta) override;
267
Chia-I Wuf1405182017-11-27 11:29:21 -0800268 // computeCurrentTransformMatrixLocked computes the transform matrix for the
269 // current texture. It uses mCurrentTransform and the current GraphicBuffer
270 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800271 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800272 void computeCurrentTransformMatrixLocked();
273
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800274 // doFenceWaitLocked inserts a wait command into the RenderEngine command
275 // stream to ensure that it is safe for future RenderEngine commands to
Chia-I Wuf1405182017-11-27 11:29:21 -0800276 // access the current texture buffer.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800277 status_t doFenceWaitLocked() const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800278
279 // syncForReleaseLocked performs the synchronization needed to release the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800280 // current slot from RenderEngine. If needed it will set the current
281 // slot's fence to guard against a producer accessing the buffer before
282 // the outstanding accesses have completed.
283 status_t syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800284
Chia-I Wuf1405182017-11-27 11:29:21 -0800285 // The default consumer usage flags that BufferLayerConsumer always sets on its
286 // BufferQueue instance; these will be OR:d with any additional flags passed
287 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
288 // consume buffers as hardware textures.
289 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
290
Chia-I Wu6748db42017-12-01 10:53:53 -0800291 // mCurrentTextureImage is the Image/buffer of the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800292 // possible that this buffer is not associated with any buffer slot, so we
293 // must track it separately in order to support the getCurrentBuffer method.
Chia-I Wu6748db42017-12-01 10:53:53 -0800294 sp<Image> mCurrentTextureImage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800295
296 // mCurrentCrop is the crop rectangle that applies to the current texture.
297 // It gets set each time updateTexImage is called.
298 Rect mCurrentCrop;
299
300 // mCurrentTransform is the transform identifier for the current texture. It
301 // gets set each time updateTexImage is called.
302 uint32_t mCurrentTransform;
303
304 // mCurrentScalingMode is the scaling mode for the current texture. It gets
305 // set each time updateTexImage is called.
306 uint32_t mCurrentScalingMode;
307
308 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
309 sp<Fence> mCurrentFence;
310
311 // The FenceTime wrapper around mCurrentFence.
312 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
313
314 // mCurrentTransformMatrix is the transform matrix for the current texture.
315 // It gets computed by computeTransformMatrix each time updateTexImage is
316 // called.
317 float mCurrentTransformMatrix[16];
318
319 // mCurrentTimestamp is the timestamp for the current texture. It
320 // gets set each time updateTexImage is called.
321 int64_t mCurrentTimestamp;
322
323 // mCurrentDataSpace is the dataspace for the current texture. It
324 // gets set each time updateTexImage is called.
325 android_dataspace mCurrentDataSpace;
326
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700327 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
328 // gets set each time updateTexImage is called.
329 HdrMetadata mCurrentHdrMetadata;
330
Chia-I Wuf1405182017-11-27 11:29:21 -0800331 // mCurrentFrameNumber is the frame counter for the current texture.
332 // It gets set each time updateTexImage is called.
333 uint64_t mCurrentFrameNumber;
334
Chia-I Wu67dcc692017-11-27 14:51:06 -0800335 // Indicates this buffer must be transformed by the inverse transform of the screen
336 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
337 // This must be set/read from SurfaceFlinger's main thread.
338 bool mCurrentTransformToDisplayInverse;
339
340 // The portion of this surface that has changed since the previous frame
341 Region mCurrentSurfaceDamage;
342
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800343 int mCurrentApi;
344
Chia-I Wuf1405182017-11-27 11:29:21 -0800345 uint32_t mDefaultWidth, mDefaultHeight;
346
347 // mFilteringEnabled indicates whether the transform matrix is computed for
348 // use with bilinear filtering. It defaults to true and is changed by
349 // setFilteringEnabled().
350 bool mFilteringEnabled;
351
Chia-I Wu9f2db772017-11-30 21:06:50 -0800352 RenderEngine& mRE;
353
Chia-I Wu221b5922017-12-14 13:59:16 -0800354 // mTexName is the name of the RenderEngine texture to which streamed
355 // images will be bound when bindTexImage is called. It is set at
356 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800357 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800358
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800359 // The layer for this BufferLayerConsumer
360 const wp<Layer> mLayer;
361
Chia-I Wufd257f82017-11-27 14:51:06 -0800362 wp<ContentsChangedListener> mContentsChangedListener;
363
Chia-I Wu6748db42017-12-01 10:53:53 -0800364 // mImages stores the buffers that have been allocated by the BufferQueue
Chia-I Wuf1405182017-11-27 11:29:21 -0800365 // for each buffer slot. It is initialized to null pointers, and gets
366 // filled in with the result of BufferQueue::acquire when the
367 // client dequeues a buffer from a
368 // slot that has not yet been used. The buffer allocated to a slot will also
369 // be replaced if the requested buffer usage or geometry differs from that
370 // of the buffer allocated to a slot.
Chia-I Wu6748db42017-12-01 10:53:53 -0800371 sp<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS];
Chia-I Wuf1405182017-11-27 11:29:21 -0800372
373 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800374 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800375 // indicating that no buffer slot is currently bound to the texture. Note,
376 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
377 // that no buffer is bound to the texture. A call to setBufferCount will
378 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
379 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800380
381 // A release that is pending on the receipt of a new release fence from
382 // presentDisplay
383 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800384};
385
386// ----------------------------------------------------------------------------
387}; // namespace android
388
389#endif // ANDROID_BUFFERLAYERCONSUMER_H