1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#import <AVFoundation/AVFoundation.h>
#include <memory>
#include "base/test/scoped_feature_list.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accelerated_widget_mac/ca_renderer_layer_tree.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/mac/io_surface.h"
#include "ui/gl/ca_renderer_layer_params.h"
@interface CALayer (Private)
@property BOOL wantsExtendedDynamicRangeContent;
@end
namespace gpu {
namespace {
struct CALayerProperties {
CALayerProperties() = default;
~CALayerProperties() = default;
bool is_clipped = true;
gfx::Rect clip_rect;
gfx::RRectF rounded_corner_bounds;
int sorting_context_id = 0;
gfx::Transform transform;
gfx::RectF contents_rect = gfx::RectF(0.0f, 0.0f, 1.0f, 1.0f);
gfx::Rect rect = gfx::Rect(0, 0, 256, 256);
SkColor4f background_color = SkColors::kWhite;
unsigned edge_aa_mask = 0;
float opacity = 1.0f;
float scale_factor = 1.0f;
unsigned filter = GL_LINEAR;
gfx::ScopedIOSurface io_surface;
gfx::ColorSpace color_space;
base::apple::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer;
bool allow_av_layers = true;
bool allow_solid_color_layers = true;
};
base::apple::ScopedCFTypeRef<CVPixelBufferRef> CreateCVPixelBuffer(
gfx::ScopedIOSurface io_surface) {
base::apple::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer;
CVPixelBufferCreateWithIOSurface(nullptr, io_surface.get(), nullptr,
cv_pixel_buffer.InitializeInto());
return cv_pixel_buffer;
}
bool ScheduleCALayer(ui::CARendererLayerTree* tree,
CALayerProperties* properties) {
gfx::ScopedIOSurface io_surface;
gfx::ColorSpace io_surface_color_space;
if (properties->io_surface) {
io_surface = properties->io_surface;
io_surface_color_space = properties->color_space;
}
return tree->ScheduleCALayer(ui::CARendererLayerParams(
properties->is_clipped, properties->clip_rect,
properties->rounded_corner_bounds, properties->sorting_context_id,
properties->transform, io_surface, io_surface_color_space,
properties->contents_rect, properties->rect, properties->background_color,
properties->edge_aa_mask, properties->opacity, properties->filter,
gfx::HDRMetadata(), gfx::ProtectedVideoType::kClear, false));
}
void UpdateCALayerTree(std::unique_ptr<ui::CARendererLayerTree>& ca_layer_tree,
CALayerProperties* properties,
CALayer* superlayer) {
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(properties->allow_av_layers,
properties->allow_solid_color_layers));
bool result = ScheduleCALayer(new_ca_layer_tree.get(), properties);
EXPECT_TRUE(result);
new_ca_layer_tree->CommitScheduledCALayers(
superlayer, std::move(ca_layer_tree), properties->rect.size(),
properties->scale_factor);
std::swap(new_ca_layer_tree, ca_layer_tree);
}
} // namespace
class CALayerTreeTest : public testing::Test {
protected:
void SetUp() override { superlayer_ = [[CALayer alloc] init]; }
// Traverse the tree. Validate that there exists only one content layer, and
// return that layer.
CALayer* GetOnlyContentLayer() {
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
CALayer* transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
return [transform_layer sublayers][0];
}
CALayer* __strong superlayer_;
};
// Test updating each layer's properties.
class CALayerTreePropertyUpdatesTest : public CALayerTreeTest {
public:
void RunTest(bool allow_solid_color_layers) {
CALayerProperties properties;
properties.allow_solid_color_layers = allow_solid_color_layers;
properties.clip_rect = gfx::Rect(2, 4, 8, 16);
properties.rounded_corner_bounds = gfx::RRectF(2, 4, 8, 16, 13);
properties.transform.Translate(10, 20);
properties.contents_rect = gfx::RectF(0.0f, 0.25f, 0.5f, 0.75f);
properties.rect = gfx::Rect(16, 32, 64, 128);
properties.background_color = SkColors::kRed;
properties.edge_aa_mask = ui::CALayerEdge::kLayerEdgeLeft;
properties.opacity = 0.5f;
properties.io_surface =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree;
CALayer* root_layer = nil;
CALayer* clip_and_sorting_layer = nil;
CALayer* rounded_rect_layer = nil;
CALayer* transform_layer = nil;
CALayer* content_layer = nil;
// Validate the initial values.
{
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(true, allow_solid_color_layers));
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* superlayer_for_transform = clip_and_sorting_layer;
if (!properties.rounded_corner_bounds.IsEmpty()) {
rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
superlayer_for_transform = rounded_rect_layer;
}
transform_layer = [superlayer_for_transform sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
content_layer = [transform_layer sublayers][0];
// Validate the clip and sorting context layer.
EXPECT_TRUE([clip_and_sorting_layer masksToBounds]);
EXPECT_EQ(gfx::Rect(properties.clip_rect.size()),
gfx::Rect([clip_and_sorting_layer bounds]));
EXPECT_EQ(properties.rounded_corner_bounds.GetSimpleRadius(),
[rounded_rect_layer cornerRadius]);
EXPECT_EQ(properties.clip_rect.origin(),
gfx::Point([clip_and_sorting_layer position]));
EXPECT_EQ(-properties.clip_rect.origin().x(),
[clip_and_sorting_layer sublayerTransform].m41);
EXPECT_EQ(-properties.clip_rect.origin().y(),
[clip_and_sorting_layer sublayerTransform].m42);
// Validate the transform layer.
EXPECT_EQ(properties.transform.rc(3, 0),
[transform_layer sublayerTransform].m41);
EXPECT_EQ(properties.transform.rc(3, 1),
[transform_layer sublayerTransform].m42);
// Validate the content layer.
EXPECT_EQ((__bridge id)properties.io_surface.get(),
[content_layer contents]);
EXPECT_EQ(properties.contents_rect,
gfx::RectF([content_layer contentsRect]));
EXPECT_EQ(properties.rect.origin(), gfx::Point([content_layer position]));
EXPECT_EQ(gfx::Rect(properties.rect.size()),
gfx::Rect([content_layer bounds]));
EXPECT_EQ(kCALayerLeftEdge, [content_layer edgeAntialiasingMask]);
EXPECT_EQ(properties.opacity, [content_layer opacity]);
EXPECT_NSEQ(kCAFilterNearest, [content_layer minificationFilter]);
EXPECT_NSEQ(kCAFilterNearest, [content_layer magnificationFilter]);
EXPECT_EQ(properties.scale_factor, [content_layer contentsScale]);
}
// Update just the clip rect and re-commit.
{
properties.clip_rect = gfx::Rect(4, 8, 16, 32);
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
// Validate the clip and sorting context layer.
EXPECT_TRUE([clip_and_sorting_layer masksToBounds]);
EXPECT_EQ(gfx::Rect(properties.clip_rect.size()),
gfx::Rect([clip_and_sorting_layer bounds]));
EXPECT_EQ(properties.clip_rect.origin(),
gfx::Point([clip_and_sorting_layer position]));
EXPECT_EQ(-properties.clip_rect.origin().x(),
[clip_and_sorting_layer sublayerTransform].m41);
EXPECT_EQ(-properties.clip_rect.origin().y(),
[clip_and_sorting_layer sublayerTransform].m42);
}
// Disable clipping and re-commit.
{
properties.is_clipped = false;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
// Validate the clip and sorting context layer.
EXPECT_FALSE([clip_and_sorting_layer masksToBounds]);
EXPECT_EQ(gfx::Rect(), gfx::Rect([clip_and_sorting_layer bounds]));
EXPECT_EQ(gfx::Point(), gfx::Point([clip_and_sorting_layer position]));
EXPECT_EQ(0.0, [clip_and_sorting_layer sublayerTransform].m41);
EXPECT_EQ(0.0, [clip_and_sorting_layer sublayerTransform].m42);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
}
// Change the transform and re-commit.
{
properties.transform.Translate(5, 5);
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
// Validate the transform layer.
EXPECT_EQ(properties.transform.rc(3, 0),
[transform_layer sublayerTransform].m41);
EXPECT_EQ(properties.transform.rc(3, 1),
[transform_layer sublayerTransform].m42);
}
// Change the edge antialiasing mask and commit.
{
properties.edge_aa_mask = ui::CALayerEdge::kLayerEdgeTop;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer. Note that top and bottom edges flip.
EXPECT_EQ(kCALayerBottomEdge, [content_layer edgeAntialiasingMask]);
}
// Change the contents and commit.
{
properties.io_surface = gfx::ScopedIOSurface();
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer. Note that edge anti-aliasing does not flip
// for solid colors.
if (allow_solid_color_layers) {
EXPECT_EQ(nil, [content_layer contents]);
EXPECT_EQ(kCALayerTopEdge, [content_layer edgeAntialiasingMask]);
} else {
EXPECT_EQ(ca_layer_tree->ContentsForSolidColorForTesting(
properties.background_color),
[content_layer contents]);
EXPECT_EQ(kCALayerBottomEdge, [content_layer edgeAntialiasingMask]);
}
}
// Change the rect size.
{
properties.rect = gfx::Rect(properties.rect.origin(), gfx::Size(32, 16));
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer.
EXPECT_EQ(properties.rect.origin(), gfx::Point([content_layer position]));
EXPECT_EQ(gfx::Rect(properties.rect.size()),
gfx::Rect([content_layer bounds]));
}
// Change the rect position.
{
properties.rect = gfx::Rect(gfx::Point(16, 4), properties.rect.size());
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer.
EXPECT_EQ(properties.rect.origin(), gfx::Point([content_layer position]));
EXPECT_EQ(gfx::Rect(properties.rect.size()),
gfx::Rect([content_layer bounds]));
}
// Change the opacity.
{
properties.opacity = 1.0f;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer.
EXPECT_EQ(properties.opacity, [content_layer opacity]);
}
// Change the filter.
{
properties.filter = GL_NEAREST;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer.
EXPECT_NSEQ(kCAFilterNearest, [content_layer minificationFilter]);
EXPECT_NSEQ(kCAFilterNearest, [content_layer magnificationFilter]);
}
// Add the clipping and IOSurface contents back.
{
properties.is_clipped = true;
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
// Validate the content layer.
EXPECT_EQ((__bridge id)properties.io_surface.get(),
[content_layer contents]);
EXPECT_EQ(kCALayerBottomEdge, [content_layer edgeAntialiasingMask]);
}
// Change the scale factor. This should result in a new tree being created.
{
properties.scale_factor = 2.0f;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_NE(root_layer, [superlayer_ sublayers][0]);
root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_NE(clip_and_sorting_layer, [root_layer sublayers][0]);
clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_NE(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
// Under a 2.0 scale factor, the corner-radius should be halved.
EXPECT_EQ(properties.rounded_corner_bounds.GetSimpleRadius() / 2.0f,
[rounded_rect_layer cornerRadius]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_NE(transform_layer, [clip_and_sorting_layer sublayers][0]);
transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
EXPECT_NE(content_layer, [transform_layer sublayers][0]);
content_layer = [transform_layer sublayers][0];
// Validate the clip and sorting context layer.
EXPECT_TRUE([clip_and_sorting_layer masksToBounds]);
EXPECT_EQ(
gfx::ToFlooredRectDeprecated(gfx::ConvertRectToDips(
gfx::Rect(properties.clip_rect.size()), properties.scale_factor)),
gfx::Rect([clip_and_sorting_layer bounds]));
EXPECT_EQ(gfx::ToFlooredPoint(gfx::ConvertPointToDips(
properties.clip_rect.origin(), properties.scale_factor)),
gfx::Point([clip_and_sorting_layer position]));
EXPECT_EQ(-properties.clip_rect.origin().x() / properties.scale_factor,
[clip_and_sorting_layer sublayerTransform].m41);
EXPECT_EQ(-properties.clip_rect.origin().y() / properties.scale_factor,
[clip_and_sorting_layer sublayerTransform].m42);
// Validate the transform layer.
EXPECT_EQ(properties.transform.rc(3, 0) / properties.scale_factor,
[transform_layer sublayerTransform].m41);
EXPECT_EQ(properties.transform.rc(3, 1) / properties.scale_factor,
[transform_layer sublayerTransform].m42);
// Validate the content layer.
EXPECT_EQ((__bridge id)properties.io_surface.get(),
[content_layer contents]);
EXPECT_EQ(properties.contents_rect,
gfx::RectF([content_layer contentsRect]));
EXPECT_EQ(gfx::ToFlooredPoint(gfx::ConvertPointToDips(
properties.rect.origin(), properties.scale_factor)),
gfx::Point([content_layer position]));
EXPECT_EQ(
gfx::ToFlooredRectDeprecated(gfx::ConvertRectToDips(
gfx::Rect(properties.rect.size()), properties.scale_factor)),
gfx::Rect([content_layer bounds]));
EXPECT_EQ(kCALayerBottomEdge, [content_layer edgeAntialiasingMask]);
EXPECT_EQ(properties.opacity, [content_layer opacity]);
EXPECT_EQ(properties.scale_factor, [content_layer contentsScale]);
}
// Remove the rounded corners. This should result in the rounded corners
// being removed on that layer.
{
properties.rounded_corner_bounds = gfx::RRectF();
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(0, [rounded_rect_layer cornerRadius]);
EXPECT_FALSE([rounded_rect_layer masksToBounds]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
}
{
// A no-op update should not invalidate any of the layers.
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
}
// Re-add rounded corners.
{
properties.rounded_corner_bounds = gfx::RRectF(1, 2, 3, 4, 5);
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
EXPECT_EQ(root_layer, [superlayer_ sublayers][0]);
EXPECT_EQ(1u, [[root_layer sublayers] count]);
EXPECT_EQ(clip_and_sorting_layer, [root_layer sublayers][0]);
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
EXPECT_EQ(rounded_rect_layer, [clip_and_sorting_layer sublayers][0]);
// Under a 2.0 scale factor, the corer-radius should be halved.
EXPECT_EQ(properties.rounded_corner_bounds.GetSimpleRadius() / 2.0f,
[rounded_rect_layer cornerRadius]);
EXPECT_TRUE([rounded_rect_layer masksToBounds]);
EXPECT_EQ(transform_layer, [rounded_rect_layer sublayers][0]);
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
EXPECT_EQ(content_layer, [transform_layer sublayers][0]);
}
}
};
TEST_F(CALayerTreePropertyUpdatesTest, AllowSolidColors) {
RunTest(true);
}
TEST_F(CALayerTreePropertyUpdatesTest, DisallowSolidColors) {
RunTest(false);
}
// Verify that sorting context zero is split at non-flat transforms.
TEST_F(CALayerTreeTest, SplitSortingContextZero) {
CALayerProperties properties;
properties.is_clipped = false;
properties.clip_rect = gfx::Rect();
properties.rect = gfx::Rect(0, 0, 256, 256);
// We'll use the IOSurface contents to identify the content layers.
gfx::ScopedIOSurface io_surfaces[5];
for (size_t i = 0; i < 5; ++i) {
io_surfaces[i] =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
}
// Have 5 transforms:
// * 2 flat but different (1 sorting context layer, 2 transform layers)
// * 1 non-flat (new sorting context layer)
// * 2 flat and the same (new sorting context layer, 1 transform layer)
gfx::Transform transforms[5];
transforms[0].Translate(10, 10);
transforms[1].RotateAboutZAxis(45.0f);
transforms[2].RotateAboutYAxis(45.0f);
transforms[3].Translate(10, 10);
transforms[4].Translate(10, 10);
// Schedule and commit the layers.
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree(
new ui::CARendererLayerTree(true, true));
for (size_t i = 0; i < 5; ++i) {
properties.io_surface = io_surfaces[i];
properties.transform = transforms[i];
bool result = ScheduleCALayer(ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
}
ca_layer_tree->CommitScheduledCALayers(
superlayer_, nullptr, properties.rect.size(), properties.scale_factor);
// Validate the root layer.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
// Validate that we have 3 sorting context layers.
EXPECT_EQ(3u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer_0 = [root_layer sublayers][0];
CALayer* clip_and_sorting_layer_1 = [root_layer sublayers][1];
CALayer* clip_and_sorting_layer_2 = [root_layer sublayers][2];
CALayer* rounded_rect_layer_0 = [clip_and_sorting_layer_0 sublayers][0];
CALayer* rounded_rect_layer_1 = [clip_and_sorting_layer_1 sublayers][0];
CALayer* rounded_rect_layer_2 = [clip_and_sorting_layer_2 sublayers][0];
// Validate that the first sorting context has 2 transform layers each with
// one content layer.
EXPECT_EQ(2u, [[rounded_rect_layer_0 sublayers] count]);
CALayer* transform_layer_0_0 = [rounded_rect_layer_0 sublayers][0];
CALayer* transform_layer_0_1 = [rounded_rect_layer_0 sublayers][1];
EXPECT_EQ(1u, [[transform_layer_0_0 sublayers] count]);
CALayer* content_layer_0 = [transform_layer_0_0 sublayers][0];
EXPECT_EQ(1u, [[transform_layer_0_1 sublayers] count]);
CALayer* content_layer_1 = [transform_layer_0_1 sublayers][0];
// Validate that the second sorting context has 1 transform layer with one
// content layer.
EXPECT_EQ(1u, [[rounded_rect_layer_1 sublayers] count]);
CALayer* transform_layer_1_0 = [rounded_rect_layer_1 sublayers][0];
EXPECT_EQ(1u, [[transform_layer_1_0 sublayers] count]);
CALayer* content_layer_2 = [transform_layer_1_0 sublayers][0];
// Validate that the third sorting context has 1 transform layer with two
// content layers.
EXPECT_EQ(1u, [[rounded_rect_layer_2 sublayers] count]);
CALayer* transform_layer_2_0 = [rounded_rect_layer_2 sublayers][0];
EXPECT_EQ(2u, [[transform_layer_2_0 sublayers] count]);
CALayer* content_layer_3 = [transform_layer_2_0 sublayers][0];
CALayer* content_layer_4 = [transform_layer_2_0 sublayers][1];
// Validate that the layers come out in order.
EXPECT_EQ((__bridge id)io_surfaces[0].get(), [content_layer_0 contents]);
EXPECT_EQ((__bridge id)io_surfaces[1].get(), [content_layer_1 contents]);
EXPECT_EQ((__bridge id)io_surfaces[2].get(), [content_layer_2 contents]);
EXPECT_EQ((__bridge id)io_surfaces[3].get(), [content_layer_3 contents]);
EXPECT_EQ((__bridge id)io_surfaces[4].get(), [content_layer_4 contents]);
}
// Verify that sorting contexts are allocated appropriately.
TEST_F(CALayerTreeTest, SortingContexts) {
CALayerProperties properties;
properties.is_clipped = false;
properties.clip_rect = gfx::Rect();
properties.rect = gfx::Rect(0, 0, 256, 256);
// We'll use the IOSurface contents to identify the content layers.
gfx::ScopedIOSurface io_surfaces[3];
for (size_t i = 0; i < 3; ++i) {
io_surfaces[i] =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
}
int sorting_context_ids[3] = {3, -1, 0};
// Schedule and commit the layers.
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree(
new ui::CARendererLayerTree(true, true));
for (size_t i = 0; i < 3; ++i) {
properties.sorting_context_id = sorting_context_ids[i];
properties.io_surface = io_surfaces[i];
bool result = ScheduleCALayer(ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
}
ca_layer_tree->CommitScheduledCALayers(
superlayer_, nullptr, properties.rect.size(), properties.scale_factor);
// Validate the root layer.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
// Validate that we have 3 sorting context layers.
EXPECT_EQ(3u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer_0 = [root_layer sublayers][0];
CALayer* clip_and_sorting_layer_1 = [root_layer sublayers][1];
CALayer* clip_and_sorting_layer_2 = [root_layer sublayers][2];
CALayer* rounded_rect_layer_0 = [clip_and_sorting_layer_0 sublayers][0];
CALayer* rounded_rect_layer_1 = [clip_and_sorting_layer_1 sublayers][0];
CALayer* rounded_rect_layer_2 = [clip_and_sorting_layer_2 sublayers][0];
// Validate that each sorting context has 1 transform layer.
EXPECT_EQ(1u, [[rounded_rect_layer_0 sublayers] count]);
CALayer* transform_layer_0 = [rounded_rect_layer_0 sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer_1 sublayers] count]);
CALayer* transform_layer_1 = [rounded_rect_layer_1 sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer_2 sublayers] count]);
CALayer* transform_layer_2 = [rounded_rect_layer_2 sublayers][0];
// Validate that each transform has 1 content layer.
EXPECT_EQ(1u, [[transform_layer_0 sublayers] count]);
CALayer* content_layer_0 = [transform_layer_0 sublayers][0];
EXPECT_EQ(1u, [[transform_layer_1 sublayers] count]);
CALayer* content_layer_1 = [transform_layer_1 sublayers][0];
EXPECT_EQ(1u, [[transform_layer_2 sublayers] count]);
CALayer* content_layer_2 = [transform_layer_2 sublayers][0];
// Validate that the layers come out in order.
EXPECT_EQ((__bridge id)io_surfaces[0].get(), [content_layer_0 contents]);
EXPECT_EQ((__bridge id)io_surfaces[1].get(), [content_layer_1 contents]);
EXPECT_EQ((__bridge id)io_surfaces[2].get(), [content_layer_2 contents]);
}
// Verify that sorting contexts must all have the same clipping properties.
TEST_F(CALayerTreeTest, SortingContextMustHaveConsistentClip) {
CALayerProperties properties;
// Vary the clipping parameters within sorting contexts.
bool is_clippeds[3] = { true, true, false};
gfx::Rect clip_rects[3] = {
gfx::Rect(0, 0, 16, 16),
gfx::Rect(4, 8, 16, 32),
gfx::Rect(0, 0, 16, 16)
};
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree(
new ui::CARendererLayerTree(true, true));
// First send the various clip parameters to sorting context zero. This is
// legitimate.
for (size_t i = 0; i < 3; ++i) {
properties.is_clipped = is_clippeds[i];
properties.clip_rect = clip_rects[i];
bool result = ScheduleCALayer(ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
}
// Next send the various clip parameters to a non-zero sorting context. This
// will fail when we try to change the clip within the sorting context.
for (size_t i = 0; i < 3; ++i) {
properties.sorting_context_id = 3;
properties.is_clipped = is_clippeds[i];
properties.clip_rect = clip_rects[i];
bool result = ScheduleCALayer(ca_layer_tree.get(), &properties);
if (i == 0)
EXPECT_TRUE(result);
else
EXPECT_FALSE(result);
}
// Try once more with the original clip and verify it works.
{
properties.is_clipped = is_clippeds[0];
properties.clip_rect = clip_rects[0];
bool result = ScheduleCALayer(ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
}
}
// Test updating each layer's properties.
TEST_F(CALayerTreeTest, AVLayer) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({ui::kFullscreenLowPowerBackdropMac}, {});
CALayerProperties properties;
properties.io_surface =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree;
CALayer* content_layer_old = nil;
CALayer* content_layer_new = nil;
// Validate the initial values.
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_FALSE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
}
content_layer_old = content_layer_new;
// Pass a YUV 420 frame. This will become an AVSampleBufferDisplayLayer
// because it is in fullscreen low power mode.
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 256), gfx::BufferFormat::YUV_420_BIPLANAR);
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_TRUE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_NE(content_layer_new, content_layer_old);
}
content_layer_old = content_layer_new;
// Pass a similar frame. Nothing should change.
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 128), gfx::BufferFormat::YUV_420_BIPLANAR);
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_TRUE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_EQ(content_layer_new, content_layer_old);
}
content_layer_old = content_layer_new;
// Break fullscreen low power mode by changing opacity. This should cause
// us to drop out of using AVSampleBufferDisplayLayer.
properties.opacity = 0.9;
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_FALSE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_NE(content_layer_new, content_layer_old);
}
content_layer_old = content_layer_new;
// Now try a P010 frame. Because this may be HDR, we should jump back to
// having an AVSampleBufferDisplayLayer.
properties.io_surface =
gfx::CreateIOSurface(gfx::Size(128, 256), gfx::BufferFormat::P010);
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_TRUE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_NE(content_layer_new, content_layer_old);
}
content_layer_old = content_layer_new;
// Go back to testing AVSampleBufferLayer and fullscreen low power.
properties.opacity = 1.0;
// Pass a frame with a CVPixelBuffer which, when scaled down, will have a
// fractional dimension.
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(513, 512), gfx::BufferFormat::YUV_420_BIPLANAR);
properties.cv_pixel_buffer = CreateCVPixelBuffer(properties.io_surface);
properties.color_space = gfx::ColorSpace::CreateREC709();
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
// Validate that the layer's size is adjusted to include the fractional
// width, which works around a macOS bug (https://crbug.com/792632).
CGSize layer_size = content_layer_new.bounds.size;
EXPECT_EQ(256.5, layer_size.width);
EXPECT_EQ(256, layer_size.height);
}
content_layer_old = content_layer_new;
// Pass a frame that is clipped.
properties.contents_rect = gfx::RectF(0, 0, 1, 0.9);
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 256), gfx::BufferFormat::YUV_420_BIPLANAR);
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
content_layer_new = GetOnlyContentLayer();
EXPECT_FALSE([content_layer_new
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_NE(content_layer_new, content_layer_old);
}
content_layer_old = content_layer_new;
}
// Ensure that blocklisting AVSampleBufferDisplayLayer works.
TEST_F(CALayerTreeTest, AVLayerBlocklist) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({ui::kFullscreenLowPowerBackdropMac}, {});
CALayerProperties properties;
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 256), gfx::BufferFormat::YUV_420_BIPLANAR);
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree;
CALayer* root_layer = nil;
CALayer* clip_and_sorting_layer = nil;
CALayer* rounded_rect_layer = nil;
CALayer* transform_layer = nil;
CALayer* content_layer1 = nil;
CALayer* content_layer2 = nil;
{
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
content_layer1 = [transform_layer sublayers][0];
// Validate the content layer.
EXPECT_TRUE([content_layer1
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
}
{
properties.allow_av_layers = false;
UpdateCALayerTree(ca_layer_tree, &properties, superlayer_);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
content_layer2 = [transform_layer sublayers][0];
// Validate the content layer.
EXPECT_FALSE([content_layer2
isKindOfClass:NSClassFromString(@"AVSampleBufferDisplayLayer")]);
EXPECT_NE(content_layer1, content_layer2);
}
}
// Test fullscreen low power detection.
TEST_F(CALayerTreeTest, FullscreenLowPower) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({ui::kFullscreenLowPowerBackdropMac}, {});
CALayerProperties properties;
properties.io_surface = gfx::CreateIOSurface(
gfx::Size(256, 256), gfx::BufferFormat::YUV_420_BIPLANAR);
properties.cv_pixel_buffer = CreateCVPixelBuffer(properties.io_surface);
properties.color_space = gfx::ColorSpace::CreateREC709();
properties.is_clipped = false;
CALayerProperties properties_black;
properties_black.is_clipped = false;
properties_black.background_color = SkColors::kBlack;
CALayerProperties properties_white;
properties_white.is_clipped = false;
properties_white.background_color = SkColors::kWhite;
std::unique_ptr<ui::CARendererLayerTree> ca_layer_tree;
// Test a configuration with no background.
{
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(true, true));
bool result = ScheduleCALayer(new_ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
new_ca_layer_tree->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_tree), properties.rect.size(),
properties.scale_factor);
std::swap(new_ca_layer_tree, ca_layer_tree);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
CALayer* transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(1u, [[transform_layer sublayers] count]);
// Validate the content layer and fullscreen low power mode.
EXPECT_FALSE(CGRectEqualToRect([root_layer frame], CGRectZero));
EXPECT_NE([root_layer backgroundColor], nil);
}
// Test a configuration with a black background.
{
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(true, true));
bool result = ScheduleCALayer(new_ca_layer_tree.get(), &properties_black);
EXPECT_TRUE(result);
result = ScheduleCALayer(new_ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
new_ca_layer_tree->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_tree), properties.rect.size(),
properties.scale_factor);
std::swap(new_ca_layer_tree, ca_layer_tree);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
CALayer* transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(2u, [[transform_layer sublayers] count]);
// Validate the content layer and fullscreen low power mode.
EXPECT_FALSE(CGRectEqualToRect([root_layer frame], CGRectZero));
EXPECT_NE([root_layer backgroundColor], nil);
}
// Test a configuration with a white background. It will fail.
{
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(true, true));
bool result = ScheduleCALayer(new_ca_layer_tree.get(), &properties_white);
EXPECT_TRUE(result);
result = ScheduleCALayer(new_ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
new_ca_layer_tree->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_tree), properties.rect.size(),
properties.scale_factor);
std::swap(new_ca_layer_tree, ca_layer_tree);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
CALayer* transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(2u, [[transform_layer sublayers] count]);
// Validate the content layer and fullscreen low power mode.
EXPECT_TRUE(CGRectEqualToRect([root_layer frame], CGRectZero));
EXPECT_EQ([root_layer backgroundColor], nil);
}
// Test a configuration with a black foreground. It too will fail.
{
std::unique_ptr<ui::CARendererLayerTree> new_ca_layer_tree(
new ui::CARendererLayerTree(true, true));
bool result = ScheduleCALayer(new_ca_layer_tree.get(), &properties);
EXPECT_TRUE(result);
result = ScheduleCALayer(new_ca_layer_tree.get(), &properties_black);
EXPECT_TRUE(result);
new_ca_layer_tree->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_tree), properties.rect.size(),
properties.scale_factor);
std::swap(new_ca_layer_tree, ca_layer_tree);
// Validate the tree structure.
EXPECT_EQ(1u, [[superlayer_ sublayers] count]);
CALayer* root_layer = [superlayer_ sublayers][0];
EXPECT_EQ(1u, [[root_layer sublayers] count]);
CALayer* clip_and_sorting_layer = [root_layer sublayers][0];
EXPECT_EQ(1u, [[clip_and_sorting_layer sublayers] count]);
CALayer* rounded_rect_layer = [clip_and_sorting_layer sublayers][0];
EXPECT_EQ(1u, [[rounded_rect_layer sublayers] count]);
CALayer* transform_layer = [rounded_rect_layer sublayers][0];
EXPECT_EQ(2u, [[transform_layer sublayers] count]);
// Validate the content layer and fullscreen low power mode.
EXPECT_TRUE(CGRectEqualToRect([root_layer frame], CGRectZero));
EXPECT_EQ([root_layer backgroundColor], nil);
}
}
// Verify that HDR is triggered appropriately.
TEST_F(CALayerTreeTest, HDRTrigger) {
std::unique_ptr<ui::CARendererLayerTree> ca_layer_trees[4]{
std::make_unique<ui::CARendererLayerTree>(true, true),
std::make_unique<ui::CARendererLayerTree>(true, true),
std::make_unique<ui::CARendererLayerTree>(true, true),
std::make_unique<ui::CARendererLayerTree>(true, true),
};
CALayerProperties properties;
properties.is_clipped = false;
properties.clip_rect = gfx::Rect();
properties.rect = gfx::Rect(0, 0, 256, 256);
bool result = false;
// We only copy images that have both high-bit-depth and an HDR color space.
auto sdr_image =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
auto tricky_sdr_image =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::BGRA_8888);
auto hdr_image =
gfx::CreateIOSurface(gfx::Size(256, 256), gfx::BufferFormat::RGBA_F16);
// Schedule and commit the HDR layer.
properties.io_surface = hdr_image;
properties.color_space = gfx::ColorSpace::CreateExtendedSRGB();
result = ScheduleCALayer(ca_layer_trees[0].get(), &properties);
EXPECT_TRUE(result);
ca_layer_trees[0]->CommitScheduledCALayers(
superlayer_, nullptr, properties.rect.size(), properties.scale_factor);
// Validate that the root layer has is triggering HDR.
CALayer* content_layer = GetOnlyContentLayer();
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
EXPECT_TRUE([content_layer wantsExtendedDynamicRangeContent]);
#pragma clang diagnostic pop
// Commit the SDR layer.
properties.io_surface = sdr_image;
properties.color_space = gfx::ColorSpace::CreateSRGB();
result = ScheduleCALayer(ca_layer_trees[1].get(), &properties);
EXPECT_TRUE(result);
ca_layer_trees[1]->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_trees[0]), properties.rect.size(),
properties.scale_factor);
// Validate that HDR is off. The previous content layer should have been
// un-parented.
EXPECT_EQ([content_layer superlayer], nil);
content_layer = GetOnlyContentLayer();
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
EXPECT_FALSE([content_layer wantsExtendedDynamicRangeContent]);
#pragma clang diagnostic pop
// Commit the tricky SDR layer.
properties.io_surface = tricky_sdr_image;
properties.color_space = gfx::ColorSpace::CreateExtendedSRGB();
result = ScheduleCALayer(ca_layer_trees[2].get(), &properties);
EXPECT_TRUE(result);
ca_layer_trees[2]->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_trees[1]), properties.rect.size(),
properties.scale_factor);
// Validate that HDR is still off, and that the content layer hasn't changed.
EXPECT_EQ(content_layer, GetOnlyContentLayer());
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
EXPECT_FALSE([content_layer wantsExtendedDynamicRangeContent]);
#pragma clang diagnostic pop
// Commit the HDR layer.
properties.io_surface = hdr_image;
properties.color_space = gfx::ColorSpace::CreateExtendedSRGB();
result = ScheduleCALayer(ca_layer_trees[3].get(), &properties);
EXPECT_TRUE(result);
ca_layer_trees[3]->CommitScheduledCALayers(
superlayer_, std::move(ca_layer_trees[2]), properties.rect.size(),
properties.scale_factor);
// Validate that HDR is back on. The previous content layer should have
// been un-parented.
EXPECT_EQ([content_layer superlayer], nil);
content_layer = GetOnlyContentLayer();
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
EXPECT_TRUE([content_layer wantsExtendedDynamicRangeContent]);
#pragma clang diagnostic pop
}
} // namespace gpu
|