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"
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
35 RETURN_NULL_ON_FAILURE
39 // The shared_section parameter is passed to gfx::PlatformDevice::create.
40 // See it for details.
41 SK_API SkCanvas
* CreatePlatformCanvas(int width
,
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
,
53 const RECT
* src_rect
);
54 #elif defined(__APPLE__)
55 SK_API SkCanvas
* CreatePlatformCanvas(CGContextRef context
,
59 OnFailureType failure_type
);
61 SK_API SkCanvas
* CreatePlatformCanvas(int width
,
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
,
76 OnFailureType failure_type
);
79 static inline SkCanvas
* CreatePlatformCanvas(int width
,
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
,
91 return CreatePlatformCanvas(width
, height
, is_opaque
, 0, CRASH_ON_FAILURE
);
94 static inline SkCanvas
* TryCreateBitmapCanvas(int width
,
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
139 class SK_API ScopedPlatformPaint
{
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_
; }
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
{
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
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
175 const SkBitmap
& GetBitmap() { return bitmap_
; }
179 PlatformSurface surface_
; // initialized to 0
180 intptr_t platform_extra_
; // platform specific, initialized to 0
182 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap
);
187 #endif // SKIA_EXT_PLATFORM_CANVAS_H_