OSDN Git Service

am 71d8d800: (-s ours) am 1441f89d: Do not merge -- restrict text selection to the...
[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/CGDataProvider.h>
38 #include <CoreGraphics/CGImage.h>
39
40 #include <wtf/RetainPtr.h>
41
42 namespace WebCore {
43
44 bool GraphicsContext3D::getImageData(Image* image,
45                                      unsigned int format,
46                                      unsigned int type,
47                                      bool premultiplyAlpha,
48                                      Vector<uint8_t>& outputVector)
49 {
50     if (!image)
51         return false;
52     CGImageRef cgImage;
53     RetainPtr<CGImageRef> decodedImage;
54     if (image->data()) {
55         ImageSource decoder(false);
56         decoder.setData(image->data(), true);
57         if (!decoder.frameCount())
58             return false;
59         decodedImage = decoder.createFrameAtIndex(0);
60         cgImage = decodedImage.get();
61     } else
62         cgImage = image->nativeImageForCurrentFrame();
63     if (!cgImage)
64         return false;
65     size_t width = CGImageGetWidth(cgImage);
66     size_t height = CGImageGetHeight(cgImage);
67     if (!width || !height || CGImageGetBitsPerComponent(cgImage) != 8)
68         return false;
69     size_t componentsPerPixel = CGImageGetBitsPerPixel(cgImage) / 8;
70     SourceDataFormat srcDataFormat = kSourceFormatRGBA8;
71     AlphaOp neededAlphaOp = kAlphaDoNothing;
72     switch (CGImageGetAlphaInfo(cgImage)) {
73     case kCGImageAlphaPremultipliedFirst:
74     case kCGImageAlphaFirst:
75     case kCGImageAlphaNoneSkipFirst:
76         return false;
77     case kCGImageAlphaPremultipliedLast:
78         // This is a special case for texImage2D with HTMLCanvasElement input,
79         // in which case image->data() should be null.
80         ASSERT(!image->data());
81         if (!premultiplyAlpha)
82             neededAlphaOp = kAlphaDoUnmultiply;
83         switch (componentsPerPixel) {
84         case 2:
85             srcDataFormat = kSourceFormatRA8;
86             break;
87         case 4:
88             srcDataFormat = kSourceFormatRGBA8;
89             break;
90         default:
91             return false;
92         }
93         break;
94     case kCGImageAlphaLast:
95         if (premultiplyAlpha)
96             neededAlphaOp = kAlphaDoPremultiply;
97         switch (componentsPerPixel) {
98         case 1:
99             srcDataFormat = kSourceFormatA8;
100             break;
101         case 2:
102             srcDataFormat = kSourceFormatRA8;
103             break;
104         case 4:
105             srcDataFormat = kSourceFormatRGBA8;
106             break;
107         default:
108             return false;
109         }
110         break;
111     case kCGImageAlphaNoneSkipLast:
112         switch (componentsPerPixel) {
113         case 2:
114             srcDataFormat = kSourceFormatRA8;
115             break;
116         case 4:
117             srcDataFormat = kSourceFormatRGBA8;
118             break;
119         default:
120             return false;
121         }
122     case kCGImageAlphaNone:
123         switch (componentsPerPixel) {
124         case 1:
125             srcDataFormat = kSourceFormatR8;
126             break;
127         case 3:
128             srcDataFormat = kSourceFormatRGB8;
129             break;
130         default:
131             return false;
132         }
133         break;
134     default:
135         return false;
136     }
137     RetainPtr<CFDataRef> pixelData;
138     pixelData.adoptCF(CGDataProviderCopyData(CGImageGetDataProvider(cgImage)));
139     if (!pixelData)
140         return false;
141     const UInt8* rgba = CFDataGetBytePtr(pixelData.get());
142     outputVector.resize(width * height * 4);
143     unsigned int srcUnpackAlignment = 0;
144     size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
145     unsigned int padding = bytesPerRow - componentsPerPixel * width;
146     if (padding) {
147         srcUnpackAlignment = padding + 1;
148         while (bytesPerRow % srcUnpackAlignment)
149             ++srcUnpackAlignment;
150     }
151     bool rt = packPixels(rgba, srcDataFormat, width, height, srcUnpackAlignment,
152                          format, type, neededAlphaOp, outputVector.data());
153     return rt;
154 }
155
156 void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, CGContextRef context)
157 {
158     if (!imagePixels || imageWidth <= 0 || imageHeight <= 0 || canvasWidth <= 0 || canvasHeight <= 0 || !context)
159         return;
160     int rowBytes = imageWidth * 4;
161     RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithData(0, imagePixels, rowBytes * imageHeight, 0));
162     RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
163     RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(imageWidth, imageHeight, 8, 32, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
164         dataProvider.get(), 0, false, kCGRenderingIntentDefault));
165     // CSS styling may cause the canvas's content to be resized on
166     // the page. Go back to the Canvas to figure out the correct
167     // width and height to draw.
168     CGRect rect = CGRectMake(0, 0, canvasWidth, canvasHeight);
169     // We want to completely overwrite the previous frame's
170     // rendering results.
171     CGContextSaveGState(context);
172     CGContextSetBlendMode(context, kCGBlendModeCopy);
173     CGContextSetInterpolationQuality(context, kCGInterpolationNone);
174     CGContextDrawImage(context, rect, cgImage.get());
175     CGContextRestoreGState(context);
176 }
177
178 } // namespace WebCore
179
180 #endif // ENABLE(3D_CANVAS)