blob: ff951d11c634a815b728dc75dcb8cecab4fc62d6 [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
32#include <condition_variable>
33#include <memory>
34#include <mutex>
35#include <queue>
36#include <thread>
37#include <unordered_map>
38#include <utility>
39
40namespace android {
41
42const auto DEFAULT_PATH = "/data/local/tmp/SurfaceTrace.dat";
43const auto RAND_COLOR_SEED = 1000;
44const auto DEFAULT_THREADS = 3;
45
46typedef uint32_t layer_id;
47
48class Replayer {
49 public:
50 Replayer(const std::string& filename, bool replayManually = false,
51 int numThreads = DEFAULT_THREADS);
52 Replayer(const Trace& trace, bool replayManually = false, int numThreads = DEFAULT_THREADS);
53
54 status_t replay();
55
56 private:
57 status_t initReplay();
58
59 void waitForConsoleCommmand();
60 static void stopAutoReplayHandler(int signal);
61
62 status_t dispatchEvent(int index);
63
64 status_t doTransaction(const Transaction& transaction, const std::shared_ptr<Event>& event);
65 status_t createSurfaceControl(const Create& create, const std::shared_ptr<Event>& event);
66 status_t deleteSurfaceControl(const Delete& delete_, const std::shared_ptr<Event>& event);
67 status_t injectVSyncEvent(const VSyncEvent& vsyncEvent, const std::shared_ptr<Event>& event);
68
69 status_t setPosition(uint32_t id, const PositionChange& pc);
70 status_t setSize(uint32_t id, const SizeChange& sc);
71 status_t setAlpha(uint32_t id, const AlphaChange& ac);
72 status_t setLayer(uint32_t id, const LayerChange& lc);
73 status_t setCrop(uint32_t id, const CropChange& cc);
74 status_t setFinalCrop(uint32_t id, const FinalCropChange& fcc);
75 status_t setMatrix(uint32_t id, const MatrixChange& mc);
76 status_t setOverrideScalingMode(uint32_t id, const OverrideScalingModeChange& osmc);
77 status_t setTransparentRegionHint(uint32_t id, const TransparentRegionHintChange& trgc);
78 status_t setLayerStack(uint32_t id, const LayerStackChange& lsc);
79 status_t setHiddenFlag(uint32_t id, const HiddenFlagChange& hfc);
80 status_t setOpaqueFlag(uint32_t id, const OpaqueFlagChange& ofc);
81 status_t setSecureFlag(uint32_t id, const SecureFlagChange& sfc);
82 status_t setDeferredTransaction(uint32_t id, const DeferredTransactionChange& dtc);
83
84 void doDeleteSurfaceControls();
85 void waitUntilTimestamp(int64_t timestamp);
86 void waitUntilDeferredTransactionLayerExists(
87 const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock);
88 status_t loadSurfaceComposerClient();
89
90 Trace mTrace;
91 bool mLoaded = false;
92 int32_t mIncrementIndex = 0;
93 int64_t mCurrentTime = 0;
94 int32_t mNumThreads = DEFAULT_THREADS;
95
96 std::string mLastInput;
97
98 static atomic_bool sReplayingManually;
99 bool mWaitingForNextVSync;
100
101 std::mutex mLayerLock;
102 std::condition_variable mLayerCond;
103 std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
104 std::unordered_map<layer_id, RGB> mColors;
105
106 std::mutex mPendingLayersLock;
107 std::vector<layer_id> mLayersPendingRemoval;
108
109 std::mutex mBufferQueueSchedulerLock;
110 std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;
111
112 sp<SurfaceComposerClient> mComposerClient;
113 std::queue<std::shared_ptr<Event>> mPendingIncrements;
114};
115
116} // namespace android
117#endif