Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: ui/gfx/canvas.cc

Issue 2523673004: [NOT FOR COMMIT] Fully replace SkCanvas uses.
Patch Set: Support Android build. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_paint_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "skia/ext/cdl_canvas.h"
13 #include "skia/ext/cdl_paint.h"
14 #include "skia/ext/cdl_shader.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkPath.h" 16 #include "third_party/skia/include/core/SkPath.h"
14 #include "third_party/skia/include/core/SkRefCnt.h" 17 #include "third_party/skia/include/core/SkRefCnt.h"
15 #include "third_party/skia/include/effects/SkGradientShader.h" 18 #include "third_party/skia/include/effects/SkGradientShader.h"
16 #include "ui/gfx/font_list.h" 19 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/geometry/insets_f.h" 20 #include "ui/gfx/geometry/insets_f.h"
18 #include "ui/gfx/geometry/rect.h" 21 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/rect_conversions.h" 22 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/geometry/rect_f.h" 23 #include "ui/gfx/geometry/rect_f.h"
21 #include "ui/gfx/geometry/safe_integer_conversions.h" 24 #include "ui/gfx/geometry/safe_integer_conversions.h"
(...skipping 20 matching lines...) Expand all
42 45
43 SkScalar scale_scalar = SkFloatToScalar(image_scale); 46 SkScalar scale_scalar = SkFloatToScalar(image_scale);
44 canvas_->scale(scale_scalar, scale_scalar); 47 canvas_->scale(scale_scalar, scale_scalar);
45 } 48 }
46 49
47 Canvas::Canvas() 50 Canvas::Canvas()
48 : image_scale_(1.f), 51 : image_scale_(1.f),
49 canvas_owner_(skia::CreatePlatformCanvas(0, 0, false)), 52 canvas_owner_(skia::CreatePlatformCanvas(0, 0, false)),
50 canvas_(canvas_owner_.get()) {} 53 canvas_(canvas_owner_.get()) {}
51 54
52 Canvas::Canvas(SkCanvas* canvas, float image_scale) 55 Canvas::Canvas(CdlCanvas* canvas, float image_scale)
53 : image_scale_(image_scale), canvas_(canvas) { 56 : image_scale_(image_scale), canvas_(canvas) {
54 DCHECK(canvas_); 57 DCHECK(canvas_);
55 } 58 }
56 59
57 Canvas::~Canvas() { 60 Canvas::~Canvas() {
58 } 61 }
59 62
60 void Canvas::RecreateBackingCanvas(const Size& size, 63 void Canvas::RecreateBackingCanvas(const Size& size,
61 float image_scale, 64 float image_scale,
62 bool is_opaque) { 65 bool is_opaque) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 for (int i = 0; i < row_pixels; i++) { 148 for (int i = 0; i < row_pixels; i++) {
146 for (int u = 0; u < col_pixels; u++) { 149 for (int u = 0; u < col_pixels; u++) {
147 if ((u % 2 + i % 2) % 2 != 0) { 150 if ((u % 2 + i % 2) % 2 != 0) {
148 dot[i * row_pixels + u] = color; 151 dot[i * row_pixels + u] = color;
149 } 152 }
150 } 153 }
151 } 154 }
152 } 155 }
153 156
154 // Make a shader for the bitmap with an origin of the box we'll draw. 157 // Make a shader for the bitmap with an origin of the box we'll draw.
155 SkPaint paint; 158 CdlPaint paint;
156 paint.setShader(SkShader::MakeBitmapShader(*dots, SkShader::kRepeat_TileMode, 159 paint.setShader(WrapSkShader(SkShader::MakeBitmapShader(
157 SkShader::kRepeat_TileMode)); 160 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode)));
158 161
159 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), paint); 162 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), paint);
160 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), 163 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1),
161 paint); 164 paint);
162 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), paint); 165 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), paint);
163 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), 166 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()),
164 paint); 167 paint);
165 } 168 }
166 169
167 float Canvas::UndoDeviceScaleFactor() { 170 float Canvas::UndoDeviceScaleFactor() {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 230
228 void Canvas::DrawColor(SkColor color, SkBlendMode mode) { 231 void Canvas::DrawColor(SkColor color, SkBlendMode mode) {
229 canvas_->drawColor(color, mode); 232 canvas_->drawColor(color, mode);
230 } 233 }
231 234
232 void Canvas::FillRect(const Rect& rect, SkColor color) { 235 void Canvas::FillRect(const Rect& rect, SkColor color) {
233 FillRect(rect, color, SkBlendMode::kSrcOver); 236 FillRect(rect, color, SkBlendMode::kSrcOver);
234 } 237 }
235 238
236 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) { 239 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) {
237 SkPaint paint; 240 CdlPaint paint;
238 paint.setColor(color); 241 paint.setColor(color);
239 paint.setStyle(SkPaint::kFill_Style); 242 paint.setStyle(CdlPaint::kFill_Style);
240 paint.setBlendMode(mode); 243 paint.setBlendMode(mode);
241 DrawRect(rect, paint); 244 DrawRect(rect, paint);
242 } 245 }
243 246
244 void Canvas::DrawRect(const Rect& rect, SkColor color) { 247 void Canvas::DrawRect(const Rect& rect, SkColor color) {
245 DrawRect(RectF(rect), color); 248 DrawRect(RectF(rect), color);
246 } 249 }
247 250
248 void Canvas::DrawRect(const RectF& rect, SkColor color) { 251 void Canvas::DrawRect(const RectF& rect, SkColor color) {
249 DrawRect(rect, color, SkBlendMode::kSrcOver); 252 DrawRect(rect, color, SkBlendMode::kSrcOver);
250 } 253 }
251 254
252 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) { 255 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) {
253 DrawRect(RectF(rect), color, mode); 256 DrawRect(RectF(rect), color, mode);
254 } 257 }
255 258
256 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) { 259 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) {
257 SkPaint paint; 260 CdlPaint paint;
258 paint.setColor(color); 261 paint.setColor(color);
259 paint.setStyle(SkPaint::kStroke_Style); 262 paint.setStyle(CdlPaint::kStroke_Style);
260 // Set a stroke width of 0, which will put us down the stroke rect path. If 263 // Set a stroke width of 0, which will put us down the stroke rect path. If
261 // we set a stroke width of 1, for example, this will internally create a 264 // we set a stroke width of 1, for example, this will internally create a
262 // path and fill it, which causes problems near the edge of the canvas. 265 // path and fill it, which causes problems near the edge of the canvas.
263 paint.setStrokeWidth(SkIntToScalar(0)); 266 paint.setStrokeWidth(SkIntToScalar(0));
264 paint.setBlendMode(mode); 267 paint.setBlendMode(mode);
265 268
266 DrawRect(rect, paint); 269 DrawRect(rect, paint);
267 } 270 }
268 271
269 void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) { 272 void Canvas::DrawRect(const Rect& rect, const CdlPaint& paint) {
270 DrawRect(RectF(rect), paint); 273 DrawRect(RectF(rect), paint);
271 } 274 }
272 275
273 void Canvas::DrawRect(const RectF& rect, const SkPaint& paint) { 276 void Canvas::DrawRect(const RectF& rect, const CdlPaint& paint) {
274 canvas_->drawRect(RectFToSkRect(rect), paint); 277 canvas_->drawRect(RectFToSkRect(rect), paint);
275 } 278 }
276 279
277 void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) { 280 void Canvas::DrawPoint(const Point& p1, const CdlPaint& paint) {
278 DrawPoint(PointF(p1), paint); 281 DrawPoint(PointF(p1), paint);
279 } 282 }
280 283
281 void Canvas::DrawPoint(const PointF& p1, const SkPaint& paint) { 284 void Canvas::DrawPoint(const PointF& p1, const CdlPaint& paint) {
282 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), paint); 285 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), paint);
283 } 286 }
284 287
285 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { 288 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) {
286 DrawLine(PointF(p1), PointF(p2), color); 289 DrawLine(PointF(p1), PointF(p2), color);
287 } 290 }
288 291
289 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { 292 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) {
290 SkPaint paint; 293 CdlPaint paint;
291 paint.setColor(color); 294 paint.setColor(color);
292 paint.setStrokeWidth(SkIntToScalar(1)); 295 paint.setStrokeWidth(SkIntToScalar(1));
293 DrawLine(p1, p2, paint); 296 DrawLine(p1, p2, paint);
294 } 297 }
295 298
296 void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) { 299 void Canvas::DrawLine(const Point& p1, const Point& p2, const CdlPaint& paint) {
297 DrawLine(PointF(p1), PointF(p2), paint); 300 DrawLine(PointF(p1), PointF(p2), paint);
298 } 301 }
299 302
300 void Canvas::DrawLine(const PointF& p1, 303 void Canvas::DrawLine(const PointF& p1,
301 const PointF& p2, 304 const PointF& p2,
302 const SkPaint& paint) { 305 const CdlPaint& paint) {
303 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), 306 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()),
304 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint); 307 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint);
305 } 308 }
306 309
307 void Canvas::DrawCircle(const Point& center_point, 310 void Canvas::DrawCircle(const Point& center_point,
308 int radius, 311 int radius,
309 const SkPaint& paint) { 312 const CdlPaint& paint) {
310 DrawCircle(PointF(center_point), radius, paint); 313 DrawCircle(PointF(center_point), radius, paint);
311 } 314 }
312 315
313 void Canvas::DrawCircle(const PointF& center_point, 316 void Canvas::DrawCircle(const PointF& center_point,
314 float radius, 317 float radius,
315 const SkPaint& paint) { 318 const CdlPaint& paint) {
316 canvas_->drawCircle(SkFloatToScalar(center_point.x()), 319 canvas_->drawCircle(SkFloatToScalar(center_point.x()),
317 SkFloatToScalar(center_point.y()), 320 SkFloatToScalar(center_point.y()),
318 SkFloatToScalar(radius), paint); 321 SkFloatToScalar(radius), paint);
319 } 322 }
320 323
321 void Canvas::DrawRoundRect(const Rect& rect, 324 void Canvas::DrawRoundRect(const Rect& rect,
322 int radius, 325 int radius,
323 const SkPaint& paint) { 326 const CdlPaint& paint) {
324 DrawRoundRect(RectF(rect), radius, paint); 327 DrawRoundRect(RectF(rect), radius, paint);
325 } 328 }
326 329
327 void Canvas::DrawRoundRect(const RectF& rect, 330 void Canvas::DrawRoundRect(const RectF& rect,
328 float radius, 331 float radius,
329 const SkPaint& paint) { 332 const CdlPaint& paint) {
330 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), 333 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius),
331 SkFloatToScalar(radius), paint); 334 SkFloatToScalar(radius), paint);
332 } 335 }
333 336
334 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) { 337 void Canvas::DrawPath(const SkPath& path, const CdlPaint& paint) {
335 canvas_->drawPath(path, paint); 338 canvas_->drawPath(path, paint);
336 } 339 }
337 340
338 void Canvas::DrawFocusRect(const Rect& rect) { 341 void Canvas::DrawFocusRect(const Rect& rect) {
339 DrawFocusRect(RectF(rect)); 342 DrawFocusRect(RectF(rect));
340 } 343 }
341 344
342 void Canvas::DrawFocusRect(const RectF& rect) { 345 void Canvas::DrawFocusRect(const RectF& rect) {
343 DrawDashedRect(rect, SK_ColorGRAY); 346 DrawDashedRect(rect, SK_ColorGRAY);
344 } 347 }
345 348
346 void Canvas::DrawSolidFocusRect(const RectF& rect, 349 void Canvas::DrawSolidFocusRect(const RectF& rect,
347 SkColor color, 350 SkColor color,
348 float thickness) { 351 float thickness) {
349 SkPaint paint; 352 CdlPaint paint;
350 paint.setColor(color); 353 paint.setColor(color);
351 paint.setStrokeWidth(SkFloatToScalar(thickness)); 354 paint.setStrokeWidth(SkFloatToScalar(thickness));
352 paint.setStyle(SkPaint::kStroke_Style); 355 paint.setStyle(CdlPaint::kStroke_Style);
353 gfx::RectF draw_rect = rect; 356 gfx::RectF draw_rect = rect;
354 draw_rect.Inset(gfx::InsetsF(thickness / 2)); 357 draw_rect.Inset(gfx::InsetsF(thickness / 2));
355 DrawRect(draw_rect, paint); 358 DrawRect(draw_rect, paint);
356 } 359 }
357 360
358 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { 361 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) {
359 SkPaint paint; 362 CdlPaint paint;
360 DrawImageInt(image, x, y, paint); 363 DrawImageInt(image, x, y, paint);
361 } 364 }
362 365
363 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { 366 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) {
364 SkPaint paint; 367 CdlPaint paint;
365 paint.setAlpha(a); 368 paint.setAlpha(a);
366 DrawImageInt(image, x, y, paint); 369 DrawImageInt(image, x, y, paint);
367 } 370 }
368 371
369 void Canvas::DrawImageInt(const ImageSkia& image, 372 void Canvas::DrawImageInt(const ImageSkia& image,
370 int x, 373 int x,
371 int y, 374 int y,
372 const SkPaint& paint) { 375 const CdlPaint& paint) {
373 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); 376 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
374 if (image_rep.is_null()) 377 if (image_rep.is_null())
375 return; 378 return;
376 const SkBitmap& bitmap = image_rep.sk_bitmap(); 379 const SkBitmap& bitmap = image_rep.sk_bitmap();
377 float bitmap_scale = image_rep.scale(); 380 float bitmap_scale = image_rep.scale();
378 381
379 ScopedCanvas scoper(this); 382 ScopedCanvas scoper(this);
380 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), 383 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
381 SkFloatToScalar(1.0f / bitmap_scale)); 384 SkFloatToScalar(1.0f / bitmap_scale));
382 canvas_->drawBitmap(bitmap, 385 canvas_->drawBitmap(bitmap,
383 SkFloatToScalar(x * bitmap_scale), 386 SkFloatToScalar(x * bitmap_scale),
384 SkFloatToScalar(y * bitmap_scale), 387 SkFloatToScalar(y * bitmap_scale),
385 &paint); 388 &paint);
386 } 389 }
387 390
388 void Canvas::DrawImageInt(const ImageSkia& image, 391 void Canvas::DrawImageInt(const ImageSkia& image,
389 int src_x, 392 int src_x,
390 int src_y, 393 int src_y,
391 int src_w, 394 int src_w,
392 int src_h, 395 int src_h,
393 int dest_x, 396 int dest_x,
394 int dest_y, 397 int dest_y,
395 int dest_w, 398 int dest_w,
396 int dest_h, 399 int dest_h,
397 bool filter) { 400 bool filter) {
398 SkPaint p; 401 CdlPaint p;
399 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, 402 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y,
400 dest_w, dest_h, filter, p); 403 dest_w, dest_h, filter, p);
401 } 404 }
402 405
403 void Canvas::DrawImageInt(const ImageSkia& image, 406 void Canvas::DrawImageInt(const ImageSkia& image,
404 int src_x, 407 int src_x,
405 int src_y, 408 int src_y,
406 int src_w, 409 int src_w,
407 int src_h, 410 int src_h,
408 int dest_x, 411 int dest_x,
409 int dest_y, 412 int dest_y,
410 int dest_w, 413 int dest_w,
411 int dest_h, 414 int dest_h,
412 bool filter, 415 bool filter,
413 const SkPaint& paint) { 416 const CdlPaint& paint) {
414 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); 417 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
415 if (image_rep.is_null()) 418 if (image_rep.is_null())
416 return; 419 return;
417 bool remove_image_scale = true; 420 bool remove_image_scale = true;
418 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, 421 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y,
419 dest_w, dest_h, filter, paint, remove_image_scale); 422 dest_w, dest_h, filter, paint, remove_image_scale);
420 } 423 }
421 424
422 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, 425 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep,
423 int dest_x, 426 int dest_x,
424 int dest_y, 427 int dest_y,
425 int dest_w, 428 int dest_w,
426 int dest_h, 429 int dest_h,
427 bool filter, 430 bool filter,
428 const SkPaint& paint) { 431 const CdlPaint& paint) {
429 int src_x = 0; 432 int src_x = 0;
430 int src_y = 0; 433 int src_y = 0;
431 int src_w = image_rep.pixel_width(); 434 int src_w = image_rep.pixel_width();
432 int src_h = image_rep.pixel_height(); 435 int src_h = image_rep.pixel_height();
433 // Don't remove image scale here, this function is used to draw the 436 // Don't remove image scale here, this function is used to draw the
434 // (already scaled) |image_rep| at a 1:1 scale with the canvas. 437 // (already scaled) |image_rep| at a 1:1 scale with the canvas.
435 bool remove_image_scale = false; 438 bool remove_image_scale = false;
436 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, 439 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y,
437 dest_w, dest_h, filter, paint, remove_image_scale); 440 dest_w, dest_h, filter, paint, remove_image_scale);
438 } 441 }
439 442
440 void Canvas::DrawImageInPath(const ImageSkia& image, 443 void Canvas::DrawImageInPath(const ImageSkia& image,
441 int x, 444 int x,
442 int y, 445 int y,
443 const SkPath& path, 446 const SkPath& path,
444 const SkPaint& paint) { 447 const CdlPaint& paint) {
445 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); 448 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
446 if (image_rep.is_null()) 449 if (image_rep.is_null())
447 return; 450 return;
448 451
449 SkMatrix matrix; 452 SkMatrix matrix;
450 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); 453 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
451 SkPaint p(paint); 454 CdlPaint p(paint);
452 p.setShader(CreateImageRepShader(image_rep, 455 p.setShader(WrapSkShader(
453 SkShader::kRepeat_TileMode, 456 CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, matrix)));
454 matrix));
455 canvas_->drawPath(path, p); 457 canvas_->drawPath(path, p);
456 } 458 }
457 459
458 void Canvas::DrawStringRect(const base::string16& text, 460 void Canvas::DrawStringRect(const base::string16& text,
459 const FontList& font_list, 461 const FontList& font_list,
460 SkColor color, 462 SkColor color,
461 const Rect& display_rect) { 463 const Rect& display_rect) {
462 DrawStringRectWithFlags(text, font_list, color, display_rect, 464 DrawStringRectWithFlags(text, font_list, color, display_rect,
463 DefaultCanvasTextAlignment()); 465 DefaultCanvasTextAlignment());
464 } 466 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 int dest_y, 501 int dest_y,
500 int w, 502 int w,
501 int h) { 503 int h) {
502 SkRect dest_rect = { SkIntToScalar(dest_x), 504 SkRect dest_rect = { SkIntToScalar(dest_x),
503 SkIntToScalar(dest_y), 505 SkIntToScalar(dest_y),
504 SkIntToScalar(dest_x + w), 506 SkIntToScalar(dest_x + w),
505 SkIntToScalar(dest_y + h) }; 507 SkIntToScalar(dest_y + h) };
506 if (!IntersectsClipRect(dest_rect)) 508 if (!IntersectsClipRect(dest_rect))
507 return; 509 return;
508 510
509 SkPaint paint; 511 CdlPaint paint;
510 if (InitSkPaintForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, 512 if (InitSkPaintForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y,
511 dest_x, dest_y, &paint)) 513 dest_x, dest_y, &paint))
512 canvas_->drawRect(dest_rect, paint); 514 canvas_->drawRect(dest_rect, paint);
513 } 515 }
514 516
515 bool Canvas::InitSkPaintForTiling(const ImageSkia& image, 517 bool Canvas::InitSkPaintForTiling(const ImageSkia& image,
516 int src_x, 518 int src_x,
517 int src_y, 519 int src_y,
518 float tile_scale_x, 520 float tile_scale_x,
519 float tile_scale_y, 521 float tile_scale_y,
520 int dest_x, 522 int dest_x,
521 int dest_y, 523 int dest_y,
522 SkPaint* paint) { 524 CdlPaint* paint) {
523 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); 525 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
524 if (image_rep.is_null()) 526 if (image_rep.is_null())
525 return false; 527 return false;
526 528
527 SkMatrix shader_scale; 529 SkMatrix shader_scale;
528 shader_scale.setScale(SkFloatToScalar(tile_scale_x), 530 shader_scale.setScale(SkFloatToScalar(tile_scale_x),
529 SkFloatToScalar(tile_scale_y)); 531 SkFloatToScalar(tile_scale_y));
530 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); 532 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
531 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); 533 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
532 534
533 paint->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, 535 paint->setShader(WrapSkShader(CreateImageRepShader(
534 shader_scale)); 536 image_rep, SkShader::kRepeat_TileMode, shader_scale)));
535 paint->setBlendMode(SkBlendMode::kSrcOver); 537 paint->setBlendMode(SkBlendMode::kSrcOver);
536 return true; 538 return true;
537 } 539 }
538 540
539 void Canvas::Transform(const gfx::Transform& transform) { 541 void Canvas::Transform(const gfx::Transform& transform) {
540 canvas_->concat(transform.matrix()); 542 canvas_->concat(transform.matrix());
541 } 543 }
542 544
543 bool Canvas::IntersectsClipRect(const SkRect& rect) { 545 bool Canvas::IntersectsClipRect(const SkRect& rect) {
544 SkRect clip; 546 SkRect clip;
545 return canvas_->getClipBounds(&clip) && clip.intersects(rect); 547 return canvas_->getClipBounds(&clip) && clip.intersects(rect);
546 } 548 }
547 549
548 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, 550 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep,
549 int src_x, 551 int src_x,
550 int src_y, 552 int src_y,
551 int src_w, 553 int src_w,
552 int src_h, 554 int src_h,
553 int dest_x, 555 int dest_x,
554 int dest_y, 556 int dest_y,
555 int dest_w, 557 int dest_w,
556 int dest_h, 558 int dest_h,
557 bool filter, 559 bool filter,
558 const SkPaint& paint, 560 const CdlPaint& paint,
559 bool remove_image_scale) { 561 bool remove_image_scale) {
560 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && 562 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
561 src_y + src_h < std::numeric_limits<int16_t>::max()); 563 src_y + src_h < std::numeric_limits<int16_t>::max());
562 if (src_w <= 0 || src_h <= 0) { 564 if (src_w <= 0 || src_h <= 0) {
563 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; 565 NOTREACHED() << "Attempting to draw bitmap from an empty rect!";
564 return; 566 return;
565 } 567 }
566 568
567 SkRect dest_rect = { SkIntToScalar(dest_x), 569 SkRect dest_rect = { SkIntToScalar(dest_x),
568 SkIntToScalar(dest_y), 570 SkIntToScalar(dest_y),
569 SkIntToScalar(dest_x + dest_w), 571 SkIntToScalar(dest_x + dest_w),
570 SkIntToScalar(dest_y + dest_h) }; 572 SkIntToScalar(dest_y + dest_h) };
571 if (!IntersectsClipRect(dest_rect)) 573 if (!IntersectsClipRect(dest_rect))
572 return; 574 return;
573 575
574 float user_scale_x = static_cast<float>(dest_w) / src_w; 576 float user_scale_x = static_cast<float>(dest_w) / src_w;
575 float user_scale_y = static_cast<float>(dest_h) / src_h; 577 float user_scale_y = static_cast<float>(dest_h) / src_h;
576 578
577 // Make a bitmap shader that contains the bitmap we want to draw. This is 579 // Make a bitmap shader that contains the bitmap we want to draw. This is
578 // basically what SkCanvas.drawBitmap does internally, but it gives us 580 // basically what SkCanvas.drawBitmap does internally, but it gives us
579 // more control over quality and will use the mipmap in the source image if 581 // more control over quality and will use the mipmap in the source image if
580 // it has one, whereas drawBitmap won't. 582 // it has one, whereas drawBitmap won't.
581 SkMatrix shader_scale; 583 SkMatrix shader_scale;
582 shader_scale.setScale(SkFloatToScalar(user_scale_x), 584 shader_scale.setScale(SkFloatToScalar(user_scale_x),
583 SkFloatToScalar(user_scale_y)); 585 SkFloatToScalar(user_scale_y));
584 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); 586 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
585 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); 587 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
586 588
587 SkPaint p(paint); 589 CdlPaint p(paint);
588 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); 590 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
589 p.setShader(CreateImageRepShaderForScale( 591 p.setShader(WrapSkShader(CreateImageRepShaderForScale(
590 image_rep, SkShader::kRepeat_TileMode, shader_scale, 592 image_rep, SkShader::kRepeat_TileMode, shader_scale,
591 remove_image_scale ? image_rep.scale() : 1.f)); 593 remove_image_scale ? image_rep.scale() : 1.f)));
592 594
593 // The rect will be filled by the bitmap. 595 // The rect will be filled by the bitmap.
594 canvas_->drawRect(dest_rect, p); 596 canvas_->drawRect(dest_rect, p);
595 } 597 }
596 598
597 } // namespace gfx 599 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_paint_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698