/libgralloc/gralloc.cpp

http://github.com/CyanogenMod/android_device_zte_blade · C++ · 205 lines · 143 code · 40 blank · 22 comment · 4 complexity · b46858af53eb7be220dedbe5c3105054 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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 <unistd.h>
  17. #include <fcntl.h>
  18. #include <sys/mman.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #include <linux/android_pmem.h>
  23. #include "allocator.h"
  24. #include "gr.h"
  25. #include "gpu.h"
  26. /*****************************************************************************/
  27. static int gralloc_alloc_buffer(alloc_device_t* dev,
  28. size_t size, int usage, buffer_handle_t* pHandle);
  29. /*****************************************************************************/
  30. int fb_device_open(const hw_module_t* module, const char* name,
  31. hw_device_t** device);
  32. static int gralloc_device_open(const hw_module_t* module, const char* name,
  33. hw_device_t** device);
  34. extern int gralloc_lock(gralloc_module_t const* module,
  35. buffer_handle_t handle, int usage,
  36. int l, int t, int w, int h,
  37. void** vaddr);
  38. extern int gralloc_unlock(gralloc_module_t const* module,
  39. buffer_handle_t handle);
  40. extern int gralloc_register_buffer(gralloc_module_t const* module,
  41. buffer_handle_t handle);
  42. extern int gralloc_unregister_buffer(gralloc_module_t const* module,
  43. buffer_handle_t handle);
  44. extern int gralloc_perform(struct gralloc_module_t const* module,
  45. int operation, ... );
  46. /*****************************************************************************/
  47. /* On-device dependency implementation */
  48. class PmemAllocatorDepsDeviceImpl : public PmemUserspaceAllocator::Deps,
  49. public PmemKernelAllocator::Deps {
  50. virtual size_t getPmemTotalSize(int fd, size_t* size) {
  51. int err = 0;
  52. #ifndef TARGET_MSM7x27
  53. pmem_region region;
  54. err = ioctl(fd, PMEM_GET_TOTAL_SIZE, &region);
  55. if (err == 0) {
  56. *size = region.len;
  57. }
  58. #else
  59. #ifdef USE_ASHMEM
  60. *size = m->info.xres * m->info.yres * 2 * 2;
  61. #else
  62. *size = 23<<20; //23MB for 7x27
  63. #endif
  64. #endif
  65. return err;
  66. }
  67. virtual int connectPmem(int fd, int master_fd) {
  68. return ioctl(fd, PMEM_CONNECT, master_fd);
  69. }
  70. virtual int mapPmem(int fd, int offset, size_t size) {
  71. struct pmem_region sub = { offset, size };
  72. return ioctl(fd, PMEM_MAP, &sub);
  73. }
  74. virtual int unmapPmem(int fd, int offset, size_t size) {
  75. struct pmem_region sub = { offset, size };
  76. return ioctl(fd, PMEM_UNMAP, &sub);
  77. }
  78. virtual int getErrno() {
  79. return errno;
  80. }
  81. virtual void* mmap(void* start, size_t length, int prot, int flags, int fd,
  82. off_t offset) {
  83. return ::mmap(start, length, prot, flags, fd, offset);
  84. }
  85. virtual int munmap(void* start, size_t length) {
  86. return ::munmap(start, length);
  87. }
  88. virtual int open(const char* pathname, int flags, int mode) {
  89. return ::open(pathname, flags, mode);
  90. }
  91. virtual int close(int fd) {
  92. return ::close(fd);
  93. }
  94. };
  95. class GpuContextDepsDeviceImpl : public gpu_context_t::Deps {
  96. public:
  97. virtual int ashmem_create_region(const char *name, size_t size) {
  98. return ::ashmem_create_region(name, size);
  99. }
  100. virtual int mapFrameBufferLocked(struct private_module_t* module) {
  101. return ::mapFrameBufferLocked(module);
  102. }
  103. virtual int terminateBuffer(gralloc_module_t const* module,
  104. private_handle_t* hnd) {
  105. return ::terminateBuffer(module, hnd);
  106. }
  107. virtual int close(int fd) {
  108. return ::close(fd);
  109. }
  110. };
  111. static PmemAllocatorDepsDeviceImpl pmemAllocatorDeviceDepsImpl;
  112. static GpuContextDepsDeviceImpl gpuContextDeviceDepsImpl;
  113. /*****************************************************************************/
  114. static SimpleBestFitAllocator pmemAllocMgr;
  115. static PmemUserspaceAllocator pmemAllocator(pmemAllocatorDeviceDepsImpl, pmemAllocMgr,
  116. "/dev/pmem");
  117. static PmemKernelAllocator pmemAdspAllocator(pmemAllocatorDeviceDepsImpl,
  118. "/dev/pmem_adsp");
  119. /*****************************************************************************/
  120. static struct hw_module_methods_t gralloc_module_methods = {
  121. open: gralloc_device_open
  122. };
  123. struct private_module_t HAL_MODULE_INFO_SYM = {
  124. base: {
  125. common: {
  126. tag: HARDWARE_MODULE_TAG,
  127. version_major: 1,
  128. version_minor: 0,
  129. id: GRALLOC_HARDWARE_MODULE_ID,
  130. name: "Graphics Memory Allocator Module",
  131. author: "The Android Open Source Project",
  132. methods: &gralloc_module_methods
  133. },
  134. registerBuffer: gralloc_register_buffer,
  135. unregisterBuffer: gralloc_unregister_buffer,
  136. lock: gralloc_lock,
  137. unlock: gralloc_unlock,
  138. perform: gralloc_perform,
  139. },
  140. framebuffer: 0,
  141. fbFormat: 0,
  142. flags: 0,
  143. numBuffers: 0,
  144. bufferMask: 0,
  145. lock: PTHREAD_MUTEX_INITIALIZER,
  146. currentBuffer: 0,
  147. };
  148. /*****************************************************************************/
  149. int gralloc_device_open(const hw_module_t* module, const char* name,
  150. hw_device_t** device)
  151. {
  152. int status = -EINVAL;
  153. if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
  154. const private_module_t* m = reinterpret_cast<const private_module_t*>(
  155. module);
  156. gpu_context_t *dev;
  157. dev = new gpu_context_t(gpuContextDeviceDepsImpl, pmemAllocator,
  158. pmemAdspAllocator, m);
  159. *device = &dev->common;
  160. status = 0;
  161. } else {
  162. status = fb_device_open(module, name, device);
  163. }
  164. return status;
  165. }