Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_SURFACEINTERCEPTOR_H |
| 18 | #define ANDROID_SURFACEINTERCEPTOR_H |
| 19 | |
Sahil Dhanju | a05cafa | 2016-07-29 09:37:48 -0700 | [diff] [blame] | 20 | #include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h> |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 21 | |
| 22 | #include <mutex> |
| 23 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 24 | #include <gui/LayerState.h> |
| 25 | |
| 26 | #include <utils/KeyedVector.h> |
Mark Salyzyn | 4dad9ce | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 27 | #include <utils/SortedVector.h> |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 28 | #include <utils/StrongPointer.h> |
Mark Salyzyn | 4dad9ce | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 29 | #include <utils/Vector.h> |
| 30 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 31 | #include "DisplayDevice.h" |
| 32 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | class BufferItem; |
| 36 | class Layer; |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 37 | class SurfaceFlinger; |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 38 | struct ComposerState; |
| 39 | struct DisplayDeviceState; |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 40 | struct DisplayState; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 41 | struct layer_state_t; |
Robert Delgado | cb12994 | 2019-07-23 16:28:20 -0700 | [diff] [blame] | 42 | using Transaction = surfaceflinger::Transaction; |
| 43 | using Trace = surfaceflinger::Trace; |
| 44 | using Rectangle = surfaceflinger::Rectangle; |
| 45 | using SurfaceChange = surfaceflinger::SurfaceChange; |
| 46 | using Increment = surfaceflinger::Increment; |
| 47 | using DisplayChange = surfaceflinger::DisplayChange; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 48 | |
Robert Delgado | aa74a66 | 2019-07-31 13:02:33 -0700 | [diff] [blame] | 49 | constexpr auto DEFAULT_FILENAME = "/data/misc/wmtrace/transaction_trace.pb"; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 50 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 51 | class SurfaceInterceptor { |
| 52 | public: |
| 53 | virtual ~SurfaceInterceptor(); |
| 54 | |
| 55 | // Both vectors are used to capture the current state of SF as the initial snapshot in the trace |
| 56 | virtual void enable(const SortedVector<sp<Layer>>& layers, |
| 57 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) = 0; |
| 58 | virtual void disable() = 0; |
| 59 | virtual bool isEnabled() = 0; |
| 60 | |
| 61 | // Intercept display and surface transactions |
| 62 | virtual void saveTransaction( |
| 63 | const Vector<ComposerState>& stateUpdates, |
| 64 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, |
Pablo Gamito | 3e8f0e6 | 2020-06-22 15:55:39 +0000 | [diff] [blame] | 65 | const Vector<DisplayState>& changedDisplays, uint32_t flags, int originPID, |
| 66 | int originUID) = 0; |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 67 | |
| 68 | // Intercept surface data |
| 69 | virtual void saveSurfaceCreation(const sp<const Layer>& layer) = 0; |
| 70 | virtual void saveSurfaceDeletion(const sp<const Layer>& layer) = 0; |
Rob Carr | a79435b | 2020-03-06 14:46:07 -0800 | [diff] [blame] | 71 | virtual void saveBufferUpdate(int32_t layerId, uint32_t width, uint32_t height, |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 72 | uint64_t frameNumber) = 0; |
| 73 | |
| 74 | // Intercept display data |
| 75 | virtual void saveDisplayCreation(const DisplayDeviceState& info) = 0; |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 76 | virtual void saveDisplayDeletion(int32_t sequenceId) = 0; |
| 77 | virtual void savePowerModeUpdate(int32_t sequenceId, int32_t mode) = 0; |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 78 | virtual void saveVSyncEvent(nsecs_t timestamp) = 0; |
| 79 | }; |
| 80 | |
| 81 | namespace impl { |
| 82 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 83 | /* |
| 84 | * SurfaceInterceptor intercepts and stores incoming streams of window |
| 85 | * properties on SurfaceFlinger. |
| 86 | */ |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 87 | class SurfaceInterceptor final : public android::SurfaceInterceptor { |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 88 | public: |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 89 | explicit SurfaceInterceptor(SurfaceFlinger* const flinger); |
| 90 | ~SurfaceInterceptor() override = default; |
| 91 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 92 | // Both vectors are used to capture the current state of SF as the initial snapshot in the trace |
| 93 | void enable(const SortedVector<sp<Layer>>& layers, |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 94 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) override; |
| 95 | void disable() override; |
| 96 | bool isEnabled() override; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 97 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 98 | // Intercept display and surface transactions |
| 99 | void saveTransaction(const Vector<ComposerState>& stateUpdates, |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 100 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, |
Pablo Gamito | 3e8f0e6 | 2020-06-22 15:55:39 +0000 | [diff] [blame] | 101 | const Vector<DisplayState>& changedDisplays, uint32_t flags, int originPID, |
| 102 | int originUID) override; |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 103 | |
| 104 | // Intercept surface data |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 105 | void saveSurfaceCreation(const sp<const Layer>& layer) override; |
| 106 | void saveSurfaceDeletion(const sp<const Layer>& layer) override; |
Rob Carr | a79435b | 2020-03-06 14:46:07 -0800 | [diff] [blame] | 107 | void saveBufferUpdate(int32_t layerId, uint32_t width, uint32_t height, |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 108 | uint64_t frameNumber) override; |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 109 | |
| 110 | // Intercept display data |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 111 | void saveDisplayCreation(const DisplayDeviceState& info) override; |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 112 | void saveDisplayDeletion(int32_t sequenceId) override; |
| 113 | void savePowerModeUpdate(int32_t sequenceId, int32_t mode) override; |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 114 | void saveVSyncEvent(nsecs_t timestamp) override; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 115 | |
| 116 | private: |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 117 | // The creation increments of Surfaces and Displays do not contain enough information to capture |
| 118 | // the initial state of each object, so a transaction with all of the missing properties is |
| 119 | // performed at the initial snapshot for each display and surface. |
| 120 | void saveExistingDisplaysLocked( |
| 121 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays); |
| 122 | void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers); |
| 123 | void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer); |
| 124 | void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display); |
| 125 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 126 | status_t writeProtoFileLocked(); |
Vishnu Nair | 456bbb2 | 2019-07-18 16:02:00 -0700 | [diff] [blame] | 127 | const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle) const; |
Vishnu Nair | 456bbb2 | 2019-07-18 16:02:00 -0700 | [diff] [blame] | 128 | int32_t getLayerId(const sp<const Layer>& layer) const; |
| 129 | int32_t getLayerIdFromWeakRef(const wp<const Layer>& layer) const; |
| 130 | int32_t getLayerIdFromHandle(const sp<const IBinder>& weakHandle) const; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 131 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 132 | Increment* createTraceIncrementLocked(); |
| 133 | void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer); |
| 134 | void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer); |
Rob Carr | a79435b | 2020-03-06 14:46:07 -0800 | [diff] [blame] | 135 | void addBufferUpdateLocked(Increment* increment, int32_t layerId, uint32_t width, |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 136 | uint32_t height, uint64_t frameNumber); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 137 | void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp); |
| 138 | void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 139 | void addDisplayDeletionLocked(Increment* increment, int32_t sequenceId); |
| 140 | void addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId, int32_t mode); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 141 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 142 | // Add surface transactions to the trace |
| 143 | SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 144 | void setProtoRectLocked(Rectangle* protoRect, const Rect& rect); |
| 145 | void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y); |
| 146 | void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z); |
| 147 | void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h); |
| 148 | void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha); |
| 149 | void addMatrixLocked(Transaction* transaction, int32_t layerId, |
| 150 | const layer_state_t::matrix22_t& matrix); |
| 151 | void addTransparentRegionLocked(Transaction* transaction, int32_t layerId, |
| 152 | const Region& transRegion); |
Vishnu Nair | 456bbb2 | 2019-07-18 16:02:00 -0700 | [diff] [blame] | 153 | void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags, uint8_t mask); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 154 | void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack); |
| 155 | void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect); |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 156 | void addCornerRadiusLocked(Transaction* transaction, int32_t layerId, float cornerRadius); |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 157 | void addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId, |
| 158 | int32_t backgroundBlurRadius); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 159 | void addDeferTransactionLocked(Transaction* transaction, int32_t layerId, |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 160 | const sp<const Layer>& layer, uint64_t frameNumber); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 161 | void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId, |
| 162 | int32_t overrideScalingMode); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 163 | void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state); |
| 164 | void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates, |
Pablo Gamito | 3e8f0e6 | 2020-06-22 15:55:39 +0000 | [diff] [blame] | 165 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, |
| 166 | const Vector<DisplayState>& changedDisplays, |
| 167 | uint32_t transactionFlags, int originPID, int originUID); |
Vishnu Nair | 456bbb2 | 2019-07-18 16:02:00 -0700 | [diff] [blame] | 168 | void addReparentLocked(Transaction* transaction, int32_t layerId, int32_t parentId); |
| 169 | void addReparentChildrenLocked(Transaction* transaction, int32_t layerId, int32_t parentId); |
| 170 | void addDetachChildrenLocked(Transaction* transaction, int32_t layerId, bool detached); |
| 171 | void addRelativeParentLocked(Transaction* transaction, int32_t layerId, int32_t parentId, |
| 172 | int z); |
Vishnu Nair | 95a1ed4 | 2019-12-06 12:25:11 -0800 | [diff] [blame] | 173 | void addShadowRadiusLocked(Transaction* transaction, int32_t layerId, float shadowRadius); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 174 | |
| 175 | // Add display transactions to the trace |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 176 | DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t sequenceId); |
| 177 | void addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId, |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 178 | const sp<const IGraphicBufferProducer>& surface); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 179 | void addDisplayLayerStackLocked(Transaction* transaction, int32_t sequenceId, |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 180 | uint32_t layerStack); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 181 | void addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId, uint32_t w, |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 182 | uint32_t h); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 183 | void addDisplayProjectionLocked(Transaction* transaction, int32_t sequenceId, |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 184 | int32_t orientation, const Rect& viewport, const Rect& frame); |
| 185 | void addDisplayChangesLocked(Transaction* transaction, |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 186 | const DisplayState& state, int32_t sequenceId); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 187 | |
Pablo Gamito | 3e8f0e6 | 2020-06-22 15:55:39 +0000 | [diff] [blame] | 188 | // Add transaction origin to trace |
| 189 | void setTransactionOriginLocked(Transaction* transaction, int32_t pid, int32_t uid); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 190 | |
| 191 | bool mEnabled {false}; |
| 192 | std::string mOutputFileName {DEFAULT_FILENAME}; |
| 193 | std::mutex mTraceMutex {}; |
| 194 | Trace mTrace {}; |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 195 | SurfaceFlinger* const mFlinger; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 198 | } // namespace impl |
Robert Delgado | cb12994 | 2019-07-23 16:28:20 -0700 | [diff] [blame] | 199 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 200 | } // namespace android |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 201 | |
| 202 | #endif // ANDROID_SURFACEINTERCEPTOR_H |