blob: 522de368fd6f385f96e43bca0caee4ee81258cb6 [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{
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700103
Naseer Ahmed88318162012-07-13 07:16:20 -0700104 fb_context_t* ctx = (fb_context_t*) dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700105
Naseer Ahmed88318162012-07-13 07:16:20 -0700106 private_handle_t *hnd = static_cast<private_handle_t*>
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700107 (const_cast<native_handle_t*>(buffer));
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700108 private_module_t* m =
109 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700110
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700111 if (hnd && hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
112 m->info.activate = FB_ACTIVATE_VBL | FB_ACTIVATE_FORCE;
113 m->info.yoffset = hnd->offset / m->finfo.line_length;
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400114 m->commit.var = m->info;
115 if (ioctl(m->framebuffer->fd, MSMFB_DISPLAY_COMMIT, &m->commit) == -1) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700116 ALOGE("%s: FBIOPUT_VSCREENINFO failed for external, err: %s", __FUNCTION__,
117 strerror(errno));
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700118 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700121 return 0;
122}
123
124static int fb_compositionComplete(struct framebuffer_device_t* dev)
125{
126 // TODO: Properly implement composition complete callback
127 glFinish();
128
129 return 0;
130}
131
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132int mapFrameBufferLocked(struct private_module_t* module)
133{
134 // already initialized...
135 if (module->framebuffer) {
136 return 0;
137 }
138 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139 "/dev/graphics/fb%u",
140 "/dev/fb%u",
141 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700142
143 int fd = -1;
144 int i=0;
145 char name[64];
146 char property[PROPERTY_VALUE_MAX];
147
148 while ((fd==-1) && device_template[i]) {
149 snprintf(name, 64, device_template[i], 0);
150 fd = open(name, O_RDWR, 0);
151 i++;
152 }
153 if (fd < 0)
154 return -errno;
155
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400156 memset(&module->fence, 0, sizeof(struct mdp_buf_fence));
157 memset(&module->commit, 0, sizeof(struct mdp_display_commit));
158
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700159 struct fb_fix_screeninfo finfo;
160 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
161 return -errno;
162
163 struct fb_var_screeninfo info;
164 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
165 return -errno;
166
167 info.reserved[0] = 0;
168 info.reserved[1] = 0;
169 info.reserved[2] = 0;
170 info.xoffset = 0;
171 info.yoffset = 0;
172 info.activate = FB_ACTIVATE_NOW;
173
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700174 /* Interpretation of offset for color fields: All offsets are from the
175 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
176 * (means: you can use the offset as right argument to <<). A pixel
177 * afterwards is a bit stream and is written to video memory as that
178 * unmodified. This implies big-endian byte order if bits_per_pixel is
179 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181
182 if(info.bits_per_pixel == 32) {
183 /*
184 * Explicitly request RGBA_8888
185 */
186 info.bits_per_pixel = 32;
187 info.red.offset = 24;
188 info.red.length = 8;
189 info.green.offset = 16;
190 info.green.length = 8;
191 info.blue.offset = 8;
192 info.blue.length = 8;
193 info.transp.offset = 0;
194 info.transp.length = 8;
195
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700196 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
197 * do not use the MDP for composition (i.e. hw composition == 0), ask
198 * for RGBA instead of RGBX. */
199 if (property_get("debug.sf.hw", property, NULL) > 0 &&
200 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700201 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700202 else if(property_get("debug.composition.type", property, NULL) > 0 &&
203 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
205 else
206 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
207 } else {
208 /*
209 * Explicitly request 5/6/5
210 */
211 info.bits_per_pixel = 16;
212 info.red.offset = 11;
213 info.red.length = 5;
214 info.green.offset = 5;
215 info.green.length = 6;
216 info.blue.offset = 0;
217 info.blue.length = 5;
218 info.transp.offset = 0;
219 info.transp.length = 0;
220 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
221 }
222
223 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700224 int size = roundUpToPageSize(info.yres * info.xres *
225 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226
227 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700229 */
230 int numberOfBuffers = (int)(finfo.smem_len/size);
231 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
232
233 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
234 int num = atoi(property);
235 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
236 numberOfBuffers = num;
237 }
238 }
239 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
240 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
241
242 ALOGV("We support %d buffers", numberOfBuffers);
243
244 //consider the included hole by 4k alignment
245 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
246 info.yres_virtual = (size * numberOfBuffers) / line_length;
247
248 uint32_t flags = PAGE_FLIP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700249
250 if (info.yres_virtual < ((size * 2) / line_length) ) {
251 // we need at least 2 for page-flipping
252 info.yres_virtual = size / line_length;
253 flags &= ~PAGE_FLIP;
254 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700255 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700256 }
257
258 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
259 return -errno;
260
261 if (int(info.width) <= 0 || int(info.height) <= 0) {
262 // the driver doesn't return that information
263 // default to 160 dpi
264 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
265 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
266 }
267
268 float xdpi = (info.xres * 25.4f) / info.width;
269 float ydpi = (info.yres * 25.4f) / info.height;
choongryeol.lee26145b82012-06-26 23:41:00 -0700270 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700271 float fps = info.reserved[3] & 0xFF;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700272
Naseer Ahmed29a26812012-06-14 00:56:20 -0700273 ALOGI("using (fd=%d)\n"
274 "id = %s\n"
275 "xres = %d px\n"
276 "yres = %d px\n"
277 "xres_virtual = %d px\n"
278 "yres_virtual = %d px\n"
279 "bpp = %d\n"
280 "r = %2u:%u\n"
281 "g = %2u:%u\n"
282 "b = %2u:%u\n",
283 fd,
284 finfo.id,
285 info.xres,
286 info.yres,
287 info.xres_virtual,
288 info.yres_virtual,
289 info.bits_per_pixel,
290 info.red.offset, info.red.length,
291 info.green.offset, info.green.length,
292 info.blue.offset, info.blue.length
293 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700294
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295 ALOGI("width = %d mm (%f dpi)\n"
296 "height = %d mm (%f dpi)\n"
297 "refresh rate = %.2f Hz\n",
298 info.width, xdpi,
299 info.height, ydpi,
300 fps
301 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700302
303
304 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
305 return -errno;
306
307 if (finfo.smem_len <= 0)
308 return -errno;
309
310 module->flags = flags;
311 module->info = info;
312 module->finfo = finfo;
313 module->xdpi = xdpi;
314 module->ydpi = ydpi;
315 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700316 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317
318 CALC_INIT();
319
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700320 /*
321 * map the framebuffer
322 */
323
324 int err;
325 module->numBuffers = info.yres_virtual / info.yres;
326 module->bufferMask = 0;
327 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700328 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
329 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700330 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700331 private_handle_t::PRIV_FLAGS_USES_PMEM,
332 BUFFER_TYPE_UI,
333 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
335 if (vaddr == MAP_FAILED) {
336 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
337 return -errno;
338 }
339 module->framebuffer->base = intptr_t(vaddr);
340 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700342 module->fbPostDone = false;
343 pthread_mutex_init(&(module->fbPostLock), NULL);
344 pthread_cond_init(&(module->fbPostCond), NULL);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700345 module->fbPanDone = false;
346 pthread_mutex_init(&(module->fbPanLock), NULL);
347 pthread_cond_init(&(module->fbPanCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 return 0;
349}
350
351static int mapFrameBuffer(struct private_module_t* module)
352{
353 pthread_mutex_lock(&module->lock);
354 int err = mapFrameBufferLocked(module);
355 pthread_mutex_unlock(&module->lock);
356 return err;
357}
358
359/*****************************************************************************/
360
361static int fb_close(struct hw_device_t *dev)
362{
363 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700364 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700365 //Hack until fbdev is removed. Framework could close this causing hwc a
366 //pain.
367 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700368 }
369 return 0;
370}
371
372int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374{
375 int status = -EINVAL;
376 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
377 alloc_device_t* gralloc_device;
378 status = gralloc_open(module, &gralloc_device);
379 if (status < 0)
380 return status;
381
382 /* initialize our state here */
383 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
384 memset(dev, 0, sizeof(*dev));
385
386 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700387 dev->device.common.tag = HARDWARE_DEVICE_TAG;
388 dev->device.common.version = 0;
389 dev->device.common.module = const_cast<hw_module_t*>(module);
390 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700391 dev->device.setSwapInterval = fb_setSwapInterval;
392 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700393 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700394 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700395
396 private_module_t* m = (private_module_t*)module;
397 status = mapFrameBuffer(m);
398 if (status >= 0) {
399 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
400 const_cast<uint32_t&>(dev->device.flags) = 0;
401 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
402 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
403 const_cast<int&>(dev->device.stride) = stride;
404 const_cast<int&>(dev->device.format) = m->fbFormat;
405 const_cast<float&>(dev->device.xdpi) = m->xdpi;
406 const_cast<float&>(dev->device.ydpi) = m->ydpi;
407 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700408 const_cast<int&>(dev->device.minSwapInterval) =
409 PRIV_MIN_SWAP_INTERVAL;
410 const_cast<int&>(dev->device.maxSwapInterval) =
411 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700412 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700413 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700414 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415 dev->device.setUpdateRect = fb_setUpdateRect;
416 ALOGD("UPDATE_ON_DEMAND supported");
417 }
418
419 *device = &dev->device.common;
420 }
421
422 // Close the gralloc module
423 gralloc_close(gralloc_device);
424 }
425 return status;
426}