blob: 3109303fd0df1b108d683fc4d1cbe135d867ec50 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Duy Truong73d36df2013-02-09 20:33:23 -08003 * Copyright (c) 2010-2012 The Linux Foundation. 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"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070042#include <cutils/properties.h>
Naseer Ahmeda87da602012-07-01 23:54:19 -070043#include <profiler.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044
Iliyan Malchev202a77d2012-06-11 14:41:12 -070045#define EVEN_OUT(x) if (x & 0x0001) {x--;}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046/** min of int a, b */
47static inline int min(int a, int b) {
48 return (a<b) ? a : b;
49}
50/** max of int a, b */
51static inline int max(int a, int b) {
52 return (a>b) ? a : b;
53}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055enum {
56 PAGE_FLIP = 0x00000001,
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057};
58
59struct fb_context_t {
60 framebuffer_device_t device;
61};
62
Iliyan Malchev202a77d2012-06-11 14:41:12 -070063
64static int fb_setSwapInterval(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070065 int interval)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070066{
Naseer Ahmed30621ab2012-06-25 15:37:21 -070067 //XXX: Get the value here and implement along with
68 //single vsync in HWC
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 char pval[PROPERTY_VALUE_MAX];
Naseer Ahmed29a26812012-06-14 00:56:20 -070070 property_get("debug.egl.swapinterval", pval, "-1");
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 int property_interval = atoi(pval);
72 if (property_interval >= 0)
73 interval = property_interval;
74
75 fb_context_t* ctx = (fb_context_t*)dev;
76 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070077 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070078 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
79 return -EINVAL;
80
81 m->swapInterval = interval;
82 return 0;
83}
84
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
86{
Naseer Ahmed00fd6a52012-07-03 21:23:12 -070087 private_module_t* m =
88 reinterpret_cast<private_module_t*>(dev->common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -040089 private_handle_t *hnd = static_cast<private_handle_t*>
90 (const_cast<native_handle_t*>(buffer));
91 const size_t offset = hnd->base - m->framebuffer->base;
92 m->info.activate = FB_ACTIVATE_VBL;
93 m->info.yoffset = offset / m->finfo.line_length;
94 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
95 ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s",
Saurabh Shah43333302013-02-06 13:30:27 -080096 __FUNCTION__, strerror(errno));
97 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070098 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -070099 return 0;
100}
101
102static int fb_compositionComplete(struct framebuffer_device_t* dev)
103{
104 // TODO: Properly implement composition complete callback
105 glFinish();
106
107 return 0;
108}
109
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700110int mapFrameBufferLocked(struct private_module_t* module)
111{
112 // already initialized...
113 if (module->framebuffer) {
114 return 0;
115 }
116 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117 "/dev/graphics/fb%u",
118 "/dev/fb%u",
119 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120
121 int fd = -1;
122 int i=0;
123 char name[64];
124 char property[PROPERTY_VALUE_MAX];
125
126 while ((fd==-1) && device_template[i]) {
127 snprintf(name, 64, device_template[i], 0);
128 fd = open(name, O_RDWR, 0);
129 i++;
130 }
131 if (fd < 0)
132 return -errno;
133
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400134 memset(&module->commit, 0, sizeof(struct mdp_display_commit));
135
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700136 struct fb_fix_screeninfo finfo;
137 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
138 return -errno;
139
140 struct fb_var_screeninfo info;
141 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
142 return -errno;
143
144 info.reserved[0] = 0;
145 info.reserved[1] = 0;
146 info.reserved[2] = 0;
147 info.xoffset = 0;
148 info.yoffset = 0;
149 info.activate = FB_ACTIVATE_NOW;
150
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700151 /* Interpretation of offset for color fields: All offsets are from the
152 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
153 * (means: you can use the offset as right argument to <<). A pixel
154 * afterwards is a bit stream and is written to video memory as that
155 * unmodified. This implies big-endian byte order if bits_per_pixel is
156 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700157 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158
159 if(info.bits_per_pixel == 32) {
160 /*
161 * Explicitly request RGBA_8888
162 */
163 info.bits_per_pixel = 32;
164 info.red.offset = 24;
165 info.red.length = 8;
166 info.green.offset = 16;
167 info.green.length = 8;
168 info.blue.offset = 8;
169 info.blue.length = 8;
170 info.transp.offset = 0;
171 info.transp.length = 8;
172
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700173 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
174 * do not use the MDP for composition (i.e. hw composition == 0), ask
175 * for RGBA instead of RGBX. */
176 if (property_get("debug.sf.hw", property, NULL) > 0 &&
177 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700178 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700179 else if(property_get("debug.composition.type", property, NULL) > 0 &&
180 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
182 else
183 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
184 } else {
185 /*
186 * Explicitly request 5/6/5
187 */
188 info.bits_per_pixel = 16;
189 info.red.offset = 11;
190 info.red.length = 5;
191 info.green.offset = 5;
192 info.green.length = 6;
193 info.blue.offset = 0;
194 info.blue.length = 5;
195 info.transp.offset = 0;
196 info.transp.length = 0;
197 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
198 }
199
200 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700201 int size = roundUpToPageSize(info.yres * info.xres *
202 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203
204 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700206 */
207 int numberOfBuffers = (int)(finfo.smem_len/size);
208 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
209
210 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
211 int num = atoi(property);
212 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
213 numberOfBuffers = num;
214 }
215 }
216 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
217 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
218
219 ALOGV("We support %d buffers", numberOfBuffers);
220
221 //consider the included hole by 4k alignment
222 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
223 info.yres_virtual = (size * numberOfBuffers) / line_length;
224
225 uint32_t flags = PAGE_FLIP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226
227 if (info.yres_virtual < ((size * 2) / line_length) ) {
228 // we need at least 2 for page-flipping
229 info.yres_virtual = size / line_length;
230 flags &= ~PAGE_FLIP;
231 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700232 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700233 }
234
235 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
236 return -errno;
237
238 if (int(info.width) <= 0 || int(info.height) <= 0) {
239 // the driver doesn't return that information
240 // default to 160 dpi
241 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
242 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
243 }
244
245 float xdpi = (info.xres * 25.4f) / info.width;
246 float ydpi = (info.yres * 25.4f) / info.height;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500247#ifdef MSMFB_METADATA_GET
248 struct msmfb_metadata metadata;
249 memset(&metadata, 0 , sizeof(metadata));
250 metadata.op = metadata_op_frame_rate;
251 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
252 ALOGE("Error retrieving panel frame rate");
253 return -errno;
254 }
255 float fps = metadata.data.panel_frame_rate;
256#else
257 //XXX: Remove reserved field usage on all baselines
choongryeol.lee26145b82012-06-26 23:41:00 -0700258 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700259 float fps = info.reserved[3] & 0xFF;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500260#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261 ALOGI("using (fd=%d)\n"
262 "id = %s\n"
263 "xres = %d px\n"
264 "yres = %d px\n"
265 "xres_virtual = %d px\n"
266 "yres_virtual = %d px\n"
267 "bpp = %d\n"
268 "r = %2u:%u\n"
269 "g = %2u:%u\n"
270 "b = %2u:%u\n",
271 fd,
272 finfo.id,
273 info.xres,
274 info.yres,
275 info.xres_virtual,
276 info.yres_virtual,
277 info.bits_per_pixel,
278 info.red.offset, info.red.length,
279 info.green.offset, info.green.length,
280 info.blue.offset, info.blue.length
281 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700282
Naseer Ahmed29a26812012-06-14 00:56:20 -0700283 ALOGI("width = %d mm (%f dpi)\n"
284 "height = %d mm (%f dpi)\n"
285 "refresh rate = %.2f Hz\n",
286 info.width, xdpi,
287 info.height, ydpi,
288 fps
289 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700290
291
292 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
293 return -errno;
294
295 if (finfo.smem_len <= 0)
296 return -errno;
297
298 module->flags = flags;
299 module->info = info;
300 module->finfo = finfo;
301 module->xdpi = xdpi;
302 module->ydpi = ydpi;
303 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305
306 CALC_INIT();
307
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700308 /*
309 * map the framebuffer
310 */
311
312 int err;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400313 module->numBuffers = info.yres_virtual / info.yres;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700314 module->bufferMask = 0;
315 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
317 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700318 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmedd7f84272012-12-13 13:09:53 -0500319 private_handle_t::PRIV_FLAGS_USES_ION,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700320 BUFFER_TYPE_UI,
321 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700322 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
323 if (vaddr == MAP_FAILED) {
324 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
325 return -errno;
326 }
327 module->framebuffer->base = intptr_t(vaddr);
328 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700329 module->currentOffset = 0;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400330 //Enable vsync
331 int enable = 1;
332 ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL,
333 &enable);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334 return 0;
335}
336
337static int mapFrameBuffer(struct private_module_t* module)
338{
Naseer Ahmed473986c2013-07-16 12:47:21 -0400339 int err = -1;
Naseer Ahmed499ab3d2013-07-04 15:24:31 -0400340 char property[PROPERTY_VALUE_MAX];
341 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
342 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
343 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
344 pthread_mutex_lock(&module->lock);
345 err = mapFrameBufferLocked(module);
346 pthread_mutex_unlock(&module->lock);
347 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 return err;
349}
350
351/*****************************************************************************/
352
353static int fb_close(struct hw_device_t *dev)
354{
355 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700356 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700357 //Hack until fbdev is removed. Framework could close this causing hwc a
358 //pain.
359 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 }
361 return 0;
362}
363
364int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700365 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700366{
367 int status = -EINVAL;
368 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
369 alloc_device_t* gralloc_device;
370 status = gralloc_open(module, &gralloc_device);
371 if (status < 0)
372 return status;
373
374 /* initialize our state here */
375 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
376 memset(dev, 0, sizeof(*dev));
377
378 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379 dev->device.common.tag = HARDWARE_DEVICE_TAG;
380 dev->device.common.version = 0;
381 dev->device.common.module = const_cast<hw_module_t*>(module);
382 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700383 dev->device.setSwapInterval = fb_setSwapInterval;
384 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700386 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700387
388 private_module_t* m = (private_module_t*)module;
389 status = mapFrameBuffer(m);
390 if (status >= 0) {
391 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
392 const_cast<uint32_t&>(dev->device.flags) = 0;
393 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
394 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
395 const_cast<int&>(dev->device.stride) = stride;
396 const_cast<int&>(dev->device.format) = m->fbFormat;
397 const_cast<float&>(dev->device.xdpi) = m->xdpi;
398 const_cast<float&>(dev->device.ydpi) = m->ydpi;
399 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700400 const_cast<int&>(dev->device.minSwapInterval) =
401 PRIV_MIN_SWAP_INTERVAL;
402 const_cast<int&>(dev->device.maxSwapInterval) =
403 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700404 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Naseer Ahmed6f8ca312013-01-15 16:55:06 -0500405 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700406
407 *device = &dev->device.common;
408 }
409
410 // Close the gralloc module
411 gralloc_close(gralloc_device);
412 }
413 return status;
414}