blob: 8adb4f464d7b969b1458505f4bd0228506e54d91 [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
37inline size_t roundUpToPageSize(size_t x) {
38 return (x + (PAGESIZE-1)) & ~(PAGESIZE-1);
39}
40
41int mapFrameBufferLocked(struct private_module_t* module);
42
43/*****************************************************************************/
44
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070045class Locker {
46 pthread_mutex_t mutex;
47public:
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 Agopiana8a75162009-04-10 14:24:31 -070062struct private_handle_t;
63
64struct 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 Agopian8c4ab1f2009-06-11 16:32:05 -070073 int pmem_master;
74 void* pmem_master_base;
75
Mathias Agopiana8a75162009-04-10 14:24:31 -070076 struct fb_var_screeninfo info;
77 struct fb_fix_screeninfo finfo;
78 float xdpi;
79 float ydpi;
80 float fps;
81
82 enum {
Mathias Agopian988b8bd2009-05-04 14:26:56 -070083 // flag to indicate we'll post this buffer
Mathias Agopiana8a75162009-04-10 14:24:31 -070084 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
85 };
86};
87
88/*****************************************************************************/
89
90struct private_handle_t : public native_handle
91{
92 enum {
93 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070094 PRIV_FLAGS_USES_PMEM = 0x00000002,
Mathias Agopian51156652009-06-09 18:55:49 -070095 };
96
97 enum {
98 LOCK_STATE_WRITE = 1<<31,
99 LOCK_STATE_MAPPED = 1<<30,
100 LOCK_STATE_READ_MASK = 0x3FFFFFFF
Mathias Agopiana8a75162009-04-10 14:24:31 -0700101 };
102
103 int fd;
104 int magic;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700105 int flags;
106 int size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700107 int offset;
Mathias Agopian485e6982009-05-05 20:21:57 -0700108 // FIXME: the attributes below should be out-of-line
109 int base;
110 int lockState;
111 int writeOwner;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700112 int pid;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700113
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700114 static const int sNumInts = 8;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700115 static const int sNumFds = 1;
116 static const int sMagic = 0x3141592;
117
118 private_handle_t(int fd, int size, int flags) :
Mathias Agopian72c85082009-06-10 16:06:28 -0700119 fd(fd), magic(sMagic), flags(flags), size(size), offset(0),
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700120 base(0), lockState(0), writeOwner(0), pid(getpid())
Mathias Agopian485e6982009-05-05 20:21:57 -0700121 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700122 version = sizeof(native_handle);
123 numInts = sNumInts;
124 numFds = sNumFds;
125 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700126 ~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 Agopiana8a75162009-04-10 14:24:31 -0700153#endif /* GRALLOC_PRIV_H_ */