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