blob: f689d529f025f8daa0c635380a6ed6cd6dccf960 [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,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700280 ovutils::ZORDER_0,
281 ovutils::IS_FG_OFF,
282 ovutils::ROT_FLAG_ENABLED);
283 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
284 bool ret = ov.setSource(pargs, dest);
285 if (!ret) {
286 ALOGE("%s setSource failed", __FUNCTION__);
287 }
288
289 // we need to communicate m->orientation that will get some
290 // modifications within setParameter func.
291 // FIXME that is ugly.
292 const ovutils::Params prms (ovutils::OVERLAY_TRANSFORM_UI,
293 m->orientation);
294 ov.setParameter(prms, dest);
295 if (!ret) {
296 ALOGE("%s setParameter failed transform", __FUNCTION__);
297 }
298
299 // x,y,w,h
300 ovutils::Dim dcrop(0, 0, m->info.xres, m->info.yres);
301 ov.setMemoryId(m->framebuffer->fd, dest);
302 ret = ov.setCrop(dcrop, dest);
303 if (!ret) {
304 ALOGE("%s setCrop failed", __FUNCTION__);
305 }
306
307 ovutils::Dim pdim (m->info.xres,
308 m->info.yres,
309 0,
310 0,
311 m->orientation);
312 ret = ov.setPosition(pdim, dest);
313 if (!ret) {
314 ALOGE("%s setPosition failed", __FUNCTION__);
315 }
316
317 if (!ov.commit(dest)) {
318 ALOGE("%s commit fails", __FUNCTION__);
319 }
320
321 ret = ov.queueBuffer(m->currentOffset, dest);
322 if (!ret) {
323 ALOGE("%s queueBuffer failed", __FUNCTION__);
324 }
325 } else {
326 setOverlayState(ovutils::OV_CLOSED);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700327 }
328 pthread_mutex_unlock(&m->overlayLock);
329 }
330 return NULL;
331}
332
333static int fb_videoOverlayStarted(struct framebuffer_device_t* dev, int started)
334{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 ALOGE_IF(FB_DEBUG, "%s started=%d", __FUNCTION__, started);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700336 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700338 pthread_mutex_lock(&m->overlayLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339 if(started != m->videoOverlay) {
340 m->videoOverlay = started;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341 m->hdmiStateChanged = true;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700342 if (!m->trueMirrorSupport) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700343 if (started) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344 m->hdmiMirroringState = HDMI_NO_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700345 ovutils::eOverlayState state = getOverlayState(m);
346 setOverlayState(state);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700347 } else if (m->enableHDMIOutput)
348 m->hdmiMirroringState = HDMI_UI_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700349 } else {
350 if (m->videoOverlay == VIDEO_3D_OVERLAY_STARTED) {
351 ALOGE_IF(FB_DEBUG, "3D Video Started, stop mirroring!");
352 m->hdmiMirroringState = HDMI_NO_MIRRORING;
353 ovutils::eOverlayState state = getOverlayState(m);
354 setOverlayState(state);
355 }
356 else if (m->enableHDMIOutput) {
357 m->hdmiMirroringState = HDMI_UI_MIRRORING;
358 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700359 }
360 }
361 pthread_mutex_unlock(&m->overlayLock);
362 return 0;
363}
364
365static int fb_enableHDMIOutput(struct framebuffer_device_t* dev, int externaltype)
366{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700367 ALOGE_IF(FB_DEBUG, "%s externaltype=%d", __FUNCTION__, externaltype);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700368 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700369 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700370 pthread_mutex_lock(&m->overlayLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371 //Check if true mirroring can be supported
Naseer Ahmed29a26812012-06-14 00:56:20 -0700372 m->trueMirrorSupport = ovutils::FrameBufferInfo::getInstance()->supportTrueMirroring();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700373 m->enableHDMIOutput = externaltype;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374 if(externaltype) {
375 if (m->trueMirrorSupport) {
376 m->hdmiMirroringState = HDMI_UI_MIRRORING;
377 } else {
378 if(!m->videoOverlay)
379 m->hdmiMirroringState = HDMI_UI_MIRRORING;
380 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700381 } else if (!externaltype) {
382 // Either HDMI is disconnected or suspend occurred
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700383 m->hdmiMirroringState = HDMI_NO_MIRRORING;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384 ovutils::eOverlayState state = getOverlayState(m);
385 setOverlayState(state);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700386 }
387 m->hdmiStateChanged = true;
388 pthread_cond_signal(&(m->overlayPost));
389 pthread_mutex_unlock(&m->overlayLock);
390 return 0;
391}
392
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700393static int fb_orientationChanged(struct framebuffer_device_t* dev, int orientation)
394{
395 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700396 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700397 pthread_mutex_lock(&m->overlayLock);
398 neworientation = orientation;
399 pthread_mutex_unlock(&m->overlayLock);
400 return 0;
401}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700402
403static int handle_open_secure_start(private_module_t* m) {
404 pthread_mutex_lock(&m->overlayLock);
405 m->hdmiMirroringState = HDMI_NO_MIRRORING;
406 m->secureVideoOverlay = true;
407 pthread_mutex_unlock(&m->overlayLock);
408 return 0;
409}
410
411static int handle_open_secure_end(private_module_t* m) {
412 pthread_mutex_lock(&m->overlayLock);
413 if (m->enableHDMIOutput) {
414 if (m->trueMirrorSupport) {
415 m->hdmiMirroringState = HDMI_UI_MIRRORING;
416 } else if(!m->videoOverlay) {
417 m->hdmiMirroringState = HDMI_UI_MIRRORING;
418 }
419 m->hdmiStateChanged = true;
420 pthread_cond_signal(&(m->overlayPost));
421 }
422 pthread_mutex_unlock(&m->overlayLock);
423 return 0;
424}
425
426static int handle_close_secure_start(private_module_t* m) {
427 pthread_mutex_lock(&m->overlayLock);
428 m->hdmiMirroringState = HDMI_NO_MIRRORING;
429 m->secureVideoOverlay = false;
430 pthread_mutex_unlock(&m->overlayLock);
431 return 0;
432}
433
434static int handle_close_secure_end(private_module_t* m) {
435 pthread_mutex_lock(&m->overlayLock);
436 if (m->enableHDMIOutput) {
437 if (m->trueMirrorSupport) {
438 m->hdmiMirroringState = HDMI_UI_MIRRORING;
439 } else if(!m->videoOverlay) {
440 m->hdmiMirroringState = HDMI_UI_MIRRORING;
441 }
442 m->hdmiStateChanged = true;
443 pthread_cond_signal(&(m->overlayPost));
444 }
445 pthread_mutex_unlock(&m->overlayLock);
446 return 0;
447}
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700448#endif
449
Naseer Ahmed29a26812012-06-14 00:56:20 -0700450
451
452/* fb_perform - used to add custom event and handle them in fb HAL
453 * Used for external display related functions as of now
454 */
455static int fb_perform(struct framebuffer_device_t* dev, int event, int value)
456{
457 private_module_t* m = reinterpret_cast<private_module_t*>(
458 dev->common.module);
459 switch(event) {
460#if defined(HDMI_DUAL_DISPLAY)
461 case EVENT_EXTERNAL_DISPLAY:
462 fb_enableHDMIOutput(dev, value);
463 break;
464 case EVENT_VIDEO_OVERLAY:
465 fb_videoOverlayStarted(dev, value);
466 break;
467 case EVENT_ORIENTATION_CHANGE:
468 fb_orientationChanged(dev, value);
469 break;
470 case EVENT_OVERLAY_STATE_CHANGE:
471 if (value == OVERLAY_STATE_CHANGE_START) {
472 // When state change starts, get a lock on overlay
473 pthread_mutex_lock(&m->overlayLock);
474 } else if (value == OVERLAY_STATE_CHANGE_END) {
475 // When state change is complete, unlock overlay
476 pthread_mutex_unlock(&m->overlayLock);
477 }
478 break;
479 case EVENT_OPEN_SECURE_START:
480 handle_open_secure_start(m);
481 break;
482 case EVENT_OPEN_SECURE_END:
483 handle_open_secure_end(m);
484 break;
485 case EVENT_CLOSE_SECURE_START:
486 handle_close_secure_start(m);
487 break;
488 case EVENT_CLOSE_SECURE_END:
489 handle_close_secure_end(m);
490 break;
491#endif
492 default:
493 ALOGE("In %s: UNKNOWN Event = %d!!!", __FUNCTION__, event);
494 break;
495 }
496 return 0;
497}
498
499
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
501{
502 if (private_handle_t::validate(buffer) < 0)
503 return -EINVAL;
504
Naseer Ahmed88318162012-07-13 07:16:20 -0700505 fb_context_t* ctx = (fb_context_t*) dev;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700506
Naseer Ahmed88318162012-07-13 07:16:20 -0700507 private_handle_t *hnd = static_cast<private_handle_t*>
508 (const_cast<native_handle_t*>(buffer));
509
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700510 private_module_t* m =
511 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512
513 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700514 genlock_lock_buffer(hnd, GENLOCK_READ_LOCK, GENLOCK_MAX_TIMEOUT);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700516 if (m->currentBuffer) {
Naseer Ahmed88318162012-07-13 07:16:20 -0700517 genlock_unlock_buffer(m->currentBuffer);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700518 m->currentBuffer = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 }
520
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700521 const size_t offset = hnd->base - m->framebuffer->base;
522 m->info.activate = FB_ACTIVATE_VBL;
523 m->info.yoffset = offset / m->finfo.line_length;
524 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
525 ALOGE("FBIOPUT_VSCREENINFO failed");
Naseer Ahmed88318162012-07-13 07:16:20 -0700526 genlock_unlock_buffer(hnd);
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700527 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700528 }
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700529 CALC_FPS();
Naseer Ahmed88318162012-07-13 07:16:20 -0700530 m->currentBuffer = hnd;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700531 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700532 return 0;
533}
534
535static int fb_compositionComplete(struct framebuffer_device_t* dev)
536{
537 // TODO: Properly implement composition complete callback
538 glFinish();
539
540 return 0;
541}
542
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700543int mapFrameBufferLocked(struct private_module_t* module)
544{
545 // already initialized...
546 if (module->framebuffer) {
547 return 0;
548 }
549 char const * const device_template[] = {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700550 "/dev/graphics/fb%u",
551 "/dev/fb%u",
552 0 };
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700553
554 int fd = -1;
555 int i=0;
556 char name[64];
557 char property[PROPERTY_VALUE_MAX];
558
559 while ((fd==-1) && device_template[i]) {
560 snprintf(name, 64, device_template[i], 0);
561 fd = open(name, O_RDWR, 0);
562 i++;
563 }
564 if (fd < 0)
565 return -errno;
566
567 struct fb_fix_screeninfo finfo;
568 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
569 return -errno;
570
571 struct fb_var_screeninfo info;
572 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
573 return -errno;
574
575 info.reserved[0] = 0;
576 info.reserved[1] = 0;
577 info.reserved[2] = 0;
578 info.xoffset = 0;
579 info.yoffset = 0;
580 info.activate = FB_ACTIVATE_NOW;
581
582 /* Interpretation of offset for color fields: All offsets are from the right,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700583 * inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you
584 * can use the offset as right argument to <<). A pixel afterwards is a bit
585 * stream and is written to video memory as that unmodified. This implies
586 * big-endian byte order if bits_per_pixel is greater than 8.
587 */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700588
589 if(info.bits_per_pixel == 32) {
590 /*
591 * Explicitly request RGBA_8888
592 */
593 info.bits_per_pixel = 32;
594 info.red.offset = 24;
595 info.red.length = 8;
596 info.green.offset = 16;
597 info.green.length = 8;
598 info.blue.offset = 8;
599 info.blue.length = 8;
600 info.transp.offset = 0;
601 info.transp.length = 8;
602
603 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we do
604 * not use the MDP for composition (i.e. hw composition == 0), ask for
605 * RGBA instead of RGBX. */
606 if (property_get("debug.sf.hw", property, NULL) > 0 && atoi(property) == 0)
607 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700608 else if(property_get("debug.composition.type", property, NULL) > 0 &&
609 (strncmp(property, "mdp", 3) == 0))
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700610 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
611 else
612 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
613 } else {
614 /*
615 * Explicitly request 5/6/5
616 */
617 info.bits_per_pixel = 16;
618 info.red.offset = 11;
619 info.red.length = 5;
620 info.green.offset = 5;
621 info.green.length = 6;
622 info.blue.offset = 0;
623 info.blue.length = 5;
624 info.transp.offset = 0;
625 info.transp.length = 0;
626 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
627 }
628
629 //adreno needs 4k aligned offsets. Max hole size is 4096-1
630 int size = roundUpToPageSize(info.yres * info.xres * (info.bits_per_pixel/8));
631
632 /*
Naseer Ahmed29a26812012-06-14 00:56:20 -0700633 * Request NUM_BUFFERS screens (at least 2 for page flipping)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700634 */
635 int numberOfBuffers = (int)(finfo.smem_len/size);
636 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
637
638 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
639 int num = atoi(property);
640 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
641 numberOfBuffers = num;
642 }
643 }
644 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
645 numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
646
647 ALOGV("We support %d buffers", numberOfBuffers);
648
649 //consider the included hole by 4k alignment
650 uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
651 info.yres_virtual = (size * numberOfBuffers) / line_length;
652
653 uint32_t flags = PAGE_FLIP;
654 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
655 info.yres_virtual = size / line_length;
656 flags &= ~PAGE_FLIP;
657 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
658 }
659
660 if (info.yres_virtual < ((size * 2) / line_length) ) {
661 // we need at least 2 for page-flipping
662 info.yres_virtual = size / line_length;
663 flags &= ~PAGE_FLIP;
664 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700665 info.yres_virtual, info.yres*2);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700666 }
667
668 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
669 return -errno;
670
671 if (int(info.width) <= 0 || int(info.height) <= 0) {
672 // the driver doesn't return that information
673 // default to 160 dpi
674 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
675 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
676 }
677
678 float xdpi = (info.xres * 25.4f) / info.width;
679 float ydpi = (info.yres * 25.4f) / info.height;
choongryeol.lee26145b82012-06-26 23:41:00 -0700680 //The reserved[3] field is used to store FPS by the driver.
681 float fps = info.reserved[3];
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700682
Naseer Ahmed29a26812012-06-14 00:56:20 -0700683 ALOGI("using (fd=%d)\n"
684 "id = %s\n"
685 "xres = %d px\n"
686 "yres = %d px\n"
687 "xres_virtual = %d px\n"
688 "yres_virtual = %d px\n"
689 "bpp = %d\n"
690 "r = %2u:%u\n"
691 "g = %2u:%u\n"
692 "b = %2u:%u\n",
693 fd,
694 finfo.id,
695 info.xres,
696 info.yres,
697 info.xres_virtual,
698 info.yres_virtual,
699 info.bits_per_pixel,
700 info.red.offset, info.red.length,
701 info.green.offset, info.green.length,
702 info.blue.offset, info.blue.length
703 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700704
Naseer Ahmed29a26812012-06-14 00:56:20 -0700705 ALOGI("width = %d mm (%f dpi)\n"
706 "height = %d mm (%f dpi)\n"
707 "refresh rate = %.2f Hz\n",
708 info.width, xdpi,
709 info.height, ydpi,
710 fps
711 );
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700712
713
714 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
715 return -errno;
716
717 if (finfo.smem_len <= 0)
718 return -errno;
719
720 module->flags = flags;
721 module->info = info;
722 module->finfo = finfo;
723 module->xdpi = xdpi;
724 module->ydpi = ydpi;
725 module->fps = fps;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700726 module->swapInterval = 1;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700727
728 CALC_INIT();
729
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700730 /*
731 * map the framebuffer
732 */
733
734 int err;
735 module->numBuffers = info.yres_virtual / info.yres;
736 module->bufferMask = 0;
737 //adreno needs page aligned offsets. Align the fbsize to pagesize.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700738 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
739 module->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700740 module->framebuffer = new private_handle_t(fd, fbSize,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700741 private_handle_t::PRIV_FLAGS_USES_PMEM,
742 BUFFER_TYPE_UI,
743 module->fbFormat, info.xres, info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700744 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
745 if (vaddr == MAP_FAILED) {
746 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
747 return -errno;
748 }
749 module->framebuffer->base = intptr_t(vaddr);
750 memset(vaddr, 0, fbSize);
751
752#if defined(HDMI_DUAL_DISPLAY)
753 /* Overlay for HDMI*/
754 pthread_mutex_init(&(module->overlayLock), NULL);
755 pthread_cond_init(&(module->overlayPost), NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700756 module->currentOffset = 0;
757 module->exitHDMIUILoop = false;
758 module->hdmiStateChanged = false;
759 pthread_t hdmiUIThread;
760 pthread_create(&hdmiUIThread, NULL, &hdmi_ui_loop, (void *) module);
761 module->hdmiMirroringState = HDMI_NO_MIRRORING;
762 module->trueMirrorSupport = false;
763#endif
764
765 return 0;
766}
767
768static int mapFrameBuffer(struct private_module_t* module)
769{
770 pthread_mutex_lock(&module->lock);
771 int err = mapFrameBufferLocked(module);
772 pthread_mutex_unlock(&module->lock);
773 return err;
774}
775
776/*****************************************************************************/
777
778static int fb_close(struct hw_device_t *dev)
779{
780 fb_context_t* ctx = (fb_context_t*)dev;
781#if defined(HDMI_DUAL_DISPLAY)
782 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700783 ctx->device.common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700784 pthread_mutex_lock(&m->overlayLock);
785 m->exitHDMIUILoop = true;
786 pthread_cond_signal(&(m->overlayPost));
787 pthread_mutex_unlock(&m->overlayLock);
788#endif
789 if (ctx) {
790 free(ctx);
791 }
792 return 0;
793}
794
795int fb_device_open(hw_module_t const* module, const char* name,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700796 hw_device_t** device)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700797{
798 int status = -EINVAL;
799 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
800 alloc_device_t* gralloc_device;
801 status = gralloc_open(module, &gralloc_device);
802 if (status < 0)
803 return status;
804
805 /* initialize our state here */
806 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
807 memset(dev, 0, sizeof(*dev));
808
809 /* initialize the procs */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700810 dev->device.common.tag = HARDWARE_DEVICE_TAG;
811 dev->device.common.version = 0;
812 dev->device.common.module = const_cast<hw_module_t*>(module);
813 dev->device.common.close = fb_close;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700814 dev->device.setSwapInterval = fb_setSwapInterval;
815 dev->device.post = fb_post;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700816 dev->device.setUpdateRect = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700817 dev->device.compositionComplete = fb_compositionComplete;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700818
819 private_module_t* m = (private_module_t*)module;
820 status = mapFrameBuffer(m);
821 if (status >= 0) {
822 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
823 const_cast<uint32_t&>(dev->device.flags) = 0;
824 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
825 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
826 const_cast<int&>(dev->device.stride) = stride;
827 const_cast<int&>(dev->device.format) = m->fbFormat;
828 const_cast<float&>(dev->device.xdpi) = m->xdpi;
829 const_cast<float&>(dev->device.ydpi) = m->ydpi;
830 const_cast<float&>(dev->device.fps) = m->fps;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700831 const_cast<int&>(dev->device.minSwapInterval) = PRIV_MIN_SWAP_INTERVAL;
832 const_cast<int&>(dev->device.maxSwapInterval) = PRIV_MAX_SWAP_INTERVAL;
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700833 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700834 if (m->finfo.reserved[0] == 0x5444 &&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700835 m->finfo.reserved[1] == 0x5055) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700836 dev->device.setUpdateRect = fb_setUpdateRect;
837 ALOGD("UPDATE_ON_DEMAND supported");
838 }
839
840 *device = &dev->device.common;
841 }
842
843 // Close the gralloc module
844 gralloc_close(gralloc_device);
845 }
846 return status;
847}