blob: c5f1598d156e5264dca9907e1ecbb0c02799eb4c [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
Pablo Gamito6ee484d2020-07-30 14:26:28 +000043void SurfaceInterceptor::addTransactionTraceListener(
44 const sp<gui::ITransactionTraceListener>& listener) {
45 sp<IBinder> asBinder = IInterface::asBinder(listener);
46
47 std::scoped_lock lock(mListenersMutex);
48
49 asBinder->linkToDeath(this);
50
51 listener->onToggled(mEnabled); // notifies of current state
52
53 mTraceToggledListeners.emplace(asBinder, listener);
54}
55
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000056void SurfaceInterceptor::binderDied(const wp<IBinder>& who) {
Pablo Gamito6ee484d2020-07-30 14:26:28 +000057 std::scoped_lock lock(mListenersMutex);
58 mTraceToggledListeners.erase(who);
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000059}
60
Irvelffc9efc2016-07-27 15:16:37 -070061void SurfaceInterceptor::enable(const SortedVector<sp<Layer>>& layers,
62 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
63{
Irvelc274c632016-06-13 16:44:08 -070064 if (mEnabled) {
65 return;
66 }
Irvelffc9efc2016-07-27 15:16:37 -070067 ATRACE_CALL();
Pablo Gamito6ee484d2020-07-30 14:26:28 +000068 {
69 std::scoped_lock lock(mListenersMutex);
70 for (const auto& [_, listener] : mTraceToggledListeners) {
71 listener->onToggled(true);
72 }
73 }
Irvelc274c632016-06-13 16:44:08 -070074 mEnabled = true;
Pablo Gamito6ee484d2020-07-30 14:26:28 +000075 std::scoped_lock<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -070076 saveExistingDisplaysLocked(displays);
77 saveExistingSurfacesLocked(layers);
Irvelc274c632016-06-13 16:44:08 -070078}
79
80void SurfaceInterceptor::disable() {
Irvelc274c632016-06-13 16:44:08 -070081 if (!mEnabled) {
82 return;
83 }
Irvelffc9efc2016-07-27 15:16:37 -070084 ATRACE_CALL();
Pablo Gamito6ee484d2020-07-30 14:26:28 +000085 {
86 std::scoped_lock lock(mListenersMutex);
87 for (const auto& [_, listener] : mTraceToggledListeners) {
88 listener->onToggled(false);
89 }
90 }
Irvelc274c632016-06-13 16:44:08 -070091 mEnabled = false;
Pablo Gamito6ee484d2020-07-30 14:26:28 +000092 std::scoped_lock<std::mutex> protoGuard(mTraceMutex);
Irvelc274c632016-06-13 16:44:08 -070093 status_t err(writeProtoFileLocked());
94 ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
95 ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
96 mTrace.Clear();
97}
98
Irvelffc9efc2016-07-27 15:16:37 -070099bool SurfaceInterceptor::isEnabled() {
100 return mEnabled;
101}
102
103void SurfaceInterceptor::saveExistingDisplaysLocked(
104 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays)
105{
106 // Caveat: The initial snapshot does not capture the power mode of the existing displays
Irvelc274c632016-06-13 16:44:08 -0700107 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700108 for (size_t i = 0 ; i < displays.size() ; i++) {
109 addDisplayCreationLocked(createTraceIncrementLocked(), displays[i]);
110 addInitialDisplayStateLocked(createTraceIncrementLocked(), displays[i]);
Irvelc274c632016-06-13 16:44:08 -0700111 }
112}
113
Irvelffc9efc2016-07-27 15:16:37 -0700114void SurfaceInterceptor::saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers) {
Irvelc274c632016-06-13 16:44:08 -0700115 ATRACE_CALL();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700116 for (const auto& l : layers) {
Dan Stoza412903f2017-04-27 13:42:17 -0700117 l->traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700118 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
119 addInitialSurfaceStateLocked(createTraceIncrementLocked(), layer);
120 });
Irvelc274c632016-06-13 16:44:08 -0700121 }
Irvelffc9efc2016-07-27 15:16:37 -0700122}
123
124void SurfaceInterceptor::addInitialSurfaceStateLocked(Increment* increment,
125 const sp<const Layer>& layer)
126{
Irvelc274c632016-06-13 16:44:08 -0700127 Transaction* transaction(increment->mutable_transaction());
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700128 const uint32_t layerFlags = layer->getTransactionFlags();
129 transaction->set_synchronous(layerFlags & BnSurfaceComposer::eSynchronous);
130 transaction->set_animation(layerFlags & BnSurfaceComposer::eAnimation);
Irvelc274c632016-06-13 16:44:08 -0700131
132 const int32_t layerId(getLayerId(layer));
chaviw4e765532021-04-30 12:11:39 -0500133 addPositionLocked(transaction, layerId, layer->mCurrentState.transform.tx(),
134 layer->mCurrentState.transform.ty());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800135 addDepthLocked(transaction, layerId, layer->mCurrentState.z);
136 addAlphaLocked(transaction, layerId, layer->mCurrentState.color.a);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700137 addTransparentRegionLocked(transaction, layerId,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 layer->mCurrentState.activeTransparentRegion_legacy);
139 addLayerStackLocked(transaction, layerId, layer->mCurrentState.layerStack);
chaviw25714502021-02-11 10:01:08 -0800140 addCropLocked(transaction, layerId, layer->mCurrentState.crop);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 addCornerRadiusLocked(transaction, layerId, layer->mCurrentState.cornerRadius);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800142 addBackgroundBlurRadiusLocked(transaction, layerId, layer->mCurrentState.backgroundBlurRadius);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700143 addBlurRegionsLocked(transaction, layerId, layer->mCurrentState.blurRegions);
Vishnu Nair456bbb22019-07-18 16:02:00 -0700144 addFlagsLocked(transaction, layerId, layer->mCurrentState.flags,
145 layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
146 layer_state_t::eLayerSecure);
147 addReparentLocked(transaction, layerId, getLayerIdFromWeakRef(layer->mCurrentParent));
Vishnu Nair456bbb22019-07-18 16:02:00 -0700148 addRelativeParentLocked(transaction, layerId,
149 getLayerIdFromWeakRef(layer->mCurrentState.zOrderRelativeOf),
150 layer->mCurrentState.z);
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800151 addShadowRadiusLocked(transaction, layerId, layer->mCurrentState.shadowRadius);
Irvelc274c632016-06-13 16:44:08 -0700152}
153
Irvelffc9efc2016-07-27 15:16:37 -0700154void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
155 const DisplayDeviceState& display)
156{
157 Transaction* transaction(increment->mutable_transaction());
158 transaction->set_synchronous(false);
159 transaction->set_animation(false);
160
Dominik Laskowski663bd282018-04-19 15:26:54 -0700161 addDisplaySurfaceLocked(transaction, display.sequenceId, display.surface);
162 addDisplayLayerStackLocked(transaction, display.sequenceId, display.layerStack);
163 addDisplaySizeLocked(transaction, display.sequenceId, display.width, display.height);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800164 addDisplayProjectionLocked(transaction, display.sequenceId, toRotationInt(display.orientation),
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200165 display.layerStackSpaceRect, display.orientedDisplaySpaceRect);
Irvelffc9efc2016-07-27 15:16:37 -0700166}
167
Irvelc274c632016-06-13 16:44:08 -0700168status_t SurfaceInterceptor::writeProtoFileLocked() {
169 ATRACE_CALL();
Colin Cross63549382016-10-26 12:52:53 -0700170 std::string output;
171
Irvelc274c632016-06-13 16:44:08 -0700172 if (!mTrace.IsInitialized()) {
173 return NOT_ENOUGH_DATA;
174 }
Colin Cross63549382016-10-26 12:52:53 -0700175 if (!mTrace.SerializeToString(&output)) {
Irvelc274c632016-06-13 16:44:08 -0700176 return PERMISSION_DENIED;
177 }
Colin Cross63549382016-10-26 12:52:53 -0700178 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
179 return PERMISSION_DENIED;
180 }
181
Irvelc274c632016-06-13 16:44:08 -0700182 return NO_ERROR;
183}
184
Vishnu Nair456bbb22019-07-18 16:02:00 -0700185const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) const {
Irvel3017c612016-08-09 16:50:06 -0700186 const sp<const IBinder>& handle(weakHandle.promote());
Irvelc274c632016-06-13 16:44:08 -0700187 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
188 const sp<const Layer> layer(layerHandle->owner.promote());
189 // layer could be a nullptr at this point
190 return layer;
191}
192
Vishnu Nair456bbb22019-07-18 16:02:00 -0700193int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) const {
Irvelc274c632016-06-13 16:44:08 -0700194 return layer->sequence;
195}
196
Vishnu Nair456bbb22019-07-18 16:02:00 -0700197int32_t SurfaceInterceptor::getLayerIdFromWeakRef(const wp<const Layer>& layer) const {
198 if (layer == nullptr) {
199 return -1;
200 }
201 auto strongLayer = layer.promote();
202 return strongLayer == nullptr ? -1 : getLayerId(strongLayer);
203}
204
205int32_t SurfaceInterceptor::getLayerIdFromHandle(const sp<const IBinder>& handle) const {
206 if (handle == nullptr) {
207 return -1;
208 }
209 const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
210 const sp<const Layer> layer(layerHandle->owner.promote());
211 return layer == nullptr ? -1 : getLayerId(layer);
212}
213
Irvelffc9efc2016-07-27 15:16:37 -0700214Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Irvelc274c632016-06-13 16:44:08 -0700215 Increment* increment(mTrace.add_increment());
Vishnu Nair45cb21a2020-03-27 17:39:24 -0700216 increment->set_time_stamp(elapsedRealtimeNano());
Irvelc274c632016-06-13 16:44:08 -0700217 return increment;
218}
219
Irvelffc9efc2016-07-27 15:16:37 -0700220SurfaceChange* SurfaceInterceptor::createSurfaceChangeLocked(Transaction* transaction,
221 int32_t layerId)
222{
223 SurfaceChange* change(transaction->add_surface_change());
Irvelc274c632016-06-13 16:44:08 -0700224 change->set_id(layerId);
225 return change;
226}
227
Irvelffc9efc2016-07-27 15:16:37 -0700228DisplayChange* SurfaceInterceptor::createDisplayChangeLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700229 int32_t sequenceId)
Irvelffc9efc2016-07-27 15:16:37 -0700230{
231 DisplayChange* dispChange(transaction->add_display_change());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700232 dispChange->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700233 return dispChange;
234}
235
Irvelc274c632016-06-13 16:44:08 -0700236void SurfaceInterceptor::setProtoRectLocked(Rectangle* protoRect, const Rect& rect) {
237 protoRect->set_left(rect.left);
238 protoRect->set_top(rect.top);
239 protoRect->set_right(rect.right);
240 protoRect->set_bottom(rect.bottom);
241}
242
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000243void SurfaceInterceptor::setTransactionOriginLocked(Transaction* transaction, int32_t pid,
244 int32_t uid) {
245 Origin* origin(transaction->mutable_origin());
246 origin->set_pid(pid);
247 origin->set_uid(uid);
248}
249
Irvelffc9efc2016-07-27 15:16:37 -0700250void SurfaceInterceptor::addPositionLocked(Transaction* transaction, int32_t layerId,
251 float x, float y)
Irvelc274c632016-06-13 16:44:08 -0700252{
Irvelffc9efc2016-07-27 15:16:37 -0700253 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700254 PositionChange* posChange(change->mutable_position());
255 posChange->set_x(x);
256 posChange->set_y(y);
257}
258
Irvelffc9efc2016-07-27 15:16:37 -0700259void SurfaceInterceptor::addDepthLocked(Transaction* transaction, int32_t layerId,
260 uint32_t z)
261{
262 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700263 LayerChange* depthChange(change->mutable_layer());
264 depthChange->set_layer(z);
265}
266
267void SurfaceInterceptor::addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w,
268 uint32_t h)
269{
Irvelffc9efc2016-07-27 15:16:37 -0700270 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700271 SizeChange* sizeChange(change->mutable_size());
272 sizeChange->set_w(w);
273 sizeChange->set_h(h);
274}
275
Irvelffc9efc2016-07-27 15:16:37 -0700276void SurfaceInterceptor::addAlphaLocked(Transaction* transaction, int32_t layerId,
277 float alpha)
278{
279 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700280 AlphaChange* alphaChange(change->mutable_alpha());
281 alphaChange->set_alpha(alpha);
282}
283
284void SurfaceInterceptor::addMatrixLocked(Transaction* transaction, int32_t layerId,
285 const layer_state_t::matrix22_t& matrix)
286{
Irvelffc9efc2016-07-27 15:16:37 -0700287 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700288 MatrixChange* matrixChange(change->mutable_matrix());
289 matrixChange->set_dsdx(matrix.dsdx);
290 matrixChange->set_dtdx(matrix.dtdx);
291 matrixChange->set_dsdy(matrix.dsdy);
292 matrixChange->set_dtdy(matrix.dtdy);
293}
294
Irvelffc9efc2016-07-27 15:16:37 -0700295void SurfaceInterceptor::addTransparentRegionLocked(Transaction* transaction,
296 int32_t layerId, const Region& transRegion)
Irvelc274c632016-06-13 16:44:08 -0700297{
Irvelffc9efc2016-07-27 15:16:37 -0700298 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700299 TransparentRegionHintChange* transparentChange(change->mutable_transparent_region_hint());
300
301 for (const auto& rect : transRegion) {
302 Rectangle* protoRect(transparentChange->add_region());
303 setProtoRectLocked(protoRect, rect);
304 }
305}
306
Vishnu Nair456bbb22019-07-18 16:02:00 -0700307void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags,
308 uint8_t mask) {
Irvelc274c632016-06-13 16:44:08 -0700309 // There can be multiple flags changed
Vishnu Nair456bbb22019-07-18 16:02:00 -0700310 if (mask & layer_state_t::eLayerHidden) {
Irvelffc9efc2016-07-27 15:16:37 -0700311 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700312 HiddenFlagChange* flagChange(change->mutable_hidden_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700313 flagChange->set_hidden_flag(flags & layer_state_t::eLayerHidden);
Irvelc274c632016-06-13 16:44:08 -0700314 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700315 if (mask & layer_state_t::eLayerOpaque) {
Irvelffc9efc2016-07-27 15:16:37 -0700316 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700317 OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700318 flagChange->set_opaque_flag(flags & layer_state_t::eLayerOpaque);
Irvelc274c632016-06-13 16:44:08 -0700319 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700320 if (mask & layer_state_t::eLayerSecure) {
Irvelffc9efc2016-07-27 15:16:37 -0700321 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700322 SecureFlagChange* flagChange(change->mutable_secure_flag());
Vishnu Nair456bbb22019-07-18 16:02:00 -0700323 flagChange->set_secure_flag(flags & layer_state_t::eLayerSecure);
Irvelc274c632016-06-13 16:44:08 -0700324 }
325}
326
327void SurfaceInterceptor::addLayerStackLocked(Transaction* transaction, int32_t layerId,
328 uint32_t layerStack)
329{
Irvelffc9efc2016-07-27 15:16:37 -0700330 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700331 LayerStackChange* layerStackChange(change->mutable_layer_stack());
332 layerStackChange->set_layer_stack(layerStack);
333}
334
335void SurfaceInterceptor::addCropLocked(Transaction* transaction, int32_t layerId,
336 const Rect& rect)
337{
Irvelffc9efc2016-07-27 15:16:37 -0700338 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
Irvelc274c632016-06-13 16:44:08 -0700339 CropChange* cropChange(change->mutable_crop());
340 Rectangle* protoRect(cropChange->mutable_rectangle());
341 setProtoRectLocked(protoRect, rect);
342}
343
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700344void SurfaceInterceptor::addCornerRadiusLocked(Transaction* transaction, int32_t layerId,
345 float cornerRadius)
346{
347 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
348 CornerRadiusChange* cornerRadiusChange(change->mutable_corner_radius());
349 cornerRadiusChange->set_corner_radius(cornerRadius);
350}
351
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800352void SurfaceInterceptor::addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId,
353 int32_t backgroundBlurRadius) {
354 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
355 BackgroundBlurRadiusChange* blurRadiusChange(change->mutable_background_blur_radius());
356 blurRadiusChange->set_background_blur_radius(backgroundBlurRadius);
357}
358
Lucas Dupinc3800b82020-10-02 16:24:48 -0700359void SurfaceInterceptor::addBlurRegionsLocked(Transaction* transaction, int32_t layerId,
360 const std::vector<BlurRegion>& blurRegions) {
361 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
362 BlurRegionsChange* blurRegionsChange(change->mutable_blur_regions());
363 for (const auto blurRegion : blurRegions) {
364 const auto blurRegionChange = blurRegionsChange->add_blur_regions();
365 blurRegionChange->set_blur_radius(blurRegion.blurRadius);
366 blurRegionChange->set_corner_radius_tl(blurRegion.cornerRadiusTL);
367 blurRegionChange->set_corner_radius_tr(blurRegion.cornerRadiusTR);
368 blurRegionChange->set_corner_radius_bl(blurRegion.cornerRadiusBL);
369 blurRegionChange->set_corner_radius_br(blurRegion.cornerRadiusBR);
370 blurRegionChange->set_alpha(blurRegion.alpha);
371 blurRegionChange->set_left(blurRegion.left);
372 blurRegionChange->set_top(blurRegion.top);
373 blurRegionChange->set_right(blurRegion.right);
374 blurRegionChange->set_bottom(blurRegion.bottom);
375 }
376}
377
Vishnu Nair456bbb22019-07-18 16:02:00 -0700378void SurfaceInterceptor::addReparentLocked(Transaction* transaction, int32_t layerId,
379 int32_t parentId) {
380 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
381 ReparentChange* overrideChange(change->mutable_reparent());
382 overrideChange->set_parent_id(parentId);
383}
384
Vishnu Nair456bbb22019-07-18 16:02:00 -0700385void SurfaceInterceptor::addRelativeParentLocked(Transaction* transaction, int32_t layerId,
386 int32_t parentId, int z) {
387 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
388 RelativeParentChange* overrideChange(change->mutable_relative_parent());
389 overrideChange->set_relative_parent_id(parentId);
390 overrideChange->set_z(z);
391}
392
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800393void SurfaceInterceptor::addShadowRadiusLocked(Transaction* transaction, int32_t layerId,
394 float shadowRadius) {
395 SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
396 ShadowRadiusChange* overrideChange(change->mutable_shadow_radius());
397 overrideChange->set_radius(shadowRadius);
398}
399
Irvelffc9efc2016-07-27 15:16:37 -0700400void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
Irvelc274c632016-06-13 16:44:08 -0700401 const layer_state_t& state)
402{
403 const sp<const Layer> layer(getLayer(state.surface));
404 if (layer == nullptr) {
405 ALOGE("An existing layer could not be retrieved with the surface "
406 "from the layer_state_t surface in the update transaction");
407 return;
408 }
409
410 const int32_t layerId(getLayerId(layer));
411
412 if (state.what & layer_state_t::ePositionChanged) {
413 addPositionLocked(transaction, layerId, state.x, state.y);
414 }
415 if (state.what & layer_state_t::eLayerChanged) {
416 addDepthLocked(transaction, layerId, state.z);
417 }
418 if (state.what & layer_state_t::eSizeChanged) {
419 addSizeLocked(transaction, layerId, state.w, state.h);
420 }
421 if (state.what & layer_state_t::eAlphaChanged) {
422 addAlphaLocked(transaction, layerId, state.alpha);
423 }
424 if (state.what & layer_state_t::eMatrixChanged) {
425 addMatrixLocked(transaction, layerId, state.matrix);
426 }
427 if (state.what & layer_state_t::eTransparentRegionChanged) {
428 addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
429 }
430 if (state.what & layer_state_t::eFlagsChanged) {
Vishnu Nair456bbb22019-07-18 16:02:00 -0700431 addFlagsLocked(transaction, layerId, state.flags, state.mask);
Irvelc274c632016-06-13 16:44:08 -0700432 }
433 if (state.what & layer_state_t::eLayerStackChanged) {
434 addLayerStackLocked(transaction, layerId, state.layerStack);
435 }
chaviw25714502021-02-11 10:01:08 -0800436 if (state.what & layer_state_t::eCropChanged) {
437 addCropLocked(transaction, layerId, state.crop);
Irvelc274c632016-06-13 16:44:08 -0700438 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700439 if (state.what & layer_state_t::eCornerRadiusChanged) {
440 addCornerRadiusLocked(transaction, layerId, state.cornerRadius);
441 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800442 if (state.what & layer_state_t::eBackgroundBlurRadiusChanged) {
443 addBackgroundBlurRadiusLocked(transaction, layerId, state.backgroundBlurRadius);
444 }
Lucas Dupinc3800b82020-10-02 16:24:48 -0700445 if (state.what & layer_state_t::eBlurRegionsChanged) {
446 addBlurRegionsLocked(transaction, layerId, state.blurRegions);
447 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700448 if (state.what & layer_state_t::eReparent) {
Pablo Gamito11dcc222020-09-12 15:49:39 +0000449 auto parentHandle = (state.parentSurfaceControlForChild)
450 ? state.parentSurfaceControlForChild->getHandle()
451 : nullptr;
452 addReparentLocked(transaction, layerId, getLayerIdFromHandle(parentHandle));
Vishnu Nair456bbb22019-07-18 16:02:00 -0700453 }
Vishnu Nair456bbb22019-07-18 16:02:00 -0700454 if (state.what & layer_state_t::eRelativeLayerChanged) {
455 addRelativeParentLocked(transaction, layerId,
Pablo Gamito11dcc222020-09-12 15:49:39 +0000456 getLayerIdFromHandle(
457 state.relativeLayerSurfaceControl->getHandle()),
458 state.z);
Vishnu Nair456bbb22019-07-18 16:02:00 -0700459 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800460 if (state.what & layer_state_t::eShadowRadiusChanged) {
461 addShadowRadiusLocked(transaction, layerId, state.shadowRadius);
462 }
John Reckc00c6692021-02-16 11:37:33 -0500463 if (state.what & layer_state_t::eStretchChanged) {
464 ALOGW("SurfaceInterceptor not implemented for eStretchChanged");
465 }
Irvelc274c632016-06-13 16:44:08 -0700466}
467
Irvelffc9efc2016-07-27 15:16:37 -0700468void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700469 const DisplayState& state, int32_t sequenceId)
Irvelc274c632016-06-13 16:44:08 -0700470{
Irvelffc9efc2016-07-27 15:16:37 -0700471 if (state.what & DisplayState::eSurfaceChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700472 addDisplaySurfaceLocked(transaction, sequenceId, state.surface);
Irvelffc9efc2016-07-27 15:16:37 -0700473 }
474 if (state.what & DisplayState::eLayerStackChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700475 addDisplayLayerStackLocked(transaction, sequenceId, state.layerStack);
Irvelffc9efc2016-07-27 15:16:37 -0700476 }
477 if (state.what & DisplayState::eDisplaySizeChanged) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700478 addDisplaySizeLocked(transaction, sequenceId, state.width, state.height);
Irvelffc9efc2016-07-27 15:16:37 -0700479 }
480 if (state.what & DisplayState::eDisplayProjectionChanged) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800481 addDisplayProjectionLocked(transaction, sequenceId, toRotationInt(state.orientation),
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200482 state.layerStackSpaceRect, state.orientedDisplaySpaceRect);
Irvelc274c632016-06-13 16:44:08 -0700483 }
484}
485
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000486void SurfaceInterceptor::addTransactionLocked(
487 Increment* increment, const Vector<ComposerState>& stateUpdates,
488 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000489 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags, int originPid,
490 int originUid, uint64_t transactionId) {
Irvelffc9efc2016-07-27 15:16:37 -0700491 Transaction* transaction(increment->mutable_transaction());
492 transaction->set_synchronous(transactionFlags & BnSurfaceComposer::eSynchronous);
493 transaction->set_animation(transactionFlags & BnSurfaceComposer::eAnimation);
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000494 setTransactionOriginLocked(transaction, originPid, originUid);
495 transaction->set_id(transactionId);
Irvelffc9efc2016-07-27 15:16:37 -0700496 for (const auto& compState: stateUpdates) {
497 addSurfaceChangesLocked(transaction, compState.state);
498 }
499 for (const auto& disp: changedDisplays) {
500 ssize_t dpyIdx = displays.indexOfKey(disp.token);
501 if (dpyIdx >= 0) {
502 const DisplayDeviceState& dispState(displays.valueAt(dpyIdx));
Dominik Laskowski663bd282018-04-19 15:26:54 -0700503 addDisplayChangesLocked(transaction, disp, dispState.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700504 }
505 }
Irvelc274c632016-06-13 16:44:08 -0700506}
507
Irvelffc9efc2016-07-27 15:16:37 -0700508void SurfaceInterceptor::addSurfaceCreationLocked(Increment* increment,
509 const sp<const Layer>& layer)
510{
511 SurfaceCreation* creation(increment->mutable_surface_creation());
512 creation->set_id(getLayerId(layer));
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700513 creation->set_name(layer->getName());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800514 creation->set_w(layer->mCurrentState.active_legacy.w);
515 creation->set_h(layer->mCurrentState.active_legacy.h);
Irvelc274c632016-06-13 16:44:08 -0700516}
517
Irvelffc9efc2016-07-27 15:16:37 -0700518void SurfaceInterceptor::addSurfaceDeletionLocked(Increment* increment,
519 const sp<const Layer>& layer)
520{
521 SurfaceDeletion* deletion(increment->mutable_surface_deletion());
522 deletion->set_id(getLayerId(layer));
523}
524
Rob Carra79435b2020-03-06 14:46:07 -0800525void SurfaceInterceptor::addBufferUpdateLocked(Increment* increment, int32_t layerId,
Irvelc274c632016-06-13 16:44:08 -0700526 uint32_t width, uint32_t height, uint64_t frameNumber)
527{
528 BufferUpdate* update(increment->mutable_buffer_update());
Rob Carra79435b2020-03-06 14:46:07 -0800529 update->set_id(layerId);
Irvelc274c632016-06-13 16:44:08 -0700530 update->set_w(width);
531 update->set_h(height);
532 update->set_frame_number(frameNumber);
533}
534
Irvelffc9efc2016-07-27 15:16:37 -0700535void SurfaceInterceptor::addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp) {
Irvelc274c632016-06-13 16:44:08 -0700536 VSyncEvent* event(increment->mutable_vsync_event());
537 event->set_when(timestamp);
538}
539
Dominik Laskowski663bd282018-04-19 15:26:54 -0700540void SurfaceInterceptor::addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700541 const sp<const IGraphicBufferProducer>& surface)
Irvelc274c632016-06-13 16:44:08 -0700542{
Irvelffc9efc2016-07-27 15:16:37 -0700543 if (surface == nullptr) {
Irvelc274c632016-06-13 16:44:08 -0700544 return;
545 }
Irvelffc9efc2016-07-27 15:16:37 -0700546 uint64_t bufferQueueId = 0;
547 status_t err(surface->getUniqueId(&bufferQueueId));
548 if (err == NO_ERROR) {
Dominik Laskowski663bd282018-04-19 15:26:54 -0700549 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700550 DispSurfaceChange* surfaceChange(dispChange->mutable_surface());
551 surfaceChange->set_buffer_queue_id(bufferQueueId);
552 surfaceChange->set_buffer_queue_name(surface->getConsumerName().string());
553 }
554 else {
555 ALOGE("invalid graphic buffer producer received while tracing a display change (%s)",
556 strerror(-err));
557 }
Irvelc274c632016-06-13 16:44:08 -0700558}
559
Irvelffc9efc2016-07-27 15:16:37 -0700560void SurfaceInterceptor::addDisplayLayerStackLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700561 int32_t sequenceId, uint32_t layerStack)
Irvelffc9efc2016-07-27 15:16:37 -0700562{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700563 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700564 LayerStackChange* layerStackChange(dispChange->mutable_layer_stack());
565 layerStackChange->set_layer_stack(layerStack);
566}
567
Dominik Laskowski663bd282018-04-19 15:26:54 -0700568void SurfaceInterceptor::addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700569 uint32_t w, uint32_t h)
570{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700571 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700572 SizeChange* sizeChange(dispChange->mutable_size());
573 sizeChange->set_w(w);
574 sizeChange->set_h(h);
575}
576
577void SurfaceInterceptor::addDisplayProjectionLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700578 int32_t sequenceId, int32_t orientation, const Rect& viewport, const Rect& frame)
Irvelffc9efc2016-07-27 15:16:37 -0700579{
Dominik Laskowski663bd282018-04-19 15:26:54 -0700580 DisplayChange* dispChange(createDisplayChangeLocked(transaction, sequenceId));
Irvelffc9efc2016-07-27 15:16:37 -0700581 ProjectionChange* projectionChange(dispChange->mutable_projection());
582 projectionChange->set_orientation(orientation);
583 Rectangle* viewportRect(projectionChange->mutable_viewport());
584 setProtoRectLocked(viewportRect, viewport);
585 Rectangle* frameRect(projectionChange->mutable_frame());
586 setProtoRectLocked(frameRect, frame);
587}
588
589void SurfaceInterceptor::addDisplayCreationLocked(Increment* increment,
590 const DisplayDeviceState& info)
591{
592 DisplayCreation* creation(increment->mutable_display_creation());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700593 creation->set_id(info.sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700594 creation->set_name(info.displayName);
Irvelffc9efc2016-07-27 15:16:37 -0700595 creation->set_is_secure(info.isSecure);
Dominik Laskowski55c85402020-01-21 16:25:47 -0800596 if (info.physical) {
597 creation->set_display_id(info.physical->id.value);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700598 }
Irvelffc9efc2016-07-27 15:16:37 -0700599}
600
Dominik Laskowski663bd282018-04-19 15:26:54 -0700601void SurfaceInterceptor::addDisplayDeletionLocked(Increment* increment, int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700602 DisplayDeletion* deletion(increment->mutable_display_deletion());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700603 deletion->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700604}
605
Dominik Laskowski663bd282018-04-19 15:26:54 -0700606void SurfaceInterceptor::addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700607 int32_t mode)
608{
609 PowerModeUpdate* powerModeUpdate(increment->mutable_power_mode_update());
Dominik Laskowski663bd282018-04-19 15:26:54 -0700610 powerModeUpdate->set_id(sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700611 powerModeUpdate->set_mode(mode);
612}
613
Pablo Gamito3e8f0e62020-06-22 15:55:39 +0000614void SurfaceInterceptor::saveTransaction(
615 const Vector<ComposerState>& stateUpdates,
616 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000617 const Vector<DisplayState>& changedDisplays, uint32_t flags, int originPid, int originUid,
618 uint64_t transactionId) {
Irvelffc9efc2016-07-27 15:16:37 -0700619 if (!mEnabled || (stateUpdates.size() <= 0 && changedDisplays.size() <= 0)) {
620 return;
621 }
Irvelc274c632016-06-13 16:44:08 -0700622 ATRACE_CALL();
Irvelffc9efc2016-07-27 15:16:37 -0700623 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
624 addTransactionLocked(createTraceIncrementLocked(), stateUpdates, displays, changedDisplays,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000625 flags, originPid, originUid, transactionId);
Irvelffc9efc2016-07-27 15:16:37 -0700626}
627
628void SurfaceInterceptor::saveSurfaceCreation(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700629 if (!mEnabled || layer == nullptr) {
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 addSurfaceCreationLocked(createTraceIncrementLocked(), layer);
635}
636
637void SurfaceInterceptor::saveSurfaceDeletion(const sp<const Layer>& layer) {
Irvelc274c632016-06-13 16:44:08 -0700638 if (!mEnabled || layer == nullptr) {
639 return;
640 }
Irvelffc9efc2016-07-27 15:16:37 -0700641 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700642 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700643 addSurfaceDeletionLocked(createTraceIncrementLocked(), layer);
Irvelc274c632016-06-13 16:44:08 -0700644}
645
Rob Carra79435b2020-03-06 14:46:07 -0800646/**
647 * Here we pass the layer by ID instead of by sp<> since this is called without
648 * holding the state-lock from a Binder thread. If we required the caller
649 * to pass 'this' by sp<> the temporary sp<> constructed could end up
650 * being the last reference and we might accidentally destroy the Layer
651 * from this binder thread.
652 */
653void SurfaceInterceptor::saveBufferUpdate(int32_t layerId, uint32_t width,
Irvelc274c632016-06-13 16:44:08 -0700654 uint32_t height, uint64_t frameNumber)
655{
Rob Carra79435b2020-03-06 14:46:07 -0800656 if (!mEnabled) {
Irvelc274c632016-06-13 16:44:08 -0700657 return;
658 }
Irvelffc9efc2016-07-27 15:16:37 -0700659 ATRACE_CALL();
Irvelc274c632016-06-13 16:44:08 -0700660 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Rob Carra79435b2020-03-06 14:46:07 -0800661 addBufferUpdateLocked(createTraceIncrementLocked(), layerId, width, height, frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700662}
663
664void SurfaceInterceptor::saveVSyncEvent(nsecs_t timestamp) {
665 if (!mEnabled) {
666 return;
667 }
668 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Irvelffc9efc2016-07-27 15:16:37 -0700669 addVSyncUpdateLocked(createTraceIncrementLocked(), timestamp);
Irvelc274c632016-06-13 16:44:08 -0700670}
671
Irvelffc9efc2016-07-27 15:16:37 -0700672void SurfaceInterceptor::saveDisplayCreation(const DisplayDeviceState& info) {
673 if (!mEnabled) {
674 return;
675 }
676 ATRACE_CALL();
677 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
678 addDisplayCreationLocked(createTraceIncrementLocked(), info);
679}
680
Dominik Laskowski663bd282018-04-19 15:26:54 -0700681void SurfaceInterceptor::saveDisplayDeletion(int32_t sequenceId) {
Irvelffc9efc2016-07-27 15:16:37 -0700682 if (!mEnabled) {
683 return;
684 }
685 ATRACE_CALL();
686 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700687 addDisplayDeletionLocked(createTraceIncrementLocked(), sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700688}
689
Dominik Laskowski663bd282018-04-19 15:26:54 -0700690void SurfaceInterceptor::savePowerModeUpdate(int32_t sequenceId, int32_t mode) {
Irvelffc9efc2016-07-27 15:16:37 -0700691 if (!mEnabled) {
692 return;
693 }
694 ATRACE_CALL();
695 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700696 addPowerModeUpdateLocked(createTraceIncrementLocked(), sequenceId, mode);
Irvelffc9efc2016-07-27 15:16:37 -0700697}
698
Lloyd Pique4dccc412018-01-22 17:21:36 -0800699} // namespace impl
Irvelc274c632016-06-13 16:44:08 -0700700} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800701
702// TODO(b/129481165): remove the #pragma below and fix conversion issues
703#pragma clang diagnostic pop // ignored "-Wconversion"