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 ionfd; 47 48 int xres; 49 int yres; 50 int line_length; 51 float xdpi; 52 float ydpi; 53 float fps; 54 void *queue; 55 pthread_mutex_t queue_lock; 56 57 }; 58 59 /*****************************************************************************/ 60 61 #ifdef __cplusplus 62 struct private_handle_t : public native_handle { 63 #else 64 struct private_handle_t { 65 struct native_handle nativeHandle; 66 #endif 67 68 // set if using video encoding colorspace 69 #define GRALLOC_USAGE_PRIVATE_CHROMA (GRALLOC_USAGE_PRIVATE_0) 70 71 enum { 72 PRIV_FLAGS_FRAMEBUFFER = 0x00000001, 73 PRIV_FLAGS_USES_UMP = 0x00000002, 74 PRIV_FLAGS_USES_ION = 0x00000020 75 }; 76 77 // file-descriptors 78 int fd; 79 int fd1; 80 int fd2; 81 // ints 82 int magic; 83 int flags; 84 int size; 85 int offset; 86 87 int format; 88 int width; 89 int height; 90 int stride; 91 int vstride; 92 int gamut; 93 int chroma; 94 95 // FIXME: the attributes below should be out-of-line 96 void *base; 97 void *base1; 98 void *base2; 99 struct ion_handle *handle; 100 struct ion_handle *handle1; 101 struct ion_handle *handle2; 102 103 #ifdef __cplusplus 104 static const int sNumFds = 3; 105 static const int sNumInts = 17; 106 static const int sMagic = 0x3141592; 107 108 private_handle_tprivate_handle_t109 private_handle_t(int fd, int size, int flags, int w, 110 int h, int format, int stride, int vstride) : 111 fd(fd), fd1(-1), fd2(-1), magic(sMagic), flags(flags), size(size), 112 offset(0), format(format), width(w), height(h), stride(stride), 113 vstride(vstride), gamut(0), chroma(0), base(0), handle(0), handle1(0), 114 handle2(0) 115 { 116 version = sizeof(native_handle); 117 numInts = sNumInts + 2; 118 numFds = sNumFds - 2; 119 } 120 private_handle_tprivate_handle_t121 private_handle_t(int fd, int fd1, int size, int flags, int w, 122 int h, int format, int stride, int vstride) : 123 fd(fd), fd1(fd1), fd2(-1), magic(sMagic), flags(flags), size(size), 124 offset(0), format(format), width(w), height(h), stride(stride), 125 vstride(vstride), gamut(0), chroma(0), base(0), base1(0), base2(0), 126 handle(0), handle1(0), handle2(0) 127 { 128 version = sizeof(native_handle); 129 numInts = sNumInts + 1; 130 numFds = sNumFds - 1; 131 } 132 private_handle_tprivate_handle_t133 private_handle_t(int fd, int fd1, int fd2, int size, int flags, int w, 134 int h, int format, int stride, int vstride) : 135 fd(fd), fd1(fd1), fd2(fd2), magic(sMagic), flags(flags), size(size), 136 offset(0), format(format), width(w), height(h), stride(stride), 137 vstride(vstride), gamut(0), chroma(0), base(0), base1(0), base2(0), 138 handle(0), handle1(0), handle2(0) 139 { 140 version = sizeof(native_handle); 141 numInts = sNumInts; 142 numFds = sNumFds; 143 } ~private_handle_tprivate_handle_t144 ~private_handle_t() { 145 magic = 0; 146 } 147 validateprivate_handle_t148 static int validate(const native_handle* h) { 149 const private_handle_t* hnd = (const private_handle_t*)h; 150 if (!h || h->version != sizeof(native_handle) || 151 hnd->numInts + hnd->numFds != sNumInts + sNumFds || 152 hnd->magic != sMagic) 153 { 154 ALOGE("invalid gralloc handle (at %p)", reinterpret_cast<void *>(const_cast<native_handle *>(h))); 155 return -EINVAL; 156 } 157 return 0; 158 } 159 dynamicCastprivate_handle_t160 static private_handle_t* dynamicCast(const native_handle* in) 161 { 162 if (validate(in) == 0) 163 return (private_handle_t*) in; 164 165 return NULL; 166 } 167 168 #endif 169 }; 170 171 #endif /* GRALLOC_PRIV_H_ */ 172