GraphicBuffer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2007 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_GRAPHIC_BUFFER_H
  17. #define ANDROID_GRAPHIC_BUFFER_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <ui/ANativeObjectBase.h>
  21. #include <ui/PixelFormat.h>
  22. #include <ui/Rect.h>
  23. #include <utils/Flattenable.h>
  24. #include <utils/RefBase.h>
  25. struct ANativeWindowBuffer;
  26. namespace android {
  27. class GraphicBufferMapper;
  28. // ===========================================================================
  29. // GraphicBuffer
  30. // ===========================================================================
  31. class GraphicBuffer
  32. : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
  33. public Flattenable<GraphicBuffer>
  34. {
  35. friend class Flattenable<GraphicBuffer>;
  36. public:
  37. enum {
  38. USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
  39. USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
  40. USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
  41. USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
  42. USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
  43. USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
  44. USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
  45. USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
  46. USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
  47. USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
  48. USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
  49. USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
  50. USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
  51. USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
  52. USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
  53. USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
  54. USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
  55. };
  56. GraphicBuffer();
  57. // creates w * h buffer
  58. GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
  59. uint32_t inUsage);
  60. // create a buffer from an existing handle
  61. GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
  62. uint32_t inUsage, uint32_t inStride, native_handle_t* inHandle,
  63. bool keepOwnership);
  64. // create a buffer from an existing ANativeWindowBuffer
  65. GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
  66. // return status
  67. status_t initCheck() const;
  68. uint32_t getWidth() const { return static_cast<uint32_t>(width); }
  69. uint32_t getHeight() const { return static_cast<uint32_t>(height); }
  70. uint32_t getStride() const { return static_cast<uint32_t>(stride); }
  71. uint32_t getUsage() const { return static_cast<uint32_t>(usage); }
  72. PixelFormat getPixelFormat() const { return format; }
  73. Rect getBounds() const { return Rect(width, height); }
  74. uint64_t getId() const { return mId; }
  75. uint32_t getGenerationNumber() const { return mGenerationNumber; }
  76. void setGenerationNumber(uint32_t generation) {
  77. mGenerationNumber = generation;
  78. }
  79. status_t reallocate(uint32_t inWidth, uint32_t inHeight,
  80. PixelFormat inFormat, uint32_t inUsage);
  81. bool needsReallocation(uint32_t inWidth, uint32_t inHeight,
  82. PixelFormat inFormat, uint32_t inUsage);
  83. status_t lock(uint32_t inUsage, void** vaddr);
  84. status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr);
  85. // For HAL_PIXEL_FORMAT_YCbCr_420_888
  86. status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr);
  87. status_t lockYCbCr(uint32_t inUsage, const Rect& rect,
  88. android_ycbcr *ycbcr);
  89. status_t unlock();
  90. status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd);
  91. status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr,
  92. int fenceFd);
  93. status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr,
  94. int fenceFd);
  95. status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
  96. android_ycbcr *ycbcr, int fenceFd);
  97. status_t unlockAsync(int *fenceFd);
  98. ANativeWindowBuffer* getNativeBuffer() const;
  99. // for debugging
  100. static void dumpAllocationsToSystemLog();
  101. // Flattenable protocol
  102. size_t getFlattenedSize() const;
  103. size_t getFdCount() const;
  104. status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
  105. status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
  106. private:
  107. ~GraphicBuffer();
  108. enum {
  109. ownNone = 0,
  110. ownHandle = 1,
  111. ownData = 2,
  112. };
  113. inline const GraphicBufferMapper& getBufferMapper() const {
  114. return mBufferMapper;
  115. }
  116. inline GraphicBufferMapper& getBufferMapper() {
  117. return mBufferMapper;
  118. }
  119. uint8_t mOwner;
  120. private:
  121. friend class Surface;
  122. friend class BpSurface;
  123. friend class BnSurface;
  124. friend class LightRefBase<GraphicBuffer>;
  125. GraphicBuffer(const GraphicBuffer& rhs);
  126. GraphicBuffer& operator = (const GraphicBuffer& rhs);
  127. const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
  128. status_t initSize(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
  129. uint32_t inUsage);
  130. void free_handle();
  131. GraphicBufferMapper& mBufferMapper;
  132. ssize_t mInitCheck;
  133. // If we're wrapping another buffer then this reference will make sure it
  134. // doesn't get freed.
  135. sp<ANativeWindowBuffer> mWrappedBuffer;
  136. uint64_t mId;
  137. // Stores the generation number of this buffer. If this number does not
  138. // match the BufferQueue's internal generation number (set through
  139. // IGBP::setGenerationNumber), attempts to attach the buffer will fail.
  140. uint32_t mGenerationNumber;
  141. };
  142. }; // namespace android
  143. #endif // ANDROID_GRAPHIC_BUFFER_H