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