GraphicBuffer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #define LOG_TAG "GraphicBuffer"
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Errors.h>
  21. #include <utils/Log.h>
  22. #include <ui/GraphicBuffer.h>
  23. #include <ui/GraphicBufferAllocator.h>
  24. #include <ui/GraphicBufferMapper.h>
  25. #include <ui/PixelFormat.h>
  26. namespace android {
  27. // ===========================================================================
  28. // Buffer and implementation of ANativeWindowBuffer
  29. // ===========================================================================
  30. static uint64_t getUniqueId() {
  31. static volatile int32_t nextId = 0;
  32. uint64_t id = static_cast<uint64_t>(getpid()) << 32;
  33. id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
  34. return id;
  35. }
  36. GraphicBuffer::GraphicBuffer()
  37. : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
  38. mInitCheck(NO_ERROR), mId(getUniqueId())
  39. {
  40. width =
  41. height =
  42. stride =
  43. format =
  44. usage = 0;
  45. handle = NULL;
  46. }
  47. GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
  48. PixelFormat inFormat, uint32_t inUsage)
  49. : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
  50. mInitCheck(NO_ERROR), mId(getUniqueId())
  51. {
  52. width =
  53. height =
  54. stride =
  55. format =
  56. usage = 0;
  57. handle = NULL;
  58. mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage);
  59. }
  60. GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
  61. PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
  62. native_handle_t* inHandle, bool keepOwnership)
  63. : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
  64. mBufferMapper(GraphicBufferMapper::get()),
  65. mInitCheck(NO_ERROR), mId(getUniqueId())
  66. {
  67. width = static_cast<int>(inWidth);
  68. height = static_cast<int>(inHeight);
  69. stride = static_cast<int>(inStride);
  70. format = inFormat;
  71. usage = static_cast<int>(inUsage);
  72. handle = inHandle;
  73. }
  74. GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
  75. : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
  76. mBufferMapper(GraphicBufferMapper::get()),
  77. mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId())
  78. {
  79. width = buffer->width;
  80. height = buffer->height;
  81. stride = buffer->stride;
  82. format = buffer->format;
  83. usage = buffer->usage;
  84. handle = buffer->handle;
  85. }
  86. GraphicBuffer::~GraphicBuffer()
  87. {
  88. if (handle) {
  89. free_handle();
  90. }
  91. }
  92. void GraphicBuffer::free_handle()
  93. {
  94. if (mOwner == ownHandle) {
  95. mBufferMapper.unregisterBuffer(handle);
  96. native_handle_close(handle);
  97. native_handle_delete(const_cast<native_handle*>(handle));
  98. } else if (mOwner == ownData) {
  99. GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
  100. allocator.free(handle);
  101. }
  102. #ifndef EGL_NEEDS_HANDLE
  103. handle = NULL;
  104. #endif
  105. mWrappedBuffer = 0;
  106. }
  107. status_t GraphicBuffer::initCheck() const {
  108. return static_cast<status_t>(mInitCheck);
  109. }
  110. void GraphicBuffer::dumpAllocationsToSystemLog()
  111. {
  112. GraphicBufferAllocator::dumpToSystemLog();
  113. }
  114. ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
  115. {
  116. LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
  117. return static_cast<ANativeWindowBuffer*>(
  118. const_cast<GraphicBuffer*>(this));
  119. }
  120. status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
  121. PixelFormat inFormat, uint32_t inUsage)
  122. {
  123. if (mOwner != ownData)
  124. return INVALID_OPERATION;
  125. if (handle &&
  126. static_cast<int>(inWidth) == width &&
  127. static_cast<int>(inHeight) == height &&
  128. inFormat == format &&
  129. static_cast<int>(inUsage) == usage)
  130. return NO_ERROR;
  131. if (handle) {
  132. GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
  133. allocator.free(handle);
  134. handle = 0;
  135. }
  136. return initSize(inWidth, inHeight, inFormat, inUsage);
  137. }
  138. bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
  139. PixelFormat inFormat, uint32_t inUsage)
  140. {
  141. if (static_cast<int>(inWidth) != width) return true;
  142. if (static_cast<int>(inHeight) != height) return true;
  143. if (inFormat != format) return true;
  144. if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
  145. return false;
  146. }
  147. status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
  148. PixelFormat inFormat, uint32_t inUsage)
  149. {
  150. GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
  151. uint32_t outStride = 0;
  152. status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
  153. &handle, &outStride);
  154. if (err == NO_ERROR) {
  155. width = static_cast<int>(inWidth);
  156. height = static_cast<int>(inHeight);
  157. format = inFormat;
  158. usage = static_cast<int>(inUsage);
  159. stride = static_cast<int>(outStride);
  160. }
  161. return err;
  162. }
  163. status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
  164. {
  165. const Rect lockBounds(width, height);
  166. status_t res = lock(inUsage, lockBounds, vaddr);
  167. return res;
  168. }
  169. status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
  170. {
  171. if (rect.left < 0 || rect.right > width ||
  172. rect.top < 0 || rect.bottom > height) {
  173. ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
  174. rect.left, rect.top, rect.right, rect.bottom,
  175. width, height);
  176. return BAD_VALUE;
  177. }
  178. status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
  179. return res;
  180. }
  181. status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
  182. {
  183. const Rect lockBounds(width, height);
  184. status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
  185. return res;
  186. }
  187. status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
  188. android_ycbcr* ycbcr)
  189. {
  190. if (rect.left < 0 || rect.right > width ||
  191. rect.top < 0 || rect.bottom > height) {
  192. ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
  193. rect.left, rect.top, rect.right, rect.bottom,
  194. width, height);
  195. return BAD_VALUE;
  196. }
  197. status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
  198. return res;
  199. }
  200. status_t GraphicBuffer::unlock()
  201. {
  202. status_t res = getBufferMapper().unlock(handle);
  203. return res;
  204. }
  205. status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
  206. {
  207. const Rect lockBounds(width, height);
  208. status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
  209. return res;
  210. }
  211. status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
  212. void** vaddr, int fenceFd)
  213. {
  214. if (rect.left < 0 || rect.right > width ||
  215. rect.top < 0 || rect.bottom > height) {
  216. ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
  217. rect.left, rect.top, rect.right, rect.bottom,
  218. width, height);
  219. return BAD_VALUE;
  220. }
  221. status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
  222. fenceFd);
  223. return res;
  224. }
  225. status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
  226. int fenceFd)
  227. {
  228. const Rect lockBounds(width, height);
  229. status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
  230. return res;
  231. }
  232. status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
  233. android_ycbcr* ycbcr, int fenceFd)
  234. {
  235. if (rect.left < 0 || rect.right > width ||
  236. rect.top < 0 || rect.bottom > height) {
  237. ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
  238. rect.left, rect.top, rect.right, rect.bottom,
  239. width, height);
  240. return BAD_VALUE;
  241. }
  242. status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
  243. ycbcr, fenceFd);
  244. return res;
  245. }
  246. status_t GraphicBuffer::unlockAsync(int *fenceFd)
  247. {
  248. status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
  249. return res;
  250. }
  251. size_t GraphicBuffer::getFlattenedSize() const {
  252. return static_cast<size_t>(11 + (handle ? handle->numInts : 0)) * sizeof(int);
  253. }
  254. size_t GraphicBuffer::getFdCount() const {
  255. return static_cast<size_t>(handle ? handle->numFds : 0);
  256. }
  257. status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
  258. size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
  259. if (size < sizeNeeded) return NO_MEMORY;
  260. size_t fdCountNeeded = GraphicBuffer::getFdCount();
  261. if (count < fdCountNeeded) return NO_MEMORY;
  262. int32_t* buf = static_cast<int32_t*>(buffer);
  263. buf[0] = 'GBFR';
  264. buf[1] = width;
  265. buf[2] = height;
  266. buf[3] = stride;
  267. buf[4] = format;
  268. buf[5] = usage;
  269. buf[6] = static_cast<int32_t>(mId >> 32);
  270. buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
  271. buf[8] = static_cast<int32_t>(mGenerationNumber);
  272. buf[9] = 0;
  273. buf[10] = 0;
  274. if (handle) {
  275. buf[9] = handle->numFds;
  276. buf[10] = handle->numInts;
  277. memcpy(fds, handle->data,
  278. static_cast<size_t>(handle->numFds) * sizeof(int));
  279. memcpy(&buf[11], handle->data + handle->numFds,
  280. static_cast<size_t>(handle->numInts) * sizeof(int));
  281. }
  282. buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
  283. size -= sizeNeeded;
  284. if (handle) {
  285. fds += handle->numFds;
  286. count -= static_cast<size_t>(handle->numFds);
  287. }
  288. return NO_ERROR;
  289. }
  290. status_t GraphicBuffer::unflatten(
  291. void const*& buffer, size_t& size, int const*& fds, size_t& count) {
  292. if (size < 11 * sizeof(int)) return NO_MEMORY;
  293. int const* buf = static_cast<int const*>(buffer);
  294. if (buf[0] != 'GBFR') return BAD_TYPE;
  295. const size_t numFds = static_cast<size_t>(buf[9]);
  296. const size_t numInts = static_cast<size_t>(buf[10]);
  297. // Limit the maxNumber to be relatively small. The number of fds or ints
  298. // should not come close to this number, and the number itself was simply
  299. // chosen to be high enough to not cause issues and low enough to prevent
  300. // overflow problems.
  301. const size_t maxNumber = 4096;
  302. if (numFds >= maxNumber || numInts >= (maxNumber - 11)) {
  303. width = height = stride = format = usage = 0;
  304. handle = NULL;
  305. ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
  306. numFds, numInts);
  307. return BAD_VALUE;
  308. }
  309. const size_t sizeNeeded = (11 + numInts) * sizeof(int);
  310. if (size < sizeNeeded) return NO_MEMORY;
  311. size_t fdCountNeeded = numFds;
  312. if (count < fdCountNeeded) return NO_MEMORY;
  313. if (handle) {
  314. // free previous handle if any
  315. free_handle();
  316. }
  317. if (numFds || numInts) {
  318. width = buf[1];
  319. height = buf[2];
  320. stride = buf[3];
  321. format = buf[4];
  322. usage = buf[5];
  323. native_handle* h = native_handle_create(
  324. static_cast<int>(numFds), static_cast<int>(numInts));
  325. if (!h) {
  326. width = height = stride = format = usage = 0;
  327. handle = NULL;
  328. ALOGE("unflatten: native_handle_create failed");
  329. return NO_MEMORY;
  330. }
  331. memcpy(h->data, fds, numFds * sizeof(int));
  332. memcpy(h->data + numFds, &buf[11], numInts * sizeof(int));
  333. handle = h;
  334. } else {
  335. width = height = stride = format = usage = 0;
  336. handle = NULL;
  337. }
  338. mId = static_cast<uint64_t>(buf[6]) << 32;
  339. mId |= static_cast<uint32_t>(buf[7]);
  340. mGenerationNumber = static_cast<uint32_t>(buf[8]);
  341. mOwner = ownHandle;
  342. if (handle != 0) {
  343. status_t err = mBufferMapper.registerBuffer(handle);
  344. if (err != NO_ERROR) {
  345. width = height = stride = format = usage = 0;
  346. handle = NULL;
  347. ALOGE("unflatten: registerBuffer failed: %s (%d)",
  348. strerror(-err), err);
  349. return err;
  350. }
  351. }
  352. buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
  353. size -= sizeNeeded;
  354. fds += numFds;
  355. count -= numFds;
  356. return NO_ERROR;
  357. }
  358. // ---------------------------------------------------------------------------
  359. }; // namespace android