CpuConsumer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2012 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_CPUCONSUMER_H
  17. #define ANDROID_GUI_CPUCONSUMER_H
  18. #include <gui/ConsumerBase.h>
  19. #include <ui/GraphicBuffer.h>
  20. #include <utils/String8.h>
  21. #include <utils/Vector.h>
  22. #include <utils/threads.h>
  23. namespace android {
  24. class BufferQueue;
  25. /**
  26. * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
  27. * access to the underlying gralloc buffers provided by BufferQueue. Multiple
  28. * buffers may be acquired by it at once, to be used concurrently by the
  29. * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
  30. * This queue is synchronous by default.
  31. */
  32. class CpuConsumer : public ConsumerBase
  33. {
  34. public:
  35. typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
  36. struct LockedBuffer {
  37. uint8_t *data;
  38. uint32_t width;
  39. uint32_t height;
  40. PixelFormat format;
  41. uint32_t stride;
  42. Rect crop;
  43. uint32_t transform;
  44. uint32_t scalingMode;
  45. int64_t timestamp;
  46. android_dataspace dataSpace;
  47. uint64_t frameNumber;
  48. // this is the same as format, except for formats that are compatible with
  49. // a flexible format (e.g. HAL_PIXEL_FORMAT_YCbCr_420_888). In the latter
  50. // case this contains that flexible format
  51. PixelFormat flexFormat;
  52. // Values below are only valid when using HAL_PIXEL_FORMAT_YCbCr_420_888
  53. // or compatible format, in which case LockedBuffer::data
  54. // contains the Y channel, and stride is the Y channel stride. For other
  55. // formats, these will all be 0.
  56. uint8_t *dataCb;
  57. uint8_t *dataCr;
  58. uint32_t chromaStride;
  59. uint32_t chromaStep;
  60. };
  61. // Create a new CPU consumer. The maxLockedBuffers parameter specifies
  62. // how many buffers can be locked for user access at the same time.
  63. CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
  64. size_t maxLockedBuffers, bool controlledByApp = false);
  65. virtual ~CpuConsumer();
  66. // set the name of the CpuConsumer that will be used to identify it in
  67. // log messages.
  68. void setName(const String8& name);
  69. // Gets the next graphics buffer from the producer and locks it for CPU use,
  70. // filling out the passed-in locked buffer structure with the native pointer
  71. // and metadata. Returns BAD_VALUE if no new buffer is available, and
  72. // NOT_ENOUGH_DATA if the maximum number of buffers is already locked.
  73. //
  74. // Only a fixed number of buffers can be locked at a time, determined by the
  75. // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is
  76. // returned by lockNextBuffer, then old buffers must be returned to the queue
  77. // by calling unlockBuffer before more buffers can be acquired.
  78. status_t lockNextBuffer(LockedBuffer *nativeBuffer);
  79. // Returns a locked buffer to the queue, allowing it to be reused. Since
  80. // only a fixed number of buffers may be locked at a time, old buffers must
  81. // be released by calling unlockBuffer to ensure new buffers can be acquired by
  82. // lockNextBuffer.
  83. status_t unlockBuffer(const LockedBuffer &nativeBuffer);
  84. private:
  85. // Maximum number of buffers that can be locked at a time
  86. size_t mMaxLockedBuffers;
  87. status_t releaseAcquiredBufferLocked(size_t lockedIdx);
  88. virtual void freeBufferLocked(int slotIndex);
  89. // Tracking for buffers acquired by the user
  90. struct AcquiredBuffer {
  91. // Need to track the original mSlot index and the buffer itself because
  92. // the mSlot entry may be freed/reused before the acquired buffer is
  93. // released.
  94. int mSlot;
  95. sp<GraphicBuffer> mGraphicBuffer;
  96. void *mBufferPointer;
  97. AcquiredBuffer() :
  98. mSlot(BufferQueue::INVALID_BUFFER_SLOT),
  99. mBufferPointer(NULL) {
  100. }
  101. };
  102. Vector<AcquiredBuffer> mAcquiredBuffers;
  103. // Count of currently locked buffers
  104. size_t mCurrentLockedBuffers;
  105. };
  106. } // namespace android
  107. #endif // ANDROID_GUI_CPUCONSUMER_H