Replace PassRef with Ref/Ref&& across the board.
[WebKit-https.git] / Source / WebCore / platform / graphics / GraphicsContext.h
1 /*
2  * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2013 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 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 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 "Font.h"
34 #include "Gradient.h"
35 #include "Image.h"
36 #include "ImageOrientation.h"
37 #include "Path.h"
38 #include "Pattern.h"
39 #include <wtf/Noncopyable.h>
40 #include <wtf/PassOwnPtr.h>
41
42 #if USE(CG)
43 typedef struct CGContext PlatformGraphicsContext;
44 #elif USE(CAIRO)
45 namespace WebCore {
46 class PlatformContextCairo;
47 }
48 typedef WebCore::PlatformContextCairo PlatformGraphicsContext;
49 #elif USE(WINGDI)
50 typedef struct HDC__ PlatformGraphicsContext;
51 #else
52 typedef void PlatformGraphicsContext;
53 #endif
54
55 #if PLATFORM(WIN)
56 #include "DIBPixelData.h"
57 typedef struct HDC__* HDC;
58 #if !USE(CG)
59 // UInt8 is defined in CoreFoundation/CFBase.h
60 typedef unsigned char UInt8;
61 #endif
62 #endif
63
64 namespace WebCore {
65
66 #if USE(WINGDI)
67     class SharedBitmap;
68     class SimpleFontData;
69     class GlyphBuffer;
70 #endif
71
72     const int cMisspellingLineThickness = 3;
73     const int cMisspellingLinePatternWidth = 4;
74     const int cMisspellingLinePatternGapWidth = 1;
75
76     class AffineTransform;
77     class DrawingBuffer;
78     class FloatRoundedRect;
79     class Gradient;
80     class GraphicsContextPlatformPrivate;
81     class ImageBuffer;
82     class IntRect;
83     class RoundedRect;
84     class URL;
85     class GraphicsContext3D;
86     class TextRun;
87     class TransformationMatrix;
88 #if PLATFORM(IOS)
89     struct BidiStatus;
90 #endif
91
92     enum TextDrawingMode {
93         TextModeFill = 1 << 0,
94         TextModeStroke = 1 << 1,
95 #if ENABLE(LETTERPRESS)
96         TextModeLetterpress = 1 << 2,
97 #endif
98     };
99     typedef unsigned TextDrawingModeFlags;
100
101     enum StrokeStyle {
102         NoStroke,
103         SolidStroke,
104         DottedStroke,
105         DashedStroke,
106         DoubleStroke,
107         WavyStroke,
108     };
109
110     enum InterpolationQuality {
111         InterpolationDefault,
112         InterpolationNone,
113         InterpolationLow,
114         InterpolationMedium,
115         InterpolationHigh
116     };
117
118     struct GraphicsContextState {
119         GraphicsContextState()
120             : strokeThickness(0)
121             , shadowBlur(0)
122             , textDrawingMode(TextModeFill)
123             , strokeColor(Color::black)
124             , fillColor(Color::black)
125             , strokeStyle(SolidStroke)
126             , fillRule(RULE_NONZERO)
127             , strokeColorSpace(ColorSpaceDeviceRGB)
128             , fillColorSpace(ColorSpaceDeviceRGB)
129             , shadowColorSpace(ColorSpaceDeviceRGB)
130             , compositeOperator(CompositeSourceOver)
131             , blendMode(BlendModeNormal)
132 #if PLATFORM(IOS)
133             , emojiDrawingEnabled(true)
134 #endif
135             , shouldAntialias(true)
136             , shouldSmoothFonts(true)
137             , shouldSubpixelQuantizeFonts(true)
138             , paintingDisabled(false)
139             , shadowsIgnoreTransforms(false)
140 #if USE(CG)
141             // Core Graphics incorrectly renders shadows with radius > 8px (<rdar://problem/8103442>),
142             // but we need to preserve this buggy behavior for canvas and -webkit-box-shadow.
143             , shadowsUseLegacyRadius(false)
144 #endif
145             , drawLuminanceMask(false)
146         {
147         }
148
149         RefPtr<Gradient> strokeGradient;
150         RefPtr<Pattern> strokePattern;
151         
152         RefPtr<Gradient> fillGradient;
153         RefPtr<Pattern> fillPattern;
154
155         FloatSize shadowOffset;
156
157         float strokeThickness;
158         float shadowBlur;
159
160         TextDrawingModeFlags textDrawingMode;
161
162         Color strokeColor;
163         Color fillColor;
164         Color shadowColor;
165
166         StrokeStyle strokeStyle;
167         WindRule fillRule;
168
169         ColorSpace strokeColorSpace;
170         ColorSpace fillColorSpace;
171         ColorSpace shadowColorSpace;
172
173         CompositeOperator compositeOperator;
174         BlendMode blendMode;
175
176 #if PLATFORM(IOS)
177         bool emojiDrawingEnabled : 1;
178 #endif
179         bool shouldAntialias : 1;
180         bool shouldSmoothFonts : 1;
181         bool shouldSubpixelQuantizeFonts : 1;
182         bool paintingDisabled : 1;
183         bool shadowsIgnoreTransforms : 1;
184 #if USE(CG)
185         bool shadowsUseLegacyRadius : 1;
186 #endif
187         bool drawLuminanceMask : 1;
188     };
189
190     struct ImagePaintingOptions {
191         ImagePaintingOptions(CompositeOperator compositeOperator = CompositeSourceOver, BlendMode blendMode = BlendModeNormal, ImageOrientationDescription orientationDescription = ImageOrientationDescription(), bool useLowQualityScale = false)
192             : m_compositeOperator(compositeOperator)
193             , m_blendMode(blendMode)
194             , m_orientationDescription(orientationDescription)
195             , m_useLowQualityScale(useLowQualityScale)
196         {
197         }
198
199         ImagePaintingOptions(ImageOrientationDescription orientationDescription, bool useLowQualityScale = false, CompositeOperator compositeOperator = CompositeSourceOver, BlendMode blendMode = BlendModeNormal)
200             : m_compositeOperator(compositeOperator)
201             , m_blendMode(blendMode)
202             , m_orientationDescription(orientationDescription)
203             , m_useLowQualityScale(useLowQualityScale)
204         {
205         }
206
207         ImagePaintingOptions(bool useLowQualityScale, ImageOrientationDescription orientationDescription = ImageOrientationDescription(), CompositeOperator compositeOperator = CompositeSourceOver, BlendMode blendMode = BlendModeNormal)
208             : m_compositeOperator(compositeOperator)
209             , m_blendMode(blendMode)
210             , m_orientationDescription(orientationDescription)
211             , m_useLowQualityScale(useLowQualityScale)
212         {
213         }
214
215         CompositeOperator m_compositeOperator;
216         BlendMode m_blendMode;
217         ImageOrientationDescription m_orientationDescription;
218         bool m_useLowQualityScale;
219     };
220
221     class GraphicsContext {
222         WTF_MAKE_NONCOPYABLE(GraphicsContext); WTF_MAKE_FAST_ALLOCATED;
223     public:
224         WEBCORE_EXPORT GraphicsContext(PlatformGraphicsContext*);
225         WEBCORE_EXPORT ~GraphicsContext();
226
227         WEBCORE_EXPORT PlatformGraphicsContext* platformContext() const;
228
229         float strokeThickness() const;
230         void setStrokeThickness(float);
231         StrokeStyle strokeStyle() const;
232         void setStrokeStyle(StrokeStyle);
233         Color strokeColor() const;
234         ColorSpace strokeColorSpace() const;
235         WEBCORE_EXPORT void setStrokeColor(const Color&, ColorSpace);
236
237         void setStrokePattern(Ref<Pattern>&&);
238         Pattern* strokePattern() const;
239
240         void setStrokeGradient(Ref<Gradient>&&);
241         Gradient* strokeGradient() const;
242
243         WindRule fillRule() const;
244         void setFillRule(WindRule);
245         Color fillColor() const;
246         ColorSpace fillColorSpace() const;
247         WEBCORE_EXPORT void setFillColor(const Color&, ColorSpace);
248
249         void setFillPattern(Ref<Pattern>&&);
250         Pattern* fillPattern() const;
251
252         WEBCORE_EXPORT void setFillGradient(Ref<Gradient>&&);
253         Gradient* fillGradient() const;
254
255         void setShadowsIgnoreTransforms(bool);
256         bool shadowsIgnoreTransforms() const;
257
258         WEBCORE_EXPORT void setShouldAntialias(bool);
259         bool shouldAntialias() const;
260
261         WEBCORE_EXPORT void setShouldSmoothFonts(bool);
262         bool shouldSmoothFonts() const;
263
264         // Normally CG enables subpixel-quantization because it improves the performance of aligning glyphs.
265         // In some cases we have to disable to to ensure a high-quality output of the glyphs.
266         void setShouldSubpixelQuantizeFonts(bool);
267         bool shouldSubpixelQuantizeFonts() const;
268
269         const GraphicsContextState& state() const;
270
271 #if USE(CG)
272         void applyStrokePattern();
273         void applyFillPattern();
274         void drawPath(const Path&);
275
276         WEBCORE_EXPORT void drawNativeImage(PassNativeImagePtr, const FloatSize& selfSize, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, ImageOrientation = DefaultImageOrientation);
277
278         // Allow font smoothing (LCD antialiasing). Not part of the graphics state.
279         void setAllowsFontSmoothing(bool);
280         
281         WEBCORE_EXPORT void setIsCALayerContext(bool);
282         bool isCALayerContext() const;
283
284         WEBCORE_EXPORT void setIsAcceleratedContext(bool);
285 #endif
286         bool isAcceleratedContext() const;
287
288         WEBCORE_EXPORT void save();
289         WEBCORE_EXPORT void restore();
290
291         // These draw methods will do both stroking and filling.
292         // FIXME: ...except drawRect(), which fills properly but always strokes
293         // using a 1-pixel stroke inset from the rect borders (of the correct
294         // stroke color).
295         void drawRect(const FloatRect&, float borderThickness = 1);
296         void drawLine(const FloatPoint&, const FloatPoint&);
297
298 #if PLATFORM(IOS)
299         void drawJoinedLines(CGPoint points[], unsigned count, bool antialias, CGLineCap = kCGLineCapButt);
300 #endif
301
302         void drawEllipse(const FloatRect&);
303         void drawRaisedEllipse(const FloatRect&, const Color& ellipseColor, ColorSpace ellipseColorSpace, const Color& shadowColor, ColorSpace shadowColorSpace);
304         void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
305
306         WEBCORE_EXPORT void fillPath(const Path&);
307         void strokePath(const Path&);
308
309         void fillEllipse(const FloatRect&);
310         void strokeEllipse(const FloatRect&);
311
312         WEBCORE_EXPORT void fillRect(const FloatRect&);
313         WEBCORE_EXPORT void fillRect(const FloatRect&, const Color&, ColorSpace);
314         void fillRect(const FloatRect&, Gradient&);
315         void fillRect(const FloatRect&, const Color&, ColorSpace, CompositeOperator, BlendMode = BlendModeNormal);
316         void fillRoundedRect(const FloatRoundedRect&, const Color&, ColorSpace, BlendMode = BlendModeNormal);
317         void fillRectWithRoundedHole(const FloatRect&, const FloatRoundedRect& roundedHoleRect, const Color&, ColorSpace);
318
319         WEBCORE_EXPORT void clearRect(const FloatRect&);
320
321         WEBCORE_EXPORT void strokeRect(const FloatRect&, float lineWidth);
322
323         WEBCORE_EXPORT void drawImage(Image*, ColorSpace, const FloatPoint& destination, const ImagePaintingOptions& = ImagePaintingOptions());
324         void drawImage(Image*, ColorSpace, const FloatRect& destination, const ImagePaintingOptions& = ImagePaintingOptions());
325         void drawImage(Image*, ColorSpace, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& = ImagePaintingOptions());
326
327         void drawTiledImage(Image*, ColorSpace, const FloatRect& destination, const FloatPoint& source, const FloatSize& tileSize,
328             const ImagePaintingOptions& = ImagePaintingOptions());
329         void drawTiledImage(Image*, ColorSpace, const FloatRect& destination, const FloatRect& source, const FloatSize& tileScaleFactor,
330             Image::TileRule, Image::TileRule, const ImagePaintingOptions& = ImagePaintingOptions());
331
332         void drawImageBuffer(ImageBuffer*, ColorSpace, const FloatPoint& destination, const ImagePaintingOptions& = ImagePaintingOptions());
333         void drawImageBuffer(ImageBuffer*, ColorSpace, const FloatRect& destination, const ImagePaintingOptions& = ImagePaintingOptions());
334         void drawImageBuffer(ImageBuffer*, ColorSpace, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& = ImagePaintingOptions());
335
336         WEBCORE_EXPORT void setImageInterpolationQuality(InterpolationQuality);
337         InterpolationQuality imageInterpolationQuality() const;
338
339         WEBCORE_EXPORT void clip(const IntRect&);
340         WEBCORE_EXPORT void clip(const FloatRect&);
341         void clipRoundedRect(const FloatRoundedRect&);
342
343         void clipOut(const FloatRect&);
344         void clipOutRoundedRect(const FloatRoundedRect&);
345         void clipPath(const Path&, WindRule);
346         void clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias = true);
347         void clipToImageBuffer(ImageBuffer*, const FloatRect&);
348         
349         IntRect clipBounds() const;
350
351         TextDrawingModeFlags textDrawingMode() const;
352         void setTextDrawingMode(TextDrawingModeFlags);
353
354 #if PLATFORM(IOS)
355         bool emojiDrawingEnabled();
356         WEBCORE_EXPORT void setEmojiDrawingEnabled(bool);
357 #endif
358         
359 #if !PLATFORM(IOS)
360         void drawText(const Font&, const TextRun&, const FloatPoint&, int from = 0, int to = -1);
361 #else
362         float drawText(const Font&, const TextRun&, const FloatPoint&, int from = 0, int to = -1);
363 #endif
364         void drawGlyphs(const Font&, const SimpleFontData&, const GlyphBuffer&, int from, int numGlyphs, const FloatPoint&);
365         void drawEmphasisMarks(const Font&, const TextRun& , const AtomicString& mark, const FloatPoint&, int from = 0, int to = -1);
366 #if !PLATFORM(IOS)
367         void drawBidiText(const Font&, const TextRun&, const FloatPoint&, Font::CustomFontNotReadyAction = Font::DoNotPaintIfFontNotReady);
368 #else
369         WEBCORE_EXPORT float drawBidiText(const Font&, const TextRun&, const FloatPoint&, Font::CustomFontNotReadyAction = Font::DoNotPaintIfFontNotReady, BidiStatus* = 0, int length = -1);
370 #endif
371         enum RoundingMode {
372             RoundAllSides,
373             RoundOriginAndDimensions
374         };
375         FloatRect roundToDevicePixels(const FloatRect&, RoundingMode = RoundAllSides);
376
377         FloatRect computeLineBoundsForText(const FloatPoint&, float width, bool printing);
378         WEBCORE_EXPORT void drawLineForText(const FloatPoint&, float width, bool printing, bool doubleLines = false);
379         void drawLinesForText(const FloatPoint&, const DashArray& widths, bool printing, bool doubleLines = false);
380         enum DocumentMarkerLineStyle {
381 #if PLATFORM(IOS)
382             TextCheckingDictationPhraseWithAlternativesLineStyle,
383 #endif
384             DocumentMarkerSpellingLineStyle,
385             DocumentMarkerGrammarLineStyle,
386             DocumentMarkerAutocorrectionReplacementLineStyle,
387             DocumentMarkerDictationAlternativesLineStyle
388         };
389         static void updateDocumentMarkerResources();
390         void drawLineForDocumentMarker(const FloatPoint&, float width, DocumentMarkerLineStyle);
391
392         WEBCORE_EXPORT bool paintingDisabled() const;
393         void setPaintingDisabled(bool);
394
395         WEBCORE_EXPORT bool updatingControlTints() const;
396         void setUpdatingControlTints(bool);
397
398         WEBCORE_EXPORT void beginTransparencyLayer(float opacity);
399         WEBCORE_EXPORT void endTransparencyLayer();
400         bool isInTransparencyLayer() const;
401
402         bool hasShadow() const;
403         WEBCORE_EXPORT void setShadow(const FloatSize&, float blur, const Color&, ColorSpace);
404         // Legacy shadow blur radius is used for canvas, and -webkit-box-shadow.
405         // It has different treatment of radii > 8px.
406         void setLegacyShadow(const FloatSize&, float blur, const Color&, ColorSpace);
407
408         bool getShadow(FloatSize&, float&, Color&, ColorSpace&) const;
409         WEBCORE_EXPORT void clearShadow();
410
411         bool hasBlurredShadow() const;
412 #if USE(CAIRO)
413         bool mustUseShadowBlur() const;
414 #endif
415
416         void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
417         void drawFocusRing(const Path&, int width, int offset, const Color&);
418 #if PLATFORM(MAC)
419         void drawFocusRing(const Vector<IntRect>&, int width, int offset, double timeOffset, bool& needsRedraw);
420 #endif
421
422         void setLineCap(LineCap);
423         void setLineDash(const DashArray&, float dashOffset);
424         void setLineJoin(LineJoin);
425         void setMiterLimit(float);
426
427         void setAlpha(float);
428
429         WEBCORE_EXPORT void setCompositeOperation(CompositeOperator, BlendMode = BlendModeNormal);
430         CompositeOperator compositeOperation() const;
431         BlendMode blendModeOperation() const;
432
433         void setDrawLuminanceMask(bool);
434         bool drawLuminanceMask() const;
435
436         WEBCORE_EXPORT void clip(const Path&, WindRule = RULE_EVENODD);
437
438         // This clip function is used only by <canvas> code. It allows
439         // implementations to handle clipping on the canvas differently since
440         // the discipline is different.
441         void canvasClip(const Path&, WindRule = RULE_EVENODD);
442         void clipOut(const Path&);
443
444         WEBCORE_EXPORT void scale(const FloatSize&);
445         void rotate(float angleInRadians);
446         void translate(const FloatSize& size) { translate(size.width(), size.height()); }
447         WEBCORE_EXPORT void translate(float x, float y);
448
449         void setURLForRect(const URL&, const IntRect&);
450
451         void concatCTM(const AffineTransform&);
452         void setCTM(const AffineTransform&);
453
454         enum IncludeDeviceScale { DefinitelyIncludeDeviceScale, PossiblyIncludeDeviceScale };
455         AffineTransform getCTM(IncludeDeviceScale includeScale = PossiblyIncludeDeviceScale) const;
456
457 #if ENABLE(3D_RENDERING) && USE(TEXTURE_MAPPER)
458         // This is needed when using accelerated-compositing in software mode, like in TextureMapper.
459         void concat3DTransform(const TransformationMatrix&);
460         void set3DTransform(const TransformationMatrix&);
461         TransformationMatrix get3DTransform() const;
462 #endif
463         // Create an image buffer compatible with this context, with suitable resolution
464         // for drawing into the buffer and then into this context.
465         std::unique_ptr<ImageBuffer> createCompatibleBuffer(const FloatSize&, bool hasAlpha = true) const;
466         bool isCompatibleWithBuffer(ImageBuffer*) const;
467
468         // This function applies the device scale factor to the context, making the context capable of
469         // acting as a base-level context for a HiDPI environment.
470         WEBCORE_EXPORT void applyDeviceScaleFactor(float);
471         void platformApplyDeviceScaleFactor(float);
472
473 #if OS(WINDOWS)
474         HDC getWindowsContext(const IntRect&, bool supportAlphaBlend, bool mayCreateBitmap); // The passed in rect is used to create a bitmap for compositing inside transparency layers.
475         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend, bool mayCreateBitmap); // The passed in HDC should be the one handed back by getWindowsContext.
476 #if PLATFORM(WIN)
477 #if USE(WINGDI)
478         void setBitmap(PassRefPtr<SharedBitmap>);
479         const AffineTransform& affineTransform() const;
480         AffineTransform& affineTransform();
481         void resetAffineTransform();
482         void fillRect(const FloatRect&, const Gradient*);
483         void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
484         void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
485         void drawFocusRect(const IntRect& rect);
486         void paintTextField(const IntRect& rect, unsigned state);
487         void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp, BlendMode blendMode);
488         void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
489         void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
490         void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
491 #else
492         GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
493
494         // When set to true, child windows should be rendered into this context
495         // rather than allowing them just to render to the screen. Defaults to
496         // false.
497         // FIXME: This is a layering violation. GraphicsContext shouldn't know
498         // what a "window" is. It would be much more appropriate for this flag
499         // to be passed as a parameter alongside the GraphicsContext, but doing
500         // that would require lots of changes in cross-platform code that we
501         // aren't sure we want to make.
502         void setShouldIncludeChildWindows(bool);
503         bool shouldIncludeChildWindows() const;
504
505         class WindowsBitmap {
506             WTF_MAKE_NONCOPYABLE(WindowsBitmap);
507         public:
508             WindowsBitmap(HDC, const IntSize&);
509             ~WindowsBitmap();
510
511             HDC hdc() const { return m_hdc; }
512             UInt8* buffer() const { return m_pixelData.buffer(); }
513             unsigned bufferLength() const { return m_pixelData.bufferLength(); }
514             const IntSize& size() const { return m_pixelData.size(); }
515             unsigned bytesPerRow() const { return m_pixelData.bytesPerRow(); }
516             unsigned short bitsPerPixel() const { return m_pixelData.bitsPerPixel(); }
517             const DIBPixelData& windowsDIB() const { return m_pixelData; }
518
519         private:
520             HDC m_hdc;
521             HBITMAP m_bitmap;
522             DIBPixelData m_pixelData;
523         };
524
525         PassOwnPtr<WindowsBitmap> createWindowsBitmap(const IntSize&);
526         // The bitmap should be non-premultiplied.
527         void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
528 #endif
529 #else // PLATFORM(WIN)
530         bool shouldIncludeChildWindows() const { return false; }
531 #endif // PLATFORM(WIN)
532 #endif // OS(WINDOWS)
533
534 #if USE(CAIRO)
535         GraphicsContext(cairo_t*);
536 #endif
537
538         static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle);
539
540     private:
541         void platformInit(PlatformGraphicsContext*);
542         void platformDestroy();
543
544 #if PLATFORM(WIN) && !USE(WINGDI)
545         void platformInit(HDC, bool hasAlpha = false);
546 #endif
547
548         void savePlatformState();
549         void restorePlatformState();
550
551         void setPlatformTextDrawingMode(TextDrawingModeFlags);
552
553         void setPlatformStrokeColor(const Color&, ColorSpace);
554         void setPlatformStrokeStyle(StrokeStyle);
555         void setPlatformStrokeThickness(float);
556
557         void setPlatformFillColor(const Color&, ColorSpace);
558
559         void setPlatformShouldAntialias(bool);
560         void setPlatformShouldSmoothFonts(bool);
561
562         void setPlatformShadow(const FloatSize&, float blur, const Color&, ColorSpace);
563         void clearPlatformShadow();
564
565         void setPlatformCompositeOperation(CompositeOperator, BlendMode = BlendModeNormal);
566
567         void beginPlatformTransparencyLayer(float opacity);
568         void endPlatformTransparencyLayer();
569         static bool supportsTransparencyLayers();
570
571         void fillEllipseAsPath(const FloatRect&);
572         void strokeEllipseAsPath(const FloatRect&);
573
574         void platformFillEllipse(const FloatRect&);
575         void platformStrokeEllipse(const FloatRect&);
576
577         void platformFillRoundedRect(const FloatRoundedRect&, const Color&, ColorSpace);
578
579         FloatRect computeLineBoundsAndAntialiasingModeForText(const FloatPoint&, float width, bool printing, bool& shouldAntialias, Color&);
580
581         GraphicsContextPlatformPrivate* m_data;
582
583         GraphicsContextState m_state;
584         Vector<GraphicsContextState> m_stack;
585         bool m_updatingControlTints;
586         unsigned m_transparencyCount;
587     };
588
589     class GraphicsContextStateSaver {
590         WTF_MAKE_FAST_ALLOCATED;
591     public:
592         GraphicsContextStateSaver(GraphicsContext& context, bool saveAndRestore = true)
593         : m_context(context)
594         , m_saveAndRestore(saveAndRestore)
595         {
596             if (m_saveAndRestore)
597                 m_context.save();
598         }
599         
600         ~GraphicsContextStateSaver()
601         {
602             if (m_saveAndRestore)
603                 m_context.restore();
604         }
605         
606         void save()
607         {
608             ASSERT(!m_saveAndRestore);
609             m_context.save();
610             m_saveAndRestore = true;
611         }
612
613         void restore()
614         {
615             ASSERT(m_saveAndRestore);
616             m_context.restore();
617             m_saveAndRestore = false;
618         }
619         
620         GraphicsContext* context() const { return &m_context; }
621
622     private:
623         GraphicsContext& m_context;
624         bool m_saveAndRestore;
625     };
626
627 } // namespace WebCore
628
629 #endif // GraphicsContext_h
630