blob: 295403eace76608253458194154064facac965b8 [file] [log] [blame]
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -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_SURFACEREPLAYER_H
18#define ANDROID_SURFACEREPLAYER_H
19
20#include "BufferQueueScheduler.h"
21#include "Color.h"
22#include "Event.h"
23
24#include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h>
25
26#include <gui/SurfaceComposerClient.h>
27#include <gui/SurfaceControl.h>
28
29#include <utils/Errors.h>
30#include <utils/StrongPointer.h>
31
Mathias Agopiane9e9fe42017-02-28 16:25:16 -080032#include <stdatomic.h>
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070033#include <condition_variable>
34#include <memory>
35#include <mutex>
36#include <queue>
37#include <thread>
38#include <unordered_map>
39#include <utility>
40
41namespace android {
42
43const auto DEFAULT_PATH = "/data/local/tmp/SurfaceTrace.dat";
Sahil Dhanju01041fe2016-08-08 20:27:23 -070044const auto RAND_COLOR_SEED = 700;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070045const auto DEFAULT_THREADS = 3;
46
Sahil Dhanju01041fe2016-08-08 20:27:23 -070047typedef int32_t layer_id;
48typedef int32_t display_id;
49
50typedef google::protobuf::RepeatedPtrField<SurfaceChange> SurfaceChanges;
51typedef google::protobuf::RepeatedPtrField<DisplayChange> DisplayChanges;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070052
53class Replayer {
54 public:
55 Replayer(const std::string& filename, bool replayManually = false,
Sahil Dhanju01041fe2016-08-08 20:27:23 -070056 int numThreads = DEFAULT_THREADS, bool wait = true, nsecs_t stopHere = -1);
57 Replayer(const Trace& trace, bool replayManually = false, int numThreads = DEFAULT_THREADS,
58 bool wait = true, nsecs_t stopHere = -1);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070059
60 status_t replay();
61
62 private:
63 status_t initReplay();
64
65 void waitForConsoleCommmand();
66 static void stopAutoReplayHandler(int signal);
67
68 status_t dispatchEvent(int index);
69
70 status_t doTransaction(const Transaction& transaction, const std::shared_ptr<Event>& event);
Sahil Dhanju01041fe2016-08-08 20:27:23 -070071 status_t createSurfaceControl(const SurfaceCreation& create,
72 const std::shared_ptr<Event>& event);
73 status_t deleteSurfaceControl(const SurfaceDeletion& delete_,
74 const std::shared_ptr<Event>& event);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070075 status_t injectVSyncEvent(const VSyncEvent& vsyncEvent, const std::shared_ptr<Event>& event);
Sahil Dhanju01041fe2016-08-08 20:27:23 -070076 void createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event);
77 void deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event);
78 void updatePowerMode(const PowerModeUpdate& update, const std::shared_ptr<Event>& event);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070079
Robert Carr4cdc58f2017-08-23 14:22:20 -070080 status_t doSurfaceTransaction(SurfaceComposerClient::Transaction& transaction,
81 const SurfaceChanges& surfaceChange);
82 void doDisplayTransaction(SurfaceComposerClient::Transaction& transaction,
83 const DisplayChanges& displayChange);
Sahil Dhanju01041fe2016-08-08 20:27:23 -070084
Robert Carr4cdc58f2017-08-23 14:22:20 -070085 void setPosition(SurfaceComposerClient::Transaction& t,
86 layer_id id, const PositionChange& pc);
87 void setSize(SurfaceComposerClient::Transaction& t,
88 layer_id id, const SizeChange& sc);
89 void setAlpha(SurfaceComposerClient::Transaction& t,
90 layer_id id, const AlphaChange& ac);
91 void setLayer(SurfaceComposerClient::Transaction& t,
92 layer_id id, const LayerChange& lc);
93 void setCrop(SurfaceComposerClient::Transaction& t,
94 layer_id id, const CropChange& cc);
95 void setFinalCrop(SurfaceComposerClient::Transaction& t,
96 layer_id id, const FinalCropChange& fcc);
97 void setMatrix(SurfaceComposerClient::Transaction& t,
98 layer_id id, const MatrixChange& mc);
99 void setOverrideScalingMode(SurfaceComposerClient::Transaction& t,
100 layer_id id, const OverrideScalingModeChange& osmc);
101 void setTransparentRegionHint(SurfaceComposerClient::Transaction& t,
102 layer_id id, const TransparentRegionHintChange& trgc);
103 void setLayerStack(SurfaceComposerClient::Transaction& t,
104 layer_id id, const LayerStackChange& lsc);
105 void setHiddenFlag(SurfaceComposerClient::Transaction& t,
106 layer_id id, const HiddenFlagChange& hfc);
107 void setOpaqueFlag(SurfaceComposerClient::Transaction& t,
108 layer_id id, const OpaqueFlagChange& ofc);
109 void setSecureFlag(SurfaceComposerClient::Transaction& t,
110 layer_id id, const SecureFlagChange& sfc);
111 void setDeferredTransaction(SurfaceComposerClient::Transaction& t,
112 layer_id id, const DeferredTransactionChange& dtc);
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700113
Robert Carr4cdc58f2017-08-23 14:22:20 -0700114 void setDisplaySurface(SurfaceComposerClient::Transaction& t,
115 display_id id, const DispSurfaceChange& dsc);
116 void setDisplayLayerStack(SurfaceComposerClient::Transaction& t,
117 display_id id, const LayerStackChange& lsc);
118 void setDisplaySize(SurfaceComposerClient::Transaction& t,
119 display_id id, const SizeChange& sc);
120 void setDisplayProjection(SurfaceComposerClient::Transaction& t,
121 display_id id, const ProjectionChange& pc);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700122
123 void doDeleteSurfaceControls();
124 void waitUntilTimestamp(int64_t timestamp);
125 void waitUntilDeferredTransactionLayerExists(
126 const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock);
127 status_t loadSurfaceComposerClient();
128
129 Trace mTrace;
130 bool mLoaded = false;
131 int32_t mIncrementIndex = 0;
132 int64_t mCurrentTime = 0;
133 int32_t mNumThreads = DEFAULT_THREADS;
134
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700135 Increment mCurrentIncrement;
136
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700137 std::string mLastInput;
138
139 static atomic_bool sReplayingManually;
140 bool mWaitingForNextVSync;
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700141 bool mWaitForTimeStamps;
142 nsecs_t mStopTimeStamp;
143 bool mHasStopped;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700144
145 std::mutex mLayerLock;
146 std::condition_variable mLayerCond;
147 std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700148 std::unordered_map<layer_id, HSV> mColors;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700149
150 std::mutex mPendingLayersLock;
151 std::vector<layer_id> mLayersPendingRemoval;
152
153 std::mutex mBufferQueueSchedulerLock;
154 std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;
155
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700156 std::mutex mDisplayLock;
157 std::condition_variable mDisplayCond;
158 std::unordered_map<display_id, sp<IBinder>> mDisplays;
159
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700160 sp<SurfaceComposerClient> mComposerClient;
161 std::queue<std::shared_ptr<Event>> mPendingIncrements;
162};
163
164} // namespace android
165#endif