blob: 9b1860833eec03e5cbf936679a3d657feeec134e [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>
24
25#include <ui/FenceTime.h>
26#include <ui/GraphicBuffer.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 Wu9f2db772017-11-30 21:06:50 -080038class RenderEngine;
Chia-I Wuf1405182017-11-27 11:29:21 -080039class String8;
40
41/*
42 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080043 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080044 *
Chia-I Wu221b5922017-12-14 13:59:16 -080045 * A typical usage pattern is to call updateTexImage() when a new frame is
46 * desired. If a new frame is available, the frame is latched. If not, the
47 * previous contents are retained. The texture is attached and updated after
48 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080049 *
Chia-I Wu221b5922017-12-14 13:59:16 -080050 * All calls to updateTexImage must be made with RenderEngine being current.
51 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080052 */
53class BufferLayerConsumer : public ConsumerBase {
54public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080055 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
56
57 class BufferRejecter {
58 friend class BufferLayerConsumer;
59 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
60
61 protected:
62 virtual ~BufferRejecter() {}
63 };
64
Chia-I Wufd257f82017-11-27 14:51:06 -080065 struct ContentsChangedListener : public FrameAvailableListener {
66 virtual void onSidebandStreamChanged() = 0;
67 };
Chia-I Wuf1405182017-11-27 11:29:21 -080068
Chia-I Wu221b5922017-12-14 13:59:16 -080069 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
70 // tex parameter indicates the name of the RenderEngine texture to which
71 // images are to be streamed.
Chia-I Wu9f2db772017-11-30 21:06:50 -080072 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, RenderEngine& engine, uint32_t tex,
73 Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080074
Chia-I Wufd257f82017-11-27 14:51:06 -080075 // Sets the contents changed listener. This should be used instead of
76 // ConsumerBase::setFrameAvailableListener().
77 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
78
Chia-I Wuda5c7302017-11-27 14:51:06 -080079 nsecs_t computeExpectedPresent(const DispSync& dispSync);
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.
Chia-I Wuda5c7302017-11-27 14:51:06 -080092 status_t updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync, bool* autoRefresh,
93 bool* queuedBuffer, uint64_t maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -080094
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080095 // See BufferLayerConsumer::bindTextureImageLocked().
96 status_t bindTextureImage();
97
Chia-I Wuf1405182017-11-27 11:29:21 -080098 // setReleaseFence stores a fence that will signal when the current buffer
99 // is no longer being read. This fence will be returned to the producer
100 // when the current buffer is released by updateTexImage(). Multiple
101 // fences can be set for a given buffer; they will be merged into a single
102 // union fence.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800103 void setReleaseFence(const sp<Fence>& fence);
104
105 bool releasePendingBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800106
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800107 sp<Fence> getPrevFinalReleaseFence() const;
108
Chia-I Wu221b5922017-12-14 13:59:16 -0800109 // See GLConsumer::getTransformMatrix.
Chia-I Wuf1405182017-11-27 11:29:21 -0800110 void getTransformMatrix(float mtx[16]);
111
Chia-I Wuf1405182017-11-27 11:29:21 -0800112 // getTimestamp retrieves the timestamp associated with the texture image
113 // set by the most recent call to updateTexImage.
114 //
115 // The timestamp is in nanoseconds, and is monotonically increasing. Its
116 // other semantics (zero point, etc) are source-dependent and should be
117 // documented by the source.
118 int64_t getTimestamp();
119
120 // getDataSpace retrieves the DataSpace associated with the texture image
121 // set by the most recent call to updateTexImage.
122 android_dataspace getCurrentDataSpace();
123
124 // getFrameNumber retrieves the frame number associated with the texture
125 // image set by the most recent call to updateTexImage.
126 //
127 // The frame number is an incrementing counter set to 0 at the creation of
128 // the BufferQueue associated with this consumer.
129 uint64_t getFrameNumber();
130
Chia-I Wu67dcc692017-11-27 14:51:06 -0800131 bool getTransformToDisplayInverse() const;
132
133 // must be called from SF main thread
134 const Region& getSurfaceDamage() const;
135
Chia-I Wu221b5922017-12-14 13:59:16 -0800136 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800137 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
138
139 // setFilteringEnabled sets whether the transform matrix should be computed
140 // for use with bilinear filtering.
141 void setFilteringEnabled(bool enabled);
142
143 // getCurrentBuffer returns the buffer associated with the current image.
144 // When outSlot is not nullptr, the current buffer slot index is also
145 // returned.
146 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr) const;
147
Chia-I Wuf1405182017-11-27 11:29:21 -0800148 // getCurrentCrop returns the cropping rectangle of the current buffer.
149 Rect getCurrentCrop() const;
150
151 // getCurrentTransform returns the transform of the current buffer.
152 uint32_t getCurrentTransform() const;
153
154 // getCurrentScalingMode returns the scaling mode of the current buffer.
155 uint32_t getCurrentScalingMode() const;
156
157 // getCurrentFence returns the fence indicating when the current buffer is
158 // ready to be read from.
159 sp<Fence> getCurrentFence() const;
160
161 // getCurrentFence returns the FenceTime indicating when the current
162 // buffer is ready to be read from.
163 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
164
165 // setConsumerUsageBits overrides the ConsumerBase method to OR
166 // DEFAULT_USAGE_FLAGS to usage.
167 status_t setConsumerUsageBits(uint64_t usage);
168
Chia-I Wuf1405182017-11-27 11:29:21 -0800169protected:
170 // abandonLocked overrides the ConsumerBase method to clear
171 // mCurrentTextureImage in addition to the ConsumerBase behavior.
172 virtual void abandonLocked();
173
174 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
175 // specific info in addition to the ConsumerBase behavior.
176 virtual void dumpLocked(String8& result, const char* prefix) const;
177
178 // acquireBufferLocked overrides the ConsumerBase method to update the
Chia-I Wu6748db42017-12-01 10:53:53 -0800179 // mImages array in addition to the ConsumerBase behavior.
Chia-I Wuf1405182017-11-27 11:29:21 -0800180 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
181 uint64_t maxFrameNumber = 0) override;
182
Chia-I Wu6748db42017-12-01 10:53:53 -0800183 bool canUseImageCrop(const Rect& crop) const;
184
Chia-I Wuf1405182017-11-27 11:29:21 -0800185 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800186 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800187
188 bool isPending;
189 int currentTexture;
190 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800191 };
192
193 // This releases the buffer in the slot referenced by mCurrentTexture,
194 // then updates state to refer to the BufferItem, which must be a
195 // newly-acquired buffer. If pendingRelease is not null, the parameters
196 // which would have been passed to releaseBufferLocked upon the successful
197 // completion of the method will instead be returned to the caller, so that
198 // it may call releaseBufferLocked itself later.
199 status_t updateAndReleaseLocked(const BufferItem& item,
200 PendingRelease* pendingRelease = nullptr);
201
Chia-I Wu6748db42017-12-01 10:53:53 -0800202 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target. Uses
Chia-I Wuf1405182017-11-27 11:29:21 -0800203 // mCurrentTexture if it's set, mCurrentTextureImage if not. If the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800204 // bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800205 status_t bindTextureImageLocked();
206
Chia-I Wuf1405182017-11-27 11:29:21 -0800207private:
Chia-I Wu6748db42017-12-01 10:53:53 -0800208 // Image is a utility class for tracking and creating RE::Images. There
Chia-I Wuf1405182017-11-27 11:29:21 -0800209 // is primarily just one image per slot, but there is also special cases:
Chia-I Wuf1405182017-11-27 11:29:21 -0800210 // - After freeBuffer, we must still keep the current image/buffer
Chia-I Wu6748db42017-12-01 10:53:53 -0800211 // Reference counting RE::Images lets us handle all these cases easily while
212 // also only creating new RE::Images from buffers when required.
213 class Image : public LightRefBase<Image> {
Chia-I Wuf1405182017-11-27 11:29:21 -0800214 public:
Chia-I Wu6748db42017-12-01 10:53:53 -0800215 Image(sp<GraphicBuffer> graphicBuffer, const RenderEngine& engine);
Chia-I Wuf1405182017-11-27 11:29:21 -0800216
Chia-I Wu6748db42017-12-01 10:53:53 -0800217 Image(const Image& rhs) = delete;
218 Image& operator=(const Image& rhs) = delete;
Chia-I Wuf1405182017-11-27 11:29:21 -0800219
Chia-I Wu6748db42017-12-01 10:53:53 -0800220 // createIfNeeded creates an RE::Image if required (we haven't created
221 // one yet, or the crop-rect has changed).
222 status_t createIfNeeded(const Rect& imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800223
224 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
225 const native_handle* graphicBufferHandle() {
226 return mGraphicBuffer == NULL ? NULL : mGraphicBuffer->handle;
227 }
228
Chia-I Wu6748db42017-12-01 10:53:53 -0800229 const RE::Image& image() const { return mImage; }
230
Chia-I Wuf1405182017-11-27 11:29:21 -0800231 private:
232 // Only allow instantiation using ref counting.
Chia-I Wu6748db42017-12-01 10:53:53 -0800233 friend class LightRefBase<Image>;
234 virtual ~Image() = default;
Chia-I Wuf1405182017-11-27 11:29:21 -0800235
236 // mGraphicBuffer is the buffer that was used to create this image.
237 sp<GraphicBuffer> mGraphicBuffer;
238
Chia-I Wu6748db42017-12-01 10:53:53 -0800239 // mImage is the image created from mGraphicBuffer.
240 RE::Image mImage;
241 bool mCreated;
242 int32_t mCropWidth;
243 int32_t mCropHeight;
Chia-I Wuf1405182017-11-27 11:29:21 -0800244 };
245
246 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800247 // initialized this will release the reference to the GraphicBuffer in
248 // that slot and destroy the RE::Image in that slot. Otherwise it has no
249 // effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800250 //
251 // This method must be called with mMutex locked.
252 virtual void freeBufferLocked(int slotIndex);
253
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800254 // IConsumerListener interface
255 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800256 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800257 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
258 FrameEventHistoryDelta* outDelta) override;
259
Chia-I Wuf1405182017-11-27 11:29:21 -0800260 // computeCurrentTransformMatrixLocked computes the transform matrix for the
261 // current texture. It uses mCurrentTransform and the current GraphicBuffer
262 // to compute this matrix and stores it in mCurrentTransformMatrix.
263 // mCurrentTextureImage must not be NULL.
264 void computeCurrentTransformMatrixLocked();
265
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800266 // doFenceWaitLocked inserts a wait command into the RenderEngine command
267 // stream to ensure that it is safe for future RenderEngine commands to
Chia-I Wuf1405182017-11-27 11:29:21 -0800268 // access the current texture buffer.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800269 status_t doFenceWaitLocked() const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800270
271 // syncForReleaseLocked performs the synchronization needed to release the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800272 // current slot from RenderEngine. If needed it will set the current
273 // slot's fence to guard against a producer accessing the buffer before
274 // the outstanding accesses have completed.
275 status_t syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800276
Chia-I Wuf1405182017-11-27 11:29:21 -0800277 // The default consumer usage flags that BufferLayerConsumer always sets on its
278 // BufferQueue instance; these will be OR:d with any additional flags passed
279 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
280 // consume buffers as hardware textures.
281 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
282
Chia-I Wu6748db42017-12-01 10:53:53 -0800283 // mCurrentTextureImage is the Image/buffer of the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800284 // possible that this buffer is not associated with any buffer slot, so we
285 // must track it separately in order to support the getCurrentBuffer method.
Chia-I Wu6748db42017-12-01 10:53:53 -0800286 sp<Image> mCurrentTextureImage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800287
288 // mCurrentCrop is the crop rectangle that applies to the current texture.
289 // It gets set each time updateTexImage is called.
290 Rect mCurrentCrop;
291
292 // mCurrentTransform is the transform identifier for the current texture. It
293 // gets set each time updateTexImage is called.
294 uint32_t mCurrentTransform;
295
296 // mCurrentScalingMode is the scaling mode for the current texture. It gets
297 // set each time updateTexImage is called.
298 uint32_t mCurrentScalingMode;
299
300 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
301 sp<Fence> mCurrentFence;
302
303 // The FenceTime wrapper around mCurrentFence.
304 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
305
306 // mCurrentTransformMatrix is the transform matrix for the current texture.
307 // It gets computed by computeTransformMatrix each time updateTexImage is
308 // called.
309 float mCurrentTransformMatrix[16];
310
311 // mCurrentTimestamp is the timestamp for the current texture. It
312 // gets set each time updateTexImage is called.
313 int64_t mCurrentTimestamp;
314
315 // mCurrentDataSpace is the dataspace for the current texture. It
316 // gets set each time updateTexImage is called.
317 android_dataspace mCurrentDataSpace;
318
319 // mCurrentFrameNumber is the frame counter for the current texture.
320 // It gets set each time updateTexImage is called.
321 uint64_t mCurrentFrameNumber;
322
Chia-I Wu67dcc692017-11-27 14:51:06 -0800323 // Indicates this buffer must be transformed by the inverse transform of the screen
324 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
325 // This must be set/read from SurfaceFlinger's main thread.
326 bool mCurrentTransformToDisplayInverse;
327
328 // The portion of this surface that has changed since the previous frame
329 Region mCurrentSurfaceDamage;
330
Chia-I Wuf1405182017-11-27 11:29:21 -0800331 uint32_t mDefaultWidth, mDefaultHeight;
332
333 // mFilteringEnabled indicates whether the transform matrix is computed for
334 // use with bilinear filtering. It defaults to true and is changed by
335 // setFilteringEnabled().
336 bool mFilteringEnabled;
337
Chia-I Wu9f2db772017-11-30 21:06:50 -0800338 RenderEngine& mRE;
339
Chia-I Wu221b5922017-12-14 13:59:16 -0800340 // mTexName is the name of the RenderEngine texture to which streamed
341 // images will be bound when bindTexImage is called. It is set at
342 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800343 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800344
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800345 // The layer for this BufferLayerConsumer
346 const wp<Layer> mLayer;
347
Chia-I Wufd257f82017-11-27 14:51:06 -0800348 wp<ContentsChangedListener> mContentsChangedListener;
349
Chia-I Wu6748db42017-12-01 10:53:53 -0800350 // mImages stores the buffers that have been allocated by the BufferQueue
Chia-I Wuf1405182017-11-27 11:29:21 -0800351 // for each buffer slot. It is initialized to null pointers, and gets
352 // filled in with the result of BufferQueue::acquire when the
353 // client dequeues a buffer from a
354 // slot that has not yet been used. The buffer allocated to a slot will also
355 // be replaced if the requested buffer usage or geometry differs from that
356 // of the buffer allocated to a slot.
Chia-I Wu6748db42017-12-01 10:53:53 -0800357 sp<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS];
Chia-I Wuf1405182017-11-27 11:29:21 -0800358
359 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800360 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800361 // indicating that no buffer slot is currently bound to the texture. Note,
362 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
363 // that no buffer is bound to the texture. A call to setBufferCount will
364 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
365 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800366
367 // A release that is pending on the receipt of a new release fence from
368 // presentDisplay
369 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800370};
371
372// ----------------------------------------------------------------------------
373}; // namespace android
374
375#endif // ANDROID_BUFFERLAYERCONSUMER_H