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 <apex/window.h>
20 #include <inttypes.h>
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 <utils/Errors.h>
29 
30 #include <cmath>
31 
32 namespace android {
33 
34 using gui::FocusRequest;
35 using gui::WindowInfoHandle;
36 
layer_state_t()37 layer_state_t::layer_state_t()
38       : what(0),
39         x(0),
40         y(0),
41         z(0),
42         w(0),
43         h(0),
44         layerStack(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         orientedDisplaySpaceRect(Rect::INVALID_RECT),
55         dataspace(ui::Dataspace::UNKNOWN),
56         surfaceDamageRegion(),
57         api(-1),
58         colorTransform(mat4()),
59         bgColorAlpha(0),
60         bgColorDataspace(ui::Dataspace::UNKNOWN),
61         colorSpaceAgnostic(false),
62         shadowRadius(0.0f),
63         frameRateSelectionPriority(-1),
64         frameRate(0.0f),
65         frameRateCompatibility(ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT),
66         changeFrameRateStrategy(ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS),
67         fixedTransformHint(ui::Transform::ROT_INVALID),
68         frameNumber(0),
69         autoRefresh(false),
70         isTrustedOverlay(false),
71         bufferCrop(Rect::INVALID_RECT),
72         destinationFrame(Rect::INVALID_RECT),
73         releaseBufferListener(nullptr),
74         dropInputMode(gui::DropInputMode::NONE) {
75     matrix.dsdx = matrix.dtdy = 1.0f;
76     matrix.dsdy = matrix.dtdx = 0.0f;
77     hdrMetadata.validTypes = 0;
78 }
79 
write(Parcel & output) const80 status_t layer_state_t::write(Parcel& output) const
81 {
82     SAFE_PARCEL(output.writeStrongBinder, surface);
83     SAFE_PARCEL(output.writeInt32, layerId);
84     SAFE_PARCEL(output.writeUint64, what);
85     SAFE_PARCEL(output.writeFloat, x);
86     SAFE_PARCEL(output.writeFloat, y);
87     SAFE_PARCEL(output.writeInt32, z);
88     SAFE_PARCEL(output.writeUint32, w);
89     SAFE_PARCEL(output.writeUint32, h);
90     SAFE_PARCEL(output.writeUint32, layerStack);
91     SAFE_PARCEL(output.writeFloat, alpha);
92     SAFE_PARCEL(output.writeUint32, flags);
93     SAFE_PARCEL(output.writeUint32, mask);
94     SAFE_PARCEL(matrix.write, output);
95     SAFE_PARCEL(output.write, crop);
96     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, reparentSurfaceControl);
97     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, relativeLayerSurfaceControl);
98     SAFE_PARCEL(SurfaceControl::writeNullableToParcel, output, parentSurfaceControlForChild);
99     SAFE_PARCEL(output.writeFloat, color.r);
100     SAFE_PARCEL(output.writeFloat, color.g);
101     SAFE_PARCEL(output.writeFloat, color.b);
102     SAFE_PARCEL(windowInfoHandle->writeToParcel, &output);
103     SAFE_PARCEL(output.write, transparentRegion);
104     SAFE_PARCEL(output.writeUint32, transform);
105     SAFE_PARCEL(output.writeBool, transformToDisplayInverse);
106     SAFE_PARCEL(output.write, orientedDisplaySpaceRect);
107 
108     if (buffer) {
109         SAFE_PARCEL(output.writeBool, true);
110         SAFE_PARCEL(output.write, *buffer);
111     } else {
112         SAFE_PARCEL(output.writeBool, false);
113     }
114 
115     if (acquireFence) {
116         SAFE_PARCEL(output.writeBool, true);
117         SAFE_PARCEL(output.write, *acquireFence);
118     } else {
119         SAFE_PARCEL(output.writeBool, false);
120     }
121 
122     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dataspace));
123     SAFE_PARCEL(output.write, hdrMetadata);
124     SAFE_PARCEL(output.write, surfaceDamageRegion);
125     SAFE_PARCEL(output.writeInt32, api);
126 
127     if (sidebandStream) {
128         SAFE_PARCEL(output.writeBool, true);
129         SAFE_PARCEL(output.writeNativeHandle, sidebandStream->handle());
130     } else {
131         SAFE_PARCEL(output.writeBool, false);
132     }
133 
134     SAFE_PARCEL(output.write, colorTransform.asArray(), 16 * sizeof(float));
135     SAFE_PARCEL(output.writeFloat, cornerRadius);
136     SAFE_PARCEL(output.writeUint32, backgroundBlurRadius);
137     SAFE_PARCEL(output.writeStrongBinder, cachedBuffer.token.promote());
138     SAFE_PARCEL(output.writeUint64, cachedBuffer.id);
139     SAFE_PARCEL(output.writeParcelable, metadata);
140     SAFE_PARCEL(output.writeFloat, bgColorAlpha);
141     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(bgColorDataspace));
142     SAFE_PARCEL(output.writeBool, colorSpaceAgnostic);
143     SAFE_PARCEL(output.writeVectorSize, listeners);
144 
145     for (auto listener : listeners) {
146         SAFE_PARCEL(output.writeStrongBinder, listener.transactionCompletedListener);
147         SAFE_PARCEL(output.writeParcelableVector, listener.callbackIds);
148     }
149     SAFE_PARCEL(output.writeFloat, shadowRadius);
150     SAFE_PARCEL(output.writeInt32, frameRateSelectionPriority);
151     SAFE_PARCEL(output.writeFloat, frameRate);
152     SAFE_PARCEL(output.writeByte, frameRateCompatibility);
153     SAFE_PARCEL(output.writeByte, changeFrameRateStrategy);
154     SAFE_PARCEL(output.writeUint32, fixedTransformHint);
155     SAFE_PARCEL(output.writeUint64, frameNumber);
156     SAFE_PARCEL(output.writeBool, autoRefresh);
157     SAFE_PARCEL(output.writeStrongBinder, IInterface::asBinder(releaseBufferListener));
158 
159     SAFE_PARCEL(output.writeUint32, blurRegions.size());
160     for (auto region : blurRegions) {
161         SAFE_PARCEL(output.writeUint32, region.blurRadius);
162         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTL);
163         SAFE_PARCEL(output.writeFloat, region.cornerRadiusTR);
164         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBL);
165         SAFE_PARCEL(output.writeFloat, region.cornerRadiusBR);
166         SAFE_PARCEL(output.writeFloat, region.alpha);
167         SAFE_PARCEL(output.writeInt32, region.left);
168         SAFE_PARCEL(output.writeInt32, region.top);
169         SAFE_PARCEL(output.writeInt32, region.right);
170         SAFE_PARCEL(output.writeInt32, region.bottom);
171     }
172 
173     SAFE_PARCEL(output.write, stretchEffect);
174     SAFE_PARCEL(output.write, bufferCrop);
175     SAFE_PARCEL(output.write, destinationFrame);
176     SAFE_PARCEL(output.writeBool, isTrustedOverlay);
177 
178     SAFE_PARCEL(output.writeStrongBinder, releaseBufferEndpoint);
179     SAFE_PARCEL(output.writeUint32, static_cast<uint32_t>(dropInputMode));
180     return NO_ERROR;
181 }
182 
read(const Parcel & input)183 status_t layer_state_t::read(const Parcel& input)
184 {
185     SAFE_PARCEL(input.readNullableStrongBinder, &surface);
186     SAFE_PARCEL(input.readInt32, &layerId);
187     SAFE_PARCEL(input.readUint64, &what);
188     SAFE_PARCEL(input.readFloat, &x);
189     SAFE_PARCEL(input.readFloat, &y);
190     SAFE_PARCEL(input.readInt32, &z);
191     SAFE_PARCEL(input.readUint32, &w);
192     SAFE_PARCEL(input.readUint32, &h);
193     SAFE_PARCEL(input.readUint32, &layerStack);
194     SAFE_PARCEL(input.readFloat, &alpha);
195 
196     SAFE_PARCEL(input.readUint32, &flags);
197 
198     SAFE_PARCEL(input.readUint32, &mask);
199 
200     SAFE_PARCEL(matrix.read, input);
201     SAFE_PARCEL(input.read, crop);
202     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &reparentSurfaceControl);
203 
204     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &relativeLayerSurfaceControl);
205     SAFE_PARCEL(SurfaceControl::readNullableFromParcel, input, &parentSurfaceControlForChild);
206 
207     float tmpFloat = 0;
208     SAFE_PARCEL(input.readFloat, &tmpFloat);
209     color.r = tmpFloat;
210     SAFE_PARCEL(input.readFloat, &tmpFloat);
211     color.g = tmpFloat;
212     SAFE_PARCEL(input.readFloat, &tmpFloat);
213     color.b = tmpFloat;
214     SAFE_PARCEL(windowInfoHandle->readFromParcel, &input);
215 
216     SAFE_PARCEL(input.read, transparentRegion);
217     SAFE_PARCEL(input.readUint32, &transform);
218     SAFE_PARCEL(input.readBool, &transformToDisplayInverse);
219     SAFE_PARCEL(input.read, orientedDisplaySpaceRect);
220 
221     bool tmpBool = false;
222     SAFE_PARCEL(input.readBool, &tmpBool);
223     if (tmpBool) {
224         buffer = new GraphicBuffer();
225         SAFE_PARCEL(input.read, *buffer);
226     }
227 
228     SAFE_PARCEL(input.readBool, &tmpBool);
229     if (tmpBool) {
230         acquireFence = new Fence();
231         SAFE_PARCEL(input.read, *acquireFence);
232     }
233 
234     uint32_t tmpUint32 = 0;
235     SAFE_PARCEL(input.readUint32, &tmpUint32);
236     dataspace = static_cast<ui::Dataspace>(tmpUint32);
237 
238     SAFE_PARCEL(input.read, hdrMetadata);
239     SAFE_PARCEL(input.read, surfaceDamageRegion);
240     SAFE_PARCEL(input.readInt32, &api);
241     SAFE_PARCEL(input.readBool, &tmpBool);
242     if (tmpBool) {
243         sidebandStream = NativeHandle::create(input.readNativeHandle(), true);
244     }
245 
246     SAFE_PARCEL(input.read, &colorTransform, 16 * sizeof(float));
247     SAFE_PARCEL(input.readFloat, &cornerRadius);
248     SAFE_PARCEL(input.readUint32, &backgroundBlurRadius);
249     sp<IBinder> tmpBinder;
250     SAFE_PARCEL(input.readNullableStrongBinder, &tmpBinder);
251     cachedBuffer.token = tmpBinder;
252     SAFE_PARCEL(input.readUint64, &cachedBuffer.id);
253     SAFE_PARCEL(input.readParcelable, &metadata);
254 
255     SAFE_PARCEL(input.readFloat, &bgColorAlpha);
256     SAFE_PARCEL(input.readUint32, &tmpUint32);
257     bgColorDataspace = static_cast<ui::Dataspace>(tmpUint32);
258     SAFE_PARCEL(input.readBool, &colorSpaceAgnostic);
259 
260     int32_t numListeners = 0;
261     SAFE_PARCEL_READ_SIZE(input.readInt32, &numListeners, input.dataSize());
262     listeners.clear();
263     for (int i = 0; i < numListeners; i++) {
264         sp<IBinder> listener;
265         std::vector<CallbackId> callbackIds;
266         SAFE_PARCEL(input.readNullableStrongBinder, &listener);
267         SAFE_PARCEL(input.readParcelableVector, &callbackIds);
268         listeners.emplace_back(listener, callbackIds);
269     }
270     SAFE_PARCEL(input.readFloat, &shadowRadius);
271     SAFE_PARCEL(input.readInt32, &frameRateSelectionPriority);
272     SAFE_PARCEL(input.readFloat, &frameRate);
273     SAFE_PARCEL(input.readByte, &frameRateCompatibility);
274     SAFE_PARCEL(input.readByte, &changeFrameRateStrategy);
275     SAFE_PARCEL(input.readUint32, &tmpUint32);
276     fixedTransformHint = static_cast<ui::Transform::RotationFlags>(tmpUint32);
277     SAFE_PARCEL(input.readUint64, &frameNumber);
278     SAFE_PARCEL(input.readBool, &autoRefresh);
279 
280     tmpBinder = nullptr;
281     SAFE_PARCEL(input.readNullableStrongBinder, &tmpBinder);
282     if (tmpBinder) {
283         releaseBufferListener = checked_interface_cast<ITransactionCompletedListener>(tmpBinder);
284     }
285 
286     uint32_t numRegions = 0;
287     SAFE_PARCEL(input.readUint32, &numRegions);
288     blurRegions.clear();
289     for (uint32_t i = 0; i < numRegions; i++) {
290         BlurRegion region;
291         SAFE_PARCEL(input.readUint32, &region.blurRadius);
292         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTL);
293         SAFE_PARCEL(input.readFloat, &region.cornerRadiusTR);
294         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBL);
295         SAFE_PARCEL(input.readFloat, &region.cornerRadiusBR);
296         SAFE_PARCEL(input.readFloat, &region.alpha);
297         SAFE_PARCEL(input.readInt32, &region.left);
298         SAFE_PARCEL(input.readInt32, &region.top);
299         SAFE_PARCEL(input.readInt32, &region.right);
300         SAFE_PARCEL(input.readInt32, &region.bottom);
301         blurRegions.push_back(region);
302     }
303 
304     SAFE_PARCEL(input.read, stretchEffect);
305     SAFE_PARCEL(input.read, bufferCrop);
306     SAFE_PARCEL(input.read, destinationFrame);
307     SAFE_PARCEL(input.readBool, &isTrustedOverlay);
308 
309     SAFE_PARCEL(input.readNullableStrongBinder, &releaseBufferEndpoint);
310 
311     uint32_t mode;
312     SAFE_PARCEL(input.readUint32, &mode);
313     dropInputMode = static_cast<gui::DropInputMode>(mode);
314     return NO_ERROR;
315 }
316 
write(Parcel & output) const317 status_t ComposerState::write(Parcel& output) const {
318     return state.write(output);
319 }
320 
read(const Parcel & input)321 status_t ComposerState::read(const Parcel& input) {
322     return state.read(input);
323 }
324 
DisplayState()325 DisplayState::DisplayState()
326       : what(0),
327         layerStack(0),
328         flags(0),
329         layerStackSpaceRect(Rect::EMPTY_RECT),
330         orientedDisplaySpaceRect(Rect::EMPTY_RECT),
331         width(0),
332         height(0) {}
333 
write(Parcel & output) const334 status_t DisplayState::write(Parcel& output) const {
335     SAFE_PARCEL(output.writeStrongBinder, token);
336     SAFE_PARCEL(output.writeStrongBinder, IInterface::asBinder(surface));
337     SAFE_PARCEL(output.writeUint32, what);
338     SAFE_PARCEL(output.writeUint32, layerStack);
339     SAFE_PARCEL(output.writeUint32, flags);
340     SAFE_PARCEL(output.writeUint32, toRotationInt(orientation));
341     SAFE_PARCEL(output.write, layerStackSpaceRect);
342     SAFE_PARCEL(output.write, orientedDisplaySpaceRect);
343     SAFE_PARCEL(output.writeUint32, width);
344     SAFE_PARCEL(output.writeUint32, height);
345     return NO_ERROR;
346 }
347 
read(const Parcel & input)348 status_t DisplayState::read(const Parcel& input) {
349     SAFE_PARCEL(input.readStrongBinder, &token);
350     sp<IBinder> tmpBinder;
351     SAFE_PARCEL(input.readNullableStrongBinder, &tmpBinder);
352     surface = interface_cast<IGraphicBufferProducer>(tmpBinder);
353 
354     SAFE_PARCEL(input.readUint32, &what);
355     SAFE_PARCEL(input.readUint32, &layerStack);
356     SAFE_PARCEL(input.readUint32, &flags);
357     uint32_t tmpUint = 0;
358     SAFE_PARCEL(input.readUint32, &tmpUint);
359     orientation = ui::toRotation(tmpUint);
360 
361     SAFE_PARCEL(input.read, layerStackSpaceRect);
362     SAFE_PARCEL(input.read, orientedDisplaySpaceRect);
363     SAFE_PARCEL(input.readUint32, &width);
364     SAFE_PARCEL(input.readUint32, &height);
365     return NO_ERROR;
366 }
367 
merge(const DisplayState & other)368 void DisplayState::merge(const DisplayState& other) {
369     if (other.what & eSurfaceChanged) {
370         what |= eSurfaceChanged;
371         surface = other.surface;
372     }
373     if (other.what & eLayerStackChanged) {
374         what |= eLayerStackChanged;
375         layerStack = other.layerStack;
376     }
377     if (other.what & eFlagsChanged) {
378         what |= eFlagsChanged;
379         flags = other.flags;
380     }
381     if (other.what & eDisplayProjectionChanged) {
382         what |= eDisplayProjectionChanged;
383         orientation = other.orientation;
384         layerStackSpaceRect = other.layerStackSpaceRect;
385         orientedDisplaySpaceRect = other.orientedDisplaySpaceRect;
386     }
387     if (other.what & eDisplaySizeChanged) {
388         what |= eDisplaySizeChanged;
389         width = other.width;
390         height = other.height;
391     }
392 }
393 
merge(const layer_state_t & other)394 void layer_state_t::merge(const layer_state_t& other) {
395     if (other.what & ePositionChanged) {
396         what |= ePositionChanged;
397         x = other.x;
398         y = other.y;
399     }
400     if (other.what & eLayerChanged) {
401         what |= eLayerChanged;
402         what &= ~eRelativeLayerChanged;
403         z = other.z;
404     }
405     if (other.what & eSizeChanged) {
406         what |= eSizeChanged;
407         w = other.w;
408         h = other.h;
409     }
410     if (other.what & eAlphaChanged) {
411         what |= eAlphaChanged;
412         alpha = other.alpha;
413     }
414     if (other.what & eMatrixChanged) {
415         what |= eMatrixChanged;
416         matrix = other.matrix;
417     }
418     if (other.what & eTransparentRegionChanged) {
419         what |= eTransparentRegionChanged;
420         transparentRegion = other.transparentRegion;
421     }
422     if (other.what & eFlagsChanged) {
423         what |= eFlagsChanged;
424         flags &= ~other.mask;
425         flags |= (other.flags & other.mask);
426         mask |= other.mask;
427     }
428     if (other.what & eLayerStackChanged) {
429         what |= eLayerStackChanged;
430         layerStack = other.layerStack;
431     }
432     if (other.what & eCornerRadiusChanged) {
433         what |= eCornerRadiusChanged;
434         cornerRadius = other.cornerRadius;
435     }
436     if (other.what & eBackgroundBlurRadiusChanged) {
437         what |= eBackgroundBlurRadiusChanged;
438         backgroundBlurRadius = other.backgroundBlurRadius;
439     }
440     if (other.what & eBlurRegionsChanged) {
441         what |= eBlurRegionsChanged;
442         blurRegions = other.blurRegions;
443     }
444     if (other.what & eRelativeLayerChanged) {
445         what |= eRelativeLayerChanged;
446         what &= ~eLayerChanged;
447         z = other.z;
448         relativeLayerSurfaceControl = other.relativeLayerSurfaceControl;
449     }
450     if (other.what & eReparent) {
451         what |= eReparent;
452         parentSurfaceControlForChild = other.parentSurfaceControlForChild;
453     }
454     if (other.what & eDestroySurface) {
455         what |= eDestroySurface;
456     }
457     if (other.what & eTransformChanged) {
458         what |= eTransformChanged;
459         transform = other.transform;
460     }
461     if (other.what & eTransformToDisplayInverseChanged) {
462         what |= eTransformToDisplayInverseChanged;
463         transformToDisplayInverse = other.transformToDisplayInverse;
464     }
465     if (other.what & eCropChanged) {
466         what |= eCropChanged;
467         crop = other.crop;
468     }
469     if (other.what & eBufferChanged) {
470         what |= eBufferChanged;
471         buffer = other.buffer;
472         releaseBufferEndpoint = other.releaseBufferEndpoint;
473     }
474     if (other.what & eAcquireFenceChanged) {
475         what |= eAcquireFenceChanged;
476         acquireFence = other.acquireFence;
477     }
478     if (other.what & eDataspaceChanged) {
479         what |= eDataspaceChanged;
480         dataspace = other.dataspace;
481     }
482     if (other.what & eHdrMetadataChanged) {
483         what |= eHdrMetadataChanged;
484         hdrMetadata = other.hdrMetadata;
485     }
486     if (other.what & eSurfaceDamageRegionChanged) {
487         what |= eSurfaceDamageRegionChanged;
488         surfaceDamageRegion = other.surfaceDamageRegion;
489     }
490     if (other.what & eApiChanged) {
491         what |= eApiChanged;
492         api = other.api;
493     }
494     if (other.what & eSidebandStreamChanged) {
495         what |= eSidebandStreamChanged;
496         sidebandStream = other.sidebandStream;
497     }
498     if (other.what & eColorTransformChanged) {
499         what |= eColorTransformChanged;
500         colorTransform = other.colorTransform;
501     }
502     if (other.what & eHasListenerCallbacksChanged) {
503         what |= eHasListenerCallbacksChanged;
504     }
505     if (other.what & eInputInfoChanged) {
506         what |= eInputInfoChanged;
507         windowInfoHandle = new WindowInfoHandle(*other.windowInfoHandle);
508     }
509     if (other.what & eCachedBufferChanged) {
510         what |= eCachedBufferChanged;
511         cachedBuffer = other.cachedBuffer;
512         releaseBufferEndpoint = other.releaseBufferEndpoint;
513     }
514     if (other.what & eBackgroundColorChanged) {
515         what |= eBackgroundColorChanged;
516         color = other.color;
517         bgColorAlpha = other.bgColorAlpha;
518         bgColorDataspace = other.bgColorDataspace;
519     }
520     if (other.what & eMetadataChanged) {
521         what |= eMetadataChanged;
522         metadata.merge(other.metadata);
523     }
524     if (other.what & eShadowRadiusChanged) {
525         what |= eShadowRadiusChanged;
526         shadowRadius = other.shadowRadius;
527     }
528     if (other.what & eFrameRateSelectionPriority) {
529         what |= eFrameRateSelectionPriority;
530         frameRateSelectionPriority = other.frameRateSelectionPriority;
531     }
532     if (other.what & eFrameRateChanged) {
533         what |= eFrameRateChanged;
534         frameRate = other.frameRate;
535         frameRateCompatibility = other.frameRateCompatibility;
536         changeFrameRateStrategy = other.changeFrameRateStrategy;
537     }
538     if (other.what & eFixedTransformHintChanged) {
539         what |= eFixedTransformHintChanged;
540         fixedTransformHint = other.fixedTransformHint;
541     }
542     if (other.what & eFrameNumberChanged) {
543         what |= eFrameNumberChanged;
544         frameNumber = other.frameNumber;
545     }
546     if (other.what & eAutoRefreshChanged) {
547         what |= eAutoRefreshChanged;
548         autoRefresh = other.autoRefresh;
549     }
550     if (other.what & eTrustedOverlayChanged) {
551         what |= eTrustedOverlayChanged;
552         isTrustedOverlay = other.isTrustedOverlay;
553     }
554     if (other.what & eReleaseBufferListenerChanged) {
555         if (releaseBufferListener) {
556             ALOGW("Overriding releaseBufferListener");
557         }
558         what |= eReleaseBufferListenerChanged;
559         releaseBufferListener = other.releaseBufferListener;
560     }
561     if (other.what & eStretchChanged) {
562         what |= eStretchChanged;
563         stretchEffect = other.stretchEffect;
564     }
565     if (other.what & eBufferCropChanged) {
566         what |= eBufferCropChanged;
567         bufferCrop = other.bufferCrop;
568     }
569     if (other.what & eDestinationFrameChanged) {
570         what |= eDestinationFrameChanged;
571         destinationFrame = other.destinationFrame;
572     }
573     if (other.what & eDropInputModeChanged) {
574         what |= eDropInputModeChanged;
575         dropInputMode = other.dropInputMode;
576     }
577     if (other.what & eColorChanged) {
578         what |= eColorChanged;
579         color = other.color;
580     }
581     if ((other.what & what) != other.what) {
582         ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
583               "other.what=0x%" PRIu64 " what=0x%" PRIu64,
584               other.what, what);
585     }
586 }
587 
hasBufferChanges() const588 bool layer_state_t::hasBufferChanges() const {
589     return (what & layer_state_t::eBufferChanged) || (what & layer_state_t::eCachedBufferChanged);
590 }
591 
hasValidBuffer() const592 bool layer_state_t::hasValidBuffer() const {
593     return buffer || cachedBuffer.isValid();
594 }
595 
write(Parcel & output) const596 status_t layer_state_t::matrix22_t::write(Parcel& output) const {
597     SAFE_PARCEL(output.writeFloat, dsdx);
598     SAFE_PARCEL(output.writeFloat, dtdx);
599     SAFE_PARCEL(output.writeFloat, dtdy);
600     SAFE_PARCEL(output.writeFloat, dsdy);
601     return NO_ERROR;
602 }
603 
read(const Parcel & input)604 status_t layer_state_t::matrix22_t::read(const Parcel& input) {
605     SAFE_PARCEL(input.readFloat, &dsdx);
606     SAFE_PARCEL(input.readFloat, &dtdx);
607     SAFE_PARCEL(input.readFloat, &dtdy);
608     SAFE_PARCEL(input.readFloat, &dsdy);
609     return NO_ERROR;
610 }
611 
612 // ------------------------------- InputWindowCommands ----------------------------------------
613 
merge(const InputWindowCommands & other)614 bool InputWindowCommands::merge(const InputWindowCommands& other) {
615     bool changes = false;
616     changes |= !other.focusRequests.empty();
617     focusRequests.insert(focusRequests.end(), std::make_move_iterator(other.focusRequests.begin()),
618                          std::make_move_iterator(other.focusRequests.end()));
619     changes |= other.syncInputWindows && !syncInputWindows;
620     syncInputWindows |= other.syncInputWindows;
621     return changes;
622 }
623 
empty() const624 bool InputWindowCommands::empty() const {
625     bool empty = true;
626     empty = focusRequests.empty() && !syncInputWindows;
627     return empty;
628 }
629 
clear()630 void InputWindowCommands::clear() {
631     focusRequests.clear();
632     syncInputWindows = false;
633 }
634 
write(Parcel & output) const635 status_t InputWindowCommands::write(Parcel& output) const {
636     SAFE_PARCEL(output.writeParcelableVector, focusRequests);
637     SAFE_PARCEL(output.writeBool, syncInputWindows);
638     return NO_ERROR;
639 }
640 
read(const Parcel & input)641 status_t InputWindowCommands::read(const Parcel& input) {
642     SAFE_PARCEL(input.readParcelableVector, &focusRequests);
643     SAFE_PARCEL(input.readBool, &syncInputWindows);
644     return NO_ERROR;
645 }
646 
ValidateFrameRate(float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy,const char * inFunctionName,bool privileged)647 bool ValidateFrameRate(float frameRate, int8_t compatibility, int8_t changeFrameRateStrategy,
648                        const char* inFunctionName, bool privileged) {
649     const char* functionName = inFunctionName != nullptr ? inFunctionName : "call";
650     int floatClassification = std::fpclassify(frameRate);
651     if (frameRate < 0 || floatClassification == FP_INFINITE || floatClassification == FP_NAN) {
652         ALOGE("%s failed - invalid frame rate %f", functionName, frameRate);
653         return false;
654     }
655 
656     if (compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT &&
657         compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE &&
658         (!privileged || compatibility != ANATIVEWINDOW_FRAME_RATE_EXACT)) {
659         ALOGE("%s failed - invalid compatibility value %d privileged: %s", functionName,
660               compatibility, privileged ? "yes" : "no");
661         return false;
662     }
663 
664     if (changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS &&
665         changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS) {
666         ALOGE("%s failed - invalid change frame rate strategy value %d", functionName,
667               changeFrameRateStrategy);
668     }
669 
670     return true;
671 }
672 
673 // ----------------------------------------------------------------------------
674 
write(Parcel & output) const675 status_t CaptureArgs::write(Parcel& output) const {
676     SAFE_PARCEL(output.writeInt32, static_cast<int32_t>(pixelFormat));
677     SAFE_PARCEL(output.write, sourceCrop);
678     SAFE_PARCEL(output.writeFloat, frameScaleX);
679     SAFE_PARCEL(output.writeFloat, frameScaleY);
680     SAFE_PARCEL(output.writeBool, captureSecureLayers);
681     SAFE_PARCEL(output.writeInt32, uid);
682     SAFE_PARCEL(output.writeInt32, static_cast<int32_t>(dataspace));
683     SAFE_PARCEL(output.writeBool, allowProtected);
684     SAFE_PARCEL(output.writeBool, grayscale);
685     return NO_ERROR;
686 }
687 
read(const Parcel & input)688 status_t CaptureArgs::read(const Parcel& input) {
689     int32_t value = 0;
690     SAFE_PARCEL(input.readInt32, &value);
691     pixelFormat = static_cast<ui::PixelFormat>(value);
692     SAFE_PARCEL(input.read, sourceCrop);
693     SAFE_PARCEL(input.readFloat, &frameScaleX);
694     SAFE_PARCEL(input.readFloat, &frameScaleY);
695     SAFE_PARCEL(input.readBool, &captureSecureLayers);
696     SAFE_PARCEL(input.readInt32, &uid);
697     SAFE_PARCEL(input.readInt32, &value);
698     dataspace = static_cast<ui::Dataspace>(value);
699     SAFE_PARCEL(input.readBool, &allowProtected);
700     SAFE_PARCEL(input.readBool, &grayscale);
701     return NO_ERROR;
702 }
703 
write(Parcel & output) const704 status_t DisplayCaptureArgs::write(Parcel& output) const {
705     SAFE_PARCEL(CaptureArgs::write, output);
706 
707     SAFE_PARCEL(output.writeStrongBinder, displayToken);
708     SAFE_PARCEL(output.writeUint32, width);
709     SAFE_PARCEL(output.writeUint32, height);
710     SAFE_PARCEL(output.writeBool, useIdentityTransform);
711     return NO_ERROR;
712 }
713 
read(const Parcel & input)714 status_t DisplayCaptureArgs::read(const Parcel& input) {
715     SAFE_PARCEL(CaptureArgs::read, input);
716 
717     SAFE_PARCEL(input.readStrongBinder, &displayToken);
718     SAFE_PARCEL(input.readUint32, &width);
719     SAFE_PARCEL(input.readUint32, &height);
720     SAFE_PARCEL(input.readBool, &useIdentityTransform);
721     return NO_ERROR;
722 }
723 
write(Parcel & output) const724 status_t LayerCaptureArgs::write(Parcel& output) const {
725     SAFE_PARCEL(CaptureArgs::write, output);
726 
727     SAFE_PARCEL(output.writeStrongBinder, layerHandle);
728     SAFE_PARCEL(output.writeInt32, excludeHandles.size());
729     for (auto el : excludeHandles) {
730         SAFE_PARCEL(output.writeStrongBinder, el);
731     }
732     SAFE_PARCEL(output.writeBool, childrenOnly);
733     return NO_ERROR;
734 }
735 
read(const Parcel & input)736 status_t LayerCaptureArgs::read(const Parcel& input) {
737     SAFE_PARCEL(CaptureArgs::read, input);
738 
739     SAFE_PARCEL(input.readStrongBinder, &layerHandle);
740 
741     int32_t numExcludeHandles = 0;
742     SAFE_PARCEL_READ_SIZE(input.readInt32, &numExcludeHandles, input.dataSize());
743     excludeHandles.reserve(numExcludeHandles);
744     for (int i = 0; i < numExcludeHandles; i++) {
745         sp<IBinder> binder;
746         SAFE_PARCEL(input.readStrongBinder, &binder);
747         excludeHandles.emplace(binder);
748     }
749 
750     SAFE_PARCEL(input.readBool, &childrenOnly);
751     return NO_ERROR;
752 }
753 
754 }; // namespace android
755