OSDN Git Service

Merge "Updated the script to allow for chosing the mode (list files vs. list folders)."
[android-x86/external-webkit.git] / WebCore / platform / graphics / cg / GraphicsContext3DCG.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28
29 #if ENABLE(3D_CANVAS)
30
31 #include "GraphicsContext3D.h"
32
33 #include "Image.h"
34
35 #include <CoreGraphics/CGBitmapContext.h>
36 #include <CoreGraphics/CGContext.h>
37 #include <CoreGraphics/CGImage.h>
38
39 #include <wtf/RetainPtr.h>
40
41 namespace WebCore {
42
43 bool GraphicsContext3D::getImageData(Image* image,
44                                      unsigned int format,
45                                      unsigned int type,
46                                      bool premultiplyAlpha,
47                                      Vector<uint8_t>& outputVector)
48 {
49     if (!image)
50         return false;
51     CGImageRef cgImage = image->nativeImageForCurrentFrame();
52     if (!cgImage)
53         return false;
54     int width = CGImageGetWidth(cgImage);
55     int height = CGImageGetHeight(cgImage);
56     // FIXME: we should get rid of this temporary copy where possible.
57     int tempRowBytes = width * 4;
58     Vector<uint8_t> tempVector;
59     tempVector.resize(height * tempRowBytes);
60     // Try to reuse the color space from the image to preserve its colors.
61     // Some images use a color space (such as indexed) unsupported by the bitmap context.
62     CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
63     bool releaseColorSpace = false;
64     CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
65     switch (colorSpaceModel) {
66     case kCGColorSpaceModelMonochrome:
67     case kCGColorSpaceModelRGB:
68     case kCGColorSpaceModelCMYK:
69     case kCGColorSpaceModelLab:
70     case kCGColorSpaceModelDeviceN:
71         break;
72     default:
73         colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
74         releaseColorSpace = true;
75         break;
76     }
77     CGContextRef tempContext = CGBitmapContextCreate(tempVector.data(),
78                                                      width, height, 8, tempRowBytes,
79                                                      colorSpace,
80                                                      // FIXME: change this!
81                                                      kCGImageAlphaPremultipliedLast);
82     if (releaseColorSpace)
83         CGColorSpaceRelease(colorSpace);
84     if (!tempContext)
85         return false;
86     CGContextSetBlendMode(tempContext, kCGBlendModeCopy);
87     CGContextDrawImage(tempContext,
88                        CGRectMake(0, 0, static_cast<CGFloat>(width), static_cast<CGFloat>(height)),
89                        cgImage);
90     CGContextRelease(tempContext);
91     // Pack the pixel data into the output vector.
92     unsigned long componentsPerPixel, bytesPerComponent;
93     if (!computeFormatAndTypeParameters(format, type, &componentsPerPixel, &bytesPerComponent))
94         return false;
95     int rowBytes = width * componentsPerPixel * bytesPerComponent;
96     outputVector.resize(height * rowBytes);
97     CGImageAlphaInfo info = CGImageGetAlphaInfo(cgImage);
98     bool hasAlphaChannel = (info != kCGImageAlphaNone
99                             && info != kCGImageAlphaNoneSkipLast
100                             && info != kCGImageAlphaNoneSkipFirst);
101     AlphaOp neededAlphaOp = kAlphaDoNothing;
102     if (!premultiplyAlpha && hasAlphaChannel)
103         // FIXME: must fetch the image data before the premultiplication step.
104         neededAlphaOp = kAlphaDoUnmultiply;
105     return packPixels(tempVector.data(), kSourceFormatRGBA8, width, height, 0,
106                       format, type, neededAlphaOp, outputVector.data());
107 }
108
109 void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, CGContextRef context)
110 {
111     if (!imagePixels || imageWidth <= 0 || imageHeight <= 0 || canvasWidth <= 0 || canvasHeight <= 0 || !context)
112         return;
113     int rowBytes = imageWidth * 4;
114     RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithData(0, imagePixels, rowBytes * imageHeight, 0));
115     RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
116     RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(imageWidth, imageHeight, 8, 32, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
117         dataProvider.get(), 0, false, kCGRenderingIntentDefault));
118     // CSS styling may cause the canvas's content to be resized on
119     // the page. Go back to the Canvas to figure out the correct
120     // width and height to draw.
121     CGRect rect = CGRectMake(0, 0, canvasWidth, canvasHeight);
122     // We want to completely overwrite the previous frame's
123     // rendering results.
124     CGContextSaveGState(context);
125     CGContextSetBlendMode(context, kCGBlendModeCopy);
126     CGContextSetInterpolationQuality(context, kCGInterpolationNone);
127     CGContextDrawImage(context, rect, cgImage.get());
128     CGContextRestoreGState(context);
129 }
130
131 } // namespace WebCore
132
133 #endif // ENABLE(3D_CANVAS)