Changeset 70706 in webkit


Ignore:
Timestamp:
Oct 27, 2010 2:09:18 PM (13 years ago)
Author:
zmo@google.com
Message:

2010-10-27 Zhenyao Mo <zmo@google.com>

Reviewed by Kenneth Russell.

refactor the nested large switch statements in GraphicsContext3DCG.cpp:getImageData()
https://bugs.webkit.org/show_bug.cgi?id=47027

  • platform/graphics/GraphicsContext3D.cpp: (WebCore::doPacking): ASSERT false if undefined format is passed in.
  • platform/graphics/GraphicsContext3D.h: Add kSourceFormatUndefined enum.
  • platform/graphics/cg/GraphicsContext3DCG.cpp: (WebCore::getSourceDataFormat): Decide source data format from componentsPerPixel, alpha format, bitsPerComponet, etc. (WebCore::GraphicsContext3D::getImageData): Refactor the code to use getSourceDataFormat and remove nested switches.
Location:
trunk/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r70705 r70706  
     12010-10-27  Zhenyao Mo  <zmo@google.com>
     2
     3        Reviewed by Kenneth Russell.
     4
     5        refactor the nested large switch statements in GraphicsContext3DCG.cpp:getImageData()
     6        https://bugs.webkit.org/show_bug.cgi?id=47027
     7
     8        * platform/graphics/GraphicsContext3D.cpp:
     9        (WebCore::doPacking): ASSERT false if undefined format is passed in.
     10        * platform/graphics/GraphicsContext3D.h: Add kSourceFormatUndefined enum.
     11        * platform/graphics/cg/GraphicsContext3DCG.cpp:
     12        (WebCore::getSourceDataFormat): Decide source data format from componentsPerPixel, alpha format, bitsPerComponet, etc.
     13        (WebCore::GraphicsContext3D::getImageData): Refactor the code to use getSourceDataFormat and remove nested switches.
     14
    1152010-10-27  Patrick Gansterer  <paroga@webkit.org>
    216
  • trunk/WebCore/platform/graphics/GraphicsContext3D.cpp

    r70073 r70706  
    946946        break;
    947947    }
     948    default:
     949        ASSERT(false);
    948950    }
    949951}
  • trunk/WebCore/platform/graphics/GraphicsContext3D.h

    r70073 r70706  
    541541    // by non-member functions.
    542542    enum SourceDataFormat {
    543         kSourceFormatRGBA8,
     543        kSourceFormatRGBA8 = 0,
    544544        kSourceFormatRGBA16Little,
    545545        kSourceFormatRGBA16Big,
     
    567567        kSourceFormatA8,
    568568        kSourceFormatA16Little,
    569         kSourceFormatA16Big
     569        kSourceFormatA16Big,
     570        kSourceFormatNumFormats
    570571    };
    571572
  • trunk/WebCore/platform/graphics/cg/GraphicsContext3DCG.cpp

    r70073 r70706  
    4242namespace WebCore {
    4343
     44enum SourceDataFormatBase {
     45    SourceFormatBaseR = 0,
     46    SourceFormatBaseA,
     47    SourceFormatBaseRA,
     48    SourceFormatBaseAR,
     49    SourceFormatBaseRGB,
     50    SourceFormatBaseRGBA,
     51    SourceFormatBaseARGB,
     52    SourceFormatBaseNumFormats
     53};
     54
     55enum AlphaFormat {
     56    AlphaFormatNone = 0,
     57    AlphaFormatFirst,
     58    AlphaFormatLast,
     59    AlphaFormatNumFormats
     60};
     61
     62// This returns kSourceFormatNumFormats if the combination of input parameters is unsupported.
     63static GraphicsContext3D::SourceDataFormat getSourceDataFormat(unsigned int componentsPerPixel, AlphaFormat alphaFormat, bool is16BitFormat, bool bigEndian)
     64{
     65    const static SourceDataFormatBase formatTableBase[4][AlphaFormatNumFormats] = { // componentsPerPixel x AlphaFormat
     66        // AlphaFormatNone            AlphaFormatFirst            AlphaFormatLast
     67        { SourceFormatBaseR,          SourceFormatBaseA,          SourceFormatBaseA          }, // 1 componentsPerPixel
     68        { SourceFormatBaseNumFormats, SourceFormatBaseAR,         SourceFormatBaseRA         }, // 2 componentsPerPixel
     69        { SourceFormatBaseRGB,        SourceFormatBaseNumFormats, SourceFormatBaseNumFormats }, // 3 componentsPerPixel
     70        { SourceFormatBaseNumFormats, SourceFormatBaseARGB,       SourceFormatBaseRGBA        } // 4 componentsPerPixel
     71    };
     72    const static GraphicsContext3D::SourceDataFormat formatTable[SourceFormatBaseNumFormats][3] = { // SourceDataFormatBase x bitsPerComponentAndEndian
     73        // 8bits                                 16bits, little endian                         16bits, big endian
     74        { GraphicsContext3D::kSourceFormatR8,    GraphicsContext3D::kSourceFormatR16Little,    GraphicsContext3D::kSourceFormatR16Big },
     75        { GraphicsContext3D::kSourceFormatA8,    GraphicsContext3D::kSourceFormatA16Little,    GraphicsContext3D::kSourceFormatA16Big },
     76        { GraphicsContext3D::kSourceFormatRA8,   GraphicsContext3D::kSourceFormatRA16Little,   GraphicsContext3D::kSourceFormatRA16Big },
     77        { GraphicsContext3D::kSourceFormatAR8,   GraphicsContext3D::kSourceFormatAR16Little,   GraphicsContext3D::kSourceFormatAR16Big },
     78        { GraphicsContext3D::kSourceFormatRGB8,  GraphicsContext3D::kSourceFormatRGB16Little,  GraphicsContext3D::kSourceFormatRGB16Big },
     79        { GraphicsContext3D::kSourceFormatRGBA8, GraphicsContext3D::kSourceFormatRGBA16Little, GraphicsContext3D::kSourceFormatRGBA16Big },
     80        { GraphicsContext3D::kSourceFormatARGB8, GraphicsContext3D::kSourceFormatARGB16Little, GraphicsContext3D::kSourceFormatARGB16Big }
     81    };
     82
     83    ASSERT(componentsPerPixel <= 4 && componentsPerPixel > 0);
     84    SourceDataFormatBase formatBase = formatTableBase[componentsPerPixel - 1][alphaFormat];
     85    if (formatBase == SourceFormatBaseNumFormats)
     86        return GraphicsContext3D::kSourceFormatNumFormats;
     87    if (!is16BitFormat)
     88        return formatTable[formatBase][0];
     89    if (!bigEndian)
     90        return formatTable[formatBase][1];
     91    return formatTable[formatBase][2];
     92}
     93
    4494bool GraphicsContext3D::getImageData(Image* image,
    4595                                     unsigned int format,
     
    63113    if (!cgImage)
    64114        return false;
     115
    65116    size_t width = CGImageGetWidth(cgImage);
    66117    size_t height = CGImageGetHeight(cgImage);
     
    74125        return false;
    75126    size_t componentsPerPixel = bitsPerPixel / bitsPerComponent;
     127
    76128    bool srcByteOrder16Big = false;
    77129    if (bitsPerComponent == 16) {
     
    94146        }
    95147    }
    96     SourceDataFormat srcDataFormat = kSourceFormatRGBA8;
     148
    97149    AlphaOp neededAlphaOp = kAlphaDoNothing;
     150    AlphaFormat alphaFormat = AlphaFormatNone;
    98151    switch (CGImageGetAlphaInfo(cgImage)) {
    99152    case kCGImageAlphaPremultipliedFirst:
     
    104157        if (!premultiplyAlpha)
    105158            neededAlphaOp = kAlphaDoUnmultiply;
    106         switch (componentsPerPixel) {
    107         case 2:
    108             if (bitsPerComponent == 8)
    109                 srcDataFormat = kSourceFormatAR8;
    110             else
    111                 srcDataFormat = srcByteOrder16Big ? kSourceFormatAR16Big : kSourceFormatAR16Little;
    112             break;
    113         case 4:
    114             if (bitsPerComponent == 8)
    115                 srcDataFormat = kSourceFormatARGB8;
    116             else
    117                 srcDataFormat = srcByteOrder16Big ? kSourceFormatARGB16Big : kSourceFormatARGB16Little;
    118             break;
    119         default:
    120             return false;
    121         }
     159        alphaFormat = AlphaFormatFirst;
    122160        break;
    123161    case kCGImageAlphaFirst:
     
    125163        if (premultiplyAlpha)
    126164            neededAlphaOp = kAlphaDoPremultiply;
    127         switch (componentsPerPixel) {
    128         case 1:
    129             if (bitsPerComponent == 8)
    130                 srcDataFormat = kSourceFormatA8;
    131             else
    132                 srcDataFormat = srcByteOrder16Big ? kSourceFormatA16Big : kSourceFormatA16Little;
    133             break;
    134         case 2:
    135             if (bitsPerComponent == 8)
    136                 srcDataFormat = kSourceFormatAR8;
    137             else
    138                 srcDataFormat = srcByteOrder16Big ? kSourceFormatAR16Big : kSourceFormatAR16Little;
    139             break;
    140         case 4:
    141             if (bitsPerComponent == 8)
    142                 srcDataFormat = kSourceFormatARGB8;
    143             else
    144                 srcDataFormat = srcByteOrder16Big ? kSourceFormatARGB16Big : kSourceFormatARGB16Little;
    145             break;
    146         default:
    147             return false;
    148         }
     165        alphaFormat = AlphaFormatFirst;
    149166        break;
    150167    case kCGImageAlphaNoneSkipFirst:
    151168        // This path is only accessible for MacOS earlier than 10.6.4.
    152         switch (componentsPerPixel) {
    153         case 2:
    154             if (bitsPerComponent == 8)
    155                 srcDataFormat = kSourceFormatAR8;
    156             else
    157                 srcDataFormat = srcByteOrder16Big ? kSourceFormatAR16Big : kSourceFormatAR16Little;
    158             break;
    159         case 4:
    160             if (bitsPerComponent == 8)
    161                 srcDataFormat = kSourceFormatARGB8;
    162             else
    163                 srcDataFormat = srcByteOrder16Big ? kSourceFormatARGB16Big : kSourceFormatARGB16Little;
    164             break;
    165         default:
    166             return false;
    167         }
     169        alphaFormat = AlphaFormatFirst;
    168170        break;
    169171    case kCGImageAlphaPremultipliedLast:
     
    173175        if (!premultiplyAlpha)
    174176            neededAlphaOp = kAlphaDoUnmultiply;
    175         switch (componentsPerPixel) {
    176         case 2:
    177             if (bitsPerComponent == 8)
    178                 srcDataFormat = kSourceFormatRA8;
    179             else
    180                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRA16Big : kSourceFormatRA16Little;
    181             break;
    182         case 4:
    183             if (bitsPerComponent == 8)
    184                 srcDataFormat = kSourceFormatRGBA8;
    185             else
    186                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRGBA16Big : kSourceFormatRGBA16Little;
    187             break;
    188         default:
    189             return false;
    190         }
     177        alphaFormat = AlphaFormatLast;
    191178        break;
    192179    case kCGImageAlphaLast:
    193180        if (premultiplyAlpha)
    194181            neededAlphaOp = kAlphaDoPremultiply;
    195         switch (componentsPerPixel) {
    196         case 1:
    197             if (bitsPerComponent == 8)
    198                 srcDataFormat = kSourceFormatA8;
    199             else
    200                 srcDataFormat = srcByteOrder16Big ? kSourceFormatA16Big :  kSourceFormatA16Little;
    201             break;
    202         case 2:
    203             if (bitsPerComponent == 8)
    204                 srcDataFormat = kSourceFormatRA8;
    205             else
    206                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRA16Big :  kSourceFormatRA16Little;
    207             break;
    208         case 4:
    209             if (bitsPerComponent == 8)
    210                 srcDataFormat = kSourceFormatRGBA8;
    211             else
    212                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRGBA16Big : kSourceFormatRGBA16Little;
    213             break;
    214         default:
    215             return false;
    216         }
     182        alphaFormat = AlphaFormatLast;
    217183        break;
    218184    case kCGImageAlphaNoneSkipLast:
    219         switch (componentsPerPixel) {
    220         case 2:
    221             if (bitsPerComponent == 8)
    222                 srcDataFormat = kSourceFormatRA8;
    223             else
    224                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRA16Big : kSourceFormatRA16Little;
    225             break;
    226         case 4:
    227             if (bitsPerComponent == 8)
    228                 srcDataFormat = kSourceFormatRGBA8;
    229             else
    230                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRGBA16Big :  kSourceFormatRGBA16Little;
    231             break;
    232         default:
    233             return false;
    234         }
     185        alphaFormat = AlphaFormatLast;
    235186        break;
    236187    case kCGImageAlphaNone:
    237         switch (componentsPerPixel) {
    238         case 1:
    239             if (bitsPerComponent == 8)
    240                 srcDataFormat = kSourceFormatR8;
    241             else
    242                 srcDataFormat = srcByteOrder16Big ? kSourceFormatR16Big : kSourceFormatR16Little;
    243             break;
    244         case 3:
    245             if (bitsPerComponent == 8)
    246                 srcDataFormat = kSourceFormatRGB8;
    247             else
    248                 srcDataFormat = srcByteOrder16Big ? kSourceFormatRGB16Big : kSourceFormatRGB16Little;
    249             break;
    250         default:
    251             return false;
    252         }
     188        alphaFormat = AlphaFormatNone;
    253189        break;
    254190    default:
    255191        return false;
    256192    }
     193    SourceDataFormat srcDataFormat = getSourceDataFormat(componentsPerPixel, alphaFormat, bitsPerComponent == 16, srcByteOrder16Big);
     194    if (srcDataFormat == kSourceFormatNumFormats)
     195        return false;
     196
    257197    RetainPtr<CFDataRef> pixelData;
    258198    pixelData.adoptCF(CGDataProviderCopyData(CGImageGetDataProvider(cgImage)));
Note: See TracChangeset for help on using the changeset viewer.