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(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 31 32 namespace skia { 33 34 class PlatformDevice; 35 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 49 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. 54 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); 63 64 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 73 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); 79 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 84 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. 91 // 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: ~PlatformDevice()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