blob: 30ebcc6a1cb7451eab0ff18e6971836c064d306f [file] [log] [blame]
Irvelc274c632016-06-13 16:44:08 -07001/*
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 Dhanjua05cafa2016-07-29 09:37:48 -070020#include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h>
Irvelc274c632016-06-13 16:44:08 -070021
22#include <mutex>
23
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070024#include <utils/SortedVector.h>
25#include <utils/Vector.h>
26
Irvelc274c632016-06-13 16:44:08 -070027namespace android {
28
29class BufferItem;
30class Layer;
Robert Carr0d480722017-01-10 16:42:54 -080031class SurfaceFlinger;
Irvelffc9efc2016-07-27 15:16:37 -070032struct DisplayState;
Irvelc274c632016-06-13 16:44:08 -070033struct layer_state_t;
34
35constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat";
36
37/*
38 * SurfaceInterceptor intercepts and stores incoming streams of window
39 * properties on SurfaceFlinger.
40 */
41class SurfaceInterceptor {
42public:
Robert Carr0d480722017-01-10 16:42:54 -080043 SurfaceInterceptor(SurfaceFlinger* const flinger);
Irvelffc9efc2016-07-27 15:16:37 -070044 // 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);
Irvelc274c632016-06-13 16:44:08 -070047 void disable();
Irvelffc9efc2016-07-27 15:16:37 -070048 bool isEnabled();
Irvelc274c632016-06-13 16:44:08 -070049
Irvelffc9efc2016-07-27 15:16:37 -070050 // 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);
Irvelc274c632016-06-13 16:44:08 -070058 void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height,
59 uint64_t frameNumber);
Irvelffc9efc2016-07-27 15:16:37 -070060
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);
Irvelc274c632016-06-13 16:44:08 -070065 void saveVSyncEvent(nsecs_t timestamp);
66
67private:
Irvelffc9efc2016-07-27 15:16:37 -070068 // 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
Irvelc274c632016-06-13 16:44:08 -070077 status_t writeProtoFileLocked();
Irvel3017c612016-08-09 16:50:06 -070078 const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle);
Irvelc274c632016-06-13 16:44:08 -070079 const std::string getLayerName(const sp<const Layer>& layer);
80 int32_t getLayerId(const sp<const Layer>& layer);
Irvelc274c632016-06-13 16:44:08 -070081
Irvelffc9efc2016-07-27 15:16:37 -070082 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,
Irvelc274c632016-06-13 16:44:08 -070086 uint32_t height, uint64_t frameNumber);
Irvelffc9efc2016-07-27 15:16:37 -070087 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);
Irvelc274c632016-06-13 16:44:08 -070091
Irvelffc9efc2016-07-27 15:16:37 -070092 // Add surface transactions to the trace
93 SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId);
Irvelc274c632016-06-13 16:44:08 -070094 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 Carr0d480722017-01-10 16:42:54 -0800107 const sp<const Layer>& layer, uint64_t frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700108 void addFinalCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
109 void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId,
110 int32_t overrideScalingMode);
Irvelffc9efc2016-07-27 15:16:37 -0700111 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
Irvelc274c632016-06-13 16:44:08 -0700129
130 bool mEnabled {false};
131 std::string mOutputFileName {DEFAULT_FILENAME};
132 std::mutex mTraceMutex {};
133 Trace mTrace {};
Robert Carr0d480722017-01-10 16:42:54 -0800134 SurfaceFlinger* const mFlinger;
Irvelc274c632016-06-13 16:44:08 -0700135};
136
137}
138
139#endif // ANDROID_SURFACEINTERCEPTOR_H