BufferItem.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include <gui/BufferItem.h>
  17. #include <ui/Fence.h>
  18. #include <ui/GraphicBuffer.h>
  19. #include <system/window.h>
  20. namespace android {
  21. BufferItem::BufferItem() :
  22. mTransform(0),
  23. mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
  24. mTimestamp(0),
  25. mIsAutoTimestamp(false),
  26. mDataSpace(HAL_DATASPACE_UNKNOWN),
  27. mFrameNumber(0),
  28. mSlot(INVALID_BUFFER_SLOT),
  29. mIsDroppable(false),
  30. mAcquireCalled(false),
  31. mTransformToDisplayInverse(false) {
  32. mCrop.makeInvalid();
  33. }
  34. BufferItem::~BufferItem() {}
  35. template <typename T>
  36. static void addAligned(size_t& size, T /* value */) {
  37. size = FlattenableUtils::align<sizeof(T)>(size);
  38. size += sizeof(T);
  39. }
  40. size_t BufferItem::getPodSize() const {
  41. size_t size = 0;
  42. addAligned(size, mCrop);
  43. addAligned(size, mTransform);
  44. addAligned(size, mScalingMode);
  45. addAligned(size, mTimestampLo);
  46. addAligned(size, mTimestampHi);
  47. addAligned(size, mIsAutoTimestamp);
  48. addAligned(size, mDataSpace);
  49. addAligned(size, mFrameNumberLo);
  50. addAligned(size, mFrameNumberHi);
  51. addAligned(size, mSlot);
  52. addAligned(size, mIsDroppable);
  53. addAligned(size, mAcquireCalled);
  54. addAligned(size, mTransformToDisplayInverse);
  55. return size;
  56. }
  57. size_t BufferItem::getFlattenedSize() const {
  58. size_t size = sizeof(uint32_t); // Flags
  59. if (mGraphicBuffer != 0) {
  60. size += mGraphicBuffer->getFlattenedSize();
  61. FlattenableUtils::align<4>(size);
  62. }
  63. if (mFence != 0) {
  64. size += mFence->getFlattenedSize();
  65. FlattenableUtils::align<4>(size);
  66. }
  67. size += mSurfaceDamage.getFlattenedSize();
  68. size = FlattenableUtils::align<8>(size);
  69. return size + getPodSize();
  70. }
  71. size_t BufferItem::getFdCount() const {
  72. size_t count = 0;
  73. if (mGraphicBuffer != 0) {
  74. count += mGraphicBuffer->getFdCount();
  75. }
  76. if (mFence != 0) {
  77. count += mFence->getFdCount();
  78. }
  79. return count;
  80. }
  81. template <typename T>
  82. static void writeAligned(void*& buffer, size_t& size, T value) {
  83. size -= FlattenableUtils::align<alignof(T)>(buffer);
  84. FlattenableUtils::write(buffer, size, value);
  85. }
  86. status_t BufferItem::flatten(
  87. void*& buffer, size_t& size, int*& fds, size_t& count) const {
  88. // make sure we have enough space
  89. if (size < BufferItem::getFlattenedSize()) {
  90. return NO_MEMORY;
  91. }
  92. // content flags are stored first
  93. uint32_t& flags = *static_cast<uint32_t*>(buffer);
  94. // advance the pointer
  95. FlattenableUtils::advance(buffer, size, sizeof(uint32_t));
  96. flags = 0;
  97. if (mGraphicBuffer != 0) {
  98. status_t err = mGraphicBuffer->flatten(buffer, size, fds, count);
  99. if (err) return err;
  100. size -= FlattenableUtils::align<4>(buffer);
  101. flags |= 1;
  102. }
  103. if (mFence != 0) {
  104. status_t err = mFence->flatten(buffer, size, fds, count);
  105. if (err) return err;
  106. size -= FlattenableUtils::align<4>(buffer);
  107. flags |= 2;
  108. }
  109. status_t err = mSurfaceDamage.flatten(buffer, size);
  110. if (err) return err;
  111. FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
  112. // Check we still have enough space
  113. if (size < getPodSize()) {
  114. return NO_MEMORY;
  115. }
  116. writeAligned(buffer, size, mCrop);
  117. writeAligned(buffer, size, mTransform);
  118. writeAligned(buffer, size, mScalingMode);
  119. writeAligned(buffer, size, mTimestampLo);
  120. writeAligned(buffer, size, mTimestampHi);
  121. writeAligned(buffer, size, mIsAutoTimestamp);
  122. writeAligned(buffer, size, mDataSpace);
  123. writeAligned(buffer, size, mFrameNumberLo);
  124. writeAligned(buffer, size, mFrameNumberHi);
  125. writeAligned(buffer, size, mSlot);
  126. writeAligned(buffer, size, mIsDroppable);
  127. writeAligned(buffer, size, mAcquireCalled);
  128. writeAligned(buffer, size, mTransformToDisplayInverse);
  129. return NO_ERROR;
  130. }
  131. template <typename T>
  132. static void readAligned(const void*& buffer, size_t& size, T& value) {
  133. size -= FlattenableUtils::align<alignof(T)>(buffer);
  134. FlattenableUtils::read(buffer, size, value);
  135. }
  136. status_t BufferItem::unflatten(
  137. void const*& buffer, size_t& size, int const*& fds, size_t& count) {
  138. if (size < sizeof(uint32_t)) {
  139. return NO_MEMORY;
  140. }
  141. uint32_t flags = 0;
  142. FlattenableUtils::read(buffer, size, flags);
  143. if (flags & 1) {
  144. mGraphicBuffer = new GraphicBuffer();
  145. status_t err = mGraphicBuffer->unflatten(buffer, size, fds, count);
  146. if (err) return err;
  147. size -= FlattenableUtils::align<4>(buffer);
  148. }
  149. if (flags & 2) {
  150. mFence = new Fence();
  151. status_t err = mFence->unflatten(buffer, size, fds, count);
  152. if (err) return err;
  153. size -= FlattenableUtils::align<4>(buffer);
  154. }
  155. status_t err = mSurfaceDamage.unflatten(buffer, size);
  156. if (err) return err;
  157. FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
  158. // Check we still have enough space
  159. if (size < getPodSize()) {
  160. return NO_MEMORY;
  161. }
  162. readAligned(buffer, size, mCrop);
  163. readAligned(buffer, size, mTransform);
  164. readAligned(buffer, size, mScalingMode);
  165. readAligned(buffer, size, mTimestampLo);
  166. readAligned(buffer, size, mTimestampHi);
  167. readAligned(buffer, size, mIsAutoTimestamp);
  168. readAligned(buffer, size, mDataSpace);
  169. readAligned(buffer, size, mFrameNumberLo);
  170. readAligned(buffer, size, mFrameNumberHi);
  171. readAligned(buffer, size, mSlot);
  172. readAligned(buffer, size, mIsDroppable);
  173. readAligned(buffer, size, mAcquireCalled);
  174. readAligned(buffer, size, mTransformToDisplayInverse);
  175. return NO_ERROR;
  176. }
  177. const char* BufferItem::scalingModeName(uint32_t scalingMode) {
  178. switch (scalingMode) {
  179. case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
  180. case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
  181. case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
  182. default: return "Unknown";
  183. }
  184. }
  185. } // namespace android