OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SKIA_EXT_PLATFORM_CANVAS_H_ | |
6 #define SKIA_EXT_PLATFORM_CANVAS_H_ | |
7 | |
8 // The platform-specific device will include the necessary platform headers | |
9 // to get the surface type. | |
10 #include "base/basictypes.h" | |
11 #include "skia/ext/platform_device.h" | |
12 #include "skia/ext/refptr.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 #include "third_party/skia/include/core/SkCanvas.h" | |
15 #include "third_party/skia/include/core/SkPixelRef.h" | |
16 #include "third_party/skia/include/core/SkPreConfig.h" | |
17 | |
18 namespace skia { | |
19 | |
20 typedef SkCanvas PlatformCanvas; | |
21 | |
22 // | |
23 // Note about error handling. | |
24 // | |
25 // Creating a canvas can fail at times, most often because we fail to allocate | |
26 // the backing-store (pixels). This can be from out-of-memory, or something | |
27 // more opaque, like GDI or cairo reported a failure. | |
28 // | |
29 // To allow the caller to handle the failure, every Create... factory takes an | |
30 // enum as its last parameter. The default value is kCrashOnFailure. If the | |
31 // caller passes kReturnNullOnFailure, then the caller is responsible to check | |
32 // the return result. | |
33 // | |
34 enum OnFailureType { | |
35 CRASH_ON_FAILURE, | |
36 RETURN_NULL_ON_FAILURE | |
37 }; | |
38 | |
39 #if defined(SK_BUILD_FOR_WIN32) | |
40 // The shared_section parameter is passed to gfx::PlatformDevice::create. | |
41 // See it for details. | |
42 SK_API SkCanvas* CreatePlatformCanvas(int width, | |
43 int height, | |
44 bool is_opaque, | |
45 HANDLE shared_section, | |
46 OnFailureType failure_type); | |
47 | |
48 // Draws the top layer of the canvas into the specified HDC. Only works | |
49 // with a PlatformCanvas with a BitmapPlatformDevice. | |
50 SK_API void DrawToNativeContext(SkCanvas* canvas, | |
51 HDC hdc, | |
52 int x, | |
53 int y, | |
54 const RECT* src_rect); | |
55 #elif defined(SK_BUILD_FOR_MAC) | |
56 SK_API SkCanvas* CreatePlatformCanvas(CGContextRef context, | |
57 int width, | |
58 int height, | |
59 bool is_opaque, | |
60 OnFailureType failure_type); | |
61 | |
62 SK_API SkCanvas* CreatePlatformCanvas(int width, | |
63 int height, | |
64 bool is_opaque, | |
65 uint8_t* context, | |
66 OnFailureType failure_type); | |
67 #elif defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) | |
68 // Linux --------------------------------------------------------------------- | |
69 | |
70 // Construct a canvas from the given memory region. The memory is not cleared | |
71 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. | |
72 SK_API SkCanvas* CreatePlatformCanvas(int width, | |
73 int height, | |
74 bool is_opaque, | |
75 uint8_t* data, | |
76 OnFailureType failure_type); | |
77 #endif | |
78 | |
79 static inline SkCanvas* CreatePlatformCanvas(int width, | |
80 int height, | |
81 bool is_opaque) { | |
82 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); | |
83 } | |
84 | |
85 SK_API SkCanvas* CreateCanvas(const skia::RefPtr<SkBaseDevice>& device, | |
86 OnFailureType failure_type); | |
87 | |
88 static inline SkCanvas* CreateBitmapCanvas(int width, | |
89 int height, | |
90 bool is_opaque) { | |
91 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); | |
92 } | |
93 | |
94 static inline SkCanvas* TryCreateBitmapCanvas(int width, | |
95 int height, | |
96 bool is_opaque) { | |
97 return CreatePlatformCanvas(width, height, is_opaque, 0, | |
98 RETURN_NULL_ON_FAILURE); | |
99 } | |
100 | |
101 // Return the stride (length of a line in bytes) for the given width. Because | |
102 // we use 32-bits per pixel, this will be roughly 4*width. However, for | |
103 // alignment reasons we may wish to increase that. | |
104 SK_API size_t PlatformCanvasStrideForWidth(unsigned width); | |
105 | |
106 // Returns the SkBaseDevice pointer of the topmost rect with a non-empty | |
107 // clip. In practice, this is usually either the top layer or nothing, since | |
108 // we usually set the clip to new layers when we make them. | |
109 // | |
110 // This may return NULL, so callers need to check. | |
111 // | |
112 // This is different than SkCanvas' getDevice, because that returns the | |
113 // bottommost device. | |
114 // | |
115 // Danger: the resulting device should not be saved. It will be invalidated | |
116 // by the next call to save() or restore(). | |
117 SK_API SkBaseDevice* GetTopDevice(const SkCanvas& canvas); | |
118 | |
119 // Returns true if native platform routines can be used to draw on the | |
120 // given canvas. If this function returns false, BeginPlatformPaint will | |
121 // return NULL PlatformSurface. | |
122 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); | |
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 | |
128 // surface returned here can be used with the native platform routines. | |
129 // | |
130 // Call EndPlatformPaint when you are done and want to use skia operations | |
131 // after calling the platform-specific BeginPlatformPaint; this will | |
132 // synchronize the bitmap to OS if necessary. | |
133 SK_API PlatformSurface BeginPlatformPaint(SkCanvas* canvas); | |
134 SK_API void EndPlatformPaint(SkCanvas* canvas); | |
135 | |
136 // Helper class for pairing calls to BeginPlatformPaint and EndPlatformPaint. | |
137 // Upon construction invokes BeginPlatformPaint, and upon destruction invokes | |
138 // EndPlatformPaint. | |
139 class SK_API ScopedPlatformPaint { | |
140 public: | |
141 explicit ScopedPlatformPaint(SkCanvas* canvas) : canvas_(canvas) { | |
142 platform_surface_ = BeginPlatformPaint(canvas); | |
143 } | |
144 ~ScopedPlatformPaint() { EndPlatformPaint(canvas_); } | |
145 | |
146 // Returns the PlatformSurface to use for native platform drawing calls. | |
147 PlatformSurface GetPlatformSurface() { return platform_surface_; } | |
148 private: | |
149 SkCanvas* canvas_; | |
150 PlatformSurface platform_surface_; | |
151 | |
152 // Disallow copy and assign | |
153 ScopedPlatformPaint(const ScopedPlatformPaint&); | |
154 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&); | |
155 }; | |
156 | |
157 // PlatformBitmap holds a PlatformSurface that can also be used as an SkBitmap. | |
158 class SK_API PlatformBitmap { | |
159 public: | |
160 PlatformBitmap(); | |
161 ~PlatformBitmap(); | |
162 | |
163 // Returns true if the bitmap was able to allocate its surface. | |
164 bool Allocate(int width, int height, bool is_opaque); | |
165 | |
166 // Returns the platform surface, or 0 if Allocate() did not return true. | |
167 PlatformSurface GetSurface() { return surface_; } | |
168 | |
169 // Return the skia bitmap, which will be empty if Allocate() did not | |
170 // return true. | |
171 // | |
172 // The resulting SkBitmap holds a refcount on the underlying platform surface, | |
173 // so the surface will remain allocated so long as the SkBitmap or its copies | |
174 // stay around. | |
175 const SkBitmap& GetBitmap() { return bitmap_; } | |
176 | |
177 private: | |
178 SkBitmap bitmap_; | |
179 PlatformSurface surface_; // initialized to 0 | |
180 intptr_t platform_extra_; // platform specific, initialized to 0 | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap); | |
183 }; | |
184 | |
185 } // namespace skia | |
186 | |
187 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ | |
OLD | NEW |