Remove unnecessary code from DrmCompositor.
Drops ~100 LoC.
Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
}
std::string DrmHwcTwo::HwcDisplay::Dump() {
+ std::string flattening_state_str;
+ switch (flattenning_state_) {
+ case ClientFlattenningState::Disabled:
+ flattening_state_str = "Disabled";
+ break;
+ case ClientFlattenningState::NotRequired:
+ flattening_state_str = "Not needed";
+ break;
+ case ClientFlattenningState::Flattened:
+ flattening_state_str = "Active";
+ break;
+ case ClientFlattenningState::ClientRefreshRequested:
+ flattening_state_str = "Refresh requested";
+ break;
+ default:
+ flattening_state_str = std::to_string(flattenning_state_) +
+ " VSync remains";
+ }
+
std::stringstream ss;
ss << "- Display on: " << connector_->name() << "\n"
- << " Flattening state: " << compositor_.GetFlatteningState() << "\n"
+ << " Flattening state: " << flattening_state_str << "\n"
<< "Statistics since system boot:\n"
<< DumpDelta(total_stats_) << "\n\n"
<< "Statistics since last dumpsys request:\n"
}
int display = static_cast<int>(handle_);
- int ret = compositor_.Init(resource_manager_, display, [this] {
- /* refresh callback */
- const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
- if (hwc2_->refresh_callback_.first != nullptr &&
- hwc2_->refresh_callback_.second != nullptr) {
- hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
- }
- });
+ int ret = compositor_.Init(resource_manager_, display);
if (ret) {
ALOGE("Failed display compositor init for display %d (%d)", display, ret);
return HWC2::Error::NoResources;
return HWC2::Error::BadDisplay;
}
+ ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
+ const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
+ /* Frontend flattening */
+ if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
+ --flattenning_state_ ==
+ ClientFlattenningState::ClientRefreshRequested &&
+ hwc2_->refresh_callback_.first != nullptr &&
+ hwc2_->refresh_callback_.second != nullptr) {
+ hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
+ flattening_vsync_worker_.VSyncControl(false);
+ }
+ });
+ if (ret) {
+ ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
+ return HWC2::Error::BadDisplay;
+ }
+
ret = BackendManager::GetInstance().SetBackendForDisplay(this);
if (ret) {
ALOGE("Failed to set backend for d=%d %d\n", display, ret);
return total_stats_;
}
+ /* returns true if composition should be sent to client */
+ bool ProcessClientFlatteningState(bool skip) {
+ int flattenning_state = flattenning_state_;
+ if (flattenning_state == ClientFlattenningState::Disabled) {
+ return false;
+ }
+
+ if (skip) {
+ flattenning_state_ = ClientFlattenningState::NotRequired;
+ return false;
+ }
+
+ if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
+ flattenning_state_ = ClientFlattenningState::Flattened;
+ return true;
+ }
+
+ flattening_vsync_worker_.VSyncControl(true);
+ flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
+ return false;
+ }
+
private:
+ enum ClientFlattenningState : int32_t {
+ Disabled = -3,
+ NotRequired = -2,
+ Flattened = -1,
+ ClientRefreshRequested = 0,
+ VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
+ };
+
+ std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
+ VSyncWorker flattening_vsync_worker_;
+
void AddFenceToPresentFence(UniqueFd fd);
constexpr static size_t MATRIX_SIZE = 16;
int client_start = -1;
size_t client_size = 0;
- if (display->compositor().ShouldFlattenOnClient()) {
+ if (display->ProcessClientFlatteningState(layers.size() <= 1)) {
+ display->total_stats().frames_flattened_++;
client_start = 0;
client_size = layers.size();
MarkValidated(layers, client_start, client_size);
*num_types = client_size;
- display->total_stats().frames_flattened_ = display->compositor()
- .GetFlattenedFramesCount();
display->total_stats().gpu_pixops_ += CalcPixOps(layers, client_start,
client_size);
display->total_stats().total_pixops_ += CalcPixOps(layers, 0, layers.size());
namespace android {
-std::ostream &operator<<(std::ostream &str, FlatteningState state) {
- std::array<const char *, 6> flattenting_state_str = {
- "None", "Not needed", "SF Requested", "Squashed by GPU",
- "Serial", "Concurrent",
- };
-
- return str << flattenting_state_str[static_cast<int>(state)];
-}
-
DrmDisplayCompositor::DrmDisplayCompositor()
: resource_manager_(nullptr),
display_(-1),
active_(false),
use_hw_overlays_(true),
dump_frames_composited_(0),
- dump_last_timestamp_ns_(0),
- flatten_countdown_(FLATTEN_COUNTDOWN_INIT),
- flattening_state_(FlatteningState::kNone),
- frames_flattened_(0) {
+ dump_last_timestamp_ns_(0) {
struct timespec ts {};
if (clock_gettime(CLOCK_MONOTONIC, &ts))
return;
DrmDisplayCompositor::~DrmDisplayCompositor() {
if (!initialized_)
return;
-
- vsync_worker_.Exit();
- int ret = pthread_mutex_lock(&lock_);
- if (ret)
- ALOGE("Failed to acquire compositor lock %d", ret);
DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
if (mode_.blob_id)
drm->DestroyPropertyBlob(mode_.blob_id);
drm->DestroyPropertyBlob(mode_.old_blob_id);
active_composition_.reset();
-
- ret = pthread_mutex_unlock(&lock_);
- if (ret)
- ALOGE("Failed to acquire compositor lock %d", ret);
-
- pthread_mutex_destroy(&lock_);
}
-auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display,
- std::function<void()> client_refresh_callback)
+auto DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display)
-> int {
- client_refresh_callback_ = std::move(client_refresh_callback);
resource_manager_ = resource_manager;
display_ = display;
DrmDevice *drm = resource_manager_->GetDrmDevice(display);
ALOGE("Could not find drmdevice for display");
return -EINVAL;
}
- int ret = pthread_mutex_init(&lock_, nullptr);
- if (ret) {
- ALOGE("Failed to initialize drm compositor lock %d\n", ret);
- return ret;
- }
planner_ = Planner::CreateInstance(drm);
- vsync_worker_.Init(drm, display_, [this](int64_t timestamp) {
- AutoLock lock(&lock_, "DrmDisplayCompositor::Init()");
- if (lock.Lock())
- return;
- flatten_countdown_--;
- if (!CountdownExpired())
- return;
- lock.Unlock();
- int ret = FlattenActiveComposition();
- ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64
- " result = %d \n",
- display_, timestamp, ret);
- });
-
initialized_ = true;
return 0;
}
return std::make_unique<DrmDisplayComposition>(crtc, planner_.get());
}
-FlatteningState DrmDisplayCompositor::GetFlatteningState() const {
- return flattening_state_;
-}
-
-uint32_t DrmDisplayCompositor::GetFlattenedFramesCount() const {
- return frames_flattened_;
-}
-
-bool DrmDisplayCompositor::ShouldFlattenOnClient() const {
- return flattening_state_ == FlatteningState::kClientRequested ||
- flattening_state_ == FlatteningState::kClientDone;
-}
-
std::tuple<uint32_t, uint32_t, int>
DrmDisplayCompositor::GetActiveModeResolution() {
DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
void DrmDisplayCompositor::ApplyFrame(
std::unique_ptr<DrmDisplayComposition> composition, int status) {
- AutoLock lock(&lock_, __func__);
- if (lock.Lock())
- return;
int ret = status;
if (!ret) {
++dump_frames_composited_;
active_composition_.swap(composition);
-
- flatten_countdown_ = FLATTEN_COUNTDOWN_INIT;
- if (flattening_state_ != FlatteningState::kClientRequested) {
- SetFlattening(FlatteningState::kNone);
- } else {
- SetFlattening(FlatteningState::kClientDone);
- }
- vsync_worker_.VSyncControl(true);
}
int DrmDisplayCompositor::ApplyComposition(
return CommitFrame(composition, true);
}
-void DrmDisplayCompositor::SetFlattening(FlatteningState new_state) {
- if (flattening_state_ != new_state) {
- switch (flattening_state_) {
- case FlatteningState::kClientDone:
- ++frames_flattened_;
- break;
- case FlatteningState::kClientRequested:
- case FlatteningState::kNone:
- case FlatteningState::kNotNeeded:
- break;
- }
- }
- flattening_state_ = new_state;
-}
-
-bool DrmDisplayCompositor::IsFlatteningNeeded() const {
- return CountdownExpired() && active_composition_->layers().size() >= 2;
-}
-
-int DrmDisplayCompositor::FlattenOnClient() {
- if (client_refresh_callback_) {
- {
- AutoLock lock(&lock_, __func__);
- if (!IsFlatteningNeeded()) {
- if (flattening_state_ != FlatteningState::kClientDone) {
- ALOGV("Flattening is not needed");
- SetFlattening(FlatteningState::kNotNeeded);
- }
- return -EALREADY;
- }
- }
-
- ALOGV(
- "No writeback connector available, "
- "falling back to client composition");
- SetFlattening(FlatteningState::kClientRequested);
- client_refresh_callback_();
- return 0;
- }
-
- ALOGV("No writeback connector available");
- return -EINVAL;
-}
-
-int DrmDisplayCompositor::FlattenActiveComposition() {
- return FlattenOnClient();
-}
-
-bool DrmDisplayCompositor::CountdownExpired() const {
- return flatten_countdown_ <= 0;
-}
-
void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
- int ret = pthread_mutex_lock(&lock_);
- if (ret)
- return;
-
uint64_t num_frames = dump_frames_composited_;
dump_frames_composited_ = 0;
struct timespec ts {};
- ret = clock_gettime(CLOCK_MONOTONIC, &ts);
+ int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
if (ret) {
- pthread_mutex_unlock(&lock_);
return;
}
<< " fps=" << fps << "\n";
dump_last_timestamp_ns_ = cur_ts;
-
- pthread_mutex_unlock(&lock_);
}
} // namespace android
#include "drm/VSyncWorker.h"
#include "drmhwcomposer.h"
-// If a scene is still for this number of vblanks flatten it to reduce power
-// consumption.
-#define FLATTEN_COUNTDOWN_INIT 60
-
namespace android {
-enum class FlatteningState {
- kNone,
- kNotNeeded,
- kClientRequested,
- kClientDone,
-};
-
-std::ostream &operator<<(std::ostream &str, FlatteningState state);
-
class DrmDisplayCompositor {
public:
DrmDisplayCompositor();
~DrmDisplayCompositor();
- auto Init(ResourceManager *resource_manager, int display,
- std::function<void()> client_refresh_callback) -> int;
+ auto Init(ResourceManager *resource_manager, int display) -> int;
std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
return std::move(active_composition_->out_fence_);
}
- FlatteningState GetFlatteningState() const;
- uint32_t GetFlattenedFramesCount() const;
- bool ShouldFlattenOnClient() const;
-
std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
private:
- std::function<void()> client_refresh_callback_;
struct ModeState {
bool needs_modeset = false;
DrmMode mode;
void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
int status);
- void SetFlattening(FlatteningState new_state);
- bool IsFlatteningNeeded() const;
- int FlattenActiveComposition();
- int FlattenOnClient();
-
- bool CountdownExpired() const;
-
std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
ResourceManager *resource_manager_;
ModeState mode_;
- // mutable since we need to acquire in Dump()
- mutable pthread_mutex_t lock_{};
-
// State tracking progress since our last Dump(). These are mutable since
// we need to reset them on every Dump() call.
mutable uint64_t dump_frames_composited_;
mutable uint64_t dump_last_timestamp_ns_;
- VSyncWorker vsync_worker_;
- int64_t flatten_countdown_;
std::unique_ptr<Planner> planner_;
-
- FlatteningState flattening_state_;
- uint32_t frames_flattened_;
-
- std::function<void(int)> refresh_display_cb_;
};
} // namespace android