blob: a6dcb7e327e9f7f41c203d6efc45e95ea42f7d69 [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));
Ady Abraham83729882018-12-07 12:26:48 -0800104 Mutex::Autolock lock(layer->mStateMutex);
105 addPositionLocked(transaction, layerId, layer->mState.current.active_legacy.transform.tx(),
106 layer->mState.current.active_legacy.transform.ty());
107 addDepthLocked(transaction, layerId, layer->mState.current.z);
108 addAlphaLocked(transaction, layerId, layer->mState.current.color.a);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700109 addTransparentRegionLocked(transaction, layerId,
Ady Abraham83729882018-12-07 12:26:48 -0800110 layer->mState.current.activeTransparentRegion_legacy);
111 addLayerStackLocked(transaction, layerId, layer->mState.current.layerStack);
112 addCropLocked(transaction, layerId, layer->mState.current.crop_legacy);
113 addCornerRadiusLocked(transaction, layerId, layer->mState.current.cornerRadius);
114 if (layer->mState.current.barrierLayer_legacy != nullptr) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700115 addDeferTransactionLocked(transaction, layerId,
Ady Abraham83729882018-12-07 12:26:48 -0800116 layer->mState.current.barrierLayer_legacy.promote(),
117 layer->mState.current.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700118 }
Irvelc274c632016-06-13 16:44:08 -0700119 addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
Ady Abraham83729882018-12-07 12:26:48 -0800120 addFlagsLocked(transaction, layerId, layer->mState.current.flags);
Irvelc274c632016-06-13 16:44:08 -0700121}
122
Irvelffc9efc2016-07-27 15:16:37 -0700123void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
124 const DisplayDeviceState& display)
125{
126 Transaction* transaction(increment->mutable_transaction());
127 transaction->set_synchronous(false);
128 transaction->set_animation(false);
129
Dominik Laskowski663bd282018-04-19 15:26:54 -0700130 addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
131 addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
132 addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
133 addDisplayProjectionLocked(transaction, display.sequenceId, display.orientation,
Irvelffc9efc2016-07-27 15:16:37 -0700134 display.viewport, display.frame);
135}
136
Irvelc274c632016-06-13 16:44:08 -0700137status_t SurfaceInterceptor::writeProtoFileLocked() {
138 ATRACE_CALL();
Colin Cross63549382016-10-26 12:52:53 -0700139 std::string output;
140
Irvelc274c632016-06-13 16:44:08 -0700141 if (!mTrace.IsInitialized()) {
142 return NOT_ENOUGH_DATA;
143 }
Colin Cross63549382016-10-26 12:52:53 -0700144 if (!mTrace.SerializeToString(&output)) {
Irvelc274c632016-06-13 16:44:08 -0700145 return PERMISSION_DENIED;
146 }
Colin Cross63549382016-10-26 12:52:53 -0700147 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
148 return PERMISSION_DENIED;
149 }
150
Irvelc274c632016-06-13 16:44:08 -0700151 return NO_ERROR;
152}
153
Irvel3017c612016-08-09 16:50:06 -0700154const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) {
155 const sp<const IBinder>& handle(weakHandle.promote());
Irvelc274c632016-06-13 16:44:08 -0700156 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
157 const sp<const Layer> layer(layerHandle->owner.promote());
158 // layer could be a nullptr at this point
159 return layer;
160}
161
162const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) {
163 return layer->getName().string();
164}
165
166int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) {
167 return layer->sequence;
168}
169
Irvelffc9efc2016-07-27 15:16:37 -0700170Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Irvelc274c632016-06-13 16:44:08 -0700171 Increment* increment(mTrace.add_increment());
172 increment->set_time_stamp(systemTime());
173 return increment;
174}
175
Irvelffc9efc2016-07-27 15:16:37 -0700176SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
177 int32_t layerId)
178{
179 SurfaceChange* change(transaction->add_surface_change());
Irvelc274c632016-06-13 16:44:08 -0700180 change->set_id(layerId);
181 return change;
182}
183
Irvelffc9efc2016-07-27 15:16:37 -0700184DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700185 int32_t sequenceId)
Irvelffc9efc2016-07-27 15:16:37 -0700186{
187 DisplayChange* dispChange(transaction->add_display_change());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700188 dispChange->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700189 return dispChange;
190}
191
Irvelc274c632016-06-13 16:44:08 -0700192void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
193 protoRect->set_left(rect.left);
194 protoRect->set_top(rect.top);
195 protoRect->set_right(rect.right);
196 protoRect->set_bottom(rect.bottom);
197}
198
Irvelffc9efc2016-07-27 15:16:37 -0700199void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
200 float x, float y)
Irvelc274c632016-06-13 16:44:08 -0700201{
Irvelffc9efc2016-07-27 15:16:37 -0700202 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700203 PositionChange* posChange(change->mutable_position());
204 posChange->set_x(x);
205 posChange->set_y(y);
206}
207
Irvelffc9efc2016-07-27 15:16:37 -0700208void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
209 uint32_t z)
210{
211 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700212 LayerChange* depthChange(change->mutable_layer());
213 depthChange->set_layer(z);
214}
215
216void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
217 uint32_t h)
218{
Irvelffc9efc2016-07-27 15:16:37 -0700219 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700220 SizeChange* sizeChange(change->mutable_size());
221 sizeChange->set_w(w);
222 sizeChange->set_h(h);
223}
224
Irvelffc9efc2016-07-27 15:16:37 -0700225void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
226 float alpha)
227{
228 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700229 AlphaChange* alphaChange(change->mutable_alpha());
230 alphaChange->set_alpha(alpha);
231}
232
233void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
234 const layer_state_t::matrix22_t& matrix)
235{
Irvelffc9efc2016-07-27 15:16:37 -0700236 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700237 MatrixChange* matrixChange(change->mutable_matrix());
238 matrixChange->set_dsdx(matrix.dsdx);
239 matrixChange->set_dtdx(matrix.dtdx);
240 matrixChange->set_dsdy(matrix.dsdy);
241 matrixChange->set_dtdy(matrix.dtdy);
242}
243
Irvelffc9efc2016-07-27 15:16:37 -0700244void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
245 int32_t layerId, const Region& transRegion)
Irvelc274c632016-06-13 16:44:08 -0700246{
Irvelffc9efc2016-07-27 15:16:37 -0700247 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700248 TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
249
250 for (const auto& rect : transRegion) {
251 Rectangle* protoRect(transparentChange->add_region());
252 setProtoRectLocked(protoRect, rect);
253 }
254}
255
Irvelffc9efc2016-07-27 15:16:37 -0700256void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId,
257 uint8_t flags)
258{
Irvelc274c632016-06-13 16:44:08 -0700259 // There can be multiple flags changed
260 if (flags & layer_state_t::eLayerHidden) {
Irvelffc9efc2016-07-27 15:16:37 -0700261 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700262 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
263 flagChange->set_hidden_flag(true);
264 }
265 if (flags & layer_state_t::eLayerOpaque) {
Irvelffc9efc2016-07-27 15:16:37 -0700266 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700267 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
268 flagChange->set_opaque_flag(true);
269 }
270 if (flags & layer_state_t::eLayerSecure) {
Irvelffc9efc2016-07-27 15:16:37 -0700271 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700272 SecureFlagChange* flagChange(change->mutable_secure_flag());
273 flagChange->set_secure_flag(true);
274 }
275}
276
277void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
278 uint32_t layerStack)
279{
Irvelffc9efc2016-07-27 15:16:37 -0700280 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700281 LayerStackChange* layerStackChange(change->mutable_layer_stack());
282 layerStackChange->set_layer_stack(layerStack);
283}
284
285void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
286 const Rect& rect)
287{
Irvelffc9efc2016-07-27 15:16:37 -0700288 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700289 CropChange* cropChange(change->mutable_crop());
290 Rectangle* protoRect(cropChange->mutable_rectangle());
291 setProtoRectLocked(protoRect, rect);
292}
293
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700294void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
295 float cornerRadius)
296{
297 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
298 CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
299 cornerRadiusChange->set_corner_radius(cornerRadius);
300}
301
Irvelc274c632016-06-13 16:44:08 -0700302void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Robert Carr0d480722017-01-10 16:42:54 -0800303 const sp<const Layer>& layer, uint64_t frameNumber)
Irvelc274c632016-06-13 16:44:08 -0700304{
Irvelffc9efc2016-07-27 15:16:37 -0700305 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700306 if (layer == nullptr) {
307 ALOGE("An existing layer could not be retrieved with the handle"
308 " for the deferred transaction");
309 return;
310 }
311 DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction());
312 deferTransaction->set_layer_id(getLayerId(layer));
313 deferTransaction->set_frame_number(frameNumber);
314}
315
Irvelffc9efc2016-07-27 15:16:37 -0700316void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
317 int32_t layerId, int32_t overrideScalingMode)
Irvelc274c632016-06-13 16:44:08 -0700318{
Irvelffc9efc2016-07-27 15:16:37 -0700319 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700320 OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
321 overrideChange->set_override_scaling_mode(overrideScalingMode);
322}
323
Irvelffc9efc2016-07-27 15:16:37 -0700324void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
Irvelc274c632016-06-13 16:44:08 -0700325 const layer_state_t& state)
326{
327 const sp<const Layer> layer(getLayer(state.surface));
328 if (layer == nullptr) {
329 ALOGE("An existing layer could not be retrieved with the surface "
330 "from the layer_state_t surface in the update transaction");
331 return;
332 }
333
334 const int32_t layerId(getLayerId(layer));
335
336 if (state.what & layer_state_t::ePositionChanged) {
337 addPositionLocked(transaction, layerId, state.x, state.y);
338 }
339 if (state.what & layer_state_t::eLayerChanged) {
340 addDepthLocked(transaction, layerId, state.z);
341 }
342 if (state.what & layer_state_t::eSizeChanged) {
343 addSizeLocked(transaction, layerId, state.w, state.h);
344 }
345 if (state.what & layer_state_t::eAlphaChanged) {
346 addAlphaLocked(transaction, layerId, state.alpha);
347 }
348 if (state.what & layer_state_t::eMatrixChanged) {
349 addMatrixLocked(transaction, layerId, state.matrix);
350 }
351 if (state.what & layer_state_t::eTransparentRegionChanged) {
352 addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
353 }
354 if (state.what & layer_state_t::eFlagsChanged) {
355 addFlagsLocked(transaction, layerId, state.flags);
356 }
357 if (state.what & layer_state_t::eLayerStackChanged) {
358 addLayerStackLocked(transaction, layerId, state.layerStack);
359 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700360 if (state.what & layer_state_t::eCropChanged_legacy) {
361 addCropLocked(transaction, layerId, state.crop_legacy);
Irvelc274c632016-06-13 16:44:08 -0700362 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700363 if (state.what & layer_state_t::eCornerRadiusChanged) {
364 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
365 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700366 if (state.what & layer_state_t::eDeferTransaction_legacy) {
Robert Carr0d480722017-01-10 16:42:54 -0800367 sp<Layer> otherLayer = nullptr;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700368 if (state.barrierHandle_legacy != nullptr) {
369 otherLayer =
370 static_cast<Layer::Handle*>(state.barrierHandle_legacy.get())->owner.promote();
371 } else if (state.barrierGbp_legacy != nullptr) {
372 auto const& gbp = state.barrierGbp_legacy;
Robert Carr0d480722017-01-10 16:42:54 -0800373 if (mFlinger->authenticateSurfaceTextureLocked(gbp)) {
374 otherLayer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
375 } else {
376 ALOGE("Attempt to defer transaction to to an unrecognized GraphicBufferProducer");
377 }
378 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700379 addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700380 }
Irvelc274c632016-06-13 16:44:08 -0700381 if (state.what & layer_state_t::eOverrideScalingModeChanged) {
382 addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
383 }
384}
385
Irvelffc9efc2016-07-27 15:16:37 -0700386void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700387 const DisplayState& state, int32_t sequenceId)
Irvelc274c632016-06-13 16:44:08 -0700388{
Irvelffc9efc2016-07-27 15:16:37 -0700389 if (state.what & DisplayState::eSurfaceChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700390 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
Irvelffc9efc2016-07-27 15:16:37 -0700391 }
392 if (state.what & DisplayState::eLayerStackChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700393 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
Irvelffc9efc2016-07-27 15:16:37 -0700394 }
395 if (state.what & DisplayState::eDisplaySizeChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700396 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
Irvelffc9efc2016-07-27 15:16:37 -0700397 }
398 if (state.what & DisplayState::eDisplayProjectionChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700399 addDisplayProjectionLocked(transaction, sequenceId, state.orientation, state.viewport,
Irvelffc9efc2016-07-27 15:16:37 -0700400 state.frame);
Irvelc274c632016-06-13 16:44:08 -0700401 }
402}
403
Irvelffc9efc2016-07-27 15:16:37 -0700404void SurfaceInterceptor::addTransactionLocked(Increment* increment,
405 const Vector<ComposerState>& stateUpdates,
406 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
407 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags)
408{
409 Transaction* transaction(increment->mutable_transaction());
410 transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
411 transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
412 for (const auto& compState: stateUpdates) {
413 addSurfaceChangesLocked(transaction, compState.state);
414 }
415 for (const auto& disp: changedDisplays) {
416 ssize_t dpyIdx = displays.indexOfKey(disp.token);
417 if (dpyIdx >= 0) {
418 const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
Dominik Laskowski663bd282018-04-19 15:26:54 -0700419 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700420 }
421 }
Irvelc274c632016-06-13 16:44:08 -0700422}
423
Irvelffc9efc2016-07-27 15:16:37 -0700424void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
425 const sp<const Layer>& layer)
426{
427 SurfaceCreation* creation(increment->mutable_surface_creation());
428 creation->set_id(getLayerId(layer));
429 creation->set_name(getLayerName(layer));
Ady Abraham83729882018-12-07 12:26:48 -0800430 Mutex::Autolock lock(layer->mStateMutex);
431 creation->set_w(layer->mState.current.active_legacy.w);
432 creation->set_h(layer->mState.current.active_legacy.h);
Irvelc274c632016-06-13 16:44:08 -0700433}
434
Irvelffc9efc2016-07-27 15:16:37 -0700435void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
436 const sp<const Layer>& layer)
437{
438 SurfaceDeletion* deletion(increment->mutable_surface_deletion());
439 deletion->set_id(getLayerId(layer));
440}
441
442void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer,
Irvelc274c632016-06-13 16:44:08 -0700443 uint32_t width, uint32_t height, uint64_t frameNumber)
444{
445 BufferUpdate* update(increment->mutable_buffer_update());
446 update->set_id(getLayerId(layer));
447 update->set_w(width);
448 update->set_h(height);
449 update->set_frame_number(frameNumber);
450}
451
Irvelffc9efc2016-07-27 15:16:37 -0700452void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
Irvelc274c632016-06-13 16:44:08 -0700453 VSyncEvent* event(increment->mutable_vsync_event());
454 event->set_when(timestamp);
455}
456
Dominik Laskowski663bd282018-04-19 15:26:54 -0700457void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700458 const sp<const IGraphicBufferProducer>& surface)
Irvelc274c632016-06-13 16:44:08 -0700459{
Irvelffc9efc2016-07-27 15:16:37 -0700460 if (surface == nullptr) {
Irvelc274c632016-06-13 16:44:08 -0700461 return;
462 }
Irvelffc9efc2016-07-27 15:16:37 -0700463 uint64_t bufferQueueId = 0;
464 status_t err(surface->getUniqueId(&bufferQueueId));
465 if (err == NO_ERROR) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700466 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700467 DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
468 surfaceChange->set_buffer_queue_id(bufferQueueId);
469 surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
470 }
471 else {
472 ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
473 strerror(-err));
474 }
Irvelc274c632016-06-13 16:44:08 -0700475}
476
Irvelffc9efc2016-07-27 15:16:37 -0700477void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700478 int32_t sequenceId, uint32_t layerStack)
Irvelffc9efc2016-07-27 15:16:37 -0700479{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700480 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700481 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
482 layerStackChange->set_layer_stack(layerStack);
483}
484
Dominik Laskowski663bd282018-04-19 15:26:54 -0700485void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700486 uint32_t w, uint32_t h)
487{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700488 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700489 SizeChange* sizeChange(dispChange->mutable_size());
490 sizeChange->set_w(w);
491 sizeChange->set_h(h);
492}
493
494void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700495 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
Irvelffc9efc2016-07-27 15:16:37 -0700496{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700497 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700498 ProjectionChange* projectionChange(dispChange->mutable_projection());
499 projectionChange->set_orientation(orientation);
500 Rectangle* viewportRect(projectionChange->mutable_viewport());
501 setProtoRectLocked(viewportRect, viewport);
502 Rectangle* frameRect(projectionChange->mutable_frame());
503 setProtoRectLocked(frameRect, frame);
504}
505
506void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
507 const DisplayDeviceState& info)
508{
509 DisplayCreation* creation(increment->mutable_display_creation());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700510 creation->set_id(info.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700511 creation->set_name(info.displayName);
Irvelffc9efc2016-07-27 15:16:37 -0700512 creation->set_is_secure(info.isSecure);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700513 if (info.displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700514 creation->set_display_id(info.displayId->value);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700515 }
Irvelffc9efc2016-07-27 15:16:37 -0700516}
517
Dominik Laskowski663bd282018-04-19 15:26:54 -0700518void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700519 DisplayDeletion* deletion(increment->mutable_display_deletion());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700520 deletion->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700521}
522
Dominik Laskowski663bd282018-04-19 15:26:54 -0700523void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700524 int32_t mode)
525{
526 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700527 powerModeUpdate->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700528 powerModeUpdate->set_mode(mode);
529}
530
531void SurfaceInterceptor::saveTransaction(const Vector<ComposerState>& stateUpdates,
532 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
533 const Vector<DisplayState>& changedDisplays, uint32_t flags)
534{
535 if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
536 return;
537 }
Irvelc274c632016-06-13 16:44:08 -0700538 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700539 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
540 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
541 flags);
542}
543
544void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700545 if (!mEnabled || layer == nullptr) {
546 return;
547 }
Irvelc274c632016-06-13 16:44:08 -0700548 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700549 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
550 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
551}
552
553void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700554 if (!mEnabled || layer == nullptr) {
555 return;
556 }
Irvelffc9efc2016-07-27 15:16:37 -0700557 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700558 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700559 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
Irvelc274c632016-06-13 16:44:08 -0700560}
561
562void SurfaceInterceptor::saveBufferUpdate(const sp<const Layer>& layer, uint32_t width,
563 uint32_t height, uint64_t frameNumber)
564{
Irvelc274c632016-06-13 16:44:08 -0700565 if (!mEnabled || layer == nullptr) {
566 return;
567 }
Irvelffc9efc2016-07-27 15:16:37 -0700568 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700569 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700570 addBufferUpdateLocked(createTraceIncrementLocked(), layer, width, height, frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700571}
572
573void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
574 if (!mEnabled) {
575 return;
576 }
577 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700578 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
Irvelc274c632016-06-13 16:44:08 -0700579}
580
Irvelffc9efc2016-07-27 15:16:37 -0700581void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
582 if (!mEnabled) {
583 return;
584 }
585 ATRACE_CALL();
586 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
587 addDisplayCreationLocked(createTraceIncrementLocked(), info);
588}
589
Dominik Laskowski663bd282018-04-19 15:26:54 -0700590void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700591 if (!mEnabled) {
592 return;
593 }
594 ATRACE_CALL();
595 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700596 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700597}
598
Dominik Laskowski663bd282018-04-19 15:26:54 -0700599void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
Irvelffc9efc2016-07-27 15:16:37 -0700600 if (!mEnabled) {
601 return;
602 }
603 ATRACE_CALL();
604 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700605 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
Irvelffc9efc2016-07-27 15:16:37 -0700606}
607
Lloyd Pique4dccc412018-01-22 17:21:36 -0800608} // namespace impl
Irvelc274c632016-06-13 16:44:08 -0700609} // namespace android