Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/canvas.h" | 5 #include "ui/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 "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "third_party/skia/include/effects/SkGradientShader.h" | 12 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font_list.h" |
| 15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/size_conversions.h" | 16 #include "ui/gfx/size_conversions.h" |
| 17 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
| 18 #include "ui/gfx/transform.h" | 18 #include "ui/gfx/transform.h" |
| 19 | 19 |
| 20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 21 #include "ui/gfx/canvas_skia_paint.h" | 21 #include "ui/gfx/canvas_skia_paint.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace gfx { | 24 namespace gfx { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); | 80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); |
| 81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), | 81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), |
| 82 pixel_size.height(), | 82 pixel_size.height(), |
| 83 is_opaque)); | 83 is_opaque)); |
| 84 canvas_ = owned_canvas_.get(); | 84 canvas_ = owned_canvas_.get(); |
| 85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); | 85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); |
| 86 canvas_->scale(scale, scale); | 86 canvas_->scale(scale, scale); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // static | 89 // static |
| 90 void Canvas::SizeStringInt(const base::string16& text, | |
| 91 const gfx::Font& font, | |
| 92 int* width, | |
| 93 int* height, | |
| 94 int line_height, | |
| 95 int flags) { | |
| 96 #if defined(OS_ANDROID) | |
|
msw
2013/08/16 16:59:25
I believe avoiding these platform-specific preproc
Yuki
2013/08/16 17:24:31
Note that these defined(OS_ANDROID) appear only in
msw
2013/08/16 17:31:39
If the obsolete redirection methods can be consoli
Yuki
2013/08/16 18:29:28
Okay, I'll leave the redirection code here in canv
| |
| 97 NOTIMPLEMENTED(); | |
| 98 #else | |
| 99 SizeStringInt(text, gfx::FontList(font), width, height, line_height, flags); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 // static | |
| 104 int Canvas::GetStringWidth(const base::string16& text, | |
| 105 const gfx::FontList& font_list) { | |
| 106 #if defined(OS_ANDROID) | |
| 107 NOTIMPLEMENTED(); | |
| 108 return 0; | |
| 109 #else | |
| 110 int width = 0, height = 0; | |
| 111 SizeStringInt(text, font_list, &width, &height, 0, NO_ELLIPSIS); | |
| 112 return width; | |
| 113 #endif | |
| 114 } | |
| 115 | |
| 116 // static | |
| 90 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { | 117 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { |
| 118 #if defined(OS_ANDROID) | |
| 119 NOTIMPLEMENTED(); | |
| 120 return 0; | |
| 121 #else | |
| 91 int width = 0, height = 0; | 122 int width = 0, height = 0; |
| 92 Canvas::SizeStringInt(text, font, &width, &height, 0, NO_ELLIPSIS); | 123 SizeStringInt(text, gfx::FontList(font), &width, &height, 0, NO_ELLIPSIS); |
| 93 return width; | 124 return width; |
| 125 #endif | |
| 94 } | 126 } |
| 95 | 127 |
| 96 // static | 128 // static |
| 97 int Canvas::DefaultCanvasTextAlignment() { | 129 int Canvas::DefaultCanvasTextAlignment() { |
| 98 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; | 130 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
| 99 } | 131 } |
| 100 | 132 |
| 133 void Canvas::DrawStringWithHalo(const base::string16& text, | |
| 134 const gfx::Font& font, | |
| 135 SkColor text_color, | |
| 136 SkColor halo_color_in, | |
| 137 int x, | |
| 138 int y, | |
| 139 int w, | |
| 140 int h, | |
| 141 int flags) { | |
| 142 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 143 NOTIMPLEMENTED(); | |
| 144 #else | |
| 145 DrawStringWithHaloRect(text, gfx::FontList(font), text_color, halo_color_in, | |
| 146 gfx::Rect(x, y, w, h), flags); | |
| 147 #endif | |
| 148 } | |
| 149 | |
| 101 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { | 150 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { |
| 102 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); | 151 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); |
| 103 | 152 |
| 104 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 153 // Make a bitmap to return, and a canvas to draw into it. We don't just want |
| 105 // to call extractSubset or the copy constructor, since we want an actual copy | 154 // to call extractSubset or the copy constructor, since we want an actual copy |
| 106 // of the bitmap. | 155 // of the bitmap. |
| 107 SkBitmap result; | 156 SkBitmap result; |
| 108 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 157 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
| 109 | 158 |
| 110 return gfx::ImageSkiaRep(result, scale_factor_); | 159 return gfx::ImageSkiaRep(result, scale_factor_); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 DrawImageInt(image, x, y, paint); | 339 DrawImageInt(image, x, y, paint); |
| 291 } | 340 } |
| 292 | 341 |
| 293 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { | 342 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { |
| 294 SkPaint paint; | 343 SkPaint paint; |
| 295 paint.setAlpha(a); | 344 paint.setAlpha(a); |
| 296 DrawImageInt(image, x, y, paint); | 345 DrawImageInt(image, x, y, paint); |
| 297 } | 346 } |
| 298 | 347 |
| 299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 348 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
| 300 int x, int y, | 349 int x, |
| 350 int y, | |
| 301 const SkPaint& paint) { | 351 const SkPaint& paint) { |
| 302 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); | 352 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); |
| 303 if (image_rep.is_null()) | 353 if (image_rep.is_null()) |
| 304 return; | 354 return; |
| 305 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 355 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
| 306 float bitmap_scale = image_rep.GetScale(); | 356 float bitmap_scale = image_rep.GetScale(); |
| 307 | 357 |
| 308 canvas_->save(); | 358 canvas_->save(); |
| 309 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 359 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
| 310 SkFloatToScalar(1.0f / bitmap_scale)); | 360 SkFloatToScalar(1.0f / bitmap_scale)); |
| 311 canvas_->drawBitmap(bitmap, | 361 canvas_->drawBitmap(bitmap, |
| 312 SkFloatToScalar(x * bitmap_scale), | 362 SkFloatToScalar(x * bitmap_scale), |
| 313 SkFloatToScalar(y * bitmap_scale), | 363 SkFloatToScalar(y * bitmap_scale), |
| 314 &paint); | 364 &paint); |
| 315 canvas_->restore(); | 365 canvas_->restore(); |
| 316 } | 366 } |
| 317 | 367 |
| 318 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 368 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
| 319 int src_x, int src_y, int src_w, int src_h, | 369 int src_x, |
| 320 int dest_x, int dest_y, int dest_w, int dest_h, | 370 int src_y, |
| 371 int src_w, | |
| 372 int src_h, | |
| 373 int dest_x, | |
| 374 int dest_y, | |
| 375 int dest_w, | |
| 376 int dest_h, | |
| 321 bool filter) { | 377 bool filter) { |
| 322 SkPaint p; | 378 SkPaint p; |
| 323 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, | 379 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 324 dest_w, dest_h, filter, p); | 380 dest_w, dest_h, filter, p); |
| 325 } | 381 } |
| 326 | 382 |
| 327 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 383 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
| 328 int src_x, int src_y, int src_w, int src_h, | 384 int src_x, |
| 329 int dest_x, int dest_y, int dest_w, int dest_h, | 385 int src_y, |
| 386 int src_w, | |
| 387 int src_h, | |
| 388 int dest_x, | |
| 389 int dest_y, | |
| 390 int dest_w, | |
| 391 int dest_h, | |
| 330 bool filter, | 392 bool filter, |
| 331 const SkPaint& paint) { | 393 const SkPaint& paint) { |
| 332 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 394 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 333 src_y + src_h < std::numeric_limits<int16_t>::max()); | 395 src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 334 if (src_w <= 0 || src_h <= 0) { | 396 if (src_w <= 0 || src_h <= 0) { |
| 335 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 397 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
| 336 return; | 398 return; |
| 337 } | 399 } |
| 338 | 400 |
| 339 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 401 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( | 464 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( |
| 403 image_rep, | 465 image_rep, |
| 404 SkShader::kRepeat_TileMode, | 466 SkShader::kRepeat_TileMode, |
| 405 matrix); | 467 matrix); |
| 406 | 468 |
| 407 SkPaint p(paint); | 469 SkPaint p(paint); |
| 408 p.setShader(shader.get()); | 470 p.setShader(shader.get()); |
| 409 canvas_->drawPath(path, p); | 471 canvas_->drawPath(path, p); |
| 410 } | 472 } |
| 411 | 473 |
| 474 void Canvas::DrawStringRect(const base::string16& text, | |
| 475 const gfx::FontList& font_list, | |
| 476 SkColor color, | |
| 477 const gfx::Rect& display_rect) { | |
| 478 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 479 NOTIMPLEMENTED(); | |
| 480 #else | |
| 481 DrawStringWithFlagsRect(text, font_list, color, display_rect, | |
| 482 DefaultCanvasTextAlignment()); | |
| 483 #endif | |
| 484 } | |
| 485 | |
| 486 void Canvas::DrawStringWithFlagsRect(const base::string16& text, | |
| 487 const gfx::FontList& font_list, | |
| 488 SkColor color, | |
| 489 const gfx::Rect& display_rect, | |
| 490 int flags) { | |
| 491 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 492 NOTIMPLEMENTED(); | |
| 493 #else | |
| 494 DrawStringWithShadowsRect(text, font_list, color, display_rect, 0, flags, | |
| 495 ShadowValues()); | |
| 496 #endif | |
| 497 } | |
| 498 | |
| 412 void Canvas::DrawStringInt(const base::string16& text, | 499 void Canvas::DrawStringInt(const base::string16& text, |
| 413 const gfx::Font& font, | 500 const gfx::Font& font, |
| 414 SkColor color, | 501 SkColor color, |
| 415 int x, int y, int w, int h) { | 502 int x, |
| 503 int y, | |
| 504 int w, | |
| 505 int h) { | |
| 416 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); | 506 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); |
| 417 } | 507 } |
| 418 | 508 |
| 419 void Canvas::DrawStringInt(const base::string16& text, | 509 void Canvas::DrawStringInt(const base::string16& text, |
| 420 const gfx::Font& font, | 510 const gfx::Font& font, |
| 421 SkColor color, | 511 SkColor color, |
| 422 const gfx::Rect& display_rect) { | 512 const gfx::Rect& display_rect) { |
| 423 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 513 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
| 424 display_rect.width(), display_rect.height()); | 514 display_rect.width(), display_rect.height()); |
| 425 } | 515 } |
| 426 | 516 |
| 427 void Canvas::DrawStringInt(const base::string16& text, | 517 void Canvas::DrawStringInt(const base::string16& text, |
| 428 const gfx::Font& font, | 518 const gfx::Font& font, |
| 429 SkColor color, | 519 SkColor color, |
| 430 int x, int y, int w, int h, | 520 int x, |
| 521 int y, | |
| 522 int w, | |
| 523 int h, | |
| 431 int flags) { | 524 int flags) { |
| 432 DrawStringWithShadows(text, | 525 DrawStringWithShadows(text, font, color, gfx::Rect(x, y, w, h), 0, flags, |
| 433 font, | |
| 434 color, | |
| 435 gfx::Rect(x, y, w, h), | |
| 436 0, | |
| 437 flags, | |
| 438 ShadowValues()); | 526 ShadowValues()); |
| 439 } | 527 } |
| 440 | 528 |
| 529 void Canvas::DrawStringWithShadows(const base::string16& text, | |
| 530 const gfx::Font& font, | |
| 531 SkColor color, | |
| 532 const gfx::Rect& text_bounds, | |
| 533 int line_height, | |
| 534 int flags, | |
| 535 const ShadowValues& shadows) { | |
| 536 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 537 NOTIMPLEMENTED(); | |
| 538 #else | |
| 539 DrawStringWithShadowsRect(text, gfx::FontList(font), color, text_bounds, | |
| 540 line_height, flags, shadows); | |
| 541 #endif | |
| 542 } | |
| 543 | |
| 441 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 544 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
| 442 int x, int y, int w, int h) { | 545 int x, |
| 546 int y, | |
| 547 int w, | |
| 548 int h) { | |
| 443 TileImageInt(image, 0, 0, x, y, w, h); | 549 TileImageInt(image, 0, 0, x, y, w, h); |
| 444 } | 550 } |
| 445 | 551 |
| 446 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 552 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
| 447 int src_x, int src_y, | 553 int src_x, |
| 448 int dest_x, int dest_y, int w, int h) { | 554 int src_y, |
| 555 int dest_x, | |
| 556 int dest_y, | |
| 557 int w, | |
| 558 int h) { | |
| 449 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); | 559 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); |
| 450 } | 560 } |
| 451 | 561 |
| 452 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 562 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
| 453 int src_x, int src_y, | 563 int src_x, |
| 454 float tile_scale_x, float tile_scale_y, | 564 int src_y, |
| 455 int dest_x, int dest_y, int w, int h) { | 565 float tile_scale_x, |
| 566 float tile_scale_y, | |
| 567 int dest_x, | |
| 568 int dest_y, | |
| 569 int w, | |
| 570 int h) { | |
| 456 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 571 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
| 457 return; | 572 return; |
| 458 | 573 |
| 459 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, | 574 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, |
| 460 tile_scale_x, tile_scale_y); | 575 tile_scale_x, tile_scale_y); |
| 461 if (image_rep.is_null()) | 576 if (image_rep.is_null()) |
| 462 return; | 577 return; |
| 463 | 578 |
| 464 SkMatrix shader_scale; | 579 SkMatrix shader_scale; |
| 465 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 580 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 488 } | 603 } |
| 489 | 604 |
| 490 void Canvas::EndPlatformPaint() { | 605 void Canvas::EndPlatformPaint() { |
| 491 skia::EndPlatformPaint(canvas_); | 606 skia::EndPlatformPaint(canvas_); |
| 492 } | 607 } |
| 493 | 608 |
| 494 void Canvas::Transform(const gfx::Transform& transform) { | 609 void Canvas::Transform(const gfx::Transform& transform) { |
| 495 canvas_->concat(transform.matrix()); | 610 canvas_->concat(transform.matrix()); |
| 496 } | 611 } |
| 497 | 612 |
| 613 void Canvas::DrawFadeTruncatingString( | |
| 614 const base::string16& text, | |
| 615 TruncateFadeMode truncate_mode, | |
| 616 size_t desired_characters_to_truncate_from_head, | |
| 617 const gfx::Font& font, | |
| 618 SkColor color, | |
| 619 const gfx::Rect& display_rect) { | |
| 620 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_MACOSX) | |
| 621 NOTIMPLEMENTED(); | |
| 622 #else | |
| 623 DrawFadeTruncatingStringRect(text, truncate_mode, | |
| 624 desired_characters_to_truncate_from_head, | |
| 625 gfx::FontList(font), color, display_rect); | |
| 626 #endif | |
| 627 } | |
| 628 | |
| 498 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) | 629 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) |
| 499 : scale_factor_(scale_factor), | 630 : scale_factor_(scale_factor), |
| 500 owned_canvas_(), | 631 owned_canvas_(), |
| 501 canvas_(canvas) { | 632 canvas_(canvas) { |
| 502 DCHECK(canvas); | 633 DCHECK(canvas); |
| 503 } | 634 } |
| 504 | 635 |
| 505 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { | 636 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { |
| 506 SkRect clip; | 637 SkRect clip; |
| 507 return canvas_->getClipBounds(&clip) && | 638 return canvas_->getClipBounds(&clip) && |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 534 | 665 |
| 535 float bitmap_scale = image_rep.GetScale(); | 666 float bitmap_scale = image_rep.GetScale(); |
| 536 if (scale_x < bitmap_scale || scale_y < bitmap_scale) | 667 if (scale_x < bitmap_scale || scale_y < bitmap_scale) |
| 537 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); | 668 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); |
| 538 } | 669 } |
| 539 | 670 |
| 540 return image_rep; | 671 return image_rep; |
| 541 } | 672 } |
| 542 | 673 |
| 543 } // namespace gfx | 674 } // namespace gfx |
| OLD | NEW |