Chromium Code Reviews| 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 SkCanvas* CreatePlatformCanvas(int width, |
| 39 int height, | |
| 40 bool is_opaque, | |
| 41 HANDLE shared_section, | |
| 42 OnFailureType); | |
|
sky
2012/11/05 14:43:10
The style guide also says you should name all args
reed1
2012/11/05 19:14:13
Done.
| |
| 31 #elif defined(__APPLE__) | 43 #elif defined(__APPLE__) |
| 32 PlatformCanvas(int width, int height, bool is_opaque, | 44 SK_API SkCanvas* CreatePlatformCanvas(CGContextRef context, |
| 33 CGContextRef context); | 45 int width, |
| 34 PlatformCanvas(int width, int height, bool is_opaque, uint8_t* context); | 46 int height, |
| 47 bool is_opaque, | |
| 48 OnFailureType); | |
| 49 | |
| 50 SK_API SkCanvas* CreatePlatformCanvas(int width, | |
| 51 int height, | |
| 52 bool is_opaque, | |
| 53 uint8_t* context, | |
| 54 OnFailureType); | |
| 35 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 55 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ |
| 36 defined(__sun) || defined(ANDROID) | 56 defined(__sun) || defined(ANDROID) |
| 37 // Linux --------------------------------------------------------------------- | 57 // Linux --------------------------------------------------------------------- |
| 38 | 58 |
| 39 // Construct a canvas from the given memory region. The memory is not cleared | 59 // Construct a canvas from the given memory region. The memory is not cleared |
| 40 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. | 60 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. |
| 41 PlatformCanvas(int width, int height, bool is_opaque, uint8_t* data); | 61 SK_API SkCanvas* CreatePlatformCanvas(int width, |
| 62 int height, | |
| 63 bool is_opaque, | |
| 64 uint8_t* data, | |
| 65 OnFailureType); | |
| 42 #endif | 66 #endif |
| 43 | 67 |
| 44 virtual ~PlatformCanvas(); | 68 static inline SkCanvas* CreatePlatformCanvas(int width, |
| 69 int height, | |
| 70 bool is_opaque) { | |
| 71 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); | |
| 72 } | |
| 45 | 73 |
| 46 #if defined(WIN32) | 74 // Takes ownership of the device, so the caller need not call unref(). |
| 47 // For two-part init, call if you use the no-argument constructor above. Note | 75 SK_API SkCanvas* CreateCanvas(SkDevice* device, OnFailureType); |
| 48 // that we want this to optionally match the Linux initialize if you only | |
| 49 // pass 3 arguments, hence the evil default argument. | |
| 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 | 76 |
| 57 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 77 static inline SkCanvas* CreateBitmapCanvas(int width, |
| 58 defined(__sun) || defined(ANDROID) | 78 int height, |
| 59 // For two-part init, call if you use the no-argument constructor above | 79 bool is_opaque) { |
| 60 bool initialize(int width, int height, bool is_opaque, uint8_t* data = NULL); | 80 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); |
| 61 #endif | 81 } |
| 62 | 82 |
| 63 // Shared -------------------------------------------------------------------- | 83 static inline SkCanvas* TryCreateBitmapCanvas(int width, |
| 84 int height, | |
| 85 bool is_opaque) { | |
| 86 return CreatePlatformCanvas(width, height, is_opaque, 0, | |
| 87 RETURN_NULL_ON_FAILURE); | |
| 88 } | |
| 64 | 89 |
| 65 // Return the stride (length of a line in bytes) for the given width. Because | 90 class SK_API ScopedPlatformCanvas : public SkAutoTUnref<SkCanvas> { |
| 66 // we use 32-bits per pixel, this will be roughly 4*width. However, for | 91 public: |
| 67 // alignment reasons we may wish to increase that. | 92 ScopedPlatformCanvas(int width, int height, bool is_opaque) : |
| 68 static size_t StrideForWidth(unsigned width); | 93 SkAutoTUnref<SkCanvas>(CreatePlatformCanvas(width, height, is_opaque)) {} |
|
sky
2012/11/05 14:43:10
nit: indent 4 and ':' on next line.
reed1
2012/11/05 19:14:13
Done.
| |
| 94 }; | |
| 69 | 95 |
| 70 // Allow callers to see the non-virtual function even though we have an | 96 // Return the stride (length of a line in bytes) for the given width. Because |
| 71 // override of a virtual one. | 97 // we use 32-bits per pixel, this will be roughly 4*width. However, for |
| 72 // FIXME(brettw) is this necessary? | 98 // alignment reasons we may wish to increase that. |
| 73 using SkCanvas::clipRect; | 99 SK_API size_t PlatformCanvasStrideForWidth(unsigned width); |
| 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 | 100 |
| 84 // Returns the SkDevice pointer of the topmost rect with a non-empty | 101 // 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 | 102 // 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. | 103 // we usually set the clip to new layers when we make them. |
| 87 // | 104 // |
| 88 // If there is no layer that is not all clipped out, this will return a | 105 // 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 | 106 // dummy device so callers do not have to check. If you are concerned about |
| 90 // performance, check the clip before doing any painting. | 107 // performance, check the clip before doing any painting. |
| 91 // | 108 // |
| 92 // This is different than SkCanvas' getDevice, because that returns the | 109 // This is different than SkCanvas' getDevice, because that returns the |
| 93 // bottommost device. | 110 // bottommost device. |
| 94 // | 111 // |
| 95 // Danger: the resulting device should not be saved. It will be invalidated | 112 // Danger: the resulting device should not be saved. It will be invalidated |
| 96 // by the next call to save() or restore(). | 113 // by the next call to save() or restore(). |
| 97 SK_API SkDevice* GetTopDevice(const SkCanvas& canvas); | 114 SK_API SkDevice* GetTopDevice(const SkCanvas& canvas); |
| 98 | 115 |
| 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 | 116 // Returns true if native platform routines can be used to draw on the |
| 112 // given canvas. If this function returns false, BeginPlatformPaint will | 117 // given canvas. If this function returns false, BeginPlatformPaint will |
| 113 // return NULL PlatformSurface. | 118 // return NULL PlatformSurface. |
| 114 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); | 119 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); |
| 115 | 120 |
| 116 // Draws into the a native platform surface, |context|. Forwards to | 121 // Draws into the a native platform surface, |context|. Forwards to |
| 117 // DrawToNativeContext on a PlatformDevice instance bound to the top device. | 122 // DrawToNativeContext on a PlatformDevice instance bound to the top device. |
| 118 // If no PlatformDevice instance is bound, is a no-operation. | 123 // If no PlatformDevice instance is bound, is a no-operation. |
| 119 SK_API void DrawToNativeContext(SkCanvas* canvas, PlatformSurface context, | 124 SK_API void DrawToNativeContext(SkCanvas* canvas, |
| 120 int x, int y, const PlatformRect* src_rect); | 125 PlatformSurface context, |
| 126 int x, | |
| 127 int y, | |
| 128 const PlatformRect* src_rect); | |
| 121 | 129 |
| 122 // Sets the opacity of each pixel in the specified region to be opaque. | 130 // Sets the opacity of each pixel in the specified region to be opaque. |
| 123 SK_API void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height); | 131 SK_API void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height); |
| 124 | 132 |
| 125 // These calls should surround calls to platform drawing routines, the | 133 // These calls should surround calls to platform drawing routines, the |
| 126 // surface returned here can be used with the native platform routines. | 134 // surface returned here can be used with the native platform routines. |
| 127 // | 135 // |
| 128 // Call EndPlatformPaint when you are done and want to use skia operations | 136 // Call EndPlatformPaint when you are done and want to use skia operations |
| 129 // after calling the platform-specific BeginPlatformPaint; this will | 137 // after calling the platform-specific BeginPlatformPaint; this will |
| 130 // synchronize the bitmap to OS if necessary. | 138 // synchronize the bitmap to OS if necessary. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 private: | 178 private: |
| 171 SkBitmap bitmap_; | 179 SkBitmap bitmap_; |
| 172 PlatformSurface surface_; | 180 PlatformSurface surface_; |
| 173 | 181 |
| 174 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap); | 182 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap); |
| 175 }; | 183 }; |
| 176 | 184 |
| 177 } // namespace skia | 185 } // namespace skia |
| 178 | 186 |
| 179 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ | 187 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ |
| OLD | NEW |