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