blob: a7a58dc43422909814acb1f2ff30a58e090ad3bf [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;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800137 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
138 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700139 return -errno;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800140 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141
142 struct fb_var_screeninfo info;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800143 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
144 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700145 return -errno;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800146 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700147
148 info.reserved[0] = 0;
149 info.reserved[1] = 0;
150 info.reserved[2] = 0;
151 info.xoffset = 0;
152 info.yoffset = 0;
153 info.activate = FB_ACTIVATE_NOW;
154
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700155 /* Interpretation of offset for color fields: All offsets are from the
156 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
157 * (means: you can use the offset as right argument to <<). A pixel
158 * afterwards is a bit stream and is written to video memory as that
159 * unmodified. This implies big-endian byte order if bits_per_pixel is
160 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700161 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162
163 if(info.bits_per_pixel == 32) {
164 /*
165 * Explicitly request RGBA_8888
166 */
167 info.bits_per_pixel = 32;
168 info.red.offset = 24;
169 info.red.length = 8;
170 info.green.offset = 16;
171 info.green.length = 8;
172 info.blue.offset = 8;
173 info.blue.length = 8;
174 info.transp.offset = 0;
175 info.transp.length = 8;
176
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700177 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
178 * do not use the MDP for composition (i.e. hw composition == 0), ask
179 * for RGBA instead of RGBX. */
180 if (property_get("debug.sf.hw", property, NULL) > 0 &&
181 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700182 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183 else if(property_get("debug.composition.type", property, NULL) > 0 &&
184 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700185 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
186 else
187 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
188 } else {
189 /*
190 * Explicitly request 5/6/5
191 */
192 info.bits_per_pixel = 16;
193 info.red.offset = 11;
194 info.red.length = 5;
195 info.green.offset = 5;
196 info.green.length = 6;
197 info.blue.offset = 0;
198 info.blue.length = 5;
199 info.transp.offset = 0;
200 info.transp.length = 0;
201 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
202 }
203
204 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700205 int size = roundUpToPageSize(info.yres * info.xres *
206 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700207
208 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700209 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700210 */
211 int numberOfBuffers = (int)(finfo.smem_len/size);
212 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
213
214 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
215 int num = atoi(property);
216 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
217 numberOfBuffers = num;
218 }
219 }
220 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
221 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
222
223 ALOGV("We support %d buffers", numberOfBuffers);
224
225 //consider the included hole by 4k alignment
226 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
227 info.yres_virtual = (size * numberOfBuffers) / line_length;
228
229 uint32_t flags = PAGE_FLIP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700230
231 if (info.yres_virtual < ((size * 2) / line_length) ) {
232 // we need at least 2 for page-flipping
233 info.yres_virtual = size / line_length;
234 flags &= ~PAGE_FLIP;
235 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700236 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237 }
238
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800239 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
240 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700241 return -errno;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800242 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700243
244 if (int(info.width) <= 0 || int(info.height) <= 0) {
245 // the driver doesn't return that information
246 // default to 160 dpi
247 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
248 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
249 }
250
251 float xdpi = (info.xres * 25.4f) / info.width;
252 float ydpi = (info.yres * 25.4f) / info.height;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500253#ifdef MSMFB_METADATA_GET
254 struct msmfb_metadata metadata;
255 memset(&metadata, 0 , sizeof(metadata));
256 metadata.op = metadata_op_frame_rate;
257 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
258 ALOGE("Error retrieving panel frame rate");
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800259 close(fd);
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500260 return -errno;
261 }
262 float fps = metadata.data.panel_frame_rate;
263#else
264 //XXX: Remove reserved field usage on all baselines
choongryeol.lee26145b82012-06-26 23:41:00 -0700265 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700266 float fps = info.reserved[3] & 0xFF;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500267#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700268 ALOGI("using (fd=%d)\n"
269 "id = %s\n"
270 "xres = %d px\n"
271 "yres = %d px\n"
272 "xres_virtual = %d px\n"
273 "yres_virtual = %d px\n"
274 "bpp = %d\n"
275 "r = %2u:%u\n"
276 "g = %2u:%u\n"
277 "b = %2u:%u\n",
278 fd,
279 finfo.id,
280 info.xres,
281 info.yres,
282 info.xres_virtual,
283 info.yres_virtual,
284 info.bits_per_pixel,
285 info.red.offset, info.red.length,
286 info.green.offset, info.green.length,
287 info.blue.offset, info.blue.length
288 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 ALOGI("width = %d mm (%f dpi)\n"
291 "height = %d mm (%f dpi)\n"
292 "refresh rate = %.2f Hz\n",
293 info.width, xdpi,
294 info.height, ydpi,
295 fps
296 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700297
298
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800299 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
300 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301 return -errno;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800302 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700303
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800304 if (finfo.smem_len <= 0) {
305 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700306 return -errno;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800307 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700308
309 module->flags = flags;
310 module->info = info;
311 module->finfo = finfo;
312 module->xdpi = xdpi;
313 module->ydpi = ydpi;
314 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700315 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700316
317 CALC_INIT();
318
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700319 /*
320 * map the framebuffer
321 */
322
323 int err;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400324 module->numBuffers = info.yres_virtual / info.yres;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700325 module->bufferMask = 0;
326 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
328 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700329 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmedd7f84272012-12-13 13:09:53 -0500330 private_handle_t::PRIV_FLAGS_USES_ION,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700331 BUFFER_TYPE_UI,
332 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700333 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
334 if (vaddr == MAP_FAILED) {
335 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800336 close(fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337 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 Ahmed47aa15e2013-04-10 21:27:09 -0400342 //Enable vsync
343 int enable = 1;
344 ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL,
345 &enable);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346 return 0;
347}
348
349static int mapFrameBuffer(struct private_module_t* module)
350{
Naseer Ahmed473986c2013-07-16 12:47:21 -0400351 int err = -1;
Naseer Ahmed499ab3d2013-07-04 15:24:31 -0400352 char property[PROPERTY_VALUE_MAX];
353 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
354 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
355 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
356 pthread_mutex_lock(&module->lock);
357 err = mapFrameBufferLocked(module);
358 pthread_mutex_unlock(&module->lock);
359 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 return err;
361}
362
363/*****************************************************************************/
364
365static int fb_close(struct hw_device_t *dev)
366{
367 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700368 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700369 //Hack until fbdev is removed. Framework could close this causing hwc a
370 //pain.
371 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700372 }
373 return 0;
374}
375
376int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700378{
379 int status = -EINVAL;
380 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
381 alloc_device_t* gralloc_device;
382 status = gralloc_open(module, &gralloc_device);
383 if (status < 0)
384 return status;
385
386 /* initialize our state here */
387 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800388 if(dev == NULL) {
389 gralloc_close(gralloc_device);
390 return status;
391 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700392 memset(dev, 0, sizeof(*dev));
393
394 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700395 dev->device.common.tag = HARDWARE_DEVICE_TAG;
396 dev->device.common.version = 0;
397 dev->device.common.module = const_cast<hw_module_t*>(module);
398 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700399 dev->device.setSwapInterval = fb_setSwapInterval;
400 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700401 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700403
404 private_module_t* m = (private_module_t*)module;
405 status = mapFrameBuffer(m);
406 if (status >= 0) {
407 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
408 const_cast<uint32_t&>(dev->device.flags) = 0;
409 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
410 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
411 const_cast<int&>(dev->device.stride) = stride;
412 const_cast<int&>(dev->device.format) = m->fbFormat;
413 const_cast<float&>(dev->device.xdpi) = m->xdpi;
414 const_cast<float&>(dev->device.ydpi) = m->ydpi;
415 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700416 const_cast<int&>(dev->device.minSwapInterval) =
417 PRIV_MIN_SWAP_INTERVAL;
418 const_cast<int&>(dev->device.maxSwapInterval) =
419 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700420 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Naseer Ahmed6f8ca312013-01-15 16:55:06 -0500421 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422
423 *device = &dev->device.common;
424 }
425
426 // Close the gralloc module
427 gralloc_close(gralloc_device);
428 }
429 return status;
430}