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