BufferSlot.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2014 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. #ifndef ANDROID_GUI_BUFFERSLOT_H
  17. #define ANDROID_GUI_BUFFERSLOT_H
  18. #include <ui/Fence.h>
  19. #include <ui/GraphicBuffer.h>
  20. #include <EGL/egl.h>
  21. #include <EGL/eglext.h>
  22. #include <utils/StrongPointer.h>
  23. namespace android {
  24. class Fence;
  25. struct BufferSlot {
  26. BufferSlot()
  27. : mEglDisplay(EGL_NO_DISPLAY),
  28. mBufferState(BufferSlot::FREE),
  29. mRequestBufferCalled(false),
  30. mFrameNumber(0),
  31. mEglFence(EGL_NO_SYNC_KHR),
  32. mAcquireCalled(false),
  33. mNeedsCleanupOnRelease(false),
  34. mAttachedByConsumer(false) {
  35. }
  36. // mGraphicBuffer points to the buffer allocated for this slot or is NULL
  37. // if no buffer has been allocated.
  38. sp<GraphicBuffer> mGraphicBuffer;
  39. // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
  40. EGLDisplay mEglDisplay;
  41. // BufferState represents the different states in which a buffer slot
  42. // can be. All slots are initially FREE.
  43. enum BufferState {
  44. // FREE indicates that the buffer is available to be dequeued
  45. // by the producer. The buffer may be in use by the consumer for
  46. // a finite time, so the buffer must not be modified until the
  47. // associated fence is signaled.
  48. //
  49. // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
  50. // when dequeueBuffer is called.
  51. FREE = 0,
  52. // DEQUEUED indicates that the buffer has been dequeued by the
  53. // producer, but has not yet been queued or canceled. The
  54. // producer may modify the buffer's contents as soon as the
  55. // associated ready fence is signaled.
  56. //
  57. // The slot is "owned" by the producer. It can transition to
  58. // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
  59. DEQUEUED = 1,
  60. // QUEUED indicates that the buffer has been filled by the
  61. // producer and queued for use by the consumer. The buffer
  62. // contents may continue to be modified for a finite time, so
  63. // the contents must not be accessed until the associated fence
  64. // is signaled.
  65. //
  66. // The slot is "owned" by BufferQueue. It can transition to
  67. // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
  68. // queued in asynchronous mode).
  69. QUEUED = 2,
  70. // ACQUIRED indicates that the buffer has been acquired by the
  71. // consumer. As with QUEUED, the contents must not be accessed
  72. // by the consumer until the fence is signaled.
  73. //
  74. // The slot is "owned" by the consumer. It transitions to FREE
  75. // when releaseBuffer is called.
  76. ACQUIRED = 3
  77. };
  78. static const char* bufferStateName(BufferState state);
  79. // mBufferState is the current state of this buffer slot.
  80. BufferState mBufferState;
  81. // mRequestBufferCalled is used for validating that the producer did
  82. // call requestBuffer() when told to do so. Technically this is not
  83. // needed but useful for debugging and catching producer bugs.
  84. bool mRequestBufferCalled;
  85. // mFrameNumber is the number of the queued frame for this slot. This
  86. // is used to dequeue buffers in LRU order (useful because buffers
  87. // may be released before their release fence is signaled).
  88. uint64_t mFrameNumber;
  89. // mEglFence is the EGL sync object that must signal before the buffer
  90. // associated with this buffer slot may be dequeued. It is initialized
  91. // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
  92. // new sync object in releaseBuffer. (This is deprecated in favor of
  93. // mFence, below.)
  94. EGLSyncKHR mEglFence;
  95. // mFence is a fence which will signal when work initiated by the
  96. // previous owner of the buffer is finished. When the buffer is FREE,
  97. // the fence indicates when the consumer has finished reading
  98. // from the buffer, or when the producer has finished writing if it
  99. // called cancelBuffer after queueing some writes. When the buffer is
  100. // QUEUED, it indicates when the producer has finished filling the
  101. // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
  102. // passed to the consumer or producer along with ownership of the
  103. // buffer, and mFence is set to NO_FENCE.
  104. sp<Fence> mFence;
  105. // Indicates whether this buffer has been seen by a consumer yet
  106. bool mAcquireCalled;
  107. // Indicates whether this buffer needs to be cleaned up by the
  108. // consumer. This is set when a buffer in ACQUIRED state is freed.
  109. // It causes releaseBuffer to return STALE_BUFFER_SLOT.
  110. bool mNeedsCleanupOnRelease;
  111. // Indicates whether the buffer was attached on the consumer side.
  112. // If so, it needs to set the BUFFER_NEEDS_REALLOCATION flag when dequeued
  113. // to prevent the producer from using a stale cached buffer.
  114. bool mAttachedByConsumer;
  115. };
  116. } // namespace android
  117. #endif