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

Side by Side Diff: cc/base/tiling_data.cc

Issue 1017713003: cc: Reduce duplicated tiling data difference iterator code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Naming Created 5 years, 9 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 | « cc/base/tiling_data.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 The Chromium Authors. All rights reserved. 1 // Copyright 2010 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 "cc/base/tiling_data.h" 5 #include "cc/base/tiling_data.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ui/gfx/geometry/rect.h" 9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/vector2d.h" 10 #include "ui/gfx/geometry/vector2d.h"
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 if (index_x_ > right_) { 343 if (index_x_ > right_) {
344 index_x_ = left_; 344 index_x_ = left_;
345 index_y_++; 345 index_y_++;
346 if (index_y_ > bottom_) 346 if (index_y_ > bottom_)
347 done(); 347 done();
348 } 348 }
349 349
350 return *this; 350 return *this;
351 } 351 }
352 352
353 TilingData::DifferenceIterator::DifferenceIterator( 353 TilingData::BaseDifferenceIterator::BaseDifferenceIterator() {
354 done();
355 }
356
357 TilingData::BaseDifferenceIterator::BaseDifferenceIterator(
354 const TilingData* tiling_data, 358 const TilingData* tiling_data,
355 const gfx::Rect& consider_rect, 359 const gfx::Rect& consider_rect,
356 const gfx::Rect& ignore_rect) 360 const gfx::Rect& ignore_rect)
357 : consider_left_(-1), 361 : consider_left_(-1),
358 consider_top_(-1), 362 consider_top_(-1),
359 consider_right_(-1), 363 consider_right_(-1),
360 consider_bottom_(-1), 364 consider_bottom_(-1),
361 ignore_left_(-1), 365 ignore_left_(-1),
362 ignore_top_(-1), 366 ignore_top_(-1),
363 ignore_right_(-1), 367 ignore_right_(-1),
364 ignore_bottom_(-1) { 368 ignore_bottom_(-1) {
365 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) { 369 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) {
366 done(); 370 done();
367 return; 371 return;
368 } 372 }
369 373
370 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size()); 374 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
371 gfx::Rect consider(consider_rect); 375 gfx::Rect consider(consider_rect);
372 gfx::Rect ignore(ignore_rect);
373 consider.Intersect(tiling_bounds_rect); 376 consider.Intersect(tiling_bounds_rect);
374 ignore.Intersect(tiling_bounds_rect); 377
375 if (consider.IsEmpty()) { 378 if (consider.IsEmpty()) {
376 done(); 379 done();
377 return; 380 return;
378 } 381 }
379 382
380 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x()); 383 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
381 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y()); 384 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
382 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1); 385 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
383 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1); 386 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
384 387
388 gfx::Rect ignore(ignore_rect);
389 ignore.Intersect(tiling_bounds_rect);
390
385 if (!ignore.IsEmpty()) { 391 if (!ignore.IsEmpty()) {
386 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x()); 392 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
387 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y()); 393 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
388 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1); 394 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
389 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1); 395 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
390 396
391 // Clamp ignore indices to consider indices. 397 // Clamp ignore indices to consider indices.
392 ignore_left_ = std::max(ignore_left_, consider_left_); 398 ignore_left_ = std::max(ignore_left_, consider_left_);
393 ignore_top_ = std::max(ignore_top_, consider_top_); 399 ignore_top_ = std::max(ignore_top_, consider_top_);
394 ignore_right_ = std::min(ignore_right_, consider_right_); 400 ignore_right_ = std::min(ignore_right_, consider_right_);
395 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_); 401 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
402
403 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
404 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
405 consider_left_ = consider_top_ = consider_right_ = consider_bottom_ = -1;
406 done();
407 return;
408 }
396 } 409 }
410 }
397 411
398 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ && 412 bool TilingData::BaseDifferenceIterator::HasConsiderRect() const {
399 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) { 413 // Consider indices are either all valid or all equal to -1.
414 DCHECK((0 <= consider_left_ && consider_left_ <= consider_right_ &&
415 0 <= consider_top_ && consider_top_ <= consider_bottom_) ||
416 (consider_left_ == -1 && consider_top_ == -1 &&
417 consider_right_ == -1 && consider_bottom_ == -1));
418 return consider_left_ != -1;
419 }
420
421 TilingData::DifferenceIterator::DifferenceIterator(
422 const TilingData* tiling_data,
423 const gfx::Rect& consider_rect,
424 const gfx::Rect& ignore_rect)
425 : BaseDifferenceIterator(tiling_data, consider_rect, ignore_rect) {
426 if (!HasConsiderRect()) {
400 done(); 427 done();
401 return; 428 return;
402 } 429 }
403 430
404 index_x_ = consider_left_; 431 index_x_ = consider_left_;
405 index_y_ = consider_top_; 432 index_y_ = consider_top_;
406 433
407 if (in_ignore_rect()) 434 if (in_ignore_rect())
408 ++(*this); 435 ++(*this);
409 } 436 }
(...skipping 29 matching lines...) Expand all
439 466
440 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator() { 467 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator() {
441 done(); 468 done();
442 } 469 }
443 470
444 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator( 471 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator(
445 const TilingData* tiling_data, 472 const TilingData* tiling_data,
446 const gfx::Rect& consider_rect, 473 const gfx::Rect& consider_rect,
447 const gfx::Rect& ignore_rect, 474 const gfx::Rect& ignore_rect,
448 const gfx::Rect& center_rect) 475 const gfx::Rect& center_rect)
449 : consider_left_(-1), 476 : BaseDifferenceIterator(tiling_data, consider_rect, ignore_rect),
450 consider_top_(-1),
451 consider_right_(-1),
452 consider_bottom_(-1),
453 ignore_left_(-1),
454 ignore_top_(-1),
455 ignore_right_(-1),
456 ignore_bottom_(-1),
457 direction_(RIGHT), 477 direction_(RIGHT),
458 delta_x_(1), 478 delta_x_(1),
459 delta_y_(0), 479 delta_y_(0),
460 current_step_(0), 480 current_step_(0),
461 horizontal_step_count_(0), 481 horizontal_step_count_(0),
462 vertical_step_count_(0) { 482 vertical_step_count_(0) {
463 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) { 483 if (!HasConsiderRect()) {
464 done();
465 return;
466 }
467
468 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
469 gfx::Rect consider(consider_rect);
470 gfx::Rect ignore(ignore_rect);
471 gfx::Rect center(center_rect);
472 consider.Intersect(tiling_bounds_rect);
473 ignore.Intersect(tiling_bounds_rect);
474 if (consider.IsEmpty()) {
475 done();
476 return;
477 }
478
479 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
480 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
481 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
482 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
483
484 if (!ignore.IsEmpty()) {
485 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
486 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
487 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
488 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
489
490 // Clamp ignore indices to consider indices.
491 ignore_left_ = std::max(ignore_left_, consider_left_);
492 ignore_top_ = std::max(ignore_top_, consider_top_);
493 ignore_right_ = std::min(ignore_right_, consider_right_);
494 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
495 }
496
497 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
498 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
499 done(); 484 done();
500 return; 485 return;
501 } 486 }
502 487
503 // Determine around left, such that it is between -1 and num_tiles_x. 488 // Determine around left, such that it is between -1 and num_tiles_x.
504 int around_left = 0; 489 int around_left = 0;
505 if (center.x() < 0 || center.IsEmpty()) 490 if (center_rect.x() < 0 || center_rect.IsEmpty())
506 around_left = -1; 491 around_left = -1;
507 else if (center.x() >= tiling_data->tiling_size().width()) 492 else if (center_rect.x() >= tiling_data->tiling_size().width())
508 around_left = tiling_data->num_tiles_x(); 493 around_left = tiling_data->num_tiles_x();
509 else 494 else
510 around_left = tiling_data->TileXIndexFromSrcCoord(center.x()); 495 around_left = tiling_data->TileXIndexFromSrcCoord(center_rect.x());
511 496
512 // Determine around top, such that it is between -1 and num_tiles_y. 497 // Determine around top, such that it is between -1 and num_tiles_y.
513 int around_top = 0; 498 int around_top = 0;
514 if (center.y() < 0 || center.IsEmpty()) 499 if (center_rect.y() < 0 || center_rect.IsEmpty())
515 around_top = -1; 500 around_top = -1;
516 else if (center.y() >= tiling_data->tiling_size().height()) 501 else if (center_rect.y() >= tiling_data->tiling_size().height())
517 around_top = tiling_data->num_tiles_y(); 502 around_top = tiling_data->num_tiles_y();
518 else 503 else
519 around_top = tiling_data->TileYIndexFromSrcCoord(center.y()); 504 around_top = tiling_data->TileYIndexFromSrcCoord(center_rect.y());
520 505
521 // Determine around right, such that it is between -1 and num_tiles_x. 506 // Determine around right, such that it is between -1 and num_tiles_x.
522 int right_src_coord = center.right() - 1; 507 int right_src_coord = center_rect.right() - 1;
523 int around_right = 0; 508 int around_right = 0;
524 if (right_src_coord < 0 || center.IsEmpty()) { 509 if (right_src_coord < 0 || center_rect.IsEmpty()) {
525 around_right = -1; 510 around_right = -1;
526 } else if (right_src_coord >= tiling_data->tiling_size().width()) { 511 } else if (right_src_coord >= tiling_data->tiling_size().width()) {
527 around_right = tiling_data->num_tiles_x(); 512 around_right = tiling_data->num_tiles_x();
528 } else { 513 } else {
529 around_right = tiling_data->TileXIndexFromSrcCoord(right_src_coord); 514 around_right = tiling_data->TileXIndexFromSrcCoord(right_src_coord);
530 } 515 }
531 516
532 // Determine around bottom, such that it is between -1 and num_tiles_y. 517 // Determine around bottom, such that it is between -1 and num_tiles_y.
533 int bottom_src_coord = center.bottom() - 1; 518 int bottom_src_coord = center_rect.bottom() - 1;
534 int around_bottom = 0; 519 int around_bottom = 0;
535 if (bottom_src_coord < 0 || center.IsEmpty()) { 520 if (bottom_src_coord < 0 || center_rect.IsEmpty()) {
536 around_bottom = -1; 521 around_bottom = -1;
537 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) { 522 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) {
538 around_bottom = tiling_data->num_tiles_y(); 523 around_bottom = tiling_data->num_tiles_y();
539 } else { 524 } else {
540 around_bottom = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord); 525 around_bottom = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord);
541 } 526 }
542 527
543 vertical_step_count_ = around_bottom - around_top + 1; 528 vertical_step_count_ = around_bottom - around_top + 1;
544 horizontal_step_count_ = around_right - around_left + 1; 529 horizontal_step_count_ = around_right - around_left + 1;
545 current_step_ = horizontal_step_count_ - 1; 530 current_step_ = horizontal_step_count_ - 1;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 647
663 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator() { 648 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator() {
664 done(); 649 done();
665 } 650 }
666 651
667 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator( 652 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator(
668 const TilingData* tiling_data, 653 const TilingData* tiling_data,
669 const gfx::Rect& consider_rect, 654 const gfx::Rect& consider_rect,
670 const gfx::Rect& ignore_rect, 655 const gfx::Rect& ignore_rect,
671 const gfx::Rect& center_rect) 656 const gfx::Rect& center_rect)
672 : consider_left_(-1), 657 : BaseDifferenceIterator(tiling_data, consider_rect, ignore_rect),
673 consider_top_(-1),
674 consider_right_(-1),
675 consider_bottom_(-1),
676 around_left_(-1), 658 around_left_(-1),
677 around_top_(-1), 659 around_top_(-1),
678 around_right_(-1), 660 around_right_(-1),
679 around_bottom_(-1), 661 around_bottom_(-1),
680 ignore_left_(-1),
681 ignore_top_(-1),
682 ignore_right_(-1),
683 ignore_bottom_(-1),
684 direction_(LEFT), 662 direction_(LEFT),
685 delta_x_(-1), 663 delta_x_(-1),
686 delta_y_(0), 664 delta_y_(0),
687 current_step_(0), 665 current_step_(0),
688 horizontal_step_count_(0), 666 horizontal_step_count_(0),
689 vertical_step_count_(0) { 667 vertical_step_count_(0) {
690 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) { 668 if (!HasConsiderRect()) {
691 done();
692 return;
693 }
694
695 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
696 gfx::Rect consider(consider_rect);
697 gfx::Rect ignore(ignore_rect);
698 gfx::Rect center(center_rect);
699 consider.Intersect(tiling_bounds_rect);
700 ignore.Intersect(tiling_bounds_rect);
701 if (consider.IsEmpty()) {
702 done();
703 return;
704 }
705
706 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
707 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
708 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
709 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
710
711 if (!ignore.IsEmpty()) {
712 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
713 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
714 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
715 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
716
717 // Clamp ignore indices to consider indices.
718 ignore_left_ = std::max(ignore_left_, consider_left_);
719 ignore_top_ = std::max(ignore_top_, consider_top_);
720 ignore_right_ = std::min(ignore_right_, consider_right_);
721 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
722 }
723
724 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
725 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
726 done(); 669 done();
727 return; 670 return;
728 } 671 }
729 672
730 // Determine around left, such that it is between -1 and num_tiles_x. 673 // Determine around left, such that it is between -1 and num_tiles_x.
731 if (center.x() < 0 || center.IsEmpty()) 674 if (center_rect.x() < 0 || center_rect.IsEmpty())
732 around_left_ = -1; 675 around_left_ = -1;
733 else if (center.x() >= tiling_data->tiling_size().width()) 676 else if (center_rect.x() >= tiling_data->tiling_size().width())
734 around_left_ = tiling_data->num_tiles_x(); 677 around_left_ = tiling_data->num_tiles_x();
735 else 678 else
736 around_left_ = tiling_data->TileXIndexFromSrcCoord(center.x()); 679 around_left_ = tiling_data->TileXIndexFromSrcCoord(center_rect.x());
737 680
738 // Determine around top, such that it is between -1 and num_tiles_y. 681 // Determine around top, such that it is between -1 and num_tiles_y.
739 if (center.y() < 0 || center.IsEmpty()) 682 if (center_rect.y() < 0 || center_rect.IsEmpty())
740 around_top_ = -1; 683 around_top_ = -1;
741 else if (center.y() >= tiling_data->tiling_size().height()) 684 else if (center_rect.y() >= tiling_data->tiling_size().height())
742 around_top_ = tiling_data->num_tiles_y(); 685 around_top_ = tiling_data->num_tiles_y();
743 else 686 else
744 around_top_ = tiling_data->TileYIndexFromSrcCoord(center.y()); 687 around_top_ = tiling_data->TileYIndexFromSrcCoord(center_rect.y());
745 688
746 // Determine around right, such that it is between -1 and num_tiles_x. 689 // Determine around right, such that it is between -1 and num_tiles_x.
747 int right_src_coord = center.right() - 1; 690 int right_src_coord = center_rect.right() - 1;
748 if (right_src_coord < 0 || center.IsEmpty()) { 691 if (right_src_coord < 0 || center_rect.IsEmpty()) {
749 around_right_ = -1; 692 around_right_ = -1;
750 } else if (right_src_coord >= tiling_data->tiling_size().width()) { 693 } else if (right_src_coord >= tiling_data->tiling_size().width()) {
751 around_right_ = tiling_data->num_tiles_x(); 694 around_right_ = tiling_data->num_tiles_x();
752 } else { 695 } else {
753 around_right_ = tiling_data->TileXIndexFromSrcCoord(right_src_coord); 696 around_right_ = tiling_data->TileXIndexFromSrcCoord(right_src_coord);
754 } 697 }
755 698
756 // Determine around bottom, such that it is between -1 and num_tiles_y. 699 // Determine around bottom, such that it is between -1 and num_tiles_y.
757 int bottom_src_coord = center.bottom() - 1; 700 int bottom_src_coord = center_rect.bottom() - 1;
758 if (bottom_src_coord < 0 || center.IsEmpty()) { 701 if (bottom_src_coord < 0 || center_rect.IsEmpty()) {
759 around_bottom_ = -1; 702 around_bottom_ = -1;
760 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) { 703 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) {
761 around_bottom_ = tiling_data->num_tiles_y(); 704 around_bottom_ = tiling_data->num_tiles_y();
762 } else { 705 } else {
763 around_bottom_ = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord); 706 around_bottom_ = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord);
764 } 707 }
765 708
766 // Figure out the maximum distance from the around edge to consider edge. 709 // Figure out the maximum distance from the around edge to consider edge.
767 int max_distance = 0; 710 int max_distance = 0;
768 max_distance = std::max(max_distance, around_top_ - consider_top_); 711 max_distance = std::max(max_distance, around_top_ - consider_top_);
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 855
913 // We should always end up in an around rect at some point. 856 // We should always end up in an around rect at some point.
914 // Since the direction is now vertical, we have to ensure that we will 857 // Since the direction is now vertical, we have to ensure that we will
915 // advance. 858 // advance.
916 DCHECK_GE(horizontal_step_count_, 1); 859 DCHECK_GE(horizontal_step_count_, 1);
917 DCHECK_GE(vertical_step_count_, 1); 860 DCHECK_GE(vertical_step_count_, 1);
918 } 861 }
919 } 862 }
920 863
921 } // namespace cc 864 } // namespace cc
OLDNEW
« no previous file with comments | « cc/base/tiling_data.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698