blob: 5293ca9b2ba759a2bc8717db2e8115482a6ac9bb [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
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500111 if (hnd) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700112 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;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500115 m->commit.flags |= MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400116 if (ioctl(m->framebuffer->fd, MSMFB_DISPLAY_COMMIT, &m->commit) == -1) {
Saurabh Shahae823e72012-10-05 00:08:11 -0400117 ALOGE("%s: MSMFB_DISPLAY_COMMIT ioctl failed, err: %s", __FUNCTION__,
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700118 strerror(errno));
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700119 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700121 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 return 0;
123}
124
125static int fb_compositionComplete(struct framebuffer_device_t* dev)
126{
127 // TODO: Properly implement composition complete callback
128 glFinish();
129
130 return 0;
131}
132
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700133int mapFrameBufferLocked(struct private_module_t* module)
134{
135 // already initialized...
136 if (module->framebuffer) {
137 return 0;
138 }
139 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140 "/dev/graphics/fb%u",
141 "/dev/fb%u",
142 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700143
144 int fd = -1;
145 int i=0;
146 char name[64];
147 char property[PROPERTY_VALUE_MAX];
148
149 while ((fd==-1) && device_template[i]) {
150 snprintf(name, 64, device_template[i], 0);
151 fd = open(name, O_RDWR, 0);
152 i++;
153 }
154 if (fd < 0)
155 return -errno;
156
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400157 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;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500270#ifdef MSMFB_METADATA_GET
271 struct msmfb_metadata metadata;
272 memset(&metadata, 0 , sizeof(metadata));
273 metadata.op = metadata_op_frame_rate;
274 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
275 ALOGE("Error retrieving panel frame rate");
276 return -errno;
277 }
278 float fps = metadata.data.panel_frame_rate;
279#else
280 //XXX: Remove reserved field usage on all baselines
choongryeol.lee26145b82012-06-26 23:41:00 -0700281 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700282 float fps = info.reserved[3] & 0xFF;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500283#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700284 ALOGI("using (fd=%d)\n"
285 "id = %s\n"
286 "xres = %d px\n"
287 "yres = %d px\n"
288 "xres_virtual = %d px\n"
289 "yres_virtual = %d px\n"
290 "bpp = %d\n"
291 "r = %2u:%u\n"
292 "g = %2u:%u\n"
293 "b = %2u:%u\n",
294 fd,
295 finfo.id,
296 info.xres,
297 info.yres,
298 info.xres_virtual,
299 info.yres_virtual,
300 info.bits_per_pixel,
301 info.red.offset, info.red.length,
302 info.green.offset, info.green.length,
303 info.blue.offset, info.blue.length
304 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305
Naseer Ahmed29a26812012-06-14 00:56:20 -0700306 ALOGI("width = %d mm (%f dpi)\n"
307 "height = %d mm (%f dpi)\n"
308 "refresh rate = %.2f Hz\n",
309 info.width, xdpi,
310 info.height, ydpi,
311 fps
312 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313
314
315 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
316 return -errno;
317
318 if (finfo.smem_len <= 0)
319 return -errno;
320
321 module->flags = flags;
322 module->info = info;
323 module->finfo = finfo;
324 module->xdpi = xdpi;
325 module->ydpi = ydpi;
326 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700327 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700328
329 CALC_INIT();
330
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331 /*
332 * map the framebuffer
333 */
334
335 int err;
Saurabh Shahd80a52c2012-10-02 14:32:40 -0700336 module->numBuffers = 2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337 module->bufferMask = 0;
338 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700339 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
340 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmedd7f84272012-12-13 13:09:53 -0500342 private_handle_t::PRIV_FLAGS_USES_ION,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700343 BUFFER_TYPE_UI,
344 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
346 if (vaddr == MAP_FAILED) {
347 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
348 return -errno;
349 }
350 module->framebuffer->base = intptr_t(vaddr);
351 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700352 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700353 module->fbPostDone = false;
354 pthread_mutex_init(&(module->fbPostLock), NULL);
355 pthread_cond_init(&(module->fbPostCond), NULL);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700356 module->fbPanDone = false;
357 pthread_mutex_init(&(module->fbPanLock), NULL);
358 pthread_cond_init(&(module->fbPanCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700359 return 0;
360}
361
362static int mapFrameBuffer(struct private_module_t* module)
363{
364 pthread_mutex_lock(&module->lock);
365 int err = mapFrameBufferLocked(module);
366 pthread_mutex_unlock(&module->lock);
367 return err;
368}
369
370/*****************************************************************************/
371
372static int fb_close(struct hw_device_t *dev)
373{
374 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700376 //Hack until fbdev is removed. Framework could close this causing hwc a
377 //pain.
378 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700379 }
380 return 0;
381}
382
383int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700385{
386 int status = -EINVAL;
387 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
388 alloc_device_t* gralloc_device;
389 status = gralloc_open(module, &gralloc_device);
390 if (status < 0)
391 return status;
392
393 /* initialize our state here */
394 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
395 memset(dev, 0, sizeof(*dev));
396
397 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700398 dev->device.common.tag = HARDWARE_DEVICE_TAG;
399 dev->device.common.version = 0;
400 dev->device.common.module = const_cast<hw_module_t*>(module);
401 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402 dev->device.setSwapInterval = fb_setSwapInterval;
403 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700404 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700406
407 private_module_t* m = (private_module_t*)module;
408 status = mapFrameBuffer(m);
409 if (status >= 0) {
410 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
411 const_cast<uint32_t&>(dev->device.flags) = 0;
412 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
413 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
414 const_cast<int&>(dev->device.stride) = stride;
415 const_cast<int&>(dev->device.format) = m->fbFormat;
416 const_cast<float&>(dev->device.xdpi) = m->xdpi;
417 const_cast<float&>(dev->device.ydpi) = m->ydpi;
418 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700419 const_cast<int&>(dev->device.minSwapInterval) =
420 PRIV_MIN_SWAP_INTERVAL;
421 const_cast<int&>(dev->device.maxSwapInterval) =
422 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700423 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700424 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 dev->device.setUpdateRect = fb_setUpdateRect;
427 ALOGD("UPDATE_ON_DEMAND supported");
428 }
429
430 *device = &dev->device.common;
431 }
432
433 // Close the gralloc module
434 gralloc_close(gralloc_device);
435 }
436 return status;
437}