OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SKIA_EXT_PLATFORM_CANVAS_H_ | 5 #ifndef SKIA_EXT_PLATFORM_CANVAS_H_ |
6 #define SKIA_EXT_PLATFORM_CANVAS_H_ | 6 #define SKIA_EXT_PLATFORM_CANVAS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // The platform-specific device will include the necessary platform headers | 9 // The platform-specific device will include the necessary platform headers |
10 // to get the surface type. | 10 // to get the surface type. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); | 56 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); |
57 | 57 |
58 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 58 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ |
59 defined(__Solaris__) | 59 defined(__Solaris__) |
60 // For two-part init, call if you use the no-argument constructor above | 60 // For two-part init, call if you use the no-argument constructor above |
61 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); | 61 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); |
62 #endif | 62 #endif |
63 | 63 |
64 // Shared -------------------------------------------------------------------- | 64 // Shared -------------------------------------------------------------------- |
65 | 65 |
66 // These calls should surround calls to platform drawing routines, the | 66 // Returns the SkDevice pointer of the topmost rect with a non-empty |
67 // surface returned here can be used with the native platform routines | |
68 // | |
69 // Call endPlatformPaint when you are done and want to use Skia operations | |
70 // after calling the platform-specific beginPlatformPaint; this will | |
71 // synchronize the bitmap to OS if necessary. | |
72 PlatformDevice::PlatformSurface beginPlatformPaint() const; | |
73 void endPlatformPaint() const; | |
74 | |
75 // Returns the platform device pointer of the topmost rect with a non-empty | |
76 // clip. In practice, this is usually either the top layer or nothing, since | 67 // clip. In practice, this is usually either the top layer or nothing, since |
77 // we usually set the clip to new layers when we make them. | 68 // we usually set the clip to new layers when we make them. |
78 // | 69 // |
79 // If there is no layer that is not all clipped out, this will return a | 70 // If there is no layer that is not all clipped out, this will return a |
80 // dummy device so callers do not have to check. If you are concerned about | 71 // dummy device so callers do not have to check. If you are concerned about |
81 // performance, check the clip before doing any painting. | 72 // performance, check the clip before doing any painting. |
82 // | 73 // |
83 // This is different than SkCanvas' getDevice, because that returns the | 74 // This is different than SkCanvas' getDevice, because that returns the |
84 // bottommost device. | 75 // bottommost device. |
85 // | 76 // |
86 // Danger: the resulting device should not be saved. It will be invalidated | 77 // Danger: the resulting device should not be saved. It will be invalidated |
87 // by the next call to save() or restore(). | 78 // by the next call to save() or restore(). |
88 PlatformDevice& getTopPlatformDevice() const; | 79 SkDevice& getTopDevice() const; |
89 | 80 |
90 // Return the stride (length of a line in bytes) for the given width. Because | 81 // Return the stride (length of a line in bytes) for the given width. Because |
91 // we use 32-bits per pixel, this will be roughly 4*width. However, for | 82 // we use 32-bits per pixel, this will be roughly 4*width. However, for |
92 // alignment reasons we may wish to increase that. | 83 // alignment reasons we may wish to increase that. |
93 static size_t StrideForWidth(unsigned width); | 84 static size_t StrideForWidth(unsigned width); |
94 | 85 |
95 // Allow callers to see the non-virtual function even though we have an | 86 // Allow callers to see the non-virtual function even though we have an |
96 // override of a virtual one. | 87 // override of a virtual one. |
97 // FIXME(brettw) is this necessary? | 88 // FIXME(brettw) is this necessary? |
98 using SkCanvas::clipRect; | 89 using SkCanvas::clipRect; |
99 | 90 |
100 private: | 91 private: |
101 // Helper method used internally by the initialize() methods. | 92 // Helper method used internally by the initialize() methods. |
102 bool initializeWithDevice(SkDevice* device); | 93 bool initializeWithDevice(SkDevice* device); |
103 | 94 |
104 // Unimplemented. This is to try to prevent people from calling this function | 95 // Unimplemented. This is to try to prevent people from calling this function |
105 // on SkCanvas. SkCanvas' version is not virtual, so we can't prevent this | 96 // on SkCanvas. SkCanvas' version is not virtual, so we can't prevent this |
106 // 100%, but hopefully this will make people notice and not use the function. | 97 // 100%, but hopefully this will make people notice and not use the function. |
107 // Calling SkCanvas' version will create a new device which is not compatible | 98 // Calling SkCanvas' version will create a new device which is not compatible |
108 // with us and we will crash if somebody tries to draw into it with | 99 // with us and we will crash if somebody tries to draw into it with |
109 // CoreGraphics. | 100 // CoreGraphics. |
110 virtual SkDevice* setBitmapDevice(const SkBitmap& bitmap); | 101 virtual SkDevice* setBitmapDevice(const SkBitmap& bitmap); |
111 | 102 |
112 // Disallow copy and assign. | 103 // Disallow copy and assign |
113 PlatformCanvas(const PlatformCanvas&); | 104 PlatformCanvas(const PlatformCanvas&); |
114 PlatformCanvas& operator=(const PlatformCanvas&); | 105 PlatformCanvas& operator=(const PlatformCanvas&); |
115 }; | 106 }; |
116 | 107 |
117 // Creates a canvas with raster bitmap backing. | 108 // Creates a canvas with raster bitmap backing. |
118 // Set is_opaque if you are going to erase the bitmap and not use | 109 // Set is_opaque if you are going to erase the bitmap and not use |
119 // transparency: this will enable some optimizations. | 110 // transparency: this will enable some optimizations. |
120 SK_API SkCanvas* CreateBitmapCanvas(int width, int height, bool is_opaque); | 111 SK_API SkCanvas* CreateBitmapCanvas(int width, int height, bool is_opaque); |
121 | 112 |
122 // Returns true if native platform routines can be used to draw on the | 113 // Returns true if native platform routines can be used to draw on the |
123 // given canvas. If this function returns false, BeginPlatformPaint will | 114 // given canvas. If this function returns false, BeginPlatformPaint will |
124 // return NULL PlatformSurface. | 115 // return NULL PlatformSurface. |
125 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); | 116 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); |
126 | 117 |
| 118 // Draws into the a native platform surface, |context|. Forwards to |
| 119 // DrawToNativeContext on a PlatformDevice instance bound to the top device. |
| 120 // If no PlatformDevice instance is bound, is a no-operation. |
| 121 SK_API void DrawToNativeContext(SkCanvas* canvas, PlatformSurface context, |
| 122 int x, int y, const PlatformRect* src_rect); |
| 123 |
| 124 // Sets the opacity of each pixel in the specified region to be opaque. |
| 125 SK_API void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height); |
| 126 |
127 // These calls should surround calls to platform drawing routines, the | 127 // These calls should surround calls to platform drawing routines, the |
128 // surface returned here can be used with the native platform routines. | 128 // surface returned here can be used with the native platform routines. |
129 // | 129 // |
130 // Call EndPlatformPaint when you are done and want to use skia operations | 130 // Call EndPlatformPaint when you are done and want to use skia operations |
131 // after calling the platform-specific BeginPlatformPaint; this will | 131 // after calling the platform-specific BeginPlatformPaint; this will |
132 // synchronize the bitmap to OS if necessary. | 132 // synchronize the bitmap to OS if necessary. |
133 // | 133 SK_API PlatformSurface BeginPlatformPaint(SkCanvas* canvas); |
134 // Note: These functions will eventually replace | |
135 // PlatformCanvas::beginPlatformPaint and PlatformCanvas::endPlatformPaint. | |
136 SK_API PlatformDevice::PlatformSurface BeginPlatformPaint(SkCanvas* canvas); | |
137 SK_API void EndPlatformPaint(SkCanvas* canvas); | 134 SK_API void EndPlatformPaint(SkCanvas* canvas); |
138 | 135 |
| 136 // Helper class for pairing calls to BeginPlatformPaint and EndPlatformPaint. |
| 137 // Upon construction invokes BeginPlatformPaint, and upon destruction invokes |
| 138 class ScopedPlatformPaint { |
| 139 public: |
| 140 explicit ScopedPlatformPaint(SkCanvas* canvas) : canvas_(canvas) { |
| 141 platform_surface_ = BeginPlatformPaint(canvas); |
| 142 } |
| 143 ~ScopedPlatformPaint() { EndPlatformPaint(canvas_); } |
| 144 |
| 145 // Returns the PlatformSurface to use for native platform drawing calls. |
| 146 PlatformSurface GetPlatformSurface() { return platform_surface_; } |
| 147 private: |
| 148 SkCanvas* canvas_; |
| 149 PlatformSurface platform_surface_; |
| 150 |
| 151 // Disallow copy and assign |
| 152 ScopedPlatformPaint(const ScopedPlatformPaint&); |
| 153 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&); |
| 154 }; |
| 155 |
139 } // namespace skia | 156 } // namespace skia |
140 | 157 |
141 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ | 158 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ |
OLD | NEW |