blob: 837c53a45d94d431007220dd1f74aca3a9940160 [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
Naseer Ahmed29a26812012-06-14 00:56:20 -070046#include "overlay.h"
47namespace ovutils = overlay::utils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070048
Iliyan Malchev202a77d2012-06-11 14:41:12 -070049#define EVEN_OUT(x) if (x & 0x0001) {x--;}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070050/** min of int a, b */
51static inline int min(int a, int b) {
52 return (a<b) ? a : b;
53}
54/** max of int a, b */
55static inline int max(int a, int b) {
56 return (a>b) ? a : b;
57}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070058
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059enum {
60 PAGE_FLIP = 0x00000001,
Naseer Ahmed29a26812012-06-14 00:56:20 -070061 LOCKED = 0x00000002
Iliyan Malchev202a77d2012-06-11 14:41:12 -070062};
63
64struct fb_context_t {
65 framebuffer_device_t device;
66};
67
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068
69static int fb_setSwapInterval(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070070 int interval)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071{
Naseer Ahmed30621ab2012-06-25 15:37:21 -070072 //XXX: Get the value here and implement along with
73 //single vsync in HWC
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074 char pval[PROPERTY_VALUE_MAX];
Naseer Ahmed29a26812012-06-14 00:56:20 -070075 property_get("debug.egl.swapinterval", pval, "-1");
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076 int property_interval = atoi(pval);
77 if (property_interval >= 0)
78 interval = property_interval;
79
80 fb_context_t* ctx = (fb_context_t*)dev;
81 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070082 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
84 return -EINVAL;
85
86 m->swapInterval = interval;
87 return 0;
88}
89
90static int fb_setUpdateRect(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070091 int l, int t, int w, int h)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092{
93 if (((w|h) <= 0) || ((l|t)<0))
94 return -EINVAL;
95 fb_context_t* ctx = (fb_context_t*)dev;
96 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070097 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070098 m->info.reserved[0] = 0x54445055; // "UPDT";
99 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
100 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
101 return 0;
102}
103
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700104#if defined(HDMI_DUAL_DISPLAY)
105static int closeHDMIChannel(private_module_t* m)
106{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700107 // XXX - when enabling HDMI
108#if 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700109 Overlay* pTemp = m->pobjOverlay;
110 if(pTemp != NULL)
111 pTemp->closeChannel();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700112#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700113 return 0;
114}
115
Naseer Ahmed29a26812012-06-14 00:56:20 -0700116// XXX - Complete when enabling HDMI
117#if 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118static void getSecondaryDisplayDestinationInfo(private_module_t* m, overlay_rect&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119 rect, int& orientation)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120{
121 Overlay* pTemp = m->pobjOverlay;
122 int width = pTemp->getFBWidth();
123 int height = pTemp->getFBHeight();
124 int fbwidth = m->info.xres, fbheight = m->info.yres;
125 rect.x = 0; rect.y = 0;
126 rect.w = width; rect.h = height;
127 int rot = m->orientation;
128 switch(rot) {
129 // ROT_0
130 case 0:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 // ROT_180
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132 case HAL_TRANSFORM_ROT_180:
133 pTemp->getAspectRatioPosition(fbwidth, fbheight,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134 &rect);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700135 if(rot == HAL_TRANSFORM_ROT_180)
136 orientation = HAL_TRANSFORM_ROT_180;
137 else
138 orientation = 0;
139 break;
140 // ROT_90
141 case HAL_TRANSFORM_ROT_90:
142 // ROT_270
143 case HAL_TRANSFORM_ROT_270:
144 //Calculate the Aspectratio for the UI
145 //in the landscape mode
146 //Width and height will be swapped as there
147 //is rotation
148 pTemp->getAspectRatioPosition(fbheight, fbwidth,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700149 &rect);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700150
151 if(rot == HAL_TRANSFORM_ROT_90)
152 orientation = HAL_TRANSFORM_ROT_270;
153 else if(rot == HAL_TRANSFORM_ROT_270)
154 orientation = HAL_TRANSFORM_ROT_90;
155 break;
156 }
157 return;
158}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159#endif
160
161/* Determine overlay state based on whether hardware supports true UI
162 mirroring and whether video is playing or not */
163static ovutils::eOverlayState getOverlayState(struct private_module_t* module)
164{
165 overlay2::Overlay& ov = *(Overlay::getInstance());
166
167 // Default to existing state
168 ovutils::eOverlayState state = ov.getState();
169
170 // Sanity check
171 if (!module) {
172 ALOGE("%s: NULL module", __FUNCTION__);
173 return state;
174 }
175
176 // Check if video is playing or not
177 if (module->videoOverlay) {
178 // Video is playing, check if hardware supports true UI mirroring
179 if (module->trueMirrorSupport) {
180 // True UI mirroring is supported by hardware
181 if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) {
182 // Currently playing 2D video
183 state = ovutils::OV_2D_TRUE_UI_MIRROR;
184 } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) {
185 // Currently playing M3D video
186 // FIXME: Support M3D true UI mirroring
187 state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV;
188 }
189 } else {
190 // True UI mirroring is not supported by hardware
191 if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) {
192 // Currently playing 2D video
193 state = ovutils::OV_2D_VIDEO_ON_PANEL_TV;
194 } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) {
195 // Currently playing M3D video
196 state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV;
197 }
198 }
199 } else {
200 // Video is not playing, true UI mirroring support is irrelevant
201 state = ovutils::OV_UI_MIRROR;
202 }
203
204 return state;
205}
206
207/* Set overlay state */
208static void setOverlayState(ovutils::eOverlayState state)
209{
210 overlay2::Overlay& ov = *(Overlay::getInstance());
211 ov.setState(state);
212}
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700213
214static void *hdmi_ui_loop(void *ptr)
215{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 private_module_t* m = reinterpret_cast<private_module_t*>(ptr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700217 while (1) {
218 pthread_mutex_lock(&m->overlayLock);
219 while(!(m->hdmiStateChanged))
220 pthread_cond_wait(&(m->overlayPost), &(m->overlayLock));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700221
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700222 m->hdmiStateChanged = false;
223 if (m->exitHDMIUILoop) {
224 pthread_mutex_unlock(&m->overlayLock);
225 return NULL;
226 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700227
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228 // No need to mirror UI if HDMI is not on
229 if (!m->enableHDMIOutput) {
230 ALOGE_IF(FB_DEBUG, "%s: hdmi not ON", __FUNCTION__);
231 pthread_mutex_unlock(&m->overlayLock);
232 continue;
233 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700234
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 overlay2::OverlayMgr* ovMgr =
236 overlay2::OverlayMgrSingleton::getOverlayMgr();
237 overlay2::Overlay& ov = ovMgr->ov();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700238
Naseer Ahmed29a26812012-06-14 00:56:20 -0700239 // Set overlay state
240 ovutils::eOverlayState state = getOverlayState(m);
241 setOverlayState(state);
242
243 // Determine the RGB pipe for UI depending on the state
244 ovutils::eDest dest = ovutils::OV_PIPE_ALL;
245 if (state == ovutils::OV_2D_TRUE_UI_MIRROR) {
246 // True UI mirroring state: external RGB pipe is OV_PIPE2
247 dest = ovutils::OV_PIPE2;
248 } else if (state == ovutils::OV_UI_MIRROR) {
249 // UI-only mirroring state: external RGB pipe is OV_PIPE0
250 dest = ovutils::OV_PIPE0;
251 } else {
252 // No UI in this case
253 pthread_mutex_unlock(&m->overlayLock);
254 continue;
255 }
256
257 if (m->hdmiMirroringState == HDMI_UI_MIRRORING) {
258 int alignedW = ALIGN(m->info.xres, 32);
259
260 private_handle_t const* hnd =
261 reinterpret_cast<private_handle_t const*>(m->framebuffer);
262 unsigned int width = alignedW;
263 unsigned int height = hnd->height;
264 unsigned int format = hnd->format;
265 unsigned int size = hnd->size/m->numBuffers;
266
267 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
268 // External display connected during secure video playback
269 // Open secure UI session
270 // NOTE: when external display is already connected and then secure
271 // playback is started, we dont have to do anything
272 if (m->secureVideoOverlay) {
273 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700274 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275
276 ovutils::Whf whf(width, height, format, size);
277 ovutils::PipeArgs parg(mdpFlags,
278 ovutils::OVERLAY_TRANSFORM_0,
279 whf,
280 ovutils::WAIT,
281 ovutils::ZORDER_0,
282 ovutils::IS_FG_OFF,
283 ovutils::ROT_FLAG_ENABLED);
284 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
285 bool ret = ov.setSource(pargs, dest);
286 if (!ret) {
287 ALOGE("%s setSource failed", __FUNCTION__);
288 }
289
290 // we need to communicate m->orientation that will get some
291 // modifications within setParameter func.
292 // FIXME that is ugly.
293 const ovutils::Params prms (ovutils::OVERLAY_TRANSFORM_UI,
294 m->orientation);
295 ov.setParameter(prms, dest);
296 if (!ret) {
297 ALOGE("%s setParameter failed transform", __FUNCTION__);
298 }
299
300 // x,y,w,h
301 ovutils::Dim dcrop(0, 0, m->info.xres, m->info.yres);
302 ov.setMemoryId(m->framebuffer->fd, dest);
303 ret = ov.setCrop(dcrop, dest);
304 if (!ret) {
305 ALOGE("%s setCrop failed", __FUNCTION__);
306 }
307
308 ovutils::Dim pdim (m->info.xres,
309 m->info.yres,
310 0,
311 0,
312 m->orientation);
313 ret = ov.setPosition(pdim, dest);
314 if (!ret) {
315 ALOGE("%s setPosition failed", __FUNCTION__);
316 }
317
318 if (!ov.commit(dest)) {
319 ALOGE("%s commit fails", __FUNCTION__);
320 }
321
322 ret = ov.queueBuffer(m->currentOffset, dest);
323 if (!ret) {
324 ALOGE("%s queueBuffer failed", __FUNCTION__);
325 }
326 } else {
327 setOverlayState(ovutils::OV_CLOSED);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700328 }
329 pthread_mutex_unlock(&m->overlayLock);
330 }
331 return NULL;
332}
333
334static int fb_videoOverlayStarted(struct framebuffer_device_t* dev, int started)
335{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 ALOGE_IF(FB_DEBUG, "%s started=%d", __FUNCTION__, started);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700338 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339 pthread_mutex_lock(&m->overlayLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340 if(started != m->videoOverlay) {
341 m->videoOverlay = started;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 m->hdmiStateChanged = true;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343 if (!m->trueMirrorSupport) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 if (started) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345 m->hdmiMirroringState = HDMI_NO_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346 ovutils::eOverlayState state = getOverlayState(m);
347 setOverlayState(state);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 } else if (m->enableHDMIOutput)
349 m->hdmiMirroringState = HDMI_UI_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350 } else {
351 if (m->videoOverlay == VIDEO_3D_OVERLAY_STARTED) {
352 ALOGE_IF(FB_DEBUG, "3D Video Started, stop mirroring!");
353 m->hdmiMirroringState = HDMI_NO_MIRRORING;
354 ovutils::eOverlayState state = getOverlayState(m);
355 setOverlayState(state);
356 }
357 else if (m->enableHDMIOutput) {
358 m->hdmiMirroringState = HDMI_UI_MIRRORING;
359 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 }
361 }
362 pthread_mutex_unlock(&m->overlayLock);
363 return 0;
364}
365
366static int fb_enableHDMIOutput(struct framebuffer_device_t* dev, int externaltype)
367{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700368 ALOGE_IF(FB_DEBUG, "%s externaltype=%d", __FUNCTION__, externaltype);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700369 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371 pthread_mutex_lock(&m->overlayLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700372 //Check if true mirroring can be supported
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 m->trueMirrorSupport = ovutils::FrameBufferInfo::getInstance()->supportTrueMirroring();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374 m->enableHDMIOutput = externaltype;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 if(externaltype) {
376 if (m->trueMirrorSupport) {
377 m->hdmiMirroringState = HDMI_UI_MIRRORING;
378 } else {
379 if(!m->videoOverlay)
380 m->hdmiMirroringState = HDMI_UI_MIRRORING;
381 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700382 } else if (!externaltype) {
383 // Either HDMI is disconnected or suspend occurred
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700384 m->hdmiMirroringState = HDMI_NO_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385 ovutils::eOverlayState state = getOverlayState(m);
386 setOverlayState(state);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700387 }
388 m->hdmiStateChanged = true;
389 pthread_cond_signal(&(m->overlayPost));
390 pthread_mutex_unlock(&m->overlayLock);
391 return 0;
392}
393
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700394static int fb_orientationChanged(struct framebuffer_device_t* dev, int orientation)
395{
396 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700397 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700398 pthread_mutex_lock(&m->overlayLock);
399 neworientation = orientation;
400 pthread_mutex_unlock(&m->overlayLock);
401 return 0;
402}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700403
404static int handle_open_secure_start(private_module_t* m) {
405 pthread_mutex_lock(&m->overlayLock);
406 m->hdmiMirroringState = HDMI_NO_MIRRORING;
407 m->secureVideoOverlay = true;
408 pthread_mutex_unlock(&m->overlayLock);
409 return 0;
410}
411
412static int handle_open_secure_end(private_module_t* m) {
413 pthread_mutex_lock(&m->overlayLock);
414 if (m->enableHDMIOutput) {
415 if (m->trueMirrorSupport) {
416 m->hdmiMirroringState = HDMI_UI_MIRRORING;
417 } else if(!m->videoOverlay) {
418 m->hdmiMirroringState = HDMI_UI_MIRRORING;
419 }
420 m->hdmiStateChanged = true;
421 pthread_cond_signal(&(m->overlayPost));
422 }
423 pthread_mutex_unlock(&m->overlayLock);
424 return 0;
425}
426
427static int handle_close_secure_start(private_module_t* m) {
428 pthread_mutex_lock(&m->overlayLock);
429 m->hdmiMirroringState = HDMI_NO_MIRRORING;
430 m->secureVideoOverlay = false;
431 pthread_mutex_unlock(&m->overlayLock);
432 return 0;
433}
434
435static int handle_close_secure_end(private_module_t* m) {
436 pthread_mutex_lock(&m->overlayLock);
437 if (m->enableHDMIOutput) {
438 if (m->trueMirrorSupport) {
439 m->hdmiMirroringState = HDMI_UI_MIRRORING;
440 } else if(!m->videoOverlay) {
441 m->hdmiMirroringState = HDMI_UI_MIRRORING;
442 }
443 m->hdmiStateChanged = true;
444 pthread_cond_signal(&(m->overlayPost));
445 }
446 pthread_mutex_unlock(&m->overlayLock);
447 return 0;
448}
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700449#endif
450
Naseer Ahmed29a26812012-06-14 00:56:20 -0700451
452
453/* fb_perform - used to add custom event and handle them in fb HAL
454 * Used for external display related functions as of now
455 */
456static int fb_perform(struct framebuffer_device_t* dev, int event, int value)
457{
458 private_module_t* m = reinterpret_cast<private_module_t*>(
459 dev->common.module);
460 switch(event) {
461#if defined(HDMI_DUAL_DISPLAY)
462 case EVENT_EXTERNAL_DISPLAY:
463 fb_enableHDMIOutput(dev, value);
464 break;
465 case EVENT_VIDEO_OVERLAY:
466 fb_videoOverlayStarted(dev, value);
467 break;
468 case EVENT_ORIENTATION_CHANGE:
469 fb_orientationChanged(dev, value);
470 break;
471 case EVENT_OVERLAY_STATE_CHANGE:
472 if (value == OVERLAY_STATE_CHANGE_START) {
473 // When state change starts, get a lock on overlay
474 pthread_mutex_lock(&m->overlayLock);
475 } else if (value == OVERLAY_STATE_CHANGE_END) {
476 // When state change is complete, unlock overlay
477 pthread_mutex_unlock(&m->overlayLock);
478 }
479 break;
480 case EVENT_OPEN_SECURE_START:
481 handle_open_secure_start(m);
482 break;
483 case EVENT_OPEN_SECURE_END:
484 handle_open_secure_end(m);
485 break;
486 case EVENT_CLOSE_SECURE_START:
487 handle_close_secure_start(m);
488 break;
489 case EVENT_CLOSE_SECURE_END:
490 handle_close_secure_end(m);
491 break;
492#endif
493 default:
494 ALOGE("In %s: UNKNOWN Event = %d!!!", __FUNCTION__, event);
495 break;
496 }
497 return 0;
498}
499
500
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
502{
503 if (private_handle_t::validate(buffer) < 0)
504 return -EINVAL;
505
Naseer Ahmed88318162012-07-13 07:16:20 -0700506 fb_context_t* ctx = (fb_context_t*) dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700507
Naseer Ahmed88318162012-07-13 07:16:20 -0700508 private_handle_t *hnd = static_cast<private_handle_t*>
509 (const_cast<native_handle_t*>(buffer));
510
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700511 private_module_t* m =
512 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700513
514 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700515 genlock_lock_buffer(hnd, GENLOCK_READ_LOCK, GENLOCK_MAX_TIMEOUT);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700517 if (m->currentBuffer) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700518 genlock_unlock_buffer(m->currentBuffer);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700519 m->currentBuffer = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700520 }
521
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700522 const size_t offset = hnd->base - m->framebuffer->base;
523 m->info.activate = FB_ACTIVATE_VBL;
524 m->info.yoffset = offset / m->finfo.line_length;
525 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
526 ALOGE("FBIOPUT_VSCREENINFO failed");
Naseer Ahmed88318162012-07-13 07:16:20 -0700527 genlock_unlock_buffer(hnd);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700528 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529 }
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700530 CALC_FPS();
Naseer Ahmed88318162012-07-13 07:16:20 -0700531 m->currentBuffer = hnd;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700532 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700533 return 0;
534}
535
536static int fb_compositionComplete(struct framebuffer_device_t* dev)
537{
538 // TODO: Properly implement composition complete callback
539 glFinish();
540
541 return 0;
542}
543
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700544int mapFrameBufferLocked(struct private_module_t* module)
545{
546 // already initialized...
547 if (module->framebuffer) {
548 return 0;
549 }
550 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551 "/dev/graphics/fb%u",
552 "/dev/fb%u",
553 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700554
555 int fd = -1;
556 int i=0;
557 char name[64];
558 char property[PROPERTY_VALUE_MAX];
559
560 while ((fd==-1) && device_template[i]) {
561 snprintf(name, 64, device_template[i], 0);
562 fd = open(name, O_RDWR, 0);
563 i++;
564 }
565 if (fd < 0)
566 return -errno;
567
568 struct fb_fix_screeninfo finfo;
569 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
570 return -errno;
571
572 struct fb_var_screeninfo info;
573 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
574 return -errno;
575
576 info.reserved[0] = 0;
577 info.reserved[1] = 0;
578 info.reserved[2] = 0;
579 info.xoffset = 0;
580 info.yoffset = 0;
581 info.activate = FB_ACTIVATE_NOW;
582
583 /* Interpretation of offset for color fields: All offsets are from the right,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 * inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you
585 * can use the offset as right argument to <<). A pixel afterwards is a bit
586 * stream and is written to video memory as that unmodified. This implies
587 * big-endian byte order if bits_per_pixel is greater than 8.
588 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700589
590 if(info.bits_per_pixel == 32) {
591 /*
592 * Explicitly request RGBA_8888
593 */
594 info.bits_per_pixel = 32;
595 info.red.offset = 24;
596 info.red.length = 8;
597 info.green.offset = 16;
598 info.green.length = 8;
599 info.blue.offset = 8;
600 info.blue.length = 8;
601 info.transp.offset = 0;
602 info.transp.length = 8;
603
604 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we do
605 * not use the MDP for composition (i.e. hw composition == 0), ask for
606 * RGBA instead of RGBX. */
607 if (property_get("debug.sf.hw", property, NULL) > 0 && atoi(property) == 0)
608 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609 else if(property_get("debug.composition.type", property, NULL) > 0 &&
610 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700611 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
612 else
613 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
614 } else {
615 /*
616 * Explicitly request 5/6/5
617 */
618 info.bits_per_pixel = 16;
619 info.red.offset = 11;
620 info.red.length = 5;
621 info.green.offset = 5;
622 info.green.length = 6;
623 info.blue.offset = 0;
624 info.blue.length = 5;
625 info.transp.offset = 0;
626 info.transp.length = 0;
627 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
628 }
629
630 //adreno needs 4k aligned offsets. Max hole size is 4096-1
631 int size = roundUpToPageSize(info.yres * info.xres * (info.bits_per_pixel/8));
632
633 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700634 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700635 */
636 int numberOfBuffers = (int)(finfo.smem_len/size);
637 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
638
639 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
640 int num = atoi(property);
641 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
642 numberOfBuffers = num;
643 }
644 }
645 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
646 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
647
648 ALOGV("We support %d buffers", numberOfBuffers);
649
650 //consider the included hole by 4k alignment
651 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
652 info.yres_virtual = (size * numberOfBuffers) / line_length;
653
654 uint32_t flags = PAGE_FLIP;
655 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
656 info.yres_virtual = size / line_length;
657 flags &= ~PAGE_FLIP;
658 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
659 }
660
661 if (info.yres_virtual < ((size * 2) / line_length) ) {
662 // we need at least 2 for page-flipping
663 info.yres_virtual = size / line_length;
664 flags &= ~PAGE_FLIP;
665 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700666 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700667 }
668
669 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
670 return -errno;
671
672 if (int(info.width) <= 0 || int(info.height) <= 0) {
673 // the driver doesn't return that information
674 // default to 160 dpi
675 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
676 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
677 }
678
679 float xdpi = (info.xres * 25.4f) / info.width;
680 float ydpi = (info.yres * 25.4f) / info.height;
choongryeol.lee26145b82012-06-26 23:41:00 -0700681 //The reserved[3] field is used to store FPS by the driver.
682 float fps = info.reserved[3];
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700683
Naseer Ahmed29a26812012-06-14 00:56:20 -0700684 ALOGI("using (fd=%d)\n"
685 "id = %s\n"
686 "xres = %d px\n"
687 "yres = %d px\n"
688 "xres_virtual = %d px\n"
689 "yres_virtual = %d px\n"
690 "bpp = %d\n"
691 "r = %2u:%u\n"
692 "g = %2u:%u\n"
693 "b = %2u:%u\n",
694 fd,
695 finfo.id,
696 info.xres,
697 info.yres,
698 info.xres_virtual,
699 info.yres_virtual,
700 info.bits_per_pixel,
701 info.red.offset, info.red.length,
702 info.green.offset, info.green.length,
703 info.blue.offset, info.blue.length
704 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700705
Naseer Ahmed29a26812012-06-14 00:56:20 -0700706 ALOGI("width = %d mm (%f dpi)\n"
707 "height = %d mm (%f dpi)\n"
708 "refresh rate = %.2f Hz\n",
709 info.width, xdpi,
710 info.height, ydpi,
711 fps
712 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700713
714
715 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
716 return -errno;
717
718 if (finfo.smem_len <= 0)
719 return -errno;
720
721 module->flags = flags;
722 module->info = info;
723 module->finfo = finfo;
724 module->xdpi = xdpi;
725 module->ydpi = ydpi;
726 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700727 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700728
729 CALC_INIT();
730
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700731 /*
732 * map the framebuffer
733 */
734
735 int err;
736 module->numBuffers = info.yres_virtual / info.yres;
737 module->bufferMask = 0;
738 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700739 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
740 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700741 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700742 private_handle_t::PRIV_FLAGS_USES_PMEM,
743 BUFFER_TYPE_UI,
744 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700745 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
746 if (vaddr == MAP_FAILED) {
747 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
748 return -errno;
749 }
750 module->framebuffer->base = intptr_t(vaddr);
751 memset(vaddr, 0, fbSize);
752
753#if defined(HDMI_DUAL_DISPLAY)
754 /* Overlay for HDMI*/
755 pthread_mutex_init(&(module->overlayLock), NULL);
756 pthread_cond_init(&(module->overlayPost), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700757 module->currentOffset = 0;
758 module->exitHDMIUILoop = false;
759 module->hdmiStateChanged = false;
760 pthread_t hdmiUIThread;
761 pthread_create(&hdmiUIThread, NULL, &hdmi_ui_loop, (void *) module);
762 module->hdmiMirroringState = HDMI_NO_MIRRORING;
763 module->trueMirrorSupport = false;
764#endif
765
766 return 0;
767}
768
769static int mapFrameBuffer(struct private_module_t* module)
770{
771 pthread_mutex_lock(&module->lock);
772 int err = mapFrameBufferLocked(module);
773 pthread_mutex_unlock(&module->lock);
774 return err;
775}
776
777/*****************************************************************************/
778
779static int fb_close(struct hw_device_t *dev)
780{
781 fb_context_t* ctx = (fb_context_t*)dev;
782#if defined(HDMI_DUAL_DISPLAY)
783 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700784 ctx->device.common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700785 pthread_mutex_lock(&m->overlayLock);
786 m->exitHDMIUILoop = true;
787 pthread_cond_signal(&(m->overlayPost));
788 pthread_mutex_unlock(&m->overlayLock);
789#endif
790 if (ctx) {
791 free(ctx);
792 }
793 return 0;
794}
795
796int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700797 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700798{
799 int status = -EINVAL;
800 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
801 alloc_device_t* gralloc_device;
802 status = gralloc_open(module, &gralloc_device);
803 if (status < 0)
804 return status;
805
806 /* initialize our state here */
807 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
808 memset(dev, 0, sizeof(*dev));
809
810 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700811 dev->device.common.tag = HARDWARE_DEVICE_TAG;
812 dev->device.common.version = 0;
813 dev->device.common.module = const_cast<hw_module_t*>(module);
814 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700815 dev->device.setSwapInterval = fb_setSwapInterval;
816 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700817 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700818 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700819
820 private_module_t* m = (private_module_t*)module;
821 status = mapFrameBuffer(m);
822 if (status >= 0) {
823 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
824 const_cast<uint32_t&>(dev->device.flags) = 0;
825 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
826 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
827 const_cast<int&>(dev->device.stride) = stride;
828 const_cast<int&>(dev->device.format) = m->fbFormat;
829 const_cast<float&>(dev->device.xdpi) = m->xdpi;
830 const_cast<float&>(dev->device.ydpi) = m->ydpi;
831 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700832 const_cast<int&>(dev->device.minSwapInterval) = PRIV_MIN_SWAP_INTERVAL;
833 const_cast<int&>(dev->device.maxSwapInterval) = PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700834 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700835 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700836 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700837 dev->device.setUpdateRect = fb_setUpdateRect;
838 ALOGD("UPDATE_ON_DEMAND supported");
839 }
840
841 *device = &dev->device.common;
842 }
843
844 // Close the gralloc module
845 gralloc_close(gralloc_device);
846 }
847 return status;
848}