Merge "Remove __linux__ ifdefs where not needed" into main
diff --git a/cmds/surfacereplayer/replayer/Replayer.cpp b/cmds/surfacereplayer/replayer/Replayer.cpp
deleted file mode 100644
index 44235cc..0000000
--- a/cmds/surfacereplayer/replayer/Replayer.cpp
+++ /dev/null
@@ -1,707 +0,0 @@
-/* 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.
- */
-
-//#define LOG_NDEBUG 0
-#define LOG_TAG "SurfaceReplayer"
-
-#include "Replayer.h"
-
-#include <android/native_window.h>
-
-#include <android-base/file.h>
-
-#include <gui/BufferQueue.h>
-#include <gui/ISurfaceComposer.h>
-#include <gui/LayerState.h>
-#include <gui/Surface.h>
-#include <private/gui/ComposerService.h>
-
-#include <utils/Log.h>
-#include <utils/String8.h>
-#include <utils/Trace.h>
-
-#include <chrono>
-#include <cmath>
-#include <condition_variable>
-#include <cstdlib>
-#include <fstream>
-#include <functional>
-#include <iostream>
-#include <mutex>
-#include <sstream>
-#include <string>
-#include <thread>
-#include <vector>
-
-using namespace android;
-
-std::atomic_bool Replayer::sReplayingManually(false);
-
-Replayer::Replayer(const std::string& filename, bool replayManually, int numThreads, bool wait,
- nsecs_t stopHere)
- : mTrace(),
- mLoaded(false),
- mIncrementIndex(0),
- mCurrentTime(0),
- mNumThreads(numThreads),
- mWaitForTimeStamps(wait),
- mStopTimeStamp(stopHere) {
- srand(RAND_COLOR_SEED);
-
- std::string input;
- if (!android::base::ReadFileToString(filename, &input, true)) {
- std::cerr << "Trace did not load. Does " << filename << " exist?" << std::endl;
- abort();
- }
-
- mLoaded = mTrace.ParseFromString(input);
- if (!mLoaded) {
- std::cerr << "Trace did not load." << std::endl;
- abort();
- }
-
- mCurrentTime = mTrace.increment(0).time_stamp();
-
- sReplayingManually.store(replayManually);
-
- if (stopHere < 0) {
- mHasStopped = true;
- }
-}
-
-Replayer::Replayer(const Trace& t, bool replayManually, int numThreads, bool wait, nsecs_t stopHere)
- : mTrace(t),
- mLoaded(true),
- mIncrementIndex(0),
- mCurrentTime(0),
- mNumThreads(numThreads),
- mWaitForTimeStamps(wait),
- mStopTimeStamp(stopHere) {
- srand(RAND_COLOR_SEED);
- mCurrentTime = mTrace.increment(0).time_stamp();
-
- sReplayingManually.store(replayManually);
-
- if (stopHere < 0) {
- mHasStopped = true;
- }
-}
-
-status_t Replayer::replay() {
- signal(SIGINT, Replayer::stopAutoReplayHandler); //for manual control
-
- ALOGV("There are %d increments.", mTrace.increment_size());
-
- status_t status = loadSurfaceComposerClient();
-
- if (status != NO_ERROR) {
- ALOGE("Couldn't create SurfaceComposerClient (%d)", status);
- return status;
- }
-
- SurfaceComposerClient::enableVSyncInjections(true);
-
- initReplay();
-
- ALOGV("Starting actual Replay!");
- while (!mPendingIncrements.empty()) {
- mCurrentIncrement = mTrace.increment(mIncrementIndex);
-
- if (mHasStopped == false && mCurrentIncrement.time_stamp() >= mStopTimeStamp) {
- mHasStopped = true;
- sReplayingManually.store(true);
- }
-
- waitForConsoleCommmand();
-
- if (mWaitForTimeStamps) {
- waitUntilTimestamp(mCurrentIncrement.time_stamp());
- }
-
- auto event = mPendingIncrements.front();
- mPendingIncrements.pop();
-
- event->complete();
-
- if (event->getIncrementType() == Increment::kVsyncEvent) {
- mWaitingForNextVSync = false;
- }
-
- if (mIncrementIndex + mNumThreads < mTrace.increment_size()) {
- status = dispatchEvent(mIncrementIndex + mNumThreads);
-
- if (status != NO_ERROR) {
- SurfaceComposerClient::enableVSyncInjections(false);
- return status;
- }
- }
-
- mIncrementIndex++;
- mCurrentTime = mCurrentIncrement.time_stamp();
- }
-
- SurfaceComposerClient::enableVSyncInjections(false);
-
- return status;
-}
-
-status_t Replayer::initReplay() {
- for (int i = 0; i < mNumThreads && i < mTrace.increment_size(); i++) {
- status_t status = dispatchEvent(i);
-
- if (status != NO_ERROR) {
- ALOGE("Unable to dispatch event (%d)", status);
- return status;
- }
- }
-
- return NO_ERROR;
-}
-
-void Replayer::stopAutoReplayHandler(int /*signal*/) {
- if (sReplayingManually) {
- SurfaceComposerClient::enableVSyncInjections(false);
- exit(0);
- }
-
- sReplayingManually.store(true);
-}
-
-std::vector<std::string> split(const std::string& s, const char delim) {
- std::vector<std::string> elems;
- std::stringstream ss(s);
- std::string item;
- while (getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
-}
-
-bool isNumber(const std::string& s) {
- return !s.empty() &&
- std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
-}
-
-void Replayer::waitForConsoleCommmand() {
- if (!sReplayingManually || mWaitingForNextVSync) {
- return;
- }
-
- while (true) {
- std::string input = "";
- std::cout << "> ";
- getline(std::cin, input);
-
- if (input.empty()) {
- input = mLastInput;
- } else {
- mLastInput = input;
- }
-
- if (mLastInput.empty()) {
- continue;
- }
-
- std::vector<std::string> inputs = split(input, ' ');
-
- if (inputs[0] == "n") { // next vsync
- mWaitingForNextVSync = true;
- break;
-
- } else if (inputs[0] == "ni") { // next increment
- break;
-
- } else if (inputs[0] == "c") { // continue
- if (inputs.size() > 1 && isNumber(inputs[1])) {
- long milliseconds = stoi(inputs[1]);
- std::thread([&] {
- std::cout << "Started!" << std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
- sReplayingManually.store(true);
- std::cout << "Should have stopped!" << std::endl;
- }).detach();
- }
- sReplayingManually.store(false);
- mWaitingForNextVSync = false;
- break;
-
- } else if (inputs[0] == "s") { // stop at this timestamp
- if (inputs.size() < 1) {
- std::cout << "No time stamp given" << std::endl;
- continue;
- }
- sReplayingManually.store(false);
- mStopTimeStamp = stol(inputs[1]);
- mHasStopped = false;
- break;
- } else if (inputs[0] == "l") { // list
- std::cout << "Time stamp: " << mCurrentIncrement.time_stamp() << "\n";
- continue;
- } else if (inputs[0] == "q") { // quit
- SurfaceComposerClient::enableVSyncInjections(false);
- exit(0);
-
- } else if (inputs[0] == "h") { // help
- // add help menu
- std::cout << "Manual Replay options:\n";
- std::cout << " n - Go to next VSync\n";
- std::cout << " ni - Go to next increment\n";
- std::cout << " c - Continue\n";
- std::cout << " c [milliseconds] - Continue until specified number of milliseconds\n";
- std::cout << " s [timestamp] - Continue and stop at specified timestamp\n";
- std::cout << " l - List out timestamp of current increment\n";
- std::cout << " h - Display help menu\n";
- std::cout << std::endl;
- continue;
- }
-
- std::cout << "Invalid Command" << std::endl;
- }
-}
-
-status_t Replayer::dispatchEvent(int index) {
- auto increment = mTrace.increment(index);
- std::shared_ptr<Event> event = std::make_shared<Event>(increment.increment_case());
- mPendingIncrements.push(event);
-
- status_t status = NO_ERROR;
- switch (increment.increment_case()) {
- case increment.kTransaction: {
- std::thread(&Replayer::doTransaction, this, increment.transaction(), event).detach();
- } break;
- case increment.kSurfaceCreation: {
- std::thread(&Replayer::createSurfaceControl, this, increment.surface_creation(), event)
- .detach();
- } break;
- case increment.kBufferUpdate: {
- std::lock_guard<std::mutex> lock1(mLayerLock);
- std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
-
- Dimensions dimensions(increment.buffer_update().w(), increment.buffer_update().h());
- BufferEvent bufferEvent(event, dimensions);
-
- auto layerId = increment.buffer_update().id();
- if (mBufferQueueSchedulers.count(layerId) == 0) {
- mBufferQueueSchedulers[layerId] = std::make_shared<BufferQueueScheduler>(
- mLayers[layerId], mColors[layerId], layerId);
- mBufferQueueSchedulers[layerId]->addEvent(bufferEvent);
-
- std::thread(&BufferQueueScheduler::startScheduling,
- mBufferQueueSchedulers[increment.buffer_update().id()].get())
- .detach();
- } else {
- auto bqs = mBufferQueueSchedulers[increment.buffer_update().id()];
- bqs->addEvent(bufferEvent);
- }
- } break;
- case increment.kVsyncEvent: {
- std::thread(&Replayer::injectVSyncEvent, this, increment.vsync_event(), event).detach();
- } break;
- case increment.kDisplayCreation: {
- std::thread(&Replayer::createDisplay, this, increment.display_creation(), event)
- .detach();
- } break;
- case increment.kDisplayDeletion: {
- std::thread(&Replayer::deleteDisplay, this, increment.display_deletion(), event)
- .detach();
- } break;
- case increment.kPowerModeUpdate: {
- std::thread(&Replayer::updatePowerMode, this, increment.power_mode_update(), event)
- .detach();
- } break;
- default:
- ALOGE("Unknown Increment Type: %d", increment.increment_case());
- status = BAD_VALUE;
- break;
- }
-
- return status;
-}
-
-status_t Replayer::doTransaction(const Transaction& t, const std::shared_ptr<Event>& event) {
- ALOGV("Started Transaction");
-
- SurfaceComposerClient::Transaction liveTransaction;
-
- status_t status = NO_ERROR;
-
- status = doSurfaceTransaction(liveTransaction, t.surface_change());
- doDisplayTransaction(liveTransaction, t.display_change());
-
- if (t.animation()) {
- liveTransaction.setAnimationTransaction();
- }
-
- event->readyToExecute();
-
- liveTransaction.apply(t.synchronous());
-
- ALOGV("Ended Transaction");
-
- return status;
-}
-
-status_t Replayer::doSurfaceTransaction(
- SurfaceComposerClient::Transaction& transaction,
- const SurfaceChanges& surfaceChanges) {
- status_t status = NO_ERROR;
-
- for (const SurfaceChange& change : surfaceChanges) {
- std::unique_lock<std::mutex> lock(mLayerLock);
- if (mLayers[change.id()] == nullptr) {
- mLayerCond.wait(lock, [&] { return (mLayers[change.id()] != nullptr); });
- }
-
- switch (change.SurfaceChange_case()) {
- case SurfaceChange::SurfaceChangeCase::kPosition:
- setPosition(transaction, change.id(), change.position());
- break;
- case SurfaceChange::SurfaceChangeCase::kSize:
- setSize(transaction, change.id(), change.size());
- break;
- case SurfaceChange::SurfaceChangeCase::kAlpha:
- setAlpha(transaction, change.id(), change.alpha());
- break;
- case SurfaceChange::SurfaceChangeCase::kLayer:
- setLayer(transaction, change.id(), change.layer());
- break;
- case SurfaceChange::SurfaceChangeCase::kCrop:
- setCrop(transaction, change.id(), change.crop());
- break;
- case SurfaceChange::SurfaceChangeCase::kCornerRadius:
- setCornerRadius(transaction, change.id(), change.corner_radius());
- break;
- case SurfaceChange::SurfaceChangeCase::kMatrix:
- setMatrix(transaction, change.id(), change.matrix());
- break;
- case SurfaceChange::SurfaceChangeCase::kTransparentRegionHint:
- setTransparentRegionHint(transaction, change.id(),
- change.transparent_region_hint());
- break;
- case SurfaceChange::SurfaceChangeCase::kLayerStack:
- setLayerStack(transaction, change.id(), change.layer_stack());
- break;
- case SurfaceChange::SurfaceChangeCase::kHiddenFlag:
- setHiddenFlag(transaction, change.id(), change.hidden_flag());
- break;
- case SurfaceChange::SurfaceChangeCase::kOpaqueFlag:
- setOpaqueFlag(transaction, change.id(), change.opaque_flag());
- break;
- case SurfaceChange::SurfaceChangeCase::kSecureFlag:
- setSecureFlag(transaction, change.id(), change.secure_flag());
- break;
- case SurfaceChange::SurfaceChangeCase::kReparent:
- setReparentChange(transaction, change.id(), change.reparent());
- break;
- case SurfaceChange::SurfaceChangeCase::kRelativeParent:
- setRelativeParentChange(transaction, change.id(), change.relative_parent());
- break;
- case SurfaceChange::SurfaceChangeCase::kShadowRadius:
- setShadowRadiusChange(transaction, change.id(), change.shadow_radius());
- break;
- case SurfaceChange::SurfaceChangeCase::kBlurRegions:
- setBlurRegionsChange(transaction, change.id(), change.blur_regions());
- break;
- default:
- status = 1;
- break;
- }
-
- if (status != NO_ERROR) {
- ALOGE("Unknown Transaction Code");
- return status;
- }
- }
- return status;
-}
-
-void Replayer::doDisplayTransaction(SurfaceComposerClient::Transaction& t,
- const DisplayChanges& displayChanges) {
- for (const DisplayChange& change : displayChanges) {
- ALOGV("Doing display transaction");
- std::unique_lock<std::mutex> lock(mDisplayLock);
- if (mDisplays[change.id()] == nullptr) {
- mDisplayCond.wait(lock, [&] { return (mDisplays[change.id()] != nullptr); });
- }
-
- switch (change.DisplayChange_case()) {
- case DisplayChange::DisplayChangeCase::kSurface:
- setDisplaySurface(t, change.id(), change.surface());
- break;
- case DisplayChange::DisplayChangeCase::kLayerStack:
- setDisplayLayerStack(t, change.id(), change.layer_stack());
- break;
- case DisplayChange::DisplayChangeCase::kSize:
- setDisplaySize(t, change.id(), change.size());
- break;
- case DisplayChange::DisplayChangeCase::kProjection:
- setDisplayProjection(t, change.id(), change.projection());
- break;
- default:
- break;
- }
- }
-}
-
-void Replayer::setPosition(SurfaceComposerClient::Transaction& t,
- layer_id id, const PositionChange& pc) {
- ALOGV("Layer %d: Setting Position -- x=%f, y=%f", id, pc.x(), pc.y());
- t.setPosition(mLayers[id], pc.x(), pc.y());
-}
-
-void Replayer::setSize(SurfaceComposerClient::Transaction& t,
- layer_id id, const SizeChange& sc) {
- ALOGV("Layer %d: Setting Size -- w=%u, h=%u", id, sc.w(), sc.h());
-}
-
-void Replayer::setLayer(SurfaceComposerClient::Transaction& t,
- layer_id id, const LayerChange& lc) {
- ALOGV("Layer %d: Setting Layer -- layer=%d", id, lc.layer());
- t.setLayer(mLayers[id], lc.layer());
-}
-
-void Replayer::setAlpha(SurfaceComposerClient::Transaction& t,
- layer_id id, const AlphaChange& ac) {
- ALOGV("Layer %d: Setting Alpha -- alpha=%f", id, ac.alpha());
- t.setAlpha(mLayers[id], ac.alpha());
-}
-
-void Replayer::setCrop(SurfaceComposerClient::Transaction& t,
- layer_id id, const CropChange& cc) {
- ALOGV("Layer %d: Setting Crop -- left=%d, top=%d, right=%d, bottom=%d", id,
- cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
- cc.rectangle().bottom());
-
- Rect r = Rect(cc.rectangle().left(), cc.rectangle().top(), cc.rectangle().right(),
- cc.rectangle().bottom());
- t.setCrop(mLayers[id], r);
-}
-
-void Replayer::setCornerRadius(SurfaceComposerClient::Transaction& t,
- layer_id id, const CornerRadiusChange& cc) {
- ALOGV("Layer %d: Setting Corner Radius -- cornerRadius=%d", id, cc.corner_radius());
-
- t.setCornerRadius(mLayers[id], cc.corner_radius());
-}
-
-void Replayer::setBackgroundBlurRadius(SurfaceComposerClient::Transaction& t,
- layer_id id, const BackgroundBlurRadiusChange& cc) {
- ALOGV("Layer %d: Setting Background Blur Radius -- backgroundBlurRadius=%d", id,
- cc.background_blur_radius());
-
- t.setBackgroundBlurRadius(mLayers[id], cc.background_blur_radius());
-}
-
-void Replayer::setMatrix(SurfaceComposerClient::Transaction& t,
- layer_id id, const MatrixChange& mc) {
- ALOGV("Layer %d: Setting Matrix -- dsdx=%f, dtdx=%f, dsdy=%f, dtdy=%f", id, mc.dsdx(),
- mc.dtdx(), mc.dsdy(), mc.dtdy());
- t.setMatrix(mLayers[id], mc.dsdx(), mc.dtdx(), mc.dsdy(), mc.dtdy());
-}
-
-void Replayer::setTransparentRegionHint(SurfaceComposerClient::Transaction& t,
- layer_id id, const TransparentRegionHintChange& trhc) {
- ALOGV("Setting Transparent Region Hint");
- Region re = Region();
-
- for (const auto& r : trhc.region()) {
- Rect rect = Rect(r.left(), r.top(), r.right(), r.bottom());
- re.merge(rect);
- }
-
- t.setTransparentRegionHint(mLayers[id], re);
-}
-
-void Replayer::setLayerStack(SurfaceComposerClient::Transaction& t,
- layer_id id, const LayerStackChange& lsc) {
- ALOGV("Layer %d: Setting LayerStack -- layer_stack=%d", id, lsc.layer_stack());
- t.setLayerStack(mLayers[id], ui::LayerStack::fromValue(lsc.layer_stack()));
-}
-
-void Replayer::setHiddenFlag(SurfaceComposerClient::Transaction& t,
- layer_id id, const HiddenFlagChange& hfc) {
- ALOGV("Layer %d: Setting Hidden Flag -- hidden_flag=%d", id, hfc.hidden_flag());
- layer_id flag = hfc.hidden_flag() ? layer_state_t::eLayerHidden : 0;
-
- t.setFlags(mLayers[id], flag, layer_state_t::eLayerHidden);
-}
-
-void Replayer::setOpaqueFlag(SurfaceComposerClient::Transaction& t,
- layer_id id, const OpaqueFlagChange& ofc) {
- ALOGV("Layer %d: Setting Opaque Flag -- opaque_flag=%d", id, ofc.opaque_flag());
- layer_id flag = ofc.opaque_flag() ? layer_state_t::eLayerOpaque : 0;
-
- t.setFlags(mLayers[id], flag, layer_state_t::eLayerOpaque);
-}
-
-void Replayer::setSecureFlag(SurfaceComposerClient::Transaction& t,
- layer_id id, const SecureFlagChange& sfc) {
- ALOGV("Layer %d: Setting Secure Flag -- secure_flag=%d", id, sfc.secure_flag());
- layer_id flag = sfc.secure_flag() ? layer_state_t::eLayerSecure : 0;
-
- t.setFlags(mLayers[id], flag, layer_state_t::eLayerSecure);
-}
-
-void Replayer::setDisplaySurface(SurfaceComposerClient::Transaction& t,
- display_id id, const DispSurfaceChange& /*dsc*/) {
- sp<IGraphicBufferProducer> outProducer;
- sp<IGraphicBufferConsumer> outConsumer;
- BufferQueue::createBufferQueue(&outProducer, &outConsumer);
-
- t.setDisplaySurface(mDisplays[id], outProducer);
-}
-
-void Replayer::setDisplayLayerStack(SurfaceComposerClient::Transaction& t,
- display_id id, const LayerStackChange& lsc) {
- t.setDisplayLayerStack(mDisplays[id], ui::LayerStack::fromValue(lsc.layer_stack()));
-}
-
-void Replayer::setDisplaySize(SurfaceComposerClient::Transaction& t,
- display_id id, const SizeChange& sc) {
- t.setDisplaySize(mDisplays[id], sc.w(), sc.h());
-}
-
-void Replayer::setDisplayProjection(SurfaceComposerClient::Transaction& t,
- display_id id, const ProjectionChange& pc) {
- Rect viewport = Rect(pc.viewport().left(), pc.viewport().top(), pc.viewport().right(),
- pc.viewport().bottom());
- Rect frame = Rect(pc.frame().left(), pc.frame().top(), pc.frame().right(), pc.frame().bottom());
-
- t.setDisplayProjection(mDisplays[id], ui::toRotation(pc.orientation()), viewport, frame);
-}
-
-status_t Replayer::createSurfaceControl(
- const SurfaceCreation& create, const std::shared_ptr<Event>& event) {
- event->readyToExecute();
-
- ALOGV("Creating Surface Control: ID: %d", create.id());
- sp<SurfaceControl> surfaceControl = mComposerClient->createSurface(
- String8(create.name().c_str()), create.w(), create.h(), PIXEL_FORMAT_RGBA_8888, 0);
-
- if (surfaceControl == nullptr) {
- ALOGE("CreateSurfaceControl: unable to create surface control");
- return BAD_VALUE;
- }
-
- std::lock_guard<std::mutex> lock1(mLayerLock);
- auto& layer = mLayers[create.id()];
- layer = surfaceControl;
-
- mColors[create.id()] = HSV(rand() % 360, 1, 1);
-
- mLayerCond.notify_all();
-
- std::lock_guard<std::mutex> lock2(mBufferQueueSchedulerLock);
- if (mBufferQueueSchedulers.count(create.id()) != 0) {
- mBufferQueueSchedulers[create.id()]->setSurfaceControl(
- mLayers[create.id()], mColors[create.id()]);
- }
-
- return NO_ERROR;
-}
-
-status_t Replayer::injectVSyncEvent(
- const VSyncEvent& vSyncEvent, const std::shared_ptr<Event>& event) {
- ALOGV("Injecting VSync Event");
-
- event->readyToExecute();
-
- SurfaceComposerClient::injectVSync(vSyncEvent.when());
-
- return NO_ERROR;
-}
-
-void Replayer::createDisplay(const DisplayCreation& create, const std::shared_ptr<Event>& event) {
- ALOGV("Creating display");
- event->readyToExecute();
-
- std::lock_guard<std::mutex> lock(mDisplayLock);
- sp<IBinder> display = SurfaceComposerClient::createDisplay(
- String8(create.name().c_str()), create.is_secure());
- mDisplays[create.id()] = display;
-
- mDisplayCond.notify_all();
-
- ALOGV("Done creating display");
-}
-
-void Replayer::deleteDisplay(const DisplayDeletion& delete_, const std::shared_ptr<Event>& event) {
- ALOGV("Delete display");
- event->readyToExecute();
-
- std::lock_guard<std::mutex> lock(mDisplayLock);
- SurfaceComposerClient::destroyDisplay(mDisplays[delete_.id()]);
- mDisplays.erase(delete_.id());
-}
-
-void Replayer::updatePowerMode(const PowerModeUpdate& pmu, const std::shared_ptr<Event>& event) {
- ALOGV("Updating power mode");
- event->readyToExecute();
- SurfaceComposerClient::setDisplayPowerMode(mDisplays[pmu.id()], pmu.mode());
-}
-
-void Replayer::waitUntilTimestamp(int64_t timestamp) {
- ALOGV("Waiting for %lld nanoseconds...", static_cast<int64_t>(timestamp - mCurrentTime));
- std::this_thread::sleep_for(std::chrono::nanoseconds(timestamp - mCurrentTime));
-}
-
-status_t Replayer::loadSurfaceComposerClient() {
- mComposerClient = new SurfaceComposerClient;
- return mComposerClient->initCheck();
-}
-
-void Replayer::setReparentChange(SurfaceComposerClient::Transaction& t,
- layer_id id, const ReparentChange& c) {
- sp<SurfaceControl> newSurfaceControl = nullptr;
- if (mLayers.count(c.parent_id()) != 0 && mLayers[c.parent_id()] != nullptr) {
- newSurfaceControl = mLayers[c.parent_id()];
- }
- t.reparent(mLayers[id], newSurfaceControl);
-}
-
-void Replayer::setRelativeParentChange(SurfaceComposerClient::Transaction& t,
- layer_id id, const RelativeParentChange& c) {
- if (mLayers.count(c.relative_parent_id()) == 0 || mLayers[c.relative_parent_id()] == nullptr) {
- ALOGE("Layer %d not found in set relative parent transaction", c.relative_parent_id());
- return;
- }
- t.setRelativeLayer(mLayers[id], mLayers[c.relative_parent_id()], c.z());
-}
-
-void Replayer::setShadowRadiusChange(SurfaceComposerClient::Transaction& t,
- layer_id id, const ShadowRadiusChange& c) {
- t.setShadowRadius(mLayers[id], c.radius());
-}
-
-void Replayer::setBlurRegionsChange(SurfaceComposerClient::Transaction& t,
- layer_id id, const BlurRegionsChange& c) {
- std::vector<BlurRegion> regions;
- for(size_t i=0; i < c.blur_regions_size(); i++) {
- auto protoRegion = c.blur_regions(i);
- regions.push_back(BlurRegion{
- .blurRadius = protoRegion.blur_radius(),
- .alpha = protoRegion.alpha(),
- .cornerRadiusTL = protoRegion.corner_radius_tl(),
- .cornerRadiusTR = protoRegion.corner_radius_tr(),
- .cornerRadiusBL = protoRegion.corner_radius_bl(),
- .cornerRadiusBR = protoRegion.corner_radius_br(),
- .left = protoRegion.left(),
- .top = protoRegion.top(),
- .right = protoRegion.right(),
- .bottom = protoRegion.bottom()
- });
- }
- t.setBlurRegions(mLayers[id], regions);
-}
diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp
index 3743025..7aaaebb 100644
--- a/libs/gui/SurfaceComposerClient.cpp
+++ b/libs/gui/SurfaceComposerClient.cpp
@@ -89,6 +89,8 @@
void emptyCallback(nsecs_t, const sp<Fence>&, const std::vector<SurfaceControlStats>&) {}
} // namespace
+const std::string SurfaceComposerClient::kEmpty{};
+
ComposerService::ComposerService()
: Singleton<ComposerService>() {
Mutex::Autolock _l(mLock);
@@ -1278,14 +1280,13 @@
}
// ---------------------------------------------------------------------------
-sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure,
- float requestedRefereshRate) {
+sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool isSecure,
+ const std::string& uniqueId,
+ float requestedRefreshRate) {
sp<IBinder> display = nullptr;
- binder::Status status =
- ComposerServiceAIDL::getComposerService()->createDisplay(std::string(
- displayName.c_str()),
- secure, requestedRefereshRate,
- &display);
+ binder::Status status = ComposerServiceAIDL::getComposerService()
+ ->createDisplay(std::string(displayName.c_str()), isSecure,
+ uniqueId, requestedRefreshRate, &display);
return status.isOk() ? display : nullptr;
}
diff --git a/libs/gui/aidl/android/gui/ISurfaceComposer.aidl b/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
index a2549e7..c6e7197 100644
--- a/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
+++ b/libs/gui/aidl/android/gui/ISurfaceComposer.aidl
@@ -72,7 +72,7 @@
void bootFinished();
/**
- * Create a display event connection
+ * Create a display event connection.
*
* layerHandle
* Optional binder handle representing a Layer in SF to associate the new
@@ -89,12 +89,14 @@
@nullable ISurfaceComposerClient createConnection();
/**
- * Create a virtual display
+ * Create a virtual display.
*
* displayName
- * The name of the virtual display
- * secure
- * Whether this virtual display is secure
+ * The name of the virtual display.
+ * isSecure
+ * Whether this virtual display is secure.
+ * uniqueId
+ * The unique ID for the display.
* requestedRefreshRate
* The refresh rate, frames per second, to request on the virtual display.
* This is just a request, the actual rate may be adjusted to align well
@@ -103,11 +105,11 @@
*
* requires ACCESS_SURFACE_FLINGER permission.
*/
- @nullable IBinder createDisplay(@utf8InCpp String displayName, boolean secure,
- float requestedRefreshRate);
+ @nullable IBinder createDisplay(@utf8InCpp String displayName, boolean isSecure,
+ @utf8InCpp String uniqueId, float requestedRefreshRate);
/**
- * Destroy a virtual display
+ * Destroy a virtual display.
* requires ACCESS_SURFACE_FLINGER permission.
*/
void destroyDisplay(IBinder display);
diff --git a/libs/gui/include/gui/SurfaceComposerClient.h b/libs/gui/include/gui/SurfaceComposerClient.h
index 49b0a7d..987efe0 100644
--- a/libs/gui/include/gui/SurfaceComposerClient.h
+++ b/libs/gui/include/gui/SurfaceComposerClient.h
@@ -18,6 +18,7 @@
#include <stdint.h>
#include <sys/types.h>
+
#include <set>
#include <thread>
#include <unordered_map>
@@ -374,17 +375,15 @@
sp<SurfaceControl> mirrorDisplay(DisplayId displayId);
- //! Create a virtual display
- static sp<IBinder> createDisplay(const String8& displayName, bool secure,
- float requestedRefereshRate = 0);
+ static const std::string kEmpty;
+ static sp<IBinder> createDisplay(const String8& displayName, bool isSecure,
+ const std::string& uniqueId = kEmpty,
+ float requestedRefreshRate = 0);
- //! Destroy a virtual display
static void destroyDisplay(const sp<IBinder>& display);
- //! Get stable IDs for connected physical displays
static std::vector<PhysicalDisplayId> getPhysicalDisplayIds();
- //! Get token for a physical display given its stable ID
static sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId);
// Returns StalledTransactionInfo if a transaction from the provided pid has not been applied
diff --git a/libs/gui/tests/Surface_test.cpp b/libs/gui/tests/Surface_test.cpp
index f4b059c..eee4fb9 100644
--- a/libs/gui/tests/Surface_test.cpp
+++ b/libs/gui/tests/Surface_test.cpp
@@ -673,8 +673,8 @@
return binder::Status::ok();
}
- binder::Status createDisplay(const std::string& /*displayName*/, bool /*secure*/,
- float /*requestedRefreshRate*/,
+ binder::Status createDisplay(const std::string& /*displayName*/, bool /*isSecure*/,
+ const std::string& /*uniqueId*/, float /*requestedRefreshRate*/,
sp<IBinder>* /*outDisplay*/) override {
return binder::Status::ok();
}
diff --git a/services/surfaceflinger/DisplayDevice.h b/services/surfaceflinger/DisplayDevice.h
index fc5089b..c2d09c9 100644
--- a/services/surfaceflinger/DisplayDevice.h
+++ b/services/surfaceflinger/DisplayDevice.h
@@ -329,6 +329,7 @@
uint32_t width = 0;
uint32_t height = 0;
std::string displayName;
+ std::string uniqueId;
bool isSecure = false;
bool isProtected = false;
// Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 0d2e514..5181fb8 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -573,13 +573,13 @@
mScheduler->run();
}
-sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName, bool secure,
- float requestedRefreshRate) {
+sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName, bool isSecure,
+ const std::string& uniqueId, float requestedRefreshRate) {
// SurfaceComposerAIDL checks for some permissions, but adding an additional check here.
// This is to ensure that only root, system, and graphics can request to create a secure
// display. Secure displays can show secure content so we add an additional restriction on it.
- const int uid = IPCThreadState::self()->getCallingUid();
- if (secure && uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM) {
+ const uid_t uid = IPCThreadState::self()->getCallingUid();
+ if (isSecure && uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM) {
ALOGE("Only privileged processes can create a secure display");
return nullptr;
}
@@ -603,11 +603,12 @@
Mutex::Autolock _l(mStateLock);
// Display ID is assigned when virtual display is allocated by HWC.
DisplayDeviceState state;
- state.isSecure = secure;
+ state.isSecure = isSecure;
// Set display as protected when marked as secure to ensure no behavior change
// TODO (b/314820005): separate as a different arg when creating the display.
- state.isProtected = secure;
+ state.isProtected = isSecure;
state.displayName = displayName;
+ state.uniqueId = uniqueId;
state.requestedRefreshRate = Fps::fromValue(requestedRefreshRate);
mCurrentState.displays.add(token, state);
return token;
@@ -9552,7 +9553,8 @@
}
}
-binder::Status SurfaceComposerAIDL::createDisplay(const std::string& displayName, bool secure,
+binder::Status SurfaceComposerAIDL::createDisplay(const std::string& displayName, bool isSecure,
+ const std::string& uniqueId,
float requestedRefreshRate,
sp<IBinder>* outDisplay) {
status_t status = checkAccessPermission();
@@ -9560,7 +9562,7 @@
return binderStatusFromStatusT(status);
}
String8 displayName8 = String8::format("%s", displayName.c_str());
- *outDisplay = mFlinger->createDisplay(displayName8, secure, requestedRefreshRate);
+ *outDisplay = mFlinger->createDisplay(displayName8, isSecure, uniqueId, requestedRefreshRate);
return binder::Status::ok();
}
@@ -9577,10 +9579,10 @@
std::vector<PhysicalDisplayId> physicalDisplayIds = mFlinger->getPhysicalDisplayIds();
std::vector<int64_t> displayIds;
displayIds.reserve(physicalDisplayIds.size());
- for (auto item : physicalDisplayIds) {
- displayIds.push_back(static_cast<int64_t>(item.value));
+ for (const auto id : physicalDisplayIds) {
+ displayIds.push_back(static_cast<int64_t>(id.value));
}
- *outDisplayIds = displayIds;
+ *outDisplayIds = std::move(displayIds);
return binder::Status::ok();
}
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 4cb5aa3..8474515 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -539,15 +539,16 @@
static const size_t MAX_LAYERS = 4096;
- // Implements IBinder.
- status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
- status_t dump(int fd, const Vector<String16>& args) override { return priorityDump(fd, args); }
- bool callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache = true)
+ static bool callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache = true)
EXCLUDES(mStateLock);
- // Implements ISurfaceComposer
- sp<IBinder> createDisplay(const String8& displayName, bool secure,
- float requestedRefreshRate = 0.0f);
+ // IBinder overrides:
+ status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
+ status_t dump(int fd, const Vector<String16>& args) override { return priorityDump(fd, args); }
+
+ // ISurfaceComposer implementation:
+ sp<IBinder> createDisplay(const String8& displayName, bool isSecure,
+ const std::string& uniqueId, float requestedRefreshRate = 0.0f);
void destroyDisplay(const sp<IBinder>& displayToken);
std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const EXCLUDES(mStateLock) {
Mutex::Autolock lock(mStateLock);
@@ -667,7 +668,7 @@
void updateHdcpLevels(hal::HWDisplayId hwcDisplayId, int32_t connectedLevel, int32_t maxLevel);
- // Implements IBinder::DeathRecipient.
+ // IBinder::DeathRecipient overrides:
void binderDied(const wp<IBinder>& who) override;
// HWC2::ComposerCallback overrides:
@@ -1559,7 +1560,7 @@
class SurfaceComposerAIDL : public gui::BnSurfaceComposer {
public:
- SurfaceComposerAIDL(sp<SurfaceFlinger> sf) : mFlinger(std::move(sf)) {}
+ explicit SurfaceComposerAIDL(sp<SurfaceFlinger> sf) : mFlinger(std::move(sf)) {}
binder::Status bootFinished() override;
binder::Status createDisplayEventConnection(
@@ -1567,8 +1568,9 @@
const sp<IBinder>& layerHandle,
sp<gui::IDisplayEventConnection>* outConnection) override;
binder::Status createConnection(sp<gui::ISurfaceComposerClient>* outClient) override;
- binder::Status createDisplay(const std::string& displayName, bool secure,
- float requestedRefreshRate, sp<IBinder>* outDisplay) override;
+ binder::Status createDisplay(const std::string& displayName, bool isSecure,
+ const std::string& uniqueId, float requestedRefreshRate,
+ sp<IBinder>* outDisplay) override;
binder::Status destroyDisplay(const sp<IBinder>& display) override;
binder::Status getPhysicalDisplayIds(std::vector<int64_t>* outDisplayIds) override;
binder::Status getPhysicalDisplayToken(int64_t displayId, sp<IBinder>* outDisplay) override;
@@ -1690,7 +1692,7 @@
gui::DynamicDisplayInfo*& outInfo);
private:
- sp<SurfaceFlinger> mFlinger;
+ const sp<SurfaceFlinger> mFlinger;
};
} // namespace android
diff --git a/services/surfaceflinger/tests/unittests/SurfaceFlinger_CreateDisplayTest.cpp b/services/surfaceflinger/tests/unittests/SurfaceFlinger_CreateDisplayTest.cpp
index 28162f4..bf5ae21 100644
--- a/services/surfaceflinger/tests/unittests/SurfaceFlinger_CreateDisplayTest.cpp
+++ b/services/surfaceflinger/tests/unittests/SurfaceFlinger_CreateDisplayTest.cpp
@@ -132,6 +132,36 @@
EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
}
+TEST_F(CreateDisplayTest, createDisplaySetsCurrentStateForUniqueId) {
+ const String8 name("virtual.test");
+ const std::string uniqueId = "virtual:package:id";
+
+ // --------------------------------------------------------------------
+ // Call Expectations
+
+ // --------------------------------------------------------------------
+ // Invocation
+
+ sp<IBinder> displayToken = mFlinger.createDisplay(name, false, uniqueId);
+
+ // --------------------------------------------------------------------
+ // Postconditions
+
+ // The display should have been added to the current state
+ ASSERT_TRUE(hasCurrentDisplayState(displayToken));
+ const auto& display = getCurrentDisplayState(displayToken);
+ EXPECT_TRUE(display.isVirtual());
+ EXPECT_FALSE(display.isSecure);
+ EXPECT_EQ(display.uniqueId, "virtual:package:id");
+ EXPECT_EQ(name.c_str(), display.displayName);
+
+ // --------------------------------------------------------------------
+ // Cleanup conditions
+
+ // Creating the display commits a display transaction.
+ EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
+}
+
// Requesting 0 tells SF not to do anything, i.e., default to refresh as physical displays
TEST_F(CreateDisplayTest, createDisplayWithRequestedRefreshRate0) {
const String8 displayName("virtual.test");
diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
index a0c1372..b251e2c 100644
--- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
+++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
@@ -420,8 +420,15 @@
commit(kComposite);
}
- auto createDisplay(const String8& displayName, bool secure, float requestedRefreshRate = 0.0f) {
- return mFlinger->createDisplay(displayName, secure, requestedRefreshRate);
+ auto createDisplay(const String8& displayName, bool isSecure,
+ float requestedRefreshRate = 0.0f) {
+ const std::string testId = "virtual:libsurfaceflinger_unittest:TestableSurfaceFlinger";
+ return mFlinger->createDisplay(displayName, isSecure, testId, requestedRefreshRate);
+ }
+
+ auto createDisplay(const String8& displayName, bool isSecure, const std::string& uniqueId,
+ float requestedRefreshRate = 0.0f) {
+ return mFlinger->createDisplay(displayName, isSecure, uniqueId, requestedRefreshRate);
}
auto destroyDisplay(const sp<IBinder>& displayToken) {