xref: /aosp_15_r20/external/skia/tests/WindowRectanglesTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkRect.h"
9 #include "include/core/SkTypes.h"
10 #include "src/base/SkRandom.h"
11 #include "src/core/SkRectPriv.h"
12 #include "src/gpu/ganesh/GrWindowRectangles.h"
13 #include "tests/Test.h"
14 
15 #include <cstring>
16 
next_irect(SkRandom & r)17 static SkIRect next_irect(SkRandom& r) {
18     return {r.nextS(), r.nextS(), r.nextS(), r.nextS()};
19 }
20 
DEF_TEST(WindowRectangles,reporter)21 DEF_TEST(WindowRectangles, reporter) {
22     SkRandom r;
23 
24     SkIRect windowData[GrWindowRectangles::kMaxWindows];
25     for (int i = 0; i < GrWindowRectangles::kMaxWindows; ++i) {
26         windowData[i] = next_irect(r);
27     }
28 
29     GrWindowRectangles wr;
30     for (int i = 0; i < GrWindowRectangles::kMaxWindows - 1; ++i) {
31         REPORTER_ASSERT(reporter, wr.count() == i);
32         REPORTER_ASSERT(reporter, !memcmp(wr.data(), windowData, i * sizeof(SkIRect)));
33 
34         GrWindowRectangles wr2(wr);
35         REPORTER_ASSERT(reporter, wr2 == wr);
36         REPORTER_ASSERT(reporter, wr2.count() == wr.count());
37         REPORTER_ASSERT(reporter, !memcmp(wr2.data(), wr.data(), i * sizeof(SkIRect)));
38 
39         wr.addWindow(windowData[i]);
40     }
41 
42     SkASSERT(wr.count() == GrWindowRectangles::kMaxWindows - 1);
43     {
44         GrWindowRectangles A(wr), B(wr);
45         REPORTER_ASSERT(reporter, B == A);
46         REPORTER_ASSERT(reporter, B.data() == A.data()); // Should use copy-on-write.
47 
48         A.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
49         REPORTER_ASSERT(reporter, B.data() != A.data());
50         REPORTER_ASSERT(reporter, B != A);
51 
52         B.addWindow(SkRectPriv::MakeILarge());
53         REPORTER_ASSERT(reporter, B != A);
54 
55         for (int i = 0; i < GrWindowRectangles::kMaxWindows - 1; i++) {
56             REPORTER_ASSERT(reporter, A.data()[i] == windowData[i]);
57             REPORTER_ASSERT(reporter, B.data()[i] == windowData[i]);
58         }
59         REPORTER_ASSERT(reporter, A.data()[GrWindowRectangles::kMaxWindows - 1]
60                              == windowData[GrWindowRectangles::kMaxWindows - 1]);
61         REPORTER_ASSERT(reporter, B.data()[GrWindowRectangles::kMaxWindows - 1]
62                              == SkRectPriv::MakeILarge());
63     }
64     {
65         GrWindowRectangles A(wr), B(wr);
66         REPORTER_ASSERT(reporter, B == A);
67         REPORTER_ASSERT(reporter, B.data() == A.data()); // Should use copy-on-write.
68 
69         A.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
70         B.addWindow(windowData[GrWindowRectangles::kMaxWindows - 1]);
71         REPORTER_ASSERT(reporter, B == A);
72         REPORTER_ASSERT(reporter, B.data() != A.data());
73 
74         for (int i = 0; i < GrWindowRectangles::kMaxWindows; i++) {
75             REPORTER_ASSERT(reporter, B.data()[i] ==   A.data()[i]);
76             REPORTER_ASSERT(reporter, A.data()[i] == windowData[i]);
77         }
78     }
79 }
80