blob: 45a27493a5762571c4e677627bad9ccb4ac0a7bf [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"
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
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
88{
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089
Naseer Ahmed88318162012-07-13 07:16:20 -070090 fb_context_t* ctx = (fb_context_t*) dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070091
Naseer Ahmed88318162012-07-13 07:16:20 -070092 private_handle_t *hnd = static_cast<private_handle_t*>
Saurabh Shah3e858eb2012-09-17 16:53:21 -070093 (const_cast<native_handle_t*>(buffer));
Naseer Ahmed00fd6a52012-07-03 21:23:12 -070094 private_module_t* m =
95 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070096
Naseer Ahmed54821fe2012-11-28 18:44:38 -050097 if (hnd) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -070098 m->info.activate = FB_ACTIVATE_VBL | FB_ACTIVATE_FORCE;
99 m->info.yoffset = hnd->offset / m->finfo.line_length;
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400100 m->commit.var = m->info;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500101 m->commit.flags |= MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400102 if (ioctl(m->framebuffer->fd, MSMFB_DISPLAY_COMMIT, &m->commit) == -1) {
Saurabh Shahae823e72012-10-05 00:08:11 -0400103 ALOGE("%s: MSMFB_DISPLAY_COMMIT ioctl failed, err: %s", __FUNCTION__,
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700104 strerror(errno));
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700105 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700106 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700107 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700108 return 0;
109}
110
111static int fb_compositionComplete(struct framebuffer_device_t* dev)
112{
113 // TODO: Properly implement composition complete callback
114 glFinish();
115
116 return 0;
117}
118
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119int mapFrameBufferLocked(struct private_module_t* module)
120{
121 // already initialized...
122 if (module->framebuffer) {
123 return 0;
124 }
125 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700126 "/dev/graphics/fb%u",
127 "/dev/fb%u",
128 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700129
130 int fd = -1;
131 int i=0;
132 char name[64];
133 char property[PROPERTY_VALUE_MAX];
134
135 while ((fd==-1) && device_template[i]) {
136 snprintf(name, 64, device_template[i], 0);
137 fd = open(name, O_RDWR, 0);
138 i++;
139 }
140 if (fd < 0)
141 return -errno;
142
Naseer Ahmed32aa90f2012-10-01 18:45:13 -0400143 memset(&module->commit, 0, sizeof(struct mdp_display_commit));
144
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700145 struct fb_fix_screeninfo finfo;
146 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
147 return -errno;
148
149 struct fb_var_screeninfo info;
150 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
151 return -errno;
152
153 info.reserved[0] = 0;
154 info.reserved[1] = 0;
155 info.reserved[2] = 0;
156 info.xoffset = 0;
157 info.yoffset = 0;
158 info.activate = FB_ACTIVATE_NOW;
159
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700160 /* Interpretation of offset for color fields: All offsets are from the
161 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
162 * (means: you can use the offset as right argument to <<). A pixel
163 * afterwards is a bit stream and is written to video memory as that
164 * unmodified. This implies big-endian byte order if bits_per_pixel is
165 * greater than 8.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700166 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167
168 if(info.bits_per_pixel == 32) {
169 /*
170 * Explicitly request RGBA_8888
171 */
172 info.bits_per_pixel = 32;
173 info.red.offset = 24;
174 info.red.length = 8;
175 info.green.offset = 16;
176 info.green.length = 8;
177 info.blue.offset = 8;
178 info.blue.length = 8;
179 info.transp.offset = 0;
180 info.transp.length = 8;
181
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700182 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
183 * do not use the MDP for composition (i.e. hw composition == 0), ask
184 * for RGBA instead of RGBX. */
185 if (property_get("debug.sf.hw", property, NULL) > 0 &&
186 atoi(property) == 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700187 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700188 else if(property_get("debug.composition.type", property, NULL) > 0 &&
189 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
191 else
192 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
193 } else {
194 /*
195 * Explicitly request 5/6/5
196 */
197 info.bits_per_pixel = 16;
198 info.red.offset = 11;
199 info.red.length = 5;
200 info.green.offset = 5;
201 info.green.length = 6;
202 info.blue.offset = 0;
203 info.blue.length = 5;
204 info.transp.offset = 0;
205 info.transp.length = 0;
206 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
207 }
208
209 //adreno needs 4k aligned offsets. Max hole size is 4096-1
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700210 int size = roundUpToPageSize(info.yres * info.xres *
211 (info.bits_per_pixel/8));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212
213 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700214 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215 */
216 int numberOfBuffers = (int)(finfo.smem_len/size);
217 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
218
219 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
220 int num = atoi(property);
221 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
222 numberOfBuffers = num;
223 }
224 }
225 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
226 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
227
228 ALOGV("We support %d buffers", numberOfBuffers);
229
230 //consider the included hole by 4k alignment
231 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
232 info.yres_virtual = (size * numberOfBuffers) / line_length;
233
234 uint32_t flags = PAGE_FLIP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700235
236 if (info.yres_virtual < ((size * 2) / line_length) ) {
237 // we need at least 2 for page-flipping
238 info.yres_virtual = size / line_length;
239 flags &= ~PAGE_FLIP;
240 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700241 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700242 }
243
244 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
245 return -errno;
246
247 if (int(info.width) <= 0 || int(info.height) <= 0) {
248 // the driver doesn't return that information
249 // default to 160 dpi
250 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
251 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
252 }
253
254 float xdpi = (info.xres * 25.4f) / info.width;
255 float ydpi = (info.yres * 25.4f) / info.height;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500256#ifdef MSMFB_METADATA_GET
257 struct msmfb_metadata metadata;
258 memset(&metadata, 0 , sizeof(metadata));
259 metadata.op = metadata_op_frame_rate;
260 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
261 ALOGE("Error retrieving panel frame rate");
262 return -errno;
263 }
264 float fps = metadata.data.panel_frame_rate;
265#else
266 //XXX: Remove reserved field usage on all baselines
choongryeol.lee26145b82012-06-26 23:41:00 -0700267 //The reserved[3] field is used to store FPS by the driver.
Naseer Ahmed9edd17c2012-08-15 18:16:50 -0700268 float fps = info.reserved[3] & 0xFF;
Ken Zhang6e6f9a92013-01-08 15:09:27 -0500269#endif
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 ALOGI("using (fd=%d)\n"
271 "id = %s\n"
272 "xres = %d px\n"
273 "yres = %d px\n"
274 "xres_virtual = %d px\n"
275 "yres_virtual = %d px\n"
276 "bpp = %d\n"
277 "r = %2u:%u\n"
278 "g = %2u:%u\n"
279 "b = %2u:%u\n",
280 fd,
281 finfo.id,
282 info.xres,
283 info.yres,
284 info.xres_virtual,
285 info.yres_virtual,
286 info.bits_per_pixel,
287 info.red.offset, info.red.length,
288 info.green.offset, info.green.length,
289 info.blue.offset, info.blue.length
290 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291
Naseer Ahmed29a26812012-06-14 00:56:20 -0700292 ALOGI("width = %d mm (%f dpi)\n"
293 "height = %d mm (%f dpi)\n"
294 "refresh rate = %.2f Hz\n",
295 info.width, xdpi,
296 info.height, ydpi,
297 fps
298 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700299
300
301 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
302 return -errno;
303
304 if (finfo.smem_len <= 0)
305 return -errno;
306
307 module->flags = flags;
308 module->info = info;
309 module->finfo = finfo;
310 module->xdpi = xdpi;
311 module->ydpi = ydpi;
312 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700314
315 CALC_INIT();
316
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317 /*
318 * map the framebuffer
319 */
320
321 int err;
Saurabh Shahd80a52c2012-10-02 14:32:40 -0700322 module->numBuffers = 2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700323 module->bufferMask = 0;
324 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
326 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700327 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmedd7f84272012-12-13 13:09:53 -0500328 private_handle_t::PRIV_FLAGS_USES_ION,
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700329 BUFFER_TYPE_UI,
330 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
332 if (vaddr == MAP_FAILED) {
333 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
334 return -errno;
335 }
336 module->framebuffer->base = intptr_t(vaddr);
337 memset(vaddr, 0, fbSize);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700338 module->currentOffset = 0;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700339 module->fbPostDone = false;
340 pthread_mutex_init(&(module->fbPostLock), NULL);
341 pthread_cond_init(&(module->fbPostCond), NULL);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700342 module->fbPanDone = false;
343 pthread_mutex_init(&(module->fbPanLock), NULL);
344 pthread_cond_init(&(module->fbPanCond), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345 return 0;
346}
347
348static int mapFrameBuffer(struct private_module_t* module)
349{
350 pthread_mutex_lock(&module->lock);
351 int err = mapFrameBufferLocked(module);
352 pthread_mutex_unlock(&module->lock);
353 return err;
354}
355
356/*****************************************************************************/
357
358static int fb_close(struct hw_device_t *dev)
359{
360 fb_context_t* ctx = (fb_context_t*)dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700361 if (ctx) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700362 //Hack until fbdev is removed. Framework could close this causing hwc a
363 //pain.
364 //free(ctx);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700365 }
366 return 0;
367}
368
369int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371{
372 int status = -EINVAL;
373 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
374 alloc_device_t* gralloc_device;
375 status = gralloc_open(module, &gralloc_device);
376 if (status < 0)
377 return status;
378
379 /* initialize our state here */
380 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
381 memset(dev, 0, sizeof(*dev));
382
383 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384 dev->device.common.tag = HARDWARE_DEVICE_TAG;
385 dev->device.common.version = 0;
386 dev->device.common.module = const_cast<hw_module_t*>(module);
387 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700388 dev->device.setSwapInterval = fb_setSwapInterval;
389 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700390 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700391 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700392
393 private_module_t* m = (private_module_t*)module;
394 status = mapFrameBuffer(m);
395 if (status >= 0) {
396 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
397 const_cast<uint32_t&>(dev->device.flags) = 0;
398 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
399 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
400 const_cast<int&>(dev->device.stride) = stride;
401 const_cast<int&>(dev->device.format) = m->fbFormat;
402 const_cast<float&>(dev->device.xdpi) = m->xdpi;
403 const_cast<float&>(dev->device.ydpi) = m->ydpi;
404 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700405 const_cast<int&>(dev->device.minSwapInterval) =
406 PRIV_MIN_SWAP_INTERVAL;
407 const_cast<int&>(dev->device.maxSwapInterval) =
408 PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700409 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Naseer Ahmed6f8ca312013-01-15 16:55:06 -0500410 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411
412 *device = &dev->device.common;
413 }
414
415 // Close the gralloc module
416 gralloc_close(gralloc_device);
417 }
418 return status;
419}