• 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 <inttypes.h>
20 
21 #include <utils/Errors.h>
22 #include <binder/Parcel.h>
23 #include <gui/ISurfaceComposerClient.h>
24 #include <gui/IGraphicBufferProducer.h>
25 #include <gui/LayerState.h>
26 
27 #include <cmath>
28 
29 namespace android {
30 
write(Parcel & output) const31 status_t layer_state_t::write(Parcel& output) const
32 {
33     output.writeStrongBinder(surface);
34     output.writeUint64(what);
35     output.writeFloat(x);
36     output.writeFloat(y);
37     output.writeInt32(z);
38     output.writeUint32(w);
39     output.writeUint32(h);
40     output.writeUint32(layerStack);
41     output.writeFloat(alpha);
42     output.writeUint32(flags);
43     output.writeUint32(mask);
44     *reinterpret_cast<layer_state_t::matrix22_t *>(
45             output.writeInplace(sizeof(layer_state_t::matrix22_t))) = matrix;
46     output.write(crop_legacy);
47     output.writeStrongBinder(barrierHandle_legacy);
48     output.writeStrongBinder(reparentHandle);
49     output.writeUint64(frameNumber_legacy);
50     output.writeInt32(overrideScalingMode);
51     output.writeStrongBinder(IInterface::asBinder(barrierGbp_legacy));
52     output.writeStrongBinder(relativeLayerHandle);
53     output.writeStrongBinder(parentHandleForChild);
54     output.writeFloat(color.r);
55     output.writeFloat(color.g);
56     output.writeFloat(color.b);
57 #ifndef NO_INPUT
58     inputInfo.write(output);
59 #endif
60     output.write(transparentRegion);
61     output.writeUint32(transform);
62     output.writeBool(transformToDisplayInverse);
63     output.write(crop);
64     output.write(frame);
65     if (buffer) {
66         output.writeBool(true);
67         output.write(*buffer);
68     } else {
69         output.writeBool(false);
70     }
71     if (acquireFence) {
72         output.writeBool(true);
73         output.write(*acquireFence);
74     } else {
75         output.writeBool(false);
76     }
77     output.writeUint32(static_cast<uint32_t>(dataspace));
78     output.write(hdrMetadata);
79     output.write(surfaceDamageRegion);
80     output.writeInt32(api);
81     if (sidebandStream) {
82         output.writeBool(true);
83         output.writeNativeHandle(sidebandStream->handle());
84     } else {
85         output.writeBool(false);
86     }
87 
88     memcpy(output.writeInplace(16 * sizeof(float)),
89            colorTransform.asArray(), 16 * sizeof(float));
90     output.writeFloat(cornerRadius);
91     output.writeUint32(backgroundBlurRadius);
92     output.writeStrongBinder(cachedBuffer.token.promote());
93     output.writeUint64(cachedBuffer.id);
94     output.writeParcelable(metadata);
95 
96     output.writeFloat(bgColorAlpha);
97     output.writeUint32(static_cast<uint32_t>(bgColorDataspace));
98     output.writeBool(colorSpaceAgnostic);
99 
100     auto err = output.writeVectorSize(listeners);
101     if (err) {
102         return err;
103     }
104 
105     for (auto listener : listeners) {
106         err = output.writeStrongBinder(listener.transactionCompletedListener);
107         if (err) {
108             return err;
109         }
110         err = output.writeInt64Vector(listener.callbackIds);
111         if (err) {
112             return err;
113         }
114     }
115     output.writeFloat(shadowRadius);
116     output.writeInt32(frameRateSelectionPriority);
117     output.writeFloat(frameRate);
118     output.writeByte(frameRateCompatibility);
119     output.writeUint32(fixedTransformHint);
120     return NO_ERROR;
121 }
122 
read(const Parcel & input)123 status_t layer_state_t::read(const Parcel& input)
124 {
125     surface = input.readStrongBinder();
126     what = input.readUint64();
127     x = input.readFloat();
128     y = input.readFloat();
129     z = input.readInt32();
130     w = input.readUint32();
131     h = input.readUint32();
132     layerStack = input.readUint32();
133     alpha = input.readFloat();
134     flags = static_cast<uint8_t>(input.readUint32());
135     mask = static_cast<uint8_t>(input.readUint32());
136     const void* matrix_data = input.readInplace(sizeof(layer_state_t::matrix22_t));
137     if (matrix_data) {
138         matrix = *reinterpret_cast<layer_state_t::matrix22_t const *>(matrix_data);
139     } else {
140         return BAD_VALUE;
141     }
142     input.read(crop_legacy);
143     barrierHandle_legacy = input.readStrongBinder();
144     reparentHandle = input.readStrongBinder();
145     frameNumber_legacy = input.readUint64();
146     overrideScalingMode = input.readInt32();
147     barrierGbp_legacy = interface_cast<IGraphicBufferProducer>(input.readStrongBinder());
148     relativeLayerHandle = input.readStrongBinder();
149     parentHandleForChild = input.readStrongBinder();
150     color.r = input.readFloat();
151     color.g = input.readFloat();
152     color.b = input.readFloat();
153 
154 #ifndef NO_INPUT
155     inputInfo = InputWindowInfo::read(input);
156 #endif
157 
158     input.read(transparentRegion);
159     transform = input.readUint32();
160     transformToDisplayInverse = input.readBool();
161     input.read(crop);
162     input.read(frame);
163     buffer = new GraphicBuffer();
164     if (input.readBool()) {
165         input.read(*buffer);
166     }
167     acquireFence = new Fence();
168     if (input.readBool()) {
169         input.read(*acquireFence);
170     }
171     dataspace = static_cast<ui::Dataspace>(input.readUint32());
172     input.read(hdrMetadata);
173     input.read(surfaceDamageRegion);
174     api = input.readInt32();
175     if (input.readBool()) {
176         sidebandStream = NativeHandle::create(input.readNativeHandle(), true);
177     }
178 
179     colorTransform = mat4(static_cast<const float*>(input.readInplace(16 * sizeof(float))));
180     cornerRadius = input.readFloat();
181     backgroundBlurRadius = input.readUint32();
182     cachedBuffer.token = input.readStrongBinder();
183     cachedBuffer.id = input.readUint64();
184     input.readParcelable(&metadata);
185 
186     bgColorAlpha = input.readFloat();
187     bgColorDataspace = static_cast<ui::Dataspace>(input.readUint32());
188     colorSpaceAgnostic = input.readBool();
189 
190     int32_t numListeners = input.readInt32();
191     listeners.clear();
192     for (int i = 0; i < numListeners; i++) {
193         auto listener = input.readStrongBinder();
194         std::vector<CallbackId> callbackIds;
195         input.readInt64Vector(&callbackIds);
196         listeners.emplace_back(listener, callbackIds);
197     }
198     shadowRadius = input.readFloat();
199     frameRateSelectionPriority = input.readInt32();
200     frameRate = input.readFloat();
201     frameRateCompatibility = input.readByte();
202     fixedTransformHint = static_cast<ui::Transform::RotationFlags>(input.readUint32());
203     return NO_ERROR;
204 }
205 
write(Parcel & output) const206 status_t ComposerState::write(Parcel& output) const {
207     return state.write(output);
208 }
209 
read(const Parcel & input)210 status_t ComposerState::read(const Parcel& input) {
211     return state.read(input);
212 }
213 
214 
DisplayState()215 DisplayState::DisplayState() :
216     what(0),
217     layerStack(0),
218     viewport(Rect::EMPTY_RECT),
219     frame(Rect::EMPTY_RECT),
220     width(0),
221     height(0) {
222 }
223 
write(Parcel & output) const224 status_t DisplayState::write(Parcel& output) const {
225     output.writeStrongBinder(token);
226     output.writeStrongBinder(IInterface::asBinder(surface));
227     output.writeUint32(what);
228     output.writeUint32(layerStack);
229     output.writeUint32(toRotationInt(orientation));
230     output.write(viewport);
231     output.write(frame);
232     output.writeUint32(width);
233     output.writeUint32(height);
234     return NO_ERROR;
235 }
236 
read(const Parcel & input)237 status_t DisplayState::read(const Parcel& input) {
238     token = input.readStrongBinder();
239     surface = interface_cast<IGraphicBufferProducer>(input.readStrongBinder());
240     what = input.readUint32();
241     layerStack = input.readUint32();
242     orientation = ui::toRotation(input.readUint32());
243     input.read(viewport);
244     input.read(frame);
245     width = input.readUint32();
246     height = input.readUint32();
247     return NO_ERROR;
248 }
249 
merge(const DisplayState & other)250 void DisplayState::merge(const DisplayState& other) {
251     if (other.what & eSurfaceChanged) {
252         what |= eSurfaceChanged;
253         surface = other.surface;
254     }
255     if (other.what & eLayerStackChanged) {
256         what |= eLayerStackChanged;
257         layerStack = other.layerStack;
258     }
259     if (other.what & eDisplayProjectionChanged) {
260         what |= eDisplayProjectionChanged;
261         orientation = other.orientation;
262         viewport = other.viewport;
263         frame = other.frame;
264     }
265     if (other.what & eDisplaySizeChanged) {
266         what |= eDisplaySizeChanged;
267         width = other.width;
268         height = other.height;
269     }
270 }
271 
merge(const layer_state_t & other)272 void layer_state_t::merge(const layer_state_t& other) {
273     if (other.what & ePositionChanged) {
274         what |= ePositionChanged;
275         x = other.x;
276         y = other.y;
277     }
278     if (other.what & eLayerChanged) {
279         what |= eLayerChanged;
280         what &= ~eRelativeLayerChanged;
281         z = other.z;
282     }
283     if (other.what & eSizeChanged) {
284         what |= eSizeChanged;
285         w = other.w;
286         h = other.h;
287     }
288     if (other.what & eAlphaChanged) {
289         what |= eAlphaChanged;
290         alpha = other.alpha;
291     }
292     if (other.what & eMatrixChanged) {
293         what |= eMatrixChanged;
294         matrix = other.matrix;
295     }
296     if (other.what & eTransparentRegionChanged) {
297         what |= eTransparentRegionChanged;
298         transparentRegion = other.transparentRegion;
299     }
300     if (other.what & eFlagsChanged) {
301         what |= eFlagsChanged;
302         flags &= ~other.mask;
303         flags |= (other.flags & other.mask);
304         mask |= other.mask;
305     }
306     if (other.what & eLayerStackChanged) {
307         what |= eLayerStackChanged;
308         layerStack = other.layerStack;
309     }
310     if (other.what & eCropChanged_legacy) {
311         what |= eCropChanged_legacy;
312         crop_legacy = other.crop_legacy;
313     }
314     if (other.what & eCornerRadiusChanged) {
315         what |= eCornerRadiusChanged;
316         cornerRadius = other.cornerRadius;
317     }
318     if (other.what & eBackgroundBlurRadiusChanged) {
319         what |= eBackgroundBlurRadiusChanged;
320         backgroundBlurRadius = other.backgroundBlurRadius;
321     }
322     if (other.what & eDeferTransaction_legacy) {
323         what |= eDeferTransaction_legacy;
324         barrierHandle_legacy = other.barrierHandle_legacy;
325         barrierGbp_legacy = other.barrierGbp_legacy;
326         frameNumber_legacy = other.frameNumber_legacy;
327     }
328     if (other.what & eOverrideScalingModeChanged) {
329         what |= eOverrideScalingModeChanged;
330         overrideScalingMode = other.overrideScalingMode;
331     }
332     if (other.what & eReparentChildren) {
333         what |= eReparentChildren;
334         reparentHandle = other.reparentHandle;
335     }
336     if (other.what & eDetachChildren) {
337         what |= eDetachChildren;
338     }
339     if (other.what & eRelativeLayerChanged) {
340         what |= eRelativeLayerChanged;
341         what &= ~eLayerChanged;
342         z = other.z;
343         relativeLayerHandle = other.relativeLayerHandle;
344     }
345     if (other.what & eReparent) {
346         what |= eReparent;
347         parentHandleForChild = other.parentHandleForChild;
348     }
349     if (other.what & eDestroySurface) {
350         what |= eDestroySurface;
351     }
352     if (other.what & eTransformChanged) {
353         what |= eTransformChanged;
354         transform = other.transform;
355     }
356     if (other.what & eTransformToDisplayInverseChanged) {
357         what |= eTransformToDisplayInverseChanged;
358         transformToDisplayInverse = other.transformToDisplayInverse;
359     }
360     if (other.what & eCropChanged) {
361         what |= eCropChanged;
362         crop = other.crop;
363     }
364     if (other.what & eFrameChanged) {
365         what |= eFrameChanged;
366         frame = other.frame;
367     }
368     if (other.what & eBufferChanged) {
369         what |= eBufferChanged;
370         buffer = other.buffer;
371     }
372     if (other.what & eAcquireFenceChanged) {
373         what |= eAcquireFenceChanged;
374         acquireFence = other.acquireFence;
375     }
376     if (other.what & eDataspaceChanged) {
377         what |= eDataspaceChanged;
378         dataspace = other.dataspace;
379     }
380     if (other.what & eHdrMetadataChanged) {
381         what |= eHdrMetadataChanged;
382         hdrMetadata = other.hdrMetadata;
383     }
384     if (other.what & eSurfaceDamageRegionChanged) {
385         what |= eSurfaceDamageRegionChanged;
386         surfaceDamageRegion = other.surfaceDamageRegion;
387     }
388     if (other.what & eApiChanged) {
389         what |= eApiChanged;
390         api = other.api;
391     }
392     if (other.what & eSidebandStreamChanged) {
393         what |= eSidebandStreamChanged;
394         sidebandStream = other.sidebandStream;
395     }
396     if (other.what & eColorTransformChanged) {
397         what |= eColorTransformChanged;
398         colorTransform = other.colorTransform;
399     }
400     if (other.what & eHasListenerCallbacksChanged) {
401         what |= eHasListenerCallbacksChanged;
402     }
403 
404 #ifndef NO_INPUT
405     if (other.what & eInputInfoChanged) {
406         what |= eInputInfoChanged;
407         inputInfo = other.inputInfo;
408     }
409 #endif
410 
411     if (other.what & eCachedBufferChanged) {
412         what |= eCachedBufferChanged;
413         cachedBuffer = other.cachedBuffer;
414     }
415     if (other.what & eBackgroundColorChanged) {
416         what |= eBackgroundColorChanged;
417         color = other.color;
418         bgColorAlpha = other.bgColorAlpha;
419         bgColorDataspace = other.bgColorDataspace;
420     }
421     if (other.what & eMetadataChanged) {
422         what |= eMetadataChanged;
423         metadata.merge(other.metadata);
424     }
425     if (other.what & eShadowRadiusChanged) {
426         what |= eShadowRadiusChanged;
427         shadowRadius = other.shadowRadius;
428     }
429     if (other.what & eFrameRateSelectionPriority) {
430         what |= eFrameRateSelectionPriority;
431         frameRateSelectionPriority = other.frameRateSelectionPriority;
432     }
433     if (other.what & eFrameRateChanged) {
434         what |= eFrameRateChanged;
435         frameRate = other.frameRate;
436         frameRateCompatibility = other.frameRateCompatibility;
437     }
438     if (other.what & eFixedTransformHintChanged) {
439         what |= eFixedTransformHintChanged;
440         fixedTransformHint = other.fixedTransformHint;
441     }
442     if ((other.what & what) != other.what) {
443         ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
444               "other.what=0x%" PRIu64 " what=0x%" PRIu64,
445               other.what, what);
446     }
447 }
448 
449 // ------------------------------- InputWindowCommands ----------------------------------------
450 
merge(const InputWindowCommands & other)451 void InputWindowCommands::merge(const InputWindowCommands& other) {
452     syncInputWindows |= other.syncInputWindows;
453 }
454 
clear()455 void InputWindowCommands::clear() {
456     syncInputWindows = false;
457 }
458 
write(Parcel & output) const459 void InputWindowCommands::write(Parcel& output) const {
460     output.writeBool(syncInputWindows);
461 }
462 
read(const Parcel & input)463 void InputWindowCommands::read(const Parcel& input) {
464     syncInputWindows = input.readBool();
465 }
466 
ValidateFrameRate(float frameRate,int8_t compatibility,const char * inFunctionName)467 bool ValidateFrameRate(float frameRate, int8_t compatibility, const char* inFunctionName) {
468     const char* functionName = inFunctionName != nullptr ? inFunctionName : "call";
469     int floatClassification = std::fpclassify(frameRate);
470     if (frameRate < 0 || floatClassification == FP_INFINITE || floatClassification == FP_NAN) {
471         ALOGE("%s failed - invalid frame rate %f", functionName, frameRate);
472         return false;
473     }
474 
475     if (compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT &&
476         compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE) {
477         ALOGE("%s failed - invalid compatibility value %d", functionName, compatibility);
478         return false;
479     }
480 
481     return true;
482 }
483 
484 }; // namespace android
485