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

Side by Side Diff: cc/output/context_cache_controller.h

Issue 2353033003: Idle cleanup for worker context (Closed)
Patch Set: Feedback and threading cleanup 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
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_ 5 #ifndef CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_
6 #define CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_ 6 #define CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_
7 7
8 #include <cstdint> 8 #include <cstdint>
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
13 #include "cc/base/cc_export.h" 14 #include "cc/base/cc_export.h"
14 15
15 class GrContext; 16 class GrContext;
16 17
17 namespace base { 18 namespace base {
19 class Lock;
18 class SingleThreadTaskRunner; 20 class SingleThreadTaskRunner;
19 } 21 }
20 22
21 namespace gpu { 23 namespace gpu {
22 class ContextSupport; 24 class ContextSupport;
23 } 25 }
24 26
25 namespace cc { 27 namespace cc {
26 28
27 // ContextCacheController manages clearing cached data on ContextProvider when 29 // ContextCacheController manages clearing cached data on ContextProvider when
28 // appropriate. Currently, cache clearing happens when the ContextProvider 30 // appropriate. Currently, cache clearing is triggered when the Context
29 // transitions from visible to not visible. As a ContextProvider may have 31 // provider transitions from Visible to Not Visible, or from Busy to Idle. As a
30 // multiple clients, ContextCacheController tracks visibility across all 32 // ContextProvider may have multiple clients, ContextCacheController tracks
31 // clients and only cleans up when appropriate. 33 // visibility and idle status across all clients and only cleans up when
32 // 34 // appropriate.
33 // Note: Virtuals on this function are for testing only. This function is not
34 // designed to have multiple implementations.
35 class CC_EXPORT ContextCacheController { 35 class CC_EXPORT ContextCacheController {
36 public: 36 public:
37 class CC_EXPORT ScopedVisibility { 37 class CC_EXPORT ScopedToken {
38 public: 38 public:
39 ~ScopedVisibility(); 39 ~ScopedToken();
40 40
41 private: 41 private:
42 friend class ContextCacheController; 42 friend class ContextCacheController;
43 ScopedVisibility(); 43 ScopedToken();
44 void Release(); 44 void Release();
45 45
46 bool released_ = false; 46 bool released_ = false;
47 }; 47 };
48 using ScopedVisibility = ScopedToken;
49 using ScopedBusy = ScopedToken;
48 50
49 ContextCacheController( 51 ContextCacheController(
50 gpu::ContextSupport* context_support, 52 gpu::ContextSupport* context_support,
51 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 53 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
52 virtual ~ContextCacheController(); 54 virtual ~ContextCacheController();
53 55
54 void SetGrContext(GrContext* gr_context); 56 void SetGrContext(GrContext* gr_context);
57 void SetLock(base::Lock* lock);
55 58
56 // Clients of the owning ContextProvider should call this function when they 59 // Clients of the owning ContextProvider should call this function when they
57 // become visible. The returned ScopedVisibility pointer must be passed back 60 // become visible. The returned ScopedVisibility pointer must be passed back
58 // to ClientBecameNotVisible or it will DCHECK in its destructor. 61 // to ClientBecameNotVisible or it will DCHECK in its destructor.
59 virtual std::unique_ptr<ScopedVisibility> ClientBecameVisible(); 62 virtual std::unique_ptr<ScopedVisibility> ClientBecameVisible();
60 63
61 // When a client becomes not visible (either due to a visibility change or 64 // When a client becomes not visible (either due to a visibility change or
62 // because it is being deleted), it must pass back any ScopedVisibility 65 // because it is being deleted), it must pass back any ScopedVisibility
63 // pointers it owns via this function. 66 // pointers it owns via this function.
64 virtual void ClientBecameNotVisible( 67 virtual void ClientBecameNotVisible(
65 std::unique_ptr<ScopedVisibility> scoped_visibility); 68 std::unique_ptr<ScopedVisibility> scoped_visibility);
66 69
67 protected: 70 // Clients of the owning ContextProvider may call this function when they
68 std::unique_ptr<ScopedVisibility> CreateScopedVisibilityForTesting() const; 71 // become busy. The returned ScopedBusy pointer must be passed back
69 void ReleaseScopedVisibilityForTesting( 72 // to ClientBecameNotBusy or it will DCHECK in its destructor.
70 std::unique_ptr<ScopedVisibility> scoped_visibility) const; 73 std::unique_ptr<ScopedBusy> ClientBecameBusy();
74
75 // When a client becomes not busy, it must pass back any ScopedBusy
76 // pointers it owns via this function.
77 void ClientBecameNotBusy(std::unique_ptr<ScopedBusy> scoped_busy);
71 78
72 private: 79 private:
80 void OnIdle(uint32_t idle_generation);
81 void PostIdleCallbackWithIdleGenerationLockAcquired();
82 void InvalidatePendingIdleCallbacks();
83
73 gpu::ContextSupport* context_support_; 84 gpu::ContextSupport* context_support_;
74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 85 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
75 GrContext* gr_context_ = nullptr; 86 GrContext* gr_context_ = nullptr;
76 87
88 // If set, |context_lock_| must be held before accessing any member within
89 // the idle callback. Exceptions to this are |current_idle_generation_|,
90 // which has its own lock, and weak_ptr_ and task_runner_, which may be
91 // accessed from multiple threads without locking.
92 base::Lock* context_lock_ = nullptr;
93
77 uint32_t num_clients_visible_ = 0; 94 uint32_t num_clients_visible_ = 0;
95 uint32_t num_clients_busy_ = 0;
96 bool callback_pending_ = false;
97
98 // |current_idle_generation_lock_| must be held when accessing
99 // |current_idle_generation_|. |current_idle_generation_lock_| must never be
100 // held while acquiring |context_lock_|.
101 base::Lock current_idle_generation_lock_;
102 uint32_t current_idle_generation_ = 0;
103
104 base::WeakPtr<ContextCacheController> weak_ptr_;
105 base::WeakPtrFactory<ContextCacheController> weak_factory_;
78 }; 106 };
79 107
80 } // namespace cc 108 } // namespace cc
81 109
82 #endif // CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_ 110 #endif // CC_OUTPUT_CONTEXT_CACHE_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698