1 /*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29 #include "BitmapImage.h"
30
31 #if USE(CAIRO)
32
33 #include "AffineTransform.h"
34 #include "CairoUtilities.h"
35 #include "Color.h"
36 #include "ContextShadow.h"
37 #include "FloatRect.h"
38 #include "GraphicsContext.h"
39 #include "PlatformContextCairo.h"
40 #include "ImageBuffer.h"
41 #include "ImageObserver.h"
42 #include "RefPtrCairo.h"
43 #include <cairo.h>
44 #include <math.h>
45 #include <wtf/OwnPtr.h>
46
47 namespace WebCore {
48
clear(bool clearMetadata)49 bool FrameData::clear(bool clearMetadata)
50 {
51 if (clearMetadata)
52 m_haveMetadata = false;
53
54 if (m_frame) {
55 cairo_surface_destroy(m_frame);
56 m_frame = 0;
57 return true;
58 }
59 return false;
60 }
61
BitmapImage(cairo_surface_t * surface,ImageObserver * observer)62 BitmapImage::BitmapImage(cairo_surface_t* surface, ImageObserver* observer)
63 : Image(observer)
64 , m_currentFrame(0)
65 , m_frames(0)
66 , m_frameTimer(0)
67 , m_repetitionCount(cAnimationNone)
68 , m_repetitionCountStatus(Unknown)
69 , m_repetitionsComplete(0)
70 , m_isSolidColor(false)
71 , m_checkedForSolidColor(false)
72 , m_animationFinished(true)
73 , m_allDataReceived(true)
74 , m_haveSize(true)
75 , m_sizeAvailable(true)
76 , m_decodedSize(0)
77 , m_haveFrameCount(true)
78 , m_frameCount(1)
79 {
80 initPlatformData();
81
82 // TODO: check to be sure this is an image surface
83
84 int width = cairo_image_surface_get_width(surface);
85 int height = cairo_image_surface_get_height(surface);
86 m_decodedSize = width * height * 4;
87 m_size = IntSize(width, height);
88
89 m_frames.grow(1);
90 m_frames[0].m_frame = surface;
91 m_frames[0].m_hasAlpha = cairo_surface_get_content(surface) != CAIRO_CONTENT_COLOR;
92 m_frames[0].m_haveMetadata = true;
93 checkForSolidColor();
94 }
95
draw(GraphicsContext * context,const FloatRect & dst,const FloatRect & src,ColorSpace styleColorSpace,CompositeOperator op)96 void BitmapImage::draw(GraphicsContext* context, const FloatRect& dst, const FloatRect& src, ColorSpace styleColorSpace, CompositeOperator op)
97 {
98 FloatRect srcRect(src);
99 FloatRect dstRect(dst);
100
101 if (dstRect.width() == 0.0f || dstRect.height() == 0.0f ||
102 srcRect.width() == 0.0f || srcRect.height() == 0.0f)
103 return;
104
105 startAnimation();
106
107 cairo_surface_t* image = frameAtIndex(m_currentFrame);
108 if (!image) // If it's too early we won't have an image yet.
109 return;
110
111 if (mayFillWithSolidColor()) {
112 fillWithSolidColor(context, dstRect, solidColor(), styleColorSpace, op);
113 return;
114 }
115
116 IntSize selfSize = size();
117
118 cairo_t* cr = context->platformContext()->cr();
119 context->save();
120
121 // Set the compositing operation.
122 if (op == CompositeSourceOver && !frameHasAlphaAtIndex(m_currentFrame))
123 context->setCompositeOperation(CompositeCopy);
124 else
125 context->setCompositeOperation(op);
126
127 // If we're drawing a sub portion of the image or scaling then create
128 // a pattern transformation on the image and draw the transformed pattern.
129 // Test using example site at http://www.meyerweb.com/eric/css/edge/complexspiral/demo.html
130 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(image);
131
132 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
133
134 float scaleX = srcRect.width() / dstRect.width();
135 float scaleY = srcRect.height() / dstRect.height();
136 cairo_matrix_t matrix = { scaleX, 0, 0, scaleY, srcRect.x(), srcRect.y() };
137 cairo_pattern_set_matrix(pattern, &matrix);
138
139 ContextShadow* shadow = context->contextShadow();
140 ASSERT(shadow);
141 if (shadow->m_type != ContextShadow::NoShadow) {
142 cairo_t* shadowContext = shadow->beginShadowLayer(context, dstRect);
143 if (shadowContext) {
144 cairo_translate(shadowContext, dstRect.x(), dstRect.y());
145 cairo_set_source(shadowContext, pattern);
146 cairo_rectangle(shadowContext, 0, 0, dstRect.width(), dstRect.height());
147 cairo_fill(shadowContext);
148 shadow->endShadowLayer(context);
149 }
150 }
151
152 // Draw the image.
153 cairo_translate(cr, dstRect.x(), dstRect.y());
154 cairo_set_source(cr, pattern);
155 cairo_pattern_destroy(pattern);
156 cairo_rectangle(cr, 0, 0, dstRect.width(), dstRect.height());
157 cairo_clip(cr);
158 cairo_paint_with_alpha(cr, context->getAlpha());
159
160 context->restore();
161
162 if (imageObserver())
163 imageObserver()->didDraw(this);
164 }
165
drawPattern(GraphicsContext * context,const FloatRect & tileRect,const AffineTransform & patternTransform,const FloatPoint & phase,ColorSpace colorSpace,CompositeOperator op,const FloatRect & destRect)166 void Image::drawPattern(GraphicsContext* context, const FloatRect& tileRect, const AffineTransform& patternTransform,
167 const FloatPoint& phase, ColorSpace colorSpace, CompositeOperator op, const FloatRect& destRect)
168 {
169 cairo_surface_t* image = nativeImageForCurrentFrame();
170 if (!image) // If it's too early we won't have an image yet.
171 return;
172
173 cairo_t* cr = context->platformContext()->cr();
174 drawPatternToCairoContext(cr, image, size(), tileRect, patternTransform, phase, toCairoOperator(op), destRect);
175
176 if (imageObserver())
177 imageObserver()->didDraw(this);
178 }
179
checkForSolidColor()180 void BitmapImage::checkForSolidColor()
181 {
182 m_isSolidColor = false;
183 m_checkedForSolidColor = true;
184
185 if (frameCount() > 1)
186 return;
187
188 cairo_surface_t* frameSurface = frameAtIndex(0);
189 if (!frameSurface)
190 return;
191
192 ASSERT(cairo_surface_get_type(frameSurface) == CAIRO_SURFACE_TYPE_IMAGE);
193
194 int width = cairo_image_surface_get_width(frameSurface);
195 int height = cairo_image_surface_get_height(frameSurface);
196
197 if (width != 1 || height != 1)
198 return;
199
200 unsigned* pixelColor = reinterpret_cast<unsigned*>(cairo_image_surface_get_data(frameSurface));
201 m_solidColor = colorFromPremultipliedARGB(*pixelColor);
202
203 m_isSolidColor = true;
204 }
205
206 }
207
208 #endif // USE(CAIRO)
209