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