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