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 20 #include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h> 21 22 #include <mutex> 23 24 #include <gui/LayerState.h> 25 26 #include <utils/KeyedVector.h> 27 #include <utils/SortedVector.h> 28 #include <utils/StrongPointer.h> 29 #include <utils/Vector.h> 30 31 #include "DisplayDevice.h" 32 33 namespace android { 34 35 class BufferItem; 36 class Layer; 37 class SurfaceFlinger; 38 struct ComposerState; 39 struct DisplayDeviceState; 40 struct DisplayState; 41 struct layer_state_t; 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; 48 49 constexpr auto DEFAULT_FILENAME = "/data/misc/wmtrace/transaction_trace.pb"; 50 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, 65 const Vector<DisplayState>& changedDisplays, uint32_t flags) = 0; 66 67 // Intercept surface data 68 virtual void saveSurfaceCreation(const sp<const Layer>& layer) = 0; 69 virtual void saveSurfaceDeletion(const sp<const Layer>& layer) = 0; 70 virtual void saveBufferUpdate(int32_t layerId, uint32_t width, uint32_t height, 71 uint64_t frameNumber) = 0; 72 73 // Intercept display data 74 virtual void saveDisplayCreation(const DisplayDeviceState& info) = 0; 75 virtual void saveDisplayDeletion(int32_t sequenceId) = 0; 76 virtual void savePowerModeUpdate(int32_t sequenceId, int32_t mode) = 0; 77 virtual void saveVSyncEvent(nsecs_t timestamp) = 0; 78 }; 79 80 namespace impl { 81 82 /* 83 * SurfaceInterceptor intercepts and stores incoming streams of window 84 * properties on SurfaceFlinger. 85 */ 86 class SurfaceInterceptor final : public android::SurfaceInterceptor { 87 public: 88 explicit SurfaceInterceptor(SurfaceFlinger* const flinger); 89 ~SurfaceInterceptor() override = default; 90 91 // Both vectors are used to capture the current state of SF as the initial snapshot in the trace 92 void enable(const SortedVector<sp<Layer>>& layers, 93 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) override; 94 void disable() override; 95 bool isEnabled() override; 96 97 // Intercept display and surface transactions 98 void saveTransaction(const Vector<ComposerState>& stateUpdates, 99 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays, 100 const Vector<DisplayState>& changedDisplays, uint32_t flags) override; 101 102 // Intercept surface data 103 void saveSurfaceCreation(const sp<const Layer>& layer) override; 104 void saveSurfaceDeletion(const sp<const Layer>& layer) override; 105 void saveBufferUpdate(int32_t layerId, uint32_t width, uint32_t height, 106 uint64_t frameNumber) override; 107 108 // Intercept display data 109 void saveDisplayCreation(const DisplayDeviceState& info) override; 110 void saveDisplayDeletion(int32_t sequenceId) override; 111 void savePowerModeUpdate(int32_t sequenceId, int32_t mode) override; 112 void saveVSyncEvent(nsecs_t timestamp) override; 113 114 private: 115 // The creation increments of Surfaces and Displays do not contain enough information to capture 116 // the initial state of each object, so a transaction with all of the missing properties is 117 // performed at the initial snapshot for each display and surface. 118 void saveExistingDisplaysLocked( 119 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays); 120 void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers); 121 void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer); 122 void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display); 123 124 status_t writeProtoFileLocked(); 125 const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle) const; 126 int32_t getLayerId(const sp<const Layer>& layer) const; 127 int32_t getLayerIdFromWeakRef(const wp<const Layer>& layer) const; 128 int32_t getLayerIdFromHandle(const sp<const IBinder>& weakHandle) const; 129 130 Increment* createTraceIncrementLocked(); 131 void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer); 132 void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer); 133 void addBufferUpdateLocked(Increment* increment, int32_t layerId, uint32_t width, 134 uint32_t height, uint64_t frameNumber); 135 void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp); 136 void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info); 137 void addDisplayDeletionLocked(Increment* increment, int32_t sequenceId); 138 void addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId, int32_t mode); 139 140 // Add surface transactions to the trace 141 SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId); 142 void setProtoRectLocked(Rectangle* protoRect, const Rect& rect); 143 void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y); 144 void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z); 145 void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h); 146 void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha); 147 void addMatrixLocked(Transaction* transaction, int32_t layerId, 148 const layer_state_t::matrix22_t& matrix); 149 void addTransparentRegionLocked(Transaction* transaction, int32_t layerId, 150 const Region& transRegion); 151 void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags, uint8_t mask); 152 void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack); 153 void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect); 154 void addCornerRadiusLocked(Transaction* transaction, int32_t layerId, float cornerRadius); 155 void addBackgroundBlurRadiusLocked(Transaction* transaction, int32_t layerId, 156 int32_t backgroundBlurRadius); 157 void addDeferTransactionLocked(Transaction* transaction, int32_t layerId, 158 const sp<const Layer>& layer, uint64_t frameNumber); 159 void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId, 160 int32_t overrideScalingMode); 161 void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state); 162 void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates, 163 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays, 164 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags); 165 void addReparentLocked(Transaction* transaction, int32_t layerId, int32_t parentId); 166 void addReparentChildrenLocked(Transaction* transaction, int32_t layerId, int32_t parentId); 167 void addDetachChildrenLocked(Transaction* transaction, int32_t layerId, bool detached); 168 void addRelativeParentLocked(Transaction* transaction, int32_t layerId, int32_t parentId, 169 int z); 170 void addShadowRadiusLocked(Transaction* transaction, int32_t layerId, float shadowRadius); 171 172 // Add display transactions to the trace 173 DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t sequenceId); 174 void addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId, 175 const sp<const IGraphicBufferProducer>& surface); 176 void addDisplayLayerStackLocked(Transaction* transaction, int32_t sequenceId, 177 uint32_t layerStack); 178 void addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId, uint32_t w, 179 uint32_t h); 180 void addDisplayProjectionLocked(Transaction* transaction, int32_t sequenceId, 181 int32_t orientation, const Rect& viewport, const Rect& frame); 182 void addDisplayChangesLocked(Transaction* transaction, 183 const DisplayState& state, int32_t sequenceId); 184 185 186 bool mEnabled {false}; 187 std::string mOutputFileName {DEFAULT_FILENAME}; 188 std::mutex mTraceMutex {}; 189 Trace mTrace {}; 190 SurfaceFlinger* const mFlinger; 191 }; 192 193 } // namespace impl 194 195 } // namespace android 196 197 #endif // ANDROID_SURFACEINTERCEPTOR_H 198