OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2008 Apple Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "DragImage.h" | |
28 | |
29 #include "BitmapInfo.h" | |
30 #include "CachedImage.h" | |
31 #include "GraphicsContext.h" | |
32 #include "GraphicsContextPlatformPrivateCairo.h" | |
33 #include "Image.h" | |
34 #include "NativeImageCairo.h" | |
35 #include <wtf/RetainPtr.h> | |
36 #include <cairo-win32.h> | |
37 #include <windows.h> | |
38 | |
39 namespace WebCore { | |
40 | |
41 void deallocContext(PlatformContextCairo* target) | |
42 { | |
43 delete target; | |
44 } | |
45 | |
46 HBITMAP allocImage(HDC dc, IntSize size, PlatformContextCairo** targetRef) | |
47 { | |
48 BitmapInfo bmpInfo = BitmapInfo::create(size); | |
49 | |
50 LPVOID bits; | |
51 HBITMAP hbmp = CreateDIBSection(dc, &bmpInfo, DIB_RGB_COLORS, &bits, 0, 0); | |
52 | |
53 // At this point, we have a Cairo surface that points to a Windows DIB. The
DIB interprets | |
54 // with the opposite meaning of positive Y axis, so everything we draw into
this cairo | |
55 // context is going to be upside down. | |
56 if (!targetRef) | |
57 return hbmp; | |
58 | |
59 cairo_surface_t* bitmapContext = cairo_image_surface_create_for_data((unsign
ed char*)bits, | |
60 CAIRO_FORMAT_ARGB32, | |
61 bmpInfo.bmiHeader.biWidth, | |
62 bmpInfo.bmiHeader.biHeight, | |
63 bmpInfo.bmiHeader.biWidth * 4); | |
64 | |
65 if (!bitmapContext) { | |
66 DeleteObject(hbmp); | |
67 return 0; | |
68 } | |
69 | |
70 cairo_t* cr = cairo_create(bitmapContext); | |
71 cairo_surface_destroy(bitmapContext); | |
72 | |
73 // At this point, we have a Cairo surface that points to a Windows DIB. The
DIB interprets | |
74 // with the opposite meaning of positive Y axis, so everything we draw into
this cairo | |
75 // context is going to be upside down. | |
76 // | |
77 // So, we must invert the CTM for the context so that drawing commands will
be flipped | |
78 // before they get written to the internal buffer. | |
79 cairo_matrix_t matrix; | |
80 cairo_matrix_init(&matrix, 1.0, 0.0, 0.0, -1.0, 0.0, size.height()); | |
81 cairo_set_matrix(cr, &matrix); | |
82 | |
83 *targetRef = new PlatformGraphicsContext(cr); | |
84 cairo_destroy(cr); | |
85 | |
86 return hbmp; | |
87 } | |
88 | |
89 static cairo_surface_t* createCairoContextFromBitmap(HBITMAP bitmap) | |
90 { | |
91 BITMAP info; | |
92 GetObject(bitmap, sizeof(info), &info); | |
93 ASSERT(info.bmBitsPixel == 32); | |
94 | |
95 // At this point, we have a Cairo surface that points to a Windows BITMAP.
The BITMAP | |
96 // has the opposite meaning of positive Y axis, so everything we draw into t
his cairo | |
97 // context is going to be upside down. | |
98 return cairo_image_surface_create_for_data((unsigned char*)info.bmBits, | |
99 CAIRO_FORMAT_ARGB32, | |
100 info.bmWidth, | |
101 info.bmHeight, | |
102 info.bmWidthBytes); | |
103 } | |
104 | |
105 DragImageRef scaleDragImage(DragImageRef image, FloatSize scale) | |
106 { | |
107 // FIXME: due to the way drag images are done on windows we need | |
108 // to preprocess the alpha channel <rdar://problem/5015946> | |
109 if (!image) | |
110 return 0; | |
111 | |
112 IntSize srcSize = dragImageSize(image); | |
113 IntSize dstSize(static_cast<int>(srcSize.width() * scale.width()), static_ca
st<int>(srcSize.height() * scale.height())); | |
114 | |
115 HBITMAP hbmp = 0; | |
116 HDC dc = GetDC(0); | |
117 HDC dstDC = CreateCompatibleDC(dc); | |
118 | |
119 if (!dstDC) | |
120 goto exit; | |
121 | |
122 PlatformContextCairo* targetContext; | |
123 hbmp = allocImage(dstDC, dstSize, &targetContext); | |
124 if (!hbmp) | |
125 goto exit; | |
126 | |
127 cairo_surface_t* srcImage = createCairoContextFromBitmap(image); | |
128 | |
129 // Scale the target surface to the new image size, and flip it | |
130 // so that when we set the srcImage as the surface it will draw | |
131 // right-side-up. | |
132 cairo_t* cr = targetContext->cr(); | |
133 cairo_translate(cr, 0, dstSize.height()); | |
134 cairo_scale(cr, scale.width(), -scale.height()); | |
135 cairo_set_source_surface(cr, srcImage, 0.0, 0.0); | |
136 | |
137 // Now we can paint and get the correct result | |
138 cairo_paint(cr); | |
139 | |
140 cairo_surface_destroy(srcImage); | |
141 deallocContext(targetContext); | |
142 ::DeleteObject(image); | |
143 image = 0; | |
144 | |
145 exit: | |
146 if (!hbmp) | |
147 hbmp = image; | |
148 if (dstDC) | |
149 DeleteDC(dstDC); | |
150 ReleaseDC(0, dc); | |
151 return hbmp; | |
152 } | |
153 | |
154 DragImageRef createDragImageFromImage(Image* img, RespectImageOrientationEnum) | |
155 { | |
156 HBITMAP hbmp = 0; | |
157 HDC dc = GetDC(0); | |
158 HDC workingDC = CreateCompatibleDC(dc); | |
159 if (!workingDC) | |
160 goto exit; | |
161 | |
162 PlatformContextCairo* drawContext = 0; | |
163 hbmp = allocImage(workingDC, img->size(), &drawContext); | |
164 if (!hbmp) | |
165 goto exit; | |
166 | |
167 if (!drawContext) { | |
168 ::DeleteObject(hbmp); | |
169 hbmp = 0; | |
170 } | |
171 | |
172 cairo_t* cr = drawContext->cr(); | |
173 cairo_set_source_rgb(cr, 1.0, 0.0, 1.0); | |
174 cairo_fill_preserve(cr); | |
175 | |
176 NativeImageCairo* srcNativeImage = img->nativeImageForCurrentFrame(); | |
177 cairo_surface_t* srcImage = (srcNativeImage) ? srcNativeImage->surface() : 0
; | |
178 | |
179 if (srcImage) { | |
180 // Draw the image. | |
181 cairo_set_source_surface(cr, srcImage, 0.0, 0.0); | |
182 cairo_paint(cr); | |
183 } | |
184 | |
185 deallocContext(drawContext); | |
186 | |
187 exit: | |
188 if (workingDC) | |
189 DeleteDC(workingDC); | |
190 ReleaseDC(0, dc); | |
191 return hbmp; | |
192 } | |
193 | |
194 } | |
OLD | NEW |