blob: 9af6e612ec3e2cef9427eb2a72f79976dde83d89 [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;
Irvelffc9efc2016-07-27 15:16:37 -070031struct DisplayState;
Irvelc274c632016-06-13 16:44:08 -070032struct layer_state_t;
33
34constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat";
35
36/*
37 * SurfaceInterceptor intercepts and stores incoming streams of window
38 * properties on SurfaceFlinger.
39 */
40class SurfaceInterceptor {
41public:
Irvelffc9efc2016-07-27 15:16:37 -070042 // Both vectors are used to capture the current state of SF as the initial snapshot in the trace
43 void enable(const SortedVector<sp<Layer>>& layers,
44 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays);
Irvelc274c632016-06-13 16:44:08 -070045 void disable();
Irvelffc9efc2016-07-27 15:16:37 -070046 bool isEnabled();
Irvelc274c632016-06-13 16:44:08 -070047
Irvelffc9efc2016-07-27 15:16:37 -070048 // Intercept display and surface transactions
49 void saveTransaction(const Vector<ComposerState>& stateUpdates,
50 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
51 const Vector<DisplayState>& changedDisplays, uint32_t flags);
52
53 // Intercept surface data
54 void saveSurfaceCreation(const sp<const Layer>& layer);
55 void saveSurfaceDeletion(const sp<const Layer>& layer);
Irvelc274c632016-06-13 16:44:08 -070056 void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height,
57 uint64_t frameNumber);
Irvelffc9efc2016-07-27 15:16:37 -070058
59 // Intercept display data
60 void saveDisplayCreation(const DisplayDeviceState& info);
61 void saveDisplayDeletion(int32_t displayId);
62 void savePowerModeUpdate(int32_t displayId, int32_t mode);
Irvelc274c632016-06-13 16:44:08 -070063 void saveVSyncEvent(nsecs_t timestamp);
64
65private:
Irvelffc9efc2016-07-27 15:16:37 -070066 // The creation increments of Surfaces and Displays do not contain enough information to capture
67 // the initial state of each object, so a transaction with all of the missing properties is
68 // performed at the initial snapshot for each display and surface.
69 void saveExistingDisplaysLocked(
70 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays);
71 void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers);
72 void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer);
73 void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display);
74
Irvelc274c632016-06-13 16:44:08 -070075 status_t writeProtoFileLocked();
Irvel3017c612016-08-09 16:50:06 -070076 const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle);
Irvelc274c632016-06-13 16:44:08 -070077 const std::string getLayerName(const sp<const Layer>& layer);
78 int32_t getLayerId(const sp<const Layer>& layer);
Irvelc274c632016-06-13 16:44:08 -070079
Irvelffc9efc2016-07-27 15:16:37 -070080 Increment* createTraceIncrementLocked();
81 void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer);
82 void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer);
83 void addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer, uint32_t width,
Irvelc274c632016-06-13 16:44:08 -070084 uint32_t height, uint64_t frameNumber);
Irvelffc9efc2016-07-27 15:16:37 -070085 void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp);
86 void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info);
87 void addDisplayDeletionLocked(Increment* increment, int32_t displayId);
88 void addPowerModeUpdateLocked(Increment* increment, int32_t displayId, int32_t mode);
Irvelc274c632016-06-13 16:44:08 -070089
Irvelffc9efc2016-07-27 15:16:37 -070090 // Add surface transactions to the trace
91 SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId);
Irvelc274c632016-06-13 16:44:08 -070092 void setProtoRectLocked(Rectangle* protoRect, const Rect& rect);
93 void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y);
94 void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z);
95 void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h);
96 void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha);
97 void addMatrixLocked(Transaction* transaction, int32_t layerId,
98 const layer_state_t::matrix22_t& matrix);
99 void addTransparentRegionLocked(Transaction* transaction, int32_t layerId,
100 const Region& transRegion);
101 void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags);
102 void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack);
103 void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
104 void addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Irvel3017c612016-08-09 16:50:06 -0700105 const wp<const IBinder>& weakHandle, uint64_t frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700106 void addFinalCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
107 void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId,
108 int32_t overrideScalingMode);
Irvelffc9efc2016-07-27 15:16:37 -0700109 void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state);
110 void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates,
111 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
112 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags);
113
114 // Add display transactions to the trace
115 DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t displayId);
116 void addDisplaySurfaceLocked(Transaction* transaction, int32_t displayId,
117 const sp<const IGraphicBufferProducer>& surface);
118 void addDisplayLayerStackLocked(Transaction* transaction, int32_t displayId,
119 uint32_t layerStack);
120 void addDisplaySizeLocked(Transaction* transaction, int32_t displayId, uint32_t w,
121 uint32_t h);
122 void addDisplayProjectionLocked(Transaction* transaction, int32_t displayId,
123 int32_t orientation, const Rect& viewport, const Rect& frame);
124 void addDisplayChangesLocked(Transaction* transaction,
125 const DisplayState& state, int32_t displayId);
126
Irvelc274c632016-06-13 16:44:08 -0700127
128 bool mEnabled {false};
129 std::string mOutputFileName {DEFAULT_FILENAME};
130 std::mutex mTraceMutex {};
131 Trace mTrace {};
132};
133
134}
135
136#endif // ANDROID_SURFACEINTERCEPTOR_H