1 /*
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 */
16
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 #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"
27
28 #include <fstream>
29
30 #include <android-base/file.h>
31 #include <log/log.h>
32 #include <utils/Trace.h>
33
34 namespace android {
35
36 // ----------------------------------------------------------------------------
37 // TODO(marissaw): add new layer state values to SurfaceInterceptor
38
39 SurfaceInterceptor::~SurfaceInterceptor() = default;
40
41 namespace impl {
42
SurfaceInterceptor(SurfaceFlinger * flinger)43 SurfaceInterceptor::SurfaceInterceptor(SurfaceFlinger* flinger)
44 : mFlinger(flinger)
45 {
46 }
47
enable(const SortedVector<sp<Layer>> & layers,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays)48 void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
49 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
50 {
51 if (mEnabled) {
52 return;
53 }
54 ATRACE_CALL();
55 mEnabled = true;
56 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
57 saveExistingDisplaysLocked(displays);
58 saveExistingSurfacesLocked(layers);
59 }
60
disable()61 void SurfaceInterceptor::disable() {
62 if (!mEnabled) {
63 return;
64 }
65 ATRACE_CALL();
66 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
isEnabled()74 bool SurfaceInterceptor::isEnabled() {
75 return mEnabled;
76 }
77
saveExistingDisplaysLocked(const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays)78 void 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
82 ATRACE_CALL();
83 for (size_t i = 0 ; i < displays.size() ; i++) {
84 addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
85 addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
86 }
87 }
88
saveExistingSurfacesLocked(const SortedVector<sp<Layer>> & layers)89 void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
90 ATRACE_CALL();
91 for (const auto& l : layers) {
92 l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
93 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
94 addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
95 });
96 }
97 }
98
addInitialSurfaceStateLocked(Increment * increment,const sp<const Layer> & layer)99 void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
100 const sp<const Layer>& layer)
101 {
102 Transaction* transaction(increment->mutable_transaction());
103 const uint32_t layerFlags = layer->getTransactionFlags();
104 transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
105 transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
106
107 const int32_t layerId(getLayerId(layer));
108 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);
112 addTransparentRegionLocked(transaction, layerId,
113 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);
117 addBackgroundBlurRadiusLocked(transaction, layerId, layer->mCurrentState.backgroundBlurRadius);
118 if (layer->mCurrentState.barrierLayer_legacy != nullptr) {
119 addDeferTransactionLocked(transaction, layerId,
120 layer->mCurrentState.barrierLayer_legacy.promote(),
121 layer->mCurrentState.frameNumber_legacy);
122 }
123 addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
124 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);
132 addShadowRadiusLocked(transaction, layerId, layer->mCurrentState.shadowRadius);
133 }
134
addInitialDisplayStateLocked(Increment * increment,const DisplayDeviceState & display)135 void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
136 const DisplayDeviceState& display)
137 {
138 Transaction* transaction(increment->mutable_transaction());
139 transaction->set_synchronous(false);
140 transaction->set_animation(false);
141
142 addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
143 addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
144 addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
145 addDisplayProjectionLocked(transaction, display.sequenceId, toRotationInt(display.orientation),
146 display.viewport, display.frame);
147 }
148
writeProtoFileLocked()149 status_t SurfaceInterceptor::writeProtoFileLocked() {
150 ATRACE_CALL();
151 std::string output;
152
153 if (!mTrace.IsInitialized()) {
154 return NOT_ENOUGH_DATA;
155 }
156 if (!mTrace.SerializeToString(&output)) {
157 return PERMISSION_DENIED;
158 }
159 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
160 return PERMISSION_DENIED;
161 }
162
163 return NO_ERROR;
164 }
165
getLayer(const wp<const IBinder> & weakHandle) const166 const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) const {
167 const sp<const IBinder>& handle(weakHandle.promote());
168 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
169 const sp<const Layer> layer(layerHandle->owner.promote());
170 // layer could be a nullptr at this point
171 return layer;
172 }
173
getLayerId(const sp<const Layer> & layer) const174 int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) const {
175 return layer->sequence;
176 }
177
getLayerIdFromWeakRef(const wp<const Layer> & layer) const178 int32_t SurfaceInterceptor::getLayerIdFromWeakRef(const wp<const Layer>& layer) const {
179 if (layer == nullptr) {
180 return -1;
181 }
182 auto strongLayer = layer.promote();
183 return strongLayer == nullptr ? -1 : getLayerId(strongLayer);
184 }
185
getLayerIdFromHandle(const sp<const IBinder> & handle) const186 int32_t SurfaceInterceptor::getLayerIdFromHandle(const sp<const IBinder>& handle) const {
187 if (handle == nullptr) {
188 return -1;
189 }
190 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
191 const sp<const Layer> layer(layerHandle->owner.promote());
192 return layer == nullptr ? -1 : getLayerId(layer);
193 }
194
createTraceIncrementLocked()195 Increment* SurfaceInterceptor::createTraceIncrementLocked() {
196 Increment* increment(mTrace.add_increment());
197 increment->set_time_stamp(elapsedRealtimeNano());
198 return increment;
199 }
200
createSurfaceChangeLocked(Transaction * transaction,int32_t layerId)201 SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
202 int32_t layerId)
203 {
204 SurfaceChange* change(transaction->add_surface_change());
205 change->set_id(layerId);
206 return change;
207 }
208
createDisplayChangeLocked(Transaction * transaction,int32_t sequenceId)209 DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
210 int32_t sequenceId)
211 {
212 DisplayChange* dispChange(transaction->add_display_change());
213 dispChange->set_id(sequenceId);
214 return dispChange;
215 }
216
setProtoRectLocked(Rectangle * protoRect,const Rect & rect)217 void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
218 protoRect->set_left(rect.left);
219 protoRect->set_top(rect.top);
220 protoRect->set_right(rect.right);
221 protoRect->set_bottom(rect.bottom);
222 }
223
addPositionLocked(Transaction * transaction,int32_t layerId,float x,float y)224 void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
225 float x, float y)
226 {
227 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
228 PositionChange* posChange(change->mutable_position());
229 posChange->set_x(x);
230 posChange->set_y(y);
231 }
232
addDepthLocked(Transaction * transaction,int32_t layerId,uint32_t z)233 void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
234 uint32_t z)
235 {
236 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
237 LayerChange* depthChange(change->mutable_layer());
238 depthChange->set_layer(z);
239 }
240
addSizeLocked(Transaction * transaction,int32_t layerId,uint32_t w,uint32_t h)241 void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
242 uint32_t h)
243 {
244 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
245 SizeChange* sizeChange(change->mutable_size());
246 sizeChange->set_w(w);
247 sizeChange->set_h(h);
248 }
249
addAlphaLocked(Transaction * transaction,int32_t layerId,float alpha)250 void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
251 float alpha)
252 {
253 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
254 AlphaChange* alphaChange(change->mutable_alpha());
255 alphaChange->set_alpha(alpha);
256 }
257
addMatrixLocked(Transaction * transaction,int32_t layerId,const layer_state_t::matrix22_t & matrix)258 void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
259 const layer_state_t::matrix22_t& matrix)
260 {
261 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
262 MatrixChange* matrixChange(change->mutable_matrix());
263 matrixChange->set_dsdx(matrix.dsdx);
264 matrixChange->set_dtdx(matrix.dtdx);
265 matrixChange->set_dsdy(matrix.dsdy);
266 matrixChange->set_dtdy(matrix.dtdy);
267 }
268
addTransparentRegionLocked(Transaction * transaction,int32_t layerId,const Region & transRegion)269 void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
270 int32_t layerId, const Region& transRegion)
271 {
272 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
273 TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
274
275 for (const auto& rect : transRegion) {
276 Rectangle* protoRect(transparentChange->add_region());
277 setProtoRectLocked(protoRect, rect);
278 }
279 }
280
addFlagsLocked(Transaction * transaction,int32_t layerId,uint8_t flags,uint8_t mask)281 void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags,
282 uint8_t mask) {
283 // There can be multiple flags changed
284 if (mask & layer_state_t::eLayerHidden) {
285 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
286 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
287 flagChange->set_hidden_flag(flags & layer_state_t::eLayerHidden);
288 }
289 if (mask & layer_state_t::eLayerOpaque) {
290 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
291 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
292 flagChange->set_opaque_flag(flags & layer_state_t::eLayerOpaque);
293 }
294 if (mask & layer_state_t::eLayerSecure) {
295 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
296 SecureFlagChange* flagChange(change->mutable_secure_flag());
297 flagChange->set_secure_flag(flags & layer_state_t::eLayerSecure);
298 }
299 }
300
addLayerStackLocked(Transaction * transaction,int32_t layerId,uint32_t layerStack)301 void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
302 uint32_t layerStack)
303 {
304 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
305 LayerStackChange* layerStackChange(change->mutable_layer_stack());
306 layerStackChange->set_layer_stack(layerStack);
307 }
308
addCropLocked(Transaction * transaction,int32_t layerId,const Rect & rect)309 void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
310 const Rect& rect)
311 {
312 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
313 CropChange* cropChange(change->mutable_crop());
314 Rectangle* protoRect(cropChange->mutable_rectangle());
315 setProtoRectLocked(protoRect, rect);
316 }
317
addCornerRadiusLocked(Transaction * transaction,int32_t layerId,float cornerRadius)318 void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
319 float cornerRadius)
320 {
321 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
322 CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
323 cornerRadiusChange->set_corner_radius(cornerRadius);
324 }
325
addBackgroundBlurRadiusLocked(Transaction * transaction,int32_t layerId,int32_t backgroundBlurRadius)326 void SurfaceInterceptor::addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId,
327 int32_t backgroundBlurRadius) {
328 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
329 BackgroundBlurRadiusChange* blurRadiusChange(change->mutable_background_blur_radius());
330 blurRadiusChange->set_background_blur_radius(backgroundBlurRadius);
331 }
332
addDeferTransactionLocked(Transaction * transaction,int32_t layerId,const sp<const Layer> & layer,uint64_t frameNumber)333 void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
334 const sp<const Layer>& layer, uint64_t frameNumber)
335 {
336 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
337 if (layer == nullptr) {
338 ALOGE("An existing layer could not be retrieved with the handle"
339 " for the deferred transaction");
340 return;
341 }
342 DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction());
343 deferTransaction->set_layer_id(getLayerId(layer));
344 deferTransaction->set_frame_number(frameNumber);
345 }
346
addOverrideScalingModeLocked(Transaction * transaction,int32_t layerId,int32_t overrideScalingMode)347 void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
348 int32_t layerId, int32_t overrideScalingMode)
349 {
350 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
351 OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
352 overrideChange->set_override_scaling_mode(overrideScalingMode);
353 }
354
addReparentLocked(Transaction * transaction,int32_t layerId,int32_t parentId)355 void SurfaceInterceptor::addReparentLocked(Transaction* transaction, int32_t layerId,
356 int32_t parentId) {
357 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
358 ReparentChange* overrideChange(change->mutable_reparent());
359 overrideChange->set_parent_id(parentId);
360 }
361
addReparentChildrenLocked(Transaction * transaction,int32_t layerId,int32_t parentId)362 void SurfaceInterceptor::addReparentChildrenLocked(Transaction* transaction, int32_t layerId,
363 int32_t parentId) {
364 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
365 ReparentChildrenChange* overrideChange(change->mutable_reparent_children());
366 overrideChange->set_parent_id(parentId);
367 }
368
addDetachChildrenLocked(Transaction * transaction,int32_t layerId,bool detached)369 void SurfaceInterceptor::addDetachChildrenLocked(Transaction* transaction, int32_t layerId,
370 bool detached) {
371 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
372 DetachChildrenChange* overrideChange(change->mutable_detach_children());
373 overrideChange->set_detach_children(detached);
374 }
375
addRelativeParentLocked(Transaction * transaction,int32_t layerId,int32_t parentId,int z)376 void SurfaceInterceptor::addRelativeParentLocked(Transaction* transaction, int32_t layerId,
377 int32_t parentId, int z) {
378 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
379 RelativeParentChange* overrideChange(change->mutable_relative_parent());
380 overrideChange->set_relative_parent_id(parentId);
381 overrideChange->set_z(z);
382 }
383
addShadowRadiusLocked(Transaction * transaction,int32_t layerId,float shadowRadius)384 void SurfaceInterceptor::addShadowRadiusLocked(Transaction* transaction, int32_t layerId,
385 float shadowRadius) {
386 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
387 ShadowRadiusChange* overrideChange(change->mutable_shadow_radius());
388 overrideChange->set_radius(shadowRadius);
389 }
390
addSurfaceChangesLocked(Transaction * transaction,const layer_state_t & state)391 void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
392 const layer_state_t& state)
393 {
394 const sp<const Layer> layer(getLayer(state.surface));
395 if (layer == nullptr) {
396 ALOGE("An existing layer could not be retrieved with the surface "
397 "from the layer_state_t surface in the update transaction");
398 return;
399 }
400
401 const int32_t layerId(getLayerId(layer));
402
403 if (state.what & layer_state_t::ePositionChanged) {
404 addPositionLocked(transaction, layerId, state.x, state.y);
405 }
406 if (state.what & layer_state_t::eLayerChanged) {
407 addDepthLocked(transaction, layerId, state.z);
408 }
409 if (state.what & layer_state_t::eSizeChanged) {
410 addSizeLocked(transaction, layerId, state.w, state.h);
411 }
412 if (state.what & layer_state_t::eAlphaChanged) {
413 addAlphaLocked(transaction, layerId, state.alpha);
414 }
415 if (state.what & layer_state_t::eMatrixChanged) {
416 addMatrixLocked(transaction, layerId, state.matrix);
417 }
418 if (state.what & layer_state_t::eTransparentRegionChanged) {
419 addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
420 }
421 if (state.what & layer_state_t::eFlagsChanged) {
422 addFlagsLocked(transaction, layerId, state.flags, state.mask);
423 }
424 if (state.what & layer_state_t::eLayerStackChanged) {
425 addLayerStackLocked(transaction, layerId, state.layerStack);
426 }
427 if (state.what & layer_state_t::eCropChanged_legacy) {
428 addCropLocked(transaction, layerId, state.crop_legacy);
429 }
430 if (state.what & layer_state_t::eCornerRadiusChanged) {
431 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
432 }
433 if (state.what & layer_state_t::eBackgroundBlurRadiusChanged) {
434 addBackgroundBlurRadiusLocked(transaction, layerId, state.backgroundBlurRadius);
435 }
436 if (state.what & layer_state_t::eDeferTransaction_legacy) {
437 sp<Layer> otherLayer = nullptr;
438 if (state.barrierHandle_legacy != nullptr) {
439 otherLayer =
440 static_cast<Layer::Handle*>(state.barrierHandle_legacy.get())->owner.promote();
441 } else if (state.barrierGbp_legacy != nullptr) {
442 auto const& gbp = state.barrierGbp_legacy;
443 if (mFlinger->authenticateSurfaceTextureLocked(gbp)) {
444 otherLayer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
445 } else {
446 ALOGE("Attempt to defer transaction to to an unrecognized GraphicBufferProducer");
447 }
448 }
449 addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
450 }
451 if (state.what & layer_state_t::eOverrideScalingModeChanged) {
452 addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
453 }
454 if (state.what & layer_state_t::eReparent) {
455 addReparentLocked(transaction, layerId, getLayerIdFromHandle(state.parentHandleForChild));
456 }
457 if (state.what & layer_state_t::eReparentChildren) {
458 addReparentChildrenLocked(transaction, layerId, getLayerIdFromHandle(state.reparentHandle));
459 }
460 if (state.what & layer_state_t::eDetachChildren) {
461 addDetachChildrenLocked(transaction, layerId, true);
462 }
463 if (state.what & layer_state_t::eRelativeLayerChanged) {
464 addRelativeParentLocked(transaction, layerId,
465 getLayerIdFromHandle(state.relativeLayerHandle), state.z);
466 }
467 if (state.what & layer_state_t::eShadowRadiusChanged) {
468 addShadowRadiusLocked(transaction, layerId, state.shadowRadius);
469 }
470 }
471
addDisplayChangesLocked(Transaction * transaction,const DisplayState & state,int32_t sequenceId)472 void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
473 const DisplayState& state, int32_t sequenceId)
474 {
475 if (state.what & DisplayState::eSurfaceChanged) {
476 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
477 }
478 if (state.what & DisplayState::eLayerStackChanged) {
479 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
480 }
481 if (state.what & DisplayState::eDisplaySizeChanged) {
482 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
483 }
484 if (state.what & DisplayState::eDisplayProjectionChanged) {
485 addDisplayProjectionLocked(transaction, sequenceId, toRotationInt(state.orientation),
486 state.viewport, state.frame);
487 }
488 }
489
addTransactionLocked(Increment * increment,const Vector<ComposerState> & stateUpdates,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays,const Vector<DisplayState> & changedDisplays,uint32_t transactionFlags)490 void SurfaceInterceptor::addTransactionLocked(Increment* increment,
491 const Vector<ComposerState>& stateUpdates,
492 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
493 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags)
494 {
495 Transaction* transaction(increment->mutable_transaction());
496 transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
497 transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
498 for (const auto& compState: stateUpdates) {
499 addSurfaceChangesLocked(transaction, compState.state);
500 }
501 for (const auto& disp: changedDisplays) {
502 ssize_t dpyIdx = displays.indexOfKey(disp.token);
503 if (dpyIdx >= 0) {
504 const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
505 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
506 }
507 }
508 }
509
addSurfaceCreationLocked(Increment * increment,const sp<const Layer> & layer)510 void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
511 const sp<const Layer>& layer)
512 {
513 SurfaceCreation* creation(increment->mutable_surface_creation());
514 creation->set_id(getLayerId(layer));
515 creation->set_name(layer->getName());
516 creation->set_w(layer->mCurrentState.active_legacy.w);
517 creation->set_h(layer->mCurrentState.active_legacy.h);
518 }
519
addSurfaceDeletionLocked(Increment * increment,const sp<const Layer> & layer)520 void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
521 const sp<const Layer>& layer)
522 {
523 SurfaceDeletion* deletion(increment->mutable_surface_deletion());
524 deletion->set_id(getLayerId(layer));
525 }
526
addBufferUpdateLocked(Increment * increment,int32_t layerId,uint32_t width,uint32_t height,uint64_t frameNumber)527 void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, int32_t layerId,
528 uint32_t width, uint32_t height, uint64_t frameNumber)
529 {
530 BufferUpdate* update(increment->mutable_buffer_update());
531 update->set_id(layerId);
532 update->set_w(width);
533 update->set_h(height);
534 update->set_frame_number(frameNumber);
535 }
536
addVSyncUpdateLocked(Increment * increment,nsecs_t timestamp)537 void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
538 VSyncEvent* event(increment->mutable_vsync_event());
539 event->set_when(timestamp);
540 }
541
addDisplaySurfaceLocked(Transaction * transaction,int32_t sequenceId,const sp<const IGraphicBufferProducer> & surface)542 void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
543 const sp<const IGraphicBufferProducer>& surface)
544 {
545 if (surface == nullptr) {
546 return;
547 }
548 uint64_t bufferQueueId = 0;
549 status_t err(surface->getUniqueId(&bufferQueueId));
550 if (err == NO_ERROR) {
551 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
552 DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
553 surfaceChange->set_buffer_queue_id(bufferQueueId);
554 surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
555 }
556 else {
557 ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
558 strerror(-err));
559 }
560 }
561
addDisplayLayerStackLocked(Transaction * transaction,int32_t sequenceId,uint32_t layerStack)562 void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
563 int32_t sequenceId, uint32_t layerStack)
564 {
565 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
566 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
567 layerStackChange->set_layer_stack(layerStack);
568 }
569
addDisplaySizeLocked(Transaction * transaction,int32_t sequenceId,uint32_t w,uint32_t h)570 void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
571 uint32_t w, uint32_t h)
572 {
573 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
574 SizeChange* sizeChange(dispChange->mutable_size());
575 sizeChange->set_w(w);
576 sizeChange->set_h(h);
577 }
578
addDisplayProjectionLocked(Transaction * transaction,int32_t sequenceId,int32_t orientation,const Rect & viewport,const Rect & frame)579 void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
580 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
581 {
582 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
583 ProjectionChange* projectionChange(dispChange->mutable_projection());
584 projectionChange->set_orientation(orientation);
585 Rectangle* viewportRect(projectionChange->mutable_viewport());
586 setProtoRectLocked(viewportRect, viewport);
587 Rectangle* frameRect(projectionChange->mutable_frame());
588 setProtoRectLocked(frameRect, frame);
589 }
590
addDisplayCreationLocked(Increment * increment,const DisplayDeviceState & info)591 void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
592 const DisplayDeviceState& info)
593 {
594 DisplayCreation* creation(increment->mutable_display_creation());
595 creation->set_id(info.sequenceId);
596 creation->set_name(info.displayName);
597 creation->set_is_secure(info.isSecure);
598 if (info.physical) {
599 creation->set_display_id(info.physical->id.value);
600 }
601 }
602
addDisplayDeletionLocked(Increment * increment,int32_t sequenceId)603 void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
604 DisplayDeletion* deletion(increment->mutable_display_deletion());
605 deletion->set_id(sequenceId);
606 }
607
addPowerModeUpdateLocked(Increment * increment,int32_t sequenceId,int32_t mode)608 void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
609 int32_t mode)
610 {
611 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
612 powerModeUpdate->set_id(sequenceId);
613 powerModeUpdate->set_mode(mode);
614 }
615
saveTransaction(const Vector<ComposerState> & stateUpdates,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays,const Vector<DisplayState> & changedDisplays,uint32_t flags)616 void SurfaceInterceptor::saveTransaction(const Vector<ComposerState>& stateUpdates,
617 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
618 const Vector<DisplayState>& changedDisplays, uint32_t flags)
619 {
620 if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
621 return;
622 }
623 ATRACE_CALL();
624 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
625 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
626 flags);
627 }
628
saveSurfaceCreation(const sp<const Layer> & layer)629 void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
630 if (!mEnabled || layer == nullptr) {
631 return;
632 }
633 ATRACE_CALL();
634 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
635 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
636 }
637
saveSurfaceDeletion(const sp<const Layer> & layer)638 void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
639 if (!mEnabled || layer == nullptr) {
640 return;
641 }
642 ATRACE_CALL();
643 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
644 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
645 }
646
647 /**
648 * Here we pass the layer by ID instead of by sp<> since this is called without
649 * holding the state-lock from a Binder thread. If we required the caller
650 * to pass 'this' by sp<> the temporary sp<> constructed could end up
651 * being the last reference and we might accidentally destroy the Layer
652 * from this binder thread.
653 */
saveBufferUpdate(int32_t layerId,uint32_t width,uint32_t height,uint64_t frameNumber)654 void SurfaceInterceptor::saveBufferUpdate(int32_t layerId, uint32_t width,
655 uint32_t height, uint64_t frameNumber)
656 {
657 if (!mEnabled) {
658 return;
659 }
660 ATRACE_CALL();
661 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
662 addBufferUpdateLocked(createTraceIncrementLocked(), layerId, width, height, frameNumber);
663 }
664
saveVSyncEvent(nsecs_t timestamp)665 void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
666 if (!mEnabled) {
667 return;
668 }
669 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
670 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
671 }
672
saveDisplayCreation(const DisplayDeviceState & info)673 void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
674 if (!mEnabled) {
675 return;
676 }
677 ATRACE_CALL();
678 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
679 addDisplayCreationLocked(createTraceIncrementLocked(), info);
680 }
681
saveDisplayDeletion(int32_t sequenceId)682 void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
683 if (!mEnabled) {
684 return;
685 }
686 ATRACE_CALL();
687 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
688 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
689 }
690
savePowerModeUpdate(int32_t sequenceId,int32_t mode)691 void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
692 if (!mEnabled) {
693 return;
694 }
695 ATRACE_CALL();
696 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
697 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
698 }
699
700 } // namespace impl
701 } // namespace android
702
703 // TODO(b/129481165): remove the #pragma below and fix conversion issues
704 #pragma clang diagnostic pop // ignored "-Wconversion"
705