• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace android {
42 
43 const auto DEFAULT_PATH = "/data/local/tmp/SurfaceTrace.dat";
44 const auto RAND_COLOR_SEED = 700;
45 const auto DEFAULT_THREADS = 3;
46 
47 typedef int32_t layer_id;
48 typedef int32_t display_id;
49 
50 typedef google::protobuf::RepeatedPtrField<SurfaceChange> SurfaceChanges;
51 typedef google::protobuf::RepeatedPtrField<DisplayChange> DisplayChanges;
52 
53 class Replayer {
54   public:
55     Replayer(const std::string& filename, bool replayManually = false,
56             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);
59 
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);
71     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);
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(const SurfaceChanges& surfaceChange);
81     void doDisplayTransaction(const DisplayChanges& displayChange);
82 
83     status_t setPosition(layer_id id, const PositionChange& pc);
84     status_t setSize(layer_id id, const SizeChange& sc);
85     status_t setAlpha(layer_id id, const AlphaChange& ac);
86     status_t setLayer(layer_id id, const LayerChange& lc);
87     status_t setCrop(layer_id id, const CropChange& cc);
88     status_t setFinalCrop(layer_id id, const FinalCropChange& fcc);
89     status_t setMatrix(layer_id id, const MatrixChange& mc);
90     status_t setOverrideScalingMode(layer_id id, const OverrideScalingModeChange& osmc);
91     status_t setTransparentRegionHint(layer_id id, const TransparentRegionHintChange& trgc);
92     status_t setLayerStack(layer_id id, const LayerStackChange& lsc);
93     status_t setHiddenFlag(layer_id id, const HiddenFlagChange& hfc);
94     status_t setOpaqueFlag(layer_id id, const OpaqueFlagChange& ofc);
95     status_t setSecureFlag(layer_id id, const SecureFlagChange& sfc);
96     status_t setDeferredTransaction(layer_id id, const DeferredTransactionChange& dtc);
97 
98     void setDisplaySurface(display_id id, const DispSurfaceChange& dsc);
99     void setDisplayLayerStack(display_id id, const LayerStackChange& lsc);
100     void setDisplaySize(display_id id, const SizeChange& sc);
101     void setDisplayProjection(display_id id, const ProjectionChange& pc);
102 
103     void doDeleteSurfaceControls();
104     void waitUntilTimestamp(int64_t timestamp);
105     void waitUntilDeferredTransactionLayerExists(
106             const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock);
107     status_t loadSurfaceComposerClient();
108 
109     Trace mTrace;
110     bool mLoaded = false;
111     int32_t mIncrementIndex = 0;
112     int64_t mCurrentTime = 0;
113     int32_t mNumThreads = DEFAULT_THREADS;
114 
115     Increment mCurrentIncrement;
116 
117     std::string mLastInput;
118 
119     static atomic_bool sReplayingManually;
120     bool mWaitingForNextVSync;
121     bool mWaitForTimeStamps;
122     nsecs_t mStopTimeStamp;
123     bool mHasStopped;
124 
125     std::mutex mLayerLock;
126     std::condition_variable mLayerCond;
127     std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
128     std::unordered_map<layer_id, HSV> mColors;
129 
130     std::mutex mPendingLayersLock;
131     std::vector<layer_id> mLayersPendingRemoval;
132 
133     std::mutex mBufferQueueSchedulerLock;
134     std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;
135 
136     std::mutex mDisplayLock;
137     std::condition_variable mDisplayCond;
138     std::unordered_map<display_id, sp<IBinder>> mDisplays;
139 
140     sp<SurfaceComposerClient> mComposerClient;
141     std::queue<std::shared_ptr<Event>> mPendingIncrements;
142 };
143 
144 }  // namespace android
145 #endif
146