| 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 <cmath> | 7 #include <cmath> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 for (int i = 0; i < row_pixels; i++) { | 160 for (int i = 0; i < row_pixels; i++) { |
| 161 for (int u = 0; u < col_pixels; u++) { | 161 for (int u = 0; u < col_pixels; u++) { |
| 162 if ((u % 2 + i % 2) % 2 != 0) { | 162 if ((u % 2 + i % 2) % 2 != 0) { |
| 163 dot[i * row_pixels + u] = color; | 163 dot[i * row_pixels + u] = color; |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 // Make a shader for the bitmap with an origin of the box we'll draw. | 169 // Make a shader for the bitmap with an origin of the box we'll draw. |
| 170 cc::PaintFlags paint; | 170 cc::PaintFlags flags; |
| 171 paint.setShader(cc::WrapSkShader(SkShader::MakeBitmapShader( | 171 flags.setShader(cc::WrapSkShader(SkShader::MakeBitmapShader( |
| 172 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode))); | 172 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode))); |
| 173 | 173 |
| 174 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), paint); | 174 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), flags); |
| 175 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), | 175 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), |
| 176 paint); | 176 flags); |
| 177 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), paint); | 177 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), flags); |
| 178 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), | 178 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), |
| 179 paint); | 179 flags); |
| 180 } | 180 } |
| 181 | 181 |
| 182 float Canvas::UndoDeviceScaleFactor() { | 182 float Canvas::UndoDeviceScaleFactor() { |
| 183 SkScalar scale_factor = 1.0f / image_scale_; | 183 SkScalar scale_factor = 1.0f / image_scale_; |
| 184 canvas_->scale(scale_factor, scale_factor); | 184 canvas_->scale(scale_factor, scale_factor); |
| 185 return image_scale_; | 185 return image_scale_; |
| 186 } | 186 } |
| 187 | 187 |
| 188 void Canvas::Save() { | 188 void Canvas::Save() { |
| 189 canvas_->save(); | 189 canvas_->save(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 242 |
| 243 void Canvas::DrawColor(SkColor color, SkBlendMode mode) { | 243 void Canvas::DrawColor(SkColor color, SkBlendMode mode) { |
| 244 canvas_->drawColor(color, mode); | 244 canvas_->drawColor(color, mode); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void Canvas::FillRect(const Rect& rect, SkColor color) { | 247 void Canvas::FillRect(const Rect& rect, SkColor color) { |
| 248 FillRect(rect, color, SkBlendMode::kSrcOver); | 248 FillRect(rect, color, SkBlendMode::kSrcOver); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) { | 251 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) { |
| 252 cc::PaintFlags paint; | 252 cc::PaintFlags flags; |
| 253 paint.setColor(color); | 253 flags.setColor(color); |
| 254 paint.setStyle(cc::PaintFlags::kFill_Style); | 254 flags.setStyle(cc::PaintFlags::kFill_Style); |
| 255 paint.setBlendMode(mode); | 255 flags.setBlendMode(mode); |
| 256 DrawRect(rect, paint); | 256 DrawRect(rect, flags); |
| 257 } | 257 } |
| 258 | 258 |
| 259 void Canvas::DrawRect(const Rect& rect, SkColor color) { | 259 void Canvas::DrawRect(const Rect& rect, SkColor color) { |
| 260 DrawRect(RectF(rect), color); | 260 DrawRect(RectF(rect), color); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void Canvas::DrawRect(const RectF& rect, SkColor color) { | 263 void Canvas::DrawRect(const RectF& rect, SkColor color) { |
| 264 DrawRect(rect, color, SkBlendMode::kSrcOver); | 264 DrawRect(rect, color, SkBlendMode::kSrcOver); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) { | 267 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) { |
| 268 DrawRect(RectF(rect), color, mode); | 268 DrawRect(RectF(rect), color, mode); |
| 269 } | 269 } |
| 270 | 270 |
| 271 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) { | 271 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) { |
| 272 cc::PaintFlags paint; | 272 cc::PaintFlags flags; |
| 273 paint.setColor(color); | 273 flags.setColor(color); |
| 274 paint.setStyle(cc::PaintFlags::kStroke_Style); | 274 flags.setStyle(cc::PaintFlags::kStroke_Style); |
| 275 // Set a stroke width of 0, which will put us down the stroke rect path. If | 275 // Set a stroke width of 0, which will put us down the stroke rect path. If |
| 276 // we set a stroke width of 1, for example, this will internally create a | 276 // we set a stroke width of 1, for example, this will internally create a |
| 277 // path and fill it, which causes problems near the edge of the canvas. | 277 // path and fill it, which causes problems near the edge of the canvas. |
| 278 paint.setStrokeWidth(SkIntToScalar(0)); | 278 flags.setStrokeWidth(SkIntToScalar(0)); |
| 279 paint.setBlendMode(mode); | 279 flags.setBlendMode(mode); |
| 280 | 280 |
| 281 DrawRect(rect, paint); | 281 DrawRect(rect, flags); |
| 282 } | 282 } |
| 283 | 283 |
| 284 void Canvas::DrawRect(const Rect& rect, const cc::PaintFlags& paint) { | 284 void Canvas::DrawRect(const Rect& rect, const cc::PaintFlags& flags) { |
| 285 DrawRect(RectF(rect), paint); | 285 DrawRect(RectF(rect), flags); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void Canvas::DrawRect(const RectF& rect, const cc::PaintFlags& paint) { | 288 void Canvas::DrawRect(const RectF& rect, const cc::PaintFlags& flags) { |
| 289 canvas_->drawRect(RectFToSkRect(rect), paint); | 289 canvas_->drawRect(RectFToSkRect(rect), flags); |
| 290 } | 290 } |
| 291 | 291 |
| 292 void Canvas::DrawPoint(const Point& p1, const cc::PaintFlags& paint) { | 292 void Canvas::DrawPoint(const Point& p1, const cc::PaintFlags& flags) { |
| 293 DrawPoint(PointF(p1), paint); | 293 DrawPoint(PointF(p1), flags); |
| 294 } | 294 } |
| 295 | 295 |
| 296 void Canvas::DrawPoint(const PointF& p1, const cc::PaintFlags& paint) { | 296 void Canvas::DrawPoint(const PointF& p1, const cc::PaintFlags& flags) { |
| 297 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), paint); | 297 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), flags); |
| 298 } | 298 } |
| 299 | 299 |
| 300 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { | 300 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { |
| 301 DrawLine(PointF(p1), PointF(p2), color); | 301 DrawLine(PointF(p1), PointF(p2), color); |
| 302 } | 302 } |
| 303 | 303 |
| 304 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { | 304 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { |
| 305 cc::PaintFlags paint; | 305 cc::PaintFlags flags; |
| 306 paint.setColor(color); | 306 flags.setColor(color); |
| 307 paint.setStrokeWidth(SkIntToScalar(1)); | 307 flags.setStrokeWidth(SkIntToScalar(1)); |
| 308 DrawLine(p1, p2, paint); | 308 DrawLine(p1, p2, flags); |
| 309 } | 309 } |
| 310 | 310 |
| 311 void Canvas::DrawLine(const Point& p1, | 311 void Canvas::DrawLine(const Point& p1, |
| 312 const Point& p2, | 312 const Point& p2, |
| 313 const cc::PaintFlags& paint) { | 313 const cc::PaintFlags& flags) { |
| 314 DrawLine(PointF(p1), PointF(p2), paint); | 314 DrawLine(PointF(p1), PointF(p2), flags); |
| 315 } | 315 } |
| 316 | 316 |
| 317 void Canvas::DrawLine(const PointF& p1, | 317 void Canvas::DrawLine(const PointF& p1, |
| 318 const PointF& p2, | 318 const PointF& p2, |
| 319 const cc::PaintFlags& paint) { | 319 const cc::PaintFlags& flags) { |
| 320 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), | 320 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), |
| 321 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint); | 321 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), flags); |
| 322 } | 322 } |
| 323 | 323 |
| 324 void Canvas::DrawCircle(const Point& center_point, | 324 void Canvas::DrawCircle(const Point& center_point, |
| 325 int radius, | 325 int radius, |
| 326 const cc::PaintFlags& paint) { | 326 const cc::PaintFlags& flags) { |
| 327 DrawCircle(PointF(center_point), radius, paint); | 327 DrawCircle(PointF(center_point), radius, flags); |
| 328 } | 328 } |
| 329 | 329 |
| 330 void Canvas::DrawCircle(const PointF& center_point, | 330 void Canvas::DrawCircle(const PointF& center_point, |
| 331 float radius, | 331 float radius, |
| 332 const cc::PaintFlags& paint) { | 332 const cc::PaintFlags& flags) { |
| 333 canvas_->drawCircle(SkFloatToScalar(center_point.x()), | 333 canvas_->drawCircle(SkFloatToScalar(center_point.x()), |
| 334 SkFloatToScalar(center_point.y()), | 334 SkFloatToScalar(center_point.y()), |
| 335 SkFloatToScalar(radius), paint); | 335 SkFloatToScalar(radius), flags); |
| 336 } | 336 } |
| 337 | 337 |
| 338 void Canvas::DrawRoundRect(const Rect& rect, | 338 void Canvas::DrawRoundRect(const Rect& rect, |
| 339 int radius, | 339 int radius, |
| 340 const cc::PaintFlags& paint) { | 340 const cc::PaintFlags& flags) { |
| 341 DrawRoundRect(RectF(rect), radius, paint); | 341 DrawRoundRect(RectF(rect), radius, flags); |
| 342 } | 342 } |
| 343 | 343 |
| 344 void Canvas::DrawRoundRect(const RectF& rect, | 344 void Canvas::DrawRoundRect(const RectF& rect, |
| 345 float radius, | 345 float radius, |
| 346 const cc::PaintFlags& paint) { | 346 const cc::PaintFlags& flags) { |
| 347 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), | 347 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), |
| 348 SkFloatToScalar(radius), paint); | 348 SkFloatToScalar(radius), flags); |
| 349 } | 349 } |
| 350 | 350 |
| 351 void Canvas::DrawPath(const SkPath& path, const cc::PaintFlags& paint) { | 351 void Canvas::DrawPath(const SkPath& path, const cc::PaintFlags& flags) { |
| 352 canvas_->drawPath(path, paint); | 352 canvas_->drawPath(path, flags); |
| 353 } | 353 } |
| 354 | 354 |
| 355 void Canvas::DrawFocusRect(const Rect& rect) { | 355 void Canvas::DrawFocusRect(const Rect& rect) { |
| 356 DrawFocusRect(RectF(rect)); | 356 DrawFocusRect(RectF(rect)); |
| 357 } | 357 } |
| 358 | 358 |
| 359 void Canvas::DrawFocusRect(const RectF& rect) { | 359 void Canvas::DrawFocusRect(const RectF& rect) { |
| 360 DrawDashedRect(rect, SK_ColorGRAY); | 360 DrawDashedRect(rect, SK_ColorGRAY); |
| 361 } | 361 } |
| 362 | 362 |
| 363 void Canvas::DrawSolidFocusRect(const RectF& rect, | 363 void Canvas::DrawSolidFocusRect(const RectF& rect, |
| 364 SkColor color, | 364 SkColor color, |
| 365 float thickness) { | 365 float thickness) { |
| 366 cc::PaintFlags paint; | 366 cc::PaintFlags flags; |
| 367 paint.setColor(color); | 367 flags.setColor(color); |
| 368 paint.setStrokeWidth(SkFloatToScalar(thickness)); | 368 flags.setStrokeWidth(SkFloatToScalar(thickness)); |
| 369 paint.setStyle(cc::PaintFlags::kStroke_Style); | 369 flags.setStyle(cc::PaintFlags::kStroke_Style); |
| 370 gfx::RectF draw_rect = rect; | 370 gfx::RectF draw_rect = rect; |
| 371 draw_rect.Inset(gfx::InsetsF(thickness / 2)); | 371 draw_rect.Inset(gfx::InsetsF(thickness / 2)); |
| 372 DrawRect(draw_rect, paint); | 372 DrawRect(draw_rect, flags); |
| 373 } | 373 } |
| 374 | 374 |
| 375 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { | 375 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { |
| 376 cc::PaintFlags paint; | 376 cc::PaintFlags flags; |
| 377 DrawImageInt(image, x, y, paint); | 377 DrawImageInt(image, x, y, flags); |
| 378 } | 378 } |
| 379 | 379 |
| 380 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { | 380 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { |
| 381 cc::PaintFlags paint; | 381 cc::PaintFlags flags; |
| 382 paint.setAlpha(a); | 382 flags.setAlpha(a); |
| 383 DrawImageInt(image, x, y, paint); | 383 DrawImageInt(image, x, y, flags); |
| 384 } | 384 } |
| 385 | 385 |
| 386 void Canvas::DrawImageInt(const ImageSkia& image, | 386 void Canvas::DrawImageInt(const ImageSkia& image, |
| 387 int x, | 387 int x, |
| 388 int y, | 388 int y, |
| 389 const cc::PaintFlags& paint) { | 389 const cc::PaintFlags& flags) { |
| 390 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 390 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
| 391 if (image_rep.is_null()) | 391 if (image_rep.is_null()) |
| 392 return; | 392 return; |
| 393 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 393 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
| 394 float bitmap_scale = image_rep.scale(); | 394 float bitmap_scale = image_rep.scale(); |
| 395 | 395 |
| 396 ScopedCanvas scoper(this); | 396 ScopedCanvas scoper(this); |
| 397 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 397 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
| 398 SkFloatToScalar(1.0f / bitmap_scale)); | 398 SkFloatToScalar(1.0f / bitmap_scale)); |
| 399 canvas_->drawBitmap(bitmap, | 399 canvas_->drawBitmap(bitmap, SkFloatToScalar(x * bitmap_scale), |
| 400 SkFloatToScalar(x * bitmap_scale), | 400 SkFloatToScalar(y * bitmap_scale), &flags); |
| 401 SkFloatToScalar(y * bitmap_scale), | |
| 402 &paint); | |
| 403 } | 401 } |
| 404 | 402 |
| 405 void Canvas::DrawImageInt(const ImageSkia& image, | 403 void Canvas::DrawImageInt(const ImageSkia& image, |
| 406 int src_x, | 404 int src_x, |
| 407 int src_y, | 405 int src_y, |
| 408 int src_w, | 406 int src_w, |
| 409 int src_h, | 407 int src_h, |
| 410 int dest_x, | 408 int dest_x, |
| 411 int dest_y, | 409 int dest_y, |
| 412 int dest_w, | 410 int dest_w, |
| 413 int dest_h, | 411 int dest_h, |
| 414 bool filter) { | 412 bool filter) { |
| 415 cc::PaintFlags p; | 413 cc::PaintFlags flags; |
| 416 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, | 414 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, dest_w, |
| 417 dest_w, dest_h, filter, p); | 415 dest_h, filter, flags); |
| 418 } | 416 } |
| 419 | 417 |
| 420 void Canvas::DrawImageInt(const ImageSkia& image, | 418 void Canvas::DrawImageInt(const ImageSkia& image, |
| 421 int src_x, | 419 int src_x, |
| 422 int src_y, | 420 int src_y, |
| 423 int src_w, | 421 int src_w, |
| 424 int src_h, | 422 int src_h, |
| 425 int dest_x, | 423 int dest_x, |
| 426 int dest_y, | 424 int dest_y, |
| 427 int dest_w, | 425 int dest_w, |
| 428 int dest_h, | 426 int dest_h, |
| 429 bool filter, | 427 bool filter, |
| 430 const cc::PaintFlags& paint) { | 428 const cc::PaintFlags& flags) { |
| 431 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 429 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
| 432 if (image_rep.is_null()) | 430 if (image_rep.is_null()) |
| 433 return; | 431 return; |
| 434 bool remove_image_scale = true; | 432 bool remove_image_scale = true; |
| 435 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, | 433 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 436 dest_w, dest_h, filter, paint, remove_image_scale); | 434 dest_w, dest_h, filter, flags, remove_image_scale); |
| 437 } | 435 } |
| 438 | 436 |
| 439 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, | 437 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, |
| 440 int dest_x, | 438 int dest_x, |
| 441 int dest_y, | 439 int dest_y, |
| 442 int dest_w, | 440 int dest_w, |
| 443 int dest_h, | 441 int dest_h, |
| 444 bool filter, | 442 bool filter, |
| 445 const cc::PaintFlags& paint) { | 443 const cc::PaintFlags& flags) { |
| 446 int src_x = 0; | 444 int src_x = 0; |
| 447 int src_y = 0; | 445 int src_y = 0; |
| 448 int src_w = image_rep.pixel_width(); | 446 int src_w = image_rep.pixel_width(); |
| 449 int src_h = image_rep.pixel_height(); | 447 int src_h = image_rep.pixel_height(); |
| 450 // Don't remove image scale here, this function is used to draw the | 448 // Don't remove image scale here, this function is used to draw the |
| 451 // (already scaled) |image_rep| at a 1:1 scale with the canvas. | 449 // (already scaled) |image_rep| at a 1:1 scale with the canvas. |
| 452 bool remove_image_scale = false; | 450 bool remove_image_scale = false; |
| 453 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, | 451 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 454 dest_w, dest_h, filter, paint, remove_image_scale); | 452 dest_w, dest_h, filter, flags, remove_image_scale); |
| 455 } | 453 } |
| 456 | 454 |
| 457 void Canvas::DrawImageInPath(const ImageSkia& image, | 455 void Canvas::DrawImageInPath(const ImageSkia& image, |
| 458 int x, | 456 int x, |
| 459 int y, | 457 int y, |
| 460 const SkPath& path, | 458 const SkPath& path, |
| 461 const cc::PaintFlags& paint) { | 459 const cc::PaintFlags& original_flags) { |
| 462 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 460 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
| 463 if (image_rep.is_null()) | 461 if (image_rep.is_null()) |
| 464 return; | 462 return; |
| 465 | 463 |
| 466 SkMatrix matrix; | 464 SkMatrix matrix; |
| 467 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); | 465 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
| 468 cc::PaintFlags p(paint); | 466 cc::PaintFlags flags(original_flags); |
| 469 p.setShader( | 467 flags.setShader( |
| 470 CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, matrix)); | 468 CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, matrix)); |
| 471 canvas_->drawPath(path, p); | 469 canvas_->drawPath(path, flags); |
| 472 } | 470 } |
| 473 | 471 |
| 474 void Canvas::DrawStringRect(const base::string16& text, | 472 void Canvas::DrawStringRect(const base::string16& text, |
| 475 const FontList& font_list, | 473 const FontList& font_list, |
| 476 SkColor color, | 474 SkColor color, |
| 477 const Rect& display_rect) { | 475 const Rect& display_rect) { |
| 478 DrawStringRectWithFlags(text, font_list, color, display_rect, | 476 DrawStringRectWithFlags(text, font_list, color, display_rect, |
| 479 DefaultCanvasTextAlignment()); | 477 DefaultCanvasTextAlignment()); |
| 480 } | 478 } |
| 481 | 479 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 506 int dest_y, | 504 int dest_y, |
| 507 int w, | 505 int w, |
| 508 int h) { | 506 int h) { |
| 509 SkRect dest_rect = { SkIntToScalar(dest_x), | 507 SkRect dest_rect = { SkIntToScalar(dest_x), |
| 510 SkIntToScalar(dest_y), | 508 SkIntToScalar(dest_y), |
| 511 SkIntToScalar(dest_x + w), | 509 SkIntToScalar(dest_x + w), |
| 512 SkIntToScalar(dest_y + h) }; | 510 SkIntToScalar(dest_y + h) }; |
| 513 if (!IntersectsClipRect(dest_rect)) | 511 if (!IntersectsClipRect(dest_rect)) |
| 514 return; | 512 return; |
| 515 | 513 |
| 516 cc::PaintFlags paint; | 514 cc::PaintFlags flags; |
| 517 if (InitPaintFlagsForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, | 515 if (InitPaintFlagsForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, |
| 518 dest_x, dest_y, &paint)) | 516 dest_x, dest_y, &flags)) |
| 519 canvas_->drawRect(dest_rect, paint); | 517 canvas_->drawRect(dest_rect, flags); |
| 520 } | 518 } |
| 521 | 519 |
| 522 bool Canvas::InitPaintFlagsForTiling(const ImageSkia& image, | 520 bool Canvas::InitPaintFlagsForTiling(const ImageSkia& image, |
| 523 int src_x, | 521 int src_x, |
| 524 int src_y, | 522 int src_y, |
| 525 float tile_scale_x, | 523 float tile_scale_x, |
| 526 float tile_scale_y, | 524 float tile_scale_y, |
| 527 int dest_x, | 525 int dest_x, |
| 528 int dest_y, | 526 int dest_y, |
| 529 cc::PaintFlags* paint) { | 527 cc::PaintFlags* flags) { |
| 530 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 528 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
| 531 if (image_rep.is_null()) | 529 if (image_rep.is_null()) |
| 532 return false; | 530 return false; |
| 533 | 531 |
| 534 SkMatrix shader_scale; | 532 SkMatrix shader_scale; |
| 535 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 533 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
| 536 SkFloatToScalar(tile_scale_y)); | 534 SkFloatToScalar(tile_scale_y)); |
| 537 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 535 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
| 538 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 536 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
| 539 | 537 |
| 540 paint->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, | 538 flags->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, |
| 541 shader_scale)); | 539 shader_scale)); |
| 542 paint->setBlendMode(SkBlendMode::kSrcOver); | 540 flags->setBlendMode(SkBlendMode::kSrcOver); |
| 543 return true; | 541 return true; |
| 544 } | 542 } |
| 545 | 543 |
| 546 void Canvas::Transform(const gfx::Transform& transform) { | 544 void Canvas::Transform(const gfx::Transform& transform) { |
| 547 canvas_->concat(transform.matrix()); | 545 canvas_->concat(transform.matrix()); |
| 548 } | 546 } |
| 549 | 547 |
| 550 bool Canvas::IntersectsClipRect(const SkRect& rect) { | 548 bool Canvas::IntersectsClipRect(const SkRect& rect) { |
| 551 SkRect clip; | 549 SkRect clip; |
| 552 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); | 550 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); |
| 553 } | 551 } |
| 554 | 552 |
| 555 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, | 553 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, |
| 556 int src_x, | 554 int src_x, |
| 557 int src_y, | 555 int src_y, |
| 558 int src_w, | 556 int src_w, |
| 559 int src_h, | 557 int src_h, |
| 560 int dest_x, | 558 int dest_x, |
| 561 int dest_y, | 559 int dest_y, |
| 562 int dest_w, | 560 int dest_w, |
| 563 int dest_h, | 561 int dest_h, |
| 564 bool filter, | 562 bool filter, |
| 565 const cc::PaintFlags& paint, | 563 const cc::PaintFlags& original_flags, |
| 566 bool remove_image_scale) { | 564 bool remove_image_scale) { |
| 567 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 565 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 568 src_y + src_h < std::numeric_limits<int16_t>::max()); | 566 src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 569 if (src_w <= 0 || src_h <= 0) { | 567 if (src_w <= 0 || src_h <= 0) { |
| 570 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 568 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
| 571 return; | 569 return; |
| 572 } | 570 } |
| 573 | 571 |
| 574 SkRect dest_rect = { SkIntToScalar(dest_x), | 572 SkRect dest_rect = { SkIntToScalar(dest_x), |
| 575 SkIntToScalar(dest_y), | 573 SkIntToScalar(dest_y), |
| 576 SkIntToScalar(dest_x + dest_w), | 574 SkIntToScalar(dest_x + dest_w), |
| 577 SkIntToScalar(dest_y + dest_h) }; | 575 SkIntToScalar(dest_y + dest_h) }; |
| 578 if (!IntersectsClipRect(dest_rect)) | 576 if (!IntersectsClipRect(dest_rect)) |
| 579 return; | 577 return; |
| 580 | 578 |
| 581 float user_scale_x = static_cast<float>(dest_w) / src_w; | 579 float user_scale_x = static_cast<float>(dest_w) / src_w; |
| 582 float user_scale_y = static_cast<float>(dest_h) / src_h; | 580 float user_scale_y = static_cast<float>(dest_h) / src_h; |
| 583 | 581 |
| 584 // Make a bitmap shader that contains the bitmap we want to draw. This is | 582 // Make a bitmap shader that contains the bitmap we want to draw. This is |
| 585 // basically what SkCanvas.drawBitmap does internally, but it gives us | 583 // basically what SkCanvas.drawBitmap does internally, but it gives us |
| 586 // more control over quality and will use the mipmap in the source image if | 584 // more control over quality and will use the mipmap in the source image if |
| 587 // it has one, whereas drawBitmap won't. | 585 // it has one, whereas drawBitmap won't. |
| 588 SkMatrix shader_scale; | 586 SkMatrix shader_scale; |
| 589 shader_scale.setScale(SkFloatToScalar(user_scale_x), | 587 shader_scale.setScale(SkFloatToScalar(user_scale_x), |
| 590 SkFloatToScalar(user_scale_y)); | 588 SkFloatToScalar(user_scale_y)); |
| 591 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 589 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
| 592 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 590 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
| 593 | 591 |
| 594 cc::PaintFlags p(paint); | 592 cc::PaintFlags flags(original_flags); |
| 595 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); | 593 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
| 596 p.setShader(CreateImageRepShaderForScale( | 594 flags.setShader(CreateImageRepShaderForScale( |
| 597 image_rep, SkShader::kRepeat_TileMode, shader_scale, | 595 image_rep, SkShader::kRepeat_TileMode, shader_scale, |
| 598 remove_image_scale ? image_rep.scale() : 1.f)); | 596 remove_image_scale ? image_rep.scale() : 1.f)); |
| 599 | 597 |
| 600 // The rect will be filled by the bitmap. | 598 // The rect will be filled by the bitmap. |
| 601 canvas_->drawRect(dest_rect, p); | 599 canvas_->drawRect(dest_rect, flags); |
| 602 } | 600 } |
| 603 | 601 |
| 604 } // namespace gfx | 602 } // namespace gfx |
| OLD | NEW |