blob: a6fa69a6d05f0f8d0ab88de0b8b9237c658899d2 [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
39int mapFrameBufferLocked(struct private_module_t* module);
40
41/*****************************************************************************/
42
43struct private_handle_t;
44
45struct private_module_t {
46 gralloc_module_t base;
47
48 private_handle_t* framebuffer;
49 uint32_t flags;
50 uint32_t numBuffers;
51 uint32_t bufferMask;
52 pthread_mutex_t lock;
53 buffer_handle_t currentBuffer;
54 struct fb_var_screeninfo info;
55 struct fb_fix_screeninfo finfo;
56 float xdpi;
57 float ydpi;
58 float fps;
59
60 enum {
61 PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
62 };
63};
64
65/*****************************************************************************/
66
67struct private_handle_t : public native_handle
68{
69 enum {
70 PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
71 PRIV_FLAGS_USES_PMEM = 0x00000002
72 };
73
74 int fd;
75 int magic;
76 int base;
77 int flags;
78 int size;
79
80 static const int sNumInts = 5;
81 static const int sNumFds = 1;
82 static const int sMagic = 0x3141592;
83
84 private_handle_t(int fd, int size, int flags) :
85 fd(fd), magic(sMagic), base(0), flags(flags), size(size) {
86 version = sizeof(native_handle);
87 numInts = sNumInts;
88 numFds = sNumFds;
89 }
90
91 ~private_handle_t() {
92 magic = 0;
93 }
94
95 bool usesPhysicallyContiguousMemory() {
96 return (flags & PRIV_FLAGS_USES_PMEM) != 0;
97 }
98
99 static int validate(const native_handle* h) {
100 if (!h || h->version != sizeof(native_handle) ||
101 h->numInts!=sNumInts || h->numFds!=sNumFds) {
102 return -EINVAL;
103 }
104 const private_handle_t* hnd = (const private_handle_t*)h;
105 if (hnd->magic != sMagic)
106 return -EINVAL;
107 return 0;
108 }
109
110 static private_handle_t* dynamicCast(const native_handle* in) {
111 if (validate(in) == 0) {
112 return (private_handle_t*) in;
113 }
114 return NULL;
115 }
116};
117
118/*****************************************************************************/
119
120template<typename T>
121struct growable_sorted_array_t {
122 int size;
123 int count;
124 T* data;
125
126 growable_sorted_array_t() : size(0), count(0), data(0) {
127 }
128
129 growable_sorted_array_t(int initialSize)
130 : size(initialSize), count(0), data(0)
131 {
132 data = new T[initialSize];
133 }
134
135 ~growable_sorted_array_t() {
136 delete[] data;
137 }
138
139 /** Returns true if we found an exact match.
140 * Argument index is set to the the first index >= key in place.
141 * Index will be in range 0..count inclusive.
142 *
143 */
144 bool find(const T& key, int& index) {
145 return binarySearch(0, count-1, key, index);
146 }
147
148 T* at(int index){
149 if (index >= 0 && index < count) {
150 return data + index;
151 }
152 return 0;
153 }
154
155 void insert(int index, const T& item) {
156 if (index >= 0 && index <= count) {
157 if (count + 1 > size) {
158 int newSize = size * 2;
159 if (newSize < count + 1) {
160 newSize = count + 1;
161 }
162 T* newData = new T[newSize];
163 if (size > 0) {
164 memcpy(newData, data, sizeof(T) * count);
165 }
166 data = newData;
167 size = newSize;
168 }
169 int toMove = count - index;
170 if (toMove > 0) {
171 memmove(data + index + 1, data + index, sizeof(T) * toMove);
172 }
173 count++;
174 data[index] = item;
175 }
176 }
177
178 void remove(int index) {
179 if (index >= 0 && index < count) {
180 int toMove = (count - 1) - index;
181 if (toMove > 0) {
182 memmove(data + index, data + index + 1, sizeof(T) * toMove);
183 }
184 count--;
185 }
186 }
187
188 /** Return the first index >= key. May be in range first..last+1. */
189 int binarySearch(int first, int last, const T& key, int& index)
190 {
191 while (first <= last) {
192 int mid = (first + last) / 2;
193 int cmp = compare(key, data[mid]);
194 if (cmp > 0) {
195 first = mid + 1;
196 } else if (cmp < 0) {
197 last = mid - 1;
198 } else {
199 index = mid;
200 return true;
201 }
202 }
203 index = first;
204 return false;
205 }
206};
207
208
209#endif /* GRALLOC_PRIV_H_ */