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

Side by Side Diff: content/browser/android/scoped_surface_request_manager.cc

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Addressed comments. Created 4 years, 3 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/android/scoped_surface_request_manager.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "crypto/random.h"
9
10 namespace content {
11
12 // static
13 ScopedSurfaceRequestManager* ScopedSurfaceRequestManager::GetInstance() {
14 return base::Singleton<
15 ScopedSurfaceRequestManager,
16 base::LeakySingletonTraits<ScopedSurfaceRequestManager>>::get();
17 }
18
19 uint64_t ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest(
20 ScopedSurfaceRequestCB request_cb) {
21 DCHECK_CURRENTLY_ON(BrowserThread::UI);
22 DCHECK(!request_cb.is_null());
23
24 uint64_t request_token;
25 crypto::RandBytes(&request_token, sizeof(request_token));
26
27 DCHECK(!request_callbacks_.count(request_token));
28 request_callbacks_.insert(std::make_pair(request_token, request_cb));
29
30 return request_token;
31 }
32
33 void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest(
34 uint64_t request_token) {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI);
36 GetAndUnregisterInternal(request_token);
37 }
38
39 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB
40 ScopedSurfaceRequestManager::GetAndUnregisterInternal(uint64_t request_token) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42
43 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request;
44
45 auto it = request_callbacks_.find(request_token);
46 if (it != request_callbacks_.end()) {
47 request = it->second;
48 request_callbacks_.erase(it);
49 }
50
51 return request;
52 }
53
54 void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest(
55 uint64_t request_token,
56 gl::SurfaceTexture* surface_texture) {
57 gl::ScopedJavaSurface surface(surface_texture);
58 FulfillScopedSurfaceRequest(request_token, std::move(surface));
59 }
60
61 void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest(
62 uint64_t request_token,
63 gl::ScopedJavaSurface surface) {
64 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
65 // base::Unretained is safe because the lifetime of this object is tied to
66 // the lifetime of the browser process.
67 BrowserThread::PostTask(
68 BrowserThread::UI, FROM_HERE,
69 base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest,
70 base::Unretained(this), request_token,
71 base::Passed(std::move(surface))));
72 return;
73 }
74
75 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request =
76 GetAndUnregisterInternal(request_token);
77
78 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.
79 request.Run(std::move(surface));
80 }
81
82 ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {}
83
84 ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {}
85
86 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698