blob: 7f86c1423a86aa99509114b736f6f88011e01d78 [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
Lloyd Pique4dccc412018-01-22 17:21:36 -080024#include <gui/LayerState.h>
25
26#include <utils/KeyedVector.h>
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070027#include <utils/SortedVector.h>
Lloyd Pique4dccc412018-01-22 17:21:36 -080028#include <utils/StrongPointer.h>
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070029#include <utils/Vector.h>
30
Lloyd Pique4dccc412018-01-22 17:21:36 -080031#include "DisplayDevice.h"
32
Irvelc274c632016-06-13 16:44:08 -070033namespace android {
34
35class BufferItem;
36class Layer;
Robert Carr0d480722017-01-10 16:42:54 -080037class SurfaceFlinger;
Lloyd Pique4dccc412018-01-22 17:21:36 -080038struct ComposerState;
39struct DisplayDeviceState;
Irvelffc9efc2016-07-27 15:16:37 -070040struct DisplayState;
Irvelc274c632016-06-13 16:44:08 -070041struct layer_state_t;
42
43constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat";
44
Lloyd Pique4dccc412018-01-22 17:21:36 -080045class SurfaceInterceptor {
46public:
47 virtual ~SurfaceInterceptor();
48
49 // Both vectors are used to capture the current state of SF as the initial snapshot in the trace
50 virtual void enable(const SortedVector<sp<Layer>>& layers,
51 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) = 0;
52 virtual void disable() = 0;
53 virtual bool isEnabled() = 0;
54
55 // Intercept display and surface transactions
56 virtual void saveTransaction(
57 const Vector<ComposerState>& stateUpdates,
58 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
59 const Vector<DisplayState>& changedDisplays, uint32_t flags) = 0;
60
61 // Intercept surface data
62 virtual void saveSurfaceCreation(const sp<const Layer>& layer) = 0;
63 virtual void saveSurfaceDeletion(const sp<const Layer>& layer) = 0;
64 virtual void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height,
65 uint64_t frameNumber) = 0;
66
67 // Intercept display data
68 virtual void saveDisplayCreation(const DisplayDeviceState& info) = 0;
Dominik Laskowski663bd282018-04-19 15:26:54 -070069 virtual void saveDisplayDeletion(int32_t sequenceId) = 0;
70 virtual void savePowerModeUpdate(int32_t sequenceId, int32_t mode) = 0;
Lloyd Pique4dccc412018-01-22 17:21:36 -080071 virtual void saveVSyncEvent(nsecs_t timestamp) = 0;
72};
73
74namespace impl {
75
Irvelc274c632016-06-13 16:44:08 -070076/*
77 * SurfaceInterceptor intercepts and stores incoming streams of window
78 * properties on SurfaceFlinger.
79 */
Lloyd Pique4dccc412018-01-22 17:21:36 -080080class SurfaceInterceptor final : public android::SurfaceInterceptor {
Irvelc274c632016-06-13 16:44:08 -070081public:
Lloyd Pique4dccc412018-01-22 17:21:36 -080082 explicit SurfaceInterceptor(SurfaceFlinger* const flinger);
83 ~SurfaceInterceptor() override = default;
84
Irvelffc9efc2016-07-27 15:16:37 -070085 // Both vectors are used to capture the current state of SF as the initial snapshot in the trace
86 void enable(const SortedVector<sp<Layer>>& layers,
Lloyd Pique4dccc412018-01-22 17:21:36 -080087 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays) override;
88 void disable() override;
89 bool isEnabled() override;
Irvelc274c632016-06-13 16:44:08 -070090
Irvelffc9efc2016-07-27 15:16:37 -070091 // Intercept display and surface transactions
92 void saveTransaction(const Vector<ComposerState>& stateUpdates,
Lloyd Pique4dccc412018-01-22 17:21:36 -080093 const DefaultKeyedVector<wp<IBinder>, DisplayDeviceState>& displays,
94 const Vector<DisplayState>& changedDisplays, uint32_t flags) override;
Irvelffc9efc2016-07-27 15:16:37 -070095
96 // Intercept surface data
Lloyd Pique4dccc412018-01-22 17:21:36 -080097 void saveSurfaceCreation(const sp<const Layer>& layer) override;
98 void saveSurfaceDeletion(const sp<const Layer>& layer) override;
Irvelc274c632016-06-13 16:44:08 -070099 void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height,
Lloyd Pique4dccc412018-01-22 17:21:36 -0800100 uint64_t frameNumber) override;
Irvelffc9efc2016-07-27 15:16:37 -0700101
102 // Intercept display data
Lloyd Pique4dccc412018-01-22 17:21:36 -0800103 void saveDisplayCreation(const DisplayDeviceState& info) override;
Dominik Laskowski663bd282018-04-19 15:26:54 -0700104 void saveDisplayDeletion(int32_t sequenceId) override;
105 void savePowerModeUpdate(int32_t sequenceId, int32_t mode) override;
Lloyd Pique4dccc412018-01-22 17:21:36 -0800106 void saveVSyncEvent(nsecs_t timestamp) override;
Irvelc274c632016-06-13 16:44:08 -0700107
108private:
Irvelffc9efc2016-07-27 15:16:37 -0700109 // The creation increments of Surfaces and Displays do not contain enough information to capture
110 // the initial state of each object, so a transaction with all of the missing properties is
111 // performed at the initial snapshot for each display and surface.
112 void saveExistingDisplaysLocked(
113 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays);
114 void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers);
115 void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer);
116 void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display);
117
Irvelc274c632016-06-13 16:44:08 -0700118 status_t writeProtoFileLocked();
Vishnu Nair456bbb22019-07-18 16:02:00 -0700119 const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle) const;
120 const std::string getLayerName(const sp<const Layer>& layer) const;
121 int32_t getLayerId(const sp<const Layer>& layer) const;
122 int32_t getLayerIdFromWeakRef(const wp<const Layer>& layer) const;
123 int32_t getLayerIdFromHandle(const sp<const IBinder>& weakHandle) const;
Irvelc274c632016-06-13 16:44:08 -0700124
Irvelffc9efc2016-07-27 15:16:37 -0700125 Increment* createTraceIncrementLocked();
126 void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer);
127 void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer);
128 void addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer, uint32_t width,
Irvelc274c632016-06-13 16:44:08 -0700129 uint32_t height, uint64_t frameNumber);
Irvelffc9efc2016-07-27 15:16:37 -0700130 void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp);
131 void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700132 void addDisplayDeletionLocked(Increment* increment, int32_t sequenceId);
133 void addPowerModeUpdateLocked(Increment* increment, int32_t sequenceId, int32_t mode);
Irvelc274c632016-06-13 16:44:08 -0700134
Irvelffc9efc2016-07-27 15:16:37 -0700135 // Add surface transactions to the trace
136 SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId);
Irvelc274c632016-06-13 16:44:08 -0700137 void setProtoRectLocked(Rectangle* protoRect, const Rect& rect);
138 void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y);
139 void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z);
140 void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h);
141 void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha);
142 void addMatrixLocked(Transaction* transaction, int32_t layerId,
143 const layer_state_t::matrix22_t& matrix);
144 void addTransparentRegionLocked(Transaction* transaction, int32_t layerId,
145 const Region& transRegion);
Vishnu Nair456bbb22019-07-18 16:02:00 -0700146 void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags, uint8_t mask);
Irvelc274c632016-06-13 16:44:08 -0700147 void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack);
148 void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700149 void addCornerRadiusLocked(Transaction* transaction, int32_t layerId, float cornerRadius);
Irvelc274c632016-06-13 16:44:08 -0700150 void addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
Robert Carr0d480722017-01-10 16:42:54 -0800151 const sp<const Layer>& layer, uint64_t frameNumber);
Irvelc274c632016-06-13 16:44:08 -0700152 void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId,
153 int32_t overrideScalingMode);
Irvelffc9efc2016-07-27 15:16:37 -0700154 void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state);
155 void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates,
156 const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
157 const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags);
Vishnu Nair456bbb22019-07-18 16:02:00 -0700158 void addReparentLocked(Transaction* transaction, int32_t layerId, int32_t parentId);
159 void addReparentChildrenLocked(Transaction* transaction, int32_t layerId, int32_t parentId);
160 void addDetachChildrenLocked(Transaction* transaction, int32_t layerId, bool detached);
161 void addRelativeParentLocked(Transaction* transaction, int32_t layerId, int32_t parentId,
162 int z);
Irvelffc9efc2016-07-27 15:16:37 -0700163
164 // Add display transactions to the trace
Dominik Laskowski663bd282018-04-19 15:26:54 -0700165 DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t sequenceId);
166 void addDisplaySurfaceLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700167 const sp<const IGraphicBufferProducer>& surface);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700168 void addDisplayLayerStackLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700169 uint32_t layerStack);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700170 void addDisplaySizeLocked(Transaction* transaction, int32_t sequenceId, uint32_t w,
Irvelffc9efc2016-07-27 15:16:37 -0700171 uint32_t h);
Dominik Laskowski663bd282018-04-19 15:26:54 -0700172 void addDisplayProjectionLocked(Transaction* transaction, int32_t sequenceId,
Irvelffc9efc2016-07-27 15:16:37 -0700173 int32_t orientation, const Rect& viewport, const Rect& frame);
174 void addDisplayChangesLocked(Transaction* transaction,
Dominik Laskowski663bd282018-04-19 15:26:54 -0700175 const DisplayState& state, int32_t sequenceId);
Irvelffc9efc2016-07-27 15:16:37 -0700176
Irvelc274c632016-06-13 16:44:08 -0700177
178 bool mEnabled {false};
179 std::string mOutputFileName {DEFAULT_FILENAME};
180 std::mutex mTraceMutex {};
181 Trace mTrace {};
Robert Carr0d480722017-01-10 16:42:54 -0800182 SurfaceFlinger* const mFlinger;
Irvelc274c632016-06-13 16:44:08 -0700183};
184
Lloyd Pique4dccc412018-01-22 17:21:36 -0800185} // namespace impl
186} // namespace android
Irvelc274c632016-06-13 16:44:08 -0700187
188#endif // ANDROID_SURFACEINTERCEPTOR_H