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 | 7 |
8 // The platform-specific device will include the necessary platform headers | 8 // The platform-specific device will include the necessary platform headers |
9 // to get the surface type. | 9 // to get the surface type. |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "skia/ext/platform_device.h" | 11 #include "skia/ext/platform_device.h" |
12 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
13 | 13 |
14 namespace skia { | 14 namespace skia { |
15 | 15 |
16 // This class is a specialization of the regular SkCanvas that is designed to | 16 typedef SkCanvas PlatformCanvas; |
17 // work with a PlatformDevice to manage platform-specific drawing. It allows | 17 |
18 // using both Skia operations and platform-specific operations. | 18 /* |
19 class SK_API PlatformCanvas : public SkCanvas { | 19 * Note about error handling. |
20 public: | 20 * |
21 // If you use the version with no arguments, you MUST call initialize() | 21 * Creating a canvas can fail at times, most often because we fail to allocate |
22 PlatformCanvas(); | 22 * the backing-store (pixels). This can be from out-of-memory, or something |
23 // Set is_opaque if you are going to erase the bitmap and not use | 23 * more opaque, like GDI or cairo reported a failure. |
24 // transparency: this will enable some optimizations. | 24 * |
25 PlatformCanvas(int width, int height, bool is_opaque); | 25 * To allow the caller to handle the failure, every Create... factory takes an |
26 * enum as its last parameter. The default value is kCrashOnFailure. If the | |
27 * caller passes kReturnNullOnFailure, then the caller is responsible to check | |
28 * the return result. | |
29 */ | |
30 enum OnFailureType { | |
31 CRASH_ON_FAILURE, | |
32 RETURN_NULL_ON_FAILURE | |
33 }; | |
26 | 34 |
27 #if defined(WIN32) | 35 #if defined(WIN32) |
28 // The shared_section parameter is passed to gfx::PlatformDevice::create. | 36 // The shared_section parameter is passed to gfx::PlatformDevice::create. |
29 // See it for details. | 37 // See it for details. |
30 PlatformCanvas(int width, int height, bool is_opaque, HANDLE shared_section); | 38 SK_API |
sky
2012/11/01 17:41:39
nit: see base/file_util.h for how we generally use
reed1
2012/11/01 21:07:21
Done.
| |
39 SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque, | |
sky
2012/11/01 17:41:39
nit: when you wrap, one param per line.
reed1
2012/11/01 21:07:21
Done.
| |
40 HANDLE shared_section = 0, | |
41 OnFailureType = CRASH_ON_FAILURE); | |
sky
2012/11/01 17:41:39
Style guide says no default values.
reed1
2012/11/01 21:07:21
Done.
| |
31 #elif defined(__APPLE__) | 42 #elif defined(__APPLE__) |
32 PlatformCanvas(int width, int height, bool is_opaque, | 43 SK_API |
33 CGContextRef context); | 44 SkCanvas* CreatePlatformCanvas(CGContextRef context, int width, int height, |
34 PlatformCanvas(int width, int height, bool is_opaque, uint8_t* context); | 45 bool is_opaque, |
46 OnFailureType = CRASH_ON_FAILURE); | |
47 | |
48 SK_API | |
49 SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque, | |
50 uint8_t* context = NULL, | |
51 OnFailureType = CRASH_ON_FAILURE); | |
35 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 52 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ |
36 defined(__sun) || defined(ANDROID) | 53 defined(__sun) || defined(ANDROID) |
37 // Linux --------------------------------------------------------------------- | 54 // Linux --------------------------------------------------------------------- |
38 | 55 |
39 // Construct a canvas from the given memory region. The memory is not cleared | 56 // Construct a canvas from the given memory region. The memory is not cleared |
40 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. | 57 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. |
41 PlatformCanvas(int width, int height, bool is_opaque, uint8_t* data); | 58 SK_API |
59 SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque, | |
60 uint8_t* data = NULL, | |
61 OnFailureType = CRASH_ON_FAILURE); | |
42 #endif | 62 #endif |
43 | 63 |
44 virtual ~PlatformCanvas(); | 64 // Takes ownership of the device, so the caller need not call unref(). |
65 SK_API | |
66 SkCanvas* CreateCanvas(SkDevice* device, OnFailureType = CRASH_ON_FAILURE); | |
45 | 67 |
46 #if defined(WIN32) | 68 static inline SkCanvas* CreateBitmapCanvas(int width, int height, |
47 // For two-part init, call if you use the no-argument constructor above. Note | 69 bool is_opaque) { |
48 // that we want this to optionally match the Linux initialize if you only | 70 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); |
49 // pass 3 arguments, hence the evil default argument. | 71 } |
50 bool initialize(int width, int height, bool is_opaque, | |
51 HANDLE shared_section = NULL); | |
52 #elif defined(__APPLE__) | |
53 // For two-part init, call if you use the no-argument constructor above | |
54 bool initialize(CGContextRef context, int width, int height, bool is_opaque); | |
55 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); | |
56 | 72 |
57 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 73 static inline SkCanvas* TryCreateBitmapCanvas(int width, int height, |
58 defined(__sun) || defined(ANDROID) | 74 bool is_opaque) { |
59 // For two-part init, call if you use the no-argument constructor above | 75 return CreatePlatformCanvas(width, height, is_opaque, 0, |
60 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); | 76 RETURN_NULL_ON_FAILURE); |
61 #endif | 77 } |
62 | 78 |
63 // Shared -------------------------------------------------------------------- | 79 // Return the stride (length of a line in bytes) for the given width. Because |
64 | 80 // we use 32-bits per pixel, this will be roughly 4*width. However, for |
65 // Return the stride (length of a line in bytes) for the given width. Because | 81 // alignment reasons we may wish to increase that. |
66 // we use 32-bits per pixel, this will be roughly 4*width. However, for | 82 SK_API size_t PlatformCanvasStrideForWidth(unsigned width); |
67 // alignment reasons we may wish to increase that. | |
68 static size_t StrideForWidth(unsigned width); | |
69 | |
70 // Allow callers to see the non-virtual function even though we have an | |
71 // override of a virtual one. | |
72 // FIXME(brettw) is this necessary? | |
73 using SkCanvas::clipRect; | |
74 | |
75 private: | |
76 // Helper method used internally by the initialize() methods. | |
77 bool initializeWithDevice(SkDevice* device); | |
78 | |
79 // Disallow copy and assign | |
80 PlatformCanvas(const PlatformCanvas&); | |
81 PlatformCanvas& operator=(const PlatformCanvas&); | |
82 }; | |
83 | 83 |
84 // Returns the SkDevice pointer of the topmost rect with a non-empty | 84 // Returns the SkDevice pointer of the topmost rect with a non-empty |
85 // clip. In practice, this is usually either the top layer or nothing, since | 85 // clip. In practice, this is usually either the top layer or nothing, since |
86 // we usually set the clip to new layers when we make them. | 86 // we usually set the clip to new layers when we make them. |
87 // | 87 // |
88 // If there is no layer that is not all clipped out, this will return a | 88 // If there is no layer that is not all clipped out, this will return a |
89 // dummy device so callers do not have to check. If you are concerned about | 89 // dummy device so callers do not have to check. If you are concerned about |
90 // performance, check the clip before doing any painting. | 90 // performance, check the clip before doing any painting. |
91 // | 91 // |
92 // This is different than SkCanvas' getDevice, because that returns the | 92 // This is different than SkCanvas' getDevice, because that returns the |
93 // bottommost device. | 93 // bottommost device. |
94 // | 94 // |
95 // Danger: the resulting device should not be saved. It will be invalidated | 95 // Danger: the resulting device should not be saved. It will be invalidated |
96 // by the next call to save() or restore(). | 96 // by the next call to save() or restore(). |
97 SK_API SkDevice* GetTopDevice(const SkCanvas& canvas); | 97 SK_API SkDevice* GetTopDevice(const SkCanvas& canvas); |
98 | 98 |
99 // Creates a canvas with raster bitmap backing. | |
100 // Set is_opaque if you are going to erase the bitmap and not use | |
101 // transparency: this will enable some optimizations. | |
102 SK_API SkCanvas* CreateBitmapCanvas(int width, int height, bool is_opaque); | |
103 | |
104 // Non-crashing version of CreateBitmapCanvas | |
105 // returns NULL if allocation fails for any reason. | |
106 // Use this instead of CreateBitmapCanvas in places that are likely to | |
107 // attempt to allocate very large canvases (therefore likely to fail), | |
108 // and where it is possible to recover gracefully from the failed allocation. | |
109 SK_API SkCanvas* TryCreateBitmapCanvas(int width, int height, bool is_opaque); | |
110 | |
111 // Returns true if native platform routines can be used to draw on the | 99 // Returns true if native platform routines can be used to draw on the |
112 // given canvas. If this function returns false, BeginPlatformPaint will | 100 // given canvas. If this function returns false, BeginPlatformPaint will |
113 // return NULL PlatformSurface. | 101 // return NULL PlatformSurface. |
114 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); | 102 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); |
115 | 103 |
116 // Draws into the a native platform surface, |context|. Forwards to | 104 // Draws into the a native platform surface, |context|. Forwards to |
117 // DrawToNativeContext on a PlatformDevice instance bound to the top device. | 105 // DrawToNativeContext on a PlatformDevice instance bound to the top device. |
118 // If no PlatformDevice instance is bound, is a no-operation. | 106 // If no PlatformDevice instance is bound, is a no-operation. |
119 SK_API void DrawToNativeContext(SkCanvas* canvas, PlatformSurface context, | 107 SK_API void DrawToNativeContext(SkCanvas* canvas, PlatformSurface context, |
120 int x, int y, const PlatformRect* src_rect); | 108 int x, int y, const PlatformRect* src_rect); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 private: | 158 private: |
171 SkBitmap bitmap_; | 159 SkBitmap bitmap_; |
172 PlatformSurface surface_; | 160 PlatformSurface surface_; |
173 | 161 |
174 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap); | 162 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap); |
175 }; | 163 }; |
176 | 164 |
177 } // namespace skia | 165 } // namespace skia |
178 | 166 |
179 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ | 167 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ |
OLD | NEW |