qtmobility/plugins/multimedia/qt7/qcvdisplaylink.mm
changeset 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qcvdisplaylink.h"
       
    43 
       
    44 #include <QtCore/qcoreapplication.h>
       
    45 #include <QtCore/qdebug.h>
       
    46 
       
    47 QTM_USE_NAMESPACE
       
    48 
       
    49 static CVReturn CVDisplayLinkCallback(CVDisplayLinkRef displayLink,
       
    50                                  const CVTimeStamp *inNow,
       
    51                                  const CVTimeStamp *inOutputTime,
       
    52                                  CVOptionFlags flagsIn,
       
    53                                  CVOptionFlags *flagsOut,
       
    54                                  void *displayLinkContext)
       
    55 {
       
    56     Q_UNUSED(displayLink);
       
    57     Q_UNUSED(inNow);
       
    58     Q_UNUSED(flagsIn);
       
    59     Q_UNUSED(flagsOut);
       
    60 
       
    61     QCvDisplayLink *link = (QCvDisplayLink *)displayLinkContext;
       
    62 
       
    63     link->displayLinkEvent(inOutputTime);
       
    64     return kCVReturnSuccess;
       
    65 }
       
    66 
       
    67 
       
    68 QCvDisplayLink::QCvDisplayLink(QObject *parent)
       
    69     :QObject(parent),
       
    70     m_pendingDisplayLinkEvent(false),
       
    71     m_isActive(false)
       
    72 {
       
    73     // create display link for the main display
       
    74     CVDisplayLinkCreateWithCGDisplay(kCGDirectMainDisplay, &m_displayLink);
       
    75     if (m_displayLink) {
       
    76         // set the current display of a display link.
       
    77         CVDisplayLinkSetCurrentCGDisplay(m_displayLink, kCGDirectMainDisplay);
       
    78 
       
    79         // set the renderer output callback function
       
    80         CVDisplayLinkSetOutputCallback(m_displayLink, &CVDisplayLinkCallback, this);
       
    81     }
       
    82 }
       
    83 
       
    84 QCvDisplayLink::~QCvDisplayLink()
       
    85 {
       
    86     if (m_displayLink) {
       
    87         CVDisplayLinkStop(m_displayLink);
       
    88         CVDisplayLinkRelease(m_displayLink);
       
    89         m_displayLink = NULL;
       
    90     }
       
    91 }
       
    92 
       
    93 bool QCvDisplayLink::isValid()
       
    94 {
       
    95     return m_displayLink != 0;
       
    96 }
       
    97 
       
    98 bool QCvDisplayLink::isActive() const
       
    99 {
       
   100     return m_isActive;
       
   101 }
       
   102 
       
   103 void QCvDisplayLink::start()
       
   104 {
       
   105      if (m_displayLink && !m_isActive) {
       
   106         CVDisplayLinkStart(m_displayLink);
       
   107         m_isActive = true;
       
   108      }
       
   109 }
       
   110 
       
   111 void QCvDisplayLink::stop()
       
   112 {
       
   113     if (m_displayLink && m_isActive) {
       
   114         CVDisplayLinkStop(m_displayLink);
       
   115         m_isActive = false;
       
   116     }
       
   117 }
       
   118 
       
   119 void QCvDisplayLink::displayLinkEvent(const CVTimeStamp *ts)
       
   120 {
       
   121     // This function is called from a
       
   122     // thread != gui thread. So we post the event.
       
   123     // But we need to make sure that we don't post faster
       
   124     // than the event loop can eat:
       
   125     m_displayLinkMutex.lock();
       
   126     bool pending = m_pendingDisplayLinkEvent;
       
   127     m_pendingDisplayLinkEvent = true;
       
   128     m_frameTimeStamp = *ts;
       
   129     m_displayLinkMutex.unlock();
       
   130 
       
   131     if (!pending)
       
   132         qApp->postEvent(this, new QEvent(QEvent::User), Qt::HighEventPriority);
       
   133 }
       
   134 
       
   135 bool QCvDisplayLink::event(QEvent *event)
       
   136 {
       
   137     switch (event->type()){
       
   138         case QEvent::User:  {
       
   139                 m_displayLinkMutex.lock();
       
   140                 m_pendingDisplayLinkEvent = false;
       
   141                 CVTimeStamp ts = m_frameTimeStamp;
       
   142                 m_displayLinkMutex.unlock();
       
   143 
       
   144                 emit tick(ts);
       
   145 
       
   146                 return false;
       
   147             }
       
   148             break;
       
   149         default:
       
   150             break;
       
   151     }
       
   152     return QObject::event(event);
       
   153 }
       
   154 
       
   155 #include "moc_qcvdisplaylink.cpp"
       
   156