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 #undef LOG_TAG
17 #define LOG_TAG "SurfaceInterceptor"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 
20 #include "Layer.h"
21 #include "SurfaceFlinger.h"
22 #include "SurfaceInterceptor.h"
23 
24 #include <fstream>
25 
26 #include <android-base/file.h>
27 #include <log/log.h>
28 #include <utils/Trace.h>
29 
30 namespace android {
31 
32 // ----------------------------------------------------------------------------
33 // TODO(marissaw): add new layer state values to SurfaceInterceptor
34 
35 SurfaceInterceptor::~SurfaceInterceptor() = default;
36 
37 namespace impl {
38 
SurfaceInterceptor(SurfaceFlinger * flinger)39 SurfaceInterceptor::SurfaceInterceptor(SurfaceFlinger* flinger)
40     :   mFlinger(flinger)
41 {
42 }
43 
enable(const SortedVector<sp<Layer>> & layers,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays)44 void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
45         const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
46 {
47     if (mEnabled) {
48         return;
49     }
50     ATRACE_CALL();
51     mEnabled = true;
52     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
53     saveExistingDisplaysLocked(displays);
54     saveExistingSurfacesLocked(layers);
55 }
56 
disable()57 void SurfaceInterceptor::disable() {
58     if (!mEnabled) {
59         return;
60     }
61     ATRACE_CALL();
62     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
63     mEnabled = false;
64     status_t err(writeProtoFileLocked());
65     ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
66     ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
67     mTrace.Clear();
68 }
69 
isEnabled()70 bool SurfaceInterceptor::isEnabled() {
71     return mEnabled;
72 }
73 
saveExistingDisplaysLocked(const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays)74 void SurfaceInterceptor::saveExistingDisplaysLocked(
75         const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
76 {
77     // Caveat: The initial snapshot does not capture the power mode of the existing displays
78     ATRACE_CALL();
79     for (size_t i = 0 ; i < displays.size() ; i++) {
80         addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
81         addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
82     }
83 }
84 
saveExistingSurfacesLocked(const SortedVector<sp<Layer>> & layers)85 void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
86     ATRACE_CALL();
87     for (const auto& l : layers) {
88         l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
89             addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
90             addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
91         });
92     }
93 }
94 
addInitialSurfaceStateLocked(Increment * increment,const sp<const Layer> & layer)95 void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
96         const sp<const Layer>& layer)
97 {
98     Transaction* transaction(increment->mutable_transaction());
99     const uint32_t layerFlags = layer->getTransactionFlags();
100     transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
101     transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
102 
103     const int32_t layerId(getLayerId(layer));
104     addPositionLocked(transaction, layerId, layer->mCurrentState.active_legacy.transform.tx(),
105                       layer->mCurrentState.active_legacy.transform.ty());
106     addDepthLocked(transaction, layerId, layer->mCurrentState.z);
107     addAlphaLocked(transaction, layerId, layer->mCurrentState.color.a);
108     addTransparentRegionLocked(transaction, layerId,
109                                layer->mCurrentState.activeTransparentRegion_legacy);
110     addLayerStackLocked(transaction, layerId, layer->mCurrentState.layerStack);
111     addCropLocked(transaction, layerId, layer->mCurrentState.crop_legacy);
112     addCornerRadiusLocked(transaction, layerId, layer->mCurrentState.cornerRadius);
113     if (layer->mCurrentState.barrierLayer_legacy != nullptr) {
114         addDeferTransactionLocked(transaction, layerId,
115                                   layer->mCurrentState.barrierLayer_legacy.promote(),
116                                   layer->mCurrentState.frameNumber_legacy);
117     }
118     addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
119     addFlagsLocked(transaction, layerId, layer->mCurrentState.flags);
120 }
121 
addInitialDisplayStateLocked(Increment * increment,const DisplayDeviceState & display)122 void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
123         const DisplayDeviceState& display)
124 {
125     Transaction* transaction(increment->mutable_transaction());
126     transaction->set_synchronous(false);
127     transaction->set_animation(false);
128 
129     addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
130     addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
131     addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
132     addDisplayProjectionLocked(transaction, display.sequenceId, display.orientation,
133             display.viewport, display.frame);
134 }
135 
writeProtoFileLocked()136 status_t SurfaceInterceptor::writeProtoFileLocked() {
137     ATRACE_CALL();
138     std::string output;
139 
140     if (!mTrace.IsInitialized()) {
141         return NOT_ENOUGH_DATA;
142     }
143     if (!mTrace.SerializeToString(&output)) {
144         return PERMISSION_DENIED;
145     }
146     if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
147         return PERMISSION_DENIED;
148     }
149 
150     return NO_ERROR;
151 }
152 
getLayer(const wp<const IBinder> & weakHandle)153 const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) {
154     const sp<const IBinder>& handle(weakHandle.promote());
155     const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
156     const sp<const Layer> layer(layerHandle->owner.promote());
157     // layer could be a nullptr at this point
158     return layer;
159 }
160 
getLayerName(const sp<const Layer> & layer)161 const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) {
162     return layer->getName().string();
163 }
164 
getLayerId(const sp<const Layer> & layer)165 int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) {
166     return layer->sequence;
167 }
168 
createTraceIncrementLocked()169 Increment* SurfaceInterceptor::createTraceIncrementLocked() {
170     Increment* increment(mTrace.add_increment());
171     increment->set_time_stamp(systemTime());
172     return increment;
173 }
174 
createSurfaceChangeLocked(Transaction * transaction,int32_t layerId)175 SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
176         int32_t layerId)
177 {
178     SurfaceChange* change(transaction->add_surface_change());
179     change->set_id(layerId);
180     return change;
181 }
182 
createDisplayChangeLocked(Transaction * transaction,int32_t sequenceId)183 DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
184         int32_t sequenceId)
185 {
186     DisplayChange* dispChange(transaction->add_display_change());
187     dispChange->set_id(sequenceId);
188     return dispChange;
189 }
190 
setProtoRectLocked(Rectangle * protoRect,const Rect & rect)191 void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
192     protoRect->set_left(rect.left);
193     protoRect->set_top(rect.top);
194     protoRect->set_right(rect.right);
195     protoRect->set_bottom(rect.bottom);
196 }
197 
addPositionLocked(Transaction * transaction,int32_t layerId,float x,float y)198 void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
199         float x, float y)
200 {
201     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
202     PositionChange* posChange(change->mutable_position());
203     posChange->set_x(x);
204     posChange->set_y(y);
205 }
206 
addDepthLocked(Transaction * transaction,int32_t layerId,uint32_t z)207 void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
208         uint32_t z)
209 {
210     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
211     LayerChange* depthChange(change->mutable_layer());
212     depthChange->set_layer(z);
213 }
214 
addSizeLocked(Transaction * transaction,int32_t layerId,uint32_t w,uint32_t h)215 void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
216         uint32_t h)
217 {
218     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
219     SizeChange* sizeChange(change->mutable_size());
220     sizeChange->set_w(w);
221     sizeChange->set_h(h);
222 }
223 
addAlphaLocked(Transaction * transaction,int32_t layerId,float alpha)224 void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
225         float alpha)
226 {
227     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
228     AlphaChange* alphaChange(change->mutable_alpha());
229     alphaChange->set_alpha(alpha);
230 }
231 
addMatrixLocked(Transaction * transaction,int32_t layerId,const layer_state_t::matrix22_t & matrix)232 void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
233         const layer_state_t::matrix22_t& matrix)
234 {
235     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
236     MatrixChange* matrixChange(change->mutable_matrix());
237     matrixChange->set_dsdx(matrix.dsdx);
238     matrixChange->set_dtdx(matrix.dtdx);
239     matrixChange->set_dsdy(matrix.dsdy);
240     matrixChange->set_dtdy(matrix.dtdy);
241 }
242 
addTransparentRegionLocked(Transaction * transaction,int32_t layerId,const Region & transRegion)243 void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
244         int32_t layerId, const Region& transRegion)
245 {
246     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
247     TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
248 
249     for (const auto& rect : transRegion) {
250         Rectangle* protoRect(transparentChange->add_region());
251         setProtoRectLocked(protoRect, rect);
252     }
253 }
254 
addFlagsLocked(Transaction * transaction,int32_t layerId,uint8_t flags)255 void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId,
256         uint8_t flags)
257 {
258     // There can be multiple flags changed
259     if (flags & layer_state_t::eLayerHidden) {
260         SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
261         HiddenFlagChange* flagChange(change->mutable_hidden_flag());
262         flagChange->set_hidden_flag(true);
263     }
264     if (flags & layer_state_t::eLayerOpaque) {
265         SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
266         OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
267         flagChange->set_opaque_flag(true);
268     }
269     if (flags & layer_state_t::eLayerSecure) {
270         SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
271         SecureFlagChange* flagChange(change->mutable_secure_flag());
272         flagChange->set_secure_flag(true);
273     }
274 }
275 
addLayerStackLocked(Transaction * transaction,int32_t layerId,uint32_t layerStack)276 void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
277         uint32_t layerStack)
278 {
279     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
280     LayerStackChange* layerStackChange(change->mutable_layer_stack());
281     layerStackChange->set_layer_stack(layerStack);
282 }
283 
addCropLocked(Transaction * transaction,int32_t layerId,const Rect & rect)284 void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
285         const Rect& rect)
286 {
287     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
288     CropChange* cropChange(change->mutable_crop());
289     Rectangle* protoRect(cropChange->mutable_rectangle());
290     setProtoRectLocked(protoRect, rect);
291 }
292 
addCornerRadiusLocked(Transaction * transaction,int32_t layerId,float cornerRadius)293 void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
294                                        float cornerRadius)
295 {
296     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
297     CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
298     cornerRadiusChange->set_corner_radius(cornerRadius);
299 }
300 
addDeferTransactionLocked(Transaction * transaction,int32_t layerId,const sp<const Layer> & layer,uint64_t frameNumber)301 void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
302         const sp<const Layer>& layer, uint64_t frameNumber)
303 {
304     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
305     if (layer == nullptr) {
306         ALOGE("An existing layer could not be retrieved with the handle"
307                 " for the deferred transaction");
308         return;
309     }
310     DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction());
311     deferTransaction->set_layer_id(getLayerId(layer));
312     deferTransaction->set_frame_number(frameNumber);
313 }
314 
addOverrideScalingModeLocked(Transaction * transaction,int32_t layerId,int32_t overrideScalingMode)315 void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
316         int32_t layerId, int32_t overrideScalingMode)
317 {
318     SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
319     OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
320     overrideChange->set_override_scaling_mode(overrideScalingMode);
321 }
322 
addSurfaceChangesLocked(Transaction * transaction,const layer_state_t & state)323 void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
324         const layer_state_t& state)
325 {
326     const sp<const Layer> layer(getLayer(state.surface));
327     if (layer == nullptr) {
328         ALOGE("An existing layer could not be retrieved with the surface "
329                 "from the layer_state_t surface in the update transaction");
330         return;
331     }
332 
333     const int32_t layerId(getLayerId(layer));
334 
335     if (state.what & layer_state_t::ePositionChanged) {
336         addPositionLocked(transaction, layerId, state.x, state.y);
337     }
338     if (state.what & layer_state_t::eLayerChanged) {
339         addDepthLocked(transaction, layerId, state.z);
340     }
341     if (state.what & layer_state_t::eSizeChanged) {
342         addSizeLocked(transaction, layerId, state.w, state.h);
343     }
344     if (state.what & layer_state_t::eAlphaChanged) {
345         addAlphaLocked(transaction, layerId, state.alpha);
346     }
347     if (state.what & layer_state_t::eMatrixChanged) {
348         addMatrixLocked(transaction, layerId, state.matrix);
349     }
350     if (state.what & layer_state_t::eTransparentRegionChanged) {
351         addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
352     }
353     if (state.what & layer_state_t::eFlagsChanged) {
354         addFlagsLocked(transaction, layerId, state.flags);
355     }
356     if (state.what & layer_state_t::eLayerStackChanged) {
357         addLayerStackLocked(transaction, layerId, state.layerStack);
358     }
359     if (state.what & layer_state_t::eCropChanged_legacy) {
360         addCropLocked(transaction, layerId, state.crop_legacy);
361     }
362     if (state.what & layer_state_t::eCornerRadiusChanged) {
363         addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
364     }
365     if (state.what & layer_state_t::eDeferTransaction_legacy) {
366         sp<Layer> otherLayer = nullptr;
367         if (state.barrierHandle_legacy != nullptr) {
368             otherLayer =
369                     static_cast<Layer::Handle*>(state.barrierHandle_legacy.get())->owner.promote();
370         } else if (state.barrierGbp_legacy != nullptr) {
371             auto const& gbp = state.barrierGbp_legacy;
372             if (mFlinger->authenticateSurfaceTextureLocked(gbp)) {
373                 otherLayer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
374             } else {
375                 ALOGE("Attempt to defer transaction to to an unrecognized GraphicBufferProducer");
376             }
377         }
378         addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
379     }
380     if (state.what & layer_state_t::eOverrideScalingModeChanged) {
381         addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
382     }
383 }
384 
addDisplayChangesLocked(Transaction * transaction,const DisplayState & state,int32_t sequenceId)385 void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
386         const DisplayState& state, int32_t sequenceId)
387 {
388     if (state.what & DisplayState::eSurfaceChanged) {
389         addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
390     }
391     if (state.what & DisplayState::eLayerStackChanged) {
392         addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
393     }
394     if (state.what & DisplayState::eDisplaySizeChanged) {
395         addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
396     }
397     if (state.what & DisplayState::eDisplayProjectionChanged) {
398         addDisplayProjectionLocked(transaction, sequenceId, state.orientation, state.viewport,
399                 state.frame);
400     }
401 }
402 
addTransactionLocked(Increment * increment,const Vector<ComposerState> & stateUpdates,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays,const Vector<DisplayState> & changedDisplays,uint32_t transactionFlags)403 void SurfaceInterceptor::addTransactionLocked(Increment* increment,
404         const Vector<ComposerState>& stateUpdates,
405         const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
406         const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags)
407 {
408     Transaction* transaction(increment->mutable_transaction());
409     transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
410     transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
411     for (const auto& compState: stateUpdates) {
412         addSurfaceChangesLocked(transaction, compState.state);
413     }
414     for (const auto& disp: changedDisplays) {
415         ssize_t dpyIdx = displays.indexOfKey(disp.token);
416         if (dpyIdx >= 0) {
417             const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
418             addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
419         }
420     }
421 }
422 
addSurfaceCreationLocked(Increment * increment,const sp<const Layer> & layer)423 void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
424         const sp<const Layer>& layer)
425 {
426     SurfaceCreation* creation(increment->mutable_surface_creation());
427     creation->set_id(getLayerId(layer));
428     creation->set_name(getLayerName(layer));
429     creation->set_w(layer->mCurrentState.active_legacy.w);
430     creation->set_h(layer->mCurrentState.active_legacy.h);
431 }
432 
addSurfaceDeletionLocked(Increment * increment,const sp<const Layer> & layer)433 void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
434         const sp<const Layer>& layer)
435 {
436     SurfaceDeletion* deletion(increment->mutable_surface_deletion());
437     deletion->set_id(getLayerId(layer));
438 }
439 
addBufferUpdateLocked(Increment * increment,const sp<const Layer> & layer,uint32_t width,uint32_t height,uint64_t frameNumber)440 void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer,
441         uint32_t width, uint32_t height, uint64_t frameNumber)
442 {
443     BufferUpdate* update(increment->mutable_buffer_update());
444     update->set_id(getLayerId(layer));
445     update->set_w(width);
446     update->set_h(height);
447     update->set_frame_number(frameNumber);
448 }
449 
addVSyncUpdateLocked(Increment * increment,nsecs_t timestamp)450 void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
451     VSyncEvent* event(increment->mutable_vsync_event());
452     event->set_when(timestamp);
453 }
454 
addDisplaySurfaceLocked(Transaction * transaction,int32_t sequenceId,const sp<const IGraphicBufferProducer> & surface)455 void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
456         const sp<const IGraphicBufferProducer>& surface)
457 {
458     if (surface == nullptr) {
459         return;
460     }
461     uint64_t bufferQueueId = 0;
462     status_t err(surface->getUniqueId(&bufferQueueId));
463     if (err == NO_ERROR) {
464         DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
465         DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
466         surfaceChange->set_buffer_queue_id(bufferQueueId);
467         surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
468     }
469     else {
470         ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
471                 strerror(-err));
472     }
473 }
474 
addDisplayLayerStackLocked(Transaction * transaction,int32_t sequenceId,uint32_t layerStack)475 void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
476         int32_t sequenceId, uint32_t layerStack)
477 {
478     DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
479     LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
480     layerStackChange->set_layer_stack(layerStack);
481 }
482 
addDisplaySizeLocked(Transaction * transaction,int32_t sequenceId,uint32_t w,uint32_t h)483 void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
484         uint32_t w, uint32_t h)
485 {
486     DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
487     SizeChange* sizeChange(dispChange->mutable_size());
488     sizeChange->set_w(w);
489     sizeChange->set_h(h);
490 }
491 
addDisplayProjectionLocked(Transaction * transaction,int32_t sequenceId,int32_t orientation,const Rect & viewport,const Rect & frame)492 void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
493         int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
494 {
495     DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
496     ProjectionChange* projectionChange(dispChange->mutable_projection());
497     projectionChange->set_orientation(orientation);
498     Rectangle* viewportRect(projectionChange->mutable_viewport());
499     setProtoRectLocked(viewportRect, viewport);
500     Rectangle* frameRect(projectionChange->mutable_frame());
501     setProtoRectLocked(frameRect, frame);
502 }
503 
addDisplayCreationLocked(Increment * increment,const DisplayDeviceState & info)504 void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
505         const DisplayDeviceState& info)
506 {
507     DisplayCreation* creation(increment->mutable_display_creation());
508     creation->set_id(info.sequenceId);
509     creation->set_name(info.displayName);
510     creation->set_is_secure(info.isSecure);
511     if (info.displayId) {
512         creation->set_display_id(info.displayId->value);
513     }
514 }
515 
addDisplayDeletionLocked(Increment * increment,int32_t sequenceId)516 void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
517     DisplayDeletion* deletion(increment->mutable_display_deletion());
518     deletion->set_id(sequenceId);
519 }
520 
addPowerModeUpdateLocked(Increment * increment,int32_t sequenceId,int32_t mode)521 void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
522         int32_t mode)
523 {
524     PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
525     powerModeUpdate->set_id(sequenceId);
526     powerModeUpdate->set_mode(mode);
527 }
528 
saveTransaction(const Vector<ComposerState> & stateUpdates,const DefaultKeyedVector<wp<IBinder>,DisplayDeviceState> & displays,const Vector<DisplayState> & changedDisplays,uint32_t flags)529 void SurfaceInterceptor::saveTransaction(const Vector<ComposerState>& stateUpdates,
530         const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
531         const Vector<DisplayState>& changedDisplays, uint32_t flags)
532 {
533     if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
534         return;
535     }
536     ATRACE_CALL();
537     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
538     addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
539             flags);
540 }
541 
saveSurfaceCreation(const sp<const Layer> & layer)542 void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
543     if (!mEnabled || layer == nullptr) {
544         return;
545     }
546     ATRACE_CALL();
547     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
548     addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
549 }
550 
saveSurfaceDeletion(const sp<const Layer> & layer)551 void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
552     if (!mEnabled || layer == nullptr) {
553         return;
554     }
555     ATRACE_CALL();
556     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
557     addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
558 }
559 
saveBufferUpdate(const sp<const Layer> & layer,uint32_t width,uint32_t height,uint64_t frameNumber)560 void SurfaceInterceptor::saveBufferUpdate(const sp<const Layer>& layer, uint32_t width,
561         uint32_t height, uint64_t frameNumber)
562 {
563     if (!mEnabled || layer == nullptr) {
564         return;
565     }
566     ATRACE_CALL();
567     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
568     addBufferUpdateLocked(createTraceIncrementLocked(), layer, width, height, frameNumber);
569 }
570 
saveVSyncEvent(nsecs_t timestamp)571 void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
572     if (!mEnabled) {
573         return;
574     }
575     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
576     addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
577 }
578 
saveDisplayCreation(const DisplayDeviceState & info)579 void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
580     if (!mEnabled) {
581         return;
582     }
583     ATRACE_CALL();
584     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
585     addDisplayCreationLocked(createTraceIncrementLocked(), info);
586 }
587 
saveDisplayDeletion(int32_t sequenceId)588 void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
589     if (!mEnabled) {
590         return;
591     }
592     ATRACE_CALL();
593     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
594     addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
595 }
596 
savePowerModeUpdate(int32_t sequenceId,int32_t mode)597 void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
598     if (!mEnabled) {
599         return;
600     }
601     ATRACE_CALL();
602     std::lock_guard<std::mutex> protoGuard(mTraceMutex);
603     addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
604 }
605 
606 } // namespace impl
607 } // namespace android
608