blob: 51c2b414d73b0dd515bd2d3985ac82511d6fe07d [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 Wu221b5922017-12-14 13:59:16 -0800141 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800142 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
143
144 // setFilteringEnabled sets whether the transform matrix should be computed
145 // for use with bilinear filtering.
146 void setFilteringEnabled(bool enabled);
147
148 // getCurrentBuffer returns the buffer associated with the current image.
149 // When outSlot is not nullptr, the current buffer slot index is also
150 // returned.
151 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr) const;
152
Chia-I Wuf1405182017-11-27 11:29:21 -0800153 // getCurrentCrop returns the cropping rectangle of the current buffer.
154 Rect getCurrentCrop() const;
155
156 // getCurrentTransform returns the transform of the current buffer.
157 uint32_t getCurrentTransform() const;
158
159 // getCurrentScalingMode returns the scaling mode of the current buffer.
160 uint32_t getCurrentScalingMode() const;
161
162 // getCurrentFence returns the fence indicating when the current buffer is
163 // ready to be read from.
164 sp<Fence> getCurrentFence() const;
165
166 // getCurrentFence returns the FenceTime indicating when the current
167 // buffer is ready to be read from.
168 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
169
170 // setConsumerUsageBits overrides the ConsumerBase method to OR
171 // DEFAULT_USAGE_FLAGS to usage.
172 status_t setConsumerUsageBits(uint64_t usage);
173
Chia-I Wuf1405182017-11-27 11:29:21 -0800174protected:
175 // abandonLocked overrides the ConsumerBase method to clear
176 // mCurrentTextureImage in addition to the ConsumerBase behavior.
177 virtual void abandonLocked();
178
179 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
180 // specific info in addition to the ConsumerBase behavior.
181 virtual void dumpLocked(String8& result, const char* prefix) const;
182
183 // acquireBufferLocked overrides the ConsumerBase method to update the
Chia-I Wu6748db42017-12-01 10:53:53 -0800184 // mImages array in addition to the ConsumerBase behavior.
Chia-I Wuf1405182017-11-27 11:29:21 -0800185 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
186 uint64_t maxFrameNumber = 0) override;
187
Chia-I Wu6748db42017-12-01 10:53:53 -0800188 bool canUseImageCrop(const Rect& crop) const;
189
Chia-I Wuf1405182017-11-27 11:29:21 -0800190 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800191 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800192
193 bool isPending;
194 int currentTexture;
195 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800196 };
197
198 // This releases the buffer in the slot referenced by mCurrentTexture,
199 // then updates state to refer to the BufferItem, which must be a
200 // newly-acquired buffer. If pendingRelease is not null, the parameters
201 // which would have been passed to releaseBufferLocked upon the successful
202 // completion of the method will instead be returned to the caller, so that
203 // it may call releaseBufferLocked itself later.
204 status_t updateAndReleaseLocked(const BufferItem& item,
205 PendingRelease* pendingRelease = nullptr);
206
Chia-I Wu6748db42017-12-01 10:53:53 -0800207 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target. Uses
Chia-I Wuf1405182017-11-27 11:29:21 -0800208 // mCurrentTexture if it's set, mCurrentTextureImage if not. If the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800209 // bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800210 status_t bindTextureImageLocked();
211
Chia-I Wuf1405182017-11-27 11:29:21 -0800212private:
Chia-I Wu6748db42017-12-01 10:53:53 -0800213 // Image is a utility class for tracking and creating RE::Images. There
Chia-I Wuf1405182017-11-27 11:29:21 -0800214 // is primarily just one image per slot, but there is also special cases:
Chia-I Wuf1405182017-11-27 11:29:21 -0800215 // - After freeBuffer, we must still keep the current image/buffer
Chia-I Wu6748db42017-12-01 10:53:53 -0800216 // Reference counting RE::Images lets us handle all these cases easily while
217 // also only creating new RE::Images from buffers when required.
218 class Image : public LightRefBase<Image> {
Chia-I Wuf1405182017-11-27 11:29:21 -0800219 public:
Chia-I Wu6748db42017-12-01 10:53:53 -0800220 Image(sp<GraphicBuffer> graphicBuffer, const RenderEngine& engine);
Chia-I Wuf1405182017-11-27 11:29:21 -0800221
Chia-I Wu6748db42017-12-01 10:53:53 -0800222 Image(const Image& rhs) = delete;
223 Image& operator=(const Image& rhs) = delete;
Chia-I Wuf1405182017-11-27 11:29:21 -0800224
Chia-I Wu6748db42017-12-01 10:53:53 -0800225 // createIfNeeded creates an RE::Image if required (we haven't created
226 // one yet, or the crop-rect has changed).
227 status_t createIfNeeded(const Rect& imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800228
229 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
230 const native_handle* graphicBufferHandle() {
231 return mGraphicBuffer == NULL ? NULL : mGraphicBuffer->handle;
232 }
233
Chia-I Wu6748db42017-12-01 10:53:53 -0800234 const RE::Image& image() const { return mImage; }
235
Chia-I Wuf1405182017-11-27 11:29:21 -0800236 private:
237 // Only allow instantiation using ref counting.
Chia-I Wu6748db42017-12-01 10:53:53 -0800238 friend class LightRefBase<Image>;
239 virtual ~Image() = default;
Chia-I Wuf1405182017-11-27 11:29:21 -0800240
241 // mGraphicBuffer is the buffer that was used to create this image.
242 sp<GraphicBuffer> mGraphicBuffer;
243
Chia-I Wu6748db42017-12-01 10:53:53 -0800244 // mImage is the image created from mGraphicBuffer.
245 RE::Image mImage;
246 bool mCreated;
247 int32_t mCropWidth;
248 int32_t mCropHeight;
Chia-I Wuf1405182017-11-27 11:29:21 -0800249 };
250
251 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800252 // initialized this will release the reference to the GraphicBuffer in
253 // that slot and destroy the RE::Image in that slot. Otherwise it has no
254 // effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800255 //
256 // This method must be called with mMutex locked.
257 virtual void freeBufferLocked(int slotIndex);
258
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800259 // IConsumerListener interface
260 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800261 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800262 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
263 FrameEventHistoryDelta* outDelta) override;
264
Chia-I Wuf1405182017-11-27 11:29:21 -0800265 // computeCurrentTransformMatrixLocked computes the transform matrix for the
266 // current texture. It uses mCurrentTransform and the current GraphicBuffer
267 // to compute this matrix and stores it in mCurrentTransformMatrix.
268 // mCurrentTextureImage must not be NULL.
269 void computeCurrentTransformMatrixLocked();
270
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800271 // doFenceWaitLocked inserts a wait command into the RenderEngine command
272 // stream to ensure that it is safe for future RenderEngine commands to
Chia-I Wuf1405182017-11-27 11:29:21 -0800273 // access the current texture buffer.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800274 status_t doFenceWaitLocked() const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800275
276 // syncForReleaseLocked performs the synchronization needed to release the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800277 // current slot from RenderEngine. If needed it will set the current
278 // slot's fence to guard against a producer accessing the buffer before
279 // the outstanding accesses have completed.
280 status_t syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800281
Chia-I Wuf1405182017-11-27 11:29:21 -0800282 // The default consumer usage flags that BufferLayerConsumer always sets on its
283 // BufferQueue instance; these will be OR:d with any additional flags passed
284 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
285 // consume buffers as hardware textures.
286 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
287
Chia-I Wu6748db42017-12-01 10:53:53 -0800288 // mCurrentTextureImage is the Image/buffer of the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800289 // possible that this buffer is not associated with any buffer slot, so we
290 // must track it separately in order to support the getCurrentBuffer method.
Chia-I Wu6748db42017-12-01 10:53:53 -0800291 sp<Image> mCurrentTextureImage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800292
293 // mCurrentCrop is the crop rectangle that applies to the current texture.
294 // It gets set each time updateTexImage is called.
295 Rect mCurrentCrop;
296
297 // mCurrentTransform is the transform identifier for the current texture. It
298 // gets set each time updateTexImage is called.
299 uint32_t mCurrentTransform;
300
301 // mCurrentScalingMode is the scaling mode for the current texture. It gets
302 // set each time updateTexImage is called.
303 uint32_t mCurrentScalingMode;
304
305 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
306 sp<Fence> mCurrentFence;
307
308 // The FenceTime wrapper around mCurrentFence.
309 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
310
311 // mCurrentTransformMatrix is the transform matrix for the current texture.
312 // It gets computed by computeTransformMatrix each time updateTexImage is
313 // called.
314 float mCurrentTransformMatrix[16];
315
316 // mCurrentTimestamp is the timestamp for the current texture. It
317 // gets set each time updateTexImage is called.
318 int64_t mCurrentTimestamp;
319
320 // mCurrentDataSpace is the dataspace for the current texture. It
321 // gets set each time updateTexImage is called.
322 android_dataspace mCurrentDataSpace;
323
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700324 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
325 // gets set each time updateTexImage is called.
326 HdrMetadata mCurrentHdrMetadata;
327
Chia-I Wuf1405182017-11-27 11:29:21 -0800328 // mCurrentFrameNumber is the frame counter for the current texture.
329 // It gets set each time updateTexImage is called.
330 uint64_t mCurrentFrameNumber;
331
Chia-I Wu67dcc692017-11-27 14:51:06 -0800332 // Indicates this buffer must be transformed by the inverse transform of the screen
333 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
334 // This must be set/read from SurfaceFlinger's main thread.
335 bool mCurrentTransformToDisplayInverse;
336
337 // The portion of this surface that has changed since the previous frame
338 Region mCurrentSurfaceDamage;
339
Chia-I Wuf1405182017-11-27 11:29:21 -0800340 uint32_t mDefaultWidth, mDefaultHeight;
341
342 // mFilteringEnabled indicates whether the transform matrix is computed for
343 // use with bilinear filtering. It defaults to true and is changed by
344 // setFilteringEnabled().
345 bool mFilteringEnabled;
346
Chia-I Wu9f2db772017-11-30 21:06:50 -0800347 RenderEngine& mRE;
348
Chia-I Wu221b5922017-12-14 13:59:16 -0800349 // mTexName is the name of the RenderEngine texture to which streamed
350 // images will be bound when bindTexImage is called. It is set at
351 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800352 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800353
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800354 // The layer for this BufferLayerConsumer
355 const wp<Layer> mLayer;
356
Chia-I Wufd257f82017-11-27 14:51:06 -0800357 wp<ContentsChangedListener> mContentsChangedListener;
358
Chia-I Wu6748db42017-12-01 10:53:53 -0800359 // mImages stores the buffers that have been allocated by the BufferQueue
Chia-I Wuf1405182017-11-27 11:29:21 -0800360 // for each buffer slot. It is initialized to null pointers, and gets
361 // filled in with the result of BufferQueue::acquire when the
362 // client dequeues a buffer from a
363 // slot that has not yet been used. The buffer allocated to a slot will also
364 // be replaced if the requested buffer usage or geometry differs from that
365 // of the buffer allocated to a slot.
Chia-I Wu6748db42017-12-01 10:53:53 -0800366 sp<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS];
Chia-I Wuf1405182017-11-27 11:29:21 -0800367
368 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800369 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800370 // indicating that no buffer slot is currently bound to the texture. Note,
371 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
372 // that no buffer is bound to the texture. A call to setBufferCount will
373 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
374 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800375
376 // A release that is pending on the receipt of a new release fence from
377 // presentDisplay
378 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800379};
380
381// ----------------------------------------------------------------------------
382}; // namespace android
383
384#endif // ANDROID_BUFFERLAYERCONSUMER_H