Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / skia / ext / platform_canvas.h
blob75458b5dd5401c63e4341395cd9e0bdf47ae4591
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.
5 #ifndef SKIA_EXT_PLATFORM_CANVAS_H_
6 #define SKIA_EXT_PLATFORM_CANVAS_H_
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"
17 namespace skia {
19 typedef SkCanvas PlatformCanvas;
22 // Note about error handling.
24 // Creating a canvas can fail at times, most often because we fail to allocate
25 // the backing-store (pixels). This can be from out-of-memory, or something
26 // more opaque, like GDI or cairo reported a failure.
28 // To allow the caller to handle the failure, every Create... factory takes an
29 // enum as its last parameter. The default value is kCrashOnFailure. If the
30 // caller passes kReturnNullOnFailure, then the caller is responsible to check
31 // the return result.
33 enum OnFailureType {
34 CRASH_ON_FAILURE,
35 RETURN_NULL_ON_FAILURE
38 #if defined(WIN32)
39 // The shared_section parameter is passed to gfx::PlatformDevice::create.
40 // See it for details.
41 SK_API SkCanvas* CreatePlatformCanvas(int width,
42 int height,
43 bool is_opaque,
44 HANDLE shared_section,
45 OnFailureType failure_type);
47 // Draws the top layer of the canvas into the specified HDC. Only works
48 // with a PlatformCanvas with a BitmapPlatformDevice.
49 SK_API void DrawToNativeContext(SkCanvas* canvas,
50 HDC hdc,
51 int x,
52 int y,
53 const RECT* src_rect);
54 #elif defined(__APPLE__)
55 SK_API SkCanvas* CreatePlatformCanvas(CGContextRef context,
56 int width,
57 int height,
58 bool is_opaque,
59 OnFailureType failure_type);
61 SK_API SkCanvas* CreatePlatformCanvas(int width,
62 int height,
63 bool is_opaque,
64 uint8_t* context,
65 OnFailureType failure_type);
66 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
67 defined(__sun) || defined(ANDROID)
68 // Linux ---------------------------------------------------------------------
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
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);
85 SK_API SkCanvas* CreateCanvas(const skia::RefPtr<SkBaseDevice>& device,
86 OnFailureType failure_type);
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);
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);
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);
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.
110 // This may return NULL, so callers need to check.
112 // This is different than SkCanvas' getDevice, because that returns the
113 // bottommost device.
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);
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);
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);
127 // These calls should surround calls to platform drawing routines, the
128 // surface returned here can be used with the native platform routines.
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);
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);
144 ~ScopedPlatformPaint() { EndPlatformPaint(canvas_); }
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_;
152 // Disallow copy and assign
153 ScopedPlatformPaint(const ScopedPlatformPaint&);
154 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&);
157 // PlatformBitmap holds a PlatformSurface that can also be used as an SkBitmap.
158 class SK_API PlatformBitmap {
159 public:
160 PlatformBitmap();
161 ~PlatformBitmap();
163 // Returns true if the bitmap was able to allocate its surface.
164 bool Allocate(int width, int height, bool is_opaque);
166 // Returns the platform surface, or 0 if Allocate() did not return true.
167 PlatformSurface GetSurface() { return surface_; }
169 // Return the skia bitmap, which will be empty if Allocate() did not
170 // return true.
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_; }
177 private:
178 SkBitmap bitmap_;
179 PlatformSurface surface_; // initialized to 0
180 intptr_t platform_extra_; // platform specific, initialized to 0
182 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap);
185 } // namespace skia
187 #endif // SKIA_EXT_PLATFORM_CANVAS_H_