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_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 
32 #include <stdatomic.h>
33 #include <condition_variable>
34 #include <memory>
35 #include <mutex>
36 #include <queue>
37 #include <thread>
38 #include <unordered_map>
39 #include <utility>
40 
41 using namespace android::surfaceflinger;
42 
43 namespace android {
44 
45 const auto DEFAULT_PATH = "/data/local/tmp/SurfaceTrace.dat";
46 const auto RAND_COLOR_SEED = 700;
47 const auto DEFAULT_THREADS = 3;
48 
49 typedef int32_t layer_id;
50 typedef int32_t display_id;
51 
52 typedef google::protobuf::RepeatedPtrField<SurfaceChange> SurfaceChanges;
53 typedef google::protobuf::RepeatedPtrField<DisplayChange> DisplayChanges;
54 
55 class Replayer {
56   public:
57     Replayer(const std::string& filename, bool replayManually = false,
58             int numThreads = DEFAULT_THREADS, bool wait = true, nsecs_t stopHere = -1);
59     Replayer(const Trace& trace, bool replayManually = false, int numThreads = DEFAULT_THREADS,
60             bool wait = true, nsecs_t stopHere = -1);
61 
62     status_t replay();
63 
64   private:
65     status_t initReplay();
66 
67     void waitForConsoleCommmand();
68     static void stopAutoReplayHandler(int signal);
69 
70     status_t dispatchEvent(int index);
71 
72     status_t doTransaction(const Transaction& transaction, const std::shared_ptr<Event>& event);
73     status_t createSurfaceControl(const SurfaceCreation& create,
74             const std::shared_ptr<Event>& event);
75     status_t injectVSyncEvent(const VSyncEvent& vsyncEvent, const std::shared_ptr<Event>& event);
76     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);
79 
80     status_t doSurfaceTransaction(SurfaceComposerClient::Transaction& transaction,
81             const SurfaceChanges& surfaceChange);
82     void doDisplayTransaction(SurfaceComposerClient::Transaction& transaction,
83             const DisplayChanges& displayChange);
84 
85     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 setCornerRadius(SurfaceComposerClient::Transaction& t,
96             layer_id id, const CornerRadiusChange& cc);
97     void setBackgroundBlurRadius(SurfaceComposerClient::Transaction& t,
98             layer_id id, const BackgroundBlurRadiusChange& cc);
99     void setMatrix(SurfaceComposerClient::Transaction& t,
100             layer_id id, const MatrixChange& mc);
101     void setOverrideScalingMode(SurfaceComposerClient::Transaction& t,
102             layer_id id, const OverrideScalingModeChange& osmc);
103     void setTransparentRegionHint(SurfaceComposerClient::Transaction& t,
104             layer_id id, const TransparentRegionHintChange& trgc);
105     void setLayerStack(SurfaceComposerClient::Transaction& t,
106             layer_id id, const LayerStackChange& lsc);
107     void setHiddenFlag(SurfaceComposerClient::Transaction& t,
108             layer_id id, const HiddenFlagChange& hfc);
109     void setOpaqueFlag(SurfaceComposerClient::Transaction& t,
110             layer_id id, const OpaqueFlagChange& ofc);
111     void setSecureFlag(SurfaceComposerClient::Transaction& t,
112             layer_id id, const SecureFlagChange& sfc);
113     void setDeferredTransaction(SurfaceComposerClient::Transaction& t,
114             layer_id id, const DeferredTransactionChange& dtc);
115     void setReparentChange(SurfaceComposerClient::Transaction& t,
116             layer_id id, const ReparentChange& c);
117     void setRelativeParentChange(SurfaceComposerClient::Transaction& t,
118             layer_id id, const RelativeParentChange& c);
119     void setDetachChildrenChange(SurfaceComposerClient::Transaction& t,
120             layer_id id, const DetachChildrenChange& c);
121     void setReparentChildrenChange(SurfaceComposerClient::Transaction& t,
122             layer_id id, const ReparentChildrenChange& c);
123     void setShadowRadiusChange(SurfaceComposerClient::Transaction& t,
124             layer_id id, const ShadowRadiusChange& c);
125 
126     void setDisplaySurface(SurfaceComposerClient::Transaction& t,
127             display_id id, const DispSurfaceChange& dsc);
128     void setDisplayLayerStack(SurfaceComposerClient::Transaction& t,
129             display_id id, const LayerStackChange& lsc);
130     void setDisplaySize(SurfaceComposerClient::Transaction& t,
131             display_id id, const SizeChange& sc);
132     void setDisplayProjection(SurfaceComposerClient::Transaction& t,
133             display_id id, const ProjectionChange& pc);
134 
135     void waitUntilTimestamp(int64_t timestamp);
136     void waitUntilDeferredTransactionLayerExists(
137             const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock);
138     status_t loadSurfaceComposerClient();
139 
140     Trace mTrace;
141     bool mLoaded = false;
142     int32_t mIncrementIndex = 0;
143     int64_t mCurrentTime = 0;
144     int32_t mNumThreads = DEFAULT_THREADS;
145 
146     Increment mCurrentIncrement;
147 
148     std::string mLastInput;
149 
150     static atomic_bool sReplayingManually;
151     bool mWaitingForNextVSync;
152     bool mWaitForTimeStamps;
153     nsecs_t mStopTimeStamp;
154     bool mHasStopped;
155 
156     std::mutex mLayerLock;
157     std::condition_variable mLayerCond;
158     std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
159     std::unordered_map<layer_id, HSV> mColors;
160 
161     std::mutex mPendingLayersLock;
162     std::vector<layer_id> mLayersPendingRemoval;
163 
164     std::mutex mBufferQueueSchedulerLock;
165     std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;
166 
167     std::mutex mDisplayLock;
168     std::condition_variable mDisplayCond;
169     std::unordered_map<display_id, sp<IBinder>> mDisplays;
170 
171     sp<SurfaceComposerClient> mComposerClient;
172     std::queue<std::shared_ptr<Event>> mPendingIncrements;
173 };
174 
175 }  // namespace android
176 #endif
177