qtmobility/src/multimedia/qpaintervideosurface_mac.mm
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     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 "qpaintervideosurface_mac_p.h"
       
    43 
       
    44 #include <QtCore/qdatetime.h>
       
    45 
       
    46 #include <qmath.h>
       
    47 
       
    48 #include <qpainter.h>
       
    49 #include <qvariant.h>
       
    50 #include <qvideosurfaceformat.h>
       
    51 
       
    52 #include <QtDebug>
       
    53 
       
    54 #include <QuartzCore/CIContext.h>
       
    55 #include <CGLCurrent.h>
       
    56 
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 extern CGContextRef qt_mac_cg_context(const QPaintDevice *pdev); //qpaintdevice_mac.cpp
       
    61 
       
    62 QVideoSurfaceCoreGraphicsPainter::QVideoSurfaceCoreGraphicsPainter(bool glSupported)
       
    63     : ciContext(0)
       
    64     , m_imageFormat(QImage::Format_Invalid)
       
    65     , m_scanLineDirection(QVideoSurfaceFormat::TopToBottom)
       
    66 {
       
    67     //qDebug() << "QVideoSurfaceCoreGraphicsPainter, GL supported:" << glSupported;
       
    68     ciContext = 0;
       
    69     m_imagePixelFormats
       
    70         << QVideoFrame::Format_RGB32
       
    71         << QVideoFrame::Format_ARGB32
       
    72         << QVideoFrame::Format_ARGB32_Premultiplied
       
    73         << QVideoFrame::Format_RGB24
       
    74         << QVideoFrame::Format_RGB565
       
    75         << QVideoFrame::Format_RGB555
       
    76         << QVideoFrame::Format_ARGB8565_Premultiplied;
       
    77 
       
    78     m_supportedHandles
       
    79             << QAbstractVideoBuffer::NoHandle
       
    80             << QAbstractVideoBuffer::CoreImageHandle;
       
    81 
       
    82     if (glSupported)
       
    83             m_supportedHandles << QAbstractVideoBuffer::GLTextureHandle;
       
    84 }
       
    85 
       
    86 QVideoSurfaceCoreGraphicsPainter::~QVideoSurfaceCoreGraphicsPainter()
       
    87 {
       
    88     [(CIContext*)ciContext release];
       
    89 }
       
    90 
       
    91 QList<QVideoFrame::PixelFormat> QVideoSurfaceCoreGraphicsPainter::supportedPixelFormats(
       
    92         QAbstractVideoBuffer::HandleType handleType) const
       
    93 {
       
    94     return m_supportedHandles.contains(handleType)
       
    95         ? m_imagePixelFormats
       
    96         : QList<QVideoFrame::PixelFormat>();
       
    97 }
       
    98 
       
    99 bool QVideoSurfaceCoreGraphicsPainter::isFormatSupported(
       
   100         const QVideoSurfaceFormat &format, QVideoSurfaceFormat *) const
       
   101 {
       
   102     return m_supportedHandles.contains(format.handleType())
       
   103             && m_imagePixelFormats.contains(format.pixelFormat())
       
   104             && !format.frameSize().isEmpty();
       
   105 }
       
   106 
       
   107 QAbstractVideoSurface::Error QVideoSurfaceCoreGraphicsPainter::start(const QVideoSurfaceFormat &format)
       
   108 {
       
   109     m_frame = QVideoFrame();
       
   110     m_imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
       
   111     m_imageSize = format.frameSize();
       
   112     m_scanLineDirection = format.scanLineDirection();
       
   113 
       
   114     return m_supportedHandles.contains(format.handleType())
       
   115             && m_imageFormat != QImage::Format_Invalid
       
   116             && !m_imageSize.isEmpty()
       
   117             ? QAbstractVideoSurface::NoError
       
   118             : QAbstractVideoSurface::UnsupportedFormatError;
       
   119 }
       
   120 
       
   121 void QVideoSurfaceCoreGraphicsPainter::stop()
       
   122 {
       
   123     m_frame = QVideoFrame();
       
   124 }
       
   125 
       
   126 QAbstractVideoSurface::Error QVideoSurfaceCoreGraphicsPainter::setCurrentFrame(const QVideoFrame &frame)
       
   127 {
       
   128     m_frame = frame;
       
   129 
       
   130     return QAbstractVideoSurface::NoError;
       
   131 }
       
   132 
       
   133 QAbstractVideoSurface::Error QVideoSurfaceCoreGraphicsPainter::paint(
       
   134             const QRectF &target, QPainter *painter, const QRectF &source)
       
   135 {
       
   136     if (m_frame.handleType() == QAbstractVideoBuffer::CoreImageHandle) {
       
   137         if (painter->paintEngine()->type() == QPaintEngine::CoreGraphics ) {
       
   138 
       
   139             CIImage *img = (CIImage*)(m_frame.handle().value<void*>());
       
   140 
       
   141             if (img) {
       
   142                 CGContextRef cgContext = qt_mac_cg_context(painter->device());
       
   143 
       
   144                 if (cgContext) {
       
   145                     painter->beginNativePainting();
       
   146 
       
   147                     CGRect sRect = CGRectMake(source.x(), source.y(), source.width(), source.height());
       
   148                     CGRect dRect = CGRectMake(target.x(), target.y(), target.width(), target.height());
       
   149 
       
   150                     NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCIImage:img];
       
   151 
       
   152                     if (m_scanLineDirection == QVideoSurfaceFormat::TopToBottom) {
       
   153                         CGContextSaveGState( cgContext );
       
   154                         CGContextTranslateCTM(cgContext, 0, dRect.origin.y + CGRectGetMaxY(dRect));
       
   155                         CGContextScaleCTM(cgContext, 1, -1);
       
   156 
       
   157                         CGContextDrawImage(cgContext, dRect, [bitmap CGImage]);
       
   158 
       
   159                         CGContextRestoreGState(cgContext);
       
   160                     } else {
       
   161                         CGContextDrawImage(cgContext, dRect, [bitmap CGImage]);
       
   162                     }
       
   163 
       
   164                     [bitmap release];
       
   165 
       
   166                     painter->endNativePainting();
       
   167 
       
   168                     return QAbstractVideoSurface::NoError;
       
   169                 }
       
   170             }
       
   171         } else if (painter->paintEngine()->type() == QPaintEngine::OpenGL2 ||
       
   172                    painter->paintEngine()->type() == QPaintEngine::OpenGL) {
       
   173             CIImage *img = (CIImage*)(m_frame.handle().value<void*>());
       
   174 
       
   175             if (img) {
       
   176                 CGLContextObj cglContext = CGLGetCurrentContext();
       
   177 
       
   178                 if (cglContext) {
       
   179 
       
   180                     if (!ciContext) {
       
   181                         CGLContextObj cglContext = CGLGetCurrentContext();
       
   182                         NSOpenGLPixelFormat *nsglPixelFormat = [NSOpenGLView defaultPixelFormat];
       
   183                         CGLPixelFormatObj cglPixelFormat = static_cast<CGLPixelFormatObj>([nsglPixelFormat CGLPixelFormatObj]);
       
   184 
       
   185                         ciContext = [CIContext contextWithCGLContext:cglContext
       
   186                                      pixelFormat:cglPixelFormat
       
   187                                          options:nil];
       
   188 
       
   189                         [(CIContext*)ciContext retain];
       
   190                     }
       
   191 
       
   192                     CGRect sRect = CGRectMake(source.x(), source.y(), source.width(), source.height());
       
   193                     CGRect dRect = m_scanLineDirection == QVideoSurfaceFormat::TopToBottom ?
       
   194                                    CGRectMake(target.x(), target.y()+target.height(), target.width(), -target.height()) :
       
   195                                    CGRectMake(target.x(), target.y(), target.width(), target.height());
       
   196 
       
   197 
       
   198                     painter->beginNativePainting();
       
   199 
       
   200                     [(CIContext*)ciContext drawImage:img inRect:dRect fromRect:sRect];
       
   201 
       
   202                     painter->endNativePainting();
       
   203 
       
   204                     return QAbstractVideoSurface::NoError;
       
   205                 }
       
   206             }
       
   207         }
       
   208     }
       
   209 
       
   210     if (m_frame.handleType() == QAbstractVideoBuffer::GLTextureHandle &&
       
   211                (painter->paintEngine()->type() == QPaintEngine::OpenGL2 ||
       
   212                 painter->paintEngine()->type() == QPaintEngine::OpenGL)) {
       
   213 
       
   214         painter->beginNativePainting();
       
   215             GLuint texture = m_frame.handle().toUInt();
       
   216 
       
   217             glDisable(GL_CULL_FACE);
       
   218             glEnable(GL_TEXTURE_2D);
       
   219 
       
   220             glBindTexture(GL_TEXTURE_2D, texture);
       
   221             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   222             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   223 
       
   224             const float txLeft = source.left() / m_frame.width();
       
   225             const float txRight = source.right() / m_frame.width();
       
   226             const float txTop = m_scanLineDirection == QVideoSurfaceFormat::TopToBottom
       
   227                     ? source.top() / m_frame.height()
       
   228                     : source.bottom() / m_frame.height();
       
   229             const float txBottom = m_scanLineDirection == QVideoSurfaceFormat::TopToBottom
       
   230                     ? source.bottom() / m_frame.height()
       
   231                     : source.top() / m_frame.height();
       
   232 
       
   233             glBegin(GL_QUADS);
       
   234                 QRectF rect = target;
       
   235                 glTexCoord2f(txLeft, txBottom);
       
   236                 glVertex2f(rect.topLeft().x(), rect.topLeft().y());
       
   237                 glTexCoord2f(txRight, txBottom);
       
   238                 glVertex2f(rect.topRight().x() + 1, rect.topRight().y());
       
   239                 glTexCoord2f(txRight, txTop);
       
   240                 glVertex2f(rect.bottomRight().x() + 1, rect.bottomRight().y() + 1);
       
   241                 glTexCoord2f(txLeft, txTop);
       
   242                 glVertex2f(rect.bottomLeft().x(), rect.bottomLeft().y() + 1);
       
   243             glEnd();
       
   244         painter->endNativePainting();
       
   245 
       
   246         return QAbstractVideoSurface::NoError;
       
   247     }
       
   248 
       
   249     //fallback case, software rendering
       
   250     if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) {
       
   251         QImage image(
       
   252                 m_frame.bits(),
       
   253                 m_imageSize.width(),
       
   254                 m_imageSize.height(),
       
   255                 m_frame.bytesPerLine(),
       
   256                 m_imageFormat);
       
   257 
       
   258         if (m_scanLineDirection == QVideoSurfaceFormat::BottomToTop) {
       
   259             const QTransform oldTransform = painter->transform();
       
   260 
       
   261             painter->scale(1, -1);
       
   262             painter->translate(0, -target.bottom());
       
   263             painter->drawImage(
       
   264                 QRectF(target.x(), 0, target.width(), target.height()), image, source);
       
   265             painter->setTransform(oldTransform);
       
   266         } else {
       
   267             painter->drawImage(target, image, source);
       
   268         }
       
   269 
       
   270         m_frame.unmap();
       
   271     } else if (m_frame.isValid()) {
       
   272         return QAbstractVideoSurface::IncorrectFormatError;
       
   273     } else {
       
   274         painter->fillRect(target, Qt::black);
       
   275     }
       
   276 
       
   277     return QAbstractVideoSurface::NoError;
       
   278 }
       
   279 
       
   280 void QVideoSurfaceCoreGraphicsPainter::updateColors(int, int, int, int)
       
   281 {
       
   282 }
       
   283 
       
   284 QT_END_NAMESPACE