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 17 #ifndef GRALLOC_PRIV_H_ 18 #define GRALLOC_PRIV_H_ 19 20 #include <stdint.h> 21 #include <limits.h> 22 #include <sys/cdefs.h> 23 #include <hardware/gralloc.h> 24 #include <pthread.h> 25 #include <errno.h> 26 #include <unistd.h> 27 28 #include <cutils/native_handle.h> 29 30 #include <linux/fb.h> 31 32 /*****************************************************************************/ 33 34 struct private_module_t; 35 struct private_handle_t; 36 37 struct private_module_t { 38 gralloc_module_t base; 39 40 struct private_handle_t* framebuffer; 41 uint32_t flags; 42 uint32_t numBuffers; 43 uint32_t bufferMask; 44 pthread_mutex_t lock; 45 buffer_handle_t currentBuffer; 46 int pmem_master; 47 void* pmem_master_base; 48 unsigned long master_phys; 49 int gpu; 50 void* gpu_base; 51 52 struct fb_var_screeninfo info; 53 struct fb_fix_screeninfo finfo; 54 float xdpi; 55 float ydpi; 56 float fps; 57 58 enum { 59 // flag to indicate we'll post this buffer 60 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000 61 }; 62 }; 63 64 /*****************************************************************************/ 65 66 #ifdef __cplusplus 67 struct private_handle_t : public native_handle { 68 #else 69 struct private_handle_t { 70 native_handle_t nativeHandle; 71 #endif 72 73 enum { 74 PRIV_FLAGS_FRAMEBUFFER = 0x00000001, 75 PRIV_FLAGS_USES_PMEM = 0x00000002, 76 PRIV_FLAGS_USES_GPU = 0x00000004, 77 }; 78 79 enum { 80 LOCK_STATE_WRITE = 1<<31, 81 LOCK_STATE_MAPPED = 1<<30, 82 LOCK_STATE_READ_MASK = 0x3FFFFFFF 83 }; 84 85 // file-descriptors 86 int fd; 87 // ints 88 int magic; 89 int flags; 90 int size; 91 int offset; 92 int gpu_fd; // stored as an int, b/c we don't want it marshalled 93 94 // FIXME: the attributes below should be out-of-line 95 int base; 96 int lockState; 97 int writeOwner; 98 int phys; // The physical address of that chunk of memory. If using ashmem, set to 0 They don't care 99 int pid; 100 101 #ifdef __cplusplus 102 static const int sNumInts = 10; 103 static const int sNumFds = 1; 104 static const int sMagic = 'gmsm'; 105 private_handle_tprivate_handle_t106 private_handle_t(int fd, int size, int flags) : 107 fd(fd), magic(sMagic), flags(flags), size(size), offset(0), 108 base(0), lockState(0), writeOwner(0), pid(getpid()) 109 { 110 version = sizeof(native_handle); 111 numInts = sNumInts; 112 numFds = sNumFds; 113 } ~private_handle_tprivate_handle_t114 ~private_handle_t() { 115 magic = 0; 116 } 117 usesPhysicallyContiguousMemoryprivate_handle_t118 bool usesPhysicallyContiguousMemory() { 119 return (flags & (PRIV_FLAGS_USES_PMEM|PRIV_FLAGS_USES_GPU)) != 0; 120 } 121 validateprivate_handle_t122 static int validate(const native_handle* h) { 123 const private_handle_t* hnd = (const private_handle_t*)h; 124 if (!h || h->version != sizeof(native_handle) || 125 h->numInts != sNumInts || h->numFds != sNumFds || 126 hnd->magic != sMagic) 127 { 128 LOGE("invalid gralloc handle (at %p)", h); 129 return -EINVAL; 130 } 131 return 0; 132 } 133 dynamicCastprivate_handle_t134 static private_handle_t* dynamicCast(const native_handle* in) { 135 if (validate(in) == 0) { 136 return (private_handle_t*) in; 137 } 138 return NULL; 139 } 140 #endif 141 }; 142 143 #endif /* GRALLOC_PRIV_H_ */ 144