blob: 97295a35d6e8260706798d410922115f12adff42 [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"
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
Naseer Ahmed29a26812012-06-14 00:56:20 -070045#include "overlay.h"
46namespace ovutils = overlay::utils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070047
Iliyan Malchev202a77d2012-06-11 14:41:12 -070048#define EVEN_OUT(x) if (x & 0x0001) {x--;}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070049/** min of int a, b */
50static inline int min(int a, int b) {
51 return (a<b) ? a : b;
52}
53/** max of int a, b */
54static inline int max(int a, int b) {
55 return (a>b) ? a : b;
56}
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057
Iliyan Malchev202a77d2012-06-11 14:41:12 -070058enum {
59 PAGE_FLIP = 0x00000001,
Naseer Ahmed29a26812012-06-14 00:56:20 -070060 LOCKED = 0x00000002
Iliyan Malchev202a77d2012-06-11 14:41:12 -070061};
62
63struct fb_context_t {
64 framebuffer_device_t device;
65};
66
Iliyan Malchev202a77d2012-06-11 14:41:12 -070067
68static int fb_setSwapInterval(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070069 int interval)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070{
Naseer Ahmed30621ab2012-06-25 15:37:21 -070071 //XXX: Get the value here and implement along with
72 //single vsync in HWC
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 char pval[PROPERTY_VALUE_MAX];
Naseer Ahmed29a26812012-06-14 00:56:20 -070074 property_get("debug.egl.swapinterval", pval, "-1");
Iliyan Malchev202a77d2012-06-11 14:41:12 -070075 int property_interval = atoi(pval);
76 if (property_interval >= 0)
77 interval = property_interval;
78
79 fb_context_t* ctx = (fb_context_t*)dev;
80 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070081 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
83 return -EINVAL;
84
85 m->swapInterval = interval;
86 return 0;
87}
88
89static int fb_setUpdateRect(struct framebuffer_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070090 int l, int t, int w, int h)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070091{
92 if (((w|h) <= 0) || ((l|t)<0))
93 return -EINVAL;
94 fb_context_t* ctx = (fb_context_t*)dev;
95 private_module_t* m = reinterpret_cast<private_module_t*>(
Naseer Ahmed29a26812012-06-14 00:56:20 -070096 dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070097 m->info.reserved[0] = 0x54445055; // "UPDT";
98 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
99 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
100 return 0;
101}
102
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700103#if defined(HDMI_DUAL_DISPLAY)
104static int closeHDMIChannel(private_module_t* m)
105{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700106 // XXX - when enabling HDMI
107#if 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700108 Overlay* pTemp = m->pobjOverlay;
109 if(pTemp != NULL)
110 pTemp->closeChannel();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700111#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700112 return 0;
113}
114
Naseer Ahmed29a26812012-06-14 00:56:20 -0700115// XXX - Complete when enabling HDMI
116#if 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700117static void getSecondaryDisplayDestinationInfo(private_module_t* m, overlay_rect&
Naseer Ahmed29a26812012-06-14 00:56:20 -0700118 rect, int& orientation)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119{
120 Overlay* pTemp = m->pobjOverlay;
121 int width = pTemp->getFBWidth();
122 int height = pTemp->getFBHeight();
123 int fbwidth = m->info.xres, fbheight = m->info.yres;
124 rect.x = 0; rect.y = 0;
125 rect.w = width; rect.h = height;
126 int rot = m->orientation;
127 switch(rot) {
128 // ROT_0
129 case 0:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130 // ROT_180
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700131 case HAL_TRANSFORM_ROT_180:
132 pTemp->getAspectRatioPosition(fbwidth, fbheight,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700133 &rect);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700134 if(rot == HAL_TRANSFORM_ROT_180)
135 orientation = HAL_TRANSFORM_ROT_180;
136 else
137 orientation = 0;
138 break;
139 // ROT_90
140 case HAL_TRANSFORM_ROT_90:
141 // ROT_270
142 case HAL_TRANSFORM_ROT_270:
143 //Calculate the Aspectratio for the UI
144 //in the landscape mode
145 //Width and height will be swapped as there
146 //is rotation
147 pTemp->getAspectRatioPosition(fbheight, fbwidth,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700148 &rect);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149
150 if(rot == HAL_TRANSFORM_ROT_90)
151 orientation = HAL_TRANSFORM_ROT_270;
152 else if(rot == HAL_TRANSFORM_ROT_270)
153 orientation = HAL_TRANSFORM_ROT_90;
154 break;
155 }
156 return;
157}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700158#endif
159
160/* Determine overlay state based on whether hardware supports true UI
161 mirroring and whether video is playing or not */
162static ovutils::eOverlayState getOverlayState(struct private_module_t* module)
163{
164 overlay2::Overlay& ov = *(Overlay::getInstance());
165
166 // Default to existing state
167 ovutils::eOverlayState state = ov.getState();
168
169 // Sanity check
170 if (!module) {
171 ALOGE("%s: NULL module", __FUNCTION__);
172 return state;
173 }
174
175 // Check if video is playing or not
176 if (module->videoOverlay) {
177 // Video is playing, check if hardware supports true UI mirroring
178 if (module->trueMirrorSupport) {
179 // True UI mirroring is supported by hardware
180 if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) {
181 // Currently playing 2D video
182 state = ovutils::OV_2D_TRUE_UI_MIRROR;
183 } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) {
184 // Currently playing M3D video
185 // FIXME: Support M3D true UI mirroring
186 state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV;
187 }
188 } else {
189 // True UI mirroring is not supported by hardware
190 if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) {
191 // Currently playing 2D video
192 state = ovutils::OV_2D_VIDEO_ON_PANEL_TV;
193 } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) {
194 // Currently playing M3D video
195 state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV;
196 }
197 }
198 } else {
199 // Video is not playing, true UI mirroring support is irrelevant
200 state = ovutils::OV_UI_MIRROR;
201 }
202
203 return state;
204}
205
206/* Set overlay state */
207static void setOverlayState(ovutils::eOverlayState state)
208{
209 overlay2::Overlay& ov = *(Overlay::getInstance());
210 ov.setState(state);
211}
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212
213static void *hdmi_ui_loop(void *ptr)
214{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700215 private_module_t* m = reinterpret_cast<private_module_t*>(ptr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700216 while (1) {
217 pthread_mutex_lock(&m->overlayLock);
218 while(!(m->hdmiStateChanged))
219 pthread_cond_wait(&(m->overlayPost), &(m->overlayLock));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 m->hdmiStateChanged = false;
222 if (m->exitHDMIUILoop) {
223 pthread_mutex_unlock(&m->overlayLock);
224 return NULL;
225 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 // No need to mirror UI if HDMI is not on
228 if (!m->enableHDMIOutput) {
229 ALOGE_IF(FB_DEBUG, "%s: hdmi not ON", __FUNCTION__);
230 pthread_mutex_unlock(&m->overlayLock);
231 continue;
232 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700233
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234 overlay2::OverlayMgr* ovMgr =
235 overlay2::OverlayMgrSingleton::getOverlayMgr();
236 overlay2::Overlay& ov = ovMgr->ov();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237
Naseer Ahmed29a26812012-06-14 00:56:20 -0700238 // Set overlay state
239 ovutils::eOverlayState state = getOverlayState(m);
240 setOverlayState(state);
241
242 // Determine the RGB pipe for UI depending on the state
243 ovutils::eDest dest = ovutils::OV_PIPE_ALL;
244 if (state == ovutils::OV_2D_TRUE_UI_MIRROR) {
245 // True UI mirroring state: external RGB pipe is OV_PIPE2
246 dest = ovutils::OV_PIPE2;
247 } else if (state == ovutils::OV_UI_MIRROR) {
248 // UI-only mirroring state: external RGB pipe is OV_PIPE0
249 dest = ovutils::OV_PIPE0;
250 } else {
251 // No UI in this case
252 pthread_mutex_unlock(&m->overlayLock);
253 continue;
254 }
255
256 if (m->hdmiMirroringState == HDMI_UI_MIRRORING) {
257 int alignedW = ALIGN(m->info.xres, 32);
258
259 private_handle_t const* hnd =
260 reinterpret_cast<private_handle_t const*>(m->framebuffer);
261 unsigned int width = alignedW;
262 unsigned int height = hnd->height;
263 unsigned int format = hnd->format;
264 unsigned int size = hnd->size/m->numBuffers;
265
266 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
267 // External display connected during secure video playback
268 // Open secure UI session
269 // NOTE: when external display is already connected and then secure
270 // playback is started, we dont have to do anything
271 if (m->secureVideoOverlay) {
272 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700273 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700274
275 ovutils::Whf whf(width, height, format, size);
276 ovutils::PipeArgs parg(mdpFlags,
277 ovutils::OVERLAY_TRANSFORM_0,
278 whf,
279 ovutils::WAIT,
280 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
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 fb_context_t* ctx = (fb_context_t*)dev;
506
Naseer Ahmed00fd6a52012-07-03 21:23:12 -0700507 private_handle_t const* hnd =
508 reinterpret_cast<private_handle_t const*>(buffer);
509 private_module_t* m =
510 reinterpret_cast<private_module_t*>(dev->common.module);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700511
512 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700513 m->base.lock(&m->base, buffer,
514 PRIV_USAGE_LOCKED_FOR_POST,
515 0, 0, m->info.xres, m->info.yres, NULL);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700517 if (m->currentBuffer) {
518 m->base.unlock(&m->base, m->currentBuffer);
519 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");
527 m->base.unlock(&m->base, buffer);
528 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529 }
Naseer Ahmed30621ab2012-06-25 15:37:21 -0700530 CALC_FPS();
531 m->currentBuffer = buffer;
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}