Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 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> |
Andy McFadden | f841069 | 2009-07-07 11:29:32 -0700 | [diff] [blame] | 21 | #ifdef HAVE_ANDROID_OS // just want PAGE_SIZE define |
| 22 | # include <asm/page.h> |
| 23 | #else |
| 24 | # include <sys/user.h> |
| 25 | #endif |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 26 | #include <limits.h> |
| 27 | #include <sys/cdefs.h> |
| 28 | #include <hardware/gralloc.h> |
| 29 | #include <pthread.h> |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <unistd.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 32 | |
| 33 | #include <cutils/native_handle.h> |
| 34 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 35 | #include <linux/fb.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 36 | |
| 37 | /*****************************************************************************/ |
| 38 | |
Mathias Agopian | 689fa73 | 2009-06-24 16:54:44 -0700 | [diff] [blame] | 39 | struct private_module_t; |
| 40 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 41 | inline size_t roundUpToPageSize(size_t x) { |
Marco Nelissen | a4b587c | 2009-07-07 09:29:00 -0700 | [diff] [blame] | 42 | return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int mapFrameBufferLocked(struct private_module_t* module); |
| 46 | |
| 47 | /*****************************************************************************/ |
| 48 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 49 | class Locker { |
| 50 | pthread_mutex_t mutex; |
| 51 | public: |
| 52 | class Autolock { |
| 53 | Locker& locker; |
| 54 | public: |
| 55 | inline Autolock(Locker& locker) : locker(locker) { locker.lock(); } |
| 56 | inline ~Autolock() { locker.unlock(); } |
| 57 | }; |
| 58 | inline Locker() { pthread_mutex_init(&mutex, 0); } |
| 59 | inline ~Locker() { pthread_mutex_destroy(&mutex); } |
| 60 | inline void lock() { pthread_mutex_lock(&mutex); } |
| 61 | inline void unlock() { pthread_mutex_unlock(&mutex); } |
| 62 | }; |
| 63 | |
| 64 | /*****************************************************************************/ |
| 65 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 66 | struct private_handle_t; |
| 67 | |
| 68 | struct private_module_t { |
| 69 | gralloc_module_t base; |
| 70 | |
| 71 | private_handle_t* framebuffer; |
| 72 | uint32_t flags; |
| 73 | uint32_t numBuffers; |
| 74 | uint32_t bufferMask; |
| 75 | pthread_mutex_t lock; |
| 76 | buffer_handle_t currentBuffer; |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 77 | int pmem_master; |
| 78 | void* pmem_master_base; |
| 79 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 80 | struct fb_var_screeninfo info; |
| 81 | struct fb_fix_screeninfo finfo; |
| 82 | float xdpi; |
| 83 | float ydpi; |
| 84 | float fps; |
| 85 | |
| 86 | enum { |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 87 | // flag to indicate we'll post this buffer |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 88 | PRIV_USAGE_LOCKED_FOR_POST = 0x80000000 |
| 89 | }; |
| 90 | }; |
| 91 | |
| 92 | /*****************************************************************************/ |
| 93 | |
| 94 | struct private_handle_t : public native_handle |
| 95 | { |
| 96 | enum { |
| 97 | PRIV_FLAGS_FRAMEBUFFER = 0x00000001, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 98 | PRIV_FLAGS_USES_PMEM = 0x00000002, |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | enum { |
| 102 | LOCK_STATE_WRITE = 1<<31, |
| 103 | LOCK_STATE_MAPPED = 1<<30, |
| 104 | LOCK_STATE_READ_MASK = 0x3FFFFFFF |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | int fd; |
| 108 | int magic; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 109 | int flags; |
| 110 | int size; |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 111 | int offset; |
Mathias Agopian | 485e698 | 2009-05-05 20:21:57 -0700 | [diff] [blame] | 112 | // FIXME: the attributes below should be out-of-line |
| 113 | int base; |
| 114 | int lockState; |
| 115 | int writeOwner; |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 116 | int pid; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 117 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 118 | static const int sNumInts = 8; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 119 | static const int sNumFds = 1; |
| 120 | static const int sMagic = 0x3141592; |
| 121 | |
| 122 | private_handle_t(int fd, int size, int flags) : |
Mathias Agopian | 72c8508 | 2009-06-10 16:06:28 -0700 | [diff] [blame] | 123 | fd(fd), magic(sMagic), flags(flags), size(size), offset(0), |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 124 | base(0), lockState(0), writeOwner(0), pid(getpid()) |
Mathias Agopian | 485e698 | 2009-05-05 20:21:57 -0700 | [diff] [blame] | 125 | { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 126 | version = sizeof(native_handle); |
| 127 | numInts = sNumInts; |
| 128 | numFds = sNumFds; |
| 129 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 130 | ~private_handle_t() { |
| 131 | magic = 0; |
| 132 | } |
| 133 | |
| 134 | bool usesPhysicallyContiguousMemory() { |
| 135 | return (flags & PRIV_FLAGS_USES_PMEM) != 0; |
| 136 | } |
| 137 | |
| 138 | static int validate(const native_handle* h) { |
| 139 | if (!h || h->version != sizeof(native_handle) || |
| 140 | h->numInts!=sNumInts || h->numFds!=sNumFds) { |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | const private_handle_t* hnd = (const private_handle_t*)h; |
| 144 | if (hnd->magic != sMagic) |
| 145 | return -EINVAL; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static private_handle_t* dynamicCast(const native_handle* in) { |
| 150 | if (validate(in) == 0) { |
| 151 | return (private_handle_t*) in; |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | }; |
| 156 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 157 | #endif /* GRALLOC_PRIV_H_ */ |