1 /* Copyright 2016 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //#define LOG_NDEBUG 0
17 #define LOG_TAG "SurfaceReplayer"
18
19 #include "Replayer.h"
20
21 #include <android/native_window.h>
22
23 #include <android-base/file.h>
24
25 #include <gui/BufferQueue.h>
26 #include <gui/ISurfaceComposer.h>
27 #include <gui/LayerState.h>
28 #include <gui/Surface.h>
29 #include <private/gui/ComposerService.h>
30
31 #include <ui/DisplayInfo.h>
32 #include <utils/Log.h>
33 #include <utils/String8.h>
34 #include <utils/Trace.h>
35
36 #include <chrono>
37 #include <cmath>
38 #include <condition_variable>
39 #include <cstdlib>
40 #include <fstream>
41 #include <functional>
42 #include <iostream>
43 #include <mutex>
44 #include <sstream>
45 #include <string>
46 #include <thread>
47 #include <vector>
48
49 using namespace android;
50
51 std::atomic_bool Replayer::sReplayingManually(false);
52
Replayer(const std::string & filename,bool replayManually,int numThreads,bool wait,nsecs_t stopHere)53 Replayer::Replayer(const std::string& filename, bool replayManually, int numThreads, bool wait,
54 nsecs_t stopHere)
55 : mTrace(),
56 mLoaded(false),
57 mIncrementIndex(0),
58 mCurrentTime(0),
59 mNumThreads(numThreads),
60 mWaitForTimeStamps(wait),
61 mStopTimeStamp(stopHere) {
62 srand(RAND_COLOR_SEED);
63
64 std::string input;
65 if (!android::base::ReadFileToString(filename, &input, true)) {
66 std::cerr << "Trace did not load. Does " << filename << " exist?" << std::endl;
67 abort();
68 }
69
70 mLoaded = mTrace.ParseFromString(input);
71 if (!mLoaded) {
72 std::cerr << "Trace did not load." << std::endl;
73 abort();
74 }
75
76 mCurrentTime = mTrace.increment(0).time_stamp();
77
78 sReplayingManually.store(replayManually);
79
80 if (stopHere < 0) {
81 mHasStopped = true;
82 }
83 }
84
Replayer(const Trace & t,bool replayManually,int numThreads,bool wait,nsecs_t stopHere)85 Replayer::Replayer(const Trace& t, bool replayManually, int numThreads, bool wait, nsecs_t stopHere)
86 : mTrace(t),
87 mLoaded(true),
88 mIncrementIndex(0),
89 mCurrentTime(0),
90 mNumThreads(numThreads),
91 mWaitForTimeStamps(wait),
92 mStopTimeStamp(stopHere) {
93 srand(RAND_COLOR_SEED);
94 mCurrentTime = mTrace.increment(0).time_stamp();
95
96 sReplayingManually.store(replayManually);
97
98 if (stopHere < 0) {
99 mHasStopped = true;
100 }
101 }
102
replay()103 status_t Replayer::replay() {
104 signal(SIGINT, Replayer::stopAutoReplayHandler); //for manual control
105
106 ALOGV("There are %d increments.", mTrace.increment_size());
107
108 status_t status = loadSurfaceComposerClient();
109
110 if (status != NO_ERROR) {
111 ALOGE("Couldn't create SurfaceComposerClient (%d)", status);
112 return status;
113 }
114
115 SurfaceComposerClient::enableVSyncInjections(true);
116
117 initReplay();
118
119 ALOGV("Starting actual Replay!");
120 while (!mPendingIncrements.empty()) {
121 mCurrentIncrement = mTrace.increment(mIncrementIndex);
122
123 if (mHasStopped == false && mCurrentIncrement.time_stamp() >= mStopTimeStamp) {
124 mHasStopped = true;
125 sReplayingManually.store(true);
126 }
127
128 waitForConsoleCommmand();
129
130 if (mWaitForTimeStamps) {
131 waitUntilTimestamp(mCurrentIncrement.time_stamp());
132 }
133
134 auto event = mPendingIncrements.front();
135 mPendingIncrements.pop();
136
137 event->complete();
138
139 if (event->getIncrementType() == Increment::kVsyncEvent) {
140 mWaitingForNextVSync = false;
141 }
142
143 if (mIncrementIndex + mNumThreads < mTrace.increment_size()) {
144 status = dispatchEvent(mIncrementIndex + mNumThreads);
145
146 if (status != NO_ERROR) {
147 SurfaceComposerClient::enableVSyncInjections(false);
148 return status;
149 }
150 }
151
152 mIncrementIndex++;
153 mCurrentTime = mCurrentIncrement.time_stamp();
154 }
155
156 SurfaceComposerClient::enableVSyncInjections(false);
157
158 return status;
159 }
160
initReplay()161 status_t Replayer::initReplay() {
162 for (int i = 0; i < mNumThreads && i < mTrace.increment_size(); i++) {
163 status_t status = dispatchEvent(i);
164
165 if (status != NO_ERROR) {
166 ALOGE("Unable to dispatch event (%d)", status);
167 return status;
168 }
169 }
170
171 return NO_ERROR;
172 }
173
stopAutoReplayHandler(int)174 void Replayer::stopAutoReplayHandler(int /*signal*/) {
175 if (sReplayingManually) {
176 SurfaceComposerClient::enableVSyncInjections(false);
177 exit(0);
178 }
179
180 sReplayingManually.store(true);
181 }
182
split(const std::string & s,const char delim)183 std::vector<std::string> split(const std::string& s, const char delim) {
184 std::vector<std::string> elems;
185 std::stringstream ss(s);
186 std::string item;
187 while (getline(ss, item, delim)) {
188 elems.push_back(item);
189 }
190 return elems;
191 }
192
isNumber(const std::string & s)193 bool isNumber(const std::string& s) {
194 return !s.empty() &&
195 std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
196 }
197
waitForConsoleCommmand()198 void Replayer::waitForConsoleCommmand() {
199 if (!sReplayingManually || mWaitingForNextVSync) {
200 return;
201 }
202
203 while (true) {
204 std::string input = "";
205 std::cout << "> ";
206 getline(std::cin, input);
207
208 if (input.empty()) {
209 input = mLastInput;
210 } else {
211 mLastInput = input;
212 }
213
214 if (mLastInput.empty()) {
215 continue;
216 }
217
218 std::vector<std::string> inputs = split(input, ' ');
219
220 if (inputs[0] == "n") { // next vsync
221 mWaitingForNextVSync = true;
222 break;
223
224 } else if (inputs[0] == "ni") { // next increment
225 break;
226
227 } else if (inputs[0] == "c") { // continue
228 if (inputs.size() > 1 && isNumber(inputs[1])) {
229 long milliseconds = stoi(inputs[1]);
230 std::thread([&] {
231 std::cout << "Started!" << std::endl;
232 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
233 sReplayingManually.store(true);
234 std::cout << "Should have stopped!" << std::endl;
235 }).detach();
236 }
237 sReplayingManually.store(false);
238 mWaitingForNextVSync = false;
239 break;
240
241 } else if (inputs[0] == "s") { // stop at this timestamp
242 if (inputs.size() < 1) {
243 std::cout << "No time stamp given" << std::endl;
244 continue;
245 }
246 sReplayingManually.store(false);
247 mStopTimeStamp = stol(inputs[1]);
248 mHasStopped = false;
249 break;
250 } else if (inputs[0] == "l") { // list
251 std::cout << "Time stamp: " << mCurrentIncrement.time_stamp() << "\n";
252 continue;
253 } else if (inputs[0] == "q") { // quit
254 SurfaceComposerClient::enableVSyncInjections(false);
255 exit(0);
256
257 } else if (inputs[0] == "h") { // help
258 // add help menu
259 std::cout << "Manual Replay options:\n";
260 std::cout << " n - Go to next VSync\n";
261 std::cout << " ni - Go to next increment\n";
262 std::cout << " c - Continue\n";
263 std::cout << " c [milliseconds] - Continue until specified number of milliseconds\n";
264 std::cout << " s [timestamp] - Continue and stop at specified timestamp\n";
265 std::cout << " l - List out timestamp of current increment\n";
266 std::cout << " h - Display help menu\n";
267 std::cout << std::endl;
268 continue;
269 }
270
271 std::cout << "Invalid Command" << std::endl;
272 }
273 }
274
dispatchEvent(int index)275 status_t Replayer::dispatchEvent(int index) {
276 auto increment = mTrace.increment(index);
277 std::shared_ptr<Event> event = std::make_shared<Event>(increment.increment_case());
278 mPendingIncrements.push(event);
279
280 status_t status = NO_ERROR;
281 switch (increment.increment_case()) {
282 case increment.kTransaction: {
283 std::thread(&Replayer::doTransaction, this, increment.transaction(), event).detach();
284 } break;
285 case increment.kSurfaceCreation: {
286 std::thread(&Replayer::createSurfaceControl, this, increment.surface_creation(), event)
287 .detach();
288 } break;
289 case increment.kBufferUpdate: {
290 std::lock_guard<std::mutex> lock1(mLayerLock);
291 std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
292
293 Dimensions dimensions(increment.buffer_update().w(), increment.buffer_update().h());
294 BufferEvent bufferEvent(event, dimensions);
295
296 auto layerId = increment.buffer_update().id();
297 if (mBufferQueueSchedulers.count(layerId) == 0) {
298 mBufferQueueSchedulers[layerId] = std::make_shared<BufferQueueScheduler>(
299 mLayers[layerId], mColors[layerId], layerId);
300 mBufferQueueSchedulers[layerId]->addEvent(bufferEvent);
301
302 std::thread(&BufferQueueScheduler::startScheduling,
303 mBufferQueueSchedulers[increment.buffer_update().id()].get())
304 .detach();
305 } else {
306 auto bqs = mBufferQueueSchedulers[increment.buffer_update().id()];
307 bqs->addEvent(bufferEvent);
308 }
309 } break;
310 case increment.kVsyncEvent: {
311 std::thread(&Replayer::injectVSyncEvent, this, increment.vsync_event(), event).detach();
312 } break;
313 case increment.kDisplayCreation: {
314 std::thread(&Replayer::createDisplay, this, increment.display_creation(), event)
315 .detach();
316 } break;
317 case increment.kDisplayDeletion: {
318 std::thread(&Replayer::deleteDisplay, this, increment.display_deletion(), event)
319 .detach();
320 } break;
321 case increment.kPowerModeUpdate: {
322 std::thread(&Replayer::updatePowerMode, this, increment.power_mode_update(), event)
323 .detach();
324 } break;
325 default:
326 ALOGE("Unknown Increment Type: %d", increment.increment_case());
327 status = BAD_VALUE;
328 break;
329 }
330
331 return status;
332 }
333
doTransaction(const Transaction & t,const std::shared_ptr<Event> & event)334 status_t Replayer::doTransaction(const Transaction& t, const std::shared_ptr<Event>& event) {
335 ALOGV("Started Transaction");
336
337 SurfaceComposerClient::Transaction liveTransaction;
338
339 status_t status = NO_ERROR;
340
341 status = doSurfaceTransaction(liveTransaction, t.surface_change());
342 doDisplayTransaction(liveTransaction, t.display_change());
343
344 if (t.animation()) {
345 liveTransaction.setAnimationTransaction();
346 }
347
348 event->readyToExecute();
349
350 liveTransaction.apply(t.synchronous());
351
352 ALOGV("Ended Transaction");
353
354 return status;
355 }
356
doSurfaceTransaction(SurfaceComposerClient::Transaction & transaction,const SurfaceChanges & surfaceChanges)357 status_t Replayer::doSurfaceTransaction(
358 SurfaceComposerClient::Transaction& transaction,
359 const SurfaceChanges& surfaceChanges) {
360 status_t status = NO_ERROR;
361
362 for (const SurfaceChange& change : surfaceChanges) {
363 std::unique_lock<std::mutex> lock(mLayerLock);
364 if (mLayers[change.id()] == nullptr) {
365 mLayerCond.wait(lock, [&] { return (mLayers[change.id()] != nullptr); });
366 }
367
368 switch (change.SurfaceChange_case()) {
369 case SurfaceChange::SurfaceChangeCase::kPosition:
370 setPosition(transaction, change.id(), change.position());
371 break;
372 case SurfaceChange::SurfaceChangeCase::kSize:
373 setSize(transaction, change.id(), change.size());
374 break;
375 case SurfaceChange::SurfaceChangeCase::kAlpha:
376 setAlpha(transaction, change.id(), change.alpha());
377 break;
378 case SurfaceChange::SurfaceChangeCase::kLayer:
379 setLayer(transaction, change.id(), change.layer());
380 break;
381 case SurfaceChange::SurfaceChangeCase::kCrop:
382 setCrop(transaction, change.id(), change.crop());
383 break;
384 case SurfaceChange::SurfaceChangeCase::kCornerRadius:
385 setCornerRadius(transaction, change.id(), change.corner_radius());
386 break;
387 case SurfaceChange::SurfaceChangeCase::kMatrix:
388 setMatrix(transaction, change.id(), change.matrix());
389 break;
390 case SurfaceChange::SurfaceChangeCase::kOverrideScalingMode:
391 setOverrideScalingMode(transaction, change.id(),
392 change.override_scaling_mode());
393 break;
394 case SurfaceChange::SurfaceChangeCase::kTransparentRegionHint:
395 setTransparentRegionHint(transaction, change.id(),
396 change.transparent_region_hint());
397 break;
398 case SurfaceChange::SurfaceChangeCase::kLayerStack:
399 setLayerStack(transaction, change.id(), change.layer_stack());
400 break;
401 case SurfaceChange::SurfaceChangeCase::kHiddenFlag:
402 setHiddenFlag(transaction, change.id(), change.hidden_flag());
403 break;
404 case SurfaceChange::SurfaceChangeCase::kOpaqueFlag:
405 setOpaqueFlag(transaction, change.id(), change.opaque_flag());
406 break;
407 case SurfaceChange::SurfaceChangeCase::kSecureFlag:
408 setSecureFlag(transaction, change.id(), change.secure_flag());
409 break;
410 case SurfaceChange::SurfaceChangeCase::kDeferredTransaction:
411 waitUntilDeferredTransactionLayerExists(change.deferred_transaction(), lock);
412 setDeferredTransaction(transaction, change.id(),
413 change.deferred_transaction());
414 break;
415 case SurfaceChange::SurfaceChangeCase::kReparent:
416 setReparentChange(transaction, change.id(), change.reparent());
417 break;
418 case SurfaceChange::SurfaceChangeCase::kReparentChildren:
419 setReparentChildrenChange(transaction, change.id(), change.reparent_children());
420 break;
421 case SurfaceChange::SurfaceChangeCase::kRelativeParent:
422 setRelativeParentChange(transaction, change.id(), change.relative_parent());
423 break;
424 case SurfaceChange::SurfaceChangeCase::kDetachChildren:
425 setDetachChildrenChange(transaction, change.id(), change.detach_children());
426 break;
427 case SurfaceChange::SurfaceChangeCase::kShadowRadius:
428 setShadowRadiusChange(transaction, change.id(), change.shadow_radius());
429 break;
430 default:
431 status = 1;
432 break;
433 }
434
435 if (status != NO_ERROR) {
436 ALOGE("Unknown Transaction Code");
437 return status;
438 }
439 }
440 return status;
441 }
442
doDisplayTransaction(SurfaceComposerClient::Transaction & t,const DisplayChanges & displayChanges)443 void Replayer::doDisplayTransaction(SurfaceComposerClient::Transaction& t,
444 const DisplayChanges& displayChanges) {
445 for (const DisplayChange& change : displayChanges) {
446 ALOGV("Doing display transaction");
447 std::unique_lock<std::mutex> lock(mDisplayLock);
448 if (mDisplays[change.id()] == nullptr) {
449 mDisplayCond.wait(lock, [&] { return (mDisplays[change.id()] != nullptr); });
450 }
451
452 switch (change.DisplayChange_case()) {
453 case DisplayChange::DisplayChangeCase::kSurface:
454 setDisplaySurface(t, change.id(), change.surface());
455 break;
456 case DisplayChange::DisplayChangeCase::kLayerStack:
457 setDisplayLayerStack(t, change.id(), change.layer_stack());
458 break;
459 case DisplayChange::DisplayChangeCase::kSize:
460 setDisplaySize(t, change.id(), change.size());
461 break;
462 case DisplayChange::DisplayChangeCase::kProjection:
463 setDisplayProjection(t, change.id(), change.projection());
464 break;
465 default:
466 break;
467 }
468 }
469 }
470
setPosition(SurfaceComposerClient::Transaction & t,layer_id id,const PositionChange & pc)471 void Replayer::setPosition(SurfaceComposerClient::Transaction& t,
472 layer_id id, const PositionChange& pc) {
473 ALOGV("Layer %d: Setting Position -- x=%f, y=%f", id, pc.x(), pc.y());
474 t.setPosition(mLayers[id], pc.x(), pc.y());
475 }
476
setSize(SurfaceComposerClient::Transaction & t,layer_id id,const SizeChange & sc)477 void Replayer::setSize(SurfaceComposerClient::Transaction& t,
478 layer_id id, const SizeChange& sc) {
479 ALOGV("Layer %d: Setting Size -- w=%u, h=%u", id, sc.w(), sc.h());
480 t.setSize(mLayers[id], sc.w(), sc.h());
481 }
482
setLayer(SurfaceComposerClient::Transaction & t,layer_id id,const LayerChange & lc)483 void Replayer::setLayer(SurfaceComposerClient::Transaction& t,
484 layer_id id, const LayerChange& lc) {
485 ALOGV("Layer %d: Setting Layer -- layer=%d", id, lc.layer());
486 t.setLayer(mLayers[id], lc.layer());
487 }
488
setAlpha(SurfaceComposerClient::Transaction & t,layer_id id,const AlphaChange & ac)489 void Replayer::setAlpha(SurfaceComposerClient::Transaction& t,
490 layer_id id, const AlphaChange& ac) {
491 ALOGV("Layer %d: Setting Alpha -- alpha=%f", id, ac.alpha());
492 t.setAlpha(mLayers[id], ac.alpha());
493 }
494
setCrop(SurfaceComposerClient::Transaction & t,layer_id id,const CropChange & cc)495 void Replayer::setCrop(SurfaceComposerClient::Transaction& t,
496 layer_id id, const CropChange& cc) {
497 ALOGV("Layer %d: Setting Crop -- left=%d, top=%d, right=%d, bottom=%d", id,
498 cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
499 cc.rectangle().bottom());
500
501 Rect r = Rect(cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
502 cc.rectangle().bottom());
503 t.setCrop_legacy(mLayers[id], r);
504 }
505
setCornerRadius(SurfaceComposerClient::Transaction & t,layer_id id,const CornerRadiusChange & cc)506 void Replayer::setCornerRadius(SurfaceComposerClient::Transaction& t,
507 layer_id id, const CornerRadiusChange& cc) {
508 ALOGV("Layer %d: Setting Corner Radius -- cornerRadius=%d", id, cc.corner_radius());
509
510 t.setCornerRadius(mLayers[id], cc.corner_radius());
511 }
512
setBackgroundBlurRadius(SurfaceComposerClient::Transaction & t,layer_id id,const BackgroundBlurRadiusChange & cc)513 void Replayer::setBackgroundBlurRadius(SurfaceComposerClient::Transaction& t,
514 layer_id id, const BackgroundBlurRadiusChange& cc) {
515 ALOGV("Layer %d: Setting Background Blur Radius -- backgroundBlurRadius=%d", id,
516 cc.background_blur_radius());
517
518 t.setBackgroundBlurRadius(mLayers[id], cc.background_blur_radius());
519 }
520
setMatrix(SurfaceComposerClient::Transaction & t,layer_id id,const MatrixChange & mc)521 void Replayer::setMatrix(SurfaceComposerClient::Transaction& t,
522 layer_id id, const MatrixChange& mc) {
523 ALOGV("Layer %d: Setting Matrix -- dsdx=%f, dtdx=%f, dsdy=%f, dtdy=%f", id, mc.dsdx(),
524 mc.dtdx(), mc.dsdy(), mc.dtdy());
525 t.setMatrix(mLayers[id], mc.dsdx(), mc.dtdx(), mc.dsdy(), mc.dtdy());
526 }
527
setOverrideScalingMode(SurfaceComposerClient::Transaction & t,layer_id id,const OverrideScalingModeChange & osmc)528 void Replayer::setOverrideScalingMode(SurfaceComposerClient::Transaction& t,
529 layer_id id, const OverrideScalingModeChange& osmc) {
530 ALOGV("Layer %d: Setting Override Scaling Mode -- mode=%d", id, osmc.override_scaling_mode());
531 t.setOverrideScalingMode(mLayers[id], osmc.override_scaling_mode());
532 }
533
setTransparentRegionHint(SurfaceComposerClient::Transaction & t,layer_id id,const TransparentRegionHintChange & trhc)534 void Replayer::setTransparentRegionHint(SurfaceComposerClient::Transaction& t,
535 layer_id id, const TransparentRegionHintChange& trhc) {
536 ALOGV("Setting Transparent Region Hint");
537 Region re = Region();
538
539 for (const auto& r : trhc.region()) {
540 Rect rect = Rect(r.left(), r.top(), r.right(), r.bottom());
541 re.merge(rect);
542 }
543
544 t.setTransparentRegionHint(mLayers[id], re);
545 }
546
setLayerStack(SurfaceComposerClient::Transaction & t,layer_id id,const LayerStackChange & lsc)547 void Replayer::setLayerStack(SurfaceComposerClient::Transaction& t,
548 layer_id id, const LayerStackChange& lsc) {
549 ALOGV("Layer %d: Setting LayerStack -- layer_stack=%d", id, lsc.layer_stack());
550 t.setLayerStack(mLayers[id], lsc.layer_stack());
551 }
552
setHiddenFlag(SurfaceComposerClient::Transaction & t,layer_id id,const HiddenFlagChange & hfc)553 void Replayer::setHiddenFlag(SurfaceComposerClient::Transaction& t,
554 layer_id id, const HiddenFlagChange& hfc) {
555 ALOGV("Layer %d: Setting Hidden Flag -- hidden_flag=%d", id, hfc.hidden_flag());
556 layer_id flag = hfc.hidden_flag() ? layer_state_t::eLayerHidden : 0;
557
558 t.setFlags(mLayers[id], flag, layer_state_t::eLayerHidden);
559 }
560
setOpaqueFlag(SurfaceComposerClient::Transaction & t,layer_id id,const OpaqueFlagChange & ofc)561 void Replayer::setOpaqueFlag(SurfaceComposerClient::Transaction& t,
562 layer_id id, const OpaqueFlagChange& ofc) {
563 ALOGV("Layer %d: Setting Opaque Flag -- opaque_flag=%d", id, ofc.opaque_flag());
564 layer_id flag = ofc.opaque_flag() ? layer_state_t::eLayerOpaque : 0;
565
566 t.setFlags(mLayers[id], flag, layer_state_t::eLayerOpaque);
567 }
568
setSecureFlag(SurfaceComposerClient::Transaction & t,layer_id id,const SecureFlagChange & sfc)569 void Replayer::setSecureFlag(SurfaceComposerClient::Transaction& t,
570 layer_id id, const SecureFlagChange& sfc) {
571 ALOGV("Layer %d: Setting Secure Flag -- secure_flag=%d", id, sfc.secure_flag());
572 layer_id flag = sfc.secure_flag() ? layer_state_t::eLayerSecure : 0;
573
574 t.setFlags(mLayers[id], flag, layer_state_t::eLayerSecure);
575 }
576
setDeferredTransaction(SurfaceComposerClient::Transaction & t,layer_id id,const DeferredTransactionChange & dtc)577 void Replayer::setDeferredTransaction(SurfaceComposerClient::Transaction& t,
578 layer_id id, const DeferredTransactionChange& dtc) {
579 ALOGV("Layer %d: Setting Deferred Transaction -- layer_id=%d, "
580 "frame_number=%llu",
581 id, dtc.layer_id(), dtc.frame_number());
582 if (mLayers.count(dtc.layer_id()) == 0 || mLayers[dtc.layer_id()] == nullptr) {
583 ALOGE("Layer %d not found in Deferred Transaction", dtc.layer_id());
584 return;
585 }
586
587 auto handle = mLayers[dtc.layer_id()]->getHandle();
588
589 t.deferTransactionUntil_legacy(mLayers[id], handle, dtc.frame_number());
590 }
591
setDisplaySurface(SurfaceComposerClient::Transaction & t,display_id id,const DispSurfaceChange &)592 void Replayer::setDisplaySurface(SurfaceComposerClient::Transaction& t,
593 display_id id, const DispSurfaceChange& /*dsc*/) {
594 sp<IGraphicBufferProducer> outProducer;
595 sp<IGraphicBufferConsumer> outConsumer;
596 BufferQueue::createBufferQueue(&outProducer, &outConsumer);
597
598 t.setDisplaySurface(mDisplays[id], outProducer);
599 }
600
setDisplayLayerStack(SurfaceComposerClient::Transaction & t,display_id id,const LayerStackChange & lsc)601 void Replayer::setDisplayLayerStack(SurfaceComposerClient::Transaction& t,
602 display_id id, const LayerStackChange& lsc) {
603 t.setDisplayLayerStack(mDisplays[id], lsc.layer_stack());
604 }
605
setDisplaySize(SurfaceComposerClient::Transaction & t,display_id id,const SizeChange & sc)606 void Replayer::setDisplaySize(SurfaceComposerClient::Transaction& t,
607 display_id id, const SizeChange& sc) {
608 t.setDisplaySize(mDisplays[id], sc.w(), sc.h());
609 }
610
setDisplayProjection(SurfaceComposerClient::Transaction & t,display_id id,const ProjectionChange & pc)611 void Replayer::setDisplayProjection(SurfaceComposerClient::Transaction& t,
612 display_id id, const ProjectionChange& pc) {
613 Rect viewport = Rect(pc.viewport().left(), pc.viewport().top(), pc.viewport().right(),
614 pc.viewport().bottom());
615 Rect frame = Rect(pc.frame().left(), pc.frame().top(), pc.frame().right(), pc.frame().bottom());
616
617 t.setDisplayProjection(mDisplays[id], ui::toRotation(pc.orientation()), viewport, frame);
618 }
619
createSurfaceControl(const SurfaceCreation & create,const std::shared_ptr<Event> & event)620 status_t Replayer::createSurfaceControl(
621 const SurfaceCreation& create, const std::shared_ptr<Event>& event) {
622 event->readyToExecute();
623
624 ALOGV("Creating Surface Control: ID: %d", create.id());
625 sp<SurfaceControl> surfaceControl = mComposerClient->createSurface(
626 String8(create.name().c_str()), create.w(), create.h(), PIXEL_FORMAT_RGBA_8888, 0);
627
628 if (surfaceControl == nullptr) {
629 ALOGE("CreateSurfaceControl: unable to create surface control");
630 return BAD_VALUE;
631 }
632
633 std::lock_guard<std::mutex> lock1(mLayerLock);
634 auto& layer = mLayers[create.id()];
635 layer = surfaceControl;
636
637 mColors[create.id()] = HSV(rand() % 360, 1, 1);
638
639 mLayerCond.notify_all();
640
641 std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
642 if (mBufferQueueSchedulers.count(create.id()) != 0) {
643 mBufferQueueSchedulers[create.id()]->setSurfaceControl(
644 mLayers[create.id()], mColors[create.id()]);
645 }
646
647 return NO_ERROR;
648 }
649
injectVSyncEvent(const VSyncEvent & vSyncEvent,const std::shared_ptr<Event> & event)650 status_t Replayer::injectVSyncEvent(
651 const VSyncEvent& vSyncEvent, const std::shared_ptr<Event>& event) {
652 ALOGV("Injecting VSync Event");
653
654 event->readyToExecute();
655
656 SurfaceComposerClient::injectVSync(vSyncEvent.when());
657
658 return NO_ERROR;
659 }
660
createDisplay(const DisplayCreation & create,const std::shared_ptr<Event> & event)661 void Replayer::createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event) {
662 ALOGV("Creating display");
663 event->readyToExecute();
664
665 std::lock_guard<std::mutex> lock(mDisplayLock);
666 sp<IBinder> display = SurfaceComposerClient::createDisplay(
667 String8(create.name().c_str()), create.is_secure());
668 mDisplays[create.id()] = display;
669
670 mDisplayCond.notify_all();
671
672 ALOGV("Done creating display");
673 }
674
deleteDisplay(const DisplayDeletion & delete_,const std::shared_ptr<Event> & event)675 void Replayer::deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event) {
676 ALOGV("Delete display");
677 event->readyToExecute();
678
679 std::lock_guard<std::mutex> lock(mDisplayLock);
680 SurfaceComposerClient::destroyDisplay(mDisplays[delete_.id()]);
681 mDisplays.erase(delete_.id());
682 }
683
updatePowerMode(const PowerModeUpdate & pmu,const std::shared_ptr<Event> & event)684 void Replayer::updatePowerMode(const PowerModeUpdate& pmu, const std::shared_ptr<Event>& event) {
685 ALOGV("Updating power mode");
686 event->readyToExecute();
687 SurfaceComposerClient::setDisplayPowerMode(mDisplays[pmu.id()], pmu.mode());
688 }
689
waitUntilTimestamp(int64_t timestamp)690 void Replayer::waitUntilTimestamp(int64_t timestamp) {
691 ALOGV("Waiting for %lld nanoseconds...", static_cast<int64_t>(timestamp - mCurrentTime));
692 std::this_thread::sleep_for(std::chrono::nanoseconds(timestamp - mCurrentTime));
693 }
694
waitUntilDeferredTransactionLayerExists(const DeferredTransactionChange & dtc,std::unique_lock<std::mutex> & lock)695 void Replayer::waitUntilDeferredTransactionLayerExists(
696 const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock) {
697 if (mLayers.count(dtc.layer_id()) == 0 || mLayers[dtc.layer_id()] == nullptr) {
698 mLayerCond.wait(lock, [&] { return (mLayers[dtc.layer_id()] != nullptr); });
699 }
700 }
701
loadSurfaceComposerClient()702 status_t Replayer::loadSurfaceComposerClient() {
703 mComposerClient = new SurfaceComposerClient;
704 return mComposerClient->initCheck();
705 }
706
setReparentChange(SurfaceComposerClient::Transaction & t,layer_id id,const ReparentChange & c)707 void Replayer::setReparentChange(SurfaceComposerClient::Transaction& t,
708 layer_id id, const ReparentChange& c) {
709 sp<IBinder> newParentHandle = nullptr;
710 if (mLayers.count(c.parent_id()) != 0 && mLayers[c.parent_id()] != nullptr) {
711 newParentHandle = mLayers[c.parent_id()]->getHandle();
712 }
713 t.reparent(mLayers[id], newParentHandle);
714 }
715
setRelativeParentChange(SurfaceComposerClient::Transaction & t,layer_id id,const RelativeParentChange & c)716 void Replayer::setRelativeParentChange(SurfaceComposerClient::Transaction& t,
717 layer_id id, const RelativeParentChange& c) {
718 if (mLayers.count(c.relative_parent_id()) == 0 || mLayers[c.relative_parent_id()] == nullptr) {
719 ALOGE("Layer %d not found in set relative parent transaction", c.relative_parent_id());
720 return;
721 }
722 t.setRelativeLayer(mLayers[id], mLayers[c.relative_parent_id()]->getHandle(), c.z());
723 }
724
setDetachChildrenChange(SurfaceComposerClient::Transaction & t,layer_id id,const DetachChildrenChange & c)725 void Replayer::setDetachChildrenChange(SurfaceComposerClient::Transaction& t,
726 layer_id id, const DetachChildrenChange& c) {
727 t.detachChildren(mLayers[id]);
728 }
729
setReparentChildrenChange(SurfaceComposerClient::Transaction & t,layer_id id,const ReparentChildrenChange & c)730 void Replayer::setReparentChildrenChange(SurfaceComposerClient::Transaction& t,
731 layer_id id, const ReparentChildrenChange& c) {
732 if (mLayers.count(c.parent_id()) == 0 || mLayers[c.parent_id()] == nullptr) {
733 ALOGE("Layer %d not found in reparent children transaction", c.parent_id());
734 return;
735 }
736 t.reparentChildren(mLayers[id], mLayers[c.parent_id()]->getHandle());
737 }
738
setShadowRadiusChange(SurfaceComposerClient::Transaction & t,layer_id id,const ShadowRadiusChange & c)739 void Replayer::setShadowRadiusChange(SurfaceComposerClient::Transaction& t,
740 layer_id id, const ShadowRadiusChange& c) {
741 t.setShadowRadius(mLayers[id], c.radius());
742 }
743