blob: a8504fb32533b52c882b67eb2aa460c2fe4faca6 [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 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Irvelc274c632016-06-13 16:44:08 -070020#undef LOG_TAG
21#define LOG_TAG "SurfaceInterceptor"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "Layer.h"
25#include "SurfaceFlinger.h"
26#include "SurfaceInterceptor.h"
Colin Cross63549382016-10-26 12:52:53 -070027
Irvelc274c632016-06-13 16:44:08 -070028#include <fstream>
29
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070030#include <android-base/file.h>
31#include <log/log.h>
32#include <utils/Trace.h>
33
Irvelc274c632016-06-13 16:44:08 -070034namespace android {
35
36// ----------------------------------------------------------------------------
Marissa Wall61c58622018-07-18 10:12:20 -070037// TODO(marissaw): add new layer state values to SurfaceInterceptor
Irvelc274c632016-06-13 16:44:08 -070038
Lloyd Pique4dccc412018-01-22 17:21:36 -080039SurfaceInterceptor::~SurfaceInterceptor() = default;
40
41namespace impl {
42
Robert Carr0d480722017-01-10 16:42:54 -080043SurfaceInterceptor::SurfaceInterceptor(SurfaceFlinger* flinger)
44 : mFlinger(flinger)
45{
46}
47
Irvelffc9efc2016-07-27 15:16:37 -070048void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
49 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
50{
Irvelc274c632016-06-13 16:44:08 -070051 if (mEnabled) {
52 return;
53 }
Irvelffc9efc2016-07-27 15:16:37 -070054 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070055 mEnabled = true;
Irvelffc9efc2016-07-27 15:16:37 -070056 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
57 saveExistingDisplaysLocked(displays);
58 saveExistingSurfacesLocked(layers);
Irvelc274c632016-06-13 16:44:08 -070059}
60
61void SurfaceInterceptor::disable() {
Irvelc274c632016-06-13 16:44:08 -070062 if (!mEnabled) {
63 return;
64 }
Irvelffc9efc2016-07-27 15:16:37 -070065 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -070066 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
67 mEnabled = false;
68 status_t err(writeProtoFileLocked());
69 ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
70 ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
71 mTrace.Clear();
72}
73
Irvelffc9efc2016-07-27 15:16:37 -070074bool SurfaceInterceptor::isEnabled() {
75 return mEnabled;
76}
77
78void SurfaceInterceptor::saveExistingDisplaysLocked(
79 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
80{
81 // Caveat: The initial snapshot does not capture the power mode of the existing displays
Irvelc274c632016-06-13 16:44:08 -070082 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -070083 for (size_t i = 0 ; i < displays.size() ; i++) {
84 addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
85 addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
Irvelc274c632016-06-13 16:44:08 -070086 }
87}
88
Irvelffc9efc2016-07-27 15:16:37 -070089void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
Irvelc274c632016-06-13 16:44:08 -070090 ATRACE_CALL();
Robert Carr1f0a16a2016-10-24 16:27:39 -070091 for (const auto& l : layers) {
Dan Stoza412903f2017-04-27 13:42:17 -070092 l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -070093 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
94 addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
95 });
Irvelc274c632016-06-13 16:44:08 -070096 }
Irvelffc9efc2016-07-27 15:16:37 -070097}
98
99void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
100 const sp<const Layer>& layer)
101{
Irvelc274c632016-06-13 16:44:08 -0700102 Transaction* transaction(increment->mutable_transaction());
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700103 const uint32_t layerFlags = layer->getTransactionFlags();
104 transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
105 transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
Irvelc274c632016-06-13 16:44:08 -0700106
107 const int32_t layerId(getLayerId(layer));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108 addPositionLocked(transaction, layerId, layer->mCurrentState.active_legacy.transform.tx(),
109 layer->mCurrentState.active_legacy.transform.ty());
110 addDepthLocked(transaction, layerId, layer->mCurrentState.z);
111 addAlphaLocked(transaction, layerId, layer->mCurrentState.color.a);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700112 addTransparentRegionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 layer->mCurrentState.activeTransparentRegion_legacy);
114 addLayerStackLocked(transaction, layerId, layer->mCurrentState.layerStack);
115 addCropLocked(transaction, layerId, layer->mCurrentState.crop_legacy);
116 addCornerRadiusLocked(transaction, layerId, layer->mCurrentState.cornerRadius);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800117 addBackgroundBlurRadiusLocked(transaction, layerId, layer->mCurrentState.backgroundBlurRadius);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800118 if (layer->mCurrentState.barrierLayer_legacy != nullptr) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700119 addDeferTransactionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 layer->mCurrentState.barrierLayer_legacy.promote(),
121 layer->mCurrentState.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700122 }
Irvelc274c632016-06-13 16:44:08 -0700123 addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700124 addFlagsLocked(transaction, layerId, layer->mCurrentState.flags,
125 layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
126 layer_state_t::eLayerSecure);
127 addReparentLocked(transaction, layerId, getLayerIdFromWeakRef(layer->mCurrentParent));
128 addDetachChildrenLocked(transaction, layerId, layer->isLayerDetached());
129 addRelativeParentLocked(transaction, layerId,
130 getLayerIdFromWeakRef(layer->mCurrentState.zOrderRelativeOf),
131 layer->mCurrentState.z);
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800132 addShadowRadiusLocked(transaction, layerId, layer->mCurrentState.shadowRadius);
Irvelc274c632016-06-13 16:44:08 -0700133}
134
Irvelffc9efc2016-07-27 15:16:37 -0700135void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
136 const DisplayDeviceState& display)
137{
138 Transaction* transaction(increment->mutable_transaction());
139 transaction->set_synchronous(false);
140 transaction->set_animation(false);
141
Dominik Laskowski663bd282018-04-19 15:26:54 -0700142 addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
143 addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
144 addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800145 addDisplayProjectionLocked(transaction, display.sequenceId, toRotationInt(display.orientation),
146 display.viewport, display.frame);
Irvelffc9efc2016-07-27 15:16:37 -0700147}
148
Irvelc274c632016-06-13 16:44:08 -0700149status_t SurfaceInterceptor::writeProtoFileLocked() {
150 ATRACE_CALL();
Colin Cross63549382016-10-26 12:52:53 -0700151 std::string output;
152
Irvelc274c632016-06-13 16:44:08 -0700153 if (!mTrace.IsInitialized()) {
154 return NOT_ENOUGH_DATA;
155 }
Colin Cross63549382016-10-26 12:52:53 -0700156 if (!mTrace.SerializeToString(&output)) {
Irvelc274c632016-06-13 16:44:08 -0700157 return PERMISSION_DENIED;
158 }
Colin Cross63549382016-10-26 12:52:53 -0700159 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
160 return PERMISSION_DENIED;
161 }
162
Irvelc274c632016-06-13 16:44:08 -0700163 return NO_ERROR;
164}
165
Vishnu Nair456bbb22019-07-18 16:02:00 -0700166const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) const {
Irvel3017c612016-08-09 16:50:06 -0700167 const sp<const IBinder>& handle(weakHandle.promote());
Irvelc274c632016-06-13 16:44:08 -0700168 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
169 const sp<const Layer> layer(layerHandle->owner.promote());
170 // layer could be a nullptr at this point
171 return layer;
172}
173
Vishnu Nair456bbb22019-07-18 16:02:00 -0700174int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) const {
Irvelc274c632016-06-13 16:44:08 -0700175 return layer->sequence;
176}
177
Vishnu Nair456bbb22019-07-18 16:02:00 -0700178int32_t SurfaceInterceptor::getLayerIdFromWeakRef(const wp<const Layer>& layer) const {
179 if (layer == nullptr) {
180 return -1;
181 }
182 auto strongLayer = layer.promote();
183 return strongLayer == nullptr ? -1 : getLayerId(strongLayer);
184}
185
186int32_t SurfaceInterceptor::getLayerIdFromHandle(const sp<const IBinder>& handle) const {
187 if (handle == nullptr) {
188 return -1;
189 }
190 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
191 const sp<const Layer> layer(layerHandle->owner.promote());
192 return layer == nullptr ? -1 : getLayerId(layer);
193}
194
Irvelffc9efc2016-07-27 15:16:37 -0700195Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Irvelc274c632016-06-13 16:44:08 -0700196 Increment* increment(mTrace.add_increment());
Vishnu Nair45cb21a2020-03-27 17:39:24 -0700197 increment->set_time_stamp(elapsedRealtimeNano());
Irvelc274c632016-06-13 16:44:08 -0700198 return increment;
199}
200
Irvelffc9efc2016-07-27 15:16:37 -0700201SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
202 int32_t layerId)
203{
204 SurfaceChange* change(transaction->add_surface_change());
Irvelc274c632016-06-13 16:44:08 -0700205 change->set_id(layerId);
206 return change;
207}
208
Irvelffc9efc2016-07-27 15:16:37 -0700209DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700210 int32_t sequenceId)
Irvelffc9efc2016-07-27 15:16:37 -0700211{
212 DisplayChange* dispChange(transaction->add_display_change());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700213 dispChange->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700214 return dispChange;
215}
216
Irvelc274c632016-06-13 16:44:08 -0700217void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
218 protoRect->set_left(rect.left);
219 protoRect->set_top(rect.top);
220 protoRect->set_right(rect.right);
221 protoRect->set_bottom(rect.bottom);
222}
223
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000224void SurfaceInterceptor::setTransactionOriginLocked(Transaction* transaction, int32_t pid,
225 int32_t uid) {
226 Origin* origin(transaction->mutable_origin());
227 origin->set_pid(pid);
228 origin->set_uid(uid);
229}
230
Irvelffc9efc2016-07-27 15:16:37 -0700231void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
232 float x, float y)
Irvelc274c632016-06-13 16:44:08 -0700233{
Irvelffc9efc2016-07-27 15:16:37 -0700234 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700235 PositionChange* posChange(change->mutable_position());
236 posChange->set_x(x);
237 posChange->set_y(y);
238}
239
Irvelffc9efc2016-07-27 15:16:37 -0700240void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
241 uint32_t z)
242{
243 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700244 LayerChange* depthChange(change->mutable_layer());
245 depthChange->set_layer(z);
246}
247
248void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
249 uint32_t h)
250{
Irvelffc9efc2016-07-27 15:16:37 -0700251 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700252 SizeChange* sizeChange(change->mutable_size());
253 sizeChange->set_w(w);
254 sizeChange->set_h(h);
255}
256
Irvelffc9efc2016-07-27 15:16:37 -0700257void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
258 float alpha)
259{
260 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700261 AlphaChange* alphaChange(change->mutable_alpha());
262 alphaChange->set_alpha(alpha);
263}
264
265void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
266 const layer_state_t::matrix22_t& matrix)
267{
Irvelffc9efc2016-07-27 15:16:37 -0700268 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700269 MatrixChange* matrixChange(change->mutable_matrix());
270 matrixChange->set_dsdx(matrix.dsdx);
271 matrixChange->set_dtdx(matrix.dtdx);
272 matrixChange->set_dsdy(matrix.dsdy);
273 matrixChange->set_dtdy(matrix.dtdy);
274}
275
Irvelffc9efc2016-07-27 15:16:37 -0700276void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
277 int32_t layerId, const Region& transRegion)
Irvelc274c632016-06-13 16:44:08 -0700278{
Irvelffc9efc2016-07-27 15:16:37 -0700279 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700280 TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
281
282 for (const auto& rect : transRegion) {
283 Rectangle* protoRect(transparentChange->add_region());
284 setProtoRectLocked(protoRect, rect);
285 }
286}
287
Vishnu Nair456bbb22019-07-18 16:02:00 -0700288void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags,
289 uint8_t mask) {
Irvelc274c632016-06-13 16:44:08 -0700290 // There can be multiple flags changed
Vishnu Nair456bbb22019-07-18 16:02:00 -0700291 if (mask & layer_state_t::eLayerHidden) {
Irvelffc9efc2016-07-27 15:16:37 -0700292 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700293 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700294 flagChange->set_hidden_flag(flags & layer_state_t::eLayerHidden);
Irvelc274c632016-06-13 16:44:08 -0700295 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700296 if (mask & layer_state_t::eLayerOpaque) {
Irvelffc9efc2016-07-27 15:16:37 -0700297 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700298 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700299 flagChange->set_opaque_flag(flags & layer_state_t::eLayerOpaque);
Irvelc274c632016-06-13 16:44:08 -0700300 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700301 if (mask & layer_state_t::eLayerSecure) {
Irvelffc9efc2016-07-27 15:16:37 -0700302 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700303 SecureFlagChange* flagChange(change->mutable_secure_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700304 flagChange->set_secure_flag(flags & layer_state_t::eLayerSecure);
Irvelc274c632016-06-13 16:44:08 -0700305 }
306}
307
308void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
309 uint32_t layerStack)
310{
Irvelffc9efc2016-07-27 15:16:37 -0700311 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700312 LayerStackChange* layerStackChange(change->mutable_layer_stack());
313 layerStackChange->set_layer_stack(layerStack);
314}
315
316void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
317 const Rect& rect)
318{
Irvelffc9efc2016-07-27 15:16:37 -0700319 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700320 CropChange* cropChange(change->mutable_crop());
321 Rectangle* protoRect(cropChange->mutable_rectangle());
322 setProtoRectLocked(protoRect, rect);
323}
324
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700325void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
326 float cornerRadius)
327{
328 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
329 CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
330 cornerRadiusChange->set_corner_radius(cornerRadius);
331}
332
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800333void SurfaceInterceptor::addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId,
334 int32_t backgroundBlurRadius) {
335 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
336 BackgroundBlurRadiusChange* blurRadiusChange(change->mutable_background_blur_radius());
337 blurRadiusChange->set_background_blur_radius(backgroundBlurRadius);
338}
339
Irvelc274c632016-06-13 16:44:08 -0700340void SurfaceInterceptor::addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Robert Carr0d480722017-01-10 16:42:54 -0800341 const sp<const Layer>& layer, uint64_t frameNumber)
Irvelc274c632016-06-13 16:44:08 -0700342{
Irvelffc9efc2016-07-27 15:16:37 -0700343 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700344 if (layer == nullptr) {
345 ALOGE("An existing layer could not be retrieved with the handle"
346 " for the deferred transaction");
347 return;
348 }
349 DeferredTransactionChange* deferTransaction(change->mutable_deferred_transaction());
350 deferTransaction->set_layer_id(getLayerId(layer));
351 deferTransaction->set_frame_number(frameNumber);
352}
353
Irvelffc9efc2016-07-27 15:16:37 -0700354void SurfaceInterceptor::addOverrideScalingModeLocked(Transaction* transaction,
355 int32_t layerId, int32_t overrideScalingMode)
Irvelc274c632016-06-13 16:44:08 -0700356{
Irvelffc9efc2016-07-27 15:16:37 -0700357 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700358 OverrideScalingModeChange* overrideChange(change->mutable_override_scaling_mode());
359 overrideChange->set_override_scaling_mode(overrideScalingMode);
360}
361
Vishnu Nair456bbb22019-07-18 16:02:00 -0700362void SurfaceInterceptor::addReparentLocked(Transaction* transaction, int32_t layerId,
363 int32_t parentId) {
364 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
365 ReparentChange* overrideChange(change->mutable_reparent());
366 overrideChange->set_parent_id(parentId);
367}
368
369void SurfaceInterceptor::addReparentChildrenLocked(Transaction* transaction, int32_t layerId,
370 int32_t parentId) {
371 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
372 ReparentChildrenChange* overrideChange(change->mutable_reparent_children());
373 overrideChange->set_parent_id(parentId);
374}
375
376void SurfaceInterceptor::addDetachChildrenLocked(Transaction* transaction, int32_t layerId,
377 bool detached) {
378 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
379 DetachChildrenChange* overrideChange(change->mutable_detach_children());
380 overrideChange->set_detach_children(detached);
381}
382
383void SurfaceInterceptor::addRelativeParentLocked(Transaction* transaction, int32_t layerId,
384 int32_t parentId, int z) {
385 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
386 RelativeParentChange* overrideChange(change->mutable_relative_parent());
387 overrideChange->set_relative_parent_id(parentId);
388 overrideChange->set_z(z);
389}
390
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800391void SurfaceInterceptor::addShadowRadiusLocked(Transaction* transaction, int32_t layerId,
392 float shadowRadius) {
393 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
394 ShadowRadiusChange* overrideChange(change->mutable_shadow_radius());
395 overrideChange->set_radius(shadowRadius);
396}
397
Irvelffc9efc2016-07-27 15:16:37 -0700398void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
Irvelc274c632016-06-13 16:44:08 -0700399 const layer_state_t& state)
400{
401 const sp<const Layer> layer(getLayer(state.surface));
402 if (layer == nullptr) {
403 ALOGE("An existing layer could not be retrieved with the surface "
404 "from the layer_state_t surface in the update transaction");
405 return;
406 }
407
408 const int32_t layerId(getLayerId(layer));
409
410 if (state.what & layer_state_t::ePositionChanged) {
411 addPositionLocked(transaction, layerId, state.x, state.y);
412 }
413 if (state.what & layer_state_t::eLayerChanged) {
414 addDepthLocked(transaction, layerId, state.z);
415 }
416 if (state.what & layer_state_t::eSizeChanged) {
417 addSizeLocked(transaction, layerId, state.w, state.h);
418 }
419 if (state.what & layer_state_t::eAlphaChanged) {
420 addAlphaLocked(transaction, layerId, state.alpha);
421 }
422 if (state.what & layer_state_t::eMatrixChanged) {
423 addMatrixLocked(transaction, layerId, state.matrix);
424 }
425 if (state.what & layer_state_t::eTransparentRegionChanged) {
426 addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
427 }
428 if (state.what & layer_state_t::eFlagsChanged) {
Vishnu Nair456bbb22019-07-18 16:02:00 -0700429 addFlagsLocked(transaction, layerId, state.flags, state.mask);
Irvelc274c632016-06-13 16:44:08 -0700430 }
431 if (state.what & layer_state_t::eLayerStackChanged) {
432 addLayerStackLocked(transaction, layerId, state.layerStack);
433 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700434 if (state.what & layer_state_t::eCropChanged_legacy) {
435 addCropLocked(transaction, layerId, state.crop_legacy);
Irvelc274c632016-06-13 16:44:08 -0700436 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700437 if (state.what & layer_state_t::eCornerRadiusChanged) {
438 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
439 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800440 if (state.what & layer_state_t::eBackgroundBlurRadiusChanged) {
441 addBackgroundBlurRadiusLocked(transaction, layerId, state.backgroundBlurRadius);
442 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700443 if (state.what & layer_state_t::eDeferTransaction_legacy) {
Robert Carr0d480722017-01-10 16:42:54 -0800444 sp<Layer> otherLayer = nullptr;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700445 if (state.barrierHandle_legacy != nullptr) {
446 otherLayer =
447 static_cast<Layer::Handle*>(state.barrierHandle_legacy.get())->owner.promote();
448 } else if (state.barrierGbp_legacy != nullptr) {
449 auto const& gbp = state.barrierGbp_legacy;
Robert Carr0d480722017-01-10 16:42:54 -0800450 if (mFlinger->authenticateSurfaceTextureLocked(gbp)) {
451 otherLayer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
452 } else {
453 ALOGE("Attempt to defer transaction to to an unrecognized GraphicBufferProducer");
454 }
455 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700456 addDeferTransactionLocked(transaction, layerId, otherLayer, state.frameNumber_legacy);
Irvelc274c632016-06-13 16:44:08 -0700457 }
Irvelc274c632016-06-13 16:44:08 -0700458 if (state.what & layer_state_t::eOverrideScalingModeChanged) {
459 addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
460 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700461 if (state.what & layer_state_t::eReparent) {
462 addReparentLocked(transaction, layerId, getLayerIdFromHandle(state.parentHandleForChild));
463 }
464 if (state.what & layer_state_t::eReparentChildren) {
465 addReparentChildrenLocked(transaction, layerId, getLayerIdFromHandle(state.reparentHandle));
466 }
467 if (state.what & layer_state_t::eDetachChildren) {
468 addDetachChildrenLocked(transaction, layerId, true);
469 }
470 if (state.what & layer_state_t::eRelativeLayerChanged) {
471 addRelativeParentLocked(transaction, layerId,
472 getLayerIdFromHandle(state.relativeLayerHandle), state.z);
473 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800474 if (state.what & layer_state_t::eShadowRadiusChanged) {
475 addShadowRadiusLocked(transaction, layerId, state.shadowRadius);
476 }
Irvelc274c632016-06-13 16:44:08 -0700477}
478
Irvelffc9efc2016-07-27 15:16:37 -0700479void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700480 const DisplayState& state, int32_t sequenceId)
Irvelc274c632016-06-13 16:44:08 -0700481{
Irvelffc9efc2016-07-27 15:16:37 -0700482 if (state.what & DisplayState::eSurfaceChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700483 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
Irvelffc9efc2016-07-27 15:16:37 -0700484 }
485 if (state.what & DisplayState::eLayerStackChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700486 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
Irvelffc9efc2016-07-27 15:16:37 -0700487 }
488 if (state.what & DisplayState::eDisplaySizeChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700489 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
Irvelffc9efc2016-07-27 15:16:37 -0700490 }
491 if (state.what & DisplayState::eDisplayProjectionChanged) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800492 addDisplayProjectionLocked(transaction, sequenceId, toRotationInt(state.orientation),
493 state.viewport, state.frame);
Irvelc274c632016-06-13 16:44:08 -0700494 }
495}
496
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000497void SurfaceInterceptor::addTransactionLocked(
498 Increment* increment, const Vector<ComposerState>& stateUpdates,
499 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
500 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags, int originPID,
501 int originUID) {
Irvelffc9efc2016-07-27 15:16:37 -0700502 Transaction* transaction(increment->mutable_transaction());
503 transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
504 transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000505 setTransactionOriginLocked(transaction, originPID, originUID);
506
Irvelffc9efc2016-07-27 15:16:37 -0700507 for (const auto& compState: stateUpdates) {
508 addSurfaceChangesLocked(transaction, compState.state);
509 }
510 for (const auto& disp: changedDisplays) {
511 ssize_t dpyIdx = displays.indexOfKey(disp.token);
512 if (dpyIdx >= 0) {
513 const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
Dominik Laskowski663bd282018-04-19 15:26:54 -0700514 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700515 }
516 }
Irvelc274c632016-06-13 16:44:08 -0700517}
518
Irvelffc9efc2016-07-27 15:16:37 -0700519void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
520 const sp<const Layer>& layer)
521{
522 SurfaceCreation* creation(increment->mutable_surface_creation());
523 creation->set_id(getLayerId(layer));
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700524 creation->set_name(layer->getName());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800525 creation->set_w(layer->mCurrentState.active_legacy.w);
526 creation->set_h(layer->mCurrentState.active_legacy.h);
Irvelc274c632016-06-13 16:44:08 -0700527}
528
Irvelffc9efc2016-07-27 15:16:37 -0700529void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
530 const sp<const Layer>& layer)
531{
532 SurfaceDeletion* deletion(increment->mutable_surface_deletion());
533 deletion->set_id(getLayerId(layer));
534}
535
Rob Carra79435b2020-03-06 14:46:07 -0800536void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, int32_t layerId,
Irvelc274c632016-06-13 16:44:08 -0700537 uint32_t width, uint32_t height, uint64_t frameNumber)
538{
539 BufferUpdate* update(increment->mutable_buffer_update());
Rob Carra79435b2020-03-06 14:46:07 -0800540 update->set_id(layerId);
Irvelc274c632016-06-13 16:44:08 -0700541 update->set_w(width);
542 update->set_h(height);
543 update->set_frame_number(frameNumber);
544}
545
Irvelffc9efc2016-07-27 15:16:37 -0700546void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
Irvelc274c632016-06-13 16:44:08 -0700547 VSyncEvent* event(increment->mutable_vsync_event());
548 event->set_when(timestamp);
549}
550
Dominik Laskowski663bd282018-04-19 15:26:54 -0700551void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700552 const sp<const IGraphicBufferProducer>& surface)
Irvelc274c632016-06-13 16:44:08 -0700553{
Irvelffc9efc2016-07-27 15:16:37 -0700554 if (surface == nullptr) {
Irvelc274c632016-06-13 16:44:08 -0700555 return;
556 }
Irvelffc9efc2016-07-27 15:16:37 -0700557 uint64_t bufferQueueId = 0;
558 status_t err(surface->getUniqueId(&bufferQueueId));
559 if (err == NO_ERROR) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700560 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700561 DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
562 surfaceChange->set_buffer_queue_id(bufferQueueId);
563 surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
564 }
565 else {
566 ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
567 strerror(-err));
568 }
Irvelc274c632016-06-13 16:44:08 -0700569}
570
Irvelffc9efc2016-07-27 15:16:37 -0700571void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700572 int32_t sequenceId, uint32_t layerStack)
Irvelffc9efc2016-07-27 15:16:37 -0700573{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700574 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700575 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
576 layerStackChange->set_layer_stack(layerStack);
577}
578
Dominik Laskowski663bd282018-04-19 15:26:54 -0700579void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700580 uint32_t w, uint32_t h)
581{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700582 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700583 SizeChange* sizeChange(dispChange->mutable_size());
584 sizeChange->set_w(w);
585 sizeChange->set_h(h);
586}
587
588void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700589 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
Irvelffc9efc2016-07-27 15:16:37 -0700590{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700591 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700592 ProjectionChange* projectionChange(dispChange->mutable_projection());
593 projectionChange->set_orientation(orientation);
594 Rectangle* viewportRect(projectionChange->mutable_viewport());
595 setProtoRectLocked(viewportRect, viewport);
596 Rectangle* frameRect(projectionChange->mutable_frame());
597 setProtoRectLocked(frameRect, frame);
598}
599
600void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
601 const DisplayDeviceState& info)
602{
603 DisplayCreation* creation(increment->mutable_display_creation());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700604 creation->set_id(info.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700605 creation->set_name(info.displayName);
Irvelffc9efc2016-07-27 15:16:37 -0700606 creation->set_is_secure(info.isSecure);
Dominik Laskowski55c85402020-01-21 16:25:47 -0800607 if (info.physical) {
608 creation->set_display_id(info.physical->id.value);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700609 }
Irvelffc9efc2016-07-27 15:16:37 -0700610}
611
Dominik Laskowski663bd282018-04-19 15:26:54 -0700612void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700613 DisplayDeletion* deletion(increment->mutable_display_deletion());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700614 deletion->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700615}
616
Dominik Laskowski663bd282018-04-19 15:26:54 -0700617void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700618 int32_t mode)
619{
620 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700621 powerModeUpdate->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700622 powerModeUpdate->set_mode(mode);
623}
624
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000625void SurfaceInterceptor::saveTransaction(
626 const Vector<ComposerState>& stateUpdates,
627 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
628 const Vector<DisplayState>& changedDisplays, uint32_t flags, int originPID, int originUID) {
Irvelffc9efc2016-07-27 15:16:37 -0700629 if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
630 return;
631 }
Irvelc274c632016-06-13 16:44:08 -0700632 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700633 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
634 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000635 flags, originPID, originUID);
Irvelffc9efc2016-07-27 15:16:37 -0700636}
637
638void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700639 if (!mEnabled || layer == nullptr) {
640 return;
641 }
Irvelc274c632016-06-13 16:44:08 -0700642 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700643 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
644 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
645}
646
647void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700648 if (!mEnabled || layer == nullptr) {
649 return;
650 }
Irvelffc9efc2016-07-27 15:16:37 -0700651 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700652 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700653 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
Irvelc274c632016-06-13 16:44:08 -0700654}
655
Rob Carra79435b2020-03-06 14:46:07 -0800656/**
657 * Here we pass the layer by ID instead of by sp<> since this is called without
658 * holding the state-lock from a Binder thread. If we required the caller
659 * to pass 'this' by sp<> the temporary sp<> constructed could end up
660 * being the last reference and we might accidentally destroy the Layer
661 * from this binder thread.
662 */
663void SurfaceInterceptor::saveBufferUpdate(int32_t layerId, uint32_t width,
Irvelc274c632016-06-13 16:44:08 -0700664 uint32_t height, uint64_t frameNumber)
665{
Rob Carra79435b2020-03-06 14:46:07 -0800666 if (!mEnabled) {
Irvelc274c632016-06-13 16:44:08 -0700667 return;
668 }
Irvelffc9efc2016-07-27 15:16:37 -0700669 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700670 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Rob Carra79435b2020-03-06 14:46:07 -0800671 addBufferUpdateLocked(createTraceIncrementLocked(), layerId, width, height, frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700672}
673
674void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
675 if (!mEnabled) {
676 return;
677 }
678 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700679 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
Irvelc274c632016-06-13 16:44:08 -0700680}
681
Irvelffc9efc2016-07-27 15:16:37 -0700682void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
683 if (!mEnabled) {
684 return;
685 }
686 ATRACE_CALL();
687 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
688 addDisplayCreationLocked(createTraceIncrementLocked(), info);
689}
690
Dominik Laskowski663bd282018-04-19 15:26:54 -0700691void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700692 if (!mEnabled) {
693 return;
694 }
695 ATRACE_CALL();
696 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700697 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700698}
699
Dominik Laskowski663bd282018-04-19 15:26:54 -0700700void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
Irvelffc9efc2016-07-27 15:16:37 -0700701 if (!mEnabled) {
702 return;
703 }
704 ATRACE_CALL();
705 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700706 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
Irvelffc9efc2016-07-27 15:16:37 -0700707}
708
Lloyd Pique4dccc412018-01-22 17:21:36 -0800709} // namespace impl
Irvelc274c632016-06-13 16:44:08 -0700710} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800711
712// TODO(b/129481165): remove the #pragma below and fix conversion issues
713#pragma clang diagnostic pop // ignored "-Wconversion"