blob: cc16a5f1f93860f75c0e926b1bc540d8bba98d65 [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,
Naseer Ahmed29a26812012-06-14 00:56:20 -070057 LOCKED = 0x00000002
Iliyan Malchev202a77d2012-06-11 14:41:12 -070058};
59
60struct fb_context_t {
61 framebuffer_device_t device;
62};
63
Iliyan Malchev202a77d2012-06-11 14:41:12 -070064
65static int fb_setSwapInterval(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070066 int interval)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070067{
Naseer Ahmed30621ab2012-06-25 15:37:21 -070068 //XXX: Get the value here and implement along with
69 //single vsync in HWC
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070 char pval[PROPERTY_VALUE_MAX];
Naseer Ahmed29a26812012-06-14 00:56:20 -070071 property_get("debug.egl.swapinterval", pval, "-1");
Iliyan Malchev202a77d2012-06-11 14:41:12 -070072 int property_interval = atoi(pval);
73 if (property_interval >= 0)
74 interval = property_interval;
75
76 fb_context_t* ctx = (fb_context_t*)dev;
77 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070078 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
80 return -EINVAL;
81
82 m->swapInterval = interval;
83 return 0;
84}
85
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
87{
Naseer Ahmed00fd6a52012-07-03 21:23:12 -070088 private_module_t* m =
89 reinterpret_cast<private_module_t*>(dev->common.module);
Saurabh Shah43333302013-02-06 13:30:27 -080090 struct mdp_display_commit prim_commit;
91 memset(&prim_commit, 0, sizeof(struct mdp_display_commit));
92 prim_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
93 if (ioctl(m->framebuffer->fd, MSMFB_DISPLAY_COMMIT, &prim_commit) == -1) {
94 ALOGE("%s: MSMFB_DISPLAY_COMMIT for primary failed, str: %s",
95 __FUNCTION__, strerror(errno));
96 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070097 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -070098 return 0;
99}
100
101static int fb_compositionComplete(struct framebuffer_device_t* dev)
102{
103 // TODO: Properly implement composition complete callback
104 glFinish();
105
106 return 0;
107}
108
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700109int mapFrameBufferLocked(struct private_module_t* module)
110{
111 // already initialized...
112 if (module->framebuffer) {
113 return 0;
114 }
115 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700116 "/dev/graphics/fb%u",
117 "/dev/fb%u",
118 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119
120 int fd = -1;
121 int i=0;
122 char name[64];
123 char property[PROPERTY_VALUE_MAX];
124
125 while ((fd==-1) && device_template[i]) {
126 snprintf(name, 64, device_template[i], 0);
127 fd = open(name, O_RDWR, 0);
128 i++;
129 }
130 if (fd < 0)
131 return -errno;
132
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400133 memset(&module->commit, 0, sizeof(struct mdp_display_commit));
134
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700135 struct fb_fix_screeninfo finfo;
136 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
137 return -errno;
138
139 struct fb_var_screeninfo info;
140 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
141 return -errno;
142
143 info.reserved[0] = 0;
144 info.reserved[1] = 0;
145 info.reserved[2] = 0;
146 info.xoffset = 0;
147 info.yoffset = 0;
148 info.activate = FB_ACTIVATE_NOW;
149
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700150 /* Interpretation of offset for color fields: All offsets are from the
151 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
152 * (means: you can use the offset as right argument to <<). A pixel
153 * afterwards is a bit stream and is written to video memory as that
154 * unmodified. This implies big-endian byte order if bits_per_pixel is
155 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157
158 if(info.bits_per_pixel == 32) {
159 /*
160 * Explicitly request RGBA_8888
161 */
162 info.bits_per_pixel = 32;
163 info.red.offset = 24;
164 info.red.length = 8;
165 info.green.offset = 16;
166 info.green.length = 8;
167 info.blue.offset = 8;
168 info.blue.length = 8;
169 info.transp.offset = 0;
170 info.transp.length = 8;
171
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700172 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
173 * do not use the MDP for composition (i.e. hw composition == 0), ask
174 * for RGBA instead of RGBX. */
175 if (property_get("debug.sf.hw", property, NULL) > 0 &&
176 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700177 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178 else if(property_get("debug.composition.type", property, NULL) > 0 &&
179 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700180 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
181 else
182 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
183 } else {
184 /*
185 * Explicitly request 5/6/5
186 */
187 info.bits_per_pixel = 16;
188 info.red.offset = 11;
189 info.red.length = 5;
190 info.green.offset = 5;
191 info.green.length = 6;
192 info.blue.offset = 0;
193 info.blue.length = 5;
194 info.transp.offset = 0;
195 info.transp.length = 0;
196 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
197 }
198
199 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700200 int size = roundUpToPageSize(info.yres * info.xres *
201 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700202
203 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700204 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205 */
206 int numberOfBuffers = (int)(finfo.smem_len/size);
207 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
208
209 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
210 int num = atoi(property);
211 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
212 numberOfBuffers = num;
213 }
214 }
215 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
216 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
217
218 ALOGV("We support %d buffers", numberOfBuffers);
219
220 //consider the included hole by 4k alignment
221 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
222 info.yres_virtual = (size * numberOfBuffers) / line_length;
223
224 uint32_t flags = PAGE_FLIP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700225
226 if (info.yres_virtual < ((size * 2) / line_length) ) {
227 // we need at least 2 for page-flipping
228 info.yres_virtual = size / line_length;
229 flags &= ~PAGE_FLIP;
230 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700231 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232 }
233
234 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
235 return -errno;
236
237 if (int(info.width) <= 0 || int(info.height) <= 0) {
238 // the driver doesn't return that information
239 // default to 160 dpi
240 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
241 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
242 }
243
244 float xdpi = (info.xres * 25.4f) / info.width;
245 float ydpi = (info.yres * 25.4f) / info.height;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500246#ifdef MSMFB_METADATA_GET
247 struct msmfb_metadata metadata;
248 memset(&metadata, 0 , sizeof(metadata));
249 metadata.op = metadata_op_frame_rate;
250 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
251 ALOGE("Error retrieving panel frame rate");
252 return -errno;
253 }
254 float fps = metadata.data.panel_frame_rate;
255#else
256 //XXX: Remove reserved field usage on all baselines
choongryeol.lee26145b82012-06-26 23:41:00 -0700257 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700258 float fps = info.reserved[3] & 0xFF;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500259#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700260 ALOGI("using (fd=%d)\n"
261 "id = %s\n"
262 "xres = %d px\n"
263 "yres = %d px\n"
264 "xres_virtual = %d px\n"
265 "yres_virtual = %d px\n"
266 "bpp = %d\n"
267 "r = %2u:%u\n"
268 "g = %2u:%u\n"
269 "b = %2u:%u\n",
270 fd,
271 finfo.id,
272 info.xres,
273 info.yres,
274 info.xres_virtual,
275 info.yres_virtual,
276 info.bits_per_pixel,
277 info.red.offset, info.red.length,
278 info.green.offset, info.green.length,
279 info.blue.offset, info.blue.length
280 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700281
Naseer Ahmed29a26812012-06-14 00:56:20 -0700282 ALOGI("width = %d mm (%f dpi)\n"
283 "height = %d mm (%f dpi)\n"
284 "refresh rate = %.2f Hz\n",
285 info.width, xdpi,
286 info.height, ydpi,
287 fps
288 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289
290
291 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
292 return -errno;
293
294 if (finfo.smem_len <= 0)
295 return -errno;
296
297 module->flags = flags;
298 module->info = info;
299 module->finfo = finfo;
300 module->xdpi = xdpi;
301 module->ydpi = ydpi;
302 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700303 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304
305 CALC_INIT();
306
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700307 /*
308 * map the framebuffer
309 */
310
311 int err;
Saurabh Shahd80a52c2012-10-02 14:32:40 -0700312 module->numBuffers = 2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313 module->bufferMask = 0;
314 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700315 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
316 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmedd7f84272012-12-13 13:09:53 -0500318 private_handle_t::PRIV_FLAGS_USES_ION,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700319 BUFFER_TYPE_UI,
320 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700321 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
322 if (vaddr == MAP_FAILED) {
323 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
324 return -errno;
325 }
326 module->framebuffer->base = intptr_t(vaddr);
327 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700328 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700329 module->fbPostDone = false;
330 pthread_mutex_init(&(module->fbPostLock), NULL);
331 pthread_cond_init(&(module->fbPostCond), NULL);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700332 module->fbPanDone = false;
333 pthread_mutex_init(&(module->fbPanLock), NULL);
334 pthread_cond_init(&(module->fbPanCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700335 return 0;
336}
337
338static int mapFrameBuffer(struct private_module_t* module)
339{
340 pthread_mutex_lock(&module->lock);
341 int err = mapFrameBufferLocked(module);
342 pthread_mutex_unlock(&module->lock);
343 return err;
344}
345
346/*****************************************************************************/
347
348static int fb_close(struct hw_device_t *dev)
349{
350 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700351 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700352 //Hack until fbdev is removed. Framework could close this causing hwc a
353 //pain.
354 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700355 }
356 return 0;
357}
358
359int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700360 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700361{
362 int status = -EINVAL;
363 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
364 alloc_device_t* gralloc_device;
365 status = gralloc_open(module, &gralloc_device);
366 if (status < 0)
367 return status;
368
369 /* initialize our state here */
370 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
371 memset(dev, 0, sizeof(*dev));
372
373 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700374 dev->device.common.tag = HARDWARE_DEVICE_TAG;
375 dev->device.common.version = 0;
376 dev->device.common.module = const_cast<hw_module_t*>(module);
377 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700378 dev->device.setSwapInterval = fb_setSwapInterval;
379 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700380 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700381 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700382
383 private_module_t* m = (private_module_t*)module;
384 status = mapFrameBuffer(m);
385 if (status >= 0) {
386 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
387 const_cast<uint32_t&>(dev->device.flags) = 0;
388 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
389 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
390 const_cast<int&>(dev->device.stride) = stride;
391 const_cast<int&>(dev->device.format) = m->fbFormat;
392 const_cast<float&>(dev->device.xdpi) = m->xdpi;
393 const_cast<float&>(dev->device.ydpi) = m->ydpi;
394 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700395 const_cast<int&>(dev->device.minSwapInterval) =
396 PRIV_MIN_SWAP_INTERVAL;
397 const_cast<int&>(dev->device.maxSwapInterval) =
398 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700399 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Naseer Ahmed6f8ca312013-01-15 16:55:06 -0500400 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700401
402 *device = &dev->device.common;
403 }
404
405 // Close the gralloc module
406 gralloc_close(gralloc_device);
407 }
408 return status;
409}