- add sources.
[platform/framework/web/crosswalk.git] / src / skia / ext / platform_device.h
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.
4
5 #ifndef SKIA_EXT_PLATFORM_DEVICE_H_
6 #define SKIA_EXT_PLATFORM_DEVICE_H_
7
8 #include "build/build_config.h"
9
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #include <vector>
13 #endif
14
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"
18
19 class SkMatrix;
20 class SkMetaData;
21 class SkPath;
22 class SkRegion;
23
24 #if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) \
25     || defined(OS_SOLARIS)
26 typedef struct _cairo cairo_t;
27 typedef struct _cairo_rectangle cairo_rectangle_t;
28 #elif defined(OS_MACOSX)
29 typedef struct CGContext* CGContextRef;
30 typedef struct CGRect CGRect;
31 #endif
32
33 namespace skia {
34
35 class PlatformDevice;
36
37 #if defined(OS_WIN)
38 typedef HDC PlatformSurface;
39 typedef RECT PlatformRect;
40 #elif defined(ANDROID)
41 typedef void* PlatformSurface;
42 typedef SkIRect* PlatformRect;
43 #elif defined(OS_LINUX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) \
44     || defined(OS_SOLARIS)
45 typedef cairo_t* PlatformSurface;
46 typedef cairo_rectangle_t PlatformRect;
47 #elif defined(OS_MACOSX)
48 typedef CGContextRef PlatformSurface;
49 typedef CGRect PlatformRect;
50 #endif
51
52 // The following routines provide accessor points for the functionality
53 // exported by the various PlatformDevice ports.  
54 // All calls to PlatformDevice::* should be routed through these 
55 // helper functions.
56
57 // Bind a PlatformDevice instance, |platform_device| to |device|.  Subsequent
58 // calls to the functions exported below will forward the request to the
59 // corresponding method on the bound PlatformDevice instance.    If no
60 // PlatformDevice has been bound to the SkBaseDevice passed, then the 
61 // routines are NOPS.
62 SK_API void SetPlatformDevice(SkBaseDevice* device,
63                               PlatformDevice* platform_device);
64 SK_API PlatformDevice* GetPlatformDevice(SkBaseDevice* device);
65
66
67 #if defined(OS_WIN)
68 // Initializes the default settings and colors in a device context.
69 SK_API void InitializeDC(HDC context);
70 #elif defined(OS_MACOSX)
71 // Returns the CGContext that backing the SkBaseDevice.  Forwards to the bound
72 // PlatformDevice.  Returns NULL if no PlatformDevice is bound.
73 SK_API CGContextRef GetBitmapContext(SkBaseDevice* device);
74 #endif
75
76 // Following routines are used in print preview workflow to mark the draft mode
77 // metafile and preview metafile.
78 SK_API SkMetaData& getMetaData(const SkCanvas& canvas);
79 SK_API void SetIsDraftMode(const SkCanvas& canvas, bool draft_mode);
80 SK_API bool IsDraftMode(const SkCanvas& canvas);
81
82 #if defined(OS_MACOSX) || defined(OS_WIN)
83 SK_API void SetIsPreviewMetafile(const SkCanvas& canvas, bool is_preview);
84 SK_API bool IsPreviewMetafile(const SkCanvas& canvas);
85 #endif
86
87 // A SkBitmapDevice is basically a wrapper around SkBitmap that provides a 
88 // surface for SkCanvas to draw into. PlatformDevice provides a surface 
89 // Windows can also write to. It also provides functionality to play well 
90 // with GDI drawing functions. This class is abstract and must be subclassed. 
91 // It provides the basic interface to implement it either with or without 
92 // a bitmap backend.
93 //
94 // PlatformDevice provides an interface which sub-classes of SkBaseDevice can 
95 // also provide to allow for drawing by the native platform into the device.
96 // TODO(robertphillips): Once the bitmap-specific entry points are removed
97 // from SkBaseDevice it might make sense for PlatformDevice to be derived
98 // from it.
99 class SK_API PlatformDevice {
100  public:
101   virtual ~PlatformDevice() {}
102
103 #if defined(OS_MACOSX)
104   // The CGContext that corresponds to the bitmap, used for CoreGraphics
105   // operations drawing into the bitmap. This is possibly heavyweight, so it
106   // should exist only during one pass of rendering.
107   virtual CGContextRef GetBitmapContext() = 0;
108 #endif
109
110   // The DC that corresponds to the bitmap, used for GDI operations drawing
111   // into the bitmap. This is possibly heavyweight, so it should be existant
112   // only during one pass of rendering.
113   virtual PlatformSurface BeginPlatformPaint();
114
115   // Finish a previous call to beginPlatformPaint.
116   virtual void EndPlatformPaint();
117
118   // Draws to the given screen DC, if the bitmap DC doesn't exist, this will
119   // temporarily create it. However, if you have created the bitmap DC, it will
120   // be more efficient if you don't free it until after this call so it doesn't
121   // have to be created twice.  If src_rect is null, then the entirety of the
122   // source device will be copied.
123   virtual void DrawToNativeContext(PlatformSurface surface, int x, int y,
124                                    const PlatformRect* src_rect) = 0;
125
126   // Returns true if GDI operations can be used for drawing into the bitmap.
127   virtual bool SupportsPlatformPaint();
128
129 #if defined(OS_WIN)
130   // Loads a SkPath into the GDI context. The path can there after be used for
131   // clipping or as a stroke. Returns false if the path failed to be loaded.
132   static bool LoadPathToDC(HDC context, const SkPath& path);
133
134   // Loads a SkRegion into the GDI context.
135   static void LoadClippingRegionToDC(HDC context, const SkRegion& region,
136                                      const SkMatrix& transformation);
137 #elif defined(OS_MACOSX)
138   // Loads a SkPath into the CG context. The path can there after be used for
139   // clipping or as a stroke.
140   static void LoadPathToCGContext(CGContextRef context, const SkPath& path);
141
142   // Initializes the default settings and colors in a device context.
143   static void InitializeCGContext(CGContextRef context);
144
145   // Loads a SkRegion into the CG context.
146   static void LoadClippingRegionToCGContext(CGContextRef context,
147                                             const SkRegion& region,
148                                             const SkMatrix& transformation);
149 #endif
150
151  protected:
152 #if defined(OS_WIN)
153   // Arrays must be inside structures.
154   struct CubicPoints {
155     SkPoint p[4];
156   };
157   typedef std::vector<CubicPoints> CubicPath;
158   typedef std::vector<CubicPath> CubicPaths;
159
160   // Loads the specified Skia transform into the device context, excluding
161   // perspective (which GDI doesn't support).
162   static void LoadTransformToDC(HDC dc, const SkMatrix& matrix);
163
164   // Transforms SkPath's paths into a series of cubic path.
165   static bool SkPathToCubicPaths(CubicPaths* paths, const SkPath& skpath);
166 #elif defined(OS_MACOSX)
167   // Loads the specified Skia transform into the device context
168   static void LoadTransformToCGContext(CGContextRef context,
169                                        const SkMatrix& matrix);
170 #endif
171 };
172
173 }  // namespace skia
174
175 #endif  // SKIA_EXT_PLATFORM_DEVICE_H_