| 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 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 // The platform-specific device will include the necessary platform headers | 11 // The platform-specific device will include the necessary platform headers |
| 12 // to get the surface type. | 12 // to get the surface type. |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "skia/ext/native_drawing_context.h" | 14 #include "skia/ext/native_drawing_context.h" |
| 15 #include "skia/ext/cdl_canvas.h" |
| 16 #include "skia/ext/cdl_paint.h" |
| 17 #include "skia/ext/cdl_shader.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "third_party/skia/include/core/SkCanvas.h" | 19 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "third_party/skia/include/core/SkPixelRef.h" | 20 #include "third_party/skia/include/core/SkPixelRef.h" |
| 18 #include "third_party/skia/include/core/SkPixmap.h" | 21 #include "third_party/skia/include/core/SkPixmap.h" |
| 19 | 22 |
| 20 class SkBaseDevice; | 23 class SkBaseDevice; |
| 21 | 24 |
| 22 // A PlatformCanvas is a software-rasterized SkCanvas which is *also* | 25 // A PlatformCanvas is a software-rasterized SkCanvas which is *also* |
| 23 // addressable by the platform-specific drawing API (GDI, Core Graphics, | 26 // addressable by the platform-specific drawing API (GDI, Core Graphics, |
| 24 // Cairo...). | 27 // Cairo...). |
| (...skipping 13 matching lines...) Expand all Loading... |
| 38 // the return result. | 41 // the return result. |
| 39 // | 42 // |
| 40 enum OnFailureType { | 43 enum OnFailureType { |
| 41 CRASH_ON_FAILURE, | 44 CRASH_ON_FAILURE, |
| 42 RETURN_NULL_ON_FAILURE | 45 RETURN_NULL_ON_FAILURE |
| 43 }; | 46 }; |
| 44 | 47 |
| 45 #if defined(WIN32) | 48 #if defined(WIN32) |
| 46 // The shared_section parameter is passed to gfx::PlatformDevice::create. | 49 // The shared_section parameter is passed to gfx::PlatformDevice::create. |
| 47 // See it for details. | 50 // See it for details. |
| 48 SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvas( | 51 SK_API std::unique_ptr<CdlCanvas> CreatePlatformCanvas( |
| 49 int width, | 52 int width, |
| 50 int height, | 53 int height, |
| 51 bool is_opaque, | 54 bool is_opaque, |
| 52 HANDLE shared_section, | 55 HANDLE shared_section, |
| 53 OnFailureType failure_type); | 56 OnFailureType failure_type); |
| 54 | 57 |
| 55 // Draws the top layer of the canvas into the specified HDC. Only works | 58 // Draws the top layer of the canvas into the specified HDC. Only works |
| 56 // with a SkCanvas with a BitmapPlatformDevice. Will create a temporary | 59 // with a SkCanvas with a BitmapPlatformDevice. Will create a temporary |
| 57 // HDC to back the canvas if one doesn't already exist, tearing it down | 60 // HDC to back the canvas if one doesn't already exist, tearing it down |
| 58 // before returning. If |src_rect| is null, copies the entire canvas. | 61 // before returning. If |src_rect| is null, copies the entire canvas. |
| 59 SK_API void DrawToNativeContext(SkCanvas* canvas, | 62 SK_API void DrawToNativeContext(CdlCanvas* canvas, |
| 60 HDC hdc, | 63 HDC hdc, |
| 61 int x, | 64 int x, |
| 62 int y, | 65 int y, |
| 63 const RECT* src_rect); | 66 const RECT* src_rect); |
| 64 #elif defined(__APPLE__) | 67 #elif defined(__APPLE__) |
| 65 SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvas( | 68 SK_API std::unique_ptr<CdlCanvas> CreatePlatformCanvas( |
| 66 CGContextRef context, | 69 CGContextRef context, |
| 67 int width, | 70 int width, |
| 68 int height, | 71 int height, |
| 69 bool is_opaque, | 72 bool is_opaque, |
| 70 OnFailureType failure_type); | 73 OnFailureType failure_type); |
| 71 | 74 |
| 72 SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvas( | 75 SK_API std::unique_ptr<CdlCanvas> CreatePlatformCanvas( |
| 73 int width, | 76 int width, |
| 74 int height, | 77 int height, |
| 75 bool is_opaque, | 78 bool is_opaque, |
| 76 uint8_t* context, | 79 uint8_t* context, |
| 77 OnFailureType failure_type); | 80 OnFailureType failure_type); |
| 78 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ | 81 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ |
| 79 defined(__sun) || defined(ANDROID) | 82 defined(__sun) || defined(ANDROID) |
| 80 // Linux --------------------------------------------------------------------- | 83 // Linux --------------------------------------------------------------------- |
| 81 | 84 |
| 82 // Construct a canvas from the given memory region. The memory is not cleared | 85 // Construct a canvas from the given memory region. The memory is not cleared |
| 83 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. | 86 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. |
| 84 SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvas( | 87 SK_API std::unique_ptr<CdlCanvas> CreatePlatformCanvas( |
| 85 int width, | 88 int width, |
| 86 int height, | 89 int height, |
| 87 bool is_opaque, | 90 bool is_opaque, |
| 88 uint8_t* data, | 91 uint8_t* data, |
| 89 OnFailureType failure_type); | 92 OnFailureType failure_type); |
| 90 #endif | 93 #endif |
| 91 | 94 |
| 92 static inline std::unique_ptr<SkCanvas> CreatePlatformCanvas(int width, | 95 static inline std::unique_ptr<CdlCanvas> CreatePlatformCanvas(int width, |
| 93 int height, | 96 int height, |
| 94 bool is_opaque) { | 97 bool is_opaque) { |
| 95 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); | 98 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); |
| 96 } | 99 } |
| 97 | 100 |
| 98 SK_API std::unique_ptr<SkCanvas> CreateCanvas(const sk_sp<SkBaseDevice>& device, | 101 SK_API std::unique_ptr<CdlCanvas> CreateCanvas( |
| 99 OnFailureType failure_type); | 102 const sk_sp<SkBaseDevice>& device, |
| 103 OnFailureType failure_type); |
| 100 | 104 |
| 101 static inline std::unique_ptr<SkCanvas> CreateBitmapCanvas(int width, | 105 static inline std::unique_ptr<CdlCanvas> CreateBitmapCanvas(int width, |
| 102 int height, | 106 int height, |
| 103 bool is_opaque) { | 107 bool is_opaque) { |
| 104 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); | 108 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE); |
| 105 } | 109 } |
| 106 | 110 |
| 107 static inline std::unique_ptr<SkCanvas> TryCreateBitmapCanvas(int width, | 111 static inline std::unique_ptr<CdlCanvas> TryCreateBitmapCanvas(int width, |
| 108 int height, | 112 int height, |
| 109 bool is_opaque) { | 113 bool is_opaque) { |
| 110 return CreatePlatformCanvas(width, height, is_opaque, 0, | 114 return CreatePlatformCanvas(width, height, is_opaque, 0, |
| 111 RETURN_NULL_ON_FAILURE); | 115 RETURN_NULL_ON_FAILURE); |
| 112 } | 116 } |
| 113 | 117 |
| 114 // Return the stride (length of a line in bytes) for the given width. Because | 118 // Return the stride (length of a line in bytes) for the given width. Because |
| 115 // we use 32-bits per pixel, this will be roughly 4*width. However, for | 119 // we use 32-bits per pixel, this will be roughly 4*width. However, for |
| 116 // alignment reasons we may wish to increase that. | 120 // alignment reasons we may wish to increase that. |
| 117 SK_API size_t PlatformCanvasStrideForWidth(unsigned width); | 121 SK_API size_t PlatformCanvasStrideForWidth(unsigned width); |
| 118 | 122 |
| 119 // Copies pixels from the SkCanvas into an SkBitmap, fetching pixels from | 123 // Copies pixels from the SkCanvas into an SkBitmap, fetching pixels from |
| 120 // GPU memory if necessary. | 124 // GPU memory if necessary. |
| 121 // | 125 // |
| 122 // The bitmap will remain empty if we can't allocate enough memory for a copy | 126 // The bitmap will remain empty if we can't allocate enough memory for a copy |
| 123 // of the pixels. | 127 // of the pixels. |
| 124 SK_API SkBitmap ReadPixels(SkCanvas* canvas); | 128 SK_API SkBitmap ReadPixels(CdlCanvas* canvas); |
| 125 | 129 |
| 126 // Gives the pixmap passed in *writable* access to the pixels backing this | 130 // Gives the pixmap passed in *writable* access to the pixels backing this |
| 127 // canvas. All writes to the pixmap should be visible if the canvas is | 131 // canvas. All writes to the pixmap should be visible if the canvas is |
| 128 // raster-backed. | 132 // raster-backed. |
| 129 // | 133 // |
| 130 // Returns false on failure: if either argument is nullptr, or if the | 134 // Returns false on failure: if either argument is nullptr, or if the |
| 131 // pixels can not be retrieved from the canvas. In the latter case resets | 135 // pixels can not be retrieved from the canvas. In the latter case resets |
| 132 // the pixmap to empty. | 136 // the pixmap to empty. |
| 133 SK_API bool GetWritablePixels(SkCanvas* canvas, SkPixmap* pixmap); | 137 SK_API bool GetWritablePixels(CdlCanvas* canvas, SkPixmap* pixmap); |
| 134 | 138 |
| 135 // Returns true if native platform routines can be used to draw on the | 139 // Returns true if native platform routines can be used to draw on the |
| 136 // given canvas. If this function returns false, | 140 // given canvas. If this function returns false, |
| 137 // ScopedPlatformPaint::GetNativeDrawingContext() should return NULL. | 141 // ScopedPlatformPaint::GetNativeDrawingContext() should return NULL. |
| 138 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas); | 142 SK_API bool SupportsPlatformPaint(const CdlCanvas* canvas); |
| 139 | 143 |
| 140 // This object guards calls to platform drawing routines. The surface | 144 // This object guards calls to platform drawing routines. The surface |
| 141 // returned from GetNativeDrawingContext() can be used with the native platform | 145 // returned from GetNativeDrawingContext() can be used with the native platform |
| 142 // routines. | 146 // routines. |
| 143 class SK_API ScopedPlatformPaint { | 147 class SK_API ScopedPlatformPaint { |
| 144 public: | 148 public: |
| 145 explicit ScopedPlatformPaint(SkCanvas* canvas); | 149 explicit ScopedPlatformPaint(CdlCanvas* canvas); |
| 146 | 150 |
| 147 // Returns the NativeDrawingContext to use for native platform drawing calls. | 151 // Returns the NativeDrawingContext to use for native platform drawing calls. |
| 148 NativeDrawingContext GetNativeDrawingContext() { | 152 NativeDrawingContext GetNativeDrawingContext() { |
| 149 return native_drawing_context_; | 153 return native_drawing_context_; |
| 150 } | 154 } |
| 151 | 155 |
| 152 private: | 156 private: |
| 153 SkCanvas* canvas_; | 157 CdlCanvas* canvas_; |
| 154 NativeDrawingContext native_drawing_context_; | 158 NativeDrawingContext native_drawing_context_; |
| 155 | 159 |
| 156 // Disallow copy and assign | 160 // Disallow copy and assign |
| 157 ScopedPlatformPaint(const ScopedPlatformPaint&); | 161 ScopedPlatformPaint(const ScopedPlatformPaint&); |
| 158 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&); | 162 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&); |
| 159 }; | 163 }; |
| 160 | 164 |
| 161 // Following routines are used in print preview workflow to mark the | 165 // Following routines are used in print preview workflow to mark the |
| 162 // preview metafile. | 166 // preview metafile. |
| 163 SK_API SkMetaData& GetMetaData(const SkCanvas& canvas); | 167 SK_API SkMetaData& GetMetaData(const CdlCanvas& canvas); |
| 164 | 168 |
| 165 #if defined(OS_MACOSX) | 169 #if defined(OS_MACOSX) |
| 166 SK_API void SetIsPreviewMetafile(const SkCanvas& canvas, bool is_preview); | 170 SK_API void SetIsPreviewMetafile(const CdlCanvas& canvas, bool is_preview); |
| 167 SK_API bool IsPreviewMetafile(const SkCanvas& canvas); | 171 SK_API bool IsPreviewMetafile(const CdlCanvas& canvas); |
| 168 | 172 |
| 169 // Returns the CGContext that backing the SkCanvas. | 173 // Returns the CGContext that backing the SkCanvas. |
| 170 // Returns NULL if none is bound. | 174 // Returns NULL if none is bound. |
| 171 SK_API CGContextRef GetBitmapContext(const SkCanvas& canvas); | 175 SK_API CGContextRef GetBitmapContext(const CdlCanvas& canvas); |
| 172 #endif | 176 #endif |
| 173 | 177 |
| 174 } // namespace skia | 178 } // namespace skia |
| 175 | 179 |
| 176 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ | 180 #endif // SKIA_EXT_PLATFORM_CANVAS_H_ |
| OLD | NEW |