summaryrefslogtreecommitdiff
path: root/cmds/surfacereplayer/replayer/Replayer.h
blob: d62522a497b770dbdf3f6e696467e485d5dcd8c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_SURFACEREPLAYER_H
#define ANDROID_SURFACEREPLAYER_H

#include "BufferQueueScheduler.h"
#include "Color.h"
#include "Event.h"

#include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h>

#include <gui/SurfaceComposerClient.h>
#include <gui/SurfaceControl.h>

#include <utils/Errors.h>
#include <utils/StrongPointer.h>

#include <stdatomic.h>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
#include <unordered_map>
#include <utility>

using namespace android::surfaceflinger;

namespace android {

const auto DEFAULT_PATH = "/data/local/tmp/SurfaceTrace.dat";
const auto RAND_COLOR_SEED = 700;
const auto DEFAULT_THREADS = 3;

typedef int32_t layer_id;
typedef int32_t display_id;

typedef google::protobuf::RepeatedPtrField<SurfaceChange> SurfaceChanges;
typedef google::protobuf::RepeatedPtrField<DisplayChange> DisplayChanges;

class Replayer {
  public:
    Replayer(const std::string& filename, bool replayManually = false,
            int numThreads = DEFAULT_THREADS, bool wait = true, nsecs_t stopHere = -1);
    Replayer(const Trace& trace, bool replayManually = false, int numThreads = DEFAULT_THREADS,
            bool wait = true, nsecs_t stopHere = -1);

    status_t replay();

  private:
    status_t initReplay();

    void waitForConsoleCommmand();
    static void stopAutoReplayHandler(int signal);

    status_t dispatchEvent(int index);

    status_t doTransaction(const Transaction& transaction, const std::shared_ptr<Event>& event);
    status_t createSurfaceControl(const SurfaceCreation& create,
            const std::shared_ptr<Event>& event);
    status_t injectVSyncEvent(const VSyncEvent& vsyncEvent, const std::shared_ptr<Event>& event);
    void createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event);
    void deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event);
    void updatePowerMode(const PowerModeUpdate& update, const std::shared_ptr<Event>& event);

    status_t doSurfaceTransaction(SurfaceComposerClient::Transaction& transaction,
            const SurfaceChanges& surfaceChange);
    void doDisplayTransaction(SurfaceComposerClient::Transaction& transaction,
            const DisplayChanges& displayChange);

    void setPosition(SurfaceComposerClient::Transaction& t,
            layer_id id, const PositionChange& pc);
    void setSize(SurfaceComposerClient::Transaction& t,
            layer_id id, const SizeChange& sc);
    void setAlpha(SurfaceComposerClient::Transaction& t,
            layer_id id, const AlphaChange& ac);
    void setLayer(SurfaceComposerClient::Transaction& t,
            layer_id id, const LayerChange& lc);
    void setCrop(SurfaceComposerClient::Transaction& t,
            layer_id id, const CropChange& cc);
    void setCornerRadius(SurfaceComposerClient::Transaction& t,
            layer_id id, const CornerRadiusChange& cc);
    void setBackgroundBlurRadius(SurfaceComposerClient::Transaction& t,
            layer_id id, const BackgroundBlurRadiusChange& cc);
    void setBlurRegions(SurfaceComposerClient::Transaction& t,
            layer_id id, const BlurRegionsChange& cc);
    void setMatrix(SurfaceComposerClient::Transaction& t,
            layer_id id, const MatrixChange& mc);
    void setTransparentRegionHint(SurfaceComposerClient::Transaction& t,
            layer_id id, const TransparentRegionHintChange& trgc);
    void setLayerStack(SurfaceComposerClient::Transaction& t,
            layer_id id, const LayerStackChange& lsc);
    void setHiddenFlag(SurfaceComposerClient::Transaction& t,
            layer_id id, const HiddenFlagChange& hfc);
    void setOpaqueFlag(SurfaceComposerClient::Transaction& t,
            layer_id id, const OpaqueFlagChange& ofc);
    void setSecureFlag(SurfaceComposerClient::Transaction& t,
            layer_id id, const SecureFlagChange& sfc);
    void setReparentChange(SurfaceComposerClient::Transaction& t,
            layer_id id, const ReparentChange& c);
    void setRelativeParentChange(SurfaceComposerClient::Transaction& t,
            layer_id id, const RelativeParentChange& c);
    void setShadowRadiusChange(SurfaceComposerClient::Transaction& t,
            layer_id id, const ShadowRadiusChange& c);
    void setBlurRegionsChange(SurfaceComposerClient::Transaction& t,
            layer_id id, const BlurRegionsChange& c);

    void setDisplaySurface(SurfaceComposerClient::Transaction& t,
            display_id id, const DispSurfaceChange& dsc);
    void setDisplayLayerStack(SurfaceComposerClient::Transaction& t,
            display_id id, const LayerStackChange& lsc);
    void setDisplaySize(SurfaceComposerClient::Transaction& t,
            display_id id, const SizeChange& sc);
    void setDisplayProjection(SurfaceComposerClient::Transaction& t,
            display_id id, const ProjectionChange& pc);

    void waitUntilTimestamp(int64_t timestamp);
    status_t loadSurfaceComposerClient();

    Trace mTrace;
    bool mLoaded = false;
    int32_t mIncrementIndex = 0;
    int64_t mCurrentTime = 0;
    int32_t mNumThreads = DEFAULT_THREADS;

    Increment mCurrentIncrement;

    std::string mLastInput;

    static atomic_bool sReplayingManually;
    bool mWaitingForNextVSync;
    bool mWaitForTimeStamps;
    nsecs_t mStopTimeStamp;
    bool mHasStopped;

    std::mutex mLayerLock;
    std::condition_variable mLayerCond;
    std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
    std::unordered_map<layer_id, HSV> mColors;

    std::mutex mPendingLayersLock;
    std::vector<layer_id> mLayersPendingRemoval;

    std::mutex mBufferQueueSchedulerLock;
    std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;

    std::mutex mDisplayLock;
    std::condition_variable mDisplayCond;
    std::unordered_map<display_id, sp<IBinder>> mDisplays;

    sp<SurfaceComposerClient> mComposerClient;
    std::queue<std::shared_ptr<Event>> mPendingIncrements;
};

}  // namespace android
#endif