forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1738971 - Part 1. Split out plumbing for AnimationFrameProvider f…
…rom Document. r=dom-worker-reviewers,smaug This patch splits out AnimationFrameProvider from the Document WebIDL to allow the workers to implement it. It also splits out a helper class to manage the requestAnimationFrame callbacks which may be reused on a worker thread. Differential Revision: https://phabricator.services.mozilla.com/D130262
- Loading branch information
Showing
10 changed files
with
164 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "mozilla/dom/AnimationFrameProvider.h" | ||
#include "nsThreadUtils.h" | ||
|
||
namespace mozilla::dom { | ||
|
||
FrameRequest::FrameRequest(FrameRequestCallback& aCallback, int32_t aHandle) | ||
: mCallback(&aCallback), mHandle(aHandle) { | ||
LogFrameRequestCallback::LogDispatch(mCallback); | ||
} | ||
|
||
FrameRequest::~FrameRequest() = default; | ||
|
||
nsresult FrameRequestManager::Schedule(FrameRequestCallback& aCallback, | ||
int32_t* aHandle) { | ||
if (mCallbackCounter == INT32_MAX) { | ||
// Can't increment without overflowing; bail out | ||
return NS_ERROR_NOT_AVAILABLE; | ||
} | ||
int32_t newHandle = ++mCallbackCounter; | ||
|
||
mCallbacks.AppendElement(FrameRequest(aCallback, newHandle)); | ||
|
||
*aHandle = newHandle; | ||
return NS_OK; | ||
} | ||
|
||
bool FrameRequestManager::Cancel(int32_t aHandle) { | ||
// mCallbacks is stored sorted by handle | ||
if (mCallbacks.RemoveElementSorted(aHandle)) { | ||
return true; | ||
} | ||
|
||
Unused << mCanceledCallbacks.put(aHandle); | ||
return false; | ||
} | ||
|
||
void FrameRequestManager::Unlink() { mCallbacks.Clear(); } | ||
|
||
void FrameRequestManager::Traverse(nsCycleCollectionTraversalCallback& aCB) { | ||
for (auto& i : mCallbacks) { | ||
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCB, | ||
"FrameRequestManager::mCallbacks[i]"); | ||
aCB.NoteXPCOMChild(i.mCallback); | ||
} | ||
} | ||
|
||
} // namespace mozilla::dom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef mozilla_dom_AnimationFrameProvider_h | ||
#define mozilla_dom_AnimationFrameProvider_h | ||
|
||
#include "mozilla/dom/AnimationFrameProviderBinding.h" | ||
#include "mozilla/HashTable.h" | ||
#include "mozilla/RefPtr.h" | ||
#include "nsTArray.h" | ||
|
||
namespace mozilla::dom { | ||
|
||
struct FrameRequest { | ||
FrameRequest(FrameRequestCallback& aCallback, int32_t aHandle); | ||
~FrameRequest(); | ||
|
||
// Comparator operators to allow RemoveElementSorted with an | ||
// integer argument on arrays of FrameRequest | ||
bool operator==(int32_t aHandle) const { return mHandle == aHandle; } | ||
bool operator<(int32_t aHandle) const { return mHandle < aHandle; } | ||
|
||
RefPtr<FrameRequestCallback> mCallback; | ||
int32_t mHandle; | ||
}; | ||
|
||
class FrameRequestManager { | ||
public: | ||
FrameRequestManager() = default; | ||
~FrameRequestManager() = default; | ||
|
||
nsresult Schedule(FrameRequestCallback& aCallback, int32_t* aHandle); | ||
bool Cancel(int32_t aHandle); | ||
|
||
bool IsEmpty() const { return mCallbacks.IsEmpty(); } | ||
|
||
bool IsCanceled(int32_t aHandle) const { | ||
return !mCanceledCallbacks.empty() && mCanceledCallbacks.has(aHandle); | ||
} | ||
|
||
void Take(nsTArray<FrameRequest>& aCallbacks) { | ||
aCallbacks = std::move(mCallbacks); | ||
mCanceledCallbacks.clear(); | ||
} | ||
|
||
void Unlink(); | ||
|
||
void Traverse(nsCycleCollectionTraversalCallback& aCB); | ||
|
||
private: | ||
nsTArray<FrameRequest> mCallbacks; | ||
|
||
// The set of frame request callbacks that were canceled but which we failed | ||
// to find in mFrameRequestCallbacks. | ||
HashSet<int32_t> mCanceledCallbacks; | ||
|
||
/** | ||
* The current frame request callback handle | ||
*/ | ||
int32_t mCallbackCounter = 0; | ||
}; | ||
|
||
inline void ImplCycleCollectionUnlink(FrameRequestManager& aField) { | ||
aField.Unlink(); | ||
} | ||
|
||
inline void ImplCycleCollectionTraverse( | ||
nsCycleCollectionTraversalCallback& aCallback, FrameRequestManager& aField, | ||
const char* aName, uint32_t aFlags) { | ||
aField.Traverse(aCallback); | ||
} | ||
|
||
} // namespace mozilla::dom | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* The origin of this IDL file is | ||
* https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animation-frames | ||
*/ | ||
|
||
callback FrameRequestCallback = void (DOMHighResTimeStamp time); | ||
|
||
interface mixin AnimationFrameProvider { | ||
[Throws] long requestAnimationFrame(FrameRequestCallback callback); | ||
[Throws] void cancelAnimationFrame(long handle); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters