blob: 117999dc7bb2d62cade0d3cdc5f47feb68c7eb05 [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
Mathias Agopiana8a75162009-04-10 14:24:31 -070081 };
82
83 int fd;
84 int magic;
Mathias Agopiana8a75162009-04-10 14:24:31 -070085 int flags;
86 int size;
Mathias Agopian485e6982009-05-05 20:21:57 -070087 // FIXME: the attributes below should be out-of-line
88 int base;
89 int lockState;
90 int writeOwner;
Mathias Agopiana8a75162009-04-10 14:24:31 -070091
Mathias Agopian485e6982009-05-05 20:21:57 -070092 static const int sNumInts = 6;
Mathias Agopiana8a75162009-04-10 14:24:31 -070093 static const int sNumFds = 1;
94 static const int sMagic = 0x3141592;
95
96 private_handle_t(int fd, int size, int flags) :
Mathias Agopian485e6982009-05-05 20:21:57 -070097 fd(fd), magic(sMagic), flags(flags), size(size), base(0),
98 lockState(0), writeOwner(0)
99 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700100 version = sizeof(native_handle);
101 numInts = sNumInts;
102 numFds = sNumFds;
103 }
104
105 ~private_handle_t() {
106 magic = 0;
107 }
108
109 bool usesPhysicallyContiguousMemory() {
110 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
111 }
112
113 static int validate(const native_handle* h) {
114 if (!h || h->version != sizeof(native_handle) ||
115 h->numInts!=sNumInts || h->numFds!=sNumFds) {
116 return -EINVAL;
117 }
118 const private_handle_t* hnd = (const private_handle_t*)h;
119 if (hnd->magic != sMagic)
120 return -EINVAL;
121 return 0;
122 }
123
124 static private_handle_t* dynamicCast(const native_handle* in) {
125 if (validate(in) == 0) {
126 return (private_handle_t*) in;
127 }
128 return NULL;
129 }
130};
131
132/*****************************************************************************/
133
134template<typename T>
135struct growable_sorted_array_t {
136 int size;
137 int count;
138 T* data;
139
140 growable_sorted_array_t() : size(0), count(0), data(0) {
141 }
142
143 growable_sorted_array_t(int initialSize)
144 : size(initialSize), count(0), data(0)
145 {
146 data = new T[initialSize];
147 }
148
149 ~growable_sorted_array_t() {
150 delete[] data;
151 }
152
153 /** Returns true if we found an exact match.
154 * Argument index is set to the the first index >= key in place.
155 * Index will be in range 0..count inclusive.
156 *
157 */
158 bool find(const T& key, int& index) {
159 return binarySearch(0, count-1, key, index);
160 }
161
162 T* at(int index){
163 if (index >= 0 && index < count) {
164 return data + index;
165 }
166 return 0;
167 }
168
169 void insert(int index, const T& item) {
170 if (index >= 0 && index <= count) {
171 if (count + 1 > size) {
172 int newSize = size * 2;
173 if (newSize < count + 1) {
174 newSize = count + 1;
175 }
176 T* newData = new T[newSize];
177 if (size > 0) {
178 memcpy(newData, data, sizeof(T) * count);
179 }
180 data = newData;
181 size = newSize;
182 }
183 int toMove = count - index;
184 if (toMove > 0) {
185 memmove(data + index + 1, data + index, sizeof(T) * toMove);
186 }
187 count++;
188 data[index] = item;
189 }
190 }
191
192 void remove(int index) {
193 if (index >= 0 && index < count) {
194 int toMove = (count - 1) - index;
195 if (toMove > 0) {
196 memmove(data + index, data + index + 1, sizeof(T) * toMove);
197 }
198 count--;
199 }
200 }
201
202 /** Return the first index >= key. May be in range first..last+1. */
203 int binarySearch(int first, int last, const T& key, int& index)
204 {
205 while (first <= last) {
206 int mid = (first + last) / 2;
207 int cmp = compare(key, data[mid]);
208 if (cmp > 0) {
209 first = mid + 1;
210 } else if (cmp < 0) {
211 last = mid - 1;
212 } else {
213 index = mid;
214 return true;
215 }
216 }
217 index = first;
218 return false;
219 }
220};
221
222
223#endif /* GRALLOC_PRIV_H_ */