MemoryHeapIon.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright Samsung Electronics Co.,LTD.
  3. * Copyright (C) 2011 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*!
  18. * \file MemoryHeapIon.cpp
  19. * \brief source file for MemoryHeapIon
  20. * \author MinGu, Jeon(mingu85.jeon)
  21. * \date 2011/11/20
  22. *
  23. * <b>Revision History: </b>
  24. * - 2011/11/20 : MinGu, Jeon(mingu85.jeon)) \n
  25. * Initial version
  26. * - 2012/11/29 : MinGu, Jeon(mingu85.jeon)) \n
  27. * Change name
  28. */
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <sys/types.h>
  34. #include <cutils/log.h>
  35. #include <binder/MemoryHeapBase.h>
  36. #include <binder/IMemory.h>
  37. #include <binder/MemoryHeapIon.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/mman.h>
  41. #include "ion.h"
  42. #define HEAP_MASK_FILTER ((1 << 16) - (2))
  43. #define FLAG_MASK_FILTER (~(HEAP_MASK_FILTER) - (1))
  44. namespace android {
  45. uint32_t ion_HeapMask_valid_check(uint32_t flags)
  46. {
  47. uint32_t heap_mask, result;
  48. result = 0;
  49. heap_mask = flags & HEAP_MASK_FILTER;
  50. switch(heap_mask) {
  51. case MHB_ION_HEAP_SYSTEM_MASK:
  52. return ION_HEAP_SYSTEM_MASK;
  53. case MHB_ION_HEAP_SYSTEM_CONTIG_MASK:
  54. return ION_HEAP_SYSTEM_CONTIG_MASK;
  55. case MHB_ION_HEAP_EXYNOS_CONTIG_MASK:
  56. return ION_HEAP_EXYNOS_CONTIG_MASK;
  57. case MHB_ION_HEAP_EXYNOS_MASK:
  58. return ION_HEAP_EXYNOS_MASK;
  59. default:
  60. ALOGE("MemoryHeapIon : Heap Mask flag is default (flags:%x)", flags);
  61. return 0;
  62. break;
  63. }
  64. ALOGE("MemoryHeapIon : Heap Mask flag is wrong (flags:%x)", flags);
  65. return 0;
  66. }
  67. uint32_t ion_FlagMask_valid_check(uint32_t flags)
  68. {
  69. uint32_t flag_mask, result;
  70. result = 0;
  71. flag_mask = flags & FLAG_MASK_FILTER;
  72. if (flag_mask & MHB_ION_FLAG_CACHED)
  73. result |= ION_FLAG_CACHED;
  74. if (flag_mask & MHB_ION_FLAG_CACHED_NEEDS_SYNC)
  75. result |= ION_FLAG_CACHED_NEEDS_SYNC;
  76. if (flag_mask & MHB_ION_FLAG_PRESERVE_KMAP)
  77. result |= ION_FLAG_PRESERVE_KMAP;
  78. if (flag_mask & MHB_ION_EXYNOS_VIDEO_MASK)
  79. result |= ION_EXYNOS_VIDEO_MASK;
  80. if (flag_mask & MHB_ION_EXYNOS_MFC_INPUT_MASK)
  81. result |= ION_EXYNOS_MFC_INPUT_MASK;
  82. if (flag_mask & MHB_ION_EXYNOS_MFC_OUTPUT_MASK)
  83. result |= ION_EXYNOS_MFC_OUTPUT_MASK;
  84. if (flag_mask & MHB_ION_EXYNOS_GSC_MASK)
  85. result |= ION_EXYNOS_GSC_MASK;
  86. if (flag_mask & MHB_ION_EXYNOS_FIMD_VIDEO_MASK)
  87. result |= ION_EXYNOS_FIMD_VIDEO_MASK;
  88. return result;
  89. }
  90. MemoryHeapIon::MemoryHeapIon(size_t size, uint32_t flags,
  91. __attribute__((unused))char const *name):MemoryHeapBase()
  92. {
  93. void* base = NULL;
  94. int fd = -1;
  95. uint32_t isReadOnly, heapMask, flagMask;
  96. mIonClient = ion_client_create();
  97. if (mIonClient < 0) {
  98. ALOGE("MemoryHeapIon : ION client creation failed : %s", strerror(errno));
  99. mIonClient = -1;
  100. } else {
  101. isReadOnly = flags & (IMemoryHeap::READ_ONLY);
  102. heapMask = ion_HeapMask_valid_check(flags);
  103. flagMask = ion_FlagMask_valid_check(flags);
  104. if (heapMask) {
  105. ALOGD("MemoryHeapIon : Allocated with size:%zu, heap:0x%X , flag:0x%X", size, heapMask, flagMask);
  106. fd = ion_alloc(mIonClient, size, 0, heapMask, flagMask);
  107. if (fd < 0) {
  108. ALOGE("MemoryHeapIon : ION Reserve memory allocation failed(size[%zu]) : %s", size, strerror(errno));
  109. if (errno == ENOMEM) { // Out of reserve memory. So re-try allocating in system heap
  110. ALOGD("MemoryHeapIon : Re-try Allocating in default heap - SYSTEM heap");
  111. fd = ion_alloc(mIonClient, size, 0, ION_HEAP_SYSTEM_MASK, ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC | ION_FLAG_PRESERVE_KMAP);
  112. }
  113. }
  114. } else {
  115. fd = ion_alloc(mIonClient, size, 0, ION_HEAP_SYSTEM_MASK, ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC | ION_FLAG_PRESERVE_KMAP);
  116. ALOGD("MemoryHeapIon : Allocated with default heap - SYSTEM heap");
  117. }
  118. flags = isReadOnly | heapMask | flagMask;
  119. if (fd < 0) {
  120. ALOGE("MemoryHeapIon : ION memory allocation failed(size[%zu]) : %s", size, strerror(errno));
  121. } else {
  122. flags |= USE_ION_FD;
  123. base = ion_map(fd, size, 0);
  124. if (base != MAP_FAILED) {
  125. init(fd, base, size, flags, NULL);
  126. } else {
  127. ALOGE("MemoryHeapIon : ION mmap failed(size[%zu], fd[%d]) : %s", size, fd, strerror(errno));
  128. ion_free(fd);
  129. }
  130. }
  131. }
  132. }
  133. MemoryHeapIon::MemoryHeapIon(int fd, size_t size, uint32_t flags,
  134. __attribute__((unused))uint32_t offset):MemoryHeapBase()
  135. {
  136. void* base = NULL;
  137. int dup_fd = -1;
  138. mIonClient = ion_client_create();
  139. if (mIonClient < 0) {
  140. ALOGE("MemoryHeapIon : ION client creation failed : %s", strerror(errno));
  141. mIonClient = -1;
  142. } else {
  143. if (fd >= 0) {
  144. dup_fd = dup(fd);
  145. if (dup_fd == -1) {
  146. ALOGE("MemoryHeapIon : cannot dup fd (size[%zu], fd[%d]) : %s", size, fd, strerror(errno));
  147. } else {
  148. flags |= USE_ION_FD;
  149. base = ion_map(dup_fd, size, 0);
  150. if (base != MAP_FAILED) {
  151. init(dup_fd, base, size, flags, NULL);
  152. } else {
  153. ALOGE("MemoryHeapIon : ION mmap failed(size[%zu], fd[%d]): %s", size, fd, strerror(errno));
  154. ion_free(dup_fd);
  155. }
  156. }
  157. } else {
  158. ALOGE("MemoryHeapIon : fd parameter error(fd : %d)", fd);
  159. }
  160. }
  161. }
  162. MemoryHeapIon::~MemoryHeapIon()
  163. {
  164. if (mIonClient != -1) {
  165. ion_unmap(getBase(), getSize());
  166. ion_client_destroy(mIonClient);
  167. mIonClient = -1;
  168. }
  169. }
  170. };