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/SkDevice.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. The PlatformDevice, and 54 // BitmapPlatformDevice classes inherit directly from SkDevice, which is no 55 // longer a supported usage-pattern for skia. In preparation of the removal of 56 // these classes, all calls to PlatformDevice::* should be routed through these 57 // helper functions. 58 59 // Bind a PlatformDevice instance, |platform_device| to |device|. Subsequent 60 // calls to the functions exported below will forward the request to the 61 // corresponding method on the bound PlatformDevice instance. If no 62 // PlatformDevice has been bound to the SkDevice passed, then the routines are 63 // NOPS. 64 SK_API void SetPlatformDevice(SkDevice* device, 65 PlatformDevice* platform_device); 66 SK_API PlatformDevice* GetPlatformDevice(SkDevice* device); 67 68 69 #if defined(OS_WIN) 70 // Initializes the default settings and colors in a device context. 71 SK_API void InitializeDC(HDC context); 72 #elif defined(OS_MACOSX) 73 // Returns the CGContext that backing the SkDevice. Forwards to the bound 74 // PlatformDevice. Returns NULL if no PlatformDevice is bound. 75 SK_API CGContextRef GetBitmapContext(SkDevice* device); 76 #endif 77 78 // Following routines are used in print preview workflow to mark the draft mode 79 // metafile and preview metafile. 80 SK_API SkMetaData& getMetaData(const SkCanvas& canvas); 81 SK_API void SetIsDraftMode(const SkCanvas& canvas, bool draft_mode); 82 SK_API bool IsDraftMode(const SkCanvas& canvas); 83 84 #if defined(OS_MACOSX) || defined(OS_WIN) 85 SK_API void SetIsPreviewMetafile(const SkCanvas& canvas, bool is_preview); 86 SK_API bool IsPreviewMetafile(const SkCanvas& canvas); 87 #endif 88 89 // A SkDevice is basically a wrapper around SkBitmap that provides a surface for 90 // SkCanvas to draw into. PlatformDevice provides a surface Windows can also 91 // write to. It also provides functionality to play well with GDI drawing 92 // functions. This class is abstract and must be subclassed. It provides the 93 // basic interface to implement it either with or without a bitmap backend. 94 // 95 // PlatformDevice provides an interface which sub-classes of SkDevice can also 96 // provide to allow for drawing by the native platform into the device. 97 class SK_API PlatformDevice { 98 public: 99 virtual ~PlatformDevice() {} 100 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 107 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(); 112 113 // Finish a previous call to beginPlatformPaint. 114 virtual void EndPlatformPaint(); 115 116 // Draws to the given screen DC, if the bitmap DC doesn't exist, this will 117 // temporarily create it. However, if you have created the bitmap DC, it will 118 // be more efficient if you don't free it until after this call so it doesn't 119 // have to be created twice. If src_rect is null, then the entirety of the 120 // source device will be copied. 121 virtual void DrawToNativeContext(PlatformSurface surface, int x, int y, 122 const PlatformRect* src_rect) = 0; 123 124 // Returns true if GDI operations can be used for drawing into the bitmap. 125 virtual bool SupportsPlatformPaint(); 126 127 #if defined(OS_WIN) 128 // Loads a SkPath into the GDI context. The path can there after be used for 129 // clipping or as a stroke. Returns false if the path failed to be loaded. 130 static bool LoadPathToDC(HDC context, const SkPath& path); 131 132 // Loads a SkRegion into the GDI context. 133 static void LoadClippingRegionToDC(HDC context, const SkRegion& region, 134 const SkMatrix& transformation); 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); 139 140 // Initializes the default settings and colors in a device context. 141 static void InitializeCGContext(CGContextRef context); 142 143 // Loads a SkRegion into the CG context. 144 static void LoadClippingRegionToCGContext(CGContextRef context, 145 const SkRegion& region, 146 const SkMatrix& transformation); 147 #endif 148 149 protected: 150 #if defined(OS_WIN) 151 // Arrays must be inside structures. 152 struct CubicPoints { 153 SkPoint p[4]; 154 }; 155 typedef std::vector<CubicPoints> CubicPath; 156 typedef std::vector<CubicPath> CubicPaths; 157 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); 161 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); 168 #endif 169 }; 170 171 } // namespace skia 172 173 #endif // SKIA_EXT_PLATFORM_DEVICE_H_ 174