Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | #include <EGL/egl.h> |
| 21 | #include <EGL/eglext.h> |
| 22 | |
| 23 | #include <gui/BufferQueueDefs.h> |
| 24 | #include <gui/ConsumerBase.h> |
| 25 | |
| 26 | #include <ui/FenceTime.h> |
| 27 | #include <ui/GraphicBuffer.h> |
| 28 | |
| 29 | #include <utils/String8.h> |
| 30 | #include <utils/Vector.h> |
| 31 | #include <utils/threads.h> |
| 32 | |
| 33 | namespace android { |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
| 36 | class String8; |
| 37 | |
| 38 | /* |
| 39 | * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue, |
| 40 | * and makes them available to OpenGL as a texture. |
| 41 | * |
| 42 | * A typical usage pattern is to set up the BufferLayerConsumer with the |
| 43 | * desired options, and call updateTexImage() when a new frame is desired. |
| 44 | * If a new frame is available, the texture will be updated. If not, |
| 45 | * the previous contents are retained. |
| 46 | * |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 47 | * The texture is attached to the GL_TEXTURE_EXTERNAL_OES texture target, in |
| 48 | * the EGL context of the first thread that calls updateTexImage(). After that |
| 49 | * point, all calls to updateTexImage must be made with the same OpenGL ES |
| 50 | * context current. |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 51 | * |
| 52 | * This class was previously called SurfaceTexture. |
| 53 | */ |
| 54 | class BufferLayerConsumer : public ConsumerBase { |
| 55 | public: |
| 56 | enum { TEXTURE_EXTERNAL = 0x8D65 }; // GL_TEXTURE_EXTERNAL_OES |
| 57 | typedef ConsumerBase::FrameAvailableListener FrameAvailableListener; |
| 58 | |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 59 | // BufferLayerConsumer constructs a new BufferLayerConsumer object. |
| 60 | // The tex parameter indicates the name of the OpenGL ES |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 61 | // texture to which images are to be streamed. texTarget specifies the |
| 62 | // OpenGL ES texture target to which the texture will be bound in |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame^] | 63 | // updateTexImage. |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 64 | BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, uint32_t texureTarget, |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame^] | 65 | bool isControlledByApp); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 66 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 67 | // updateTexImage acquires the most recently queued buffer, and sets the |
| 68 | // image contents of the target texture to it. |
| 69 | // |
| 70 | // This call may only be made while the OpenGL ES context to which the |
| 71 | // target texture belongs is bound to the calling thread. |
| 72 | // |
| 73 | // This calls doGLFenceWait to ensure proper synchronization. |
| 74 | status_t updateTexImage(); |
| 75 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 76 | // setReleaseFence stores a fence that will signal when the current buffer |
| 77 | // is no longer being read. This fence will be returned to the producer |
| 78 | // when the current buffer is released by updateTexImage(). Multiple |
| 79 | // fences can be set for a given buffer; they will be merged into a single |
| 80 | // union fence. |
| 81 | virtual void setReleaseFence(const sp<Fence>& fence); |
| 82 | |
| 83 | // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix |
| 84 | // associated with the texture image set by the most recent call to |
| 85 | // updateTexImage. |
| 86 | // |
| 87 | // This transform matrix maps 2D homogeneous texture coordinates of the form |
| 88 | // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture |
| 89 | // coordinate that should be used to sample that location from the texture. |
| 90 | // Sampling the texture outside of the range of this transform is undefined. |
| 91 | // |
| 92 | // This transform is necessary to compensate for transforms that the stream |
| 93 | // content producer may implicitly apply to the content. By forcing users of |
| 94 | // a BufferLayerConsumer to apply this transform we avoid performing an extra |
| 95 | // copy of the data that would be needed to hide the transform from the |
| 96 | // user. |
| 97 | // |
| 98 | // The matrix is stored in column-major order so that it may be passed |
| 99 | // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv |
| 100 | // functions. |
| 101 | void getTransformMatrix(float mtx[16]); |
| 102 | |
| 103 | // Computes the transform matrix documented by getTransformMatrix |
| 104 | // from the BufferItem sub parts. |
| 105 | static void computeTransformMatrix(float outTransform[16], const sp<GraphicBuffer>& buf, |
| 106 | const Rect& cropRect, uint32_t transform, bool filtering); |
| 107 | |
| 108 | // Scale the crop down horizontally or vertically such that it has the |
| 109 | // same aspect ratio as the buffer does. |
| 110 | static Rect scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight); |
| 111 | |
| 112 | // 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 | |
| 131 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 132 | // requestBuffers when a with and height of zero is requested. |
| 133 | // A call to setDefaultBufferSize() may trigger requestBuffers() to |
| 134 | // be called from the client. |
| 135 | // The width and height parameters must be no greater than the minimum of |
| 136 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 137 | // An error due to invalid dimensions might not be reported until |
| 138 | // updateTexImage() is called. |
| 139 | status_t setDefaultBufferSize(uint32_t width, uint32_t height); |
| 140 | |
| 141 | // setFilteringEnabled sets whether the transform matrix should be computed |
| 142 | // for use with bilinear filtering. |
| 143 | void setFilteringEnabled(bool enabled); |
| 144 | |
| 145 | // getCurrentBuffer returns the buffer associated with the current image. |
| 146 | // When outSlot is not nullptr, the current buffer slot index is also |
| 147 | // returned. |
| 148 | sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr) const; |
| 149 | |
| 150 | // getCurrentTextureTarget returns the texture target of the current |
| 151 | // texture as returned by updateTexImage(). |
| 152 | uint32_t getCurrentTextureTarget() const; |
| 153 | |
| 154 | // getCurrentCrop returns the cropping rectangle of the current buffer. |
| 155 | Rect getCurrentCrop() const; |
| 156 | |
| 157 | // getCurrentTransform returns the transform of the current buffer. |
| 158 | uint32_t getCurrentTransform() const; |
| 159 | |
| 160 | // getCurrentScalingMode returns the scaling mode of the current buffer. |
| 161 | uint32_t getCurrentScalingMode() const; |
| 162 | |
| 163 | // getCurrentFence returns the fence indicating when the current buffer is |
| 164 | // ready to be read from. |
| 165 | sp<Fence> getCurrentFence() const; |
| 166 | |
| 167 | // getCurrentFence returns the FenceTime indicating when the current |
| 168 | // buffer is ready to be read from. |
| 169 | std::shared_ptr<FenceTime> getCurrentFenceTime() const; |
| 170 | |
| 171 | // setConsumerUsageBits overrides the ConsumerBase method to OR |
| 172 | // DEFAULT_USAGE_FLAGS to usage. |
| 173 | status_t setConsumerUsageBits(uint64_t usage); |
| 174 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 175 | protected: |
| 176 | // abandonLocked overrides the ConsumerBase method to clear |
| 177 | // mCurrentTextureImage in addition to the ConsumerBase behavior. |
| 178 | virtual void abandonLocked(); |
| 179 | |
| 180 | // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer- |
| 181 | // specific info in addition to the ConsumerBase behavior. |
| 182 | virtual void dumpLocked(String8& result, const char* prefix) const; |
| 183 | |
| 184 | // acquireBufferLocked overrides the ConsumerBase method to update the |
| 185 | // mEglSlots array in addition to the ConsumerBase behavior. |
| 186 | virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 187 | uint64_t maxFrameNumber = 0) override; |
| 188 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 189 | struct PendingRelease { |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame^] | 190 | PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {} |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 191 | |
| 192 | bool isPending; |
| 193 | int currentTexture; |
| 194 | sp<GraphicBuffer> graphicBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | // This releases the buffer in the slot referenced by mCurrentTexture, |
| 198 | // then updates state to refer to the BufferItem, which must be a |
| 199 | // newly-acquired buffer. If pendingRelease is not null, the parameters |
| 200 | // which would have been passed to releaseBufferLocked upon the successful |
| 201 | // completion of the method will instead be returned to the caller, so that |
| 202 | // it may call releaseBufferLocked itself later. |
| 203 | status_t updateAndReleaseLocked(const BufferItem& item, |
| 204 | PendingRelease* pendingRelease = nullptr); |
| 205 | |
| 206 | // Binds mTexName and the current buffer to mTexTarget. Uses |
| 207 | // mCurrentTexture if it's set, mCurrentTextureImage if not. If the |
| 208 | // bind succeeds, this calls doGLFenceWait. |
| 209 | status_t bindTextureImageLocked(); |
| 210 | |
| 211 | // Gets the current EGLDisplay and EGLContext values, and compares them |
| 212 | // to mEglDisplay and mEglContext. If the fields have been previously |
| 213 | // set, the values must match; if not, the fields are set to the current |
| 214 | // values. |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 215 | status_t checkAndUpdateEglStateLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 216 | |
| 217 | private: |
| 218 | // EglImage is a utility class for tracking and creating EGLImageKHRs. There |
| 219 | // is primarily just one image per slot, but there is also special cases: |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 220 | // - After freeBuffer, we must still keep the current image/buffer |
| 221 | // Reference counting EGLImages lets us handle all these cases easily while |
| 222 | // also only creating new EGLImages from buffers when required. |
| 223 | class EglImage : public LightRefBase<EglImage> { |
| 224 | public: |
| 225 | EglImage(sp<GraphicBuffer> graphicBuffer); |
| 226 | |
| 227 | // createIfNeeded creates an EGLImage if required (we haven't created |
| 228 | // one yet, or the EGLDisplay or crop-rect has changed). |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 229 | status_t createIfNeeded(EGLDisplay display, const Rect& cropRect); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 230 | |
| 231 | // This calls glEGLImageTargetTexture2DOES to bind the image to the |
| 232 | // texture in the specified texture target. |
| 233 | void bindToTextureTarget(uint32_t texTarget); |
| 234 | |
| 235 | const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; } |
| 236 | const native_handle* graphicBufferHandle() { |
| 237 | return mGraphicBuffer == NULL ? NULL : mGraphicBuffer->handle; |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | // Only allow instantiation using ref counting. |
| 242 | friend class LightRefBase<EglImage>; |
| 243 | virtual ~EglImage(); |
| 244 | |
| 245 | // createImage creates a new EGLImage from a GraphicBuffer. |
| 246 | EGLImageKHR createImage(EGLDisplay dpy, const sp<GraphicBuffer>& graphicBuffer, |
| 247 | const Rect& crop); |
| 248 | |
| 249 | // Disallow copying |
| 250 | EglImage(const EglImage& rhs); |
| 251 | void operator=(const EglImage& rhs); |
| 252 | |
| 253 | // mGraphicBuffer is the buffer that was used to create this image. |
| 254 | sp<GraphicBuffer> mGraphicBuffer; |
| 255 | |
| 256 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 257 | EGLImageKHR mEglImage; |
| 258 | |
| 259 | // mEGLDisplay is the EGLDisplay that was used to create mEglImage. |
| 260 | EGLDisplay mEglDisplay; |
| 261 | |
| 262 | // mCropRect is the crop rectangle passed to EGL when mEglImage |
| 263 | // was created. |
| 264 | Rect mCropRect; |
| 265 | }; |
| 266 | |
| 267 | // freeBufferLocked frees up the given buffer slot. If the slot has been |
| 268 | // initialized this will release the reference to the GraphicBuffer in that |
| 269 | // slot and destroy the EGLImage in that slot. Otherwise it has no effect. |
| 270 | // |
| 271 | // This method must be called with mMutex locked. |
| 272 | virtual void freeBufferLocked(int slotIndex); |
| 273 | |
| 274 | // computeCurrentTransformMatrixLocked computes the transform matrix for the |
| 275 | // current texture. It uses mCurrentTransform and the current GraphicBuffer |
| 276 | // to compute this matrix and stores it in mCurrentTransformMatrix. |
| 277 | // mCurrentTextureImage must not be NULL. |
| 278 | void computeCurrentTransformMatrixLocked(); |
| 279 | |
| 280 | // doGLFenceWaitLocked inserts a wait command into the OpenGL ES command |
| 281 | // stream to ensure that it is safe for future OpenGL ES commands to |
| 282 | // access the current texture buffer. |
| 283 | status_t doGLFenceWaitLocked() const; |
| 284 | |
| 285 | // syncForReleaseLocked performs the synchronization needed to release the |
| 286 | // current slot from an OpenGL ES context. If needed it will set the |
| 287 | // current slot's fence to guard against a producer accessing the buffer |
| 288 | // before the outstanding accesses have completed. |
| 289 | status_t syncForReleaseLocked(EGLDisplay dpy); |
| 290 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 291 | // The default consumer usage flags that BufferLayerConsumer always sets on its |
| 292 | // BufferQueue instance; these will be OR:d with any additional flags passed |
| 293 | // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always |
| 294 | // consume buffers as hardware textures. |
| 295 | static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE; |
| 296 | |
| 297 | // mCurrentTextureImage is the EglImage/buffer of the current texture. It's |
| 298 | // possible that this buffer is not associated with any buffer slot, so we |
| 299 | // must track it separately in order to support the getCurrentBuffer method. |
| 300 | sp<EglImage> mCurrentTextureImage; |
| 301 | |
| 302 | // mCurrentCrop is the crop rectangle that applies to the current texture. |
| 303 | // It gets set each time updateTexImage is called. |
| 304 | Rect mCurrentCrop; |
| 305 | |
| 306 | // mCurrentTransform is the transform identifier for the current texture. It |
| 307 | // gets set each time updateTexImage is called. |
| 308 | uint32_t mCurrentTransform; |
| 309 | |
| 310 | // mCurrentScalingMode is the scaling mode for the current texture. It gets |
| 311 | // set each time updateTexImage is called. |
| 312 | uint32_t mCurrentScalingMode; |
| 313 | |
| 314 | // mCurrentFence is the fence received from BufferQueue in updateTexImage. |
| 315 | sp<Fence> mCurrentFence; |
| 316 | |
| 317 | // The FenceTime wrapper around mCurrentFence. |
| 318 | std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE}; |
| 319 | |
| 320 | // mCurrentTransformMatrix is the transform matrix for the current texture. |
| 321 | // It gets computed by computeTransformMatrix each time updateTexImage is |
| 322 | // called. |
| 323 | float mCurrentTransformMatrix[16]; |
| 324 | |
| 325 | // mCurrentTimestamp is the timestamp for the current texture. It |
| 326 | // gets set each time updateTexImage is called. |
| 327 | int64_t mCurrentTimestamp; |
| 328 | |
| 329 | // mCurrentDataSpace is the dataspace for the current texture. It |
| 330 | // gets set each time updateTexImage is called. |
| 331 | android_dataspace mCurrentDataSpace; |
| 332 | |
| 333 | // mCurrentFrameNumber is the frame counter for the current texture. |
| 334 | // It gets set each time updateTexImage is called. |
| 335 | uint64_t mCurrentFrameNumber; |
| 336 | |
| 337 | uint32_t mDefaultWidth, mDefaultHeight; |
| 338 | |
| 339 | // mFilteringEnabled indicates whether the transform matrix is computed for |
| 340 | // use with bilinear filtering. It defaults to true and is changed by |
| 341 | // setFilteringEnabled(). |
| 342 | bool mFilteringEnabled; |
| 343 | |
| 344 | // mTexName is the name of the OpenGL texture to which streamed images will |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 345 | // be bound when updateTexImage is called. It is set at construction time. |
| 346 | const uint32_t mTexName; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 347 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 348 | // mTexTarget is the GL texture target with which the GL texture object is |
| 349 | // associated. It is set in the constructor and never changed. It is |
| 350 | // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android |
| 351 | // Browser. In that case it is set to GL_TEXTURE_2D to allow |
| 352 | // glCopyTexSubImage to read from the texture. This is a hack to work |
| 353 | // around a GL driver limitation on the number of FBO attachments, which the |
| 354 | // browser's tile cache exceeds. |
| 355 | const uint32_t mTexTarget; |
| 356 | |
| 357 | // EGLSlot contains the information and object references that |
| 358 | // BufferLayerConsumer maintains about a BufferQueue buffer slot. |
| 359 | struct EglSlot { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 360 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 361 | sp<EglImage> mEglImage; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | // mEglDisplay is the EGLDisplay with which this BufferLayerConsumer is currently |
| 365 | // associated. It is intialized to EGL_NO_DISPLAY and gets set to the |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 366 | // current display when updateTexImage is called for the first time. |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 367 | EGLDisplay mEglDisplay; |
| 368 | |
| 369 | // mEglContext is the OpenGL ES context with which this BufferLayerConsumer is |
| 370 | // currently associated. It is initialized to EGL_NO_CONTEXT and gets set |
| 371 | // to the current GL context when updateTexImage is called for the first |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 372 | // time. |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 373 | EGLContext mEglContext; |
| 374 | |
| 375 | // mEGLSlots stores the buffers that have been allocated by the BufferQueue |
| 376 | // for each buffer slot. It is initialized to null pointers, and gets |
| 377 | // filled in with the result of BufferQueue::acquire when the |
| 378 | // client dequeues a buffer from a |
| 379 | // slot that has not yet been used. The buffer allocated to a slot will also |
| 380 | // be replaced if the requested buffer usage or geometry differs from that |
| 381 | // of the buffer allocated to a slot. |
| 382 | EglSlot mEglSlots[BufferQueueDefs::NUM_BUFFER_SLOTS]; |
| 383 | |
| 384 | // mCurrentTexture is the buffer slot index of the buffer that is currently |
| 385 | // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, |
| 386 | // indicating that no buffer slot is currently bound to the texture. Note, |
| 387 | // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean |
| 388 | // that no buffer is bound to the texture. A call to setBufferCount will |
| 389 | // reset mCurrentTexture to INVALID_BUFFER_SLOT. |
| 390 | int mCurrentTexture; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | // ---------------------------------------------------------------------------- |
| 394 | }; // namespace android |
| 395 | |
| 396 | #endif // ANDROID_BUFFERLAYERCONSUMER_H |