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

Side by Side Diff: sky/sdk/lib/rendering/box.dart

Issue 1209413004: Instead of applying a transform for every RenderObject, pass down an Offset for where to paint. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 import 'dart:math' as math; 5 import 'dart:math' as math;
6 import 'dart:sky' as sky; 6 import 'dart:sky' as sky;
7 7
8 import 'package:vector_math/vector_math.dart'; 8 import 'package:vector_math/vector_math.dart';
9 9
10 import '../base/debug.dart'; 10 import '../base/debug.dart';
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 } 422 }
423 } 423 }
424 424
425 void hitTestChildren(HitTestResult result, { Point position }) { 425 void hitTestChildren(HitTestResult result, { Point position }) {
426 if (child != null) 426 if (child != null)
427 child.hitTest(result, position: position); 427 child.hitTest(result, position: position);
428 else 428 else
429 super.hitTestChildren(result, position: position); 429 super.hitTestChildren(result, position: position);
430 } 430 }
431 431
432 void paint(RenderCanvas canvas) { 432 void paint(RenderCanvas canvas, Offset offset) {
433 if (child != null) 433 if (child != null)
434 child.paint(canvas); 434 child.paint(canvas, offset);
435 } 435 }
436 436
437 } 437 }
438 438
439 class RenderConstrainedBox extends RenderProxyBox { 439 class RenderConstrainedBox extends RenderProxyBox {
440 RenderConstrainedBox({ 440 RenderConstrainedBox({
441 RenderBox child, 441 RenderBox child,
442 BoxConstraints additionalConstraints 442 BoxConstraints additionalConstraints
443 }) : super(child), _additionalConstraints = additionalConstraints { 443 }) : super(child), _additionalConstraints = additionalConstraints {
444 assert(additionalConstraints != null); 444 assert(additionalConstraints != null);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 double get opacity => _opacity; 543 double get opacity => _opacity;
544 void set opacity (double value) { 544 void set opacity (double value) {
545 assert(value != null); 545 assert(value != null);
546 assert(value >= 0.0 && value <= 1.0); 546 assert(value >= 0.0 && value <= 1.0);
547 if (_opacity == value) 547 if (_opacity == value)
548 return; 548 return;
549 _opacity = value; 549 _opacity = value;
550 markNeedsPaint(); 550 markNeedsPaint();
551 } 551 }
552 552
553 void paint(RenderCanvas canvas) { 553 void paint(RenderCanvas canvas, Offset offset) {
554 if (child != null) { 554 if (child != null) {
555 int a = (_opacity * 255).round(); 555 int a = (_opacity * 255).round();
556 556
557 if (a == 0) 557 if (a == 0)
558 return; 558 return;
559 559
560 if (a == 255) { 560 if (a == 255) {
561 child.paint(canvas); 561 child.paint(canvas, offset);
562 return; 562 return;
563 } 563 }
564 564
565 Paint paint = new Paint() 565 Paint paint = new Paint()
566 ..color = new Color.fromARGB(a, 0, 0, 0) 566 ..color = new Color.fromARGB(a, 0, 0, 0)
567 ..setTransferMode(sky.TransferMode.srcOver); 567 ..setTransferMode(sky.TransferMode.srcOver);
568 canvas.saveLayer(null, paint); 568 canvas.saveLayer(null, paint);
569 child.paint(canvas); 569 child.paint(canvas, offset);
570 canvas.restore(); 570 canvas.restore();
571 } 571 }
572 } 572 }
573 } 573 }
574 574
575 class RenderColorFilter extends RenderProxyBox { 575 class RenderColorFilter extends RenderProxyBox {
576 RenderColorFilter({ RenderBox child, Color color, sky.TransferMode transferMod e }) 576 RenderColorFilter({ RenderBox child, Color color, sky.TransferMode transferMod e })
577 : _color = color, _transferMode = transferMode, super(child) { 577 : _color = color, _transferMode = transferMode, super(child) {
578 } 578 }
579 579
(...skipping 10 matching lines...) Expand all
590 sky.TransferMode _transferMode; 590 sky.TransferMode _transferMode;
591 sky.TransferMode get transferMode => _transferMode; 591 sky.TransferMode get transferMode => _transferMode;
592 void set transferMode (sky.TransferMode value) { 592 void set transferMode (sky.TransferMode value) {
593 assert(value != null); 593 assert(value != null);
594 if (_transferMode == value) 594 if (_transferMode == value)
595 return; 595 return;
596 _transferMode = value; 596 _transferMode = value;
597 markNeedsPaint(); 597 markNeedsPaint();
598 } 598 }
599 599
600 void paint(RenderCanvas canvas) { 600 void paint(RenderCanvas canvas, Offset offset) {
601 if (child != null) { 601 if (child != null) {
602 Paint paint = new Paint() 602 Paint paint = new Paint()
603 ..setColorFilter(new sky.ColorFilter.mode(_color, _transferMode)); 603 ..setColorFilter(new sky.ColorFilter.mode(_color, _transferMode));
604 canvas.saveLayer(null, paint); 604 canvas.saveLayer(null, paint);
605 child.paint(canvas); 605 child.paint(canvas, offset);
606 canvas.restore(); 606 canvas.restore();
607 } 607 }
608 } 608 }
609 } 609 }
610 610
611 class RenderClipRect extends RenderProxyBox { 611 class RenderClipRect extends RenderProxyBox {
612 RenderClipRect({ RenderBox child }) : super(child); 612 RenderClipRect({ RenderBox child }) : super(child);
613 613
614 void paint(RenderCanvas canvas) { 614 void paint(RenderCanvas canvas, Offset offset) {
615 if (child != null) { 615 if (child != null) {
616 canvas.save(); 616 canvas.save();
617 canvas.clipRect(new Rect.fromSize(size)); 617 canvas.clipRect(offset + size);
618 child.paint(canvas); 618 child.paint(canvas, offset);
619 canvas.restore(); 619 canvas.restore();
620 } 620 }
621 } 621 }
622 } 622 }
623 623
624 class RenderClipRRect extends RenderProxyBox { 624 class RenderClipRRect extends RenderProxyBox {
625 RenderClipRRect({ RenderBox child, double xRadius, double yRadius }) 625 RenderClipRRect({ RenderBox child, double xRadius, double yRadius })
626 : _xRadius = xRadius, _yRadius = yRadius, super(child) { 626 : _xRadius = xRadius, _yRadius = yRadius, super(child) {
627 assert(_xRadius != null); 627 assert(_xRadius != null);
628 assert(_yRadius != null); 628 assert(_yRadius != null);
(...skipping 12 matching lines...) Expand all
641 double _yRadius; 641 double _yRadius;
642 double get yRadius => _yRadius; 642 double get yRadius => _yRadius;
643 void set yRadius (double value) { 643 void set yRadius (double value) {
644 assert(value != null); 644 assert(value != null);
645 if (_yRadius == value) 645 if (_yRadius == value)
646 return; 646 return;
647 _yRadius = value; 647 _yRadius = value;
648 markNeedsPaint(); 648 markNeedsPaint();
649 } 649 }
650 650
651 void paint(RenderCanvas canvas) { 651 void paint(RenderCanvas canvas, Offset offset) {
652 if (child != null) { 652 if (child != null) {
653 Rect rect = new Rect.fromSize(size); 653 Rect rect = offset + size;
abarth-chromium 2015/06/27 00:39:37 This looks really slick, but I wonder if it's goin
654 canvas.saveLayer(rect, new Paint()); 654 canvas.saveLayer(rect, new Paint());
655 sky.RRect rrect = new sky.RRect()..setRectXY(rect, xRadius, yRadius); 655 sky.RRect rrect = new sky.RRect()..setRectXY(rect, xRadius, yRadius);
656 canvas.clipRRect(rrect); 656 canvas.clipRRect(rrect);
657 child.paint(canvas); 657 child.paint(canvas, offset);
658 canvas.restore(); 658 canvas.restore();
659 } 659 }
660 } 660 }
661 } 661 }
662 662
663 class RenderClipOval extends RenderProxyBox { 663 class RenderClipOval extends RenderProxyBox {
664 RenderClipOval({ RenderBox child }) : super(child); 664 RenderClipOval({ RenderBox child }) : super(child);
665 665
666 void paint(RenderCanvas canvas) { 666 void paint(RenderCanvas canvas, Offset offset) {
667 if (child != null) { 667 if (child != null) {
668 Rect rect = new Rect.fromSize(size); 668 Rect rect = offset + size;
669 canvas.saveLayer(rect, new Paint()); 669 canvas.saveLayer(rect, new Paint());
670 Path path = new Path(); 670 Path path = new Path();
671 path.addOval(rect); 671 path.addOval(rect);
672 canvas.clipPath(path); 672 canvas.clipPath(path);
673 child.paint(canvas); 673 child.paint(canvas, offset);
674 canvas.restore(); 674 canvas.restore();
675 } 675 }
676 } 676 }
677 } 677 }
678 678
679 abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi n<RenderBox> { 679 abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi n<RenderBox> {
680 680
681 // Abstract class for one-child-layout render boxes 681 // Abstract class for one-child-layout render boxes
682 682
683 RenderShiftedBox(RenderBox child) { 683 RenderShiftedBox(RenderBox child) {
684 this.child = child; 684 this.child = child;
685 } 685 }
686 686
687 void paint(RenderCanvas canvas) { 687 void paint(RenderCanvas canvas, Offset offset) {
688 if (child != null) 688 if (child != null)
689 canvas.paintChild(child, child.parentData.position); 689 canvas.paintChild(child, child.parentData.position + offset);
690 } 690 }
691 691
692 double getDistanceToActualBaseline(TextBaseline baseline) { 692 double getDistanceToActualBaseline(TextBaseline baseline) {
693 double result; 693 double result;
694 if (child != null) { 694 if (child != null) {
695 assert(!needsLayout); 695 assert(!needsLayout);
696 result = child.getDistanceToActualBaseline(baseline); 696 result = child.getDistanceToActualBaseline(baseline);
697 assert(child.parentData is BoxParentData); 697 assert(child.parentData is BoxParentData);
698 if (result != null) 698 if (result != null)
699 result += child.parentData.position.y; 699 result += child.parentData.position.y;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 } 927 }
928 928
929 double getMaxIntrinsicHeight(BoxConstraints constraints) { 929 double getMaxIntrinsicHeight(BoxConstraints constraints) {
930 return _sizeForConstraints(constraints).height; 930 return _sizeForConstraints(constraints).height;
931 } 931 }
932 932
933 void performLayout() { 933 void performLayout() {
934 size = _sizeForConstraints(constraints); 934 size = _sizeForConstraints(constraints);
935 } 935 }
936 936
937 void paint(RenderCanvas canvas) { 937 void paint(RenderCanvas canvas, Offset offset) {
938 if (_image == null) return; 938 if (_image == null)
939 return;
939 bool needsScale = size.width != _image.width || size.height != _image.height ; 940 bool needsScale = size.width != _image.width || size.height != _image.height ;
940 if (needsScale) { 941 if (needsScale) {
941 double widthScale = size.width / _image.width; 942 double widthScale = size.width / _image.width;
942 double heightScale = size.height / _image.height; 943 double heightScale = size.height / _image.height;
943 canvas.save(); 944 canvas.save();
945 canvas.translate(offset.dx, offset.dy);
944 canvas.scale(widthScale, heightScale); 946 canvas.scale(widthScale, heightScale);
947 offset = Offset.zero;
945 } 948 }
946 Paint paint = new Paint(); 949 Paint paint = new Paint();
947 canvas.drawImage(_image, Point.origin, paint); 950 canvas.drawImage(_image, offset.toPoint(), paint);
948 if (needsScale) 951 if (needsScale)
949 canvas.restore(); 952 canvas.restore();
950 } 953 }
951 954
952 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}\n'; 955 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}\n';
953 } 956 }
954 957
955 class RenderDecoratedBox extends RenderProxyBox { 958 class RenderDecoratedBox extends RenderProxyBox {
956 959
957 RenderDecoratedBox({ 960 RenderDecoratedBox({
958 BoxDecoration decoration, 961 BoxDecoration decoration,
959 RenderBox child 962 RenderBox child
960 }) : _painter = new BoxPainter(decoration), super(child); 963 }) : _painter = new BoxPainter(decoration), super(child);
961 964
962 BoxPainter _painter; 965 BoxPainter _painter;
963 BoxDecoration get decoration => _painter.decoration; 966 BoxDecoration get decoration => _painter.decoration;
964 void set decoration (BoxDecoration value) { 967 void set decoration (BoxDecoration value) {
965 assert(value != null); 968 assert(value != null);
966 if (value == _painter.decoration) 969 if (value == _painter.decoration)
967 return; 970 return;
968 _painter.decoration = value; 971 _painter.decoration = value;
969 markNeedsPaint(); 972 markNeedsPaint();
970 } 973 }
971 974
972 void paint(RenderCanvas canvas) { 975 void paint(RenderCanvas canvas, Offset offset) {
973 assert(size.width != null); 976 assert(size.width != null);
974 assert(size.height != null); 977 assert(size.height != null);
975 _painter.paint(canvas, new Rect.fromSize(size)); 978 _painter.paint(canvas, offset + size);
976 super.paint(canvas); 979 super.paint(canvas, offset);
977 } 980 }
978 981
979 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n'; 982 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n';
980 } 983 }
981 984
982 class RenderTransform extends RenderProxyBox { 985 class RenderTransform extends RenderProxyBox {
983 RenderTransform({ 986 RenderTransform({
984 Matrix4 transform, 987 Matrix4 transform,
985 RenderBox child 988 RenderBox child
986 }) : super(child) { 989 }) : super(child) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 Matrix4 inverse = new Matrix4.zero(); 1035 Matrix4 inverse = new Matrix4.zero();
1033 /* double det = */ inverse.copyInverse(_transform); 1036 /* double det = */ inverse.copyInverse(_transform);
1034 // TODO(abarth): Check the determinant for degeneracy. 1037 // TODO(abarth): Check the determinant for degeneracy.
1035 1038
1036 Vector3 position3 = new Vector3(position.x, position.y, 0.0); 1039 Vector3 position3 = new Vector3(position.x, position.y, 0.0);
1037 Vector3 transformed3 = inverse.transform3(position3); 1040 Vector3 transformed3 = inverse.transform3(position3);
1038 Point transformed = new Point(transformed3.x, transformed3.y); 1041 Point transformed = new Point(transformed3.x, transformed3.y);
1039 super.hitTestChildren(result, position: transformed); 1042 super.hitTestChildren(result, position: transformed);
1040 } 1043 }
1041 1044
1042 void paint(RenderCanvas canvas) { 1045 void paint(RenderCanvas canvas, Offset offset) {
1043 canvas.save(); 1046 canvas.save();
1047 canvas.translate(offset.dx, offset.dy);
1044 canvas.concat(_transform.storage); 1048 canvas.concat(_transform.storage);
1045 super.paint(canvas); 1049 super.paint(canvas, Offset.zero);
1046 canvas.restore(); 1050 canvas.restore();
1047 } 1051 }
1048 1052
1049 String debugDescribeSettings(String prefix) { 1053 String debugDescribeSettings(String prefix) {
1050 List<String> result = _transform.toString().split('\n').map((s) => '$prefix $s\n').toList(); 1054 List<String> result = _transform.toString().split('\n').map((s) => '$prefix $s\n').toList();
1051 result.removeLast(); 1055 result.removeLast();
1052 return '${super.debugDescribeSettings(prefix)}${prefix}transform matrix:\n${ result.join()}'; 1056 return '${super.debugDescribeSettings(prefix)}${prefix}transform matrix:\n${ result.join()}';
1053 } 1057 }
1054 } 1058 }
1055 1059
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 return; 1098 return;
1095 _callback = value; 1099 _callback = value;
1096 markNeedsPaint(); 1100 markNeedsPaint();
1097 } 1101 }
1098 1102
1099 void attach() { 1103 void attach() {
1100 assert(_callback != null); 1104 assert(_callback != null);
1101 super.attach(); 1105 super.attach();
1102 } 1106 }
1103 1107
1104 void paint(RenderCanvas canvas) { 1108 void paint(RenderCanvas canvas, Offset offset) {
1105 assert(_callback != null); 1109 assert(_callback != null);
1110 canvas.translate(offset.dx, offset.dy);
1106 _callback(canvas, size); 1111 _callback(canvas, size);
1107 super.paint(canvas); 1112 super.paint(canvas, Offset.zero);
1113 canvas.translate(-offset.dx, -offset.dy);
abarth-chromium 2015/06/27 00:39:37 It's a shame that custom paint callbacks need to p
1108 } 1114 }
1109 } 1115 }
1110 1116
1111 // RENDER VIEW LAYOUT MANAGER 1117 // RENDER VIEW LAYOUT MANAGER
1112 1118
1113 class ViewConstraints { 1119 class ViewConstraints {
1114 const ViewConstraints({ 1120 const ViewConstraints({
1115 this.size: Size.zero, 1121 this.size: Size.zero,
1116 this.orientation 1122 this.orientation
1117 }); 1123 });
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 bool hitTest(HitTestResult result, { Point position }) { 1174 bool hitTest(HitTestResult result, { Point position }) {
1169 if (child != null) { 1175 if (child != null) {
1170 Rect childBounds = new Rect.fromSize(child.size); 1176 Rect childBounds = new Rect.fromSize(child.size);
1171 if (childBounds.contains(position)) 1177 if (childBounds.contains(position))
1172 child.hitTest(result, position: position); 1178 child.hitTest(result, position: position);
1173 } 1179 }
1174 result.add(new HitTestEntry(this)); 1180 result.add(new HitTestEntry(this));
1175 return true; 1181 return true;
1176 } 1182 }
1177 1183
1178 void paint(RenderCanvas canvas) { 1184 void paint(RenderCanvas canvas, Offset offset) {
1179 if (child != null) 1185 if (child != null)
1180 canvas.paintChild(child, Point.origin); 1186 canvas.paintChild(child, offset.toPoint());
1181 } 1187 }
1182 1188
1183 void paintFrame() { 1189 void paintFrame() {
1184 sky.tracing.begin('RenderView.paintFrame'); 1190 sky.tracing.begin('RenderView.paintFrame');
1185 RenderObject.debugDoingPaint = true; 1191 RenderObject.debugDoingPaint = true;
1186 try { 1192 try {
1187 sky.PictureRecorder recorder = new sky.PictureRecorder(); 1193 sky.PictureRecorder recorder = new sky.PictureRecorder();
1188 RenderCanvas canvas = new RenderCanvas(recorder, _size); 1194 RenderCanvas canvas = new RenderCanvas(recorder, _size);
1189 paint(canvas); 1195 paint(canvas, Offset.zero);
1190 sky.view.picture = recorder.endRecording(); 1196 sky.view.picture = recorder.endRecording();
1191 } finally { 1197 } finally {
1192 RenderObject.debugDoingPaint = false; 1198 RenderObject.debugDoingPaint = false;
1193 sky.tracing.end('RenderView.paintFrame'); 1199 sky.tracing.end('RenderView.paintFrame');
1194 } 1200 }
1195 } 1201 }
1196 1202
1197 } 1203 }
1198 1204
1199 // HELPER METHODS FOR RENDERBOX CONTAINERS 1205 // HELPER METHODS FOR RENDERBOX CONTAINERS
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, ch ild.size); 1248 Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, ch ild.size);
1243 if (childBounds.contains(position)) { 1249 if (childBounds.contains(position)) {
1244 if (child.hitTest(result, position: new Point(position.x - child.parentD ata.position.x, 1250 if (child.hitTest(result, position: new Point(position.x - child.parentD ata.position.x,
1245 position.y - child.par entData.position.y))) 1251 position.y - child.par entData.position.y)))
1246 break; 1252 break;
1247 } 1253 }
1248 child = child.parentData.previousSibling; 1254 child = child.parentData.previousSibling;
1249 } 1255 }
1250 } 1256 }
1251 1257
1252 void defaultPaint(RenderCanvas canvas) { 1258 void defaultPaint(RenderCanvas canvas, Offset offset) {
1253 RenderBox child = firstChild; 1259 RenderBox child = firstChild;
1254 while (child != null) { 1260 while (child != null) {
1255 assert(child.parentData is ParentDataType); 1261 assert(child.parentData is ParentDataType);
1256 canvas.paintChild(child, child.parentData.position); 1262 canvas.paintChild(child, child.parentData.position + offset);
1257 child = child.parentData.nextSibling; 1263 child = child.parentData.nextSibling;
1258 } 1264 }
1259 } 1265 }
1260 } 1266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698