| 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; | 
|  | 42 |  | 
|  | 43 | constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat"; | 
|  | 44 |  | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 45 | class SurfaceInterceptor { | 
|  | 46 | public: | 
|  | 47 | virtual ~SurfaceInterceptor(); | 
|  | 48 |  | 
|  | 49 | // Both vectors are used to capture the current state of SF as the initial snapshot in the trace | 
|  | 50 | virtual void enable(const SortedVector<sp<Layer>>& layers, | 
|  | 51 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) = 0; | 
|  | 52 | virtual void disable() = 0; | 
|  | 53 | virtual bool isEnabled() = 0; | 
|  | 54 |  | 
|  | 55 | // Intercept display and surface transactions | 
|  | 56 | virtual void saveTransaction( | 
|  | 57 | const Vector<ComposerState>& stateUpdates, | 
|  | 58 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, | 
|  | 59 | const Vector<DisplayState>& changedDisplays, uint32_t flags) = 0; | 
|  | 60 |  | 
|  | 61 | // Intercept surface data | 
|  | 62 | virtual void saveSurfaceCreation(const sp<const Layer>& layer) = 0; | 
|  | 63 | virtual void saveSurfaceDeletion(const sp<const Layer>& layer) = 0; | 
|  | 64 | virtual void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height, | 
|  | 65 | uint64_t frameNumber) = 0; | 
|  | 66 |  | 
|  | 67 | // Intercept display data | 
|  | 68 | virtual void saveDisplayCreation(const DisplayDeviceState& info) = 0; | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 69 | virtual void saveDisplayDeletion(int32_t sequenceId) = 0; | 
|  | 70 | virtual void savePowerModeUpdate(int32_t sequenceId, int32_t mode) = 0; | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 71 | virtual void saveVSyncEvent(nsecs_t timestamp) = 0; | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | namespace impl { | 
|  | 75 |  | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 76 | /* | 
|  | 77 | * SurfaceInterceptor intercepts and stores incoming streams of window | 
|  | 78 | * properties on SurfaceFlinger. | 
|  | 79 | */ | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 80 | class SurfaceInterceptor final : public android::SurfaceInterceptor { | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 81 | public: | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 82 | explicit SurfaceInterceptor(SurfaceFlinger* const flinger); | 
|  | 83 | ~SurfaceInterceptor() override = default; | 
|  | 84 |  | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 85 | // Both vectors are used to capture the current state of SF as the initial snapshot in the trace | 
|  | 86 | void enable(const SortedVector<sp<Layer>>& layers, | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 87 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) override; | 
|  | 88 | void disable() override; | 
|  | 89 | bool isEnabled() override; | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 90 |  | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 91 | // Intercept display and surface transactions | 
|  | 92 | void saveTransaction(const Vector<ComposerState>& stateUpdates, | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 93 | const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, | 
|  | 94 | const Vector<DisplayState>& changedDisplays, uint32_t flags) override; | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | // Intercept surface data | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 97 | void saveSurfaceCreation(const sp<const Layer>& layer) override; | 
|  | 98 | void saveSurfaceDeletion(const sp<const Layer>& layer) override; | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 99 | void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height, | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 100 | uint64_t frameNumber) override; | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | // Intercept display data | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 103 | void saveDisplayCreation(const DisplayDeviceState& info) override; | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 104 | void saveDisplayDeletion(int32_t sequenceId) override; | 
|  | 105 | void savePowerModeUpdate(int32_t sequenceId, int32_t mode) override; | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 106 | void saveVSyncEvent(nsecs_t timestamp) override; | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | private: | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 109 | // The creation increments of Surfaces and Displays do not contain enough information to capture | 
|  | 110 | // the initial state of each object, so a transaction with all of the missing properties is | 
|  | 111 | // performed at the initial snapshot for each display and surface. | 
|  | 112 | void saveExistingDisplaysLocked( | 
|  | 113 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays); | 
|  | 114 | void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers); | 
|  | 115 | void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer); | 
|  | 116 | void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display); | 
|  | 117 |  | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 118 | status_t writeProtoFileLocked(); | 
| Irvel | 3017c61 | 2016-08-09 16:50:06 -0700 | [diff] [blame] | 119 | const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle); | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 120 | const std::string getLayerName(const sp<const Layer>& layer); | 
|  | 121 | int32_t getLayerId(const sp<const Layer>& layer); | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 122 |  | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 123 | Increment* createTraceIncrementLocked(); | 
|  | 124 | void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer); | 
|  | 125 | void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer); | 
|  | 126 | void addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer, uint32_t width, | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 127 | uint32_t height, uint64_t frameNumber); | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 128 | void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp); | 
|  | 129 | void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info); | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 130 | void addDisplayDeletionLocked(Increment* increment, int32_t sequenceId); | 
|  | 131 | void addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId, int32_t mode); | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 132 |  | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 133 | // Add surface transactions to the trace | 
|  | 134 | SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId); | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 135 | void setProtoRectLocked(Rectangle* protoRect, const Rect& rect); | 
|  | 136 | void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y); | 
|  | 137 | void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z); | 
|  | 138 | void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h); | 
|  | 139 | void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha); | 
|  | 140 | void addMatrixLocked(Transaction* transaction, int32_t layerId, | 
|  | 141 | const layer_state_t::matrix22_t& matrix); | 
|  | 142 | void addTransparentRegionLocked(Transaction* transaction, int32_t layerId, | 
|  | 143 | const Region& transRegion); | 
|  | 144 | void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags); | 
|  | 145 | void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack); | 
|  | 146 | void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect); | 
|  | 147 | void addDeferTransactionLocked(Transaction* transaction, int32_t layerId, | 
| Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 148 | const sp<const Layer>& layer, uint64_t frameNumber); | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 149 | void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId, | 
|  | 150 | int32_t overrideScalingMode); | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 151 | void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state); | 
|  | 152 | void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates, | 
|  | 153 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays, | 
|  | 154 | const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags); | 
|  | 155 |  | 
|  | 156 | // Add display transactions to the trace | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 157 | DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t sequenceId); | 
|  | 158 | void addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId, | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 159 | const sp<const IGraphicBufferProducer>& surface); | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 160 | void addDisplayLayerStackLocked(Transaction* transaction, int32_t sequenceId, | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 161 | uint32_t layerStack); | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 162 | void addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId, uint32_t w, | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 163 | uint32_t h); | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 164 | void addDisplayProjectionLocked(Transaction* transaction, int32_t sequenceId, | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 165 | int32_t orientation, const Rect& viewport, const Rect& frame); | 
|  | 166 | void addDisplayChangesLocked(Transaction* transaction, | 
| Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 167 | const DisplayState& state, int32_t sequenceId); | 
| Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 168 |  | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | bool mEnabled {false}; | 
|  | 171 | std::string mOutputFileName {DEFAULT_FILENAME}; | 
|  | 172 | std::mutex mTraceMutex {}; | 
|  | 173 | Trace mTrace {}; | 
| Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 174 | SurfaceFlinger* const mFlinger; | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 175 | }; | 
|  | 176 |  | 
| Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 177 | } // namespace impl | 
|  | 178 | } // namespace android | 
| Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | #endif // ANDROID_SURFACEINTERCEPTOR_H |