blob: 7bfe0338f79dca74ddd130215e951127035e6007 [file] [log] [blame]
Irvelc274c632016-06-13 16:44:08 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Irvelc274c632016-06-13 16:44:08 -070016#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"
Colin Cross63549382016-10-26 12:52:53 -070023
Irvelc274c632016-06-13 16:44:08 -070024#include <fstream>
25
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070026#include <android-base/file.h>
27#include <log/log.h>
28#include <utils/Trace.h>
29
Irvelc274c632016-06-13 16:44:08 -070030namespace android {
31
32// ----------------------------------------------------------------------------
Marissa Wall61c58622018-07-18 10:12:20 -070033// TODO(marissaw): add new layer state values to SurfaceInterceptor
Irvelc274c632016-06-13 16:44:08 -070034
Lloyd Pique4dccc412018-01-22 17:21:36 -080035SurfaceInterceptor::~SurfaceInterceptor() = default;
36
37namespace impl {
38
Robert Carr0d480722017-01-10 16:42:54 -080039SurfaceInterceptor::SurfaceInterceptor(SurfaceFlinger* flinger)
40 : mFlinger(flinger)
41{
42}
43
Irvelffc9efc2016-07-27 15:16:37 -070044void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
45 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
46{
Irvelc274c632016-06-13 16:44:08 -070047 if (mEnabled) {
48 return;
49 }
Irvelffc9efc2016-07-27 15:16:37 -070050 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070051 mEnabled = true;
Irvelffc9efc2016-07-27 15:16:37 -070052 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
53 saveExistingDisplaysLocked(displays);
54 saveExistingSurfacesLocked(layers);
Irvelc274c632016-06-13 16:44:08 -070055}
56
57void SurfaceInterceptor::disable() {
Irvelc274c632016-06-13 16:44:08 -070058 if (!mEnabled) {
59 return;
60 }
Irvelffc9efc2016-07-27 15:16:37 -070061 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070062 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
Irvelffc9efc2016-07-27 15:16:37 -070070bool SurfaceInterceptor::isEnabled() {
71 return mEnabled;
72}
73
74void 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
Irvelc274c632016-06-13 16:44:08 -070078 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -070079 for (size_t i = 0 ; i < displays.size() ; i++) {
80 addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
81 addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
Irvelc274c632016-06-13 16:44:08 -070082 }
83}
84
Irvelffc9efc2016-07-27 15:16:37 -070085void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
Irvelc274c632016-06-13 16:44:08 -070086 ATRACE_CALL();
Robert Carr1f0a16a2016-10-24 16:27:39 -070087 for (const auto& l : layers) {
Dan Stoza412903f2017-04-27 13:42:17 -070088 l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -070089 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
90 addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
91 });
Irvelc274c632016-06-13 16:44:08 -070092 }
Irvelffc9efc2016-07-27 15:16:37 -070093}
94
95void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
96 const sp<const Layer>& layer)
97{
Irvelc274c632016-06-13 16:44:08 -070098 Transaction* transaction(increment->mutable_transaction());
Lloyd Piquef1c675b2018-09-12 20:45:39 -070099 const uint32_t layerFlags = layer->getTransactionFlags();
100 transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
101 transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
Irvelc274c632016-06-13 16:44:08 -0700102
103 const int32_t layerId(getLayerId(layer));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800104 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);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700108 addTransparentRegionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 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) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700114 addDeferTransactionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115 layer->mCurrentState.barrierLayer_legacy.promote(),
116 layer->mCurrentState.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700117 }
Irvelc274c632016-06-13 16:44:08 -0700118 addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 addFlagsLocked(transaction, layerId, layer->mCurrentState.flags);
Irvelc274c632016-06-13 16:44:08 -0700120}
121
Irvelffc9efc2016-07-27 15:16:37 -0700122void 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
Dominik Laskowski663bd282018-04-19 15:26:54 -0700129 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,
Irvelffc9efc2016-07-27 15:16:37 -0700133 display.viewport, display.frame);
134}
135
Irvelc274c632016-06-13 16:44:08 -0700136status_t SurfaceInterceptor::writeProtoFileLocked() {
137 ATRACE_CALL();
Colin Cross63549382016-10-26 12:52:53 -0700138 std::string output;
139
Irvelc274c632016-06-13 16:44:08 -0700140 if (!mTrace.IsInitialized()) {
141 return NOT_ENOUGH_DATA;
142 }
Colin Cross63549382016-10-26 12:52:53 -0700143 if (!mTrace.SerializeToString(&output)) {
Irvelc274c632016-06-13 16:44:08 -0700144 return PERMISSION_DENIED;
145 }
Colin Cross63549382016-10-26 12:52:53 -0700146 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
147 return PERMISSION_DENIED;
148 }
149
Irvelc274c632016-06-13 16:44:08 -0700150 return NO_ERROR;
151}
152
Irvel3017c612016-08-09 16:50:06 -0700153const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) {
154 const sp<const IBinder>& handle(weakHandle.promote());
Irvelc274c632016-06-13 16:44:08 -0700155 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
161const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) {
162 return layer->getName().string();
163}
164
165int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) {
166 return layer->sequence;
167}
168
Irvelffc9efc2016-07-27 15:16:37 -0700169Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Irvelc274c632016-06-13 16:44:08 -0700170 Increment* increment(mTrace.add_increment());
171 increment->set_time_stamp(systemTime());
172 return increment;
173}
174
Irvelffc9efc2016-07-27 15:16:37 -0700175SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
176 int32_t layerId)
177{
178 SurfaceChange* change(transaction->add_surface_change());
Irvelc274c632016-06-13 16:44:08 -0700179 change->set_id(layerId);
180 return change;
181}
182
Irvelffc9efc2016-07-27 15:16:37 -0700183DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700184 int32_t sequenceId)
Irvelffc9efc2016-07-27 15:16:37 -0700185{
186 DisplayChange* dispChange(transaction->add_display_change());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700187 dispChange->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700188 return dispChange;
189}
190
Irvelc274c632016-06-13 16:44:08 -0700191void 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
Irvelffc9efc2016-07-27 15:16:37 -0700198void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
199 float x, float y)
Irvelc274c632016-06-13 16:44:08 -0700200{
Irvelffc9efc2016-07-27 15:16:37 -0700201 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700202 PositionChange* posChange(change->mutable_position());
203 posChange->set_x(x);
204 posChange->set_y(y);
205}
206
Irvelffc9efc2016-07-27 15:16:37 -0700207void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
208 uint32_t z)
209{
210 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700211 LayerChange* depthChange(change->mutable_layer());
212 depthChange->set_layer(z);
213}
214
215void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
216 uint32_t h)
217{
Irvelffc9efc2016-07-27 15:16:37 -0700218 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700219 SizeChange* sizeChange(change->mutable_size());
220 sizeChange->set_w(w);
221 sizeChange->set_h(h);
222}
223
Irvelffc9efc2016-07-27 15:16:37 -0700224void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
225 float alpha)
226{
227 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700228 AlphaChange* alphaChange(change->mutable_alpha());
229 alphaChange->set_alpha(alpha);
230}
231
232void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
233 const layer_state_t::matrix22_t& matrix)
234{
Irvelffc9efc2016-07-27 15:16:37 -0700235 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700236 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
Irvelffc9efc2016-07-27 15:16:37 -0700243void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
244 int32_t layerId, const Region& transRegion)
Irvelc274c632016-06-13 16:44:08 -0700245{
Irvelffc9efc2016-07-27 15:16:37 -0700246 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700247 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
Irvelffc9efc2016-07-27 15:16:37 -0700255void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId,
256 uint8_t flags)
257{
Irvelc274c632016-06-13 16:44:08 -0700258 // There can be multiple flags changed
259 if (flags & layer_state_t::eLayerHidden) {
Irvelffc9efc2016-07-27 15:16:37 -0700260 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700261 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
262 flagChange->set_hidden_flag(true);
263 }
264 if (flags & layer_state_t::eLayerOpaque) {
Irvelffc9efc2016-07-27 15:16:37 -0700265 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700266 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
267 flagChange->set_opaque_flag(true);
268 }
269 if (flags & layer_state_t::eLayerSecure) {
Irvelffc9efc2016-07-27 15:16:37 -0700270 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700271 SecureFlagChange* flagChange(change->mutable_secure_flag());
272 flagChange->set_secure_flag(true);
273 }
274}
275
276void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
277 uint32_t layerStack)
278{
Irvelffc9efc2016-07-27 15:16:37 -0700279 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700280 LayerStackChange* layerStackChange(change->mutable_layer_stack());
281 layerStackChange->set_layer_stack(layerStack);
282}
283
284void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
285 const Rect& rect)
286{
Irvelffc9efc2016-07-27 15:16:37 -0700287 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700288 CropChange* cropChange(change->mutable_crop());
289 Rectangle* protoRect(cropChange->mutable_rectangle());
290 setProtoRectLocked(protoRect, rect);
291}
292
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700293void 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
Irvelc274c632016-06-13 16:44:08 -0700301void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Robert Carr0d480722017-01-10 16:42:54 -0800302 const sp<const Layer>& layer, uint64_t frameNumber)
Irvelc274c632016-06-13 16:44:08 -0700303{
Irvelffc9efc2016-07-27 15:16:37 -0700304 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700305 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
Irvelffc9efc2016-07-27 15:16:37 -0700315void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
316 int32_t layerId, int32_t overrideScalingMode)
Irvelc274c632016-06-13 16:44:08 -0700317{
Irvelffc9efc2016-07-27 15:16:37 -0700318 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700319 OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
320 overrideChange->set_override_scaling_mode(overrideScalingMode);
321}
322
Irvelffc9efc2016-07-27 15:16:37 -0700323void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
Irvelc274c632016-06-13 16:44:08 -0700324 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 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700359 if (state.what & layer_state_t::eCropChanged_legacy) {
360 addCropLocked(transaction, layerId, state.crop_legacy);
Irvelc274c632016-06-13 16:44:08 -0700361 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700362 if (state.what & layer_state_t::eCornerRadiusChanged) {
363 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
364 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700365 if (state.what & layer_state_t::eDeferTransaction_legacy) {
Robert Carr0d480722017-01-10 16:42:54 -0800366 sp<Layer> otherLayer = nullptr;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700367 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;
Robert Carr0d480722017-01-10 16:42:54 -0800372 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 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700378 addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700379 }
Irvelc274c632016-06-13 16:44:08 -0700380 if (state.what & layer_state_t::eOverrideScalingModeChanged) {
381 addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
382 }
383}
384
Irvelffc9efc2016-07-27 15:16:37 -0700385void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700386 const DisplayState& state, int32_t sequenceId)
Irvelc274c632016-06-13 16:44:08 -0700387{
Irvelffc9efc2016-07-27 15:16:37 -0700388 if (state.what & DisplayState::eSurfaceChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700389 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
Irvelffc9efc2016-07-27 15:16:37 -0700390 }
391 if (state.what & DisplayState::eLayerStackChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700392 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
Irvelffc9efc2016-07-27 15:16:37 -0700393 }
394 if (state.what & DisplayState::eDisplaySizeChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700395 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
Irvelffc9efc2016-07-27 15:16:37 -0700396 }
397 if (state.what & DisplayState::eDisplayProjectionChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700398 addDisplayProjectionLocked(transaction, sequenceId, state.orientation, state.viewport,
Irvelffc9efc2016-07-27 15:16:37 -0700399 state.frame);
Irvelc274c632016-06-13 16:44:08 -0700400 }
401}
402
Irvelffc9efc2016-07-27 15:16:37 -0700403void 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));
Dominik Laskowski663bd282018-04-19 15:26:54 -0700418 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700419 }
420 }
Irvelc274c632016-06-13 16:44:08 -0700421}
422
Irvelffc9efc2016-07-27 15:16:37 -0700423void 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));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800429 creation->set_w(layer->mCurrentState.active_legacy.w);
430 creation->set_h(layer->mCurrentState.active_legacy.h);
Irvelc274c632016-06-13 16:44:08 -0700431}
432
Irvelffc9efc2016-07-27 15:16:37 -0700433void 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
440void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer,
Irvelc274c632016-06-13 16:44:08 -0700441 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
Irvelffc9efc2016-07-27 15:16:37 -0700450void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
Irvelc274c632016-06-13 16:44:08 -0700451 VSyncEvent* event(increment->mutable_vsync_event());
452 event->set_when(timestamp);
453}
454
Dominik Laskowski663bd282018-04-19 15:26:54 -0700455void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700456 const sp<const IGraphicBufferProducer>& surface)
Irvelc274c632016-06-13 16:44:08 -0700457{
Irvelffc9efc2016-07-27 15:16:37 -0700458 if (surface == nullptr) {
Irvelc274c632016-06-13 16:44:08 -0700459 return;
460 }
Irvelffc9efc2016-07-27 15:16:37 -0700461 uint64_t bufferQueueId = 0;
462 status_t err(surface->getUniqueId(&bufferQueueId));
463 if (err == NO_ERROR) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700464 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700465 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 }
Irvelc274c632016-06-13 16:44:08 -0700473}
474
Irvelffc9efc2016-07-27 15:16:37 -0700475void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700476 int32_t sequenceId, uint32_t layerStack)
Irvelffc9efc2016-07-27 15:16:37 -0700477{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700478 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700479 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
480 layerStackChange->set_layer_stack(layerStack);
481}
482
Dominik Laskowski663bd282018-04-19 15:26:54 -0700483void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700484 uint32_t w, uint32_t h)
485{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700486 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700487 SizeChange* sizeChange(dispChange->mutable_size());
488 sizeChange->set_w(w);
489 sizeChange->set_h(h);
490}
491
492void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700493 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
Irvelffc9efc2016-07-27 15:16:37 -0700494{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700495 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700496 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
504void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
505 const DisplayDeviceState& info)
506{
507 DisplayCreation* creation(increment->mutable_display_creation());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700508 creation->set_id(info.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700509 creation->set_name(info.displayName);
Irvelffc9efc2016-07-27 15:16:37 -0700510 creation->set_is_secure(info.isSecure);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700511 if (info.displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700512 creation->set_display_id(info.displayId->value);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700513 }
Irvelffc9efc2016-07-27 15:16:37 -0700514}
515
Dominik Laskowski663bd282018-04-19 15:26:54 -0700516void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700517 DisplayDeletion* deletion(increment->mutable_display_deletion());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700518 deletion->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700519}
520
Dominik Laskowski663bd282018-04-19 15:26:54 -0700521void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700522 int32_t mode)
523{
524 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700525 powerModeUpdate->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700526 powerModeUpdate->set_mode(mode);
527}
528
529void 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 }
Irvelc274c632016-06-13 16:44:08 -0700536 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700537 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
538 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
539 flags);
540}
541
542void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700543 if (!mEnabled || layer == nullptr) {
544 return;
545 }
Irvelc274c632016-06-13 16:44:08 -0700546 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700547 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
548 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
549}
550
551void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700552 if (!mEnabled || layer == nullptr) {
553 return;
554 }
Irvelffc9efc2016-07-27 15:16:37 -0700555 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700556 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700557 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
Irvelc274c632016-06-13 16:44:08 -0700558}
559
560void SurfaceInterceptor::saveBufferUpdate(const sp<const Layer>& layer, uint32_t width,
561 uint32_t height, uint64_t frameNumber)
562{
Irvelc274c632016-06-13 16:44:08 -0700563 if (!mEnabled || layer == nullptr) {
564 return;
565 }
Irvelffc9efc2016-07-27 15:16:37 -0700566 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700567 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700568 addBufferUpdateLocked(createTraceIncrementLocked(), layer, width, height, frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700569}
570
571void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
572 if (!mEnabled) {
573 return;
574 }
575 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700576 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
Irvelc274c632016-06-13 16:44:08 -0700577}
578
Irvelffc9efc2016-07-27 15:16:37 -0700579void 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
Dominik Laskowski663bd282018-04-19 15:26:54 -0700588void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700589 if (!mEnabled) {
590 return;
591 }
592 ATRACE_CALL();
593 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700594 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700595}
596
Dominik Laskowski663bd282018-04-19 15:26:54 -0700597void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
Irvelffc9efc2016-07-27 15:16:37 -0700598 if (!mEnabled) {
599 return;
600 }
601 ATRACE_CALL();
602 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700603 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
Irvelffc9efc2016-07-27 15:16:37 -0700604}
605
Lloyd Pique4dccc412018-01-22 17:21:36 -0800606} // namespace impl
Irvelc274c632016-06-13 16:44:08 -0700607} // namespace android