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/Surface.h>
28 #include <private/gui/ComposerService.h>
29 #include <private/gui/LayerState.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.kSurfaceDeletion: {
290 std::thread(&Replayer::deleteSurfaceControl, this, increment.surface_deletion(), event)
291 .detach();
292 } break;
293 case increment.kBufferUpdate: {
294 std::lock_guard<std::mutex> lock1(mLayerLock);
295 std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
296
297 Dimensions dimensions(increment.buffer_update().w(), increment.buffer_update().h());
298 BufferEvent bufferEvent(event, dimensions);
299
300 auto layerId = increment.buffer_update().id();
301 if (mBufferQueueSchedulers.count(layerId) == 0) {
302 mBufferQueueSchedulers[layerId] = std::make_shared<BufferQueueScheduler>(
303 mLayers[layerId], mColors[layerId], layerId);
304 mBufferQueueSchedulers[layerId]->addEvent(bufferEvent);
305
306 std::thread(&BufferQueueScheduler::startScheduling,
307 mBufferQueueSchedulers[increment.buffer_update().id()].get())
308 .detach();
309 } else {
310 auto bqs = mBufferQueueSchedulers[increment.buffer_update().id()];
311 bqs->addEvent(bufferEvent);
312 }
313 } break;
314 case increment.kVsyncEvent: {
315 std::thread(&Replayer::injectVSyncEvent, this, increment.vsync_event(), event).detach();
316 } break;
317 case increment.kDisplayCreation: {
318 std::thread(&Replayer::createDisplay, this, increment.display_creation(), event)
319 .detach();
320 } break;
321 case increment.kDisplayDeletion: {
322 std::thread(&Replayer::deleteDisplay, this, increment.display_deletion(), event)
323 .detach();
324 } break;
325 case increment.kPowerModeUpdate: {
326 std::thread(&Replayer::updatePowerMode, this, increment.power_mode_update(), event)
327 .detach();
328 } break;
329 default:
330 ALOGE("Unknown Increment Type: %d", increment.increment_case());
331 status = BAD_VALUE;
332 break;
333 }
334
335 return status;
336 }
337
doTransaction(const Transaction & t,const std::shared_ptr<Event> & event)338 status_t Replayer::doTransaction(const Transaction& t, const std::shared_ptr<Event>& event) {
339 ALOGV("Started Transaction");
340
341 SurfaceComposerClient::openGlobalTransaction();
342
343 status_t status = NO_ERROR;
344
345 status = doSurfaceTransaction(t.surface_change());
346 doDisplayTransaction(t.display_change());
347
348 if (t.animation()) {
349 SurfaceComposerClient::setAnimationTransaction();
350 }
351
352 event->readyToExecute();
353
354 SurfaceComposerClient::closeGlobalTransaction(t.synchronous());
355
356 ALOGV("Ended Transaction");
357
358 return status;
359 }
360
doSurfaceTransaction(const SurfaceChanges & surfaceChanges)361 status_t Replayer::doSurfaceTransaction(const SurfaceChanges& surfaceChanges) {
362 status_t status = NO_ERROR;
363
364 for (const SurfaceChange& change : surfaceChanges) {
365 std::unique_lock<std::mutex> lock(mLayerLock);
366 if (mLayers[change.id()] == nullptr) {
367 mLayerCond.wait(lock, [&] { return (mLayers[change.id()] != nullptr); });
368 }
369
370 switch (change.SurfaceChange_case()) {
371 case SurfaceChange::SurfaceChangeCase::kPosition:
372 status = setPosition(change.id(), change.position());
373 break;
374 case SurfaceChange::SurfaceChangeCase::kSize:
375 status = setSize(change.id(), change.size());
376 break;
377 case SurfaceChange::SurfaceChangeCase::kAlpha:
378 status = setAlpha(change.id(), change.alpha());
379 break;
380 case SurfaceChange::SurfaceChangeCase::kLayer:
381 status = setLayer(change.id(), change.layer());
382 break;
383 case SurfaceChange::SurfaceChangeCase::kCrop:
384 status = setCrop(change.id(), change.crop());
385 break;
386 case SurfaceChange::SurfaceChangeCase::kMatrix:
387 status = setMatrix(change.id(), change.matrix());
388 break;
389 case SurfaceChange::SurfaceChangeCase::kFinalCrop:
390 status = setFinalCrop(change.id(), change.final_crop());
391 break;
392 case SurfaceChange::SurfaceChangeCase::kOverrideScalingMode:
393 status = setOverrideScalingMode(change.id(), change.override_scaling_mode());
394 break;
395 case SurfaceChange::SurfaceChangeCase::kTransparentRegionHint:
396 status = setTransparentRegionHint(change.id(), change.transparent_region_hint());
397 break;
398 case SurfaceChange::SurfaceChangeCase::kLayerStack:
399 status = setLayerStack(change.id(), change.layer_stack());
400 break;
401 case SurfaceChange::SurfaceChangeCase::kHiddenFlag:
402 status = setHiddenFlag(change.id(), change.hidden_flag());
403 break;
404 case SurfaceChange::SurfaceChangeCase::kOpaqueFlag:
405 status = setOpaqueFlag(change.id(), change.opaque_flag());
406 break;
407 case SurfaceChange::SurfaceChangeCase::kSecureFlag:
408 status = setSecureFlag(change.id(), change.secure_flag());
409 break;
410 case SurfaceChange::SurfaceChangeCase::kDeferredTransaction:
411 waitUntilDeferredTransactionLayerExists(change.deferred_transaction(), lock);
412 status = setDeferredTransaction(change.id(), change.deferred_transaction());
413 break;
414 default:
415 status = NO_ERROR;
416 break;
417 }
418
419 if (status != NO_ERROR) {
420 ALOGE("SET TRANSACTION FAILED");
421 return status;
422 }
423 }
424 return status;
425 }
426
doDisplayTransaction(const DisplayChanges & displayChanges)427 void Replayer::doDisplayTransaction(const DisplayChanges& displayChanges) {
428 for (const DisplayChange& change : displayChanges) {
429 ALOGV("Doing display transaction");
430 std::unique_lock<std::mutex> lock(mDisplayLock);
431 if (mDisplays[change.id()] == nullptr) {
432 mDisplayCond.wait(lock, [&] { return (mDisplays[change.id()] != nullptr); });
433 }
434
435 switch (change.DisplayChange_case()) {
436 case DisplayChange::DisplayChangeCase::kSurface:
437 setDisplaySurface(change.id(), change.surface());
438 break;
439 case DisplayChange::DisplayChangeCase::kLayerStack:
440 setDisplayLayerStack(change.id(), change.layer_stack());
441 break;
442 case DisplayChange::DisplayChangeCase::kSize:
443 setDisplaySize(change.id(), change.size());
444 break;
445 case DisplayChange::DisplayChangeCase::kProjection:
446 setDisplayProjection(change.id(), change.projection());
447 break;
448 default:
449 break;
450 }
451 }
452 }
453
setPosition(layer_id id,const PositionChange & pc)454 status_t Replayer::setPosition(layer_id id, const PositionChange& pc) {
455 ALOGV("Layer %d: Setting Position -- x=%f, y=%f", id, pc.x(), pc.y());
456 return mLayers[id]->setPosition(pc.x(), pc.y());
457 }
458
setSize(layer_id id,const SizeChange & sc)459 status_t Replayer::setSize(layer_id id, const SizeChange& sc) {
460 ALOGV("Layer %d: Setting Size -- w=%u, h=%u", id, sc.w(), sc.h());
461 return mLayers[id]->setSize(sc.w(), sc.h());
462 }
463
setLayer(layer_id id,const LayerChange & lc)464 status_t Replayer::setLayer(layer_id id, const LayerChange& lc) {
465 ALOGV("Layer %d: Setting Layer -- layer=%d", id, lc.layer());
466 return mLayers[id]->setLayer(lc.layer());
467 }
468
setAlpha(layer_id id,const AlphaChange & ac)469 status_t Replayer::setAlpha(layer_id id, const AlphaChange& ac) {
470 ALOGV("Layer %d: Setting Alpha -- alpha=%f", id, ac.alpha());
471 return mLayers[id]->setAlpha(ac.alpha());
472 }
473
setCrop(layer_id id,const CropChange & cc)474 status_t Replayer::setCrop(layer_id id, const CropChange& cc) {
475 ALOGV("Layer %d: Setting Crop -- left=%d, top=%d, right=%d, bottom=%d", id,
476 cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
477 cc.rectangle().bottom());
478
479 Rect r = Rect(cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
480 cc.rectangle().bottom());
481 return mLayers[id]->setCrop(r);
482 }
483
setFinalCrop(layer_id id,const FinalCropChange & fcc)484 status_t Replayer::setFinalCrop(layer_id id, const FinalCropChange& fcc) {
485 ALOGV("Layer %d: Setting Final Crop -- left=%d, top=%d, right=%d, bottom=%d", id,
486 fcc.rectangle().left(), fcc.rectangle().top(), fcc.rectangle().right(),
487 fcc.rectangle().bottom());
488 Rect r = Rect(fcc.rectangle().left(), fcc.rectangle().top(), fcc.rectangle().right(),
489 fcc.rectangle().bottom());
490 return mLayers[id]->setFinalCrop(r);
491 }
492
setMatrix(layer_id id,const MatrixChange & mc)493 status_t Replayer::setMatrix(layer_id id, const MatrixChange& mc) {
494 ALOGV("Layer %d: Setting Matrix -- dsdx=%f, dtdx=%f, dsdy=%f, dtdy=%f", id, mc.dsdx(),
495 mc.dtdx(), mc.dsdy(), mc.dtdy());
496 return mLayers[id]->setMatrix(mc.dsdx(), mc.dtdx(), mc.dsdy(), mc.dtdy());
497 }
498
setOverrideScalingMode(layer_id id,const OverrideScalingModeChange & osmc)499 status_t Replayer::setOverrideScalingMode(layer_id id, const OverrideScalingModeChange& osmc) {
500 ALOGV("Layer %d: Setting Override Scaling Mode -- mode=%d", id, osmc.override_scaling_mode());
501 return mLayers[id]->setOverrideScalingMode(osmc.override_scaling_mode());
502 }
503
setTransparentRegionHint(layer_id id,const TransparentRegionHintChange & trhc)504 status_t Replayer::setTransparentRegionHint(layer_id id, const TransparentRegionHintChange& trhc) {
505 ALOGV("Setting Transparent Region Hint");
506 Region re = Region();
507
508 for (auto r : trhc.region()) {
509 Rect rect = Rect(r.left(), r.top(), r.right(), r.bottom());
510 re.merge(rect);
511 }
512
513 return mLayers[id]->setTransparentRegionHint(re);
514 }
515
setLayerStack(layer_id id,const LayerStackChange & lsc)516 status_t Replayer::setLayerStack(layer_id id, const LayerStackChange& lsc) {
517 ALOGV("Layer %d: Setting LayerStack -- layer_stack=%d", id, lsc.layer_stack());
518 return mLayers[id]->setLayerStack(lsc.layer_stack());
519 }
520
setHiddenFlag(layer_id id,const HiddenFlagChange & hfc)521 status_t Replayer::setHiddenFlag(layer_id id, const HiddenFlagChange& hfc) {
522 ALOGV("Layer %d: Setting Hidden Flag -- hidden_flag=%d", id, hfc.hidden_flag());
523 layer_id flag = hfc.hidden_flag() ? layer_state_t::eLayerHidden : 0;
524
525 return mLayers[id]->setFlags(flag, layer_state_t::eLayerHidden);
526 }
527
setOpaqueFlag(layer_id id,const OpaqueFlagChange & ofc)528 status_t Replayer::setOpaqueFlag(layer_id id, const OpaqueFlagChange& ofc) {
529 ALOGV("Layer %d: Setting Opaque Flag -- opaque_flag=%d", id, ofc.opaque_flag());
530 layer_id flag = ofc.opaque_flag() ? layer_state_t::eLayerOpaque : 0;
531
532 return mLayers[id]->setFlags(flag, layer_state_t::eLayerOpaque);
533 }
534
setSecureFlag(layer_id id,const SecureFlagChange & sfc)535 status_t Replayer::setSecureFlag(layer_id id, const SecureFlagChange& sfc) {
536 ALOGV("Layer %d: Setting Secure Flag -- secure_flag=%d", id, sfc.secure_flag());
537 layer_id flag = sfc.secure_flag() ? layer_state_t::eLayerSecure : 0;
538
539 return mLayers[id]->setFlags(flag, layer_state_t::eLayerSecure);
540 }
541
setDeferredTransaction(layer_id id,const DeferredTransactionChange & dtc)542 status_t Replayer::setDeferredTransaction(layer_id id, const DeferredTransactionChange& dtc) {
543 ALOGV("Layer %d: Setting Deferred Transaction -- layer_id=%d, "
544 "frame_number=%llu",
545 id, dtc.layer_id(), dtc.frame_number());
546 if (mLayers.count(dtc.layer_id()) == 0 || mLayers[dtc.layer_id()] == nullptr) {
547 ALOGE("Layer %d not found in Deferred Transaction", dtc.layer_id());
548 return BAD_VALUE;
549 }
550
551 auto handle = mLayers[dtc.layer_id()]->getHandle();
552
553 return mLayers[id]->deferTransactionUntil(handle, dtc.frame_number());
554 }
555
setDisplaySurface(display_id id,const DispSurfaceChange &)556 void Replayer::setDisplaySurface(display_id id, const DispSurfaceChange& /*dsc*/) {
557 sp<IGraphicBufferProducer> outProducer;
558 sp<IGraphicBufferConsumer> outConsumer;
559 BufferQueue::createBufferQueue(&outProducer, &outConsumer);
560
561 SurfaceComposerClient::setDisplaySurface(mDisplays[id], outProducer);
562 }
563
setDisplayLayerStack(display_id id,const LayerStackChange & lsc)564 void Replayer::setDisplayLayerStack(display_id id, const LayerStackChange& lsc) {
565 SurfaceComposerClient::setDisplayLayerStack(mDisplays[id], lsc.layer_stack());
566 }
567
setDisplaySize(display_id id,const SizeChange & sc)568 void Replayer::setDisplaySize(display_id id, const SizeChange& sc) {
569 SurfaceComposerClient::setDisplaySize(mDisplays[id], sc.w(), sc.h());
570 }
571
setDisplayProjection(display_id id,const ProjectionChange & pc)572 void Replayer::setDisplayProjection(display_id id, const ProjectionChange& pc) {
573 Rect viewport = Rect(pc.viewport().left(), pc.viewport().top(), pc.viewport().right(),
574 pc.viewport().bottom());
575 Rect frame = Rect(pc.frame().left(), pc.frame().top(), pc.frame().right(), pc.frame().bottom());
576
577 SurfaceComposerClient::setDisplayProjection(mDisplays[id], pc.orientation(), viewport, frame);
578 }
579
createSurfaceControl(const SurfaceCreation & create,const std::shared_ptr<Event> & event)580 status_t Replayer::createSurfaceControl(
581 const SurfaceCreation& create, const std::shared_ptr<Event>& event) {
582 event->readyToExecute();
583
584 ALOGV("Creating Surface Control: ID: %d", create.id());
585 sp<SurfaceControl> surfaceControl = mComposerClient->createSurface(
586 String8(create.name().c_str()), create.w(), create.h(), PIXEL_FORMAT_RGBA_8888, 0);
587
588 if (surfaceControl == nullptr) {
589 ALOGE("CreateSurfaceControl: unable to create surface control");
590 return BAD_VALUE;
591 }
592
593 std::lock_guard<std::mutex> lock1(mLayerLock);
594 auto& layer = mLayers[create.id()];
595 layer = surfaceControl;
596
597 mColors[create.id()] = HSV(rand() % 360, 1, 1);
598
599 mLayerCond.notify_all();
600
601 std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
602 if (mBufferQueueSchedulers.count(create.id()) != 0) {
603 mBufferQueueSchedulers[create.id()]->setSurfaceControl(
604 mLayers[create.id()], mColors[create.id()]);
605 }
606
607 return NO_ERROR;
608 }
609
deleteSurfaceControl(const SurfaceDeletion & delete_,const std::shared_ptr<Event> & event)610 status_t Replayer::deleteSurfaceControl(
611 const SurfaceDeletion& delete_, const std::shared_ptr<Event>& event) {
612 ALOGV("Deleting %d Surface Control", delete_.id());
613 event->readyToExecute();
614
615 std::lock_guard<std::mutex> lock1(mPendingLayersLock);
616
617 mLayersPendingRemoval.push_back(delete_.id());
618
619 const auto& iterator = mBufferQueueSchedulers.find(delete_.id());
620 if (iterator != mBufferQueueSchedulers.end()) {
621 (*iterator).second->stopScheduling();
622 }
623
624 std::lock_guard<std::mutex> lock2(mLayerLock);
625 if (mLayers[delete_.id()] != nullptr) {
626 mComposerClient->destroySurface(mLayers[delete_.id()]->getHandle());
627 }
628
629 return NO_ERROR;
630 }
631
doDeleteSurfaceControls()632 void Replayer::doDeleteSurfaceControls() {
633 std::lock_guard<std::mutex> lock1(mPendingLayersLock);
634 std::lock_guard<std::mutex> lock2(mLayerLock);
635 if (!mLayersPendingRemoval.empty()) {
636 for (int id : mLayersPendingRemoval) {
637 mLayers.erase(id);
638 mColors.erase(id);
639 mBufferQueueSchedulers.erase(id);
640 }
641 mLayersPendingRemoval.clear();
642 }
643 }
644
injectVSyncEvent(const VSyncEvent & vSyncEvent,const std::shared_ptr<Event> & event)645 status_t Replayer::injectVSyncEvent(
646 const VSyncEvent& vSyncEvent, const std::shared_ptr<Event>& event) {
647 ALOGV("Injecting VSync Event");
648
649 doDeleteSurfaceControls();
650
651 event->readyToExecute();
652
653 SurfaceComposerClient::injectVSync(vSyncEvent.when());
654
655 return NO_ERROR;
656 }
657
createDisplay(const DisplayCreation & create,const std::shared_ptr<Event> & event)658 void Replayer::createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event) {
659 ALOGV("Creating display");
660 event->readyToExecute();
661
662 std::lock_guard<std::mutex> lock(mDisplayLock);
663 sp<IBinder> display = SurfaceComposerClient::createDisplay(
664 String8(create.name().c_str()), create.is_secure());
665 mDisplays[create.id()] = display;
666
667 mDisplayCond.notify_all();
668
669 ALOGV("Done creating display");
670 }
671
deleteDisplay(const DisplayDeletion & delete_,const std::shared_ptr<Event> & event)672 void Replayer::deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event) {
673 ALOGV("Delete display");
674 event->readyToExecute();
675
676 std::lock_guard<std::mutex> lock(mDisplayLock);
677 SurfaceComposerClient::destroyDisplay(mDisplays[delete_.id()]);
678 mDisplays.erase(delete_.id());
679 }
680
updatePowerMode(const PowerModeUpdate & pmu,const std::shared_ptr<Event> & event)681 void Replayer::updatePowerMode(const PowerModeUpdate& pmu, const std::shared_ptr<Event>& event) {
682 ALOGV("Updating power mode");
683 event->readyToExecute();
684 SurfaceComposerClient::setDisplayPowerMode(mDisplays[pmu.id()], pmu.mode());
685 }
686
waitUntilTimestamp(int64_t timestamp)687 void Replayer::waitUntilTimestamp(int64_t timestamp) {
688 ALOGV("Waiting for %lld nanoseconds...", static_cast<int64_t>(timestamp - mCurrentTime));
689 std::this_thread::sleep_for(std::chrono::nanoseconds(timestamp - mCurrentTime));
690 }
691
waitUntilDeferredTransactionLayerExists(const DeferredTransactionChange & dtc,std::unique_lock<std::mutex> & lock)692 void Replayer::waitUntilDeferredTransactionLayerExists(
693 const DeferredTransactionChange& dtc, std::unique_lock<std::mutex>& lock) {
694 if (mLayers.count(dtc.layer_id()) == 0 || mLayers[dtc.layer_id()] == nullptr) {
695 mLayerCond.wait(lock, [&] { return (mLayers[dtc.layer_id()] != nullptr); });
696 }
697 }
698
loadSurfaceComposerClient()699 status_t Replayer::loadSurfaceComposerClient() {
700 mComposerClient = new SurfaceComposerClient;
701 return mComposerClient->initCheck();
702 }
703