Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Unified Diff: content/browser/android/scoped_surface_request_manager.cc

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Addressed comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/android/scoped_surface_request_manager.cc
diff --git a/content/browser/android/scoped_surface_request_manager.cc b/content/browser/android/scoped_surface_request_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..51d6bd38a3b58eddc25b5de6e8058abc322c33cb
--- /dev/null
+++ b/content/browser/android/scoped_surface_request_manager.cc
@@ -0,0 +1,86 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/android/scoped_surface_request_manager.h"
+
+#include "content/public/browser/browser_thread.h"
+#include "crypto/random.h"
+
+namespace content {
+
+// static
+ScopedSurfaceRequestManager* ScopedSurfaceRequestManager::GetInstance() {
+ return base::Singleton<
+ ScopedSurfaceRequestManager,
+ base::LeakySingletonTraits<ScopedSurfaceRequestManager>>::get();
+}
+
+uint64_t ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest(
+ ScopedSurfaceRequestCB request_cb) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(!request_cb.is_null());
+
+ uint64_t request_token;
+ crypto::RandBytes(&request_token, sizeof(request_token));
+
+ DCHECK(!request_callbacks_.count(request_token));
+ request_callbacks_.insert(std::make_pair(request_token, request_cb));
+
+ return request_token;
+}
+
+void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest(
+ uint64_t request_token) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ GetAndUnregisterInternal(request_token);
+}
+
+ScopedSurfaceRequestManager::ScopedSurfaceRequestCB
+ScopedSurfaceRequestManager::GetAndUnregisterInternal(uint64_t request_token) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+ ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request;
+
+ auto it = request_callbacks_.find(request_token);
+ if (it != request_callbacks_.end()) {
+ request = it->second;
+ request_callbacks_.erase(it);
+ }
+
+ return request;
+}
+
+void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest(
+ uint64_t request_token,
+ gl::SurfaceTexture* surface_texture) {
+ gl::ScopedJavaSurface surface(surface_texture);
+ FulfillScopedSurfaceRequest(request_token, std::move(surface));
+}
+
+void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest(
+ uint64_t request_token,
+ gl::ScopedJavaSurface surface) {
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ // base::Unretained is safe because the lifetime of this object is tied to
+ // the lifetime of the browser process.
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest,
+ base::Unretained(this), request_token,
+ base::Passed(std::move(surface))));
+ return;
+ }
+
+ ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request =
+ GetAndUnregisterInternal(request_token);
+
+ DCHECK(!request_cb.is_null());
watk 2016/08/31 18:12:14 Yeah, I think you were right that this one should
tguilbert 2016/08/31 20:44:44 Done.
+ request.Run(std::move(surface));
+}
+
+ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {}
+
+ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698