blob: 7ec07ef8ad70d7e93f1870a94b5921efcf6e45c3 [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>
21#include <asm/page.h>
22#include <limits.h>
23#include <sys/cdefs.h>
24#include <hardware/gralloc.h>
25#include <pthread.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070026#include <errno.h>
27#include <unistd.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070028
29#include <cutils/native_handle.h>
30
31#if HAVE_ANDROID_OS
32#include <linux/fb.h>
33#endif
34
35/*****************************************************************************/
36
Mathias Agopian689fa732009-06-24 16:54:44 -070037struct private_module_t;
38
Mathias Agopiana8a75162009-04-10 14:24:31 -070039inline size_t roundUpToPageSize(size_t x) {
40 return (x + (PAGESIZE-1)) & ~(PAGESIZE-1);
41}
42
43int mapFrameBufferLocked(struct private_module_t* module);
44
45/*****************************************************************************/
46
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070047class Locker {
48 pthread_mutex_t mutex;
49public:
50 class Autolock {
51 Locker& locker;
52 public:
53 inline Autolock(Locker& locker) : locker(locker) { locker.lock(); }
54 inline ~Autolock() { locker.unlock(); }
55 };
56 inline Locker() { pthread_mutex_init(&mutex, 0); }
57 inline ~Locker() { pthread_mutex_destroy(&mutex); }
58 inline void lock() { pthread_mutex_lock(&mutex); }
59 inline void unlock() { pthread_mutex_unlock(&mutex); }
60};
61
62/*****************************************************************************/
63
Mathias Agopiana8a75162009-04-10 14:24:31 -070064struct private_handle_t;
65
66struct private_module_t {
67 gralloc_module_t base;
68
69 private_handle_t* framebuffer;
70 uint32_t flags;
71 uint32_t numBuffers;
72 uint32_t bufferMask;
73 pthread_mutex_t lock;
74 buffer_handle_t currentBuffer;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070075 int pmem_master;
76 void* pmem_master_base;
77
Mathias Agopiana8a75162009-04-10 14:24:31 -070078 struct fb_var_screeninfo info;
79 struct fb_fix_screeninfo finfo;
80 float xdpi;
81 float ydpi;
82 float fps;
83
84 enum {
Mathias Agopian988b8bd2009-05-04 14:26:56 -070085 // flag to indicate we'll post this buffer
Mathias Agopiana8a75162009-04-10 14:24:31 -070086 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
87 };
88};
89
90/*****************************************************************************/
91
92struct private_handle_t : public native_handle
93{
94 enum {
95 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070096 PRIV_FLAGS_USES_PMEM = 0x00000002,
Mathias Agopian51156652009-06-09 18:55:49 -070097 };
98
99 enum {
100 LOCK_STATE_WRITE = 1<<31,
101 LOCK_STATE_MAPPED = 1<<30,
102 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Mathias Agopiana8a75162009-04-10 14:24:31 -0700103 };
104
105 int fd;
106 int magic;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700107 int flags;
108 int size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700109 int offset;
Mathias Agopian485e6982009-05-05 20:21:57 -0700110 // FIXME: the attributes below should be out-of-line
111 int base;
112 int lockState;
113 int writeOwner;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700114 int pid;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700115
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700116 static const int sNumInts = 8;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700117 static const int sNumFds = 1;
118 static const int sMagic = 0x3141592;
119
120 private_handle_t(int fd, int size, int flags) :
Mathias Agopian72c85082009-06-10 16:06:28 -0700121 fd(fd), magic(sMagic), flags(flags), size(size), offset(0),
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700122 base(0), lockState(0), writeOwner(0), pid(getpid())
Mathias Agopian485e6982009-05-05 20:21:57 -0700123 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700124 version = sizeof(native_handle);
125 numInts = sNumInts;
126 numFds = sNumFds;
127 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128 ~private_handle_t() {
129 magic = 0;
130 }
131
132 bool usesPhysicallyContiguousMemory() {
133 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
134 }
135
136 static int validate(const native_handle* h) {
137 if (!h || h->version != sizeof(native_handle) ||
138 h->numInts!=sNumInts || h->numFds!=sNumFds) {
139 return -EINVAL;
140 }
141 const private_handle_t* hnd = (const private_handle_t*)h;
142 if (hnd->magic != sMagic)
143 return -EINVAL;
144 return 0;
145 }
146
147 static private_handle_t* dynamicCast(const native_handle* in) {
148 if (validate(in) == 0) {
149 return (private_handle_t*) in;
150 }
151 return NULL;
152 }
153};
154
Mathias Agopiana8a75162009-04-10 14:24:31 -0700155#endif /* GRALLOC_PRIV_H_ */