blob: 4f8401158007fc157f56a350a3e5141803ea4177 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Naseer Ahmed29a26812012-06-14 00:56:20 -07003 * Copyright (c) 2010-2012 Code Aurora Forum. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <sys/mman.h>
19
Iliyan Malchev202a77d2012-06-11 14:41:12 -070020#include <cutils/log.h>
21#include <cutils/properties.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070022#include <dlfcn.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070023
24#include <hardware/hardware.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070025
26#include <fcntl.h>
27#include <errno.h>
28#include <sys/ioctl.h>
29#include <string.h>
30#include <stdlib.h>
31#include <pthread.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032#include <cutils/atomic.h>
33
34#include <linux/fb.h>
35#include <linux/msm_mdp.h>
36
37#include <GLES/gl.h>
38
39#include "gralloc_priv.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070040#include "fb_priv.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041#include "gr.h"
Naseer Ahmed88318162012-07-13 07:16:20 -070042#include <genlock.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043#include <cutils/properties.h>
Naseer Ahmeda87da602012-07-01 23:54:19 -070044#include <profiler.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070045
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046#define EVEN_OUT(x) if (x & 0x0001) {x--;}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070047/** min of int a, b */
48static inline int min(int a, int b) {
49 return (a<b) ? a : b;
50}
51/** max of int a, b */
52static inline int max(int a, int b) {
53 return (a>b) ? a : b;
54}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056enum {
57 PAGE_FLIP = 0x00000001,
Naseer Ahmed29a26812012-06-14 00:56:20 -070058 LOCKED = 0x00000002
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059};
60
61struct fb_context_t {
62 framebuffer_device_t device;
63};
64
Iliyan Malchev202a77d2012-06-11 14:41:12 -070065
66static int fb_setSwapInterval(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070067 int interval)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068{
Naseer Ahmed30621ab2012-06-25 15:37:21 -070069 //XXX: Get the value here and implement along with
70 //single vsync in HWC
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 char pval[PROPERTY_VALUE_MAX];
Naseer Ahmed29a26812012-06-14 00:56:20 -070072 property_get("debug.egl.swapinterval", pval, "-1");
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 int property_interval = atoi(pval);
74 if (property_interval >= 0)
75 interval = property_interval;
76
77 fb_context_t* ctx = (fb_context_t*)dev;
78 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070079 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070080 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
81 return -EINVAL;
82
83 m->swapInterval = interval;
84 return 0;
85}
86
87static int fb_setUpdateRect(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070088 int l, int t, int w, int h)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089{
90 if (((w|h) <= 0) || ((l|t)<0))
91 return -EINVAL;
92 fb_context_t* ctx = (fb_context_t*)dev;
93 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070094 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070095 m->info.reserved[0] = 0x54445055; // "UPDT";
96 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
97 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
98 return 0;
99}
100
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
102{
103 if (private_handle_t::validate(buffer) < 0)
104 return -EINVAL;
105
Naseer Ahmed88318162012-07-13 07:16:20 -0700106 fb_context_t* ctx = (fb_context_t*) dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700107
Naseer Ahmed88318162012-07-13 07:16:20 -0700108 private_handle_t *hnd = static_cast<private_handle_t*>
109 (const_cast<native_handle_t*>(buffer));
110
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700111 private_module_t* m =
112 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700113
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700114
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700115 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700116 genlock_lock_buffer(hnd, GENLOCK_READ_LOCK, GENLOCK_MAX_TIMEOUT);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700117
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700118 if (m->currentBuffer) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700119 genlock_unlock_buffer(m->currentBuffer);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700120 m->currentBuffer = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700121 }
122
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700123 const size_t offset = hnd->base - m->framebuffer->base;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700124 // frame ready to be posted, signal so that hwc can update External
125 // display
126 pthread_mutex_lock(&m->fbPostLock);
127 m->currentOffset = offset;
128 m->fbPostDone = true;
129 pthread_cond_signal(&m->fbPostCond);
130 pthread_mutex_unlock(&m->fbPostLock);
131
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700132 m->info.activate = FB_ACTIVATE_VBL;
133 m->info.yoffset = offset / m->finfo.line_length;
134 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
135 ALOGE("FBIOPUT_VSCREENINFO failed");
Naseer Ahmed88318162012-07-13 07:16:20 -0700136 genlock_unlock_buffer(hnd);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700137 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700138 }
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700139 CALC_FPS();
Naseer Ahmed88318162012-07-13 07:16:20 -0700140 m->currentBuffer = hnd;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700142 return 0;
143}
144
145static int fb_compositionComplete(struct framebuffer_device_t* dev)
146{
147 // TODO: Properly implement composition complete callback
148 glFinish();
149
150 return 0;
151}
152
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700153int mapFrameBufferLocked(struct private_module_t* module)
154{
155 // already initialized...
156 if (module->framebuffer) {
157 return 0;
158 }
159 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700160 "/dev/graphics/fb%u",
161 "/dev/fb%u",
162 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163
164 int fd = -1;
165 int i=0;
166 char name[64];
167 char property[PROPERTY_VALUE_MAX];
168
169 while ((fd==-1) && device_template[i]) {
170 snprintf(name, 64, device_template[i], 0);
171 fd = open(name, O_RDWR, 0);
172 i++;
173 }
174 if (fd < 0)
175 return -errno;
176
177 struct fb_fix_screeninfo finfo;
178 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
179 return -errno;
180
181 struct fb_var_screeninfo info;
182 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
183 return -errno;
184
185 info.reserved[0] = 0;
186 info.reserved[1] = 0;
187 info.reserved[2] = 0;
188 info.xoffset = 0;
189 info.yoffset = 0;
190 info.activate = FB_ACTIVATE_NOW;
191
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700192 /* Interpretation of offset for color fields: All offsets are from the
193 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
194 * (means: you can use the offset as right argument to <<). A pixel
195 * afterwards is a bit stream and is written to video memory as that
196 * unmodified. This implies big-endian byte order if bits_per_pixel is
197 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199
200 if(info.bits_per_pixel == 32) {
201 /*
202 * Explicitly request RGBA_8888
203 */
204 info.bits_per_pixel = 32;
205 info.red.offset = 24;
206 info.red.length = 8;
207 info.green.offset = 16;
208 info.green.length = 8;
209 info.blue.offset = 8;
210 info.blue.length = 8;
211 info.transp.offset = 0;
212 info.transp.length = 8;
213
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700214 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
215 * do not use the MDP for composition (i.e. hw composition == 0), ask
216 * for RGBA instead of RGBX. */
217 if (property_get("debug.sf.hw", property, NULL) > 0 &&
218 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220 else if(property_get("debug.composition.type", property, NULL) > 0 &&
221 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700222 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
223 else
224 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
225 } else {
226 /*
227 * Explicitly request 5/6/5
228 */
229 info.bits_per_pixel = 16;
230 info.red.offset = 11;
231 info.red.length = 5;
232 info.green.offset = 5;
233 info.green.length = 6;
234 info.blue.offset = 0;
235 info.blue.length = 5;
236 info.transp.offset = 0;
237 info.transp.length = 0;
238 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
239 }
240
241 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700242 int size = roundUpToPageSize(info.yres * info.xres *
243 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700244
245 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700246 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247 */
248 int numberOfBuffers = (int)(finfo.smem_len/size);
249 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
250
251 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
252 int num = atoi(property);
253 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
254 numberOfBuffers = num;
255 }
256 }
257 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
258 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
259
260 ALOGV("We support %d buffers", numberOfBuffers);
261
262 //consider the included hole by 4k alignment
263 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
264 info.yres_virtual = (size * numberOfBuffers) / line_length;
265
266 uint32_t flags = PAGE_FLIP;
267 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
268 info.yres_virtual = size / line_length;
269 flags &= ~PAGE_FLIP;
270 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
271 }
272
273 if (info.yres_virtual < ((size * 2) / line_length) ) {
274 // we need at least 2 for page-flipping
275 info.yres_virtual = size / line_length;
276 flags &= ~PAGE_FLIP;
277 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700278 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700279 }
280
281 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
282 return -errno;
283
284 if (int(info.width) <= 0 || int(info.height) <= 0) {
285 // the driver doesn't return that information
286 // default to 160 dpi
287 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
288 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
289 }
290
291 float xdpi = (info.xres * 25.4f) / info.width;
292 float ydpi = (info.yres * 25.4f) / info.height;
choongryeol.lee26145b82012-06-26 23:41:00 -0700293 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700294 float fps = info.reserved[3] & 0xFF;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700295
Naseer Ahmed29a26812012-06-14 00:56:20 -0700296 ALOGI("using (fd=%d)\n"
297 "id = %s\n"
298 "xres = %d px\n"
299 "yres = %d px\n"
300 "xres_virtual = %d px\n"
301 "yres_virtual = %d px\n"
302 "bpp = %d\n"
303 "r = %2u:%u\n"
304 "g = %2u:%u\n"
305 "b = %2u:%u\n",
306 fd,
307 finfo.id,
308 info.xres,
309 info.yres,
310 info.xres_virtual,
311 info.yres_virtual,
312 info.bits_per_pixel,
313 info.red.offset, info.red.length,
314 info.green.offset, info.green.length,
315 info.blue.offset, info.blue.length
316 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317
Naseer Ahmed29a26812012-06-14 00:56:20 -0700318 ALOGI("width = %d mm (%f dpi)\n"
319 "height = %d mm (%f dpi)\n"
320 "refresh rate = %.2f Hz\n",
321 info.width, xdpi,
322 info.height, ydpi,
323 fps
324 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700325
326
327 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
328 return -errno;
329
330 if (finfo.smem_len <= 0)
331 return -errno;
332
333 module->flags = flags;
334 module->info = info;
335 module->finfo = finfo;
336 module->xdpi = xdpi;
337 module->ydpi = ydpi;
338 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340
341 CALC_INIT();
342
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343 /*
344 * map the framebuffer
345 */
346
347 int err;
348 module->numBuffers = info.yres_virtual / info.yres;
349 module->bufferMask = 0;
350 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700351 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
352 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700353 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700354 private_handle_t::PRIV_FLAGS_USES_PMEM,
355 BUFFER_TYPE_UI,
356 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700357 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
358 if (vaddr == MAP_FAILED) {
359 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
360 return -errno;
361 }
362 module->framebuffer->base = intptr_t(vaddr);
363 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700364 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700365 module->fbPostDone = false;
366 pthread_mutex_init(&(module->fbPostLock), NULL);
367 pthread_cond_init(&(module->fbPostCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700368 return 0;
369}
370
371static int mapFrameBuffer(struct private_module_t* module)
372{
373 pthread_mutex_lock(&module->lock);
374 int err = mapFrameBufferLocked(module);
375 pthread_mutex_unlock(&module->lock);
376 return err;
377}
378
379/*****************************************************************************/
380
381static int fb_close(struct hw_device_t *dev)
382{
383 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700384 if (ctx) {
385 free(ctx);
386 }
387 return 0;
388}
389
390int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700391 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700392{
393 int status = -EINVAL;
394 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
395 alloc_device_t* gralloc_device;
396 status = gralloc_open(module, &gralloc_device);
397 if (status < 0)
398 return status;
399
400 /* initialize our state here */
401 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
402 memset(dev, 0, sizeof(*dev));
403
404 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700405 dev->device.common.tag = HARDWARE_DEVICE_TAG;
406 dev->device.common.version = 0;
407 dev->device.common.module = const_cast<hw_module_t*>(module);
408 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700409 dev->device.setSwapInterval = fb_setSwapInterval;
410 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700411 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700412 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700413
414 private_module_t* m = (private_module_t*)module;
415 status = mapFrameBuffer(m);
416 if (status >= 0) {
417 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
418 const_cast<uint32_t&>(dev->device.flags) = 0;
419 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
420 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
421 const_cast<int&>(dev->device.stride) = stride;
422 const_cast<int&>(dev->device.format) = m->fbFormat;
423 const_cast<float&>(dev->device.xdpi) = m->xdpi;
424 const_cast<float&>(dev->device.ydpi) = m->ydpi;
425 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700426 const_cast<int&>(dev->device.minSwapInterval) =
427 PRIV_MIN_SWAP_INTERVAL;
428 const_cast<int&>(dev->device.maxSwapInterval) =
429 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700430 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700431 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700432 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700433 dev->device.setUpdateRect = fb_setUpdateRect;
434 ALOGD("UPDATE_ON_DEMAND supported");
435 }
436
437 *device = &dev->device.common;
438 }
439
440 // Close the gralloc module
441 gralloc_close(gralloc_device);
442 }
443 return status;
444}