1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <binder/Binder.h>
20 #include <binder/Parcel.h>
21 
22 #include <gui/LayerState.h>
23 
24 namespace android {
25 using gui::DisplayCaptureArgs;
26 using gui::LayerCaptureArgs;
27 using gui::ScreenCaptureResults;
28 
29 namespace test {
30 
TEST(LayerStateTest,ParcellingDisplayCaptureArgs)31 TEST(LayerStateTest, ParcellingDisplayCaptureArgs) {
32     DisplayCaptureArgs args;
33     args.pixelFormat = ui::PixelFormat::RGB_565;
34     args.sourceCrop = Rect(0, 0, 500, 200);
35     args.frameScaleX = 2;
36     args.frameScaleY = 4;
37     args.captureSecureLayers = true;
38     args.displayToken = sp<BBinder>::make();
39     args.width = 10;
40     args.height = 20;
41     args.grayscale = true;
42 
43     Parcel p;
44     args.writeToParcel(&p);
45     p.setDataPosition(0);
46 
47     DisplayCaptureArgs args2;
48     args2.readFromParcel(&p);
49 
50     ASSERT_EQ(args.pixelFormat, args2.pixelFormat);
51     ASSERT_EQ(args.sourceCrop, args2.sourceCrop);
52     ASSERT_EQ(args.frameScaleX, args2.frameScaleX);
53     ASSERT_EQ(args.frameScaleY, args2.frameScaleY);
54     ASSERT_EQ(args.captureSecureLayers, args2.captureSecureLayers);
55     ASSERT_EQ(args.displayToken, args2.displayToken);
56     ASSERT_EQ(args.width, args2.width);
57     ASSERT_EQ(args.height, args2.height);
58     ASSERT_EQ(args.grayscale, args2.grayscale);
59 }
60 
TEST(LayerStateTest,ParcellingLayerCaptureArgs)61 TEST(LayerStateTest, ParcellingLayerCaptureArgs) {
62     LayerCaptureArgs args;
63     args.pixelFormat = ui::PixelFormat::RGB_565;
64     args.sourceCrop = Rect(0, 0, 500, 200);
65     args.frameScaleX = 2;
66     args.frameScaleY = 4;
67     args.captureSecureLayers = true;
68     args.layerHandle = sp<BBinder>::make();
69     args.excludeHandles = {sp<BBinder>::make(), sp<BBinder>::make()};
70     args.childrenOnly = false;
71     args.grayscale = true;
72 
73     Parcel p;
74     args.writeToParcel(&p);
75     p.setDataPosition(0);
76 
77     LayerCaptureArgs args2;
78     args2.readFromParcel(&p);
79 
80     ASSERT_EQ(args.pixelFormat, args2.pixelFormat);
81     ASSERT_EQ(args.sourceCrop, args2.sourceCrop);
82     ASSERT_EQ(args.frameScaleX, args2.frameScaleX);
83     ASSERT_EQ(args.frameScaleY, args2.frameScaleY);
84     ASSERT_EQ(args.captureSecureLayers, args2.captureSecureLayers);
85     ASSERT_EQ(args.layerHandle, args2.layerHandle);
86     ASSERT_EQ(args.excludeHandles, args2.excludeHandles);
87     ASSERT_EQ(args.childrenOnly, args2.childrenOnly);
88     ASSERT_EQ(args.grayscale, args2.grayscale);
89 }
90 
TEST(LayerStateTest,ParcellingScreenCaptureResultsWithFence)91 TEST(LayerStateTest, ParcellingScreenCaptureResultsWithFence) {
92     ScreenCaptureResults results;
93     results.buffer = sp<GraphicBuffer>::make(100u, 200u, PIXEL_FORMAT_RGBA_8888, 1u, 0u);
94     results.fenceResult = sp<Fence>::make(dup(fileno(tmpfile())));
95     results.capturedSecureLayers = true;
96     results.capturedDataspace = ui::Dataspace::DISPLAY_P3;
97 
98     Parcel p;
99     results.writeToParcel(&p);
100     p.setDataPosition(0);
101 
102     ScreenCaptureResults results2;
103     results2.readFromParcel(&p);
104 
105     // GraphicBuffer object is reallocated so compare the data in the graphic buffer
106     // rather than the object itself
107     ASSERT_EQ(results.buffer->getWidth(), results2.buffer->getWidth());
108     ASSERT_EQ(results.buffer->getHeight(), results2.buffer->getHeight());
109     ASSERT_EQ(results.buffer->getPixelFormat(), results2.buffer->getPixelFormat());
110     ASSERT_TRUE(results.fenceResult.ok());
111     ASSERT_TRUE(results2.fenceResult.ok());
112     ASSERT_EQ(results.fenceResult.value()->isValid(), results2.fenceResult.value()->isValid());
113     ASSERT_EQ(results.capturedSecureLayers, results2.capturedSecureLayers);
114     ASSERT_EQ(results.capturedDataspace, results2.capturedDataspace);
115 }
116 
TEST(LayerStateTest,ParcellingScreenCaptureResultsWithNoFenceOrError)117 TEST(LayerStateTest, ParcellingScreenCaptureResultsWithNoFenceOrError) {
118     ScreenCaptureResults results;
119 
120     Parcel p;
121     results.writeToParcel(&p);
122     p.setDataPosition(0);
123 
124     ScreenCaptureResults results2;
125     results2.readFromParcel(&p);
126 
127     ASSERT_TRUE(results2.fenceResult.ok());
128     ASSERT_EQ(results2.fenceResult.value(), Fence::NO_FENCE);
129 }
130 
TEST(LayerStateTest,ParcellingScreenCaptureResultsWithFenceError)131 TEST(LayerStateTest, ParcellingScreenCaptureResultsWithFenceError) {
132     ScreenCaptureResults results;
133     results.fenceResult = base::unexpected(BAD_VALUE);
134 
135     Parcel p;
136     results.writeToParcel(&p);
137     p.setDataPosition(0);
138 
139     ScreenCaptureResults results2;
140     results2.readFromParcel(&p);
141 
142     ASSERT_FALSE(results.fenceResult.ok());
143     ASSERT_FALSE(results2.fenceResult.ok());
144     ASSERT_EQ(results.fenceResult.error(), results2.fenceResult.error());
145 }
146 
147 } // namespace test
148 } // namespace android
149