1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SKIA_EXT_PLATFORM_DEVICE_H_
6 #define SKIA_EXT_PLATFORM_DEVICE_H_
8 #include "build/build_config.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "third_party/skia/include/core/SkBitmapDevice.h"
17 #include "third_party/skia/include/core/SkPreConfig.h"
24 #if defined(USE_CAIRO)
25 typedef struct _cairo cairo_t
;
26 typedef struct _cairo_rectangle cairo_rectangle_t
;
27 #elif defined(OS_MACOSX)
28 typedef struct CGContext
* CGContextRef
;
29 typedef struct CGRect CGRect
;
37 typedef HDC PlatformSurface
;
38 typedef RECT PlatformRect
;
39 #elif defined(USE_CAIRO)
40 typedef cairo_t
* PlatformSurface
;
41 typedef cairo_rectangle_t PlatformRect
;
42 #elif defined(OS_MACOSX)
43 typedef CGContextRef PlatformSurface
;
44 typedef CGRect PlatformRect
;
46 typedef void* PlatformSurface
;
47 typedef SkIRect
* PlatformRect
;
50 // The following routines provide accessor points for the functionality
51 // exported by the various PlatformDevice ports.
52 // All calls to PlatformDevice::* should be routed through these
55 // Bind a PlatformDevice instance, |platform_device| to |device|. Subsequent
56 // calls to the functions exported below will forward the request to the
57 // corresponding method on the bound PlatformDevice instance. If no
58 // PlatformDevice has been bound to the SkBaseDevice passed, then the
60 SK_API
void SetPlatformDevice(SkBaseDevice
* device
,
61 PlatformDevice
* platform_device
);
62 SK_API PlatformDevice
* GetPlatformDevice(SkBaseDevice
* device
);
66 // Initializes the default settings and colors in a device context.
67 SK_API
void InitializeDC(HDC context
);
68 #elif defined(OS_MACOSX)
69 // Returns the CGContext that backing the SkBaseDevice. Forwards to the bound
70 // PlatformDevice. Returns NULL if no PlatformDevice is bound.
71 SK_API CGContextRef
GetBitmapContext(SkBaseDevice
* device
);
74 // Following routines are used in print preview workflow to mark the draft mode
75 // metafile and preview metafile.
76 SK_API SkMetaData
& getMetaData(const SkCanvas
& canvas
);
77 SK_API
void SetIsDraftMode(const SkCanvas
& canvas
, bool draft_mode
);
78 SK_API
bool IsDraftMode(const SkCanvas
& canvas
);
80 #if defined(OS_MACOSX) || defined(OS_WIN)
81 SK_API
void SetIsPreviewMetafile(const SkCanvas
& canvas
, bool is_preview
);
82 SK_API
bool IsPreviewMetafile(const SkCanvas
& canvas
);
85 // A SkBitmapDevice is basically a wrapper around SkBitmap that provides a
86 // surface for SkCanvas to draw into. PlatformDevice provides a surface
87 // Windows can also write to. It also provides functionality to play well
88 // with GDI drawing functions. This class is abstract and must be subclassed.
89 // It provides the basic interface to implement it either with or without
92 // PlatformDevice provides an interface which sub-classes of SkBaseDevice can
93 // also provide to allow for drawing by the native platform into the device.
94 // TODO(robertphillips): Once the bitmap-specific entry points are removed
95 // from SkBaseDevice it might make sense for PlatformDevice to be derived
97 class SK_API PlatformDevice
{
99 virtual ~PlatformDevice() {}
101 #if defined(OS_MACOSX)
102 // The CGContext that corresponds to the bitmap, used for CoreGraphics
103 // operations drawing into the bitmap. This is possibly heavyweight, so it
104 // should exist only during one pass of rendering.
105 virtual CGContextRef
GetBitmapContext() = 0;
108 // The DC that corresponds to the bitmap, used for GDI operations drawing
109 // into the bitmap. This is possibly heavyweight, so it should be existant
110 // only during one pass of rendering.
111 virtual PlatformSurface
BeginPlatformPaint();
113 // Finish a previous call to beginPlatformPaint.
114 virtual void EndPlatformPaint();
116 // Returns true if GDI operations can be used for drawing into the bitmap.
117 virtual bool SupportsPlatformPaint();
120 // Loads a SkPath into the GDI context. The path can there after be used for
121 // clipping or as a stroke. Returns false if the path failed to be loaded.
122 static bool LoadPathToDC(HDC context
, const SkPath
& path
);
124 // Loads a SkRegion into the GDI context.
125 static void LoadClippingRegionToDC(HDC context
, const SkRegion
& region
,
126 const SkMatrix
& transformation
);
128 // Draws to the given screen DC, if the bitmap DC doesn't exist, this will
129 // temporarily create it. However, if you have created the bitmap DC, it will
130 // be more efficient if you don't free it until after this call so it doesn't
131 // have to be created twice. If src_rect is null, then the entirety of the
132 // source device will be copied.
133 virtual void DrawToHDC(HDC
, int x
, int y
, const RECT
* src_rect
);
135 #elif defined(OS_MACOSX)
136 // Loads a SkPath into the CG context. The path can there after be used for
137 // clipping or as a stroke.
138 static void LoadPathToCGContext(CGContextRef context
, const SkPath
& path
);
140 // Initializes the default settings and colors in a device context.
141 static void InitializeCGContext(CGContextRef context
);
143 // Loads a SkRegion into the CG context.
144 static void LoadClippingRegionToCGContext(CGContextRef context
,
145 const SkRegion
& region
,
146 const SkMatrix
& transformation
);
151 // Arrays must be inside structures.
155 typedef std::vector
<CubicPoints
> CubicPath
;
156 typedef std::vector
<CubicPath
> CubicPaths
;
158 // Loads the specified Skia transform into the device context, excluding
159 // perspective (which GDI doesn't support).
160 static void LoadTransformToDC(HDC dc
, const SkMatrix
& matrix
);
162 // Transforms SkPath's paths into a series of cubic path.
163 static bool SkPathToCubicPaths(CubicPaths
* paths
, const SkPath
& skpath
);
164 #elif defined(OS_MACOSX)
165 // Loads the specified Skia transform into the device context
166 static void LoadTransformToCGContext(CGContextRef context
,
167 const SkMatrix
& matrix
);
173 #endif // SKIA_EXT_PLATFORM_DEVICE_H_