| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "gfx/canvas.h" | 5 #include "gfx/canvas.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "gfx/canvas.h" |
| 11 #include "gfx/font.h" | 12 #include "gfx/font.h" |
| 12 #include "gfx/rect.h" | 13 #include "gfx/rect.h" |
| 13 #include "third_party/skia/include/core/SkShader.h" | 14 #include "third_party/skia/include/core/SkShader.h" |
| 14 | 15 |
| 15 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 16 #include "gfx/canvas_paint.h" | 17 #include "gfx/canvas_paint.h" |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 namespace gfx { | 20 namespace gfx { |
| 20 | 21 |
| 21 bool Canvas::GetClipRect(gfx::Rect* r) { | 22 Canvas* CanvasSkia::AsCanvas() { |
| 23 return this; |
| 24 } |
| 25 |
| 26 bool CanvasSkia::GetClipRect(gfx::Rect* r) { |
| 22 SkRect clip; | 27 SkRect clip; |
| 23 if (!getClipBounds(&clip)) { | 28 if (!getClipBounds(&clip)) { |
| 24 if (r) | 29 if (r) |
| 25 r->SetRect(0, 0, 0, 0); | 30 r->SetRect(0, 0, 0, 0); |
| 26 return false; | 31 return false; |
| 27 } | 32 } |
| 28 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), | 33 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), |
| 29 SkScalarRound(clip.fRight - clip.fLeft), | 34 SkScalarRound(clip.fRight - clip.fLeft), |
| 30 SkScalarRound(clip.fBottom - clip.fTop)); | 35 SkScalarRound(clip.fBottom - clip.fTop)); |
| 31 return true; | 36 return true; |
| 32 } | 37 } |
| 33 | 38 |
| 34 bool Canvas::ClipRectInt(int x, int y, int w, int h) { | 39 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) { |
| 35 SkRect new_clip; | 40 SkRect new_clip; |
| 36 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), | 41 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), |
| 37 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 42 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
| 38 return clipRect(new_clip); | 43 return clipRect(new_clip); |
| 39 } | 44 } |
| 40 | 45 |
| 41 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { | 46 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) { |
| 42 SkRect clip; | 47 SkRect clip; |
| 43 return getClipBounds(&clip) && | 48 return getClipBounds(&clip) && |
| 44 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 49 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
| 45 SkIntToScalar(y + h)); | 50 SkIntToScalar(y + h)); |
| 46 } | 51 } |
| 47 | 52 |
| 48 void Canvas::TranslateInt(int x, int y) { | 53 void CanvasSkia::TranslateInt(int x, int y) { |
| 49 translate(SkIntToScalar(x), SkIntToScalar(y)); | 54 translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 50 } | 55 } |
| 51 | 56 |
| 52 void Canvas::ScaleInt(int x, int y) { | 57 void CanvasSkia::ScaleInt(int x, int y) { |
| 53 scale(SkIntToScalar(x), SkIntToScalar(y)); | 58 scale(SkIntToScalar(x), SkIntToScalar(y)); |
| 54 } | 59 } |
| 55 | 60 |
| 56 void Canvas::FillRectInt(const SkColor& color, int x, int y, int w, int h) { | 61 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) { |
| 57 SkPaint paint; | 62 SkPaint paint; |
| 58 paint.setColor(color); | 63 paint.setColor(color); |
| 59 paint.setStyle(SkPaint::kFill_Style); | 64 paint.setStyle(SkPaint::kFill_Style); |
| 60 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | 65 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 61 FillRectInt(x, y, w, h, paint); | 66 FillRectInt(x, y, w, h, paint); |
| 62 } | 67 } |
| 63 | 68 |
| 64 void Canvas::FillRectInt(int x, int y, int w, int h, const SkPaint& paint) { | 69 void CanvasSkia::FillRectInt(int x, int y, int w, int h, const SkPaint& paint) { |
| 65 SkIRect rc = {x, y, x + w, y + h}; | 70 SkIRect rc = {x, y, x + w, y + h}; |
| 66 drawIRect(rc, paint); | 71 drawIRect(rc, paint); |
| 67 } | 72 } |
| 68 | 73 |
| 69 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h) { | 74 void CanvasSkia::DrawRectInt(const SkColor& color, int x, int y, int w, int h) { |
| 70 DrawRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); | 75 DrawRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); |
| 71 } | 76 } |
| 72 | 77 |
| 73 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h, | 78 void CanvasSkia::DrawRectInt(const SkColor& color, int x, int y, int w, int h, |
| 74 SkXfermode::Mode mode) { | 79 SkXfermode::Mode mode) { |
| 75 SkPaint paint; | 80 SkPaint paint; |
| 76 paint.setColor(color); | 81 paint.setColor(color); |
| 77 paint.setStyle(SkPaint::kStroke_Style); | 82 paint.setStyle(SkPaint::kStroke_Style); |
| 78 // Set a stroke width of 0, which will put us down the stroke rect path. If | 83 // Set a stroke width of 0, which will put us down the stroke rect path. If |
| 79 // we set a stroke width of 1, for example, this will internally create a | 84 // we set a stroke width of 1, for example, this will internally create a |
| 80 // path and fill it, which causes problems near the edge of the canvas. | 85 // path and fill it, which causes problems near the edge of the canvas. |
| 81 paint.setStrokeWidth(SkIntToScalar(0)); | 86 paint.setStrokeWidth(SkIntToScalar(0)); |
| 82 paint.setXfermodeMode(mode); | 87 paint.setXfermodeMode(mode); |
| 83 | 88 |
| 84 SkIRect rc = {x, y, x + w, y + h}; | 89 SkIRect rc = {x, y, x + w, y + h}; |
| 85 drawIRect(rc, paint); | 90 drawIRect(rc, paint); |
| 86 } | 91 } |
| 87 | 92 |
| 88 void Canvas::DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2) { | 93 void CanvasSkia::DrawLineInt(const SkColor& color, int x1, int y1, int x2, |
| 94 int y2) { |
| 89 SkPaint paint; | 95 SkPaint paint; |
| 90 paint.setColor(color); | 96 paint.setColor(color); |
| 91 paint.setStrokeWidth(SkIntToScalar(1)); | 97 paint.setStrokeWidth(SkIntToScalar(1)); |
| 92 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), | 98 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), |
| 93 SkIntToScalar(y2), paint); | 99 SkIntToScalar(y2), paint); |
| 94 } | 100 } |
| 95 | 101 |
| 96 void Canvas::DrawFocusRect(int x, int y, int width, int height) { | 102 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) { |
| 97 // Create a 2D bitmap containing alternating on/off pixels - we do this | 103 // Create a 2D bitmap containing alternating on/off pixels - we do this |
| 98 // so that you never get two pixels of the same color around the edges | 104 // so that you never get two pixels of the same color around the edges |
| 99 // of the focus rect (this may mean that opposing edges of the rect may | 105 // of the focus rect (this may mean that opposing edges of the rect may |
| 100 // have a dot pattern out of phase to each other). | 106 // have a dot pattern out of phase to each other). |
| 101 static SkBitmap* dots = NULL; | 107 static SkBitmap* dots = NULL; |
| 102 if (!dots) { | 108 if (!dots) { |
| 103 int col_pixels = 32; | 109 int col_pixels = 32; |
| 104 int row_pixels = 32; | 110 int row_pixels = 32; |
| 105 | 111 |
| 106 dots = new SkBitmap; | 112 dots = new SkBitmap; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 drawRect(rect, paint); | 146 drawRect(rect, paint); |
| 141 | 147 |
| 142 rect.set(SkIntToScalar(x), SkIntToScalar(y), | 148 rect.set(SkIntToScalar(x), SkIntToScalar(y), |
| 143 SkIntToScalar(x + 1), SkIntToScalar(y + height)); | 149 SkIntToScalar(x + 1), SkIntToScalar(y + height)); |
| 144 drawRect(rect, paint); | 150 drawRect(rect, paint); |
| 145 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), | 151 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), |
| 146 SkIntToScalar(x + width), SkIntToScalar(y + height)); | 152 SkIntToScalar(x + width), SkIntToScalar(y + height)); |
| 147 drawRect(rect, paint); | 153 drawRect(rect, paint); |
| 148 } | 154 } |
| 149 | 155 |
| 150 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { | 156 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { |
| 151 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); | 157 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); |
| 152 } | 158 } |
| 153 | 159 |
| 154 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, | 160 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, |
| 155 const SkPaint& paint) { | 161 const SkPaint& paint) { |
| 156 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); | 162 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 157 } | 163 } |
| 158 | 164 |
| 159 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | 165 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 160 int src_w, int src_h, int dest_x, int dest_y, | 166 int src_w, int src_h, int dest_x, int dest_y, |
| 161 int dest_w, int dest_h, | 167 int dest_w, int dest_h, |
| 162 bool filter) { | 168 bool filter) { |
| 163 SkPaint p; | 169 SkPaint p; |
| 164 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, | 170 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 165 dest_w, dest_h, filter, p); | 171 dest_w, dest_h, filter, p); |
| 166 } | 172 } |
| 167 | 173 |
| 168 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | 174 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 169 int src_w, int src_h, int dest_x, int dest_y, | 175 int src_w, int src_h, int dest_x, int dest_y, |
| 170 int dest_w, int dest_h, | 176 int dest_w, int dest_h, |
| 171 bool filter, const SkPaint& paint) { | 177 bool filter, const SkPaint& paint) { |
| 172 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 178 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 173 src_y + src_h < std::numeric_limits<int16_t>::max()); | 179 src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 174 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { | 180 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { |
| 175 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; | 181 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; |
| 176 return; | 182 return; |
| 177 } | 183 } |
| 178 | 184 |
| 179 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 185 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
| 180 return; | 186 return; |
| 181 | 187 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 210 // by the paint). | 216 // by the paint). |
| 211 SkPaint p(paint); | 217 SkPaint p(paint); |
| 212 p.setFilterBitmap(filter); | 218 p.setFilterBitmap(filter); |
| 213 p.setShader(shader); | 219 p.setShader(shader); |
| 214 shader->unref(); | 220 shader->unref(); |
| 215 | 221 |
| 216 // The rect will be filled by the bitmap. | 222 // The rect will be filled by the bitmap. |
| 217 drawRect(dest_rect, p); | 223 drawRect(dest_rect, p); |
| 218 } | 224 } |
| 219 | 225 |
| 220 void Canvas::DrawStringInt(const std::wstring& text, | 226 void CanvasSkia::DrawStringInt(const std::wstring& text, |
| 221 const gfx::Font& font, | 227 const gfx::Font& font, |
| 222 const SkColor& color, | 228 const SkColor& color, |
| 223 int x, int y, int w, int h) { | 229 int x, int y, int w, int h) { |
| 224 DrawStringInt(text, font, color, x, y, w, h, | 230 DrawStringInt(text, font, color, x, y, w, h, |
| 225 gfx::Canvas::DefaultCanvasTextAlignment()); | 231 gfx::CanvasSkia::DefaultCanvasTextAlignment()); |
| 226 } | 232 } |
| 227 | 233 |
| 228 void Canvas::DrawStringInt(const std::wstring& text, | 234 void CanvasSkia::DrawStringInt(const std::wstring& text, |
| 229 const gfx::Font& font, | 235 const gfx::Font& font, |
| 230 const SkColor& color, | 236 const SkColor& color, |
| 231 const gfx::Rect& display_rect) { | 237 const gfx::Rect& display_rect) { |
| 232 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 238 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
| 233 display_rect.width(), display_rect.height()); | 239 display_rect.width(), display_rect.height()); |
| 234 } | 240 } |
| 235 | 241 |
| 236 void Canvas::TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h) { | 242 void CanvasSkia::TileImageInt(const SkBitmap& bitmap, int x, int y, int w, |
| 243 int h) { |
| 237 TileImageInt(bitmap, 0, 0, x, y, w, h); | 244 TileImageInt(bitmap, 0, 0, x, y, w, h); |
| 238 } | 245 } |
| 239 | 246 |
| 240 void Canvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, | 247 void CanvasSkia::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 241 int dest_x, int dest_y, int w, int h) { | 248 int dest_x, int dest_y, int w, int h) { |
| 242 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 249 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
| 243 return; | 250 return; |
| 244 | 251 |
| 245 SkPaint paint; | 252 SkPaint paint; |
| 246 | 253 |
| 247 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | 254 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
| 248 SkShader::kRepeat_TileMode, | 255 SkShader::kRepeat_TileMode, |
| 249 SkShader::kRepeat_TileMode); | 256 SkShader::kRepeat_TileMode); |
| 250 paint.setShader(shader); | 257 paint.setShader(shader); |
| 251 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | 258 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 252 | 259 |
| 253 // CreateBitmapShader returns a Shader with a reference count of one, we | 260 // CreateBitmapShader returns a Shader with a reference count of one, we |
| 254 // need to unref after paint takes ownership of the shader. | 261 // need to unref after paint takes ownership of the shader. |
| 255 shader->unref(); | 262 shader->unref(); |
| 256 save(); | 263 save(); |
| 257 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); | 264 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); |
| 258 ClipRectInt(src_x, src_y, w, h); | 265 ClipRectInt(src_x, src_y, w, h); |
| 259 drawPaint(paint); | 266 drawPaint(paint); |
| 260 restore(); | 267 restore(); |
| 261 } | 268 } |
| 262 | 269 |
| 263 SkBitmap Canvas::ExtractBitmap() const { | 270 SkBitmap CanvasSkia::ExtractBitmap() const { |
| 264 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); | 271 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); |
| 265 | 272 |
| 266 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 273 // Make a bitmap to return, and a canvas to draw into it. We don't just want |
| 267 // to call extractSubset or the copy constuctor, since we want an actual copy | 274 // to call extractSubset or the copy constuctor, since we want an actual copy |
| 268 // of the bitmap. | 275 // of the bitmap. |
| 269 SkBitmap result; | 276 SkBitmap result; |
| 270 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 277 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
| 271 return result; | 278 return result; |
| 272 } | 279 } |
| 273 | 280 |
| 274 Canvas* Canvas::AsCanvas() { | 281 CanvasSkia* CanvasSkia::AsCanvasSkia() { |
| 275 return this; | 282 return this; |
| 276 } | 283 } |
| 277 | 284 |
| 278 // static | 285 // static |
| 279 int Canvas::DefaultCanvasTextAlignment() { | 286 int CanvasSkia::DefaultCanvasTextAlignment() { |
| 280 if (!base::i18n::IsRTL()) | 287 if (!base::i18n::IsRTL()) |
| 281 return gfx::Canvas::TEXT_ALIGN_LEFT; | 288 return gfx::Canvas::TEXT_ALIGN_LEFT; |
| 282 return gfx::Canvas::TEXT_ALIGN_RIGHT; | 289 return gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 283 } | 290 } |
| 284 | 291 |
| 285 //////////////////////////////////////////////////////////////////////////////// | 292 //////////////////////////////////////////////////////////////////////////////// |
| 286 // Canvas2, public: | 293 // Canvas2, public: |
| 287 | 294 |
| 288 Canvas2* Canvas2::CreateCanvas() { | 295 Canvas2* Canvas2::CreateCanvas() { |
| 289 return new Canvas; | 296 return new Canvas; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 316 | 323 |
| 317 CanvasPaint2* CanvasPaint2::CreateCanvasPaint(gfx::NativeView view) { | 324 CanvasPaint2* CanvasPaint2::CreateCanvasPaint(gfx::NativeView view) { |
| 318 #if defined(OS_WIN) | 325 #if defined(OS_WIN) |
| 319 return new CanvasPaintWin(view); | 326 return new CanvasPaintWin(view); |
| 320 #else | 327 #else |
| 321 return NULL; | 328 return NULL; |
| 322 #endif | 329 #endif |
| 323 } | 330 } |
| 324 | 331 |
| 325 } // namespace gfx | 332 } // namespace gfx |
| OLD | NEW |