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