diff options
| -rw-r--r-- | compositor/HdcpController.cpp | 48 | ||||
| -rw-r--r-- | compositor/HdcpController.h | 31 | ||||
| -rw-r--r-- | compositor/HdcpControllerTest.cpp | 81 | ||||
| -rw-r--r-- | drm/AtomicStateManager.h | 3 | ||||
| -rw-r--r-- | drm/DrmAtomicCommitSink.cpp | 17 | ||||
| -rw-r--r-- | drm/DrmAtomicStateManager.cpp | 30 | ||||
| -rw-r--r-- | drm/DrmAtomicStateManager.h | 1 | ||||
| -rw-r--r-- | drm/DrmAtomicStateManagerTest.cpp | 171 | ||||
| -rw-r--r-- | hwc/HwcDisplay.cpp | 103 | ||||
| -rw-r--r-- | hwc/HwcDisplay.h | 3 | ||||
| -rw-r--r-- | hwc/HwcDisplayTest.cpp | 360 |
11 files changed, 758 insertions, 90 deletions
diff --git a/compositor/HdcpController.cpp b/compositor/HdcpController.cpp index c2e20ab..cc89ba6 100644 --- a/compositor/HdcpController.cpp +++ b/compositor/HdcpController.cpp @@ -53,7 +53,7 @@ HdcpController::~HdcpController() { } // Set the HDCP state to desired if not already requested -void HdcpController::Start() { +void HdcpController::Start(HdcpContentType initial_type) { bool changed = false; bool should_notify = false; std::optional<HdcpContentType> notify_type; @@ -68,13 +68,21 @@ void HdcpController::Start() { should_notify = true; notify_type = std::nullopt; break; + case HdcpState::kRequested: case HdcpState::kRetry: + case HdcpState::kThreadExit: break; - default: - ALOGI("HDCP requested to start"); - hdcp_state_ = HdcpState::kDesired; - enabled_content_type_ = std::nullopt; - changed = true; + case HdcpState::kUndesired: + case HdcpState::kDesired: + if (hdcp_state_ != HdcpState::kDesired || + target_content_type_ != initial_type) { + ALOGI("HDCP requested to start with Content Type %d", + static_cast<int>(initial_type)); + hdcp_state_ = HdcpState::kDesired; + target_content_type_ = initial_type; + enabled_content_type_ = std::nullopt; + changed = true; + } break; } } @@ -99,7 +107,6 @@ void HdcpController::Requested() { } ++request_generation_; - was_retry_ = (hdcp_state_ == HdcpState::kRetry); hdcp_state_ = HdcpState::kRequested; sleep_until_ = std::chrono::system_clock::now() + timeout_; @@ -134,21 +141,18 @@ void HdcpController::SetContentProtectionStatus(uint64_t expected_generation, if (cp_enabled) { hdcp_state_ = HdcpState::kEnabled; should_notify_hdcp_status = true; - if (was_retry_) { - was_retry_ = false; - notify_type = HdcpContentType::kType0; - } else { - notify_type = HdcpContentType::kType1; - } + notify_type = target_content_type_; enabled_content_type_ = notify_type; } else { - if (was_retry_) { - was_retry_ = false; + if (target_content_type_ == HdcpContentType::kType0) { hdcp_state_ = HdcpState::kInfeasible; notify_type = std::nullopt; - ALOGI("HDCP negotiation failed after retry. Marking as infeasible."); + ALOGI( + "HDCP negotiation failed for Content Type 0. Marking as " + "infeasible."); should_notify_hdcp_status = true; } else { + target_content_type_ = HdcpContentType::kType0; hdcp_state_ = HdcpState::kRetry; trigger_retry = true; } @@ -170,6 +174,14 @@ HdcpController::HdcpState HdcpController::GetHdcpState() const { return hdcp_state_; } +std::optional<HdcpContentType> HdcpController::GetPendingContentType() const { + auto lock = std::lock_guard<std::mutex>(mutex_); + if (hdcp_state_ == HdcpState::kDesired || hdcp_state_ == HdcpState::kRetry) { + return target_content_type_; + } + return std::nullopt; +} + bool HdcpController::GetSettledNotification( std::optional<HdcpContentType>* out_level) const { std::lock_guard<std::mutex> lock(mutex_); @@ -192,7 +204,7 @@ void HdcpController::Terminate() { } ++request_generation_; hdcp_state_ = HdcpState::kUndesired; - was_retry_ = false; + target_content_type_ = HdcpContentType::kType1; enabled_content_type_ = std::nullopt; } cbks_.notify_hdcp_status(std::nullopt); @@ -211,7 +223,7 @@ void HdcpController::Reset() { } ++request_generation_; hdcp_state_ = HdcpState::kUndesired; - was_retry_ = false; + target_content_type_ = HdcpContentType::kType1; enabled_content_type_ = std::nullopt; } cv_.notify_all(); diff --git a/compositor/HdcpController.h b/compositor/HdcpController.h index 1ba8b6e..823c91e 100644 --- a/compositor/HdcpController.h +++ b/compositor/HdcpController.h @@ -57,12 +57,14 @@ class HdcpController { kThreadExit }; - // Initiates the HDCP enablement process. + // Initiates the HDCP enablement process with |initial_type|. // - // Sets the internal state to kDesired if it's not currently in a retry state. - // This signals the display pipeline to include HDCP properties in the next - // commit. - void Start(); + // Sets the internal state to kDesired if it's not currently enabled, + // infeasible, requested, or retrying. This signals the display pipeline to + // include HDCP properties with |initial_type| in the next commit. When + // |initial_type| is kType0, timeout transitions directly to kInfeasible + // without a retry frame. + void Start(HdcpContentType initial_type = HdcpContentType::kType1); // Signals that an HDCP enablement request has been sent to the kernel. // @@ -77,6 +79,10 @@ class HdcpController { // Returns the current state of the HDCP state machine. HdcpState GetHdcpState() const; + // Returns the HDCP content type to attach to the next atomic commit when + // negotiation is pending (kDesired or kRetry), or std::nullopt otherwise. + std::optional<HdcpContentType> GetPendingContentType() const; + // Returns true and populates |out_level| when the controller has settled in // kEnabled or kInfeasible so a newly attached client can be notified. bool GetSettledNotification(std::optional<HdcpContentType>* out_level) const; @@ -102,13 +108,13 @@ class HdcpController { // enabled content protection for |expected_generation|. // // State transitions: - // - Success: Transitions to kEnabled. Notifies client with kType1 (or kType0 - // if retrying). + // - Success: Transitions to kEnabled. Notifies client with + // target_content_type_. // - Failure (when !only_if_enabled): - // - If first attempt: Transitions to kRetry and triggers a retry frame - // (fallback to Type 0). - // - If retry attempt: Transitions to kInfeasible and notifies client of - // failure. + // - If targeting kType1: Transitions to kRetry with target_content_type_ = + // kType0 and triggers a retry frame. + // - If targeting kType0: Transitions directly to kInfeasible and notifies + // client of failure without retrying. void SetContentProtectionStatus(uint64_t expected_generation, bool only_if_enabled) EXCLUDES(mutex_); @@ -125,7 +131,8 @@ class HdcpController { HdcpState hdcp_state_ GUARDED_BY(mutex_) = HdcpState::kUndesired; uint64_t request_generation_ GUARDED_BY(mutex_) = 0; - bool was_retry_ GUARDED_BY(mutex_) = false; + HdcpContentType target_content_type_ GUARDED_BY(mutex_) = + HdcpContentType::kType1; std::optional<HdcpContentType> enabled_content_type_ GUARDED_BY(mutex_) = std::nullopt; diff --git a/compositor/HdcpControllerTest.cpp b/compositor/HdcpControllerTest.cpp index 93f061a..8c05657 100644 --- a/compositor/HdcpControllerTest.cpp +++ b/compositor/HdcpControllerTest.cpp @@ -155,15 +155,52 @@ TEST_F(HdcpControllerTest, Type1NegotiationSuccess) { CreateController(); controller_->Start(); EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kDesired); + EXPECT_EQ(controller_->GetPendingContentType(), HdcpContentType::kType1); connector_->SetContentProtectionEnabled(true); controller_->Requested(); + EXPECT_EQ(controller_->GetPendingContentType(), std::nullopt); auto type = WaitForNotification(); EXPECT_EQ(type, HdcpContentType::kType1); EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kEnabled); } +TEST_F(HdcpControllerTest, DirectType0NegotiationSuccess) { + CreateController(); + controller_->Start(HdcpContentType::kType0); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kDesired); + EXPECT_EQ(controller_->GetPendingContentType(), HdcpContentType::kType0); + + connector_->SetContentProtectionEnabled(true); + controller_->Requested(); + EXPECT_EQ(controller_->GetPendingContentType(), std::nullopt); + + auto type = WaitForNotification(); + EXPECT_EQ(type, HdcpContentType::kType0); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kEnabled); +} + +TEST_F(HdcpControllerTest, DirectType0FailureMarksInfeasibleWithoutRetry) { + CreateController(); + controller_->Start(HdcpContentType::kType0); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kDesired); + EXPECT_EQ(controller_->GetPendingContentType(), HdcpContentType::kType0); + + connector_->SetContentProtectionEnabled(false); + controller_->Requested(); + + auto type = WaitForNotification(); + EXPECT_EQ(type, std::nullopt); + EXPECT_EQ(controller_->GetHdcpState(), + HdcpController::HdcpState::kInfeasible); + EXPECT_EQ(controller_->GetPendingContentType(), std::nullopt); + { + auto cbk_lock = LockCallbacks(); + EXPECT_FALSE(retry_frame_triggered_); + } +} + TEST_F(HdcpControllerTest, Type0RetryFallback) { CreateController(); controller_->Start(); @@ -182,6 +219,50 @@ TEST_F(HdcpControllerTest, Type0RetryFallback) { EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kEnabled); } +TEST_F(HdcpControllerTest, StartDuringRequestedPreservesInFlightNegotiation) { + constexpr int kLongTimeoutMs = 5000; + CreateController(std::chrono::milliseconds(kLongTimeoutMs)); + + // Duplicate Start() while Type 1 authentication is in flight must not reset + // kRequested back to kDesired or overwrite target_content_type_. + controller_->Start(HdcpContentType::kType1); + connector_->SetContentProtectionEnabled(false); + controller_->Requested(); + ASSERT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kRequested); + + controller_->Start(HdcpContentType::kType0); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kRequested); + EXPECT_EQ(controller_->GetPendingContentType(), std::nullopt); + + connector_->SetContentProtectionEnabled(true); + EXPECT_EQ(WaitForNotification(), HdcpContentType::kType1); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kEnabled); + + // Also verify duplicate Start(kType1) during a Type 0 fallback's kRequested + // window does not clobber target_content_type_ back to kType1. + controller_->Terminate(); + ClearNotifications(); + connector_->SetContentProtectionEnabled(false); + + controller_->Start(HdcpContentType::kType1); + controller_->Requested(); + controller_->ExpireRequestTimeoutForTesting(); + ASSERT_TRUE(WaitForRetryFrame()); + ASSERT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kRetry); + ASSERT_EQ(controller_->GetPendingContentType(), HdcpContentType::kType0); + + controller_->Requested(); + ASSERT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kRequested); + + controller_->Start(HdcpContentType::kType1); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kRequested); + EXPECT_EQ(controller_->GetPendingContentType(), std::nullopt); + + connector_->SetContentProtectionEnabled(true); + EXPECT_EQ(WaitForNotification(), HdcpContentType::kType0); + EXPECT_EQ(controller_->GetHdcpState(), HdcpController::HdcpState::kEnabled); +} + TEST_F(HdcpControllerTest, InfeasibleTerminationAfterRetryFailure) { CreateController(); controller_->Start(); diff --git a/drm/AtomicStateManager.h b/drm/AtomicStateManager.h index c733f75..a114aa6 100644 --- a/drm/AtomicStateManager.h +++ b/drm/AtomicStateManager.h @@ -70,7 +70,8 @@ struct AtomicCommitArgs { /* helpers */ auto HasInputs() const -> bool { - return display_mode || power_mode || composition; + return display_mode || power_mode || composition || + (content_protection.has_value() && hdcp_content_type.has_value()); } }; diff --git a/drm/DrmAtomicCommitSink.cpp b/drm/DrmAtomicCommitSink.cpp index 7cf61d0..2a07fed 100644 --- a/drm/DrmAtomicCommitSink.cpp +++ b/drm/DrmAtomicCommitSink.cpp @@ -101,12 +101,12 @@ CommitStatus DrmAtomicCommitSink::CommitFrame( return CommitStatus::InternalFailure(); } - err = drmModeAtomicMerge( - pset.get(), - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) - (static_cast<DrmAtomicStateManager::DrmAtomicRequest *>(request.get())) - ->property_set.get()); - nonblock &= !arg.blocking && !arg.power_mode; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto *drm_request = static_cast<DrmAtomicStateManager::DrmAtomicRequest *>( + request.get()); + err = drmModeAtomicMerge(pset.get(), drm_request->property_set.get()); + nonblock &= !arg.blocking && !arg.power_mode && + !drm_request->hdcp_only_request; if (err != 0) { ALOGE("Failed to append request"); return CommitStatus::InternalFailure(); @@ -212,8 +212,9 @@ void DrmAtomicCommitSink::CleanUpFailedRequest( for (const auto &[atomic_state_manager, arg] : args) { // Only disable planes when an ordinary pageflip composition commit fails on // an already-active display. - if (!atomic_state_manager->IsActive() || arg.display_mode.has_value() || - arg.power_mode.has_value() || arg.teardown) { + if (!atomic_state_manager->IsActive() || !arg.composition || + arg.display_mode.has_value() || arg.power_mode.has_value() || + arg.teardown) { continue; } AtomicCommitArgs cl_arg{}; diff --git a/drm/DrmAtomicStateManager.cpp b/drm/DrmAtomicStateManager.cpp index 57d0ca4..e6a4b00 100644 --- a/drm/DrmAtomicStateManager.cpp +++ b/drm/DrmAtomicStateManager.cpp @@ -846,6 +846,29 @@ std::unique_ptr<AtomicRequest> DrmAtomicStateManager::GetAtomicModeReqForArgs( // helpers. atomic_request->new_frame_state = committed_frame_state_; + const bool hdcp_only_request = !args.display_mode && !args.power_mode && + !args.composition && !args.teardown && + !args.writeback && + args.content_protection.has_value() && + args.hdcp_content_type.has_value(); + if (hdcp_only_request) { + auto *connector = pipe_->connector->Get(); + if (connector == nullptr || connector->IsDeleted() || + !connector->GetContentProtectionProperty() || + !connector->GetHdcpContentTypeProperty()) { + ALOGE( + "Cannot create HDCP-only request without active connector HDCP " + "properties"); + return nullptr; + } + if (!SetContentProtectionIfNeeded(args, *atomic_request)) { + ALOGE("Failed to set Content Protection and HDCP Content Type"); + return nullptr; + } + atomic_request->hdcp_only_request = true; + return atomic_request; + } + if (!SetWriteBackFenceIfNeeded(args, *atomic_request)) { ALOGE("Failed to set writeback fence"); return nullptr; @@ -944,6 +967,13 @@ AtomicCommitResult DrmAtomicStateManager::FinishPendingRequest( atomic_request.get()); AtomicCommitResult result; + if (request->hdcp_only_request) { + for (const auto &[prop, value] : request->property_updates) { + prop->UpdateValue(value); + } + return result; + } + result.present_fence = MakeSharedFd(request->out_fence_address); committed_frame_state_ = std::move(request->new_frame_state); for (const auto &[prop, value] : request->property_updates) { diff --git a/drm/DrmAtomicStateManager.h b/drm/DrmAtomicStateManager.h index 62ccdee..ee2900c 100644 --- a/drm/DrmAtomicStateManager.h +++ b/drm/DrmAtomicStateManager.h @@ -108,6 +108,7 @@ class DrmAtomicStateManager : public AtomicStateManager { KmsObjects used_kms_objects; KmsState new_frame_state; std::vector<std::pair<DrmProperty *, uint64_t>> property_updates; + bool hdcp_only_request = false; void RecordPropertyUpdate(DrmProperty &prop, uint64_t value) { property_updates.emplace_back(&prop, value); diff --git a/drm/DrmAtomicStateManagerTest.cpp b/drm/DrmAtomicStateManagerTest.cpp index 9ff70d3..0cd829d 100644 --- a/drm/DrmAtomicStateManagerTest.cpp +++ b/drm/DrmAtomicStateManagerTest.cpp @@ -1837,4 +1837,175 @@ TEST_F(DrmAtomicStateManagerTest, .has_value()); } +TEST_F(DrmAtomicStateManagerTest, + HdcpOnlyArgsSetsOnlyHdcpConnectorPropertiesAndTestsWithAllowModeset) { + ActivateWithConnectorProps(*this); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_crtc = static_cast<FakeDrmCrtc*>(pipeline->crtc->Get()); + ASSERT_NE(fake_crtc, nullptr); + constexpr uint64_t kLutSize = 256; + fake_crtc->SetCtmPropertySupported(true); + fake_crtc->SetGammaLutProperties(kLutSize, kFakeBlobBaseId); + + AtomicCommitArgs hdcp_args{}; + hdcp_args.content_protection = ContentProtection::kDesired; + hdcp_args.hdcp_content_type = HdcpContentType::kType1; + EXPECT_TRUE(hdcp_args.HasInputs()); + + auto req = state_manager->GetAtomicModeReqForArgs(hdcp_args); + ASSERT_NE(req, nullptr); + EXPECT_FALSE(hdcp_args.power_mode.has_value()); + + const uint32_t conn_id = fake_connector->GetId(); + auto* prop_set = AsDrmRequest(req)->property_set.get(); + const auto items = GetAtomicReqItems(prop_set); + constexpr size_t kExpectedHdcpPropCount = 2; + EXPECT_EQ(items.size(), kExpectedHdcpPropCount); + EXPECT_EQ(GetAtomicReqPropertyValue(prop_set, conn_id, + kFakeContentProtectionPropId), + static_cast<uint64_t>(ContentProtection::kDesired)); + EXPECT_EQ(GetAtomicReqPropertyValue(prop_set, conn_id, + kFakeHdcpContentTypePropId), + static_cast<uint64_t>(HdcpContentType::kType1)); + + uint32_t observed_flags = 0; + FakeKernelDrmAtomicCommitSink sink_impl( + [&observed_flags](int /*fd*/, drmModeAtomicReqPtr /*req*/, uint32_t flags, + void* /*user_data*/, size_t /*attempt*/) -> int { + observed_flags = flags; + return -EINVAL; + }); + AtomicCommitSink& sink = sink_impl; + const CommitStatus status = sink.TestAtomicCommit( + {{state_manager.get(), hdcp_args}}); + EXPECT_EQ(sink_impl.GetTotalCalls(), 1U); + EXPECT_EQ(observed_flags, + static_cast<uint32_t>(DRM_MODE_ATOMIC_ALLOW_MODESET | + DRM_MODE_ATOMIC_TEST_ONLY)); + EXPECT_FALSE(status.success); + EXPECT_EQ(status.error_code, -EINVAL); +} + +TEST_F(DrmAtomicStateManagerTest, + HdcpOnlyLiveCommitIsSynchronousAndPreservesActiveScanoutBuffers) { + ActivateWithConnectorProps(*this); + + // Stage an active scanout framebuffer and retain only a weak reference so we + // can verify FinishPendingRequest() does not drop it on HDCP-only commits. + auto active_fb = std::make_shared<FakeFbIdHandle>(kTestFbId2); + const std::weak_ptr<FakeFbIdHandle> weak_fb = active_fb; + auto plan = std::make_shared<LayerToPlaneJoiningPlan>(); + LayerData layer = CreateTestLayerData(kTestFbId2); + layer.fb = std::move(active_fb); + plan->plan.emplace_back(std::move(layer), pipeline->primary_plane, 0); + + AtomicCommitArgs flip_args{}; + flip_args.blocking = true; + flip_args.composition = std::move(plan); + auto flip_req = state_manager->GetAtomicModeReqForArgs(flip_args); + ASSERT_NE(flip_req, nullptr); + state_manager->FinishPendingRequest(flip_args, std::move(flip_req)); + flip_args.composition.reset(); + ASSERT_FALSE(weak_fb.expired()); + + // 1. Standalone non-blocking-flagged HDCP-only commit must execute + // synchronously (without DRM_MODE_ATOMIC_NONBLOCK) and preserve active FBs. + uint32_t observed_flags = 0; + FakeKernelDrmAtomicCommitSink sink_impl( + [&observed_flags](int /*fd*/, drmModeAtomicReqPtr /*req*/, uint32_t flags, + void* /*user_data*/, size_t /*attempt*/) -> int { + observed_flags = flags; + return 0; + }); + AtomicCommitSink& sink = sink_impl; + + AtomicCommitArgs hdcp_args{}; + hdcp_args.content_protection = ContentProtection::kDesired; + hdcp_args.hdcp_content_type = HdcpContentType::kType1; + const auto res1 = sink.ExecuteAtomicCommit( + {{state_manager.get(), hdcp_args}}); + ASSERT_TRUE(res1.IsSuccess()); + EXPECT_EQ(sink_impl.GetTotalCalls(), 1U); + EXPECT_EQ(sink_impl.GetNonBlockCalls(), 0U); + EXPECT_EQ(observed_flags, + static_cast<uint32_t>(DRM_MODE_ATOMIC_ALLOW_MODESET)); + EXPECT_FALSE(weak_fb.expired()); + + // 2. A blocking commit whose redundant power_mode is cleared by + // CheckDoubleSettingState() becomes an hdcp_only_request and must also + // preserve active scanout buffers in FinishPendingRequest(). + AtomicCommitArgs redundant_power_args{}; + redundant_power_args.blocking = true; + redundant_power_args.power_mode = PowerMode::kOn; + redundant_power_args.content_protection = ContentProtection::kUndesired; + redundant_power_args.hdcp_content_type = HdcpContentType::kType0; + const auto res2 = sink.ExecuteAtomicCommit( + {{state_manager.get(), redundant_power_args}}); + ASSERT_TRUE(res2.IsSuccess()); + EXPECT_EQ(sink_impl.GetTotalCalls(), 2U); + EXPECT_FALSE(weak_fb.expired()); +} + +TEST_F(DrmAtomicStateManagerTest, + DrmAtomicCommitSinkSkipsPlaneCleanupOnFailedHdcpOnlyCommit) { + ActivateWithConnectorProps(*this); + RecordingAtomicStateManager recording_manager(pipeline.get()); + + AtomicCommitArgs modeset0{}; + modeset0.blocking = true; + modeset0.power_mode = PowerMode::kOn; + modeset0.display_mode = CreateFakeDrmMode(kTestWidth, kTestHeight, + kTestVRefresh); + modeset0.composition = CreateSinglePlanePlan(kTestFbId1); + auto req0 = recording_manager.GetAtomicModeReqForArgs(modeset0); + ASSERT_NE(req0, nullptr); + recording_manager.FinishPendingRequest(modeset0, std::move(req0)); + ASSERT_TRUE(recording_manager.IsActive()); + ASSERT_EQ(recording_manager.GetRecordedArgs().size(), 1U); + + FakeKernelDrmAtomicCommitSink sink_impl( + [](int /*fd*/, drmModeAtomicReqPtr /*req*/, uint32_t /*flags*/, + void* /*user_data*/, size_t /*attempt*/) -> int { return -EINVAL; }); + AtomicCommitSink& sink = sink_impl; + + AtomicCommitArgs hdcp_args{}; + hdcp_args.content_protection = ContentProtection::kDesired; + hdcp_args.hdcp_content_type = HdcpContentType::kType1; + const auto res = sink.ExecuteAtomicCommit({{&recording_manager, hdcp_args}}); + EXPECT_FALSE(res.IsSuccess()); + EXPECT_EQ(sink_impl.GetTotalCalls(), 1U); + // Only the initial modeset and the failed HDCP-only commit should be + // recorded; CleanUpFailedRequest() must not issue a plane-disabling commit. + EXPECT_EQ(recording_manager.GetRecordedArgs().size(), 2U); +} + +TEST_F(DrmAtomicStateManagerTest, + HdcpOnlyArgsFailsWithoutIoctlWhenConnectorDeletedOrMissingProps) { + FakeKernelDrmAtomicCommitSink sink_impl( + [](int /*fd*/, drmModeAtomicReqPtr /*req*/, uint32_t /*flags*/, + void* /*user_data*/, size_t /*attempt*/) -> int { return 0; }); + AtomicCommitSink& sink = sink_impl; + + AtomicCommitArgs hdcp_args{}; + hdcp_args.content_protection = ContentProtection::kDesired; + hdcp_args.hdcp_content_type = HdcpContentType::kType1; + + // Connector has no HDCP properties registered yet. + EXPECT_EQ(state_manager->GetAtomicModeReqForArgs(hdcp_args), nullptr); + const CommitStatus missing_status = sink.TestAtomicCommit( + {{state_manager.get(), hdcp_args}}); + EXPECT_FALSE(missing_status.success); + EXPECT_EQ(sink_impl.GetTotalCalls(), 0U); + + // Register HDCP properties and activate, then delete the connector (e.g. + // unregistered MST connector). + ActivateWithConnectorProps(*this); + fake_connector->MarkDeleted(); + EXPECT_EQ(state_manager->GetAtomicModeReqForArgs(hdcp_args), nullptr); + const CommitStatus deleted_status = sink.TestAtomicCommit( + {{state_manager.get(), hdcp_args}}); + EXPECT_FALSE(deleted_status.success); + EXPECT_EQ(sink_impl.GetTotalCalls(), 0U); +} + } // namespace android::drm_hwcomposer diff --git a/hwc/HwcDisplay.cpp b/hwc/HwcDisplay.cpp index 3832a97..fa6a6f9 100644 --- a/hwc/HwcDisplay.cpp +++ b/hwc/HwcDisplay.cpp @@ -27,6 +27,7 @@ #include <algorithm> #include <array> +#include <cerrno> #include <cinttypes> #include <cmath> #include <cstddef> @@ -257,6 +258,7 @@ bool HwcDisplay::RefreshConfigsFromConnector() { ClearStagedModeConfig(); staged_mode_config_id_ = configs_.preferred_config_id; link_recovery_modeset_required_ = true; + hdcp_type1_supported_.reset(); return true; } @@ -573,6 +575,8 @@ auto HwcDisplay::ValidateStagedComposition() -> ValidateResult { writeback_.reset(); } + ResolvePendingHdcpContentType(); + auto [composition, short_circuited] = pipeline_->planner->ValidateDisplay(this); validated_composition_.emplace(std::move(composition)); @@ -1156,6 +1160,7 @@ void HwcDisplay::Deinit() { defer_commits_until_power_on_ = false; clear_hdcp_on_next_commit_ = false; hdcp_desired_in_kms_ = false; + hdcp_type1_supported_.reset(); if (vsync_worker_) { vsync_worker_->StopThread(); @@ -1240,14 +1245,17 @@ void HwcDisplay::ResetClientState(bool reset_hdcp) { } } - if (reset_hdcp && hdcpcon_ != nullptr) { - const auto state = hdcpcon_->GetHdcpState(); - if (state != HdcpController::HdcpState::kUndesired && - state != HdcpController::HdcpState::kInfeasible) { - hdcpcon_->Reset(); - clear_hdcp_on_next_commit_ = true; - } else if (hdcp_desired_in_kms_) { - clear_hdcp_on_next_commit_ = true; + if (reset_hdcp) { + hdcp_type1_supported_.reset(); + if (hdcpcon_ != nullptr) { + const auto state = hdcpcon_->GetHdcpState(); + if (state != HdcpController::HdcpState::kUndesired && + state != HdcpController::HdcpState::kInfeasible) { + hdcpcon_->Reset(); + clear_hdcp_on_next_commit_ = true; + } else if (hdcp_desired_in_kms_) { + clear_hdcp_on_next_commit_ = true; + } } } } @@ -1371,6 +1379,7 @@ bool HwcDisplay::Init() { requires_reprobe_on_power_on_ = false; defer_commits_until_power_on_ = false; + hdcp_type1_supported_.reset(); if (!IsInHeadlessMode()) { if (Properties::FlatteningEnabled()) { auto flatcbk = (struct FlatConCallbacks){ @@ -1704,6 +1713,56 @@ auto HwcDisplay::IsHdcpPropertyPresent() -> bool { return true; } +std::optional<bool> HwcDisplay::ProbeHdcpType1Support() { + if (hdcp_type1_supported_.has_value()) { + return hdcp_type1_supported_; + } + if (IsInHeadlessMode() || defer_commits_until_power_on_ || + link_recovery_modeset_required_ || staged_power_mode_.has_value() || + clear_hdcp_on_next_commit_ || + !GetPipe().atomic_state_manager->IsActive()) { + return std::nullopt; + } + auto *connector = GetPipe().connector ? GetPipe().connector->Get() : nullptr; + if (connector == nullptr || connector->IsDeleted() || + !IsHdcpPropertyPresent()) { + return std::nullopt; + } + + AtomicCommitArgs test_args{}; + test_args.content_protection = ContentProtection::kDesired; + test_args.hdcp_content_type = HdcpContentType::kType1; + + const CommitStatus + status = GetPipe().device->GetAtomicCommitSink().TestAtomicCommit( + {{GetPipe().atomic_state_manager.get(), test_args}}); + if (status.success) { + hdcp_type1_supported_ = true; + return true; + } + if (status.error_code == -EINVAL || status.error_code == -EOPNOTSUPP) { + ALOGI( + "HDCP Content Type 1 TEST_ONLY commit rejected (err=%d) on display %d; " + "starting directly with Content Type 0", + status.error_code, static_cast<int>(handle_)); + hdcp_type1_supported_ = false; + return false; + } + return std::nullopt; +} + +void HwcDisplay::ResolvePendingHdcpContentType() { + if (hdcpcon_ == nullptr || hdcp_type1_supported_.has_value() || + hdcpcon_->GetHdcpState() != HdcpController::HdcpState::kDesired || + hdcpcon_->GetPendingContentType() != HdcpContentType::kType1) { + return; + } + const auto type1_supported = ProbeHdcpType1Support(); + if (type1_supported.has_value() && !*type1_supported) { + hdcpcon_->Start(HdcpContentType::kType0); + } +} + auto HwcDisplay::StartHdcp() -> bool { if (hdcpcon_ == nullptr) { const int forced_level = Properties::ForceHdcpLevel(); @@ -1731,7 +1790,17 @@ auto HwcDisplay::StartHdcp() -> bool { "display"); return false; } - hdcpcon_->Start(); + + const auto hdcp_state = hdcpcon_->GetHdcpState(); + auto initial_type = HdcpContentType::kType1; + if (hdcp_state == HdcpController::HdcpState::kUndesired || + hdcp_state == HdcpController::HdcpState::kDesired) { + const auto type1_supported = ProbeHdcpType1Support(); + if (type1_supported.has_value() && !*type1_supported) { + initial_type = HdcpContentType::kType0; + } + } + hdcpcon_->Start(initial_type); return true; } @@ -1998,20 +2067,18 @@ std::optional<AtomicCommitArgs> HwcDisplay::CreateFrameUpdateCommit( a_args.blocking = true; } - auto hdcp_state = hdcpcon_ ? hdcpcon_->GetHdcpState() - : HdcpController::HdcpState::kUndesired; // Flush a pending UNDESIRED transition before any new DESIRED request so the // kernel latches the new HDCP Content Type on UNDESIRED -> DESIRED rather // than reusing an active Type 0 session from the detached client. if (clear_hdcp_on_next_commit_) { a_args.content_protection = ContentProtection::kUndesired; a_args.hdcp_content_type = HdcpContentType::kType0; - } else if (hdcp_state == HdcpController::HdcpState::kDesired) { - a_args.content_protection = ContentProtection::kDesired; - a_args.hdcp_content_type = HdcpContentType::kType1; - } else if (hdcp_state == HdcpController::HdcpState::kRetry) { - a_args.content_protection = ContentProtection::kDesired; - a_args.hdcp_content_type = HdcpContentType::kType0; + } else if (hdcpcon_ != nullptr && !staged_power_mode_.has_value()) { + if (auto pending_type = hdcpcon_->GetPendingContentType(); + pending_type.has_value()) { + a_args.content_protection = ContentProtection::kDesired; + a_args.hdcp_content_type = pending_type; + } } a_args.composition = composition.composition_plan @@ -2134,6 +2201,8 @@ CommitStatus HwcDisplay::CommitStagedComposition(SharedFd &out_present_fence) { return CommitStatus::InternalFailure(); } + ResolvePendingHdcpContentType(); + PrepareCompositionForCommit(validated_composition_.value()); auto a_args = CreateFrameUpdateCommit(validated_composition_.value()); if (a_args) { diff --git a/hwc/HwcDisplay.h b/hwc/HwcDisplay.h index ba873de..4276053 100644 --- a/hwc/HwcDisplay.h +++ b/hwc/HwcDisplay.h @@ -408,6 +408,8 @@ class HwcDisplay : public ICompositorDisplay { private: bool HasPhysicalConnector() const; + std::optional<bool> ProbeHdcpType1Support(); + void ResolvePendingHdcpContentType(); bool IsDozeSupported() const; bool IsDozeSuspendSupported() const; @@ -501,6 +503,7 @@ class HwcDisplay : public ICompositorDisplay { bool defer_commits_until_power_on_ = false; bool clear_hdcp_on_next_commit_ = false; bool hdcp_desired_in_kms_ = false; + std::optional<bool> hdcp_type1_supported_; std::shared_ptr<DrmDisplayPipeline> pipeline_; diff --git a/hwc/HwcDisplayTest.cpp b/hwc/HwcDisplayTest.cpp index a6e64cf..6d901c6 100644 --- a/hwc/HwcDisplayTest.cpp +++ b/hwc/HwcDisplayTest.cpp @@ -23,11 +23,13 @@ #include <atomic> #include <cerrno> #include <chrono> +#include <condition_variable> #include <cstddef> #include <cstdint> #include <cstdlib> #include <cstring> #include <memory> +#include <mutex> #include <optional> #include <string> #include <thread> @@ -137,8 +139,30 @@ class TestDrmHwc : public DrmHwc { void SendHdcpLevelsChangedEventToClient( DisplayHandle /*display_handle*/, std::optional<HdcpContentType> current_hdcp_level) override { - last_hdcp_level = current_hdcp_level; - hdcp_callback_count++; + { + const std::lock_guard<std::mutex> lock(hdcp_mutex_); + last_hdcp_level_ = current_hdcp_level; + ++hdcp_callback_count_; + } + hdcp_cv_.notify_all(); + } + + int GetHdcpCallbackCount() const { + const std::lock_guard<std::mutex> lock(hdcp_mutex_); + return hdcp_callback_count_; + } + + std::optional<HdcpContentType> GetLastHdcpLevel() const { + const std::lock_guard<std::mutex> lock(hdcp_mutex_); + return last_hdcp_level_; + } + + bool WaitForHdcpCallbackCount(int expected_count, + std::chrono::milliseconds timeout) { + std::unique_lock<std::mutex> lock(hdcp_mutex_); + return hdcp_cv_.wait_for(lock, timeout, [this, expected_count]() { + return hdcp_callback_count_ >= expected_count; + }); } void ResetVsyncCount() const { @@ -165,12 +189,11 @@ class TestDrmHwc : public DrmHwc { return hotplug_count_.load(); } - // NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes) - std::optional<HdcpContentType> last_hdcp_level; - int hdcp_callback_count = 0; - // NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes) - private: + mutable std::mutex hdcp_mutex_; + std::condition_variable hdcp_cv_; + std::optional<HdcpContentType> last_hdcp_level_; + int hdcp_callback_count_ = 0; mutable std::atomic<int> vsync_count_{0}; std::atomic<int> refresh_count_{0}; std::atomic<int> hotplug_count_{0}; @@ -1188,19 +1211,16 @@ TEST_F(HwcDisplayTest, StartHdcpRepeatedNotifications) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast<HdcpController*>(display.GetHdcpController())->Requested(); - // Wait for the background thread to process the callback - - - for (int i = 0; i < kMaxRetries; ++i) { - if (hwc.last_hdcp_level == HdcpContentType::kType1) - break; - std::this_thread::sleep_for(std::chrono::milliseconds(kRetryDelayMs)); - } + // Wait for the background thread to process the callback. + ASSERT_TRUE( + hwc.WaitForHdcpCallbackCount(1, std::chrono::milliseconds( + kMaxRetries * kRetryDelayMs))); + EXPECT_EQ(hwc.GetLastHdcpLevel(), HdcpContentType::kType1); - int count_before = hwc.hdcp_callback_count; + const int count_before = hwc.GetHdcpCallbackCount(); EXPECT_TRUE(display.StartHdcp()); - EXPECT_EQ(hwc.hdcp_callback_count, count_before + 1); - EXPECT_EQ(hwc.last_hdcp_level, HdcpContentType::kType1); + EXPECT_EQ(hwc.GetHdcpCallbackCount(), count_before + 1); + EXPECT_EQ(hwc.GetLastHdcpLevel(), HdcpContentType::kType1); } TEST_F(HwcDisplayTest, StopHdcpTeardown) { @@ -1251,24 +1271,18 @@ TEST_F(HwcDisplayTest, StopHdcpTeardown) { ASSERT_NE(display.GetHdcpController(), nullptr); // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast<HdcpController*>(display.GetHdcpController())->Requested(); - - - for (int i = 0; i < kMaxRetries; ++i) { - if (hwc.last_hdcp_level == HdcpContentType::kType1) - break; - std::this_thread::sleep_for(std::chrono::milliseconds(kRetryDelayMs)); - } - EXPECT_TRUE(display.StopHdcp()); - - - for (int i = 0; i < kMaxRetries; ++i) { - if (hwc.last_hdcp_level == std::nullopt) - break; - std::this_thread::sleep_for(std::chrono::milliseconds(kRetryDelayMs)); - } + ASSERT_TRUE( + hwc.WaitForHdcpCallbackCount(1, std::chrono::milliseconds( + kMaxRetries * kRetryDelayMs))); + EXPECT_EQ(hwc.GetLastHdcpLevel(), HdcpContentType::kType1); - EXPECT_EQ(hwc.last_hdcp_level, std::nullopt); + const int callbacks_before_stop = hwc.GetHdcpCallbackCount(); + EXPECT_TRUE(display.StopHdcp()); + ASSERT_TRUE(hwc.WaitForHdcpCallbackCount(callbacks_before_stop + 1, + std::chrono::milliseconds( + kMaxRetries * kRetryDelayMs))); + EXPECT_EQ(hwc.GetLastHdcpLevel(), std::nullopt); fake_device->GetFakeAtomicCommitSink()->ClearHistory(); display.ValidateStagedComposition(); @@ -5319,4 +5333,282 @@ TEST_F(HwcDisplayTest, ResetClientStatePreservesEarlyBootAnimationBrightness) { } } +TEST_F(HwcDisplayTest, StartHdcpProbesType1WithTestCommitAndUsesType1OnPass) { + auto pipeline = CreatePipelineWithModes( + {CreateFakeDrmMode(kFhdWidth, kFhdHeight, kRefresh60Hz, + DRM_MODE_TYPE_PREFERRED, "1080p60")}, + /*is_external=*/true); + ASSERT_NE(pipeline, nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_conn = static_cast<FakeDrmConnector*>(pipeline->connector->Get()); + fake_conn->SetContentProtectionPropertySupported(true); + fake_conn->SetHdcpContentTypePropertySupported(true); + + HwcDisplay display(kPrimaryDisplay, /*is_virtual=*/false, &hwc); + ASSERT_TRUE(display.SetPipeline(pipeline)); + ASSERT_TRUE(display.GetPipe().atomic_state_manager->IsActive()); + + auto* sink = fake_device->GetFakeAtomicCommitSink(); + sink->ClearHistory(); + std::optional<AtomicCommitArgs> probe_args; + sink->SetTestCommitHook( + [&probe_args]( + const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>& + args) { + if (args.size() == 1U && args[0].second.content_protection && + args[0].second.hdcp_content_type && !args[0].second.composition) { + probe_args = args[0].second; + } + return CommitStatus::Success(); + }); + + ASSERT_TRUE(display.StartHdcp()); + if (!probe_args.has_value()) { + ADD_FAILURE() << "Expected HDCP Type 1 TEST_ONLY probe args"; + return; + } + EXPECT_EQ(probe_args->content_protection, ContentProtection::kDesired); + EXPECT_EQ(probe_args->hdcp_content_type, HdcpContentType::kType1); + EXPECT_FALSE(probe_args->seamless); + EXPECT_EQ(display.GetHdcpController()->GetHdcpState(), + HdcpController::HdcpState::kDesired); + EXPECT_EQ(display.GetHdcpController()->GetPendingContentType(), + HdcpContentType::kType1); + + sink->SetTestCommitHook(nullptr); + StageSingleDeviceLayer(display); + sink->ClearHistory(); + ValidateAcceptAndPresent(display); + ASSERT_FALSE(sink->GetCommitHistory().empty()); + const auto& frame_args = sink->GetCommitHistory().back().front(); + EXPECT_EQ(frame_args.content_protection, ContentProtection::kDesired); + EXPECT_EQ(frame_args.hdcp_content_type, HdcpContentType::kType1); +} + +TEST_F( + HwcDisplayTest, + StartHdcpFallsBackDirectlyToType0WithoutRetryWhenTestCommitRejectsType1) { + auto pipeline = CreatePipelineWithModes( + {CreateFakeDrmMode(kFhdWidth, kFhdHeight, kRefresh60Hz, + DRM_MODE_TYPE_PREFERRED, "1080p60")}, + /*is_external=*/true); + ASSERT_NE(pipeline, nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_conn = static_cast<FakeDrmConnector*>(pipeline->connector->Get()); + fake_conn->SetContentProtectionPropertySupported(true); + fake_conn->SetHdcpContentTypePropertySupported(true); + fake_conn->SetContentProtectionEnabled(false); + + HwcDisplay display(kPrimaryDisplay, /*is_virtual=*/false, &hwc); + ASSERT_TRUE(display.SetPipeline(pipeline)); + ASSERT_TRUE(display.GetPipe().atomic_state_manager->IsActive()); + + auto* sink = fake_device->GetFakeAtomicCommitSink(); + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>& + args) { + if (args.size() == 1U && + args[0].second.hdcp_content_type == HdcpContentType::kType1 && + !args[0].second.composition) { + return CommitStatus::Failure(-EINVAL); + } + return CommitStatus::Success(); + }); + + ASSERT_TRUE(display.StartHdcp()); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + auto* hdcp = const_cast<HdcpController*>(display.GetHdcpController()); + ASSERT_NE(hdcp, nullptr); + EXPECT_EQ(hdcp->GetHdcpState(), HdcpController::HdcpState::kDesired); + EXPECT_EQ(hdcp->GetPendingContentType(), HdcpContentType::kType0); + + sink->SetTestCommitHook(nullptr); + StageSingleDeviceLayer(display); + sink->ClearHistory(); + hwc.ResetRefreshCount(); + ValidateAcceptAndPresent(display); + ASSERT_FALSE(sink->GetCommitHistory().empty()); + const auto& frame_args = sink->GetCommitHistory().back().front(); + EXPECT_EQ(frame_args.content_protection, ContentProtection::kDesired); + EXPECT_EQ(frame_args.hdcp_content_type, HdcpContentType::kType0); + + // Timeout on the direct Type 0 attempt must transition straight to + // kInfeasible without requesting a retry refresh. + const int callbacks_before = hwc.GetHdcpCallbackCount(); + hdcp->ExpireRequestTimeoutForTesting(); + ASSERT_TRUE(hwc.WaitForHdcpCallbackCount(callbacks_before + 1, + std::chrono::milliseconds( + kMaxRetries * kRetryDelayMs))); + EXPECT_EQ(hdcp->GetHdcpState(), HdcpController::HdcpState::kInfeasible); + EXPECT_EQ(hwc.GetRefreshCount(), 0); + EXPECT_EQ(hwc.GetHdcpCallbackCount(), callbacks_before + 1); + EXPECT_EQ(hwc.GetLastHdcpLevel(), std::nullopt); +} + +TEST_F(HwcDisplayTest, + StartHdcpKeepsType1OnTransientOrInternalTestCommitFailure) { + auto pipeline = CreatePipelineWithModes( + {CreateFakeDrmMode(kFhdWidth, kFhdHeight, kRefresh60Hz, + DRM_MODE_TYPE_PREFERRED, "1080p60")}, + /*is_external=*/true); + ASSERT_NE(pipeline, nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_conn = static_cast<FakeDrmConnector*>(pipeline->connector->Get()); + fake_conn->SetContentProtectionPropertySupported(true); + fake_conn->SetHdcpContentTypePropertySupported(true); + + HwcDisplay display(kPrimaryDisplay, /*is_virtual=*/false, &hwc); + ASSERT_TRUE(display.SetPipeline(pipeline)); + ASSERT_TRUE(display.GetPipe().atomic_state_manager->IsActive()); + + auto* sink = fake_device->GetFakeAtomicCommitSink(); + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>&) { + return CommitStatus::InternalFailure(); + }); + ASSERT_TRUE(display.StartHdcp()); + EXPECT_EQ(display.GetHdcpController()->GetPendingContentType(), + HdcpContentType::kType1); + + display.ResetClientState(/*reset_hdcp=*/true); + // Flush the pending UNDESIRED clear commit so the subsequent StartHdcp() + // probe runs against a clean KMS state. + StageSingleDeviceLayer(display); + sink->SetTestCommitHook(nullptr); + ValidateAcceptAndPresent(display); + + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>&) { + return CommitStatus::Failure(-EBUSY); + }); + ASSERT_TRUE(display.StartHdcp()); + EXPECT_EQ(display.GetHdcpController()->GetPendingContentType(), + HdcpContentType::kType1); + + // Because -EBUSY is transient and not cached, presenting a frame once the + // kernel starts returning -EINVAL for Type 1 must re-probe and downgrade to + // Type 0 before committing. + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>& + args) { + if (args.size() == 1U && + args[0].second.hdcp_content_type == HdcpContentType::kType1) { + return CommitStatus::Failure(-EINVAL); + } + return CommitStatus::Success(); + }); + sink->ClearHistory(); + ValidateAcceptAndPresent(display); + ASSERT_FALSE(sink->GetCommitHistory().empty()); + const auto& frame_args = sink->GetCommitHistory().back().front(); + EXPECT_EQ(frame_args.content_protection, ContentProtection::kDesired); + EXPECT_EQ(frame_args.hdcp_content_type, HdcpContentType::kType0); + sink->SetTestCommitHook(nullptr); +} + +TEST_F(HwcDisplayTest, + StartHdcpDefersProbeWhileInactiveAndDowngradesOnPowerOnFrame) { + constexpr DisplayHandle kExternalHandle = 1; + auto pipeline = CreatePipelineWithModes( + {CreateFakeDrmMode(kFhdWidth, kFhdHeight, kRefresh60Hz, + DRM_MODE_TYPE_PREFERRED, "1080p60")}, + /*is_external=*/true); + ASSERT_NE(pipeline, nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_conn = static_cast<FakeDrmConnector*>(pipeline->connector->Get()); + fake_conn->SetContentProtectionPropertySupported(true); + fake_conn->SetHdcpContentTypePropertySupported(true); + + HwcDisplay display(kExternalHandle, /*is_virtual=*/false, &hwc); + ASSERT_TRUE(display.SetPipeline(pipeline)); + ASSERT_FALSE(display.GetPipe().atomic_state_manager->IsActive()); + + auto* sink = fake_device->GetFakeAtomicCommitSink(); + sink->ClearHistory(); + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>& + args) { + if (args.size() == 1U && + args[0].second.hdcp_content_type == HdcpContentType::kType1) { + return CommitStatus::Failure(-EINVAL); + } + return CommitStatus::Success(); + }); + + // Display is inactive: probe is deferred and StartHdcp() stages kType1. + ASSERT_TRUE(display.StartHdcp()); + EXPECT_EQ(sink->GetTestCommitCount(), 0U); + EXPECT_EQ(display.GetHdcpController()->GetHdcpState(), + HdcpController::HdcpState::kDesired); + EXPECT_EQ(display.GetHdcpController()->GetPendingContentType(), + HdcpContentType::kType1); + + // Power the external display on, then validate & present a frame. The + // deferred Type 1 probe must run once active, cache unsupported, and commit + // Content Type 0 cleanly without hitting a permanent -EINVAL loop. + ASSERT_EQ(display.SetPowerMode(PowerMode::kOn), HwcDisplay::Error::kNone); + ASSERT_TRUE(display.GetPipe().atomic_state_manager->IsActive()); + + StageSingleDeviceLayer(display); + sink->ClearHistory(); + ValidateAcceptAndPresent(display); + ASSERT_FALSE(sink->GetCommitHistory().empty()); + const auto& frame_args = sink->GetCommitHistory().back().front(); + EXPECT_EQ(frame_args.content_protection, ContentProtection::kDesired); + EXPECT_EQ(frame_args.hdcp_content_type, HdcpContentType::kType0); + EXPECT_EQ(display.GetHdcpController()->GetHdcpState(), + HdcpController::HdcpState::kRequested); + sink->SetTestCommitHook(nullptr); +} + +TEST_F(HwcDisplayTest, + StartHdcpSkipsType1ProbeDuringLinkRecoveryAndRequestedState) { + auto pipeline = CreatePipelineWithModes( + {CreateFakeDrmMode(kFhdWidth, kFhdHeight, kRefresh60Hz, + DRM_MODE_TYPE_PREFERRED, "1080p60")}, + /*is_external=*/true); + ASSERT_NE(pipeline, nullptr); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) + auto* fake_conn = static_cast<FakeDrmConnector*>(pipeline->connector->Get()); + fake_conn->SetContentProtectionPropertySupported(true); + fake_conn->SetHdcpContentTypePropertySupported(true); + fake_conn->SetContentProtectionEnabled(false); + + HwcDisplay display(kPrimaryDisplay, /*is_virtual=*/false, &hwc); + ASSERT_TRUE(display.SetPipeline(pipeline)); + ASSERT_TRUE(display.GetPipe().atomic_state_manager->IsActive()); + + auto* sink = fake_device->GetFakeAtomicCommitSink(); + // Simulate a link-recovery reprobe: RefreshConfigsFromConnector() sets + // link_recovery_modeset_required_ and clears any cached Type 1 probe result. + ASSERT_TRUE(display.RefreshConfigsFromConnector()); + sink->ClearHistory(); + sink->SetTestCommitHook( + [](const std::vector<std::pair<AtomicStateManager*, AtomicCommitArgs>>&) { + return CommitStatus::Failure(-EINVAL); + }); + + ASSERT_TRUE(display.StartHdcp()); + EXPECT_EQ(sink->GetTestCommitCount(), 0U); + EXPECT_EQ(display.GetHdcpController()->GetPendingContentType(), + HdcpContentType::kType1); + + // Recover the link via SetConfig(), then present a frame with Type 1 passing. + sink->SetTestCommitHook(nullptr); + ASSERT_EQ(display.SetConfig(display.GetLastRequestedConfig()->id), + HwcDisplay::ConfigError::kNone); + StageSingleDeviceLayer(display); + ValidateAcceptAndPresent(display); + ASSERT_EQ(display.GetHdcpController()->GetHdcpState(), + HdcpController::HdcpState::kRequested); + + // A duplicate StartHdcp() while kRequested must not issue a probe or reset + // the in-flight authentication back to kDesired. + sink->ClearHistory(); + ASSERT_TRUE(display.StartHdcp()); + EXPECT_EQ(sink->GetTestCommitCount(), 0U); + EXPECT_EQ(display.GetHdcpController()->GetHdcpState(), + HdcpController::HdcpState::kRequested); +} + } // namespace android::drm_hwcomposer |
