blob: b0f2c6c8f22827f447578b457c731ce066a0a7ab [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>
26
27#include <cutils/native_handle.h>
28
29#if HAVE_ANDROID_OS
30#include <linux/fb.h>
31#endif
32
33/*****************************************************************************/
34
35inline size_t roundUpToPageSize(size_t x) {
36 return (x + (PAGESIZE-1)) & ~(PAGESIZE-1);
37}
38
Mathias Agopian988b8bd2009-05-04 14:26:56 -070039int gralloc_map(gralloc_module_t const* module,
40 buffer_handle_t handle, void** vaddr);
41
42int gralloc_unmap(gralloc_module_t const* module,
43 buffer_handle_t handle);
44
45
Mathias Agopiana8a75162009-04-10 14:24:31 -070046int mapFrameBufferLocked(struct private_module_t* module);
47
48/*****************************************************************************/
49
50struct private_handle_t;
51
52struct private_module_t {
53 gralloc_module_t base;
54
55 private_handle_t* framebuffer;
56 uint32_t flags;
57 uint32_t numBuffers;
58 uint32_t bufferMask;
59 pthread_mutex_t lock;
60 buffer_handle_t currentBuffer;
61 struct fb_var_screeninfo info;
62 struct fb_fix_screeninfo finfo;
63 float xdpi;
64 float ydpi;
65 float fps;
66
67 enum {
Mathias Agopian988b8bd2009-05-04 14:26:56 -070068 // flag to indicate we'll post this buffer
Mathias Agopiana8a75162009-04-10 14:24:31 -070069 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
70 };
71};
72
73/*****************************************************************************/
74
75struct private_handle_t : public native_handle
76{
77 enum {
78 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070079 PRIV_FLAGS_USES_PMEM = 0x00000002,
80 PRIV_FLAGS_MAPPED = 0x00000004, // FIXME: should be out-of-line
81 PRIV_FLAGS_LOCKED = 0x00000008 // FIXME: should be out-of-line
Mathias Agopiana8a75162009-04-10 14:24:31 -070082 };
83
84 int fd;
85 int magic;
Mathias Agopian988b8bd2009-05-04 14:26:56 -070086 int base; // FIXME: should be out-of-line (meaningless with ipc)
Mathias Agopiana8a75162009-04-10 14:24:31 -070087 int flags;
88 int size;
89
Mathias Agopian988b8bd2009-05-04 14:26:56 -070090 static const int sNumInts = 4;
Mathias Agopiana8a75162009-04-10 14:24:31 -070091 static const int sNumFds = 1;
92 static const int sMagic = 0x3141592;
93
94 private_handle_t(int fd, int size, int flags) :
95 fd(fd), magic(sMagic), base(0), flags(flags), size(size) {
96 version = sizeof(native_handle);
97 numInts = sNumInts;
98 numFds = sNumFds;
99 }
100
101 ~private_handle_t() {
102 magic = 0;
103 }
104
105 bool usesPhysicallyContiguousMemory() {
106 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
107 }
108
109 static int validate(const native_handle* h) {
110 if (!h || h->version != sizeof(native_handle) ||
111 h->numInts!=sNumInts || h->numFds!=sNumFds) {
112 return -EINVAL;
113 }
114 const private_handle_t* hnd = (const private_handle_t*)h;
115 if (hnd->magic != sMagic)
116 return -EINVAL;
117 return 0;
118 }
119
120 static private_handle_t* dynamicCast(const native_handle* in) {
121 if (validate(in) == 0) {
122 return (private_handle_t*) in;
123 }
124 return NULL;
125 }
126};
127
128/*****************************************************************************/
129
130template<typename T>
131struct growable_sorted_array_t {
132 int size;
133 int count;
134 T* data;
135
136 growable_sorted_array_t() : size(0), count(0), data(0) {
137 }
138
139 growable_sorted_array_t(int initialSize)
140 : size(initialSize), count(0), data(0)
141 {
142 data = new T[initialSize];
143 }
144
145 ~growable_sorted_array_t() {
146 delete[] data;
147 }
148
149 /** Returns true if we found an exact match.
150 * Argument index is set to the the first index >= key in place.
151 * Index will be in range 0..count inclusive.
152 *
153 */
154 bool find(const T& key, int& index) {
155 return binarySearch(0, count-1, key, index);
156 }
157
158 T* at(int index){
159 if (index >= 0 && index < count) {
160 return data + index;
161 }
162 return 0;
163 }
164
165 void insert(int index, const T& item) {
166 if (index >= 0 && index <= count) {
167 if (count + 1 > size) {
168 int newSize = size * 2;
169 if (newSize < count + 1) {
170 newSize = count + 1;
171 }
172 T* newData = new T[newSize];
173 if (size > 0) {
174 memcpy(newData, data, sizeof(T) * count);
175 }
176 data = newData;
177 size = newSize;
178 }
179 int toMove = count - index;
180 if (toMove > 0) {
181 memmove(data + index + 1, data + index, sizeof(T) * toMove);
182 }
183 count++;
184 data[index] = item;
185 }
186 }
187
188 void remove(int index) {
189 if (index >= 0 && index < count) {
190 int toMove = (count - 1) - index;
191 if (toMove > 0) {
192 memmove(data + index, data + index + 1, sizeof(T) * toMove);
193 }
194 count--;
195 }
196 }
197
198 /** Return the first index >= key. May be in range first..last+1. */
199 int binarySearch(int first, int last, const T& key, int& index)
200 {
201 while (first <= last) {
202 int mid = (first + last) / 2;
203 int cmp = compare(key, data[mid]);
204 if (cmp > 0) {
205 first = mid + 1;
206 } else if (cmp < 0) {
207 last = mid - 1;
208 } else {
209 index = mid;
210 return true;
211 }
212 }
213 index = first;
214 return false;
215 }
216};
217
218
219#endif /* GRALLOC_PRIV_H_ */