• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 #define LOG_TAG "LayerState"
18 
19 #include <cinttypes>
20 
21 #include <android/gui/ISurfaceComposerClient.h>
22 #include <android/native_window.h>
23 #include <binder/Parcel.h>
24 #include <com_android_graphics_libgui_flags.h>
25 #include <gui/FrameRateUtils.h>
26 #include <gui/IGraphicBufferProducer.h>
27 #include <gui/LayerState.h>
28 #include <gui/SurfaceControl.h>
29 #include <private/gui/ParcelUtils.h>
30 #include <system/window.h>
31 #include <utils/Errors.h>
32 
33 #define CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD)          \
34     {                                                               \
35         if ((OTHER.what & CHANGE_FLAG) && (FIELD != OTHER.FIELD)) { \
36             DIFF_RESULT |= CHANGE_FLAG;                             \
37         }                                                           \
38     }
39 
40 #define CHECK_DIFF2(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD1, FIELD2) \
41     {                                                                \
42         CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD1)          \
43         CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD2)          \
44     }
45 
46 #define CHECK_DIFF3(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD1, FIELD2, FIELD3) \
47     {                                                                        \
48         CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD1)                  \
49         CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD2)                  \
50         CHECK_DIFF(DIFF_RESULT, CHANGE_FLAG, OTHER, FIELD3)                  \
51     }
52 
53 namespace android {
54 
55 using gui::FocusRequest;
56 using gui::WindowInfoHandle;
57 
58 namespace {
isSameWindowHandle(const sp<WindowInfoHandle> & lhs,const sp<WindowInfoHandle> & rhs)59 bool isSameWindowHandle(const sp<WindowInfoHandle>& lhs, const sp<WindowInfoHandle>& rhs) {
60     if (lhs == rhs) {
61         return true;
62     }
63 
64     if (!lhs || !rhs) {
65         return false;
66     }
67 
68     return *lhs->getInfo() == *rhs->getInfo();
69 };
70 
isSameSurfaceControl(const sp<SurfaceControl> & lhs,const sp<SurfaceControl> & rhs)71 bool isSameSurfaceControl(const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) {
72     if (lhs == rhs) {
73         return true;
74     }
75 
76     return SurfaceControl::isSameSurface(lhs, rhs);
77 };
78 } // namespace
79 
layer_state_t()80 layer_state_t::layer_state_t()
81       : surface(nullptr),
82         layerId(-1),
83         what(0),
84         x(0),
85         y(0),
86         z(0),
87         flags(0),
88         mask(0),
89         reserved(0),
90         cornerRadius(0.0f),
91         clientDrawnCornerRadius(0.0f),
92         backgroundBlurRadius(0),
93         color(0),
94         bufferTransform(0),
95         transformToDisplayInverse(false),
96         crop({0, 0, -1, -1}),
97         dataspace(ui::Dataspace::UNKNOWN),
98         api(-1),
99         colorTransform(mat4()),
100         bgColor(0),
101         bgColorDataspace(ui::Dataspace::UNKNOWN),
102         colorSpaceAgnostic(false),
103         shadowRadius(0.0f),
104         frameRateSelectionPriority(-1),
105         frameRate(0.0f),
106         frameRateCompatibility(ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT),
107         changeFrameRateStrategy(ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS),
108         defaultFrameRateCompatibility(ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT),
109         frameRateCategory(ANATIVEWINDOW_FRAME_RATE_CATEGORY_DEFAULT),
110         frameRateCategorySmoothSwitchOnly(false),
111         frameRateSelectionStrategy(ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_PROPAGATE),
112         fixedTransformHint(ui::Transform::ROT_INVALID),
113         autoRefresh(false),
114         trustedOverlay(gui::TrustedOverlay::UNSET),
115         bufferCrop(Rect::INVALID_RECT),
116         destinationFrame(Rect::INVALID_RECT),
117         dropInputMode(gui::DropInputMode::NONE),
118         pictureProfileHandle(PictureProfileHandle::NONE),
119         appContentPriority(0) {
120     matrix.dsdx = matrix.dtdy = 1.0f;
121     matrix.dsdy = matrix.dtdx = 0.0f;
122     hdrMetadata.validTypes = 0;
123 }
124 
write(Parcel & output) const125 status_t layer_state_t::write(Parcel& output) const
126 {
127     SAFE_PARCEL(output.writeStrongBinder, surface);
128     SAFE_PARCEL(output.writeInt32, layerId);
129     SAFE_PARCEL(output.writeUint64, what);
130     SAFE_PARCEL(output.writeFloat, x);
131     SAFE_PARCEL(output.writeFloat, y);
132     SAFE_PARCEL(output.writeInt32, z);
133     SAFE_PARCEL(output.writeUint32, layerStack.id);
134     SAFE_PARCEL(output.writeUint32, flags);
135     SAFE_PARCEL(output.writeUint32, mask);
136     SAFE_PARCEL(matrix.write, output);
137     SAFE_PARCEL(output.writeFloat, crop.top);
138     SAFE_PARCEL(output.writeFloat, crop.left);
139     SAFE_PARCEL(output.writeFloat, crop.bottom);
140     SAFE_PARCEL(output.writeFloat, crop.right);
141     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output,
142                 mNotDefCmpState.relativeLayerSurfaceControl);
143     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output,
144                 mNotDefCmpState.parentSurfaceControlForChild);
145     SAFE_PARCEL(output.writeFloat, color.r);
146     SAFE_PARCEL(output.writeFloat, color.g);
147     SAFE_PARCEL(output.writeFloat, color.b);
148     SAFE_PARCEL(output.writeFloat, color.a);
149     SAFE_PARCEL(mNotDefCmpState.windowInfoHandle->writeToParcel, &output);
150     SAFE_PARCEL(output.write, mNotDefCmpState.transparentRegion);
151     SAFE_PARCEL(output.writeUint32, bufferTransform);
152     SAFE_PARCEL(output.writeBool, transformToDisplayInverse);
153     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dataspace));
154     SAFE_PARCEL(output.write, hdrMetadata);
155     SAFE_PARCEL(output.write, mNotDefCmpState.surfaceDamageRegion);
156     SAFE_PARCEL(output.writeInt32, api);
157 
158     if (sidebandStream) {
159         SAFE_PARCEL(output.writeBool, true);
160         SAFE_PARCEL(output.writeNativeHandle, sidebandStream->handle());
161     } else {
162         SAFE_PARCEL(output.writeBool, false);
163     }
164 
165     SAFE_PARCEL(output.write, colorTransform.asArray(), 16 * sizeof(float));
166     SAFE_PARCEL(output.writeFloat, cornerRadius);
167     SAFE_PARCEL(output.writeFloat, clientDrawnCornerRadius);
168     SAFE_PARCEL(output.writeUint32, backgroundBlurRadius);
169     SAFE_PARCEL(output.writeParcelable, metadata);
170     SAFE_PARCEL(output.writeFloat, bgColor.r);
171     SAFE_PARCEL(output.writeFloat, bgColor.g);
172     SAFE_PARCEL(output.writeFloat, bgColor.b);
173     SAFE_PARCEL(output.writeFloat, bgColor.a);
174     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(bgColorDataspace));
175     SAFE_PARCEL(output.writeBool, colorSpaceAgnostic);
176     SAFE_PARCEL(output.writeVectorSize, listeners);
177 
178     for (auto listener : listeners) {
179         SAFE_PARCEL(output.writeStrongBinder, listener.transactionCompletedListener);
180         SAFE_PARCEL(output.writeParcelableVector, listener.callbackIds);
181     }
182     SAFE_PARCEL(output.writeFloat, shadowRadius);
183     SAFE_PARCEL(output.writeParcelable, borderSettings);
184     SAFE_PARCEL(output.writeInt32, frameRateSelectionPriority);
185     SAFE_PARCEL(output.writeFloat, frameRate);
186     SAFE_PARCEL(output.writeByte, frameRateCompatibility);
187     SAFE_PARCEL(output.writeByte, changeFrameRateStrategy);
188     SAFE_PARCEL(output.writeByte, defaultFrameRateCompatibility);
189     SAFE_PARCEL(output.writeByte, frameRateCategory);
190     SAFE_PARCEL(output.writeBool, frameRateCategorySmoothSwitchOnly);
191     SAFE_PARCEL(output.writeByte, frameRateSelectionStrategy);
192     SAFE_PARCEL(output.writeUint32, fixedTransformHint);
193     SAFE_PARCEL(output.writeBool, autoRefresh);
194     SAFE_PARCEL(output.writeBool, dimmingEnabled);
195 
196     SAFE_PARCEL(output.writeUint32, blurRegions.size());
197     for (auto region : blurRegions) {
198         SAFE_PARCEL(output.writeUint32, region.blurRadius);
199         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTL);
200         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTR);
201         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBL);
202         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBR);
203         SAFE_PARCEL(output.writeFloat, region.alpha);
204         SAFE_PARCEL(output.writeInt32, region.left);
205         SAFE_PARCEL(output.writeInt32, region.top);
206         SAFE_PARCEL(output.writeInt32, region.right);
207         SAFE_PARCEL(output.writeInt32, region.bottom);
208     }
209 
210     SAFE_PARCEL(output.write, stretchEffect);
211     SAFE_PARCEL(output.writeParcelable, edgeExtensionParameters);
212     SAFE_PARCEL(output.write, bufferCrop);
213     SAFE_PARCEL(output.write, destinationFrame);
214     SAFE_PARCEL(output.writeInt32, static_cast<uint32_t>(trustedOverlay));
215 
216     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dropInputMode));
217 
218     const bool hasBufferData = (bufferData != nullptr);
219     SAFE_PARCEL(output.writeBool, hasBufferData);
220     if (hasBufferData) {
221         SAFE_PARCEL(output.writeParcelable, *bufferData);
222     }
223     SAFE_PARCEL(output.writeParcelable, trustedPresentationThresholds);
224     SAFE_PARCEL(output.writeParcelable, trustedPresentationListener);
225     SAFE_PARCEL(output.writeFloat, currentHdrSdrRatio);
226     SAFE_PARCEL(output.writeFloat, desiredHdrSdrRatio);
227     SAFE_PARCEL(output.writeInt32, static_cast<int32_t>(cachingHint));
228 
229     const bool hasBufferReleaseChannel = (bufferReleaseChannel != nullptr);
230     SAFE_PARCEL(output.writeBool, hasBufferReleaseChannel);
231     if (hasBufferReleaseChannel) {
232         SAFE_PARCEL(output.writeParcelable, *bufferReleaseChannel);
233     }
234 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS_APPLY_PICTURE_PROFILES
235     SAFE_PARCEL(output.writeInt64, pictureProfileHandle.getId());
236     SAFE_PARCEL(output.writeInt32, appContentPriority);
237 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS_APPLY_PICTURE_PROFILES
238 
239     const bool hasLuts = (luts != nullptr);
240     SAFE_PARCEL(output.writeBool, hasLuts);
241     if (hasLuts) {
242         SAFE_PARCEL(output.writeParcelable, *luts);
243     }
244 
245     return NO_ERROR;
246 }
247 
read(const Parcel & input)248 status_t layer_state_t::read(const Parcel& input)
249 {
250     SAFE_PARCEL(input.readNullableStrongBinder, &surface);
251     SAFE_PARCEL(input.readInt32, &layerId);
252     SAFE_PARCEL(input.readUint64, &what);
253     SAFE_PARCEL(input.readFloat, &x);
254     SAFE_PARCEL(input.readFloat, &y);
255     SAFE_PARCEL(input.readInt32, &z);
256     SAFE_PARCEL(input.readUint32, &layerStack.id);
257 
258     SAFE_PARCEL(input.readUint32, &flags);
259 
260     SAFE_PARCEL(input.readUint32, &mask);
261 
262     SAFE_PARCEL(matrix.read, input);
263     SAFE_PARCEL(input.readFloat, &crop.top);
264     SAFE_PARCEL(input.readFloat, &crop.left);
265     SAFE_PARCEL(input.readFloat, &crop.bottom);
266     SAFE_PARCEL(input.readFloat, &crop.right);
267 
268     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input,
269                 &mNotDefCmpState.relativeLayerSurfaceControl);
270     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input,
271                 &mNotDefCmpState.parentSurfaceControlForChild);
272 
273     float tmpFloat = 0;
274     SAFE_PARCEL(input.readFloat, &tmpFloat);
275     color.r = tmpFloat;
276     SAFE_PARCEL(input.readFloat, &tmpFloat);
277     color.g = tmpFloat;
278     SAFE_PARCEL(input.readFloat, &tmpFloat);
279     color.b = tmpFloat;
280     SAFE_PARCEL(input.readFloat, &tmpFloat);
281     color.a = tmpFloat;
282 
283     SAFE_PARCEL(mNotDefCmpState.windowInfoHandle->readFromParcel, &input);
284 
285     SAFE_PARCEL(input.read, mNotDefCmpState.transparentRegion);
286     SAFE_PARCEL(input.readUint32, &bufferTransform);
287     SAFE_PARCEL(input.readBool, &transformToDisplayInverse);
288 
289     uint32_t tmpUint32 = 0;
290     SAFE_PARCEL(input.readUint32, &tmpUint32);
291     dataspace = static_cast<ui::Dataspace>(tmpUint32);
292 
293     SAFE_PARCEL(input.read, hdrMetadata);
294     SAFE_PARCEL(input.read, mNotDefCmpState.surfaceDamageRegion);
295     SAFE_PARCEL(input.readInt32, &api);
296 
297     bool tmpBool = false;
298     SAFE_PARCEL(input.readBool, &tmpBool);
299     if (tmpBool) {
300         sidebandStream = NativeHandle::create(input.readNativeHandle(), true);
301     }
302 
303     SAFE_PARCEL(input.read, &colorTransform, 16 * sizeof(float));
304     SAFE_PARCEL(input.readFloat, &cornerRadius);
305     SAFE_PARCEL(input.readFloat, &clientDrawnCornerRadius);
306     SAFE_PARCEL(input.readUint32, &backgroundBlurRadius);
307     SAFE_PARCEL(input.readParcelable, &metadata);
308 
309     SAFE_PARCEL(input.readFloat, &tmpFloat);
310     bgColor.r = tmpFloat;
311     SAFE_PARCEL(input.readFloat, &tmpFloat);
312     bgColor.g = tmpFloat;
313     SAFE_PARCEL(input.readFloat, &tmpFloat);
314     bgColor.b = tmpFloat;
315     SAFE_PARCEL(input.readFloat, &tmpFloat);
316     bgColor.a = tmpFloat;
317     SAFE_PARCEL(input.readUint32, &tmpUint32);
318     bgColorDataspace = static_cast<ui::Dataspace>(tmpUint32);
319     SAFE_PARCEL(input.readBool, &colorSpaceAgnostic);
320 
321     int32_t numListeners = 0;
322     SAFE_PARCEL_READ_SIZE(input.readInt32, &numListeners, input.dataSize());
323     listeners.clear();
324     for (int i = 0; i < numListeners; i++) {
325         sp<IBinder> listener;
326         std::vector<CallbackId> callbackIds;
327         SAFE_PARCEL(input.readNullableStrongBinder, &listener);
328         SAFE_PARCEL(input.readParcelableVector, &callbackIds);
329         listeners.emplace_back(listener, callbackIds);
330     }
331     SAFE_PARCEL(input.readFloat, &shadowRadius);
332     SAFE_PARCEL(input.readParcelable, &borderSettings);
333 
334     SAFE_PARCEL(input.readInt32, &frameRateSelectionPriority);
335     SAFE_PARCEL(input.readFloat, &frameRate);
336     SAFE_PARCEL(input.readByte, &frameRateCompatibility);
337     SAFE_PARCEL(input.readByte, &changeFrameRateStrategy);
338     SAFE_PARCEL(input.readByte, &defaultFrameRateCompatibility);
339     SAFE_PARCEL(input.readByte, &frameRateCategory);
340     SAFE_PARCEL(input.readBool, &frameRateCategorySmoothSwitchOnly);
341     SAFE_PARCEL(input.readByte, &frameRateSelectionStrategy);
342     SAFE_PARCEL(input.readUint32, &tmpUint32);
343     fixedTransformHint = static_cast<ui::Transform::RotationFlags>(tmpUint32);
344     SAFE_PARCEL(input.readBool, &autoRefresh);
345     SAFE_PARCEL(input.readBool, &dimmingEnabled);
346 
347     uint32_t numRegions = 0;
348     SAFE_PARCEL(input.readUint32, &numRegions);
349     blurRegions.clear();
350     for (uint32_t i = 0; i < numRegions; i++) {
351         BlurRegion region;
352         SAFE_PARCEL(input.readUint32, &region.blurRadius);
353         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTL);
354         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTR);
355         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBL);
356         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBR);
357         SAFE_PARCEL(input.readFloat, &region.alpha);
358         SAFE_PARCEL(input.readInt32, &region.left);
359         SAFE_PARCEL(input.readInt32, &region.top);
360         SAFE_PARCEL(input.readInt32, &region.right);
361         SAFE_PARCEL(input.readInt32, &region.bottom);
362         blurRegions.push_back(region);
363     }
364 
365     SAFE_PARCEL(input.read, stretchEffect);
366     SAFE_PARCEL(input.readParcelable, &edgeExtensionParameters);
367     SAFE_PARCEL(input.read, bufferCrop);
368     SAFE_PARCEL(input.read, destinationFrame);
369     uint32_t trustedOverlayInt;
370     SAFE_PARCEL(input.readUint32, &trustedOverlayInt);
371     trustedOverlay = static_cast<gui::TrustedOverlay>(trustedOverlayInt);
372 
373     uint32_t mode;
374     SAFE_PARCEL(input.readUint32, &mode);
375     dropInputMode = static_cast<gui::DropInputMode>(mode);
376 
377     bool hasBufferData;
378     SAFE_PARCEL(input.readBool, &hasBufferData);
379     if (hasBufferData) {
380         bufferData = std::make_shared<BufferData>();
381         SAFE_PARCEL(input.readParcelable, bufferData.get());
382     } else {
383         bufferData = nullptr;
384     }
385 
386     SAFE_PARCEL(input.readParcelable, &trustedPresentationThresholds);
387     SAFE_PARCEL(input.readParcelable, &trustedPresentationListener);
388 
389     SAFE_PARCEL(input.readFloat, &tmpFloat);
390     currentHdrSdrRatio = tmpFloat;
391     SAFE_PARCEL(input.readFloat, &tmpFloat);
392     desiredHdrSdrRatio = tmpFloat;
393 
394     int32_t tmpInt32;
395     SAFE_PARCEL(input.readInt32, &tmpInt32);
396     cachingHint = static_cast<gui::CachingHint>(tmpInt32);
397 
398     bool hasBufferReleaseChannel;
399     SAFE_PARCEL(input.readBool, &hasBufferReleaseChannel);
400     if (hasBufferReleaseChannel) {
401         bufferReleaseChannel = std::make_shared<gui::BufferReleaseChannel::ProducerEndpoint>();
402         SAFE_PARCEL(input.readParcelable, bufferReleaseChannel.get());
403     }
404 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS_APPLY_PICTURE_PROFILES
405     int64_t pictureProfileId;
406     SAFE_PARCEL(input.readInt64, &pictureProfileId);
407     pictureProfileHandle = PictureProfileHandle(pictureProfileId);
408     SAFE_PARCEL(input.readInt32, &appContentPriority);
409 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS_APPLY_PICTURE_PROFILES
410 
411     bool hasLuts;
412     SAFE_PARCEL(input.readBool, &hasLuts);
413     if (hasLuts) {
414         luts = std::make_shared<gui::DisplayLuts>();
415         SAFE_PARCEL(input.readParcelable, luts.get());
416     } else {
417         luts = nullptr;
418     }
419 
420     return NO_ERROR;
421 }
422 
write(Parcel & output) const423 status_t ComposerState::write(Parcel& output) const {
424     return state.write(output);
425 }
426 
read(const Parcel & input)427 status_t ComposerState::read(const Parcel& input) {
428     return state.read(input);
429 }
430 
431 DisplayState::DisplayState() = default;
432 
write(Parcel & output) const433 status_t DisplayState::write(Parcel& output) const {
434     SAFE_PARCEL(output.writeStrongBinder, token);
435     SAFE_PARCEL(output.writeStrongBinder, IInterface::asBinder(surface));
436     SAFE_PARCEL(output.writeUint32, what);
437     SAFE_PARCEL(output.writeUint32, flags);
438     SAFE_PARCEL(output.writeUint32, layerStack.id);
439     SAFE_PARCEL(output.writeUint32, toRotationInt(orientation));
440     SAFE_PARCEL(output.write, layerStackSpaceRect);
441     SAFE_PARCEL(output.write, orientedDisplaySpaceRect);
442     SAFE_PARCEL(output.writeUint32, width);
443     SAFE_PARCEL(output.writeUint32, height);
444     return NO_ERROR;
445 }
446 
read(const Parcel & input)447 status_t DisplayState::read(const Parcel& input) {
448     SAFE_PARCEL(input.readStrongBinder, &token);
449     sp<IBinder> tmpBinder;
450     SAFE_PARCEL(input.readNullableStrongBinder, &tmpBinder);
451     surface = interface_cast<IGraphicBufferProducer>(tmpBinder);
452 
453     SAFE_PARCEL(input.readUint32, &what);
454     SAFE_PARCEL(input.readUint32, &flags);
455     SAFE_PARCEL(input.readUint32, &layerStack.id);
456     uint32_t tmpUint = 0;
457     SAFE_PARCEL(input.readUint32, &tmpUint);
458     orientation = ui::toRotation(tmpUint);
459 
460     SAFE_PARCEL(input.read, layerStackSpaceRect);
461     SAFE_PARCEL(input.read, orientedDisplaySpaceRect);
462     SAFE_PARCEL(input.readUint32, &width);
463     SAFE_PARCEL(input.readUint32, &height);
464     return NO_ERROR;
465 }
466 
merge(const DisplayState & other)467 void DisplayState::merge(const DisplayState& other) {
468     if (other.what & eSurfaceChanged) {
469         what |= eSurfaceChanged;
470         surface = other.surface;
471     }
472     if (other.what & eLayerStackChanged) {
473         what |= eLayerStackChanged;
474         layerStack = other.layerStack;
475     }
476     if (other.what & eFlagsChanged) {
477         what |= eFlagsChanged;
478         flags = other.flags;
479     }
480     if (other.what & eDisplayProjectionChanged) {
481         what |= eDisplayProjectionChanged;
482         orientation = other.orientation;
483         layerStackSpaceRect = other.layerStackSpaceRect;
484         orientedDisplaySpaceRect = other.orientedDisplaySpaceRect;
485     }
486     if (other.what & eDisplaySizeChanged) {
487         what |= eDisplaySizeChanged;
488         width = other.width;
489         height = other.height;
490     }
491 }
492 
sanitize(int32_t permissions)493 void DisplayState::sanitize(int32_t permissions) {
494     if (what & DisplayState::eLayerStackChanged) {
495         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
496             what &= ~DisplayState::eLayerStackChanged;
497             ALOGE("Stripped attempt to set eLayerStackChanged in sanitize");
498         }
499     }
500     if (what & DisplayState::eDisplayProjectionChanged) {
501         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
502             what &= ~DisplayState::eDisplayProjectionChanged;
503             ALOGE("Stripped attempt to set eDisplayProjectionChanged in sanitize");
504         }
505     }
506     if (what & DisplayState::eSurfaceChanged) {
507         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
508             what &= ~DisplayState::eSurfaceChanged;
509             ALOGE("Stripped attempt to set eSurfaceChanged in sanitize");
510         }
511     }
512 }
513 
sanitize(int32_t permissions)514 void layer_state_t::sanitize(int32_t permissions) {
515     // TODO: b/109894387
516     //
517     // SurfaceFlinger's renderer is not prepared to handle cropping in the face of arbitrary
518     // rotation. To see the problem observe that if we have a square parent, and a child
519     // of the same size, then we rotate the child 45 degrees around its center, the child
520     // must now be cropped to a non rectangular 8 sided region.
521     //
522     // Of course we can fix this in the future. For now, we are lucky, SurfaceControl is
523     // private API, and arbitrary rotation is used in limited use cases, for instance:
524     // - WindowManager only uses rotation in one case, which is on a top level layer in which
525     //   cropping is not an issue.
526     // - Launcher, as a privileged app, uses this to transition an application to PiP
527     //   (picture-in-picture) mode.
528     //
529     // However given that abuse of rotation matrices could lead to surfaces extending outside
530     // of cropped areas, we need to prevent non-root clients without permission
531     // ACCESS_SURFACE_FLINGER nor ROTATE_SURFACE_FLINGER
532     // (a.k.a. everyone except WindowManager / tests / Launcher) from setting non rectangle
533     // preserving transformations.
534     if (what & eMatrixChanged) {
535         if (!(permissions & Permission::ROTATE_SURFACE_FLINGER)) {
536             ui::Transform t;
537             t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
538             if (!t.preserveRects()) {
539                 what &= ~eMatrixChanged;
540                 ALOGE("Stripped non rect preserving matrix in sanitize");
541             }
542         }
543     }
544 
545     if (what & eFlagsChanged) {
546         if ((flags & eLayerIsDisplayDecoration) &&
547             !(permissions & Permission::INTERNAL_SYSTEM_WINDOW)) {
548             flags &= ~eLayerIsDisplayDecoration;
549             ALOGE("Stripped attempt to set LayerIsDisplayDecoration in sanitize");
550         }
551         if ((mask & eCanOccludePresentation) &&
552             !(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
553             flags &= ~eCanOccludePresentation;
554             mask &= ~eCanOccludePresentation;
555             ALOGE("Stripped attempt to set eCanOccludePresentation in sanitize");
556         }
557     }
558 
559     if (what & layer_state_t::eInputInfoChanged) {
560         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
561             what &= ~eInputInfoChanged;
562             ALOGE("Stripped attempt to set eInputInfoChanged in sanitize");
563         }
564     }
565     if (what & layer_state_t::eTrustedOverlayChanged) {
566         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
567             what &= ~eTrustedOverlayChanged;
568             ALOGE("Stripped attempt to set eTrustedOverlay in sanitize");
569         }
570     }
571     if (what & layer_state_t::eDropInputModeChanged) {
572         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
573             what &= ~eDropInputModeChanged;
574             ALOGE("Stripped attempt to set eDropInputModeChanged in sanitize");
575         }
576     }
577     if (what & layer_state_t::eFrameRateSelectionPriority) {
578         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
579             what &= ~eFrameRateSelectionPriority;
580             ALOGE("Stripped attempt to set eFrameRateSelectionPriority in sanitize");
581         }
582     }
583     if (what & layer_state_t::eFrameRateChanged) {
584         if (!ValidateFrameRate(frameRate, frameRateCompatibility,
585                                changeFrameRateStrategy,
586                                "layer_state_t::sanitize",
587                                permissions & Permission::ACCESS_SURFACE_FLINGER)) {
588             what &= ~eFrameRateChanged; // logged in ValidateFrameRate
589         }
590     }
591 }
592 
merge(const layer_state_t & other)593 void layer_state_t::merge(const layer_state_t& other) {
594     if (other.what & ePositionChanged) {
595         what |= ePositionChanged;
596         x = other.x;
597         y = other.y;
598     }
599     if (other.what & eLayerChanged) {
600         what |= eLayerChanged;
601         what &= ~eRelativeLayerChanged;
602         z = other.z;
603     }
604     if (other.what & eAlphaChanged) {
605         what |= eAlphaChanged;
606         color.a = other.color.a;
607     }
608     if (other.what & eMatrixChanged) {
609         what |= eMatrixChanged;
610         matrix = other.matrix;
611     }
612     if (other.what & eTransparentRegionChanged) {
613         what |= eTransparentRegionChanged;
614         mNotDefCmpState.transparentRegion = other.mNotDefCmpState.transparentRegion;
615     }
616     if (other.what & eFlagsChanged) {
617         what |= eFlagsChanged;
618         flags &= ~other.mask;
619         flags |= (other.flags & other.mask);
620         mask |= other.mask;
621     }
622     if (other.what & eLayerStackChanged) {
623         what |= eLayerStackChanged;
624         layerStack = other.layerStack;
625     }
626     if (other.what & eCornerRadiusChanged) {
627         what |= eCornerRadiusChanged;
628         cornerRadius = other.cornerRadius;
629     }
630     if (other.what & eClientDrawnCornerRadiusChanged) {
631         what |= eClientDrawnCornerRadiusChanged;
632         clientDrawnCornerRadius = other.clientDrawnCornerRadius;
633     }
634     if (other.what & eBackgroundBlurRadiusChanged) {
635         what |= eBackgroundBlurRadiusChanged;
636         backgroundBlurRadius = other.backgroundBlurRadius;
637     }
638     if (other.what & eBlurRegionsChanged) {
639         what |= eBlurRegionsChanged;
640         blurRegions = other.blurRegions;
641     }
642     if (other.what & eRelativeLayerChanged) {
643         what |= eRelativeLayerChanged;
644         what &= ~eLayerChanged;
645         z = other.z;
646         mNotDefCmpState.relativeLayerSurfaceControl =
647                 other.mNotDefCmpState.relativeLayerSurfaceControl;
648     }
649     if (other.what & eReparent) {
650         what |= eReparent;
651         mNotDefCmpState.parentSurfaceControlForChild =
652                 other.mNotDefCmpState.parentSurfaceControlForChild;
653     }
654     if (other.what & eBufferTransformChanged) {
655         what |= eBufferTransformChanged;
656         bufferTransform = other.bufferTransform;
657     }
658     if (other.what & eTransformToDisplayInverseChanged) {
659         what |= eTransformToDisplayInverseChanged;
660         transformToDisplayInverse = other.transformToDisplayInverse;
661     }
662     if (other.what & eCropChanged) {
663         what |= eCropChanged;
664         crop = other.crop;
665     }
666     if (other.what & eBufferChanged) {
667         what |= eBufferChanged;
668         bufferData = other.bufferData;
669     }
670     if (other.what & eTrustedPresentationInfoChanged) {
671         what |= eTrustedPresentationInfoChanged;
672         trustedPresentationListener = other.trustedPresentationListener;
673         trustedPresentationThresholds = other.trustedPresentationThresholds;
674     }
675     if (other.what & eDataspaceChanged) {
676         what |= eDataspaceChanged;
677         dataspace = other.dataspace;
678     }
679     if (other.what & eExtendedRangeBrightnessChanged) {
680         what |= eExtendedRangeBrightnessChanged;
681         desiredHdrSdrRatio = other.desiredHdrSdrRatio;
682         currentHdrSdrRatio = other.currentHdrSdrRatio;
683     }
684     if (other.what & eDesiredHdrHeadroomChanged) {
685         what |= eDesiredHdrHeadroomChanged;
686         desiredHdrSdrRatio = other.desiredHdrSdrRatio;
687     }
688     if (other.what & eCachingHintChanged) {
689         what |= eCachingHintChanged;
690         cachingHint = other.cachingHint;
691     }
692     if (other.what & eHdrMetadataChanged) {
693         what |= eHdrMetadataChanged;
694         hdrMetadata = other.hdrMetadata;
695     }
696     if (other.what & eSurfaceDamageRegionChanged) {
697         what |= eSurfaceDamageRegionChanged;
698         mNotDefCmpState.surfaceDamageRegion = other.mNotDefCmpState.surfaceDamageRegion;
699     }
700     if (other.what & eApiChanged) {
701         what |= eApiChanged;
702         api = other.api;
703     }
704     if (other.what & eSidebandStreamChanged) {
705         what |= eSidebandStreamChanged;
706         sidebandStream = other.sidebandStream;
707     }
708     if (other.what & eColorTransformChanged) {
709         what |= eColorTransformChanged;
710         colorTransform = other.colorTransform;
711     }
712     if (other.what & eHasListenerCallbacksChanged) {
713         what |= eHasListenerCallbacksChanged;
714     }
715     if (other.what & eInputInfoChanged) {
716         what |= eInputInfoChanged;
717         mNotDefCmpState.windowInfoHandle =
718                 sp<WindowInfoHandle>::make(*other.mNotDefCmpState.windowInfoHandle);
719     }
720     if (other.what & eBackgroundColorChanged) {
721         what |= eBackgroundColorChanged;
722         bgColor = other.bgColor;
723         bgColorDataspace = other.bgColorDataspace;
724     }
725     if (other.what & eMetadataChanged) {
726         what |= eMetadataChanged;
727         metadata.merge(other.metadata);
728     }
729     if (other.what & eShadowRadiusChanged) {
730         what |= eShadowRadiusChanged;
731         shadowRadius = other.shadowRadius;
732     }
733     if (other.what & eBorderSettingsChanged) {
734         what |= eBorderSettingsChanged;
735         borderSettings = other.borderSettings;
736     }
737     if (other.what & eLutsChanged) {
738         what |= eLutsChanged;
739         luts = other.luts;
740     }
741     if (other.what & eDefaultFrameRateCompatibilityChanged) {
742         what |= eDefaultFrameRateCompatibilityChanged;
743         defaultFrameRateCompatibility = other.defaultFrameRateCompatibility;
744     }
745     if (other.what & eFrameRateSelectionPriority) {
746         what |= eFrameRateSelectionPriority;
747         frameRateSelectionPriority = other.frameRateSelectionPriority;
748     }
749     if (other.what & eFrameRateChanged) {
750         what |= eFrameRateChanged;
751         frameRate = other.frameRate;
752         frameRateCompatibility = other.frameRateCompatibility;
753         changeFrameRateStrategy = other.changeFrameRateStrategy;
754     }
755     if (other.what & eFrameRateCategoryChanged) {
756         what |= eFrameRateCategoryChanged;
757         frameRateCategory = other.frameRateCategory;
758         frameRateCategorySmoothSwitchOnly = other.frameRateCategorySmoothSwitchOnly;
759     }
760     if (other.what & eFrameRateSelectionStrategyChanged) {
761         what |= eFrameRateSelectionStrategyChanged;
762         frameRateSelectionStrategy = other.frameRateSelectionStrategy;
763     }
764     if (other.what & eFixedTransformHintChanged) {
765         what |= eFixedTransformHintChanged;
766         fixedTransformHint = other.fixedTransformHint;
767     }
768     if (other.what & eAutoRefreshChanged) {
769         what |= eAutoRefreshChanged;
770         autoRefresh = other.autoRefresh;
771     }
772     if (other.what & eTrustedOverlayChanged) {
773         what |= eTrustedOverlayChanged;
774         trustedOverlay = other.trustedOverlay;
775     }
776     if (other.what & eStretchChanged) {
777         what |= eStretchChanged;
778         stretchEffect = other.stretchEffect;
779     }
780     if (other.what & eEdgeExtensionChanged) {
781         what |= eEdgeExtensionChanged;
782         edgeExtensionParameters = other.edgeExtensionParameters;
783     }
784     if (other.what & eBufferCropChanged) {
785         what |= eBufferCropChanged;
786         bufferCrop = other.bufferCrop;
787     }
788     if (other.what & eDestinationFrameChanged) {
789         what |= eDestinationFrameChanged;
790         destinationFrame = other.destinationFrame;
791     }
792     if (other.what & eProducerDisconnect) {
793         what |= eProducerDisconnect;
794     }
795     if (other.what & eDropInputModeChanged) {
796         what |= eDropInputModeChanged;
797         dropInputMode = other.dropInputMode;
798     }
799     if (other.what & eColorChanged) {
800         what |= eColorChanged;
801         color.rgb = other.color.rgb;
802     }
803     if (other.what & eColorSpaceAgnosticChanged) {
804         what |= eColorSpaceAgnosticChanged;
805         colorSpaceAgnostic = other.colorSpaceAgnostic;
806     }
807     if (other.what & eDimmingEnabledChanged) {
808         what |= eDimmingEnabledChanged;
809         dimmingEnabled = other.dimmingEnabled;
810     }
811     if (other.what & eFlushJankData) {
812         what |= eFlushJankData;
813     }
814     if (other.what & eBufferReleaseChannelChanged) {
815         what |= eBufferReleaseChannelChanged;
816         bufferReleaseChannel = other.bufferReleaseChannel;
817     }
818     if (com_android_graphics_libgui_flags_apply_picture_profiles()) {
819         if (other.what & ePictureProfileHandleChanged) {
820             what |= ePictureProfileHandleChanged;
821             pictureProfileHandle = other.pictureProfileHandle;
822         }
823         if (other.what & eAppContentPriorityChanged) {
824             what |= eAppContentPriorityChanged;
825             appContentPriority = other.appContentPriority;
826         }
827     }
828     if ((other.what & what) != other.what) {
829         ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
830               "other.what=0x%" PRIX64 " what=0x%" PRIX64 " unmerged flags=0x%" PRIX64,
831               other.what, what, (other.what & what) ^ other.what);
832     }
833 }
834 
diff(const layer_state_t & other) const835 uint64_t layer_state_t::diff(const layer_state_t& other) const {
836     uint64_t diff = 0;
837     CHECK_DIFF2(diff, ePositionChanged, other, x, y);
838     if (other.what & eLayerChanged) {
839         diff |= eLayerChanged;
840         diff &= ~eRelativeLayerChanged;
841     }
842     CHECK_DIFF(diff, eAlphaChanged, other, color.a);
843     CHECK_DIFF(diff, eMatrixChanged, other, matrix);
844     if (other.what & eTransparentRegionChanged &&
845         (!mNotDefCmpState.transparentRegion.hasSameRects(
846                 other.mNotDefCmpState.transparentRegion))) {
847         diff |= eTransparentRegionChanged;
848     }
849     if (other.what & eFlagsChanged) {
850         uint64_t changedFlags = (flags & other.mask) ^ (other.flags & other.mask);
851         if (changedFlags) diff |= eFlagsChanged;
852     }
853     CHECK_DIFF(diff, eLayerStackChanged, other, layerStack);
854     CHECK_DIFF(diff, eCornerRadiusChanged, other, cornerRadius);
855     CHECK_DIFF(diff, eClientDrawnCornerRadiusChanged, other, clientDrawnCornerRadius);
856     CHECK_DIFF(diff, eBackgroundBlurRadiusChanged, other, backgroundBlurRadius);
857     if (other.what & eBlurRegionsChanged) diff |= eBlurRegionsChanged;
858     if (other.what & eRelativeLayerChanged) {
859         diff |= eRelativeLayerChanged;
860         diff &= ~eLayerChanged;
861     }
862     if (other.what & eReparent &&
863         !SurfaceControl::isSameSurface(mNotDefCmpState.parentSurfaceControlForChild,
864                                        other.mNotDefCmpState.parentSurfaceControlForChild)) {
865         diff |= eReparent;
866     }
867     CHECK_DIFF(diff, eBufferTransformChanged, other, bufferTransform);
868     CHECK_DIFF(diff, eTransformToDisplayInverseChanged, other, transformToDisplayInverse);
869     CHECK_DIFF(diff, eCropChanged, other, crop);
870     if (other.what & eBufferChanged) diff |= eBufferChanged;
871     CHECK_DIFF(diff, eDataspaceChanged, other, dataspace);
872     CHECK_DIFF2(diff, eExtendedRangeBrightnessChanged, other, currentHdrSdrRatio,
873                 desiredHdrSdrRatio);
874     CHECK_DIFF(diff, eDesiredHdrHeadroomChanged, other, desiredHdrSdrRatio);
875     CHECK_DIFF(diff, eCachingHintChanged, other, cachingHint);
876     CHECK_DIFF(diff, eHdrMetadataChanged, other, hdrMetadata);
877     if (other.what & eSurfaceDamageRegionChanged &&
878         (!mNotDefCmpState.surfaceDamageRegion.hasSameRects(
879                 other.mNotDefCmpState.surfaceDamageRegion))) {
880         diff |= eSurfaceDamageRegionChanged;
881     }
882     CHECK_DIFF(diff, eApiChanged, other, api);
883     if (other.what & eSidebandStreamChanged) diff |= eSidebandStreamChanged;
884     CHECK_DIFF(diff, eApiChanged, other, api);
885     CHECK_DIFF(diff, eColorTransformChanged, other, colorTransform);
886     if (other.what & eHasListenerCallbacksChanged) diff |= eHasListenerCallbacksChanged;
887     if (other.what & eInputInfoChanged) diff |= eInputInfoChanged;
888     CHECK_DIFF2(diff, eBackgroundColorChanged, other, bgColor, bgColorDataspace);
889     if (other.what & eMetadataChanged) diff |= eMetadataChanged;
890     CHECK_DIFF(diff, eShadowRadiusChanged, other, shadowRadius);
891     CHECK_DIFF(diff, eBorderSettingsChanged, other, borderSettings);
892     CHECK_DIFF(diff, eDefaultFrameRateCompatibilityChanged, other, defaultFrameRateCompatibility);
893     CHECK_DIFF(diff, eFrameRateSelectionPriority, other, frameRateSelectionPriority);
894     CHECK_DIFF3(diff, eFrameRateChanged, other, frameRate, frameRateCompatibility,
895                 changeFrameRateStrategy);
896     CHECK_DIFF2(diff, eFrameRateCategoryChanged, other, frameRateCategory,
897                 frameRateCategorySmoothSwitchOnly);
898     CHECK_DIFF(diff, eFrameRateSelectionStrategyChanged, other, frameRateSelectionStrategy);
899     CHECK_DIFF(diff, eFixedTransformHintChanged, other, fixedTransformHint);
900     CHECK_DIFF(diff, eAutoRefreshChanged, other, autoRefresh);
901     CHECK_DIFF(diff, eTrustedOverlayChanged, other, trustedOverlay);
902     CHECK_DIFF(diff, eStretchChanged, other, stretchEffect);
903     CHECK_DIFF(diff, eEdgeExtensionChanged, other, edgeExtensionParameters);
904     CHECK_DIFF(diff, eBufferCropChanged, other, bufferCrop);
905     CHECK_DIFF(diff, eDestinationFrameChanged, other, destinationFrame);
906     if (other.what & eProducerDisconnect) diff |= eProducerDisconnect;
907     CHECK_DIFF(diff, eDropInputModeChanged, other, dropInputMode);
908     CHECK_DIFF(diff, eColorChanged, other, color.rgb);
909     CHECK_DIFF(diff, eColorSpaceAgnosticChanged, other, colorSpaceAgnostic);
910     CHECK_DIFF(diff, eDimmingEnabledChanged, other, dimmingEnabled);
911     if (other.what & eBufferReleaseChannelChanged) diff |= eBufferReleaseChannelChanged;
912     if (other.what & eLutsChanged) diff |= eLutsChanged;
913     CHECK_DIFF(diff, ePictureProfileHandleChanged, other, pictureProfileHandle);
914     CHECK_DIFF(diff, eAppContentPriorityChanged, other, appContentPriority);
915 
916     return diff;
917 }
918 
hasBufferChanges() const919 bool layer_state_t::hasBufferChanges() const {
920     return what & layer_state_t::eBufferChanged;
921 }
922 
hasValidBuffer() const923 bool layer_state_t::hasValidBuffer() const {
924     return bufferData && (bufferData->hasBuffer() || bufferData->cachedBuffer.isValid());
925 }
926 
write(Parcel & output) const927 status_t layer_state_t::matrix22_t::write(Parcel& output) const {
928     SAFE_PARCEL(output.writeFloat, dsdx);
929     SAFE_PARCEL(output.writeFloat, dtdx);
930     SAFE_PARCEL(output.writeFloat, dtdy);
931     SAFE_PARCEL(output.writeFloat, dsdy);
932     return NO_ERROR;
933 }
934 
read(const Parcel & input)935 status_t layer_state_t::matrix22_t::read(const Parcel& input) {
936     SAFE_PARCEL(input.readFloat, &dsdx);
937     SAFE_PARCEL(input.readFloat, &dtdx);
938     SAFE_PARCEL(input.readFloat, &dtdy);
939     SAFE_PARCEL(input.readFloat, &dsdy);
940     return NO_ERROR;
941 }
updateTransparentRegion(const Region & transparentRegion)942 void layer_state_t::updateTransparentRegion(const Region& transparentRegion) {
943     what |= eTransparentRegionChanged;
944     mNotDefCmpState.transparentRegion = transparentRegion;
945 }
updateSurfaceDamageRegion(const Region & surfaceDamageRegion)946 void layer_state_t::updateSurfaceDamageRegion(const Region& surfaceDamageRegion) {
947     what |= eSurfaceDamageRegionChanged;
948     mNotDefCmpState.surfaceDamageRegion = surfaceDamageRegion;
949 }
updateRelativeLayer(const sp<SurfaceControl> & relativeTo,int32_t z)950 void layer_state_t::updateRelativeLayer(const sp<SurfaceControl>& relativeTo, int32_t z) {
951     what |= layer_state_t::eRelativeLayerChanged;
952     what &= ~layer_state_t::eLayerChanged;
953     mNotDefCmpState.relativeLayerSurfaceControl = relativeTo;
954     this->z = z;
955 }
updateParentLayer(const sp<SurfaceControl> & newParent)956 void layer_state_t::updateParentLayer(const sp<SurfaceControl>& newParent) {
957     what |= layer_state_t::eReparent;
958     mNotDefCmpState.parentSurfaceControlForChild =
959             newParent ? newParent->getParentingLayer() : nullptr;
960 }
updateInputWindowInfo(sp<gui::WindowInfoHandle> && info)961 void layer_state_t::updateInputWindowInfo(sp<gui::WindowInfoHandle>&& info) {
962     what |= eInputInfoChanged;
963     mNotDefCmpState.windowInfoHandle = std::move(info);
964 }
965 
operator ==(const NotDefaultComparableState & rhs) const966 bool layer_state_t::NotDefaultComparableState::operator==(
967         const NotDefaultComparableState& rhs) const {
968     return transparentRegion.hasSameRects(rhs.transparentRegion) &&
969             surfaceDamageRegion.hasSameRects(rhs.surfaceDamageRegion) &&
970             isSameWindowHandle(windowInfoHandle, rhs.windowInfoHandle) &&
971             isSameSurfaceControl(relativeLayerSurfaceControl, rhs.relativeLayerSurfaceControl) &&
972             isSameSurfaceControl(parentSurfaceControlForChild, rhs.parentSurfaceControlForChild);
973 }
974 
975 // ------------------------------- InputWindowCommands ----------------------------------------
976 
merge(const InputWindowCommands & other)977 bool InputWindowCommands::merge(const InputWindowCommands& other) {
978     bool changes = false;
979     changes |= !other.focusRequests.empty();
980     focusRequests.insert(focusRequests.end(), std::make_move_iterator(other.focusRequests.begin()),
981                          std::make_move_iterator(other.focusRequests.end()));
982     changes |= !other.windowInfosReportedListeners.empty();
983     windowInfosReportedListeners.insert(other.windowInfosReportedListeners.begin(),
984                                         other.windowInfosReportedListeners.end());
985     return changes;
986 }
987 
empty() const988 bool InputWindowCommands::empty() const {
989     return focusRequests.empty() && windowInfosReportedListeners.empty();
990 }
991 
clear()992 void InputWindowCommands::clear() {
993     focusRequests.clear();
994     windowInfosReportedListeners.clear();
995 }
996 
write(Parcel & output) const997 status_t InputWindowCommands::write(Parcel& output) const {
998     SAFE_PARCEL(output.writeParcelableVector, focusRequests);
999 
1000     SAFE_PARCEL(output.writeInt32, windowInfosReportedListeners.size());
1001     for (const auto& listener : windowInfosReportedListeners) {
1002         SAFE_PARCEL(output.writeStrongBinder, listener);
1003     }
1004 
1005     return NO_ERROR;
1006 }
1007 
read(const Parcel & input)1008 status_t InputWindowCommands::read(const Parcel& input) {
1009     SAFE_PARCEL(input.readParcelableVector, &focusRequests);
1010 
1011     int listenerSize = 0;
1012     SAFE_PARCEL_READ_SIZE(input.readInt32, &listenerSize, input.dataSize());
1013     windowInfosReportedListeners.reserve(listenerSize);
1014     for (int i = 0; i < listenerSize; i++) {
1015         sp<gui::IWindowInfosReportedListener> listener;
1016         SAFE_PARCEL(input.readStrongBinder, &listener);
1017         windowInfosReportedListeners.insert(listener);
1018     }
1019 
1020     return NO_ERROR;
1021 }
1022 
1023 // ----------------------------------------------------------------------------
1024 
generateReleaseCallbackId() const1025 ReleaseCallbackId BufferData::generateReleaseCallbackId() const {
1026     uint64_t bufferId;
1027     if (buffer) {
1028         bufferId = buffer->getId();
1029     } else {
1030         bufferId = cachedBuffer.id;
1031     }
1032     return {bufferId, frameNumber};
1033 }
1034 
writeToParcel(Parcel * output) const1035 status_t BufferData::writeToParcel(Parcel* output) const {
1036     SAFE_PARCEL(output->writeInt32, flags.get());
1037 
1038     if (buffer) {
1039         SAFE_PARCEL(output->writeBool, true);
1040         SAFE_PARCEL(output->write, *buffer);
1041     } else {
1042         SAFE_PARCEL(output->writeBool, false);
1043     }
1044 
1045     if (acquireFence) {
1046         SAFE_PARCEL(output->writeBool, true);
1047         SAFE_PARCEL(output->write, *acquireFence);
1048     } else {
1049         SAFE_PARCEL(output->writeBool, false);
1050     }
1051 
1052     SAFE_PARCEL(output->writeUint64, frameNumber);
1053     SAFE_PARCEL(output->writeStrongBinder, IInterface::asBinder(releaseBufferListener));
1054     SAFE_PARCEL(output->writeStrongBinder, releaseBufferEndpoint);
1055 
1056     SAFE_PARCEL(output->writeStrongBinder, cachedBuffer.token.promote());
1057     SAFE_PARCEL(output->writeUint64, cachedBuffer.id);
1058     SAFE_PARCEL(output->writeBool, hasBarrier);
1059     SAFE_PARCEL(output->writeUint64, barrierFrameNumber);
1060     SAFE_PARCEL(output->writeUint32, producerId);
1061     SAFE_PARCEL(output->writeInt64, dequeueTime);
1062 
1063     return NO_ERROR;
1064 }
1065 
readFromParcel(const Parcel * input)1066 status_t BufferData::readFromParcel(const Parcel* input) {
1067     int32_t tmpInt32;
1068     SAFE_PARCEL(input->readInt32, &tmpInt32);
1069     flags = ftl::Flags<BufferDataChange>(tmpInt32);
1070 
1071     bool tmpBool = false;
1072     SAFE_PARCEL(input->readBool, &tmpBool);
1073     if (tmpBool) {
1074         buffer = sp<GraphicBuffer>::make();
1075         SAFE_PARCEL(input->read, *buffer);
1076     }
1077 
1078     SAFE_PARCEL(input->readBool, &tmpBool);
1079     if (tmpBool) {
1080         acquireFence = sp<Fence>::make();
1081         SAFE_PARCEL(input->read, *acquireFence);
1082     }
1083 
1084     SAFE_PARCEL(input->readUint64, &frameNumber);
1085 
1086     sp<IBinder> tmpBinder = nullptr;
1087     SAFE_PARCEL(input->readNullableStrongBinder, &tmpBinder);
1088     if (tmpBinder) {
1089         releaseBufferListener = checked_interface_cast<ITransactionCompletedListener>(tmpBinder);
1090     }
1091     SAFE_PARCEL(input->readNullableStrongBinder, &releaseBufferEndpoint);
1092 
1093     tmpBinder = nullptr;
1094     SAFE_PARCEL(input->readNullableStrongBinder, &tmpBinder);
1095     cachedBuffer.token = tmpBinder;
1096     SAFE_PARCEL(input->readUint64, &cachedBuffer.id);
1097 
1098     SAFE_PARCEL(input->readBool, &hasBarrier);
1099     SAFE_PARCEL(input->readUint64, &barrierFrameNumber);
1100     SAFE_PARCEL(input->readUint32, &producerId);
1101     SAFE_PARCEL(input->readInt64, &dequeueTime);
1102 
1103     return NO_ERROR;
1104 }
1105 
writeToParcel(Parcel * parcel) const1106 status_t TrustedPresentationListener::writeToParcel(Parcel* parcel) const {
1107     SAFE_PARCEL(parcel->writeStrongBinder, mState.callbackInterface);
1108     SAFE_PARCEL(parcel->writeInt32, mState.callbackId);
1109     return NO_ERROR;
1110 }
1111 
readFromParcel(const Parcel * parcel)1112 status_t TrustedPresentationListener::readFromParcel(const Parcel* parcel) {
1113     sp<IBinder> tmpBinder = nullptr;
1114     SAFE_PARCEL(parcel->readNullableStrongBinder, &tmpBinder);
1115     if (tmpBinder) {
1116         mState.callbackInterface = checked_interface_cast<ITransactionCompletedListener>(tmpBinder);
1117     }
1118     SAFE_PARCEL(parcel->readInt32, &mState.callbackId);
1119     return NO_ERROR;
1120 }
1121 
1122 }; // namespace android
1123