f4d7c17cc8211346ecc92c8dc5d983d927723e2a
[WebKit-https.git] / Source / WebCore / platform / graphics / blackberry / GraphicsContextBlackBerry.cpp
1 /*
2  * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "config.h"
19 #include "GraphicsContext.h"
20
21 #include "AffineTransform.h"
22 #include "Gradient.h"
23 #include "GraphicsContext3D.h"
24 #include "KURL.h"
25 #include "NotImplemented.h"
26 #include "TransformationMatrix.h"
27
28 #include <BlackBerryPlatformGraphicsContext.h>
29 #include <stdio.h>
30 #include <wtf/Assertions.h>
31 #include <wtf/MathExtras.h>
32 #include <wtf/UnusedParam.h>
33 #include <wtf/Vector.h>
34
35 namespace WebCore {
36
37 class GraphicsContextPlatformPrivate {
38 public:
39     GraphicsContextPlatformPrivate(PlatformGraphicsContext* platformContext)
40         : m_platformContext(platformContext)
41         , m_isComposited(false)
42     {
43     }
44
45     PlatformGraphicsContext* m_platformContext;
46     bool m_isComposited;
47 };
48
49 void GraphicsContext::platformInit(PlatformGraphicsContext* platformContext)
50 {
51     m_data = new GraphicsContextPlatformPrivate(platformContext);
52 }
53
54 void GraphicsContext::platformDestroy()
55 {
56     delete m_data;
57 }
58
59 PlatformGraphicsContext* GraphicsContext::platformContext() const
60 {
61     return m_data->m_platformContext;
62 }
63
64 void GraphicsContext::savePlatformState()
65 {
66     if (paintingDisabled())
67         return;
68
69     platformContext()->save();
70 }
71
72 void GraphicsContext::restorePlatformState()
73 {
74     if (paintingDisabled())
75         return;
76
77     platformContext()->restore();
78     platformContext()->setFillColor(m_state.fillColor.rgb());
79     if (hasShadow())
80         setPlatformShadow(m_state.shadowOffset, m_state.shadowBlur, m_state.shadowColor, m_state.shadowColorSpace);
81     else
82         clearPlatformShadow();
83     platformContext()->setStrokeColor(m_state.strokeColor.rgb());
84     platformContext()->setStrokeStyle(static_cast<BlackBerry::Platform::Graphics::StrokeStyle>(m_state.strokeStyle));
85     platformContext()->setStrokeThickness(m_state.strokeThickness);
86     platformContext()->setTextDrawingMode(m_state.textDrawingMode);
87 }
88
89 void GraphicsContext::setIsAcceleratedContext(bool accelerated)
90 {
91     m_data->m_isComposited = accelerated;
92 }
93
94 bool GraphicsContext::isAcceleratedContext() const
95 {
96     return m_data->m_isComposited;
97 }
98
99 AffineTransform GraphicsContext::getCTM(IncludeDeviceScale) const
100 {
101     if (paintingDisabled())
102         return AffineTransform();
103
104     AffineTransform result;
105     platformContext()->getTransform((double*)&result);
106     return result;
107 }
108
109 void GraphicsContext::concatCTM(const AffineTransform& transformation)
110 {
111     if (paintingDisabled())
112         return;
113
114     AffineTransform current;
115     platformContext()->getTransform((double*)&current);
116     current *= transformation;
117     platformContext()->setTransform((double*)&current);
118 }
119
120 void GraphicsContext::setCTM(const AffineTransform& transformation)
121 {
122     if (paintingDisabled())
123         return;
124
125     platformContext()->setTransform((const double*)&transformation);
126 }
127
128 void GraphicsContext::scale(const FloatSize& scaleFactors)
129 {
130     if (paintingDisabled())
131         return;
132
133     AffineTransform current;
134     platformContext()->getTransform((double*)&current);
135     current.scaleNonUniform(scaleFactors.width(), scaleFactors.height());
136     platformContext()->setTransform((double*)&current);
137 }
138
139 void GraphicsContext::rotate(float radians)
140 {
141     if (paintingDisabled())
142         return;
143
144     AffineTransform current;
145     platformContext()->getTransform((double*)&current);
146     current.rotate(rad2deg(radians));
147     platformContext()->setTransform((double*)&current);
148 }
149
150 void GraphicsContext::translate(float dx, float dy)
151 {
152     if (paintingDisabled())
153         return;
154
155     AffineTransform current;
156     platformContext()->getTransform((double*)&current);
157     current.translate(dx, dy);
158     platformContext()->setTransform((double*)&current);
159 }
160
161 void GraphicsContext::drawEllipse(const IntRect& rect)
162 {
163     if (paintingDisabled())
164         return;
165
166     platformContext()->addEllipse(FloatRect(rect));
167 }
168
169 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
170 {
171     if (paintingDisabled())
172         return;
173
174     platformContext()->addArc(rect, startAngle, angleSpan);
175 }
176
177 void GraphicsContext::drawConvexPolygon(size_t numPoints, const FloatPoint* points, bool shouldAntialias)
178 {
179     if (paintingDisabled())
180         return;
181
182     platformContext()->addConvexPolygon(numPoints, (const BlackBerry::Platform::FloatPoint*)points);
183 }
184
185 void GraphicsContext::drawRect(const IntRect& rect)
186 {
187     if (paintingDisabled())
188         return;
189
190     platformContext()->addDrawRect(FloatRect(rect));
191 }
192
193 void GraphicsContext::fillRect(const FloatRect& rect)
194 {
195     if (paintingDisabled())
196         return;
197
198     BlackBerry::Platform::Graphics::Gradient* platformGradient = fillGradient() ? fillGradient()->platformGradient() : 0;
199     BlackBerry::Platform::Graphics::Pattern* platformPattern = fillPattern() ? fillPattern()->platformPattern(AffineTransform()) : 0;
200     platformContext()->addFillRect(rect, platformGradient, platformPattern);
201 }
202
203 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
204 {
205     if (paintingDisabled())
206         return;
207
208     platformContext()->setFillColor(color.rgb());
209     platformContext()->addFillRect(rect);
210     platformContext()->setFillColor(m_state.fillColor.rgb());
211 }
212
213 void GraphicsContext::clearRect(const FloatRect& rect)
214 {
215     if (paintingDisabled())
216         return;
217
218     platformContext()->addClearRect(rect);
219 }
220
221 void GraphicsContext::strokeRect(const FloatRect& rect, float lineWidth)
222 {
223     if (paintingDisabled())
224         return;
225
226     if (strokeGradient() || strokePattern()) {
227         Path path;
228         path.addRect(rect);
229         float oldLineWidth = m_state.strokeThickness;
230         setPlatformStrokeThickness(lineWidth);
231         strokePath(path);
232         setPlatformStrokeThickness(oldLineWidth);
233     } else
234         platformContext()->addStrokeRect(rect, lineWidth);
235 }
236
237 void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
238 {
239     if (paintingDisabled())
240         return;
241
242     BlackBerry::Platform::FloatRoundedRect r = BlackBerry::Platform::FloatRoundedRect(FloatRect(rect), FloatSize(topLeft), FloatSize(topRight), FloatSize(bottomLeft), FloatSize(bottomRight));
243     platformContext()->setFillColor(color.rgb());
244     platformContext()->addRoundedRect(r);
245     platformContext()->setFillColor(m_state.fillColor.rgb());
246 }
247
248 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect, RoundingMode roundingMode)
249 {
250     return rect;
251 }
252
253 void GraphicsContext::setPlatformShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
254 {
255     if (paintingDisabled())
256         return;
257
258     FloatSize adjustedOffset = offset;
259
260     if (m_state.shadowsIgnoreTransforms) {
261         // Meaning that this graphics context is associated with a CanvasRenderingContext
262         // We flip the height since CG and HTML5 Canvas have opposite Y axis
263         adjustedOffset.setHeight(-offset.height());
264     }
265
266     // if there is an invalid shadow color, we're supposed to use Apple's CG default
267     platformContext()->setShadow(adjustedOffset, blur, color.isValid() ? color.rgb() : makeRGBA(0, 0, 0, 0xFF / 3), m_state.shadowsIgnoreTransforms);
268 }
269
270 void GraphicsContext::clearPlatformShadow()
271 {
272     if (paintingDisabled())
273         return;
274
275     platformContext()->clearShadow();
276 }
277
278 void GraphicsContext::beginPlatformTransparencyLayer(float opacity)
279 {
280     if (paintingDisabled())
281         return;
282
283     platformContext()->beginPlatformTransparencyLayer(opacity);
284 }
285
286 void GraphicsContext::endPlatformTransparencyLayer()
287 {
288     if (paintingDisabled())
289         return;
290
291     platformContext()->endPlatformTransparencyLayer();
292 }
293
294 bool GraphicsContext::supportsTransparencyLayers()
295 {
296     return true;
297 }
298
299 void GraphicsContext::setLineCap(LineCap lc)
300 {
301     if (paintingDisabled())
302         return;
303
304     platformContext()->setLineCap((BlackBerry::Platform::Graphics::LineCap)lc);
305 }
306
307 void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
308 {
309     if (paintingDisabled())
310         return;
311
312     platformContext()->setLineDash(dashes.data(), dashes.size(), dashOffset);
313 }
314
315 void GraphicsContext::setLineJoin(LineJoin lj)
316 {
317     if (paintingDisabled())
318         return;
319
320     platformContext()->setLineJoin((BlackBerry::Platform::Graphics::LineJoin)lj); // naming coincides
321 }
322
323 void GraphicsContext::setMiterLimit(float limit)
324 {
325     if (paintingDisabled())
326         return;
327
328     platformContext()->setMiterLimit(limit);
329 }
330
331 void GraphicsContext::setAlpha(float opacity)
332 {
333     if (paintingDisabled())
334         return;
335
336     platformContext()->setAlpha(opacity);
337 }
338
339
340 void GraphicsContext::clip(const FloatRect& rect)
341 {
342     if (paintingDisabled())
343         return;
344
345     platformContext()->clip(rect);
346 }
347
348 void GraphicsContext::clipOut(const IntRect& rect)
349 {
350     if (paintingDisabled())
351         return;
352
353     platformContext()->clipOut(FloatRect(rect));
354 }
355
356 void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialias)
357 {
358     if (paintingDisabled())
359         return;
360
361     platformContext()->clipConvexPolygon(numPoints, (const BlackBerry::Platform::FloatPoint*)points);
362 }
363
364 void GraphicsContext::addRoundedRectClip(const RoundedRect& rect)
365 {
366     if (paintingDisabled())
367         return;
368
369     BlackBerry::Platform::FloatRoundedRect r = BlackBerry::Platform::FloatRoundedRect(
370         FloatRect(rect.rect()),
371         FloatSize(rect.radii().topLeft()),
372         FloatSize(rect.radii().topRight()),
373         FloatSize(rect.radii().bottomLeft()),
374         FloatSize(rect.radii().bottomRight()));
375     platformContext()->clipRoundedRect(r);
376 }
377
378 void GraphicsContext::clipOutRoundedRect(const RoundedRect& rect)
379 {
380     if (paintingDisabled())
381         return;
382
383     BlackBerry::Platform::FloatRoundedRect r = BlackBerry::Platform::FloatRoundedRect(
384         FloatRect(rect.rect()),
385         FloatSize(rect.radii().topLeft()),
386         FloatSize(rect.radii().topRight()),
387         FloatSize(rect.radii().bottomLeft()),
388         FloatSize(rect.radii().bottomRight()));
389     platformContext()->clipOutRoundedRect(r);
390 }
391
392 IntRect GraphicsContext::clipBounds() const
393 {
394     // FIXME: return the max IntRect for now
395     return IntRect(IntPoint(INT_MIN / 2, INT_MIN / 2), IntSize(INT_MAX, INT_MAX));
396 }
397
398 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
399 {
400 }
401
402 void GraphicsContext::setPlatformTextDrawingMode(TextDrawingModeFlags mode)
403 {
404     if (paintingDisabled())
405         return;
406
407     platformContext()->setTextDrawingMode(mode);
408 }
409
410 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
411 {
412     if (paintingDisabled())
413         return;
414
415     platformContext()->setStrokeColor(color.rgb());
416 }
417
418 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle stroke)
419 {
420     if (paintingDisabled())
421         return;
422
423     platformContext()->setStrokeStyle((BlackBerry::Platform::Graphics::StrokeStyle)stroke);
424 }
425
426 void GraphicsContext::setPlatformStrokeThickness(float thickness)
427 {
428     if (paintingDisabled())
429         return;
430
431     platformContext()->setStrokeThickness(thickness);
432 }
433
434 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
435 {
436     if (paintingDisabled())
437         return;
438
439     platformContext()->setFillColor(color.rgb());
440 }
441
442 void GraphicsContext::setPlatformCompositeOperation(CompositeOperator op)
443 {
444     if (paintingDisabled())
445         return;
446
447     platformContext()->setCompositeOperation(static_cast<BlackBerry::Platform::Graphics::CompositeOperator>(op));
448 }
449
450 void GraphicsContext::setPlatformShouldAntialias(bool enable)
451 {
452     if (paintingDisabled())
453         return;
454
455     platformContext()->setUseAntialiasing(enable);
456 }
457
458 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality interpolationQuality)
459 {
460     platformContext()->setHighQualityFilter(interpolationQuality != InterpolationNone);
461 }
462
463 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
464 {
465     return platformContext()->highQualityFilter() ? InterpolationDefault : InterpolationNone;
466 }
467
468 }