OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / platform / graphics / GraphicsContext.h
1 /*
2  * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2008-2009 Torch Mobile, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef GraphicsContext_h
28 #define GraphicsContext_h
29
30 #include "ColorSpace.h"
31 #include "DashArray.h"
32 #include "FloatRect.h"
33 #include "Image.h"
34 #include "IntRect.h"
35 #include "Path.h"
36 #include "TextDirection.h"
37 #include <wtf/Noncopyable.h>
38 #include <wtf/PassOwnPtr.h>
39
40 #if PLATFORM(CG)
41 typedef struct CGContext PlatformGraphicsContext;
42 #elif PLATFORM(CAIRO)
43 namespace WebCore {
44 class ContextShadow;
45 }
46 typedef struct _cairo PlatformGraphicsContext;
47 #elif PLATFORM(OPENVG)
48 namespace WebCore {
49 class SurfaceOpenVG;
50 }
51 typedef class WebCore::SurfaceOpenVG PlatformGraphicsContext;
52 #elif PLATFORM(QT)
53 #include <QPainter>
54 namespace WebCore {
55 class ContextShadow;
56 }
57 typedef QPainter PlatformGraphicsContext;
58 #elif PLATFORM(WX)
59 class wxGCDC;
60 class wxWindowDC;
61
62 // wxGraphicsContext allows us to support Path, etc.
63 // but on some platforms, e.g. Linux, it requires fairly
64 // new software.
65 #if USE(WXGC)
66 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
67 // the linker happy.
68 #ifdef __APPLE__
69     class wxDC;
70     typedef wxDC PlatformGraphicsContext;
71 #else
72     typedef wxGCDC PlatformGraphicsContext;
73 #endif
74 #else
75     typedef wxWindowDC PlatformGraphicsContext;
76 #endif
77 #elif PLATFORM(SKIA)
78 #if PLATFORM(ANDROID)
79 namespace WebCore {
80 class PlatformGraphicsContext;
81 }
82 class SkPaint;
83 struct SkPoint;
84 #else
85 namespace WebCore {
86 class PlatformContextSkia;
87 }
88 typedef WebCore::PlatformContextSkia PlatformGraphicsContext;
89 #endif
90 #elif PLATFORM(HAIKU)
91 class BView;
92 typedef BView PlatformGraphicsContext;
93 struct pattern;
94 #elif OS(WINCE)
95 typedef struct HDC__ PlatformGraphicsContext;
96 #else
97 typedef void PlatformGraphicsContext;
98 #endif
99
100 #if PLATFORM(GTK)
101 typedef struct _GdkDrawable GdkDrawable;
102 typedef struct _GdkEventExpose GdkEventExpose;
103 #endif
104
105 #if PLATFORM(WIN)
106 typedef struct HDC__* HDC;
107 #if !PLATFORM(CG)
108 // UInt8 is defined in CoreFoundation/CFBase.h
109 typedef unsigned char UInt8;
110 #endif
111 #endif
112
113 #if PLATFORM(QT) && defined(Q_WS_WIN)
114 #include <windows.h>
115 #endif
116
117 namespace WebCore {
118
119 #if OS(WINCE) && !PLATFORM(QT)
120     class SharedBitmap;
121     class SimpleFontData;
122     class GlyphBuffer;
123 #endif
124
125     const int cMisspellingLineThickness = 3;
126     const int cMisspellingLinePatternWidth = 4;
127     const int cMisspellingLinePatternGapWidth = 1;
128
129     class AffineTransform;
130     class DrawingBuffer;
131     class Font;
132     class Generator;
133     class Gradient;
134     class GraphicsContextPlatformPrivate;
135     class GraphicsContextPrivate;
136     class ImageBuffer;
137     class KURL;
138     class Pattern;
139     class SharedGraphicsContext3D;
140     class TextRun;
141
142     // These bits can be ORed together for a total of 8 possible text drawing modes.
143     const int cTextInvisible = 0;
144     const int cTextFill = 1;
145     const int cTextStroke = 2;
146     const int cTextClip = 4;
147
148     enum StrokeStyle {
149         NoStroke,
150         SolidStroke,
151         DottedStroke,
152         DashedStroke
153     };
154
155     enum InterpolationQuality {
156         InterpolationDefault,
157         InterpolationNone,
158         InterpolationLow,
159         InterpolationMedium,
160         InterpolationHigh
161     };
162
163     class GraphicsContext : public Noncopyable {
164     public:
165         GraphicsContext(PlatformGraphicsContext*);
166         ~GraphicsContext();
167
168 #if !OS(WINCE) || PLATFORM(QT)
169         PlatformGraphicsContext* platformContext() const;
170 #endif
171
172         float strokeThickness() const;
173         void setStrokeThickness(float);
174         StrokeStyle strokeStyle() const;
175         void setStrokeStyle(const StrokeStyle& style);
176         Color strokeColor() const;
177         ColorSpace strokeColorSpace() const;
178         void setStrokeColor(const Color&, ColorSpace);
179
180         void setStrokePattern(PassRefPtr<Pattern>);
181         Pattern* strokePattern() const;
182
183         void setStrokeGradient(PassRefPtr<Gradient>);
184         Gradient* strokeGradient() const;
185
186         WindRule fillRule() const;
187         void setFillRule(WindRule);
188         Color fillColor() const;
189         ColorSpace fillColorSpace() const;
190         void setFillColor(const Color&, ColorSpace);
191
192         void setFillPattern(PassRefPtr<Pattern>);
193         Pattern* fillPattern() const;
194
195         void setFillGradient(PassRefPtr<Gradient>);
196         Gradient* fillGradient() const;
197
198         void setShadowsIgnoreTransforms(bool);
199
200         void setShouldAntialias(bool);
201         bool shouldAntialias() const;
202
203 #if PLATFORM(CG)
204         void applyStrokePattern();
205         void applyFillPattern();
206         void drawPath(const Path&);
207 #endif
208
209 #if PLATFORM(ANDROID)
210         // initialize a paint for bitmaps
211         void setupBitmapPaint(SkPaint*);
212         // initialize a paint for filling
213         void setupFillPaint(SkPaint*);
214         // initialize a paint for stroking
215         void setupStrokePaint(SkPaint*);
216         // initialize a paint for a shadow, or if false is returned, the
217         // parameters are left untouched
218         bool setupShadowPaint(SkPaint* paint, SkPoint* offset);
219         // returns true if there is a valid (non-transparent) fill color
220         bool willFill() const;
221         // returns true if there is a valid (non-transparent) stroke color
222         bool willStroke() const;
223
224         // may return NULL, since we lazily allocate the path. This is the path
225         // that is drawn by drawPath()
226         const SkPath* getCurrPath() const;
227
228         /** platform-specific factory method to return a bitmap graphicscontext,
229          called by <canvas> when we need to draw offscreen. Caller is responsible for
230          deleting the context. Use drawOffscreenContext() to draw the context's image
231          onto another graphics context.
232          */
233         static GraphicsContext* createOffscreenContext(int width, int height);
234 #endif
235
236         void save();
237         void restore();
238
239         // These draw methods will do both stroking and filling.
240         // FIXME: ...except drawRect(), which fills properly but always strokes
241         // using a 1-pixel stroke inset from the rect borders (of the correct
242         // stroke color).
243         void drawRect(const IntRect&);
244         void drawLine(const IntPoint&, const IntPoint&);
245         void drawEllipse(const IntRect&);
246         void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
247
248         void fillPath(const Path&);
249         void strokePath(const Path&);
250
251         // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
252         void strokeArc(const IntRect&, int startAngle, int angleSpan);
253
254         void fillRect(const FloatRect&);
255         void fillRect(const FloatRect&, const Color&, ColorSpace);
256         void fillRect(const FloatRect&, Generator&);
257         void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&, ColorSpace);
258
259         void clearRect(const FloatRect&);
260
261         void strokeRect(const FloatRect&, float lineWidth);
262
263         void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
264         void drawImage(Image*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
265         void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
266         void drawImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
267         void drawImage(Image*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
268                        CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
269         void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
270                        CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
271         void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect,
272                             Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
273                             CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
274
275         void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
276         void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
277         void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
278         void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
279         void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
280                              CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
281
282         void setImageInterpolationQuality(InterpolationQuality);
283         InterpolationQuality imageInterpolationQuality() const;
284
285         void clip(const FloatRect&);
286         void addRoundedRectClip(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
287         void addInnerRoundedRectClip(const IntRect&, int thickness);
288         void clipOut(const IntRect&);
289         void clipOutRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
290         void clipPath(const Path&, WindRule);
291         void clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias = true);
292         void clipToImageBuffer(ImageBuffer*, const FloatRect&);
293
294         int textDrawingMode();
295         void setTextDrawingMode(int);
296
297         void drawText(const Font&, const TextRun&, const IntPoint&, int from = 0, int to = -1);
298         void drawBidiText(const Font&, const TextRun&, const FloatPoint&);
299         void drawHighlightForText(const Font&, const TextRun&, const IntPoint&, int h, const Color& backgroundColor, ColorSpace, int from = 0, int to = -1);
300
301         FloatRect roundToDevicePixels(const FloatRect&);
302
303         void drawLineForText(const IntPoint&, int width, bool printing);
304         enum TextCheckingLineStyle {
305             TextCheckingSpellingLineStyle,
306             TextCheckingGrammarLineStyle,
307             TextCheckingReplacementLineStyle
308         };
309         void drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle);
310
311         bool paintingDisabled() const;
312         void setPaintingDisabled(bool);
313
314         bool updatingControlTints() const;
315         void setUpdatingControlTints(bool);
316
317         void beginTransparencyLayer(float opacity);
318         void endTransparencyLayer();
319
320         void setShadow(const FloatSize&, float blur, const Color&, ColorSpace);
321         bool getShadow(FloatSize&, float&, Color&) const;
322         void clearShadow();
323
324         void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
325         void drawFocusRing(const Path&, int width, int offset, const Color&);
326
327         void setLineCap(LineCap);
328         void setLineDash(const DashArray&, float dashOffset);
329         void setLineJoin(LineJoin);
330         void setMiterLimit(float);
331
332         void setAlpha(float);
333 #if PLATFORM(CAIRO)
334         float getAlpha();
335 #endif
336
337         void setCompositeOperation(CompositeOperator);
338
339 #if PLATFORM(SKIA) || PLATFORM(WX) || PLATFORM(OPENVG) || OS(WINCE)
340         void beginPath();
341         void addPath(const Path&);
342 #endif
343
344         void clip(const Path&);
345
346         // This clip function is used only by <canvas> code. It allows
347         // implementations to handle clipping on the canvas differently since
348         // the disipline is different.
349         void canvasClip(const Path&);
350         void clipOut(const Path&);
351
352         void scale(const FloatSize&);
353         void rotate(float angleInRadians);
354         void translate(const FloatSize& size) { translate(size.width(), size.height()); }
355         void translate(float x, float y);
356
357         void setURLForRect(const KURL&, const IntRect&);
358
359         void concatCTM(const AffineTransform&);
360         AffineTransform getCTM() const;
361
362 #if OS(WINCE) && !PLATFORM(QT)
363         void setBitmap(PassRefPtr<SharedBitmap>);
364         const AffineTransform& affineTransform() const;
365         AffineTransform& affineTransform();
366         void resetAffineTransform();
367         void fillRect(const FloatRect&, const Gradient*);
368         void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
369         void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
370         void drawFocusRect(const IntRect& rect);
371         void paintTextField(const IntRect& rect, unsigned state);
372         void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp);
373         void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
374         void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
375         HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true); // The passed in rect is used to create a bitmap for compositing inside transparency layers.
376         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true);    // The passed in HDC should be the one handed back by getWindowsContext.
377         void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
378 #elif PLATFORM(WIN)
379         GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
380         bool inTransparencyLayer() const;
381         HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); // The passed in rect is used to create a bitmap for compositing inside transparency layers.
382         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);    // The passed in HDC should be the one handed back by getWindowsContext.
383
384         // When set to true, child windows should be rendered into this context
385         // rather than allowing them just to render to the screen. Defaults to
386         // false.
387         // FIXME: This is a layering violation. GraphicsContext shouldn't know
388         // what a "window" is. It would be much more appropriate for this flag
389         // to be passed as a parameter alongside the GraphicsContext, but doing
390         // that would require lots of changes in cross-platform code that we
391         // aren't sure we want to make.
392         void setShouldIncludeChildWindows(bool);
393         bool shouldIncludeChildWindows() const;
394
395         class WindowsBitmap : public Noncopyable {
396         public:
397             WindowsBitmap(HDC, IntSize);
398             ~WindowsBitmap();
399
400             HDC hdc() const { return m_hdc; }
401             UInt8* buffer() const { return m_bitmapBuffer; }
402             unsigned bufferLength() const { return m_bitmapBufferLength; }
403             IntSize size() const { return m_size; }
404             unsigned bytesPerRow() const { return m_bytesPerRow; }
405
406         private:
407             HDC m_hdc;
408             HBITMAP m_bitmap;
409             UInt8* m_bitmapBuffer;
410             unsigned m_bitmapBufferLength;
411             IntSize m_size;
412             unsigned m_bytesPerRow;
413         };
414
415         WindowsBitmap* createWindowsBitmap(IntSize);
416         // The bitmap should be non-premultiplied.
417         void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
418 #endif
419
420 #if (PLATFORM(QT) && defined(Q_WS_WIN)) || (PLATFORM(WX) && OS(WINDOWS))
421         HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
422         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
423         bool shouldIncludeChildWindows() const { return false; }
424 #endif
425
426 #if PLATFORM(WX)
427         bool inTransparencyLayer() const { return false; }
428 #endif
429
430 #if PLATFORM(QT)
431         bool inTransparencyLayer() const;
432         void pushTransparencyLayerInternal(const QRect &rect, qreal opacity, QPixmap& alphaMask);
433         void takeOwnershipOfPlatformContext();
434         static QPainter::CompositionMode toQtCompositionMode(CompositeOperator op);
435 #endif
436
437 #if PLATFORM(QT) || PLATFORM(CAIRO)
438         ContextShadow* contextShadow();
439 #endif
440
441 #if PLATFORM(GTK)
442         void setGdkExposeEvent(GdkEventExpose*);
443         GdkDrawable* gdkDrawable() const;
444         GdkEventExpose* gdkExposeEvent() const;
445 #endif
446
447 #if PLATFORM(HAIKU)
448         pattern getHaikuStrokeStyle();
449 #endif
450
451         void setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
452         void syncSoftwareCanvas();
453         void markDirtyRect(const IntRect&); // Hints that a portion of the backing store is dirty.
454
455     private:
456         void savePlatformState();
457         void restorePlatformState();
458
459         void setPlatformTextDrawingMode(int);
460         void setPlatformFont(const Font& font);
461
462         void setPlatformStrokeColor(const Color&, ColorSpace);
463         void setPlatformStrokeStyle(const StrokeStyle&);
464         void setPlatformStrokeThickness(float);
465         void setPlatformStrokeGradient(Gradient*);
466         void setPlatformStrokePattern(Pattern*);
467
468         void setPlatformFillColor(const Color&, ColorSpace);
469         void setPlatformFillGradient(Gradient*);
470         void setPlatformFillPattern(Pattern*);
471
472         void setPlatformShouldAntialias(bool b);
473
474         void setPlatformShadow(const FloatSize&, float blur, const Color&, ColorSpace);
475         void clearPlatformShadow();
476
477         static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, const StrokeStyle&);
478
479         static GraphicsContextPrivate* createGraphicsContextPrivate();
480         static void destroyGraphicsContextPrivate(GraphicsContextPrivate*);
481
482         GraphicsContextPrivate* m_common;
483         GraphicsContextPlatformPrivate* m_data; // Deprecated; m_commmon can just be downcasted. To be removed.
484     };
485
486 } // namespace WebCore
487
488 #endif // GraphicsContext_h