• 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 #include <cmath>
21 
22 #include <android/native_window.h>
23 #include <binder/Parcel.h>
24 #include <gui/IGraphicBufferProducer.h>
25 #include <gui/ISurfaceComposerClient.h>
26 #include <gui/LayerState.h>
27 #include <private/gui/ParcelUtils.h>
28 #include <system/window.h>
29 #include <utils/Errors.h>
30 
31 namespace android {
32 
33 using gui::FocusRequest;
34 using gui::WindowInfoHandle;
35 
layer_state_t()36 layer_state_t::layer_state_t()
37       : surface(nullptr),
38         layerId(-1),
39         what(0),
40         x(0),
41         y(0),
42         z(0),
43         w(0),
44         h(0),
45         alpha(0),
46         flags(0),
47         mask(0),
48         reserved(0),
49         cornerRadius(0.0f),
50         backgroundBlurRadius(0),
51         transform(0),
52         transformToDisplayInverse(false),
53         crop(Rect::INVALID_RECT),
54         dataspace(ui::Dataspace::UNKNOWN),
55         surfaceDamageRegion(),
56         api(-1),
57         colorTransform(mat4()),
58         bgColorAlpha(0),
59         bgColorDataspace(ui::Dataspace::UNKNOWN),
60         colorSpaceAgnostic(false),
61         shadowRadius(0.0f),
62         frameRateSelectionPriority(-1),
63         frameRate(0.0f),
64         frameRateCompatibility(ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT),
65         changeFrameRateStrategy(ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS),
66         fixedTransformHint(ui::Transform::ROT_INVALID),
67         autoRefresh(false),
68         isTrustedOverlay(false),
69         bufferCrop(Rect::INVALID_RECT),
70         destinationFrame(Rect::INVALID_RECT),
71         dropInputMode(gui::DropInputMode::NONE) {
72     matrix.dsdx = matrix.dtdy = 1.0f;
73     matrix.dsdy = matrix.dtdx = 0.0f;
74     hdrMetadata.validTypes = 0;
75 }
76 
write(Parcel & output) const77 status_t layer_state_t::write(Parcel& output) const
78 {
79     SAFE_PARCEL(output.writeStrongBinder, surface);
80     SAFE_PARCEL(output.writeInt32, layerId);
81     SAFE_PARCEL(output.writeUint64, what);
82     SAFE_PARCEL(output.writeFloat, x);
83     SAFE_PARCEL(output.writeFloat, y);
84     SAFE_PARCEL(output.writeInt32, z);
85     SAFE_PARCEL(output.writeUint32, w);
86     SAFE_PARCEL(output.writeUint32, h);
87     SAFE_PARCEL(output.writeUint32, layerStack.id);
88     SAFE_PARCEL(output.writeFloat, alpha);
89     SAFE_PARCEL(output.writeUint32, flags);
90     SAFE_PARCEL(output.writeUint32, mask);
91     SAFE_PARCEL(matrix.write, output);
92     SAFE_PARCEL(output.write, crop);
93     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, reparentSurfaceControl);
94     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, relativeLayerSurfaceControl);
95     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, parentSurfaceControlForChild);
96     SAFE_PARCEL(output.writeFloat, color.r);
97     SAFE_PARCEL(output.writeFloat, color.g);
98     SAFE_PARCEL(output.writeFloat, color.b);
99     SAFE_PARCEL(windowInfoHandle->writeToParcel, &output);
100     SAFE_PARCEL(output.write, transparentRegion);
101     SAFE_PARCEL(output.writeUint32, transform);
102     SAFE_PARCEL(output.writeBool, transformToDisplayInverse);
103 
104     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dataspace));
105     SAFE_PARCEL(output.write, hdrMetadata);
106     SAFE_PARCEL(output.write, surfaceDamageRegion);
107     SAFE_PARCEL(output.writeInt32, api);
108 
109     if (sidebandStream) {
110         SAFE_PARCEL(output.writeBool, true);
111         SAFE_PARCEL(output.writeNativeHandle, sidebandStream->handle());
112     } else {
113         SAFE_PARCEL(output.writeBool, false);
114     }
115 
116     SAFE_PARCEL(output.write, colorTransform.asArray(), 16 * sizeof(float));
117     SAFE_PARCEL(output.writeFloat, cornerRadius);
118     SAFE_PARCEL(output.writeUint32, backgroundBlurRadius);
119     SAFE_PARCEL(output.writeParcelable, metadata);
120     SAFE_PARCEL(output.writeFloat, bgColorAlpha);
121     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(bgColorDataspace));
122     SAFE_PARCEL(output.writeBool, colorSpaceAgnostic);
123     SAFE_PARCEL(output.writeVectorSize, listeners);
124 
125     for (auto listener : listeners) {
126         SAFE_PARCEL(output.writeStrongBinder, listener.transactionCompletedListener);
127         SAFE_PARCEL(output.writeParcelableVector, listener.callbackIds);
128     }
129     SAFE_PARCEL(output.writeFloat, shadowRadius);
130     SAFE_PARCEL(output.writeInt32, frameRateSelectionPriority);
131     SAFE_PARCEL(output.writeFloat, frameRate);
132     SAFE_PARCEL(output.writeByte, frameRateCompatibility);
133     SAFE_PARCEL(output.writeByte, changeFrameRateStrategy);
134     SAFE_PARCEL(output.writeUint32, fixedTransformHint);
135     SAFE_PARCEL(output.writeBool, autoRefresh);
136     SAFE_PARCEL(output.writeBool, dimmingEnabled);
137 
138     SAFE_PARCEL(output.writeUint32, blurRegions.size());
139     for (auto region : blurRegions) {
140         SAFE_PARCEL(output.writeUint32, region.blurRadius);
141         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTL);
142         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTR);
143         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBL);
144         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBR);
145         SAFE_PARCEL(output.writeFloat, region.alpha);
146         SAFE_PARCEL(output.writeInt32, region.left);
147         SAFE_PARCEL(output.writeInt32, region.top);
148         SAFE_PARCEL(output.writeInt32, region.right);
149         SAFE_PARCEL(output.writeInt32, region.bottom);
150     }
151 
152     SAFE_PARCEL(output.write, stretchEffect);
153     SAFE_PARCEL(output.write, bufferCrop);
154     SAFE_PARCEL(output.write, destinationFrame);
155     SAFE_PARCEL(output.writeBool, isTrustedOverlay);
156 
157     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dropInputMode));
158 
159     const bool hasBufferData = (bufferData != nullptr);
160     SAFE_PARCEL(output.writeBool, hasBufferData);
161     if (hasBufferData) {
162         SAFE_PARCEL(output.writeParcelable, *bufferData);
163     }
164     return NO_ERROR;
165 }
166 
read(const Parcel & input)167 status_t layer_state_t::read(const Parcel& input)
168 {
169     SAFE_PARCEL(input.readNullableStrongBinder, &surface);
170     SAFE_PARCEL(input.readInt32, &layerId);
171     SAFE_PARCEL(input.readUint64, &what);
172     SAFE_PARCEL(input.readFloat, &x);
173     SAFE_PARCEL(input.readFloat, &y);
174     SAFE_PARCEL(input.readInt32, &z);
175     SAFE_PARCEL(input.readUint32, &w);
176     SAFE_PARCEL(input.readUint32, &h);
177     SAFE_PARCEL(input.readUint32, &layerStack.id);
178     SAFE_PARCEL(input.readFloat, &alpha);
179 
180     SAFE_PARCEL(input.readUint32, &flags);
181 
182     SAFE_PARCEL(input.readUint32, &mask);
183 
184     SAFE_PARCEL(matrix.read, input);
185     SAFE_PARCEL(input.read, crop);
186     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &reparentSurfaceControl);
187 
188     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &relativeLayerSurfaceControl);
189     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &parentSurfaceControlForChild);
190 
191     float tmpFloat = 0;
192     SAFE_PARCEL(input.readFloat, &tmpFloat);
193     color.r = tmpFloat;
194     SAFE_PARCEL(input.readFloat, &tmpFloat);
195     color.g = tmpFloat;
196     SAFE_PARCEL(input.readFloat, &tmpFloat);
197     color.b = tmpFloat;
198     SAFE_PARCEL(windowInfoHandle->readFromParcel, &input);
199 
200     SAFE_PARCEL(input.read, transparentRegion);
201     SAFE_PARCEL(input.readUint32, &transform);
202     SAFE_PARCEL(input.readBool, &transformToDisplayInverse);
203 
204     uint32_t tmpUint32 = 0;
205     SAFE_PARCEL(input.readUint32, &tmpUint32);
206     dataspace = static_cast<ui::Dataspace>(tmpUint32);
207 
208     SAFE_PARCEL(input.read, hdrMetadata);
209     SAFE_PARCEL(input.read, surfaceDamageRegion);
210     SAFE_PARCEL(input.readInt32, &api);
211 
212     bool tmpBool = false;
213     SAFE_PARCEL(input.readBool, &tmpBool);
214     if (tmpBool) {
215         sidebandStream = NativeHandle::create(input.readNativeHandle(), true);
216     }
217 
218     SAFE_PARCEL(input.read, &colorTransform, 16 * sizeof(float));
219     SAFE_PARCEL(input.readFloat, &cornerRadius);
220     SAFE_PARCEL(input.readUint32, &backgroundBlurRadius);
221     SAFE_PARCEL(input.readParcelable, &metadata);
222 
223     SAFE_PARCEL(input.readFloat, &bgColorAlpha);
224     SAFE_PARCEL(input.readUint32, &tmpUint32);
225     bgColorDataspace = static_cast<ui::Dataspace>(tmpUint32);
226     SAFE_PARCEL(input.readBool, &colorSpaceAgnostic);
227 
228     int32_t numListeners = 0;
229     SAFE_PARCEL_READ_SIZE(input.readInt32, &numListeners, input.dataSize());
230     listeners.clear();
231     for (int i = 0; i < numListeners; i++) {
232         sp<IBinder> listener;
233         std::vector<CallbackId> callbackIds;
234         SAFE_PARCEL(input.readNullableStrongBinder, &listener);
235         SAFE_PARCEL(input.readParcelableVector, &callbackIds);
236         listeners.emplace_back(listener, callbackIds);
237     }
238     SAFE_PARCEL(input.readFloat, &shadowRadius);
239     SAFE_PARCEL(input.readInt32, &frameRateSelectionPriority);
240     SAFE_PARCEL(input.readFloat, &frameRate);
241     SAFE_PARCEL(input.readByte, &frameRateCompatibility);
242     SAFE_PARCEL(input.readByte, &changeFrameRateStrategy);
243     SAFE_PARCEL(input.readUint32, &tmpUint32);
244     fixedTransformHint = static_cast<ui::Transform::RotationFlags>(tmpUint32);
245     SAFE_PARCEL(input.readBool, &autoRefresh);
246     SAFE_PARCEL(input.readBool, &dimmingEnabled);
247 
248     uint32_t numRegions = 0;
249     SAFE_PARCEL(input.readUint32, &numRegions);
250     blurRegions.clear();
251     for (uint32_t i = 0; i < numRegions; i++) {
252         BlurRegion region;
253         SAFE_PARCEL(input.readUint32, &region.blurRadius);
254         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTL);
255         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTR);
256         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBL);
257         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBR);
258         SAFE_PARCEL(input.readFloat, &region.alpha);
259         SAFE_PARCEL(input.readInt32, &region.left);
260         SAFE_PARCEL(input.readInt32, &region.top);
261         SAFE_PARCEL(input.readInt32, &region.right);
262         SAFE_PARCEL(input.readInt32, &region.bottom);
263         blurRegions.push_back(region);
264     }
265 
266     SAFE_PARCEL(input.read, stretchEffect);
267     SAFE_PARCEL(input.read, bufferCrop);
268     SAFE_PARCEL(input.read, destinationFrame);
269     SAFE_PARCEL(input.readBool, &isTrustedOverlay);
270 
271     uint32_t mode;
272     SAFE_PARCEL(input.readUint32, &mode);
273     dropInputMode = static_cast<gui::DropInputMode>(mode);
274 
275     bool hasBufferData;
276     SAFE_PARCEL(input.readBool, &hasBufferData);
277     if (hasBufferData) {
278         bufferData = std::make_shared<BufferData>();
279         SAFE_PARCEL(input.readParcelable, bufferData.get());
280     } else {
281         bufferData = nullptr;
282     }
283     return NO_ERROR;
284 }
285 
write(Parcel & output) const286 status_t ComposerState::write(Parcel& output) const {
287     return state.write(output);
288 }
289 
read(const Parcel & input)290 status_t ComposerState::read(const Parcel& input) {
291     return state.read(input);
292 }
293 
294 DisplayState::DisplayState() = default;
295 
write(Parcel & output) const296 status_t DisplayState::write(Parcel& output) const {
297     SAFE_PARCEL(output.writeStrongBinder, token);
298     SAFE_PARCEL(output.writeStrongBinder, IInterface::asBinder(surface));
299     SAFE_PARCEL(output.writeUint32, what);
300     SAFE_PARCEL(output.writeUint32, flags);
301     SAFE_PARCEL(output.writeUint32, layerStack.id);
302     SAFE_PARCEL(output.writeUint32, toRotationInt(orientation));
303     SAFE_PARCEL(output.write, layerStackSpaceRect);
304     SAFE_PARCEL(output.write, orientedDisplaySpaceRect);
305     SAFE_PARCEL(output.writeUint32, width);
306     SAFE_PARCEL(output.writeUint32, height);
307     return NO_ERROR;
308 }
309 
read(const Parcel & input)310 status_t DisplayState::read(const Parcel& input) {
311     SAFE_PARCEL(input.readStrongBinder, &token);
312     sp<IBinder> tmpBinder;
313     SAFE_PARCEL(input.readNullableStrongBinder, &tmpBinder);
314     surface = interface_cast<IGraphicBufferProducer>(tmpBinder);
315 
316     SAFE_PARCEL(input.readUint32, &what);
317     SAFE_PARCEL(input.readUint32, &flags);
318     SAFE_PARCEL(input.readUint32, &layerStack.id);
319     uint32_t tmpUint = 0;
320     SAFE_PARCEL(input.readUint32, &tmpUint);
321     orientation = ui::toRotation(tmpUint);
322 
323     SAFE_PARCEL(input.read, layerStackSpaceRect);
324     SAFE_PARCEL(input.read, orientedDisplaySpaceRect);
325     SAFE_PARCEL(input.readUint32, &width);
326     SAFE_PARCEL(input.readUint32, &height);
327     return NO_ERROR;
328 }
329 
merge(const DisplayState & other)330 void DisplayState::merge(const DisplayState& other) {
331     if (other.what & eSurfaceChanged) {
332         what |= eSurfaceChanged;
333         surface = other.surface;
334     }
335     if (other.what & eLayerStackChanged) {
336         what |= eLayerStackChanged;
337         layerStack = other.layerStack;
338     }
339     if (other.what & eFlagsChanged) {
340         what |= eFlagsChanged;
341         flags = other.flags;
342     }
343     if (other.what & eDisplayProjectionChanged) {
344         what |= eDisplayProjectionChanged;
345         orientation = other.orientation;
346         layerStackSpaceRect = other.layerStackSpaceRect;
347         orientedDisplaySpaceRect = other.orientedDisplaySpaceRect;
348     }
349     if (other.what & eDisplaySizeChanged) {
350         what |= eDisplaySizeChanged;
351         width = other.width;
352         height = other.height;
353     }
354 }
355 
sanitize(int32_t permissions)356 void DisplayState::sanitize(int32_t permissions) {
357     if (what & DisplayState::eLayerStackChanged) {
358         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
359             what &= ~DisplayState::eLayerStackChanged;
360             ALOGE("Stripped attempt to set eLayerStackChanged in sanitize");
361         }
362     }
363     if (what & DisplayState::eDisplayProjectionChanged) {
364         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
365             what &= ~DisplayState::eDisplayProjectionChanged;
366             ALOGE("Stripped attempt to set eDisplayProjectionChanged in sanitize");
367         }
368     }
369     if (what & DisplayState::eSurfaceChanged) {
370         if (!(permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER)) {
371             what &= ~DisplayState::eSurfaceChanged;
372             ALOGE("Stripped attempt to set eSurfaceChanged in sanitize");
373         }
374     }
375 }
376 
sanitize(int32_t permissions)377 void layer_state_t::sanitize(int32_t permissions) {
378     // TODO: b/109894387
379     //
380     // SurfaceFlinger's renderer is not prepared to handle cropping in the face of arbitrary
381     // rotation. To see the problem observe that if we have a square parent, and a child
382     // of the same size, then we rotate the child 45 degrees around its center, the child
383     // must now be cropped to a non rectangular 8 sided region.
384     //
385     // Of course we can fix this in the future. For now, we are lucky, SurfaceControl is
386     // private API, and arbitrary rotation is used in limited use cases, for instance:
387     // - WindowManager only uses rotation in one case, which is on a top level layer in which
388     //   cropping is not an issue.
389     // - Launcher, as a privileged app, uses this to transition an application to PiP
390     //   (picture-in-picture) mode.
391     //
392     // However given that abuse of rotation matrices could lead to surfaces extending outside
393     // of cropped areas, we need to prevent non-root clients without permission
394     // ACCESS_SURFACE_FLINGER nor ROTATE_SURFACE_FLINGER
395     // (a.k.a. everyone except WindowManager / tests / Launcher) from setting non rectangle
396     // preserving transformations.
397     if (what & eMatrixChanged) {
398         if (!(permissions & Permission::ROTATE_SURFACE_FLINGER)) {
399             ui::Transform t;
400             t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
401             if (!t.preserveRects()) {
402                 what &= ~eMatrixChanged;
403                 ALOGE("Stripped non rect preserving matrix in sanitize");
404             }
405         }
406     }
407 
408     if (what & eFlagsChanged) {
409         if ((flags & eLayerIsDisplayDecoration) &&
410             !(permissions & Permission::INTERNAL_SYSTEM_WINDOW)) {
411             flags &= ~eLayerIsDisplayDecoration;
412             ALOGE("Stripped attempt to set LayerIsDisplayDecoration in sanitize");
413         }
414     }
415 
416     if (what & layer_state_t::eInputInfoChanged) {
417         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
418             what &= ~eInputInfoChanged;
419             ALOGE("Stripped attempt to set eInputInfoChanged in sanitize");
420         }
421     }
422     if (what & layer_state_t::eTrustedOverlayChanged) {
423         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
424             what &= ~eTrustedOverlayChanged;
425             ALOGE("Stripped attempt to set eTrustedOverlay in sanitize");
426         }
427     }
428     if (what & layer_state_t::eDropInputModeChanged) {
429         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
430             what &= ~eDropInputModeChanged;
431             ALOGE("Stripped attempt to set eDropInputModeChanged in sanitize");
432         }
433     }
434     if (what & layer_state_t::eFrameRateSelectionPriority) {
435         if (!(permissions & Permission::ACCESS_SURFACE_FLINGER)) {
436             what &= ~eFrameRateSelectionPriority;
437             ALOGE("Stripped attempt to set eFrameRateSelectionPriority in sanitize");
438         }
439     }
440     if (what & layer_state_t::eFrameRateChanged) {
441         if (!ValidateFrameRate(frameRate, frameRateCompatibility,
442                                changeFrameRateStrategy,
443                                "layer_state_t::sanitize",
444                                permissions & Permission::ACCESS_SURFACE_FLINGER)) {
445             what &= ~eFrameRateChanged; // logged in ValidateFrameRate
446         }
447     }
448 }
449 
merge(const layer_state_t & other)450 void layer_state_t::merge(const layer_state_t& other) {
451     if (other.what & ePositionChanged) {
452         what |= ePositionChanged;
453         x = other.x;
454         y = other.y;
455     }
456     if (other.what & eLayerChanged) {
457         what |= eLayerChanged;
458         what &= ~eRelativeLayerChanged;
459         z = other.z;
460     }
461     if (other.what & eSizeChanged) {
462         what |= eSizeChanged;
463         w = other.w;
464         h = other.h;
465     }
466     if (other.what & eAlphaChanged) {
467         what |= eAlphaChanged;
468         alpha = other.alpha;
469     }
470     if (other.what & eMatrixChanged) {
471         what |= eMatrixChanged;
472         matrix = other.matrix;
473     }
474     if (other.what & eTransparentRegionChanged) {
475         what |= eTransparentRegionChanged;
476         transparentRegion = other.transparentRegion;
477     }
478     if (other.what & eFlagsChanged) {
479         what |= eFlagsChanged;
480         flags &= ~other.mask;
481         flags |= (other.flags & other.mask);
482         mask |= other.mask;
483     }
484     if (other.what & eLayerStackChanged) {
485         what |= eLayerStackChanged;
486         layerStack = other.layerStack;
487     }
488     if (other.what & eCornerRadiusChanged) {
489         what |= eCornerRadiusChanged;
490         cornerRadius = other.cornerRadius;
491     }
492     if (other.what & eBackgroundBlurRadiusChanged) {
493         what |= eBackgroundBlurRadiusChanged;
494         backgroundBlurRadius = other.backgroundBlurRadius;
495     }
496     if (other.what & eBlurRegionsChanged) {
497         what |= eBlurRegionsChanged;
498         blurRegions = other.blurRegions;
499     }
500     if (other.what & eRelativeLayerChanged) {
501         what |= eRelativeLayerChanged;
502         what &= ~eLayerChanged;
503         z = other.z;
504         relativeLayerSurfaceControl = other.relativeLayerSurfaceControl;
505     }
506     if (other.what & eReparent) {
507         what |= eReparent;
508         parentSurfaceControlForChild = other.parentSurfaceControlForChild;
509     }
510     if (other.what & eDestroySurface) {
511         what |= eDestroySurface;
512     }
513     if (other.what & eTransformChanged) {
514         what |= eTransformChanged;
515         transform = other.transform;
516     }
517     if (other.what & eTransformToDisplayInverseChanged) {
518         what |= eTransformToDisplayInverseChanged;
519         transformToDisplayInverse = other.transformToDisplayInverse;
520     }
521     if (other.what & eCropChanged) {
522         what |= eCropChanged;
523         crop = other.crop;
524     }
525     if (other.what & eBufferChanged) {
526         what |= eBufferChanged;
527         bufferData = other.bufferData;
528     }
529     if (other.what & eDataspaceChanged) {
530         what |= eDataspaceChanged;
531         dataspace = other.dataspace;
532     }
533     if (other.what & eHdrMetadataChanged) {
534         what |= eHdrMetadataChanged;
535         hdrMetadata = other.hdrMetadata;
536     }
537     if (other.what & eSurfaceDamageRegionChanged) {
538         what |= eSurfaceDamageRegionChanged;
539         surfaceDamageRegion = other.surfaceDamageRegion;
540     }
541     if (other.what & eApiChanged) {
542         what |= eApiChanged;
543         api = other.api;
544     }
545     if (other.what & eSidebandStreamChanged) {
546         what |= eSidebandStreamChanged;
547         sidebandStream = other.sidebandStream;
548     }
549     if (other.what & eColorTransformChanged) {
550         what |= eColorTransformChanged;
551         colorTransform = other.colorTransform;
552     }
553     if (other.what & eHasListenerCallbacksChanged) {
554         what |= eHasListenerCallbacksChanged;
555     }
556     if (other.what & eInputInfoChanged) {
557         what |= eInputInfoChanged;
558         windowInfoHandle = new WindowInfoHandle(*other.windowInfoHandle);
559     }
560     if (other.what & eBackgroundColorChanged) {
561         what |= eBackgroundColorChanged;
562         color = other.color;
563         bgColorAlpha = other.bgColorAlpha;
564         bgColorDataspace = other.bgColorDataspace;
565     }
566     if (other.what & eMetadataChanged) {
567         what |= eMetadataChanged;
568         metadata.merge(other.metadata);
569     }
570     if (other.what & eShadowRadiusChanged) {
571         what |= eShadowRadiusChanged;
572         shadowRadius = other.shadowRadius;
573     }
574     if (other.what & eFrameRateSelectionPriority) {
575         what |= eFrameRateSelectionPriority;
576         frameRateSelectionPriority = other.frameRateSelectionPriority;
577     }
578     if (other.what & eFrameRateChanged) {
579         what |= eFrameRateChanged;
580         frameRate = other.frameRate;
581         frameRateCompatibility = other.frameRateCompatibility;
582         changeFrameRateStrategy = other.changeFrameRateStrategy;
583     }
584     if (other.what & eFixedTransformHintChanged) {
585         what |= eFixedTransformHintChanged;
586         fixedTransformHint = other.fixedTransformHint;
587     }
588     if (other.what & eAutoRefreshChanged) {
589         what |= eAutoRefreshChanged;
590         autoRefresh = other.autoRefresh;
591     }
592     if (other.what & eTrustedOverlayChanged) {
593         what |= eTrustedOverlayChanged;
594         isTrustedOverlay = other.isTrustedOverlay;
595     }
596     if (other.what & eStretchChanged) {
597         what |= eStretchChanged;
598         stretchEffect = other.stretchEffect;
599     }
600     if (other.what & eBufferCropChanged) {
601         what |= eBufferCropChanged;
602         bufferCrop = other.bufferCrop;
603     }
604     if (other.what & eDestinationFrameChanged) {
605         what |= eDestinationFrameChanged;
606         destinationFrame = other.destinationFrame;
607     }
608     if (other.what & eProducerDisconnect) {
609         what |= eProducerDisconnect;
610     }
611     if (other.what & eDropInputModeChanged) {
612         what |= eDropInputModeChanged;
613         dropInputMode = other.dropInputMode;
614     }
615     if (other.what & eColorChanged) {
616         what |= eColorChanged;
617         color = other.color;
618     }
619     if (other.what & eColorSpaceAgnosticChanged) {
620         what |= eColorSpaceAgnosticChanged;
621         colorSpaceAgnostic = other.colorSpaceAgnostic;
622     }
623     if (other.what & eDimmingEnabledChanged) {
624         what |= eDimmingEnabledChanged;
625         dimmingEnabled = other.dimmingEnabled;
626     }
627     if ((other.what & what) != other.what) {
628         ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
629               "other.what=0x%" PRIX64 " what=0x%" PRIX64 " unmerged flags=0x%" PRIX64,
630               other.what, what, (other.what & what) ^ other.what);
631     }
632 }
633 
hasBufferChanges() const634 bool layer_state_t::hasBufferChanges() const {
635     return what & layer_state_t::eBufferChanged;
636 }
637 
hasValidBuffer() const638 bool layer_state_t::hasValidBuffer() const {
639     return bufferData && (bufferData->buffer || bufferData->cachedBuffer.isValid());
640 }
641 
write(Parcel & output) const642 status_t layer_state_t::matrix22_t::write(Parcel& output) const {
643     SAFE_PARCEL(output.writeFloat, dsdx);
644     SAFE_PARCEL(output.writeFloat, dtdx);
645     SAFE_PARCEL(output.writeFloat, dtdy);
646     SAFE_PARCEL(output.writeFloat, dsdy);
647     return NO_ERROR;
648 }
649 
read(const Parcel & input)650 status_t layer_state_t::matrix22_t::read(const Parcel& input) {
651     SAFE_PARCEL(input.readFloat, &dsdx);
652     SAFE_PARCEL(input.readFloat, &dtdx);
653     SAFE_PARCEL(input.readFloat, &dtdy);
654     SAFE_PARCEL(input.readFloat, &dsdy);
655     return NO_ERROR;
656 }
657 
658 // ------------------------------- InputWindowCommands ----------------------------------------
659 
merge(const InputWindowCommands & other)660 bool InputWindowCommands::merge(const InputWindowCommands& other) {
661     bool changes = false;
662     changes |= !other.focusRequests.empty();
663     focusRequests.insert(focusRequests.end(), std::make_move_iterator(other.focusRequests.begin()),
664                          std::make_move_iterator(other.focusRequests.end()));
665     changes |= other.syncInputWindows && !syncInputWindows;
666     syncInputWindows |= other.syncInputWindows;
667     return changes;
668 }
669 
empty() const670 bool InputWindowCommands::empty() const {
671     return focusRequests.empty() && !syncInputWindows;
672 }
673 
clear()674 void InputWindowCommands::clear() {
675     focusRequests.clear();
676     syncInputWindows = false;
677 }
678 
write(Parcel & output) const679 status_t InputWindowCommands::write(Parcel& output) const {
680     SAFE_PARCEL(output.writeParcelableVector, focusRequests);
681     SAFE_PARCEL(output.writeBool, syncInputWindows);
682     return NO_ERROR;
683 }
684 
read(const Parcel & input)685 status_t InputWindowCommands::read(const Parcel& input) {
686     SAFE_PARCEL(input.readParcelableVector, &focusRequests);
687     SAFE_PARCEL(input.readBool, &syncInputWindows);
688     return NO_ERROR;
689 }
690 
ValidateFrameRate(float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy,const char * inFunctionName,bool privileged)691 bool ValidateFrameRate(float frameRate, int8_t compatibility, int8_t changeFrameRateStrategy,
692                        const char* inFunctionName, bool privileged) {
693     const char* functionName = inFunctionName != nullptr ? inFunctionName : "call";
694     int floatClassification = std::fpclassify(frameRate);
695     if (frameRate < 0 || floatClassification == FP_INFINITE || floatClassification == FP_NAN) {
696         ALOGE("%s failed - invalid frame rate %f", functionName, frameRate);
697         return false;
698     }
699 
700     if (compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT &&
701         compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE &&
702         (!privileged ||
703          (compatibility != ANATIVEWINDOW_FRAME_RATE_EXACT &&
704           compatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE))) {
705         ALOGE("%s failed - invalid compatibility value %d privileged: %s", functionName,
706               compatibility, privileged ? "yes" : "no");
707         return false;
708     }
709 
710     if (changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS &&
711         changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS) {
712         ALOGE("%s failed - invalid change frame rate strategy value %d", functionName,
713               changeFrameRateStrategy);
714     }
715 
716     return true;
717 }
718 
719 // ----------------------------------------------------------------------------
720 
721 namespace gui {
722 
writeToParcel(Parcel * output) const723 status_t CaptureArgs::writeToParcel(Parcel* output) const {
724     SAFE_PARCEL(output->writeInt32, static_cast<int32_t>(pixelFormat));
725     SAFE_PARCEL(output->write, sourceCrop);
726     SAFE_PARCEL(output->writeFloat, frameScaleX);
727     SAFE_PARCEL(output->writeFloat, frameScaleY);
728     SAFE_PARCEL(output->writeBool, captureSecureLayers);
729     SAFE_PARCEL(output->writeInt32, uid);
730     SAFE_PARCEL(output->writeInt32, static_cast<int32_t>(dataspace));
731     SAFE_PARCEL(output->writeBool, allowProtected);
732     SAFE_PARCEL(output->writeBool, grayscale);
733     return NO_ERROR;
734 }
735 
readFromParcel(const Parcel * input)736 status_t CaptureArgs::readFromParcel(const Parcel* input) {
737     int32_t value = 0;
738     SAFE_PARCEL(input->readInt32, &value);
739     pixelFormat = static_cast<ui::PixelFormat>(value);
740     SAFE_PARCEL(input->read, sourceCrop);
741     SAFE_PARCEL(input->readFloat, &frameScaleX);
742     SAFE_PARCEL(input->readFloat, &frameScaleY);
743     SAFE_PARCEL(input->readBool, &captureSecureLayers);
744     SAFE_PARCEL(input->readInt32, &uid);
745     SAFE_PARCEL(input->readInt32, &value);
746     dataspace = static_cast<ui::Dataspace>(value);
747     SAFE_PARCEL(input->readBool, &allowProtected);
748     SAFE_PARCEL(input->readBool, &grayscale);
749     return NO_ERROR;
750 }
751 
writeToParcel(Parcel * output) const752 status_t DisplayCaptureArgs::writeToParcel(Parcel* output) const {
753     SAFE_PARCEL(CaptureArgs::writeToParcel, output);
754 
755     SAFE_PARCEL(output->writeStrongBinder, displayToken);
756     SAFE_PARCEL(output->writeUint32, width);
757     SAFE_PARCEL(output->writeUint32, height);
758     SAFE_PARCEL(output->writeBool, useIdentityTransform);
759     return NO_ERROR;
760 }
761 
readFromParcel(const Parcel * input)762 status_t DisplayCaptureArgs::readFromParcel(const Parcel* input) {
763     SAFE_PARCEL(CaptureArgs::readFromParcel, input);
764 
765     SAFE_PARCEL(input->readStrongBinder, &displayToken);
766     SAFE_PARCEL(input->readUint32, &width);
767     SAFE_PARCEL(input->readUint32, &height);
768     SAFE_PARCEL(input->readBool, &useIdentityTransform);
769     return NO_ERROR;
770 }
771 
writeToParcel(Parcel * output) const772 status_t LayerCaptureArgs::writeToParcel(Parcel* output) const {
773     SAFE_PARCEL(CaptureArgs::writeToParcel, output);
774 
775     SAFE_PARCEL(output->writeStrongBinder, layerHandle);
776     SAFE_PARCEL(output->writeInt32, excludeHandles.size());
777     for (auto el : excludeHandles) {
778         SAFE_PARCEL(output->writeStrongBinder, el);
779     }
780     SAFE_PARCEL(output->writeBool, childrenOnly);
781     return NO_ERROR;
782 }
783 
readFromParcel(const Parcel * input)784 status_t LayerCaptureArgs::readFromParcel(const Parcel* input) {
785     SAFE_PARCEL(CaptureArgs::readFromParcel, input);
786 
787     SAFE_PARCEL(input->readStrongBinder, &layerHandle);
788 
789     int32_t numExcludeHandles = 0;
790     SAFE_PARCEL_READ_SIZE(input->readInt32, &numExcludeHandles, input->dataSize());
791     excludeHandles.reserve(numExcludeHandles);
792     for (int i = 0; i < numExcludeHandles; i++) {
793         sp<IBinder> binder;
794         SAFE_PARCEL(input->readStrongBinder, &binder);
795         excludeHandles.emplace(binder);
796     }
797 
798     SAFE_PARCEL(input->readBool, &childrenOnly);
799     return NO_ERROR;
800 }
801 
802 }; // namespace gui
803 
generateReleaseCallbackId() const804 ReleaseCallbackId BufferData::generateReleaseCallbackId() const {
805     uint64_t bufferId;
806     if (buffer) {
807         bufferId = buffer->getId();
808     } else {
809         bufferId = cachedBuffer.id;
810     }
811     return {bufferId, frameNumber};
812 }
813 
writeToParcel(Parcel * output) const814 status_t BufferData::writeToParcel(Parcel* output) const {
815     SAFE_PARCEL(output->writeInt32, flags.get());
816 
817     if (buffer) {
818         SAFE_PARCEL(output->writeBool, true);
819         SAFE_PARCEL(output->write, *buffer);
820     } else {
821         SAFE_PARCEL(output->writeBool, false);
822     }
823 
824     if (acquireFence) {
825         SAFE_PARCEL(output->writeBool, true);
826         SAFE_PARCEL(output->write, *acquireFence);
827     } else {
828         SAFE_PARCEL(output->writeBool, false);
829     }
830 
831     SAFE_PARCEL(output->writeUint64, frameNumber);
832     SAFE_PARCEL(output->writeStrongBinder, IInterface::asBinder(releaseBufferListener));
833     SAFE_PARCEL(output->writeStrongBinder, releaseBufferEndpoint);
834 
835     SAFE_PARCEL(output->writeStrongBinder, cachedBuffer.token.promote());
836     SAFE_PARCEL(output->writeUint64, cachedBuffer.id);
837     SAFE_PARCEL(output->writeBool, hasBarrier);
838     SAFE_PARCEL(output->writeUint64, barrierFrameNumber);
839 
840     return NO_ERROR;
841 }
842 
readFromParcel(const Parcel * input)843 status_t BufferData::readFromParcel(const Parcel* input) {
844     int32_t tmpInt32;
845     SAFE_PARCEL(input->readInt32, &tmpInt32);
846     flags = ftl::Flags<BufferDataChange>(tmpInt32);
847 
848     bool tmpBool = false;
849     SAFE_PARCEL(input->readBool, &tmpBool);
850     if (tmpBool) {
851         buffer = new GraphicBuffer();
852         SAFE_PARCEL(input->read, *buffer);
853     }
854 
855     SAFE_PARCEL(input->readBool, &tmpBool);
856     if (tmpBool) {
857         acquireFence = new Fence();
858         SAFE_PARCEL(input->read, *acquireFence);
859     }
860 
861     SAFE_PARCEL(input->readUint64, &frameNumber);
862 
863     sp<IBinder> tmpBinder = nullptr;
864     SAFE_PARCEL(input->readNullableStrongBinder, &tmpBinder);
865     if (tmpBinder) {
866         releaseBufferListener = checked_interface_cast<ITransactionCompletedListener>(tmpBinder);
867     }
868     SAFE_PARCEL(input->readNullableStrongBinder, &releaseBufferEndpoint);
869 
870     tmpBinder = nullptr;
871     SAFE_PARCEL(input->readNullableStrongBinder, &tmpBinder);
872     cachedBuffer.token = tmpBinder;
873     SAFE_PARCEL(input->readUint64, &cachedBuffer.id);
874 
875     SAFE_PARCEL(input->readBool, &hasBarrier);
876     SAFE_PARCEL(input->readUint64, &barrierFrameNumber);
877 
878     return NO_ERROR;
879 }
880 
881 }; // namespace android
882