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