Roll leveldb from r73 to r75.
[chromium-blink-merge.git] / skia / ext / platform_canvas.h
blob3ef2cffaea6830de1f2399b0c3ac605a478a6ea6
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);
46 #elif defined(__APPLE__)
47 SK_API SkCanvas* CreatePlatformCanvas(CGContextRef context,
48 int width,
49 int height,
50 bool is_opaque,
51 OnFailureType failure_type);
53 SK_API SkCanvas* CreatePlatformCanvas(int width,
54 int height,
55 bool is_opaque,
56 uint8_t* context,
57 OnFailureType failure_type);
58 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
59 defined(__sun) || defined(ANDROID)
60 // Linux ---------------------------------------------------------------------
62 // Construct a canvas from the given memory region. The memory is not cleared
63 // first. @data must be, at least, @height * StrideForWidth(@width) bytes.
64 SK_API SkCanvas* CreatePlatformCanvas(int width,
65 int height,
66 bool is_opaque,
67 uint8_t* data,
68 OnFailureType failure_type);
69 #endif
71 static inline SkCanvas* CreatePlatformCanvas(int width,
72 int height,
73 bool is_opaque) {
74 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE);
77 SK_API SkCanvas* CreateCanvas(const skia::RefPtr<SkDevice>& device,
78 OnFailureType failure_type);
80 static inline SkCanvas* CreateBitmapCanvas(int width,
81 int height,
82 bool is_opaque) {
83 return CreatePlatformCanvas(width, height, is_opaque, 0, CRASH_ON_FAILURE);
86 static inline SkCanvas* TryCreateBitmapCanvas(int width,
87 int height,
88 bool is_opaque) {
89 return CreatePlatformCanvas(width, height, is_opaque, 0,
90 RETURN_NULL_ON_FAILURE);
93 // Return the stride (length of a line in bytes) for the given width. Because
94 // we use 32-bits per pixel, this will be roughly 4*width. However, for
95 // alignment reasons we may wish to increase that.
96 SK_API size_t PlatformCanvasStrideForWidth(unsigned width);
98 // Returns the SkDevice pointer of the topmost rect with a non-empty
99 // clip. In practice, this is usually either the top layer or nothing, since
100 // we usually set the clip to new layers when we make them.
102 // If there is no layer that is not all clipped out, this will return a
103 // dummy device so callers do not have to check. If you are concerned about
104 // performance, check the clip before doing any painting.
106 // This is different than SkCanvas' getDevice, because that returns the
107 // bottommost device.
109 // Danger: the resulting device should not be saved. It will be invalidated
110 // by the next call to save() or restore().
111 SK_API SkDevice* GetTopDevice(const SkCanvas& canvas);
113 // Returns true if native platform routines can be used to draw on the
114 // given canvas. If this function returns false, BeginPlatformPaint will
115 // return NULL PlatformSurface.
116 SK_API bool SupportsPlatformPaint(const SkCanvas* canvas);
118 // Draws into the a native platform surface, |context|. Forwards to
119 // DrawToNativeContext on a PlatformDevice instance bound to the top device.
120 // If no PlatformDevice instance is bound, is a no-operation.
121 SK_API void DrawToNativeContext(SkCanvas* canvas,
122 PlatformSurface context,
123 int x,
124 int y,
125 const PlatformRect* src_rect);
127 // Sets the opacity of each pixel in the specified region to be opaque.
128 SK_API void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height);
130 // These calls should surround calls to platform drawing routines, the
131 // surface returned here can be used with the native platform routines.
133 // Call EndPlatformPaint when you are done and want to use skia operations
134 // after calling the platform-specific BeginPlatformPaint; this will
135 // synchronize the bitmap to OS if necessary.
136 SK_API PlatformSurface BeginPlatformPaint(SkCanvas* canvas);
137 SK_API void EndPlatformPaint(SkCanvas* canvas);
139 // Helper class for pairing calls to BeginPlatformPaint and EndPlatformPaint.
140 // Upon construction invokes BeginPlatformPaint, and upon destruction invokes
141 // EndPlatformPaint.
142 class SK_API ScopedPlatformPaint {
143 public:
144 explicit ScopedPlatformPaint(SkCanvas* canvas) : canvas_(canvas) {
145 platform_surface_ = BeginPlatformPaint(canvas);
147 ~ScopedPlatformPaint() { EndPlatformPaint(canvas_); }
149 // Returns the PlatformSurface to use for native platform drawing calls.
150 PlatformSurface GetPlatformSurface() { return platform_surface_; }
151 private:
152 SkCanvas* canvas_;
153 PlatformSurface platform_surface_;
155 // Disallow copy and assign
156 ScopedPlatformPaint(const ScopedPlatformPaint&);
157 ScopedPlatformPaint& operator=(const ScopedPlatformPaint&);
160 // PlatformBitmap holds a PlatformSurface that can also be used as an SkBitmap.
161 class SK_API PlatformBitmap {
162 public:
163 PlatformBitmap();
164 ~PlatformBitmap();
166 // Returns true if the bitmap was able to allocate its surface.
167 bool Allocate(int width, int height, bool is_opaque);
169 // Returns the platform surface, or 0 if Allocate() did not return true.
170 PlatformSurface GetSurface() { return surface_; }
172 // Return the skia bitmap, which will be empty if Allocate() did not
173 // return true.
175 // The resulting SkBitmap holds a refcount on the underlying platform surface,
176 // so the surface will remain allocated so long as the SkBitmap or its copies
177 // stay around.
178 const SkBitmap& GetBitmap() { return bitmap_; }
180 private:
181 SkBitmap bitmap_;
182 PlatformSurface surface_; // initialized to 0
183 intptr_t platform_extra_; // platform specific, initialized to 0
185 DISALLOW_COPY_AND_ASSIGN(PlatformBitmap);
188 } // namespace skia
190 #endif // SKIA_EXT_PLATFORM_CANVAS_H_