blob: f757fc3c1512212fb0f032985af81f61a2ac1cc1 [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";
Sahil Dhanju01041fe2016-08-08 20:27:23 -070043const auto RAND_COLOR_SEED = 700;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070044const auto DEFAULT_THREADS = 3;
45
Sahil Dhanju01041fe2016-08-08 20:27:23 -070046typedef int32_t layer_id;
47typedef int32_t display_id;
48
49typedef google::protobuf::RepeatedPtrField<SurfaceChange> SurfaceChanges;
50typedef google::protobuf::RepeatedPtrField<DisplayChange> DisplayChanges;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070051
52class Replayer {
53 public:
54 Replayer(const std::string& filename, bool replayManually = false,
Sahil Dhanju01041fe2016-08-08 20:27:23 -070055 int numThreads = DEFAULT_THREADS, bool wait = true, nsecs_t stopHere = -1);
56 Replayer(const Trace& trace, bool replayManually = false, int numThreads = DEFAULT_THREADS,
57 bool wait = true, nsecs_t stopHere = -1);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070058
59 status_t replay();
60
61 private:
62 status_t initReplay();
63
64 void waitForConsoleCommmand();
65 static void stopAutoReplayHandler(int signal);
66
67 status_t dispatchEvent(int index);
68
69 status_t doTransaction(const Transaction& transaction, const std::shared_ptr<Event>& event);
Sahil Dhanju01041fe2016-08-08 20:27:23 -070070 status_t createSurfaceControl(const SurfaceCreation& create,
71 const std::shared_ptr<Event>& event);
72 status_t deleteSurfaceControl(const SurfaceDeletion& delete_,
73 const std::shared_ptr<Event>& event);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070074 status_t injectVSyncEvent(const VSyncEvent& vsyncEvent, const std::shared_ptr<Event>& event);
Sahil Dhanju01041fe2016-08-08 20:27:23 -070075 void createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event);
76 void deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event);
77 void updatePowerMode(const PowerModeUpdate& update, const std::shared_ptr<Event>& event);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -070078
Sahil Dhanju01041fe2016-08-08 20:27:23 -070079 status_t doSurfaceTransaction(const SurfaceChanges& surfaceChange);
80 void doDisplayTransaction(const DisplayChanges& displayChange);
81
82 status_t setPosition(layer_id id, const PositionChange& pc);
83 status_t setSize(layer_id id, const SizeChange& sc);
84 status_t setAlpha(layer_id id, const AlphaChange& ac);
85 status_t setLayer(layer_id id, const LayerChange& lc);
86 status_t setCrop(layer_id id, const CropChange& cc);
87 status_t setFinalCrop(layer_id id, const FinalCropChange& fcc);
88 status_t setMatrix(layer_id id, const MatrixChange& mc);
89 status_t setOverrideScalingMode(layer_id id, const OverrideScalingModeChange& osmc);
90 status_t setTransparentRegionHint(layer_id id, const TransparentRegionHintChange& trgc);
91 status_t setLayerStack(layer_id id, const LayerStackChange& lsc);
92 status_t setHiddenFlag(layer_id id, const HiddenFlagChange& hfc);
93 status_t setOpaqueFlag(layer_id id, const OpaqueFlagChange& ofc);
94 status_t setSecureFlag(layer_id id, const SecureFlagChange& sfc);
95 status_t setDeferredTransaction(layer_id id, const DeferredTransactionChange& dtc);
96
97 void setDisplaySurface(display_id id, const DispSurfaceChange& dsc);
98 void setDisplayLayerStack(display_id id, const LayerStackChange& lsc);
99 void setDisplaySize(display_id id, const SizeChange& sc);
100 void setDisplayProjection(display_id id, const ProjectionChange& pc);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700101
102 void doDeleteSurfaceControls();
103 void waitUntilTimestamp(int64_t timestamp);
104 void waitUntilDeferredTransactionLayerExists(
105 const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock);
106 status_t loadSurfaceComposerClient();
107
108 Trace mTrace;
109 bool mLoaded = false;
110 int32_t mIncrementIndex = 0;
111 int64_t mCurrentTime = 0;
112 int32_t mNumThreads = DEFAULT_THREADS;
113
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700114 Increment mCurrentIncrement;
115
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700116 std::string mLastInput;
117
118 static atomic_bool sReplayingManually;
119 bool mWaitingForNextVSync;
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700120 bool mWaitForTimeStamps;
121 nsecs_t mStopTimeStamp;
122 bool mHasStopped;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700123
124 std::mutex mLayerLock;
125 std::condition_variable mLayerCond;
126 std::unordered_map<layer_id, sp<SurfaceControl>> mLayers;
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700127 std::unordered_map<layer_id, HSV> mColors;
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700128
129 std::mutex mPendingLayersLock;
130 std::vector<layer_id> mLayersPendingRemoval;
131
132 std::mutex mBufferQueueSchedulerLock;
133 std::unordered_map<layer_id, std::shared_ptr<BufferQueueScheduler>> mBufferQueueSchedulers;
134
Sahil Dhanju01041fe2016-08-08 20:27:23 -0700135 std::mutex mDisplayLock;
136 std::condition_variable mDisplayCond;
137 std::unordered_map<display_id, sp<IBinder>> mDisplays;
138
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700139 sp<SurfaceComposerClient> mComposerClient;
140 std::queue<std::shared_ptr<Event>> mPendingIncrements;
141};
142
143} // namespace android
144#endif