[email protected] | fa8cfb0 | 2012-01-13 00:27:41 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_CANVAS_H_ |
| 6 | #define UI_GFX_CANVAS_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/string16.h" |
[email protected] | 91ef7b0 | 2011-08-02 15:26:03 | [diff] [blame] | 12 | // TODO(beng): remove these includes when we no longer depend on SkTypes. |
| 13 | #include "third_party/skia/include/core/SkColor.h" |
| 14 | #include "third_party/skia/include/core/SkXfermode.h" |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 15 | #include "ui/base/ui_export.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 17 | |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 18 | class SkCanvas; |
| 19 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 20 | namespace ui { |
| 21 | class Transform; |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 22 | typedef unsigned int TextureID; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 25 | namespace gfx { |
| 26 | |
| 27 | class Brush; |
| 28 | class CanvasSkia; |
| 29 | class Font; |
| 30 | class Point; |
| 31 | class Rect; |
[email protected] | 7e82ed01 | 2011-11-01 01:40:07 | [diff] [blame] | 32 | class Size; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 33 | |
| 34 | // TODO(beng): documentation. |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 35 | class UI_EXPORT Canvas { |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 36 | public: |
| 37 | // Specifies the alignment for text rendered with the DrawStringInt method. |
| 38 | enum { |
| 39 | TEXT_ALIGN_LEFT = 1, |
| 40 | TEXT_ALIGN_CENTER = 2, |
| 41 | TEXT_ALIGN_RIGHT = 4, |
| 42 | TEXT_VALIGN_TOP = 8, |
| 43 | TEXT_VALIGN_MIDDLE = 16, |
| 44 | TEXT_VALIGN_BOTTOM = 32, |
| 45 | |
| 46 | // Specifies the text consists of multiple lines. |
| 47 | MULTI_LINE = 64, |
| 48 | |
| 49 | // By default DrawStringInt does not process the prefix ('&') character |
| 50 | // specially. That is, the string "&foo" is rendered as "&foo". When |
| 51 | // rendering text from a resource that uses the prefix character for |
| 52 | // mnemonics, the prefix should be processed and can be rendered as an |
| 53 | // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). |
| 54 | SHOW_PREFIX = 128, |
| 55 | HIDE_PREFIX = 256, |
| 56 | |
| 57 | // Prevent ellipsizing |
| 58 | NO_ELLIPSIS = 512, |
| 59 | |
| 60 | // Specifies if words can be split by new lines. |
| 61 | // This only works with MULTI_LINE. |
| 62 | CHARACTER_BREAK = 1024, |
| 63 | |
| 64 | // Instructs DrawStringInt() to render the text using RTL directionality. |
| 65 | // In most cases, passing this flag is not necessary because information |
| 66 | // about the text directionality is going to be embedded within the string |
| 67 | // in the form of special Unicode characters. However, we don't insert |
| 68 | // directionality characters into strings if the locale is LTR because some |
| 69 | // platforms (for example, an English Windows XP with no RTL fonts |
| 70 | // installed) don't support these characters. Thus, this flag should be |
| 71 | // used to render text using RTL directionality when the locale is LTR. |
| 72 | FORCE_RTL_DIRECTIONALITY = 2048, |
[email protected] | 3f49b81 | 2011-08-18 23:32:34 | [diff] [blame] | 73 | |
| 74 | // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right. |
| 75 | // See FORCE_RTL_DIRECTIONALITY for details. |
| 76 | FORCE_LTR_DIRECTIONALITY = 4096, |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | virtual ~Canvas() {} |
| 80 | |
| 81 | // Creates an empty canvas. Must be initialized before it can be used. |
| 82 | static Canvas* CreateCanvas(); |
| 83 | |
| 84 | // Creates a canvas with the specified size. |
[email protected] | 7e82ed01 | 2011-11-01 01:40:07 | [diff] [blame] | 85 | static Canvas* CreateCanvas(const gfx::Size& size, bool is_opaque); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 86 | |
| 87 | // Saves a copy of the drawing state onto a stack, operating on this copy |
| 88 | // until a balanced call to Restore() is made. |
| 89 | virtual void Save() = 0; |
| 90 | |
| 91 | // As with Save(), except draws to a layer that is blended with the canvas |
| 92 | // at the specified alpha once Restore() is called. |
| 93 | // |layer_bounds| are the bounds of the layer relative to the current |
| 94 | // transform. |
| 95 | virtual void SaveLayerAlpha(uint8 alpha) = 0; |
| 96 | virtual void SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) = 0; |
| 97 | |
| 98 | // Restores the drawing state after a call to Save*(). It is an error to |
| 99 | // call Restore() more times than Save*(). |
| 100 | virtual void Restore() = 0; |
| 101 | |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 102 | // Returns true if the clip is non-empty. |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 103 | virtual bool ClipRect(const gfx::Rect& rect) = 0; |
| 104 | |
| 105 | virtual void Translate(const gfx::Point& point) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 106 | |
[email protected] | 8027711 | 2011-10-27 12:29:21 | [diff] [blame] | 107 | virtual void Scale(int x_scale, int y_scale) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 108 | |
| 109 | // Fills the specified region with the specified color using a transfer |
| 110 | // mode of SkXfermode::kSrcOver_Mode. |
[email protected] | e728d1d | 2011-10-31 00:11:05 | [diff] [blame] | 111 | virtual void FillRect(const SkColor& color, const gfx::Rect& rect) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 112 | |
[email protected] | e728d1d | 2011-10-31 00:11:05 | [diff] [blame] | 113 | // Fills the specified region with the specified color and mode. |
| 114 | virtual void FillRect(const SkColor& color, |
| 115 | const gfx::Rect& rect, |
| 116 | SkXfermode::Mode mode) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 117 | |
| 118 | // Fills the specified region with the specified brush. |
[email protected] | e728d1d | 2011-10-31 00:11:05 | [diff] [blame] | 119 | virtual void FillRect(const gfx::Brush* brush, const gfx::Rect& rect) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 120 | |
| 121 | // Draws a single pixel rect in the specified region with the specified |
| 122 | // color, using a transfer mode of SkXfermode::kSrcOver_Mode. |
| 123 | // |
| 124 | // NOTE: if you need a single pixel line, use DrawLineInt. |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 125 | virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 126 | |
| 127 | // Draws a single pixel rect in the specified region with the specified |
| 128 | // color and transfer mode. |
| 129 | // |
| 130 | // NOTE: if you need a single pixel line, use DrawLineInt. |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 131 | virtual void DrawRect(const gfx::Rect& rect, |
| 132 | const SkColor& color, |
| 133 | SkXfermode::Mode mode) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 134 | |
| 135 | // Draws the given rectangle with the given paint's parameters. |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 136 | virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 137 | |
| 138 | // Draws a single pixel line with the specified color. |
| 139 | virtual void DrawLineInt(const SkColor& color, |
| 140 | int x1, int y1, |
| 141 | int x2, int y2) = 0; |
| 142 | |
| 143 | // Draws a bitmap with the origin at the specified location. The upper left |
| 144 | // corner of the bitmap is rendered at the specified location. |
| 145 | virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) = 0; |
| 146 | |
| 147 | // Draws a bitmap with the origin at the specified location, using the |
| 148 | // specified paint. The upper left corner of the bitmap is rendered at the |
| 149 | // specified location. |
| 150 | virtual void DrawBitmapInt(const SkBitmap& bitmap, |
| 151 | int x, int y, |
| 152 | const SkPaint& paint) = 0; |
| 153 | |
| 154 | // Draws a portion of a bitmap in the specified location. The src parameters |
| 155 | // correspond to the region of the bitmap to draw in the region defined |
| 156 | // by the dest coordinates. |
| 157 | // |
| 158 | // If the width or height of the source differs from that of the destination, |
| 159 | // the bitmap will be scaled. When scaling down, it is highly recommended |
| 160 | // that you call buildMipMap(false) on your bitmap to ensure that it has |
| 161 | // a mipmap, which will result in much higher-quality output. Set |filter| |
| 162 | // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm |
| 163 | // is used for resampling. |
| 164 | // |
| 165 | // An optional custom SkPaint can be provided. |
| 166 | virtual void DrawBitmapInt(const SkBitmap& bitmap, |
| 167 | int src_x, int src_y, int src_w, int src_h, |
| 168 | int dest_x, int dest_y, int dest_w, int dest_h, |
| 169 | bool filter) = 0; |
| 170 | virtual void DrawBitmapInt(const SkBitmap& bitmap, |
| 171 | int src_x, int src_y, int src_w, int src_h, |
| 172 | int dest_x, int dest_y, int dest_w, int dest_h, |
| 173 | bool filter, |
| 174 | const SkPaint& paint) = 0; |
| 175 | |
| 176 | // Draws text with the specified color, font and location. The text is |
| 177 | // aligned to the left, vertically centered, clipped to the region. If the |
| 178 | // text is too big, it is truncated and '...' is added to the end. |
[email protected] | 4e8655d | 2011-06-16 17:20:17 | [diff] [blame] | 179 | virtual void DrawStringInt(const string16& text, |
| 180 | const gfx::Font& font, |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 181 | const SkColor& color, |
| 182 | int x, int y, int w, int h) = 0; |
| 183 | virtual void DrawStringInt(const string16& text, |
| 184 | const gfx::Font& font, |
| 185 | const SkColor& color, |
| 186 | const gfx::Rect& display_rect) = 0; |
| 187 | |
| 188 | // Draws text with the specified color, font and location. The last argument |
| 189 | // specifies flags for how the text should be rendered. It can be one of |
| 190 | // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. |
| 191 | virtual void DrawStringInt(const string16& text, |
| 192 | const gfx::Font& font, |
| 193 | const SkColor& color, |
| 194 | int x, int y, int w, int h, |
| 195 | int flags) = 0; |
| 196 | |
| 197 | // Draws a dotted gray rectangle used for focus purposes. |
[email protected] | 27488c2 | 2011-10-25 02:45:21 | [diff] [blame] | 198 | virtual void DrawFocusRect(const gfx::Rect& rect) = 0; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 199 | |
| 200 | // Tiles the image in the specified region. |
| 201 | virtual void TileImageInt(const SkBitmap& bitmap, |
| 202 | int x, int y, int w, int h) = 0; |
| 203 | virtual void TileImageInt(const SkBitmap& bitmap, |
| 204 | int src_x, int src_y, |
| 205 | int dest_x, int dest_y, int w, int h) = 0; |
| 206 | |
| 207 | // Returns a native drawing context for platform specific drawing routines to |
| 208 | // use. Must be balanced by a call to EndPlatformPaint(). |
| 209 | virtual gfx::NativeDrawingContext BeginPlatformPaint() = 0; |
| 210 | |
| 211 | // Signifies the end of platform drawing using the native drawing context |
| 212 | // returned by BeginPlatformPaint(). |
| 213 | virtual void EndPlatformPaint() = 0; |
| 214 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 215 | // Apply transformation on the canvas. |
| 216 | virtual void Transform(const ui::Transform& transform) = 0; |
| 217 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 218 | // Create a texture ID that can be used for accelerated drawing. |
| 219 | virtual ui::TextureID GetTextureID() = 0; |
| 220 | |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 221 | // TODO(beng): remove this once we don't need to use any skia-specific methods |
| 222 | // through this interface. |
| 223 | // A quick and dirty way to obtain the underlying SkCanvas. |
| 224 | virtual CanvasSkia* AsCanvasSkia(); |
| 225 | virtual const CanvasSkia* AsCanvasSkia() const; |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 226 | virtual SkCanvas* GetSkCanvas(); |
| 227 | virtual const SkCanvas* GetSkCanvas() const; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 228 | }; |
| 229 | |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 230 | class UI_EXPORT CanvasPaint { |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 231 | public: |
| 232 | virtual ~CanvasPaint() {} |
| 233 | |
| 234 | // Creates a canvas that paints to |view| when it is destroyed. The canvas is |
| 235 | // sized to the client area of |view|. |
| 236 | static CanvasPaint* CreateCanvasPaint(gfx::NativeView view); |
| 237 | |
| 238 | // Returns true if the canvas has an invalid rect that needs to be repainted. |
| 239 | virtual bool IsValid() const = 0; |
| 240 | |
| 241 | // Returns the rectangle that is invalid. |
| 242 | virtual gfx::Rect GetInvalidRect() const = 0; |
| 243 | |
| 244 | // Returns the underlying Canvas. |
| 245 | virtual Canvas* AsCanvas() = 0; |
| 246 | }; |
| 247 | |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 248 | } // namespace gfx |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 249 | |
| 250 | #endif // UI_GFX_CANVAS_H_ |