src/gui/image/qimage_ssse3.cpp
changeset 37 758a864f9613
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 QtGui module of the Qt Toolkit.
       
     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 <qimage.h>
       
    43 #include <private/qimage_p.h>
       
    44 #include <private/qsimd_p.h>
       
    45 
       
    46 #ifdef QT_HAVE_SSSE3
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 // Convert a scanline of RGB888 (src) to RGB32 (dst)
       
    51 // src must be at least len * 3 bytes
       
    52 // dst must be at least len * 4 bytes
       
    53 Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32_ssse3(quint32 *dst, const uchar *src, int len)
       
    54 {
       
    55     quint32 *const end = dst + len;
       
    56 
       
    57     // Prologue, align dst to 16 bytes. The alignement is done on dst because it has 4 store()
       
    58     // for each 3 load() of src.
       
    59     const int offsetToAlignOn16Bytes = (4 - ((reinterpret_cast<quintptr>(dst) >> 2) & 0x3)) & 0x3;
       
    60     const int prologLength = qMin(len, offsetToAlignOn16Bytes);
       
    61 
       
    62     for (int i = 0; i < prologLength; ++i) {
       
    63         *dst++ = qRgb(src[0], src[1], src[2]);
       
    64         src += 3;
       
    65     }
       
    66 
       
    67     // Mask the 4 first colors of the RGB888 vector
       
    68     const __m128i shuffleMask = _mm_set_epi8(0xff, 9, 10, 11, 0xff, 6, 7, 8, 0xff, 3, 4, 5, 0xff, 0, 1, 2);
       
    69 
       
    70     // Mask the 4 last colors of a RGB888 vector with an offset of 1 (so the last 3 bytes are RGB)
       
    71     const __m128i shuffleMaskEnd = _mm_set_epi8(0xff, 13, 14, 15, 0xff, 10, 11, 12, 0xff, 7, 8, 9, 0xff, 4, 5, 6);
       
    72 
       
    73     // Mask to have alpha = 0xff
       
    74     const __m128i alphaMask = _mm_set1_epi32(0xff000000);
       
    75 
       
    76     __m128i *inVectorPtr = (__m128i *)src;
       
    77     __m128i *dstVectorPtr = (__m128i *)dst;
       
    78 
       
    79     const int simdRoundCount = (len - prologLength) / 16; // one iteration in the loop converts 16 pixels
       
    80     for (int i = 0; i < simdRoundCount; ++i) {
       
    81         /*
       
    82          RGB888 has 5 pixels per vector, + 1 byte from the next pixel. The idea here is
       
    83          to load vectors of RGB888 and use palignr to select a vector out of two vectors.
       
    84 
       
    85          After 3 loads of RGB888 and 3 stores of RGB32, we have 4 pixels left in the last
       
    86          vector of RGB888, we can mask it directly to get a last store or RGB32. After that,
       
    87          the first next byte is a R, and we can loop for the next 16 pixels.
       
    88 
       
    89          The conversion itself is done with a byte permutation (pshufb).
       
    90          */
       
    91         __m128i firstSrcVector = _mm_lddqu_si128(inVectorPtr);
       
    92         __m128i outputVector = _mm_shuffle_epi8(firstSrcVector, shuffleMask);
       
    93         _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
       
    94         ++inVectorPtr;
       
    95         ++dstVectorPtr;
       
    96 
       
    97         // There are 4 unused bytes left in srcVector, we need to load the next 16 bytes
       
    98         // and load the next input with palignr
       
    99         __m128i secondSrcVector = _mm_lddqu_si128(inVectorPtr);
       
   100         __m128i srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 12);
       
   101         outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
       
   102         _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
       
   103         ++inVectorPtr;
       
   104         ++dstVectorPtr;
       
   105         firstSrcVector = secondSrcVector;
       
   106 
       
   107         // We now have 8 unused bytes left in firstSrcVector
       
   108         secondSrcVector = _mm_lddqu_si128(inVectorPtr);
       
   109         srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 8);
       
   110         outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
       
   111         _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
       
   112         ++inVectorPtr;
       
   113         ++dstVectorPtr;
       
   114 
       
   115         // There are now 12 unused bytes in firstSrcVector.
       
   116         // We can mask them directly, almost there.
       
   117         outputVector = _mm_shuffle_epi8(secondSrcVector, shuffleMaskEnd);
       
   118         _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
       
   119         ++dstVectorPtr;
       
   120     }
       
   121     src = (uchar *)inVectorPtr;
       
   122     dst = (quint32 *)dstVectorPtr;
       
   123 
       
   124     while (dst != end) {
       
   125         *dst++ = qRgb(src[0], src[1], src[2]);
       
   126         src += 3;
       
   127     }
       
   128 }
       
   129 
       
   130 void convert_RGB888_to_RGB32_ssse3(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
       
   131 {
       
   132     Q_ASSERT(src->format == QImage::Format_RGB888);
       
   133     Q_ASSERT(dest->format == QImage::Format_RGB32 || dest->format == QImage::Format_ARGB32 || dest->format == QImage::Format_ARGB32_Premultiplied);
       
   134     Q_ASSERT(src->width == dest->width);
       
   135     Q_ASSERT(src->height == dest->height);
       
   136 
       
   137     const uchar *src_data = (uchar *) src->data;
       
   138     quint32 *dest_data = (quint32 *) dest->data;
       
   139 
       
   140     for (int i = 0; i < src->height; ++i) {
       
   141         qt_convert_rgb888_to_rgb32_ssse3(dest_data, src_data, src->width);
       
   142         src_data += src->bytes_per_line;
       
   143         dest_data = (quint32 *)((uchar*)dest_data + dest->bytes_per_line);
       
   144     }
       
   145 }
       
   146 
       
   147 QT_END_NAMESPACE
       
   148 
       
   149 #endif // QT_HAVE_SSSE3