2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "GraphicsContext.h"
23 #include "AffineTransform.h"
25 #include "NotImplemented.h"
26 #include "PainterOpenVG.h"
27 #include "SurfaceOpenVG.h"
29 #include <wtf/Assertions.h>
30 #include <wtf/MathExtras.h>
31 #include <wtf/UnusedParam.h>
32 #include <wtf/Vector.h>
35 #include "EGLDisplayOpenVG.h"
42 // typedef'ing doesn't work, let's inherit from PainterOpenVG instead
43 class GraphicsContextPlatformPrivate : public PainterOpenVG {
45 GraphicsContextPlatformPrivate(SurfaceOpenVG* surface)
46 : PainterOpenVG(surface)
51 void GraphicsContext::platformInit(SurfaceOpenVG* surface)
53 m_data = surface ? new GraphicsContextPlatformPrivate(surface) : 0;
54 setPaintingDisabled(!surface);
57 void GraphicsContext::platformDestroy()
62 PlatformGraphicsContext* GraphicsContext::platformContext() const
64 if (paintingDisabled())
67 return m_data->baseSurface();
70 AffineTransform GraphicsContext::getCTM() const
72 if (paintingDisabled())
73 return AffineTransform();
75 return m_data->transformation();
78 void GraphicsContext::savePlatformState()
80 if (paintingDisabled())
86 void GraphicsContext::restorePlatformState()
88 if (paintingDisabled())
94 void GraphicsContext::drawRect(const IntRect& rect)
96 if (paintingDisabled())
99 m_data->drawRect(rect);
102 void GraphicsContext::drawLine(const IntPoint& from, const IntPoint& to)
104 if (paintingDisabled())
107 m_data->drawLine(from, to);
111 * Draw the largest ellipse that fits into the given rectangle.
113 void GraphicsContext::drawEllipse(const IntRect& rect)
115 if (paintingDisabled())
118 m_data->drawEllipse(rect);
121 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
123 if (paintingDisabled())
126 m_data->drawArc(rect, startAngle, angleSpan, VG_STROKE_PATH);
129 void GraphicsContext::drawConvexPolygon(size_t numPoints, const FloatPoint* points, bool shouldAntialias)
131 if (paintingDisabled())
134 m_data->drawPolygon(numPoints, points);
136 UNUSED_PARAM(shouldAntialias); // FIXME
139 void GraphicsContext::fillPath(const Path& path)
141 if (paintingDisabled())
144 m_data->drawPath(path, VG_FILL_PATH, m_state.fillRule);
147 void GraphicsContext::strokePath(const Path& path)
149 if (paintingDisabled())
152 m_data->drawPath(path, VG_STROKE_PATH, m_state.fillRule);
155 void GraphicsContext::fillRect(const FloatRect& rect)
157 if (paintingDisabled())
160 m_data->drawRect(rect, VG_FILL_PATH);
163 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
165 if (paintingDisabled())
168 Color oldColor = m_data->fillColor();
169 m_data->setFillColor(color);
170 m_data->drawRect(rect, VG_FILL_PATH);
171 m_data->setFillColor(oldColor);
173 UNUSED_PARAM(colorSpace); // FIXME
176 void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
178 if (paintingDisabled())
181 Color oldColor = m_data->fillColor();
182 m_data->setFillColor(color);
183 m_data->drawRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight, VG_FILL_PATH);
184 m_data->setFillColor(oldColor);
186 UNUSED_PARAM(colorSpace); // FIXME
189 void GraphicsContext::clip(const FloatRect& rect)
191 if (paintingDisabled())
194 m_data->intersectClipRect(rect);
197 void GraphicsContext::clipPath(const Path& path, WindRule clipRule)
199 if (paintingDisabled())
202 m_data->clipPath(path, PainterOpenVG::IntersectClip, clipRule);
205 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int offset, const Color& color)
207 if (paintingDisabled())
213 // FIXME: We just unite all focus ring rects into one for now.
214 // We should outline the edge of the full region.
215 offset += (width - 1) / 2;
216 IntRect finalFocusRect;
218 for (unsigned i = 0; i < rects.size(); i++) {
219 IntRect focusRect = rects[i];
220 focusRect.inflate(offset);
221 finalFocusRect.unite(focusRect);
224 StrokeStyle oldStyle = m_data->strokeStyle();
225 Color oldStrokeColor = m_data->strokeColor();
226 m_data->setStrokeStyle(DashedStroke);
227 m_data->setStrokeColor(color);
228 strokeRect(FloatRect(finalFocusRect), 1.f);
229 m_data->setStrokeStyle(oldStyle);
230 m_data->setStrokeColor(oldStrokeColor);
233 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
235 if (paintingDisabled())
241 StrokeStyle oldStyle = m_data->strokeStyle();
242 m_data->setStrokeStyle(SolidStroke);
243 drawLine(origin, origin + IntSize(width, 0));
244 m_data->setStrokeStyle(oldStyle);
246 UNUSED_PARAM(printing);
249 void GraphicsContext::drawLineForTextChecking(const IntPoint& origin, int width, TextCheckingLineStyle style)
251 if (paintingDisabled())
255 UNUSED_PARAM(origin);
260 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect, RoundingMode)
262 if (paintingDisabled())
265 return FloatRect(enclosingIntRect(m_data->transformation().mapRect(rect)));
268 void GraphicsContext::setPlatformShadow(const FloatSize& size, float blur, const Color& color, ColorSpace colorSpace)
270 if (paintingDisabled())
277 UNUSED_PARAM(colorSpace);
280 void GraphicsContext::clearPlatformShadow()
282 if (paintingDisabled())
288 void GraphicsContext::beginTransparencyLayer(float opacity)
290 if (paintingDisabled())
294 UNUSED_PARAM(opacity);
297 void GraphicsContext::endTransparencyLayer()
299 if (paintingDisabled())
305 void GraphicsContext::clearRect(const FloatRect& rect)
307 if (paintingDisabled())
310 CompositeOperator op = m_data->compositeOperation();
311 m_data->setCompositeOperation(CompositeClear);
312 m_data->drawRect(rect, VG_FILL_PATH);
313 m_data->setCompositeOperation(op);
316 void GraphicsContext::strokeRect(const FloatRect& rect, float lineWidth)
318 if (paintingDisabled())
321 float oldThickness = m_data->strokeThickness();
322 m_data->setStrokeThickness(lineWidth);
323 m_data->drawRect(rect, VG_STROKE_PATH);
324 m_data->setStrokeThickness(oldThickness);
327 void GraphicsContext::setLineCap(LineCap lc)
329 if (paintingDisabled())
332 m_data->setLineCap(lc);
335 void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
337 if (paintingDisabled())
340 m_data->setLineDash(dashes, dashOffset);
343 void GraphicsContext::setLineJoin(LineJoin lj)
345 if (paintingDisabled())
348 m_data->setLineJoin(lj);
351 void GraphicsContext::setMiterLimit(float limit)
353 if (paintingDisabled())
356 m_data->setMiterLimit(limit);
359 void GraphicsContext::setAlpha(float opacity)
361 if (paintingDisabled())
364 m_data->setOpacity(opacity);
367 void GraphicsContext::setPlatformCompositeOperation(CompositeOperator op)
369 if (paintingDisabled())
372 m_data->setCompositeOperation(op);
375 void GraphicsContext::clip(const Path& path)
377 if (paintingDisabled())
380 m_data->clipPath(path, PainterOpenVG::IntersectClip, m_state.fillRule);
383 void GraphicsContext::canvasClip(const Path& path)
388 void GraphicsContext::clipOut(const Path& path)
390 if (paintingDisabled())
393 m_data->clipPath(path, PainterOpenVG::SubtractClip, m_state.fillRule);
396 void GraphicsContext::scale(const FloatSize& scaleFactors)
398 if (paintingDisabled())
401 m_data->scale(scaleFactors);
404 void GraphicsContext::rotate(float radians)
406 if (paintingDisabled())
409 m_data->rotate(radians);
412 void GraphicsContext::translate(float dx, float dy)
414 if (paintingDisabled())
417 m_data->translate(dx, dy);
420 void GraphicsContext::clipOut(const IntRect& rect)
422 if (paintingDisabled())
427 m_data->clipPath(path, PainterOpenVG::SubtractClip, m_state.fillRule);
430 void GraphicsContext::clipToImageBuffer(const FloatRect& rect, const ImageBuffer* imageBuffer)
432 if (paintingDisabled())
437 UNUSED_PARAM(imageBuffer);
440 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
442 if (paintingDisabled())
446 path.addEllipse(rect);
447 path.addEllipse(FloatRect(rect.x() + thickness, rect.y() + thickness,
448 rect.width() - (thickness * 2), rect.height() - (thickness * 2)));
450 m_data->clipPath(path, PainterOpenVG::IntersectClip, m_state.fillRule);
453 void GraphicsContext::concatCTM(const AffineTransform& transformation)
455 if (paintingDisabled())
458 m_data->concatTransformation(transformation);
461 void GraphicsContext::setCTM(const AffineTransform& transformation)
463 if (paintingDisabled())
466 m_data->setTransformation(transformation);
469 void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
473 UNUSED_PARAM(destRect);
476 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
478 if (paintingDisabled())
481 m_data->setStrokeColor(color);
483 UNUSED_PARAM(colorSpace); // FIXME
486 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle strokeStyle)
488 if (paintingDisabled())
491 m_data->setStrokeStyle(strokeStyle);
494 void GraphicsContext::setPlatformStrokeThickness(float thickness)
496 if (paintingDisabled())
499 m_data->setStrokeThickness(thickness);
502 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
504 if (paintingDisabled())
507 m_data->setFillColor(color);
509 UNUSED_PARAM(colorSpace); // FIXME
512 void GraphicsContext::setPlatformShouldAntialias(bool enable)
514 if (paintingDisabled())
517 m_data->setAntialiasingEnabled(enable);
520 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
525 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
528 return InterpolationDefault;