-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into h5vcc_system_review
- Loading branch information
Showing
9,700 changed files
with
1,219 additions
and
1,225,353 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
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,173 @@ | ||
// Copyright 2018 Google Inc. All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
#include "base/message_loop/message_pump_ui_starboard.h" | ||
|
||
#include "base/logging.h" | ||
#include "base/time/time.h" | ||
#include "starboard/event.h" | ||
#include "starboard/system.h" | ||
|
||
namespace base { | ||
|
||
namespace { | ||
|
||
void CallMessagePumpImmediate(void* context) { | ||
DCHECK(context); | ||
MessagePumpUIStarboard* pump = | ||
reinterpret_cast<MessagePumpUIStarboard*>(context); | ||
pump->CancelImmediate(); | ||
pump->RunUntilIdle(); | ||
} | ||
|
||
void CallMessagePumpDelayed(void* context) { | ||
DCHECK(context); | ||
MessagePumpUIStarboard* pump = | ||
reinterpret_cast<MessagePumpUIStarboard*>(context); | ||
pump->CancelDelayed(); | ||
pump->RunUntilIdle(); | ||
} | ||
|
||
} // namespace | ||
|
||
MessagePumpUIStarboard::MessagePumpUIStarboard() : delegate_(nullptr) {} | ||
|
||
void MessagePumpUIStarboard::CancelDelayed() { | ||
base::AutoLock auto_lock(outstanding_events_lock_); | ||
CancelDelayedLocked(); | ||
} | ||
|
||
void MessagePumpUIStarboard::CancelImmediate() { | ||
base::AutoLock auto_lock(outstanding_events_lock_); | ||
CancelImmediateLocked(); | ||
} | ||
|
||
void MessagePumpUIStarboard::RunUntilIdle() { | ||
DCHECK(delegate_); | ||
#if !defined(COBALT_BUILD_TYPE_GOLD) | ||
// Abort if this is a QA build to signal that this is unexpected. | ||
CHECK(delegate_); | ||
#endif | ||
|
||
if (should_quit()) | ||
return; | ||
|
||
for (;;) { | ||
// Do some work and see if the next task is ready right away. | ||
Delegate::NextWorkInfo next_work_info = delegate_->DoWork(); | ||
bool attempt_more_work = next_work_info.is_immediate(); | ||
|
||
if (should_quit()) | ||
break; | ||
|
||
if (attempt_more_work) | ||
continue; | ||
|
||
attempt_more_work = delegate_->DoIdleWork(); | ||
|
||
if (should_quit()) | ||
break; | ||
|
||
if (attempt_more_work) | ||
continue; | ||
|
||
// If there is delayed work. | ||
if (!next_work_info.delayed_run_time.is_max()) { | ||
ScheduleDelayedWork(next_work_info); | ||
} | ||
|
||
// Idle. | ||
break; | ||
} | ||
} | ||
|
||
void MessagePumpUIStarboard::Run(Delegate* delegate) { | ||
// This should never be called because we are not like a normal message pump | ||
// where we loop until told to quit. We are providing a MessagePump interface | ||
// on top of an externally-owned message pump. We want to exist and be able to | ||
// schedule work, but the actual for(;;) loop is owned by Starboard. | ||
NOTREACHED(); | ||
} | ||
|
||
void MessagePumpUIStarboard::Attach(Delegate* delegate) { | ||
// Since the Looper is controlled by the UI thread or JavaHandlerThread, we | ||
// can't use Run() like we do on other platforms or we would prevent Java | ||
// tasks from running. Instead we create and initialize a run loop here, then | ||
// return control back to the Looper. | ||
|
||
SetDelegate(delegate); | ||
} | ||
|
||
void MessagePumpUIStarboard::Quit() { | ||
delegate_ = nullptr; | ||
CancelAll(); | ||
} | ||
|
||
void MessagePumpUIStarboard::ScheduleWork() { | ||
// Check if outstanding event already exists. | ||
if (outstanding_event_) | ||
return; | ||
|
||
base::AutoLock auto_lock(outstanding_events_lock_); | ||
outstanding_event_ = | ||
SbEventSchedule(&CallMessagePumpImmediate, this, 0); | ||
} | ||
|
||
void MessagePumpUIStarboard::ScheduleDelayedWork( | ||
const Delegate::NextWorkInfo& next_work_info) { | ||
if (next_work_info.is_immediate() || next_work_info.delayed_run_time.is_max()) { | ||
return; | ||
} | ||
|
||
TimeDelta delay = next_work_info.remaining_delay(); | ||
if (delay.is_negative()) { | ||
delay = base::TimeDelta(); | ||
} | ||
|
||
base::AutoLock auto_lock(outstanding_events_lock_); | ||
// Make sure any outstanding delayed event is canceled. | ||
CancelDelayedLocked(); | ||
outstanding_delayed_event_ = | ||
SbEventSchedule(&CallMessagePumpDelayed, this, delay.InMicroseconds()); | ||
} | ||
|
||
void MessagePumpUIStarboard::CancelAll() { | ||
base::AutoLock auto_lock(outstanding_events_lock_); | ||
CancelImmediateLocked(); | ||
CancelDelayedLocked(); | ||
} | ||
|
||
void MessagePumpUIStarboard::CancelImmediateLocked() { | ||
outstanding_events_lock_.AssertAcquired(); | ||
if (!outstanding_event_) | ||
return; | ||
|
||
SbEventCancel(*outstanding_event_); | ||
outstanding_event_.reset(); | ||
} | ||
|
||
void MessagePumpUIStarboard::CancelDelayedLocked() { | ||
outstanding_events_lock_.AssertAcquired(); | ||
if (!outstanding_delayed_event_) | ||
return; | ||
|
||
SbEventCancel(*outstanding_delayed_event_); | ||
outstanding_delayed_event_.reset(); | ||
} | ||
|
||
MessagePump::Delegate* MessagePumpForUI::SetDelegate(Delegate* delegate) { | ||
return std::exchange(delegate_, delegate); | ||
} | ||
|
||
} // namespace base |
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,103 @@ | ||
// Copyright 2018 Google Inc. All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
#ifndef BASE_MESSAGE_PUMP_UI_STARBOARD_H_ | ||
#define BASE_MESSAGE_PUMP_UI_STARBOARD_H_ | ||
|
||
#include <set> | ||
|
||
#include "base/base_export.h" | ||
#include "base/message_loop/message_pump.h" | ||
#include "base/synchronization/lock.h" | ||
#include "base/time/time.h" | ||
#include "starboard/event.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace base { | ||
|
||
// MessagePump that integrates with the Starboard message pump. Starboard has | ||
// its own main loop, so the MessagePumpUIStarboard just piggybacks on that | ||
// rather than implementing its own pump. | ||
// | ||
// To adopt Starboard's message pump, one simply has to create a MessageLoop of | ||
// TYPE_UI from the Starboard message pump thread. A traditional place to do | ||
// this would be in the kSbEventStart event handler. One has to be sure to keep | ||
// the MessageLoop alive for as long as the application wants to use the | ||
// Starboard message pump as a MessageLoop. That would typically be for the | ||
// entire lifetime of the application, and in that case, the MessageLoop would | ||
// traditionally be deleted in the kSbEventStop handler. | ||
class BASE_EXPORT MessagePumpUIStarboard : public MessagePump { | ||
public: | ||
MessagePumpUIStarboard(); | ||
virtual ~MessagePumpUIStarboard() { Quit(); } | ||
|
||
MessagePumpUIStarboard(const MessagePumpUIStarboard&) = delete; | ||
MessagePumpUIStarboard& operator=(const MessagePumpUIStarboard&) = delete; | ||
|
||
// Cancels delayed schedule callback events. | ||
void CancelDelayed(); | ||
|
||
// Cancels immediate schedule callback events. | ||
void CancelImmediate(); | ||
|
||
// Runs one iteration of the run loop, and schedules another iteration if | ||
// necessary. | ||
void RunUntilIdle(); | ||
|
||
// --- MessagePump Implementation --- | ||
|
||
virtual void Run(Delegate* delegate) override; | ||
virtual void Quit() override; | ||
virtual void ScheduleWork() override; | ||
virtual void ScheduleDelayedWork( | ||
const Delegate::NextWorkInfo& next_work_info) override; | ||
|
||
// Attaches |delegate| to this native MessagePump. |delegate| will from then | ||
// on be invoked by the native loop to process application tasks. | ||
virtual void Attach(Delegate* delegate); | ||
|
||
protected: | ||
Delegate* SetDelegate(Delegate* delegate); | ||
|
||
private: | ||
// Cancels all outstanding scheduled callback events, if any. | ||
void CancelAll(); | ||
|
||
// Cancel workhorse that assumes |outstanding_events_lock_| is locked. | ||
void CancelImmediateLocked(); | ||
|
||
// Cancel delayed workhorse that assumes |outstanding_events_lock_| is locked. | ||
void CancelDelayedLocked(); | ||
|
||
// If the delegate has been removed, Quit() has been called. | ||
bool should_quit() const { return delegate_ == nullptr; } | ||
|
||
// The MessagePump::Delegate configured in Start(). | ||
Delegate* delegate_; | ||
|
||
// Lock protecting outstanding scheduled callback events. | ||
base::Lock outstanding_events_lock_; | ||
|
||
// The set of outstanding scheduled callback events for immediate work. | ||
absl::optional<SbEventId> outstanding_event_; | ||
|
||
// The set of outstanding scheduled callback events for delayed work. | ||
absl::optional<SbEventId> outstanding_delayed_event_; | ||
}; | ||
|
||
using MessagePumpForUI = MessagePumpUIStarboard; | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_MESSAGE_PUMP_UI_STARBOARD_H_ |
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
Oops, something went wrong.