Skip to content

Commit

Permalink
Bug 1665225 - Remove unnecessary to-main-thread dispatch r=alwu
Browse files Browse the repository at this point in the history
The SMTC-thumbnail-update task is dispatched to main-thread from the
on-complete callback of the async image storing operation. However, the
callback is actually on the main-thread itself so the
thumbnail-update-task can be executed in that callback directly instead
of being dispatched. The task should be finished slightly faster if the
task dispatch is replaced by executing the task.

Differential Revision: https://phabricator.services.mozilla.com/D90331
  • Loading branch information
ChunMinChang committed Sep 16, 2020
1 parent 1cfac8c commit 7142b48
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
52 changes: 25 additions & 27 deletions widget/windows/WindowsSMTCProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ void WindowsSMTCProvider::LoadImage(const char* aImageData,
[this, self = RefPtr<WindowsSMTCProvider>(this),
aImageUrl = nsString(mProcessingUrl)](
IAsyncOperation<unsigned int>* aAsyncOp, AsyncStatus aStatus) {
MOZ_ASSERT(NS_IsMainThread());

if (aStatus != AsyncStatus::Completed) {
LOG("Asynchronous operation is not completed");
return E_ABORT;
Expand All @@ -609,9 +611,8 @@ void WindowsSMTCProvider::LoadImage(const char* aImageData,
return hr;
}

nsresult rv = UpdateThumbnailOnMainThread(aImageUrl);
if (NS_FAILED(rv)) {
LOG("Failed to dispatch a task to thumbnail update");
if (!UpdateThumbnail(aImageUrl)) {
LOG("Failed to update thumbnail");
}

// If an error occurs above:
Expand Down Expand Up @@ -689,34 +690,31 @@ void WindowsSMTCProvider::ClearThumbnail() {
mThumbnailUrl = EmptyString();
}

nsresult WindowsSMTCProvider::UpdateThumbnailOnMainThread(
const nsAString& aUrl) {
return NS_DispatchToMainThread(
media::NewRunnableFrom([this, self = RefPtr<WindowsSMTCProvider>(this),
aImageUrl = nsString(aUrl)] {
if (!IsOpened()) {
LOG("Abort the thumbnail update: SMTC is closed");
return NS_OK;
}
bool WindowsSMTCProvider::UpdateThumbnail(const nsAString& aUrl) {
MOZ_ASSERT(NS_IsMainThread());

if (aImageUrl != mProcessingUrl) {
LOG("Abort the thumbnail update: The image from %s is out of date",
NS_ConvertUTF16toUTF8(aImageUrl).get());
return NS_OK;
}
if (!IsOpened()) {
LOG("Abort the thumbnail update: SMTC is closed");
return false;
}

mProcessingUrl = EmptyString();
if (aUrl != mProcessingUrl) {
LOG("Abort the thumbnail update: The image from %s is out of date",
NS_ConvertUTF16toUTF8(aUrl).get());
return false;
}

if (!SetThumbnail(aImageUrl)) {
LOG("Failed to update thumbnail");
return NS_OK;
}
mProcessingUrl = EmptyString();

if (!SetThumbnail(aUrl)) {
LOG("Failed to update thumbnail");
return false;
}

MOZ_ASSERT(mThumbnailUrl == aImageUrl);
LOG("The thumbnail is updated to the image from: %s",
NS_ConvertUTF16toUTF8(mThumbnailUrl).get());
return NS_OK;
}));
MOZ_ASSERT(mThumbnailUrl == aUrl);
LOG("The thumbnail is updated to the image from: %s",
NS_ConvertUTF16toUTF8(mThumbnailUrl).get());
return true;
}

void WindowsSMTCProvider::CancelPendingStoreAsyncOperation() const {
Expand Down
2 changes: 1 addition & 1 deletion widget/windows/WindowsSMTCProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class WindowsSMTCProvider final : public mozilla::dom::MediaControlKeySource {
bool SetThumbnail(const nsAString& aUrl);
void ClearThumbnail();

nsresult UpdateThumbnailOnMainThread(const nsAString& aUrl);
bool UpdateThumbnail(const nsAString& aUrl);
void CancelPendingStoreAsyncOperation() const;

bool mInitialized = false;
Expand Down

0 comments on commit 7142b48

Please sign in to comment.