blob: 11048d8df8e18b9177113f888257157214f3795d [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>
Chia-I Wu67dcc692017-11-27 14:51:06 -080026#include <ui/Region.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080027
28#include <utils/String8.h>
29#include <utils/Vector.h>
30#include <utils/threads.h>
31
32namespace android {
33// ----------------------------------------------------------------------------
34
Chia-I Wuda5c7302017-11-27 14:51:06 -080035class DispSync;
Chia-I Wuc75c44d2017-11-27 14:32:57 -080036class Layer;
Chia-I Wuf1405182017-11-27 11:29:21 -080037class String8;
38
Lloyd Pique144e1162017-12-20 16:44:52 -080039namespace RE {
40class RenderEngine;
41class Image;
42} // namespace RE
43
Chia-I Wuf1405182017-11-27 11:29:21 -080044/*
45 * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
Chia-I Wu221b5922017-12-14 13:59:16 -080046 * and makes them available to RenderEngine as a texture.
Chia-I Wuf1405182017-11-27 11:29:21 -080047 *
Chia-I Wu221b5922017-12-14 13:59:16 -080048 * A typical usage pattern is to call updateTexImage() when a new frame is
49 * desired. If a new frame is available, the frame is latched. If not, the
50 * previous contents are retained. The texture is attached and updated after
51 * bindTextureImage() is called.
Chia-I Wuf1405182017-11-27 11:29:21 -080052 *
Chia-I Wu221b5922017-12-14 13:59:16 -080053 * All calls to updateTexImage must be made with RenderEngine being current.
54 * The texture is attached to the TEXTURE_EXTERNAL texture target.
Chia-I Wuf1405182017-11-27 11:29:21 -080055 */
56class BufferLayerConsumer : public ConsumerBase {
57public:
Chia-I Wuda5c7302017-11-27 14:51:06 -080058 static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
59
60 class BufferRejecter {
61 friend class BufferLayerConsumer;
62 virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
63
64 protected:
65 virtual ~BufferRejecter() {}
66 };
67
Chia-I Wufd257f82017-11-27 14:51:06 -080068 struct ContentsChangedListener : public FrameAvailableListener {
69 virtual void onSidebandStreamChanged() = 0;
70 };
Chia-I Wuf1405182017-11-27 11:29:21 -080071
Chia-I Wu221b5922017-12-14 13:59:16 -080072 // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
73 // tex parameter indicates the name of the RenderEngine texture to which
74 // images are to be streamed.
Lloyd Pique144e1162017-12-20 16:44:52 -080075 BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, RE::RenderEngine& engine,
76 uint32_t tex, Layer* layer);
Chia-I Wuf1405182017-11-27 11:29:21 -080077
Chia-I Wufd257f82017-11-27 14:51:06 -080078 // Sets the contents changed listener. This should be used instead of
79 // ConsumerBase::setFrameAvailableListener().
80 void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
81
Chia-I Wuda5c7302017-11-27 14:51:06 -080082 nsecs_t computeExpectedPresent(const DispSync& dispSync);
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.
Chia-I Wuda5c7302017-11-27 14:51:06 -080095 status_t updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync, bool* autoRefresh,
96 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.
125 android_dataspace getCurrentDataSpace();
126
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
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800143 // getCurrentApi retrieves the API which queues the current buffer.
144 int getCurrentApi() const;
145
Chia-I Wu221b5922017-12-14 13:59:16 -0800146 // See GLConsumer::setDefaultBufferSize.
Chia-I Wuf1405182017-11-27 11:29:21 -0800147 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
148
149 // setFilteringEnabled sets whether the transform matrix should be computed
150 // for use with bilinear filtering.
151 void setFilteringEnabled(bool enabled);
152
153 // getCurrentBuffer returns the buffer associated with the current image.
154 // When outSlot is not nullptr, the current buffer slot index is also
155 // returned.
156 sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr) const;
157
Chia-I Wuf1405182017-11-27 11:29:21 -0800158 // getCurrentCrop returns the cropping rectangle of the current buffer.
159 Rect getCurrentCrop() const;
160
161 // getCurrentTransform returns the transform of the current buffer.
162 uint32_t getCurrentTransform() const;
163
164 // getCurrentScalingMode returns the scaling mode of the current buffer.
165 uint32_t getCurrentScalingMode() const;
166
167 // getCurrentFence returns the fence indicating when the current buffer is
168 // ready to be read from.
169 sp<Fence> getCurrentFence() const;
170
171 // getCurrentFence returns the FenceTime indicating when the current
172 // buffer is ready to be read from.
173 std::shared_ptr<FenceTime> getCurrentFenceTime() const;
174
175 // setConsumerUsageBits overrides the ConsumerBase method to OR
176 // DEFAULT_USAGE_FLAGS to usage.
177 status_t setConsumerUsageBits(uint64_t usage);
178
Chia-I Wuf1405182017-11-27 11:29:21 -0800179protected:
180 // abandonLocked overrides the ConsumerBase method to clear
181 // mCurrentTextureImage in addition to the ConsumerBase behavior.
182 virtual void abandonLocked();
183
184 // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
185 // specific info in addition to the ConsumerBase behavior.
186 virtual void dumpLocked(String8& result, const char* prefix) const;
187
188 // acquireBufferLocked overrides the ConsumerBase method to update the
Chia-I Wu6748db42017-12-01 10:53:53 -0800189 // mImages array in addition to the ConsumerBase behavior.
Chia-I Wuf1405182017-11-27 11:29:21 -0800190 virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
191 uint64_t maxFrameNumber = 0) override;
192
Chia-I Wu6748db42017-12-01 10:53:53 -0800193 bool canUseImageCrop(const Rect& crop) const;
194
Chia-I Wuf1405182017-11-27 11:29:21 -0800195 struct PendingRelease {
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800196 PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800197
198 bool isPending;
199 int currentTexture;
200 sp<GraphicBuffer> graphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800201 };
202
203 // This releases the buffer in the slot referenced by mCurrentTexture,
204 // then updates state to refer to the BufferItem, which must be a
205 // newly-acquired buffer. If pendingRelease is not null, the parameters
206 // which would have been passed to releaseBufferLocked upon the successful
207 // completion of the method will instead be returned to the caller, so that
208 // it may call releaseBufferLocked itself later.
209 status_t updateAndReleaseLocked(const BufferItem& item,
210 PendingRelease* pendingRelease = nullptr);
211
Chia-I Wu6748db42017-12-01 10:53:53 -0800212 // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target. Uses
Chia-I Wuf1405182017-11-27 11:29:21 -0800213 // mCurrentTexture if it's set, mCurrentTextureImage if not. If the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800214 // bind succeeds, this calls doFenceWait.
Chia-I Wuf1405182017-11-27 11:29:21 -0800215 status_t bindTextureImageLocked();
216
Chia-I Wuf1405182017-11-27 11:29:21 -0800217private:
Chia-I Wu6748db42017-12-01 10:53:53 -0800218 // Image is a utility class for tracking and creating RE::Images. There
Chia-I Wuf1405182017-11-27 11:29:21 -0800219 // is primarily just one image per slot, but there is also special cases:
Chia-I Wuf1405182017-11-27 11:29:21 -0800220 // - After freeBuffer, we must still keep the current image/buffer
Chia-I Wu6748db42017-12-01 10:53:53 -0800221 // Reference counting RE::Images lets us handle all these cases easily while
222 // also only creating new RE::Images from buffers when required.
223 class Image : public LightRefBase<Image> {
Chia-I Wuf1405182017-11-27 11:29:21 -0800224 public:
Lloyd Pique144e1162017-12-20 16:44:52 -0800225 Image(sp<GraphicBuffer> graphicBuffer, RE::RenderEngine& engine);
Chia-I Wuf1405182017-11-27 11:29:21 -0800226
Chia-I Wu6748db42017-12-01 10:53:53 -0800227 Image(const Image& rhs) = delete;
228 Image& operator=(const Image& rhs) = delete;
Chia-I Wuf1405182017-11-27 11:29:21 -0800229
Chia-I Wu6748db42017-12-01 10:53:53 -0800230 // createIfNeeded creates an RE::Image if required (we haven't created
231 // one yet, or the crop-rect has changed).
232 status_t createIfNeeded(const Rect& imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800233
234 const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
235 const native_handle* graphicBufferHandle() {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800236 return mGraphicBuffer == nullptr ? nullptr : mGraphicBuffer->handle;
Chia-I Wuf1405182017-11-27 11:29:21 -0800237 }
238
Lloyd Pique144e1162017-12-20 16:44:52 -0800239 const RE::Image& image() const { return *mImage; }
Chia-I Wu6748db42017-12-01 10:53:53 -0800240
Chia-I Wuf1405182017-11-27 11:29:21 -0800241 private:
242 // Only allow instantiation using ref counting.
Chia-I Wu6748db42017-12-01 10:53:53 -0800243 friend class LightRefBase<Image>;
Lloyd Pique144e1162017-12-20 16:44:52 -0800244 virtual ~Image();
Chia-I Wuf1405182017-11-27 11:29:21 -0800245
246 // mGraphicBuffer is the buffer that was used to create this image.
247 sp<GraphicBuffer> mGraphicBuffer;
248
Chia-I Wu6748db42017-12-01 10:53:53 -0800249 // mImage is the image created from mGraphicBuffer.
Lloyd Pique144e1162017-12-20 16:44:52 -0800250 std::unique_ptr<RE::Image> mImage;
Chia-I Wu6748db42017-12-01 10:53:53 -0800251 bool mCreated;
252 int32_t mCropWidth;
253 int32_t mCropHeight;
Chia-I Wuf1405182017-11-27 11:29:21 -0800254 };
255
256 // freeBufferLocked frees up the given buffer slot. If the slot has been
Chia-I Wu221b5922017-12-14 13:59:16 -0800257 // initialized this will release the reference to the GraphicBuffer in
258 // that slot and destroy the RE::Image in that slot. Otherwise it has no
259 // effect.
Chia-I Wuf1405182017-11-27 11:29:21 -0800260 //
261 // This method must be called with mMutex locked.
262 virtual void freeBufferLocked(int slotIndex);
263
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800264 // IConsumerListener interface
265 void onDisconnect() override;
Chia-I Wufd257f82017-11-27 14:51:06 -0800266 void onSidebandStreamChanged() override;
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800267 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
268 FrameEventHistoryDelta* outDelta) override;
269
Chia-I Wuf1405182017-11-27 11:29:21 -0800270 // computeCurrentTransformMatrixLocked computes the transform matrix for the
271 // current texture. It uses mCurrentTransform and the current GraphicBuffer
272 // to compute this matrix and stores it in mCurrentTransformMatrix.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800273 // mCurrentTextureImage must not be nullptr.
Chia-I Wuf1405182017-11-27 11:29:21 -0800274 void computeCurrentTransformMatrixLocked();
275
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800276 // doFenceWaitLocked inserts a wait command into the RenderEngine command
277 // stream to ensure that it is safe for future RenderEngine commands to
Chia-I Wuf1405182017-11-27 11:29:21 -0800278 // access the current texture buffer.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800279 status_t doFenceWaitLocked() const;
Chia-I Wuf1405182017-11-27 11:29:21 -0800280
281 // syncForReleaseLocked performs the synchronization needed to release the
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800282 // current slot from RenderEngine. If needed it will set the current
283 // slot's fence to guard against a producer accessing the buffer before
284 // the outstanding accesses have completed.
285 status_t syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800286
Chia-I Wuf1405182017-11-27 11:29:21 -0800287 // The default consumer usage flags that BufferLayerConsumer always sets on its
288 // BufferQueue instance; these will be OR:d with any additional flags passed
289 // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
290 // consume buffers as hardware textures.
291 static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
292
Chia-I Wu6748db42017-12-01 10:53:53 -0800293 // mCurrentTextureImage is the Image/buffer of the current texture. It's
Chia-I Wuf1405182017-11-27 11:29:21 -0800294 // possible that this buffer is not associated with any buffer slot, so we
295 // must track it separately in order to support the getCurrentBuffer method.
Chia-I Wu6748db42017-12-01 10:53:53 -0800296 sp<Image> mCurrentTextureImage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800297
298 // mCurrentCrop is the crop rectangle that applies to the current texture.
299 // It gets set each time updateTexImage is called.
300 Rect mCurrentCrop;
301
302 // mCurrentTransform is the transform identifier for the current texture. It
303 // gets set each time updateTexImage is called.
304 uint32_t mCurrentTransform;
305
306 // mCurrentScalingMode is the scaling mode for the current texture. It gets
307 // set each time updateTexImage is called.
308 uint32_t mCurrentScalingMode;
309
310 // mCurrentFence is the fence received from BufferQueue in updateTexImage.
311 sp<Fence> mCurrentFence;
312
313 // The FenceTime wrapper around mCurrentFence.
314 std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
315
316 // mCurrentTransformMatrix is the transform matrix for the current texture.
317 // It gets computed by computeTransformMatrix each time updateTexImage is
318 // called.
319 float mCurrentTransformMatrix[16];
320
321 // mCurrentTimestamp is the timestamp for the current texture. It
322 // gets set each time updateTexImage is called.
323 int64_t mCurrentTimestamp;
324
325 // mCurrentDataSpace is the dataspace for the current texture. It
326 // gets set each time updateTexImage is called.
327 android_dataspace mCurrentDataSpace;
328
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700329 // mCurrentHdrMetadata is the HDR metadata for the current texture. It
330 // gets set each time updateTexImage is called.
331 HdrMetadata mCurrentHdrMetadata;
332
Chia-I Wuf1405182017-11-27 11:29:21 -0800333 // mCurrentFrameNumber is the frame counter for the current texture.
334 // It gets set each time updateTexImage is called.
335 uint64_t mCurrentFrameNumber;
336
Chia-I Wu67dcc692017-11-27 14:51:06 -0800337 // Indicates this buffer must be transformed by the inverse transform of the screen
338 // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
339 // This must be set/read from SurfaceFlinger's main thread.
340 bool mCurrentTransformToDisplayInverse;
341
342 // The portion of this surface that has changed since the previous frame
343 Region mCurrentSurfaceDamage;
344
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800345 int mCurrentApi;
346
Chia-I Wuf1405182017-11-27 11:29:21 -0800347 uint32_t mDefaultWidth, mDefaultHeight;
348
349 // mFilteringEnabled indicates whether the transform matrix is computed for
350 // use with bilinear filtering. It defaults to true and is changed by
351 // setFilteringEnabled().
352 bool mFilteringEnabled;
353
Lloyd Pique144e1162017-12-20 16:44:52 -0800354 RE::RenderEngine& mRE;
Chia-I Wu9f2db772017-11-30 21:06:50 -0800355
Chia-I Wu221b5922017-12-14 13:59:16 -0800356 // mTexName is the name of the RenderEngine texture to which streamed
357 // images will be bound when bindTexImage is called. It is set at
358 // construction time.
Chia-I Wuc91077c2017-11-27 13:32:04 -0800359 const uint32_t mTexName;
Chia-I Wuf1405182017-11-27 11:29:21 -0800360
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800361 // The layer for this BufferLayerConsumer
362 const wp<Layer> mLayer;
363
Chia-I Wufd257f82017-11-27 14:51:06 -0800364 wp<ContentsChangedListener> mContentsChangedListener;
365
Chia-I Wu6748db42017-12-01 10:53:53 -0800366 // mImages stores the buffers that have been allocated by the BufferQueue
Chia-I Wuf1405182017-11-27 11:29:21 -0800367 // for each buffer slot. It is initialized to null pointers, and gets
368 // filled in with the result of BufferQueue::acquire when the
369 // client dequeues a buffer from a
370 // slot that has not yet been used. The buffer allocated to a slot will also
371 // be replaced if the requested buffer usage or geometry differs from that
372 // of the buffer allocated to a slot.
Chia-I Wu6748db42017-12-01 10:53:53 -0800373 sp<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS];
Chia-I Wuf1405182017-11-27 11:29:21 -0800374
375 // mCurrentTexture is the buffer slot index of the buffer that is currently
Chia-I Wu221b5922017-12-14 13:59:16 -0800376 // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
Chia-I Wuf1405182017-11-27 11:29:21 -0800377 // indicating that no buffer slot is currently bound to the texture. Note,
378 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
379 // that no buffer is bound to the texture. A call to setBufferCount will
380 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
381 int mCurrentTexture;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800382
383 // A release that is pending on the receipt of a new release fence from
384 // presentDisplay
385 PendingRelease mPendingRelease;
Chia-I Wuf1405182017-11-27 11:29:21 -0800386};
387
388// ----------------------------------------------------------------------------
389}; // namespace android
390
391#endif // ANDROID_BUFFERLAYERCONSUMER_H