blob: bfd3635b62df21fb0c251d63fd80d1f34f109c2a [file] [log] [blame]
Mathias Agopiana8a75162009-04-10 14:24:31 -07001/*
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 McFaddenf8410692009-07-07 11:29:32 -070021#ifdef HAVE_ANDROID_OS // just want PAGE_SIZE define
22# include <asm/page.h>
23#else
24# include <sys/user.h>
25#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -070026#include <limits.h>
27#include <sys/cdefs.h>
28#include <hardware/gralloc.h>
29#include <pthread.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070030#include <errno.h>
31#include <unistd.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070032
33#include <cutils/native_handle.h>
34
Mathias Agopiana8a75162009-04-10 14:24:31 -070035#include <linux/fb.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070036
37/*****************************************************************************/
38
Mathias Agopian689fa732009-06-24 16:54:44 -070039struct private_module_t;
Mathias Agopianbd80b382009-07-07 17:53:43 -070040struct private_handle_t;
Mathias Agopian689fa732009-06-24 16:54:44 -070041
Mathias Agopiana8a75162009-04-10 14:24:31 -070042inline size_t roundUpToPageSize(size_t x) {
Marco Nelissena4b587c2009-07-07 09:29:00 -070043 return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
Mathias Agopiana8a75162009-04-10 14:24:31 -070044}
45
46int mapFrameBufferLocked(struct private_module_t* module);
Mathias Agopianbd80b382009-07-07 17:53:43 -070047int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
Mathias Agopiana8a75162009-04-10 14:24:31 -070048
49/*****************************************************************************/
50
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070051class Locker {
52 pthread_mutex_t mutex;
53public:
54 class Autolock {
55 Locker& locker;
56 public:
57 inline Autolock(Locker& locker) : locker(locker) { locker.lock(); }
58 inline ~Autolock() { locker.unlock(); }
59 };
60 inline Locker() { pthread_mutex_init(&mutex, 0); }
61 inline ~Locker() { pthread_mutex_destroy(&mutex); }
62 inline void lock() { pthread_mutex_lock(&mutex); }
63 inline void unlock() { pthread_mutex_unlock(&mutex); }
64};
65
66/*****************************************************************************/
67
Mathias Agopiana8a75162009-04-10 14:24:31 -070068struct private_handle_t;
69
70struct private_module_t {
71 gralloc_module_t base;
72
73 private_handle_t* framebuffer;
74 uint32_t flags;
75 uint32_t numBuffers;
76 uint32_t bufferMask;
77 pthread_mutex_t lock;
78 buffer_handle_t currentBuffer;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070079 int pmem_master;
80 void* pmem_master_base;
81
Mathias Agopiana8a75162009-04-10 14:24:31 -070082 struct fb_var_screeninfo info;
83 struct fb_fix_screeninfo finfo;
84 float xdpi;
85 float ydpi;
86 float fps;
87
88 enum {
Mathias Agopian988b8bd2009-05-04 14:26:56 -070089 // flag to indicate we'll post this buffer
Mathias Agopiana8a75162009-04-10 14:24:31 -070090 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
91 };
92};
93
94/*****************************************************************************/
95
96struct private_handle_t : public native_handle
97{
98 enum {
99 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700100 PRIV_FLAGS_USES_PMEM = 0x00000002,
Mathias Agopian51156652009-06-09 18:55:49 -0700101 };
102
103 enum {
104 LOCK_STATE_WRITE = 1<<31,
105 LOCK_STATE_MAPPED = 1<<30,
106 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Mathias Agopiana8a75162009-04-10 14:24:31 -0700107 };
108
109 int fd;
110 int magic;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700111 int flags;
112 int size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700113 int offset;
Mathias Agopian485e6982009-05-05 20:21:57 -0700114 // FIXME: the attributes below should be out-of-line
115 int base;
116 int lockState;
117 int writeOwner;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700118 int pid;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700119
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700120 static const int sNumInts = 8;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700121 static const int sNumFds = 1;
122 static const int sMagic = 0x3141592;
123
124 private_handle_t(int fd, int size, int flags) :
Mathias Agopian72c85082009-06-10 16:06:28 -0700125 fd(fd), magic(sMagic), flags(flags), size(size), offset(0),
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700126 base(0), lockState(0), writeOwner(0), pid(getpid())
Mathias Agopian485e6982009-05-05 20:21:57 -0700127 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128 version = sizeof(native_handle);
129 numInts = sNumInts;
130 numFds = sNumFds;
131 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700132 ~private_handle_t() {
133 magic = 0;
134 }
135
136 bool usesPhysicallyContiguousMemory() {
137 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
138 }
139
140 static int validate(const native_handle* h) {
141 if (!h || h->version != sizeof(native_handle) ||
142 h->numInts!=sNumInts || h->numFds!=sNumFds) {
143 return -EINVAL;
144 }
145 const private_handle_t* hnd = (const private_handle_t*)h;
146 if (hnd->magic != sMagic)
147 return -EINVAL;
148 return 0;
149 }
150
151 static private_handle_t* dynamicCast(const native_handle* in) {
152 if (validate(in) == 0) {
153 return (private_handle_t*) in;
154 }
155 return NULL;
156 }
157};
158
Mathias Agopiana8a75162009-04-10 14:24:31 -0700159#endif /* GRALLOC_PRIV_H_ */