blob: aa757b1c29ce587be0258fd96fae36b236d1c706 [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;
40
Mathias Agopiana8a75162009-04-10 14:24:31 -070041inline size_t roundUpToPageSize(size_t x) {
Marco Nelissena4b587c2009-07-07 09:29:00 -070042 return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
Mathias Agopiana8a75162009-04-10 14:24:31 -070043}
44
45int mapFrameBufferLocked(struct private_module_t* module);
46
47/*****************************************************************************/
48
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070049class Locker {
50 pthread_mutex_t mutex;
51public:
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 Agopiana8a75162009-04-10 14:24:31 -070066struct private_handle_t;
67
68struct 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 Agopian8c4ab1f2009-06-11 16:32:05 -070077 int pmem_master;
78 void* pmem_master_base;
79
Mathias Agopiana8a75162009-04-10 14:24:31 -070080 struct fb_var_screeninfo info;
81 struct fb_fix_screeninfo finfo;
82 float xdpi;
83 float ydpi;
84 float fps;
85
86 enum {
Mathias Agopian988b8bd2009-05-04 14:26:56 -070087 // flag to indicate we'll post this buffer
Mathias Agopiana8a75162009-04-10 14:24:31 -070088 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
89 };
90};
91
92/*****************************************************************************/
93
94struct private_handle_t : public native_handle
95{
96 enum {
97 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070098 PRIV_FLAGS_USES_PMEM = 0x00000002,
Mathias Agopian51156652009-06-09 18:55:49 -070099 };
100
101 enum {
102 LOCK_STATE_WRITE = 1<<31,
103 LOCK_STATE_MAPPED = 1<<30,
104 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Mathias Agopiana8a75162009-04-10 14:24:31 -0700105 };
106
107 int fd;
108 int magic;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700109 int flags;
110 int size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700111 int offset;
Mathias Agopian485e6982009-05-05 20:21:57 -0700112 // FIXME: the attributes below should be out-of-line
113 int base;
114 int lockState;
115 int writeOwner;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700116 int pid;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700117
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700118 static const int sNumInts = 8;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700119 static const int sNumFds = 1;
120 static const int sMagic = 0x3141592;
121
122 private_handle_t(int fd, int size, int flags) :
Mathias Agopian72c85082009-06-10 16:06:28 -0700123 fd(fd), magic(sMagic), flags(flags), size(size), offset(0),
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700124 base(0), lockState(0), writeOwner(0), pid(getpid())
Mathias Agopian485e6982009-05-05 20:21:57 -0700125 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700126 version = sizeof(native_handle);
127 numInts = sNumInts;
128 numFds = sNumFds;
129 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700130 ~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 Agopiana8a75162009-04-10 14:24:31 -0700157#endif /* GRALLOC_PRIV_H_ */