blob: dd630d52514064fbaf9749357f88a4c39107a9d2 [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 }
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700139
140 //Signals the composition thread to unblock and loop over if necessary
141 pthread_mutex_lock(&m->fbPanLock);
142 m->fbPanDone = true;
143 pthread_cond_signal(&m->fbPanCond);
144 pthread_mutex_unlock(&m->fbPanLock);
145
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700146 CALC_FPS();
Naseer Ahmed88318162012-07-13 07:16:20 -0700147 m->currentBuffer = hnd;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700148 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149 return 0;
150}
151
152static int fb_compositionComplete(struct framebuffer_device_t* dev)
153{
154 // TODO: Properly implement composition complete callback
155 glFinish();
156
157 return 0;
158}
159
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700160int mapFrameBufferLocked(struct private_module_t* module)
161{
162 // already initialized...
163 if (module->framebuffer) {
164 return 0;
165 }
166 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700167 "/dev/graphics/fb%u",
168 "/dev/fb%u",
169 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700170
171 int fd = -1;
172 int i=0;
173 char name[64];
174 char property[PROPERTY_VALUE_MAX];
175
176 while ((fd==-1) && device_template[i]) {
177 snprintf(name, 64, device_template[i], 0);
178 fd = open(name, O_RDWR, 0);
179 i++;
180 }
181 if (fd < 0)
182 return -errno;
183
184 struct fb_fix_screeninfo finfo;
185 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
186 return -errno;
187
188 struct fb_var_screeninfo info;
189 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
190 return -errno;
191
192 info.reserved[0] = 0;
193 info.reserved[1] = 0;
194 info.reserved[2] = 0;
195 info.xoffset = 0;
196 info.yoffset = 0;
197 info.activate = FB_ACTIVATE_NOW;
198
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700199 /* Interpretation of offset for color fields: All offsets are from the
200 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
201 * (means: you can use the offset as right argument to <<). A pixel
202 * afterwards is a bit stream and is written to video memory as that
203 * unmodified. This implies big-endian byte order if bits_per_pixel is
204 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700206
207 if(info.bits_per_pixel == 32) {
208 /*
209 * Explicitly request RGBA_8888
210 */
211 info.bits_per_pixel = 32;
212 info.red.offset = 24;
213 info.red.length = 8;
214 info.green.offset = 16;
215 info.green.length = 8;
216 info.blue.offset = 8;
217 info.blue.length = 8;
218 info.transp.offset = 0;
219 info.transp.length = 8;
220
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700221 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
222 * do not use the MDP for composition (i.e. hw composition == 0), ask
223 * for RGBA instead of RGBX. */
224 if (property_get("debug.sf.hw", property, NULL) > 0 &&
225 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 else if(property_get("debug.composition.type", property, NULL) > 0 &&
228 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700229 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
230 else
231 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
232 } else {
233 /*
234 * Explicitly request 5/6/5
235 */
236 info.bits_per_pixel = 16;
237 info.red.offset = 11;
238 info.red.length = 5;
239 info.green.offset = 5;
240 info.green.length = 6;
241 info.blue.offset = 0;
242 info.blue.length = 5;
243 info.transp.offset = 0;
244 info.transp.length = 0;
245 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
246 }
247
248 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700249 int size = roundUpToPageSize(info.yres * info.xres *
250 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251
252 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700253 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700254 */
255 int numberOfBuffers = (int)(finfo.smem_len/size);
256 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
257
258 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
259 int num = atoi(property);
260 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
261 numberOfBuffers = num;
262 }
263 }
264 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
265 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
266
267 ALOGV("We support %d buffers", numberOfBuffers);
268
269 //consider the included hole by 4k alignment
270 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
271 info.yres_virtual = (size * numberOfBuffers) / line_length;
272
273 uint32_t flags = PAGE_FLIP;
274 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
275 info.yres_virtual = size / line_length;
276 flags &= ~PAGE_FLIP;
277 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
278 }
279
280 if (info.yres_virtual < ((size * 2) / line_length) ) {
281 // we need at least 2 for page-flipping
282 info.yres_virtual = size / line_length;
283 flags &= ~PAGE_FLIP;
284 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286 }
287
288 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
289 return -errno;
290
291 if (int(info.width) <= 0 || int(info.height) <= 0) {
292 // the driver doesn't return that information
293 // default to 160 dpi
294 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
295 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
296 }
297
298 float xdpi = (info.xres * 25.4f) / info.width;
299 float ydpi = (info.yres * 25.4f) / info.height;
choongryeol.lee26145b82012-06-26 23:41:00 -0700300 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700301 float fps = info.reserved[3] & 0xFF;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700302
Naseer Ahmed29a26812012-06-14 00:56:20 -0700303 ALOGI("using (fd=%d)\n"
304 "id = %s\n"
305 "xres = %d px\n"
306 "yres = %d px\n"
307 "xres_virtual = %d px\n"
308 "yres_virtual = %d px\n"
309 "bpp = %d\n"
310 "r = %2u:%u\n"
311 "g = %2u:%u\n"
312 "b = %2u:%u\n",
313 fd,
314 finfo.id,
315 info.xres,
316 info.yres,
317 info.xres_virtual,
318 info.yres_virtual,
319 info.bits_per_pixel,
320 info.red.offset, info.red.length,
321 info.green.offset, info.green.length,
322 info.blue.offset, info.blue.length
323 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700324
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 ALOGI("width = %d mm (%f dpi)\n"
326 "height = %d mm (%f dpi)\n"
327 "refresh rate = %.2f Hz\n",
328 info.width, xdpi,
329 info.height, ydpi,
330 fps
331 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700332
333
334 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
335 return -errno;
336
337 if (finfo.smem_len <= 0)
338 return -errno;
339
340 module->flags = flags;
341 module->info = info;
342 module->finfo = finfo;
343 module->xdpi = xdpi;
344 module->ydpi = ydpi;
345 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700347
348 CALC_INIT();
349
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700350 /*
351 * map the framebuffer
352 */
353
354 int err;
355 module->numBuffers = info.yres_virtual / info.yres;
356 module->bufferMask = 0;
357 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
359 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700361 private_handle_t::PRIV_FLAGS_USES_PMEM,
362 BUFFER_TYPE_UI,
363 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700364 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
365 if (vaddr == MAP_FAILED) {
366 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
367 return -errno;
368 }
369 module->framebuffer->base = intptr_t(vaddr);
370 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700372 module->fbPostDone = false;
373 pthread_mutex_init(&(module->fbPostLock), NULL);
374 pthread_cond_init(&(module->fbPostCond), NULL);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700375 module->fbPanDone = false;
376 pthread_mutex_init(&(module->fbPanLock), NULL);
377 pthread_cond_init(&(module->fbPanCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700378 return 0;
379}
380
381static int mapFrameBuffer(struct private_module_t* module)
382{
383 pthread_mutex_lock(&module->lock);
384 int err = mapFrameBufferLocked(module);
385 pthread_mutex_unlock(&module->lock);
386 return err;
387}
388
389/*****************************************************************************/
390
391static int fb_close(struct hw_device_t *dev)
392{
393 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700394 if (ctx) {
395 free(ctx);
396 }
397 return 0;
398}
399
400int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700401 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402{
403 int status = -EINVAL;
404 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
405 alloc_device_t* gralloc_device;
406 status = gralloc_open(module, &gralloc_device);
407 if (status < 0)
408 return status;
409
410 /* initialize our state here */
411 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
412 memset(dev, 0, sizeof(*dev));
413
414 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700415 dev->device.common.tag = HARDWARE_DEVICE_TAG;
416 dev->device.common.version = 0;
417 dev->device.common.module = const_cast<hw_module_t*>(module);
418 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700419 dev->device.setSwapInterval = fb_setSwapInterval;
420 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700421 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700423
424 private_module_t* m = (private_module_t*)module;
425 status = mapFrameBuffer(m);
426 if (status >= 0) {
427 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
428 const_cast<uint32_t&>(dev->device.flags) = 0;
429 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
430 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
431 const_cast<int&>(dev->device.stride) = stride;
432 const_cast<int&>(dev->device.format) = m->fbFormat;
433 const_cast<float&>(dev->device.xdpi) = m->xdpi;
434 const_cast<float&>(dev->device.ydpi) = m->ydpi;
435 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700436 const_cast<int&>(dev->device.minSwapInterval) =
437 PRIV_MIN_SWAP_INTERVAL;
438 const_cast<int&>(dev->device.maxSwapInterval) =
439 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700440 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700441 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700442 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700443 dev->device.setUpdateRect = fb_setUpdateRect;
444 ALOGD("UPDATE_ON_DEMAND supported");
445 }
446
447 *device = &dev->device.common;
448 }
449
450 // Close the gralloc module
451 gralloc_close(gralloc_device);
452 }
453 return status;
454}