Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / skia / ext / platform_device.h
blobc903c87c08e816c1be7fd1cec3659d9242b9380e
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"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #include <vector>
13 #endif
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"
19 class SkMatrix;
20 class SkMetaData;
21 class SkPath;
22 class SkRegion;
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;
30 #endif
32 namespace skia {
34 class PlatformDevice;
36 #if defined(OS_WIN)
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;
45 #else
46 typedef void* PlatformSurface;
47 typedef SkIRect* PlatformRect;
48 #endif
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
53 // helper functions.
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
59 // routines are NOPS.
60 SK_API void SetPlatformDevice(SkBaseDevice* device,
61 PlatformDevice* platform_device);
62 SK_API PlatformDevice* GetPlatformDevice(SkBaseDevice* device);
65 #if defined(OS_WIN)
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);
72 #endif
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);
83 #endif
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
90 // a bitmap backend.
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
96 // from it.
97 class SK_API PlatformDevice {
98 public:
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;
106 #endif
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();
119 #if defined(OS_WIN)
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);
134 #endif
136 protected:
137 #if defined(OS_WIN)
138 // Arrays must be inside structures.
139 struct CubicPoints {
140 SkPoint p[4];
142 typedef std::vector<CubicPoints> CubicPath;
143 typedef std::vector<CubicPath> CubicPaths;
145 // Loads the specified Skia transform into the device context, excluding
146 // perspective (which GDI doesn't support).
147 static void LoadTransformToDC(HDC dc, const SkMatrix& matrix);
149 // Transforms SkPath's paths into a series of cubic path.
150 static bool SkPathToCubicPaths(CubicPaths* paths, const SkPath& skpath);
151 #endif
154 } // namespace skia
156 #endif // SKIA_EXT_PLATFORM_DEVICE_H_