b522532db40596c07267cd35b6653efc127196d7
[WebKit-https.git] / Source / WebCore / platform / graphics / win / ImageCairoWin.cpp
1 /*
2  * Copyright (C) 2008, 2013 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 "Image.h"
28 #include "BitmapImage.h"
29 #include "GraphicsContext.h"
30 #include "RefPtrCairo.h"
31 #include <cairo.h>
32 #include <cairo-win32.h>
33
34 #include <windows.h>
35 #include <wtf/text/WTFString.h>
36
37 namespace WebCore {
38
39 PassRefPtr<BitmapImage> BitmapImage::create(HBITMAP hBitmap)
40 {
41     DIBSECTION dibSection;
42     if (!GetObject(hBitmap, sizeof(DIBSECTION), &dibSection))
43         return 0;
44
45     ASSERT(dibSection.dsBm.bmBitsPixel == 32);
46     if (dibSection.dsBm.bmBitsPixel != 32)
47         return 0;
48
49     ASSERT(dibSection.dsBm.bmBits);
50     if (!dibSection.dsBm.bmBits)
51         return 0;
52
53     RefPtr<cairo_surface_t> surface = adoptRef(cairo_win32_surface_create_with_dib(CAIRO_FORMAT_ARGB32, dibSection.dsBm.bmWidth, dibSection.dsBm.bmHeight));
54
55     return BitmapImage::create(surface.release());
56 }
57
58 bool BitmapImage::getHBITMAPOfSize(HBITMAP bmp, const IntSize* size)
59 {
60     ASSERT(bmp);
61
62     BITMAP bmpInfo;
63     GetObject(bmp, sizeof(BITMAP), &bmpInfo);
64
65     // If this is a 32bpp bitmap, which it always should be, we'll clear it so alpha-wise it will be visible
66     if (bmpInfo.bmBitsPixel == 32 && bmpInfo.bmBits) {
67         int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
68         memset(bmpInfo.bmBits, 255, bufferSize);
69     }
70
71     cairo_surface_t* image = cairo_image_surface_create_for_data((unsigned char*)bmpInfo.bmBits,
72                                                CAIRO_FORMAT_ARGB32,
73                                                bmpInfo.bmWidth,
74                                                bmpInfo.bmHeight,
75                                                bmpInfo.bmWidthBytes);
76
77
78     cairo_t* targetRef = cairo_create(image);
79     cairo_surface_destroy(image);
80
81     GraphicsContext gc(targetRef);
82
83     IntSize imageSize = BitmapImage::size();
84     if (size)
85         drawFrameMatchingSourceSize(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), *size, ColorSpaceDeviceRGB, CompositeCopy);
86     else
87         draw(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), ColorSpaceDeviceRGB, CompositeCopy, BlendModeNormal, ImageOrientationDescription());
88
89     // Do cleanup
90     cairo_destroy(targetRef);
91
92     return true;
93 }
94
95 void BitmapImage::drawFrameMatchingSourceSize(GraphicsContext* ctxt, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator compositeOp)
96 {
97     size_t frames = frameCount();
98     for (size_t i = 0; i < frames; ++i) {
99         RefPtr<cairo_surface_t> surface = frameAtIndex(i);
100         if (!surface)
101             continue;
102
103         if (cairo_image_surface_get_height(surface.get()) == static_cast<size_t>(srcSize.height()) && cairo_image_surface_get_width(surface.get()) == static_cast<size_t>(srcSize.width())) {
104             size_t currentFrame = m_currentFrame;
105             m_currentFrame = i;
106             draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, srcSize.width(), srcSize.height()), ColorSpaceDeviceRGB, compositeOp, BlendModeNormal, ImageOrientationDescription());
107             m_currentFrame = currentFrame;
108             return;
109         }
110     }
111
112     // No image of the correct size was found, fallback to drawing the current frame
113     IntSize imageSize = BitmapImage::size();
114     draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), ColorSpaceDeviceRGB, compositeOp, BlendModeNormal, ImageOrientationDescription());
115 }
116
117 } // namespace WebCore