blob: 0555acff43b4f451cb620e551fc725ac55c4d2a2 [file] [log] [blame]
Irvelc274c632016-06-13 16:44:08 -07001/*
2 * Copyright 2016 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 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Irvelc274c632016-06-13 16:44:08 -070020#undef LOG_TAG
21#define LOG_TAG "SurfaceInterceptor"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "Layer.h"
25#include "SurfaceFlinger.h"
26#include "SurfaceInterceptor.h"
Colin Cross63549382016-10-26 12:52:53 -070027
Irvelc274c632016-06-13 16:44:08 -070028#include <fstream>
29
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070030#include <android-base/file.h>
31#include <log/log.h>
32#include <utils/Trace.h>
33
Irvelc274c632016-06-13 16:44:08 -070034namespace android {
35
36// ----------------------------------------------------------------------------
Marissa Wall61c58622018-07-18 10:12:20 -070037// TODO(marissaw): add new layer state values to SurfaceInterceptor
Irvelc274c632016-06-13 16:44:08 -070038
Lloyd Pique4dccc412018-01-22 17:21:36 -080039SurfaceInterceptor::~SurfaceInterceptor() = default;
40
41namespace impl {
42
Robert Carr0d480722017-01-10 16:42:54 -080043SurfaceInterceptor::SurfaceInterceptor(SurfaceFlinger* flinger)
44 : mFlinger(flinger)
45{
46}
47
Irvelffc9efc2016-07-27 15:16:37 -070048void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
49 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
50{
Irvelc274c632016-06-13 16:44:08 -070051 if (mEnabled) {
52 return;
53 }
Irvelffc9efc2016-07-27 15:16:37 -070054 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070055 mEnabled = true;
Irvelffc9efc2016-07-27 15:16:37 -070056 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
57 saveExistingDisplaysLocked(displays);
58 saveExistingSurfacesLocked(layers);
Irvelc274c632016-06-13 16:44:08 -070059}
60
61void SurfaceInterceptor::disable() {
Irvelc274c632016-06-13 16:44:08 -070062 if (!mEnabled) {
63 return;
64 }
Irvelffc9efc2016-07-27 15:16:37 -070065 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070066 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
67 mEnabled = false;
68 status_t err(writeProtoFileLocked());
69 ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
70 ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
71 mTrace.Clear();
72}
73
Irvelffc9efc2016-07-27 15:16:37 -070074bool SurfaceInterceptor::isEnabled() {
75 return mEnabled;
76}
77
78void SurfaceInterceptor::saveExistingDisplaysLocked(
79 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
80{
81 // Caveat: The initial snapshot does not capture the power mode of the existing displays
Irvelc274c632016-06-13 16:44:08 -070082 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -070083 for (size_t i = 0 ; i < displays.size() ; i++) {
84 addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
85 addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
Irvelc274c632016-06-13 16:44:08 -070086 }
87}
88
Irvelffc9efc2016-07-27 15:16:37 -070089void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
Irvelc274c632016-06-13 16:44:08 -070090 ATRACE_CALL();
Robert Carr1f0a16a2016-10-24 16:27:39 -070091 for (const auto& l : layers) {
Dan Stoza412903f2017-04-27 13:42:17 -070092 l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -070093 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
94 addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
95 });
Irvelc274c632016-06-13 16:44:08 -070096 }
Irvelffc9efc2016-07-27 15:16:37 -070097}
98
99void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
100 const sp<const Layer>& layer)
101{
Irvelc274c632016-06-13 16:44:08 -0700102 Transaction* transaction(increment->mutable_transaction());
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700103 const uint32_t layerFlags = layer->getTransactionFlags();
104 transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
105 transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
Irvelc274c632016-06-13 16:44:08 -0700106
107 const int32_t layerId(getLayerId(layer));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108 addPositionLocked(transaction, layerId, layer->mCurrentState.active_legacy.transform.tx(),
109 layer->mCurrentState.active_legacy.transform.ty());
110 addDepthLocked(transaction, layerId, layer->mCurrentState.z);
111 addAlphaLocked(transaction, layerId, layer->mCurrentState.color.a);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700112 addTransparentRegionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 layer->mCurrentState.activeTransparentRegion_legacy);
114 addLayerStackLocked(transaction, layerId, layer->mCurrentState.layerStack);
115 addCropLocked(transaction, layerId, layer->mCurrentState.crop_legacy);
116 addCornerRadiusLocked(transaction, layerId, layer->mCurrentState.cornerRadius);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800117 addBackgroundBlurRadiusLocked(transaction, layerId, layer->mCurrentState.backgroundBlurRadius);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800118 if (layer->mCurrentState.barrierLayer_legacy != nullptr) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700119 addDeferTransactionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 layer->mCurrentState.barrierLayer_legacy.promote(),
121 layer->mCurrentState.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700122 }
Irvelc274c632016-06-13 16:44:08 -0700123 addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700124 addFlagsLocked(transaction, layerId, layer->mCurrentState.flags,
125 layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
126 layer_state_t::eLayerSecure);
127 addReparentLocked(transaction, layerId, getLayerIdFromWeakRef(layer->mCurrentParent));
128 addDetachChildrenLocked(transaction, layerId, layer->isLayerDetached());
129 addRelativeParentLocked(transaction, layerId,
130 getLayerIdFromWeakRef(layer->mCurrentState.zOrderRelativeOf),
131 layer->mCurrentState.z);
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800132 addShadowRadiusLocked(transaction, layerId, layer->mCurrentState.shadowRadius);
Winson Chung7605fb42021-06-29 15:42:56 -0700133 addTrustedOverlayLocked(transaction, layerId, layer->mDrawingState.isTrustedOverlay);
Irvelc274c632016-06-13 16:44:08 -0700134}
135
Irvelffc9efc2016-07-27 15:16:37 -0700136void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
137 const DisplayDeviceState& display)
138{
139 Transaction* transaction(increment->mutable_transaction());
140 transaction->set_synchronous(false);
141 transaction->set_animation(false);
142
Dominik Laskowski663bd282018-04-19 15:26:54 -0700143 addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
144 addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
145 addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800146 addDisplayProjectionLocked(transaction, display.sequenceId, toRotationInt(display.orientation),
147 display.viewport, display.frame);
Irvelffc9efc2016-07-27 15:16:37 -0700148}
149
Irvelc274c632016-06-13 16:44:08 -0700150status_t SurfaceInterceptor::writeProtoFileLocked() {
151 ATRACE_CALL();
Colin Cross63549382016-10-26 12:52:53 -0700152 std::string output;
153
Irvelc274c632016-06-13 16:44:08 -0700154 if (!mTrace.IsInitialized()) {
155 return NOT_ENOUGH_DATA;
156 }
Colin Cross63549382016-10-26 12:52:53 -0700157 if (!mTrace.SerializeToString(&output)) {
Irvelc274c632016-06-13 16:44:08 -0700158 return PERMISSION_DENIED;
159 }
Colin Cross63549382016-10-26 12:52:53 -0700160 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
161 return PERMISSION_DENIED;
162 }
163
Irvelc274c632016-06-13 16:44:08 -0700164 return NO_ERROR;
165}
166
Vishnu Nair456bbb22019-07-18 16:02:00 -0700167const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) const {
Irvel3017c612016-08-09 16:50:06 -0700168 const sp<const IBinder>& handle(weakHandle.promote());
Irvelc274c632016-06-13 16:44:08 -0700169 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
170 const sp<const Layer> layer(layerHandle->owner.promote());
171 // layer could be a nullptr at this point
172 return layer;
173}
174
Vishnu Nair456bbb22019-07-18 16:02:00 -0700175int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) const {
Irvelc274c632016-06-13 16:44:08 -0700176 return layer->sequence;
177}
178
Vishnu Nair456bbb22019-07-18 16:02:00 -0700179int32_t SurfaceInterceptor::getLayerIdFromWeakRef(const wp<const Layer>& layer) const {
180 if (layer == nullptr) {
181 return -1;
182 }
183 auto strongLayer = layer.promote();
184 return strongLayer == nullptr ? -1 : getLayerId(strongLayer);
185}
186
187int32_t SurfaceInterceptor::getLayerIdFromHandle(const sp<const IBinder>& handle) const {
188 if (handle == nullptr) {
189 return -1;
190 }
191 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
192 const sp<const Layer> layer(layerHandle->owner.promote());
193 return layer == nullptr ? -1 : getLayerId(layer);
194}
195
Irvelffc9efc2016-07-27 15:16:37 -0700196Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Irvelc274c632016-06-13 16:44:08 -0700197 Increment* increment(mTrace.add_increment());
Vishnu Nair45cb21a2020-03-27 17:39:24 -0700198 increment->set_time_stamp(elapsedRealtimeNano());
Irvelc274c632016-06-13 16:44:08 -0700199 return increment;
200}
201
Irvelffc9efc2016-07-27 15:16:37 -0700202SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
203 int32_t layerId)
204{
205 SurfaceChange* change(transaction->add_surface_change());
Irvelc274c632016-06-13 16:44:08 -0700206 change->set_id(layerId);
207 return change;
208}
209
Irvelffc9efc2016-07-27 15:16:37 -0700210DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700211 int32_t sequenceId)
Irvelffc9efc2016-07-27 15:16:37 -0700212{
213 DisplayChange* dispChange(transaction->add_display_change());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700214 dispChange->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700215 return dispChange;
216}
217
Irvelc274c632016-06-13 16:44:08 -0700218void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
219 protoRect->set_left(rect.left);
220 protoRect->set_top(rect.top);
221 protoRect->set_right(rect.right);
222 protoRect->set_bottom(rect.bottom);
223}
224
Irvelffc9efc2016-07-27 15:16:37 -0700225void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
226 float x, float y)
Irvelc274c632016-06-13 16:44:08 -0700227{
Irvelffc9efc2016-07-27 15:16:37 -0700228 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700229 PositionChange* posChange(change->mutable_position());
230 posChange->set_x(x);
231 posChange->set_y(y);
232}
233
Irvelffc9efc2016-07-27 15:16:37 -0700234void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
235 uint32_t z)
236{
237 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700238 LayerChange* depthChange(change->mutable_layer());
239 depthChange->set_layer(z);
240}
241
242void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
243 uint32_t h)
244{
Irvelffc9efc2016-07-27 15:16:37 -0700245 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700246 SizeChange* sizeChange(change->mutable_size());
247 sizeChange->set_w(w);
248 sizeChange->set_h(h);
249}
250
Irvelffc9efc2016-07-27 15:16:37 -0700251void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
252 float alpha)
253{
254 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700255 AlphaChange* alphaChange(change->mutable_alpha());
256 alphaChange->set_alpha(alpha);
257}
258
259void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
260 const layer_state_t::matrix22_t& matrix)
261{
Irvelffc9efc2016-07-27 15:16:37 -0700262 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700263 MatrixChange* matrixChange(change->mutable_matrix());
264 matrixChange->set_dsdx(matrix.dsdx);
265 matrixChange->set_dtdx(matrix.dtdx);
266 matrixChange->set_dsdy(matrix.dsdy);
267 matrixChange->set_dtdy(matrix.dtdy);
268}
269
Irvelffc9efc2016-07-27 15:16:37 -0700270void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
271 int32_t layerId, const Region& transRegion)
Irvelc274c632016-06-13 16:44:08 -0700272{
Irvelffc9efc2016-07-27 15:16:37 -0700273 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700274 TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
275
276 for (const auto& rect : transRegion) {
277 Rectangle* protoRect(transparentChange->add_region());
278 setProtoRectLocked(protoRect, rect);
279 }
280}
281
Vishnu Nair456bbb22019-07-18 16:02:00 -0700282void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags,
283 uint8_t mask) {
Irvelc274c632016-06-13 16:44:08 -0700284 // There can be multiple flags changed
Vishnu Nair456bbb22019-07-18 16:02:00 -0700285 if (mask & layer_state_t::eLayerHidden) {
Irvelffc9efc2016-07-27 15:16:37 -0700286 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700287 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700288 flagChange->set_hidden_flag(flags & layer_state_t::eLayerHidden);
Irvelc274c632016-06-13 16:44:08 -0700289 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700290 if (mask & layer_state_t::eLayerOpaque) {
Irvelffc9efc2016-07-27 15:16:37 -0700291 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700292 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700293 flagChange->set_opaque_flag(flags & layer_state_t::eLayerOpaque);
Irvelc274c632016-06-13 16:44:08 -0700294 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700295 if (mask & layer_state_t::eLayerSecure) {
Irvelffc9efc2016-07-27 15:16:37 -0700296 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700297 SecureFlagChange* flagChange(change->mutable_secure_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700298 flagChange->set_secure_flag(flags & layer_state_t::eLayerSecure);
Irvelc274c632016-06-13 16:44:08 -0700299 }
300}
301
302void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
303 uint32_t layerStack)
304{
Irvelffc9efc2016-07-27 15:16:37 -0700305 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700306 LayerStackChange* layerStackChange(change->mutable_layer_stack());
307 layerStackChange->set_layer_stack(layerStack);
308}
309
310void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
311 const Rect& rect)
312{
Irvelffc9efc2016-07-27 15:16:37 -0700313 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700314 CropChange* cropChange(change->mutable_crop());
315 Rectangle* protoRect(cropChange->mutable_rectangle());
316 setProtoRectLocked(protoRect, rect);
317}
318
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700319void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
320 float cornerRadius)
321{
322 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
323 CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
324 cornerRadiusChange->set_corner_radius(cornerRadius);
325}
326
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800327void SurfaceInterceptor::addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId,
328 int32_t backgroundBlurRadius) {
329 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
330 BackgroundBlurRadiusChange* blurRadiusChange(change->mutable_background_blur_radius());
331 blurRadiusChange->set_background_blur_radius(backgroundBlurRadius);
332}
333
Irvelc274c632016-06-13 16:44:08 -0700334void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Robert Carr0d480722017-01-10 16:42:54 -0800335 const sp<const Layer>& layer, uint64_t frameNumber)
Irvelc274c632016-06-13 16:44:08 -0700336{
Irvelffc9efc2016-07-27 15:16:37 -0700337 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700338 if (layer == nullptr) {
339 ALOGE("An existing layer could not be retrieved with the handle"
340 " for the deferred transaction");
341 return;
342 }
343 DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction());
344 deferTransaction->set_layer_id(getLayerId(layer));
345 deferTransaction->set_frame_number(frameNumber);
346}
347
Irvelffc9efc2016-07-27 15:16:37 -0700348void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
349 int32_t layerId, int32_t overrideScalingMode)
Irvelc274c632016-06-13 16:44:08 -0700350{
Irvelffc9efc2016-07-27 15:16:37 -0700351 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700352 OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
353 overrideChange->set_override_scaling_mode(overrideScalingMode);
354}
355
Vishnu Nair456bbb22019-07-18 16:02:00 -0700356void SurfaceInterceptor::addReparentLocked(Transaction* transaction, int32_t layerId,
357 int32_t parentId) {
358 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
359 ReparentChange* overrideChange(change->mutable_reparent());
360 overrideChange->set_parent_id(parentId);
361}
362
363void SurfaceInterceptor::addReparentChildrenLocked(Transaction* transaction, int32_t layerId,
364 int32_t parentId) {
365 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
366 ReparentChildrenChange* overrideChange(change->mutable_reparent_children());
367 overrideChange->set_parent_id(parentId);
368}
369
370void SurfaceInterceptor::addDetachChildrenLocked(Transaction* transaction, int32_t layerId,
371 bool detached) {
372 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
373 DetachChildrenChange* overrideChange(change->mutable_detach_children());
374 overrideChange->set_detach_children(detached);
375}
376
377void SurfaceInterceptor::addRelativeParentLocked(Transaction* transaction, int32_t layerId,
378 int32_t parentId, int z) {
379 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
380 RelativeParentChange* overrideChange(change->mutable_relative_parent());
381 overrideChange->set_relative_parent_id(parentId);
382 overrideChange->set_z(z);
383}
384
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800385void SurfaceInterceptor::addShadowRadiusLocked(Transaction* transaction, int32_t layerId,
386 float shadowRadius) {
387 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
388 ShadowRadiusChange* overrideChange(change->mutable_shadow_radius());
389 overrideChange->set_radius(shadowRadius);
390}
391
Winson Chung7605fb42021-06-29 15:42:56 -0700392void SurfaceInterceptor::addTrustedOverlayLocked(Transaction* transaction, int32_t layerId,
393 bool isTrustedOverlay) {
394 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
395 TrustedOverlayChange* overrideChange(change->mutable_trusted_overlay());
396 overrideChange->set_is_trusted_overlay(isTrustedOverlay);
397}
398
Irvelffc9efc2016-07-27 15:16:37 -0700399void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
Irvelc274c632016-06-13 16:44:08 -0700400 const layer_state_t& state)
401{
402 const sp<const Layer> layer(getLayer(state.surface));
403 if (layer == nullptr) {
404 ALOGE("An existing layer could not be retrieved with the surface "
405 "from the layer_state_t surface in the update transaction");
406 return;
407 }
408
409 const int32_t layerId(getLayerId(layer));
410
411 if (state.what & layer_state_t::ePositionChanged) {
412 addPositionLocked(transaction, layerId, state.x, state.y);
413 }
414 if (state.what & layer_state_t::eLayerChanged) {
415 addDepthLocked(transaction, layerId, state.z);
416 }
417 if (state.what & layer_state_t::eSizeChanged) {
418 addSizeLocked(transaction, layerId, state.w, state.h);
419 }
420 if (state.what & layer_state_t::eAlphaChanged) {
421 addAlphaLocked(transaction, layerId, state.alpha);
422 }
423 if (state.what & layer_state_t::eMatrixChanged) {
424 addMatrixLocked(transaction, layerId, state.matrix);
425 }
426 if (state.what & layer_state_t::eTransparentRegionChanged) {
427 addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
428 }
429 if (state.what & layer_state_t::eFlagsChanged) {
Vishnu Nair456bbb22019-07-18 16:02:00 -0700430 addFlagsLocked(transaction, layerId, state.flags, state.mask);
Irvelc274c632016-06-13 16:44:08 -0700431 }
432 if (state.what & layer_state_t::eLayerStackChanged) {
433 addLayerStackLocked(transaction, layerId, state.layerStack);
434 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700435 if (state.what & layer_state_t::eCropChanged_legacy) {
436 addCropLocked(transaction, layerId, state.crop_legacy);
Irvelc274c632016-06-13 16:44:08 -0700437 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700438 if (state.what & layer_state_t::eCornerRadiusChanged) {
439 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
440 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800441 if (state.what & layer_state_t::eBackgroundBlurRadiusChanged) {
442 addBackgroundBlurRadiusLocked(transaction, layerId, state.backgroundBlurRadius);
443 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700444 if (state.what & layer_state_t::eDeferTransaction_legacy) {
Robert Carr0d480722017-01-10 16:42:54 -0800445 sp<Layer> otherLayer = nullptr;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700446 if (state.barrierHandle_legacy != nullptr) {
447 otherLayer =
448 static_cast<Layer::Handle*>(state.barrierHandle_legacy.get())->owner.promote();
449 } else if (state.barrierGbp_legacy != nullptr) {
450 auto const& gbp = state.barrierGbp_legacy;
Robert Carr0d480722017-01-10 16:42:54 -0800451 if (mFlinger->authenticateSurfaceTextureLocked(gbp)) {
452 otherLayer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
453 } else {
454 ALOGE("Attempt to defer transaction to to an unrecognized GraphicBufferProducer");
455 }
456 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700457 addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700458 }
Irvelc274c632016-06-13 16:44:08 -0700459 if (state.what & layer_state_t::eOverrideScalingModeChanged) {
460 addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
461 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700462 if (state.what & layer_state_t::eReparent) {
463 addReparentLocked(transaction, layerId, getLayerIdFromHandle(state.parentHandleForChild));
464 }
465 if (state.what & layer_state_t::eReparentChildren) {
466 addReparentChildrenLocked(transaction, layerId, getLayerIdFromHandle(state.reparentHandle));
467 }
468 if (state.what & layer_state_t::eDetachChildren) {
469 addDetachChildrenLocked(transaction, layerId, true);
470 }
471 if (state.what & layer_state_t::eRelativeLayerChanged) {
472 addRelativeParentLocked(transaction, layerId,
473 getLayerIdFromHandle(state.relativeLayerHandle), state.z);
474 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800475 if (state.what & layer_state_t::eShadowRadiusChanged) {
476 addShadowRadiusLocked(transaction, layerId, state.shadowRadius);
477 }
Winson Chung7605fb42021-06-29 15:42:56 -0700478 if (state.what & layer_state_t::eTrustedOverlayChanged) {
479 addTrustedOverlayLocked(transaction, layerId, state.isTrustedOverlay);
480 }
Irvelc274c632016-06-13 16:44:08 -0700481}
482
Irvelffc9efc2016-07-27 15:16:37 -0700483void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700484 const DisplayState& state, int32_t sequenceId)
Irvelc274c632016-06-13 16:44:08 -0700485{
Irvelffc9efc2016-07-27 15:16:37 -0700486 if (state.what & DisplayState::eSurfaceChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700487 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
Irvelffc9efc2016-07-27 15:16:37 -0700488 }
489 if (state.what & DisplayState::eLayerStackChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700490 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
Irvelffc9efc2016-07-27 15:16:37 -0700491 }
492 if (state.what & DisplayState::eDisplaySizeChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700493 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
Irvelffc9efc2016-07-27 15:16:37 -0700494 }
495 if (state.what & DisplayState::eDisplayProjectionChanged) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800496 addDisplayProjectionLocked(transaction, sequenceId, toRotationInt(state.orientation),
497 state.viewport, state.frame);
Irvelc274c632016-06-13 16:44:08 -0700498 }
499}
500
Irvelffc9efc2016-07-27 15:16:37 -0700501void SurfaceInterceptor::addTransactionLocked(Increment* increment,
502 const Vector<ComposerState>& stateUpdates,
503 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
504 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags)
505{
506 Transaction* transaction(increment->mutable_transaction());
507 transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
508 transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
509 for (const auto& compState: stateUpdates) {
510 addSurfaceChangesLocked(transaction, compState.state);
511 }
512 for (const auto& disp: changedDisplays) {
513 ssize_t dpyIdx = displays.indexOfKey(disp.token);
514 if (dpyIdx >= 0) {
515 const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
Dominik Laskowski663bd282018-04-19 15:26:54 -0700516 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700517 }
518 }
Irvelc274c632016-06-13 16:44:08 -0700519}
520
Irvelffc9efc2016-07-27 15:16:37 -0700521void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
522 const sp<const Layer>& layer)
523{
524 SurfaceCreation* creation(increment->mutable_surface_creation());
525 creation->set_id(getLayerId(layer));
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700526 creation->set_name(layer->getName());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800527 creation->set_w(layer->mCurrentState.active_legacy.w);
528 creation->set_h(layer->mCurrentState.active_legacy.h);
Irvelc274c632016-06-13 16:44:08 -0700529}
530
Irvelffc9efc2016-07-27 15:16:37 -0700531void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
532 const sp<const Layer>& layer)
533{
534 SurfaceDeletion* deletion(increment->mutable_surface_deletion());
535 deletion->set_id(getLayerId(layer));
536}
537
Rob Carra79435b2020-03-06 14:46:07 -0800538void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, int32_t layerId,
Irvelc274c632016-06-13 16:44:08 -0700539 uint32_t width, uint32_t height, uint64_t frameNumber)
540{
541 BufferUpdate* update(increment->mutable_buffer_update());
Rob Carra79435b2020-03-06 14:46:07 -0800542 update->set_id(layerId);
Irvelc274c632016-06-13 16:44:08 -0700543 update->set_w(width);
544 update->set_h(height);
545 update->set_frame_number(frameNumber);
546}
547
Irvelffc9efc2016-07-27 15:16:37 -0700548void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
Irvelc274c632016-06-13 16:44:08 -0700549 VSyncEvent* event(increment->mutable_vsync_event());
550 event->set_when(timestamp);
551}
552
Dominik Laskowski663bd282018-04-19 15:26:54 -0700553void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700554 const sp<const IGraphicBufferProducer>& surface)
Irvelc274c632016-06-13 16:44:08 -0700555{
Irvelffc9efc2016-07-27 15:16:37 -0700556 if (surface == nullptr) {
Irvelc274c632016-06-13 16:44:08 -0700557 return;
558 }
Irvelffc9efc2016-07-27 15:16:37 -0700559 uint64_t bufferQueueId = 0;
560 status_t err(surface->getUniqueId(&bufferQueueId));
561 if (err == NO_ERROR) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700562 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700563 DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
564 surfaceChange->set_buffer_queue_id(bufferQueueId);
565 surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
566 }
567 else {
568 ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
569 strerror(-err));
570 }
Irvelc274c632016-06-13 16:44:08 -0700571}
572
Irvelffc9efc2016-07-27 15:16:37 -0700573void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700574 int32_t sequenceId, uint32_t layerStack)
Irvelffc9efc2016-07-27 15:16:37 -0700575{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700576 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700577 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
578 layerStackChange->set_layer_stack(layerStack);
579}
580
Dominik Laskowski663bd282018-04-19 15:26:54 -0700581void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700582 uint32_t w, uint32_t h)
583{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700584 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700585 SizeChange* sizeChange(dispChange->mutable_size());
586 sizeChange->set_w(w);
587 sizeChange->set_h(h);
588}
589
590void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700591 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
Irvelffc9efc2016-07-27 15:16:37 -0700592{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700593 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700594 ProjectionChange* projectionChange(dispChange->mutable_projection());
595 projectionChange->set_orientation(orientation);
596 Rectangle* viewportRect(projectionChange->mutable_viewport());
597 setProtoRectLocked(viewportRect, viewport);
598 Rectangle* frameRect(projectionChange->mutable_frame());
599 setProtoRectLocked(frameRect, frame);
600}
601
602void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
603 const DisplayDeviceState& info)
604{
605 DisplayCreation* creation(increment->mutable_display_creation());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700606 creation->set_id(info.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700607 creation->set_name(info.displayName);
Irvelffc9efc2016-07-27 15:16:37 -0700608 creation->set_is_secure(info.isSecure);
Dominik Laskowski55c85402020-01-21 16:25:47 -0800609 if (info.physical) {
610 creation->set_display_id(info.physical->id.value);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700611 }
Irvelffc9efc2016-07-27 15:16:37 -0700612}
613
Dominik Laskowski663bd282018-04-19 15:26:54 -0700614void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700615 DisplayDeletion* deletion(increment->mutable_display_deletion());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700616 deletion->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700617}
618
Dominik Laskowski663bd282018-04-19 15:26:54 -0700619void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700620 int32_t mode)
621{
622 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700623 powerModeUpdate->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700624 powerModeUpdate->set_mode(mode);
625}
626
627void SurfaceInterceptor::saveTransaction(const Vector<ComposerState>& stateUpdates,
628 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
629 const Vector<DisplayState>& changedDisplays, uint32_t flags)
630{
631 if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
632 return;
633 }
Irvelc274c632016-06-13 16:44:08 -0700634 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700635 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
636 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
637 flags);
638}
639
640void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700641 if (!mEnabled || layer == nullptr) {
642 return;
643 }
Irvelc274c632016-06-13 16:44:08 -0700644 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700645 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
646 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
647}
648
649void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700650 if (!mEnabled || layer == nullptr) {
651 return;
652 }
Irvelffc9efc2016-07-27 15:16:37 -0700653 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700654 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700655 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
Irvelc274c632016-06-13 16:44:08 -0700656}
657
Rob Carra79435b2020-03-06 14:46:07 -0800658/**
659 * Here we pass the layer by ID instead of by sp<> since this is called without
660 * holding the state-lock from a Binder thread. If we required the caller
661 * to pass 'this' by sp<> the temporary sp<> constructed could end up
662 * being the last reference and we might accidentally destroy the Layer
663 * from this binder thread.
664 */
665void SurfaceInterceptor::saveBufferUpdate(int32_t layerId, uint32_t width,
Irvelc274c632016-06-13 16:44:08 -0700666 uint32_t height, uint64_t frameNumber)
667{
Rob Carra79435b2020-03-06 14:46:07 -0800668 if (!mEnabled) {
Irvelc274c632016-06-13 16:44:08 -0700669 return;
670 }
Irvelffc9efc2016-07-27 15:16:37 -0700671 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700672 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Rob Carra79435b2020-03-06 14:46:07 -0800673 addBufferUpdateLocked(createTraceIncrementLocked(), layerId, width, height, frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700674}
675
676void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
677 if (!mEnabled) {
678 return;
679 }
680 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700681 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
Irvelc274c632016-06-13 16:44:08 -0700682}
683
Irvelffc9efc2016-07-27 15:16:37 -0700684void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
685 if (!mEnabled) {
686 return;
687 }
688 ATRACE_CALL();
689 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
690 addDisplayCreationLocked(createTraceIncrementLocked(), info);
691}
692
Dominik Laskowski663bd282018-04-19 15:26:54 -0700693void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700694 if (!mEnabled) {
695 return;
696 }
697 ATRACE_CALL();
698 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700699 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700700}
701
Dominik Laskowski663bd282018-04-19 15:26:54 -0700702void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
Irvelffc9efc2016-07-27 15:16:37 -0700703 if (!mEnabled) {
704 return;
705 }
706 ATRACE_CALL();
707 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700708 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
Irvelffc9efc2016-07-27 15:16:37 -0700709}
710
Lloyd Pique4dccc412018-01-22 17:21:36 -0800711} // namespace impl
Irvelc274c632016-06-13 16:44:08 -0700712} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800713
714// TODO(b/129481165): remove the #pragma below and fix conversion issues
715#pragma clang diagnostic pop // ignored "-Wconversion"