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 | |
Mark Salyzyn | 4dad9ce | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 | #include <utils/SortedVector.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | class BufferItem; |
| 30 | class Layer; |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 31 | class SurfaceFlinger; |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 32 | struct DisplayState; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 33 | struct layer_state_t; |
| 34 | |
| 35 | constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat"; |
| 36 | |
| 37 | /* |
| 38 | * SurfaceInterceptor intercepts and stores incoming streams of window |
| 39 | * properties on SurfaceFlinger. |
| 40 | */ |
| 41 | class SurfaceInterceptor { |
| 42 | public: |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 43 | SurfaceInterceptor(SurfaceFlinger* const flinger); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 44 | // Both vectors are used to capture the current state of SF as the initial snapshot in the trace |
| 45 | void enable(const SortedVector<sp<Layer>>& layers, |
| 46 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 47 | void disable(); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 48 | bool isEnabled(); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 49 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 50 | // Intercept display and surface transactions |
| 51 | void saveTransaction(const Vector<ComposerState>& stateUpdates, |
| 52 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays, |
| 53 | const Vector<DisplayState>& changedDisplays, uint32_t flags); |
| 54 | |
| 55 | // Intercept surface data |
| 56 | void saveSurfaceCreation(const sp<const Layer>& layer); |
| 57 | void saveSurfaceDeletion(const sp<const Layer>& layer); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 58 | void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height, |
| 59 | uint64_t frameNumber); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 60 | |
| 61 | // Intercept display data |
| 62 | void saveDisplayCreation(const DisplayDeviceState& info); |
| 63 | void saveDisplayDeletion(int32_t displayId); |
| 64 | void savePowerModeUpdate(int32_t displayId, int32_t mode); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 65 | void saveVSyncEvent(nsecs_t timestamp); |
| 66 | |
| 67 | private: |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 68 | // The creation increments of Surfaces and Displays do not contain enough information to capture |
| 69 | // the initial state of each object, so a transaction with all of the missing properties is |
| 70 | // performed at the initial snapshot for each display and surface. |
| 71 | void saveExistingDisplaysLocked( |
| 72 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays); |
| 73 | void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers); |
| 74 | void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer); |
| 75 | void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display); |
| 76 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 77 | status_t writeProtoFileLocked(); |
Irvel | 3017c61 | 2016-08-09 16:50:06 -0700 | [diff] [blame] | 78 | const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 79 | const std::string getLayerName(const sp<const Layer>& layer); |
| 80 | int32_t getLayerId(const sp<const Layer>& layer); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 81 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 82 | Increment* createTraceIncrementLocked(); |
| 83 | void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer); |
| 84 | void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer); |
| 85 | void addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer, uint32_t width, |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 86 | uint32_t height, uint64_t frameNumber); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 87 | void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp); |
| 88 | void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info); |
| 89 | void addDisplayDeletionLocked(Increment* increment, int32_t displayId); |
| 90 | void addPowerModeUpdateLocked(Increment* increment, int32_t displayId, int32_t mode); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 91 | |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 92 | // Add surface transactions to the trace |
| 93 | SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 94 | void setProtoRectLocked(Rectangle* protoRect, const Rect& rect); |
| 95 | void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y); |
| 96 | void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z); |
| 97 | void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h); |
| 98 | void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha); |
| 99 | void addMatrixLocked(Transaction* transaction, int32_t layerId, |
| 100 | const layer_state_t::matrix22_t& matrix); |
| 101 | void addTransparentRegionLocked(Transaction* transaction, int32_t layerId, |
| 102 | const Region& transRegion); |
| 103 | void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags); |
| 104 | void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack); |
| 105 | void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect); |
| 106 | void addDeferTransactionLocked(Transaction* transaction, int32_t layerId, |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 107 | const sp<const Layer>& layer, uint64_t frameNumber); |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 108 | void addFinalCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect); |
| 109 | void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId, |
| 110 | int32_t overrideScalingMode); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 111 | void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state); |
| 112 | void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates, |
| 113 | const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays, |
| 114 | const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags); |
| 115 | |
| 116 | // Add display transactions to the trace |
| 117 | DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t displayId); |
| 118 | void addDisplaySurfaceLocked(Transaction* transaction, int32_t displayId, |
| 119 | const sp<const IGraphicBufferProducer>& surface); |
| 120 | void addDisplayLayerStackLocked(Transaction* transaction, int32_t displayId, |
| 121 | uint32_t layerStack); |
| 122 | void addDisplaySizeLocked(Transaction* transaction, int32_t displayId, uint32_t w, |
| 123 | uint32_t h); |
| 124 | void addDisplayProjectionLocked(Transaction* transaction, int32_t displayId, |
| 125 | int32_t orientation, const Rect& viewport, const Rect& frame); |
| 126 | void addDisplayChangesLocked(Transaction* transaction, |
| 127 | const DisplayState& state, int32_t displayId); |
| 128 | |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 129 | |
| 130 | bool mEnabled {false}; |
| 131 | std::string mOutputFileName {DEFAULT_FILENAME}; |
| 132 | std::mutex mTraceMutex {}; |
| 133 | Trace mTrace {}; |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 134 | SurfaceFlinger* const mFlinger; |
Irvel | c274c63 | 2016-06-13 16:44:08 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | #endif // ANDROID_SURFACEINTERCEPTOR_H |