OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_PROVIDER_H_ | 5 #ifndef CC_OUTPUT_CONTEXT_PROVIDER_H_ |
6 #define CC_OUTPUT_CONTEXT_PROVIDER_H_ | 6 #define CC_OUTPUT_CONTEXT_PROVIDER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 namespace cc { | 25 namespace cc { |
26 struct ManagedMemoryPolicy; | 26 struct ManagedMemoryPolicy; |
27 | 27 |
28 class ContextProvider : public base::RefCountedThreadSafe<ContextProvider> { | 28 class ContextProvider : public base::RefCountedThreadSafe<ContextProvider> { |
29 public: | 29 public: |
30 class ScopedContextLock { | 30 class ScopedContextLock { |
31 public: | 31 public: |
32 explicit ScopedContextLock(ContextProvider* context_provider) | 32 explicit ScopedContextLock(ContextProvider* context_provider) |
33 : context_provider_(context_provider), | 33 : context_provider_(context_provider), |
34 context_lock_(*context_provider_->GetLock()) { | 34 context_lock_(*context_provider_->GetLock()) { |
35 // Allow current thread to bind to |context_provider|. | 35 // Allow current thread to use |context_provider_|. |
36 context_provider_->DetachFromThread(); | 36 context_provider_->DetachFromThread(); |
37 } | 37 } |
38 ~ScopedContextLock() { | 38 ~ScopedContextLock() { |
39 // Allow a different thread to bind to |context_provider|. | 39 // Allow usage by thread for which |context_provider_| is bound to. |
40 context_provider_->DetachFromThread(); | 40 context_provider_->DetachFromThread(); |
41 } | 41 } |
42 | 42 |
| 43 // Returns a raw pointer to the GLES2Interface or null if context has been |
| 44 // destroyed. |
43 gpu::gles2::GLES2Interface* ContextGL() { | 45 gpu::gles2::GLES2Interface* ContextGL() { |
44 return context_provider_->ContextGL(); | 46 return context_provider_->HasBeenDestroyed() |
| 47 ? nullptr |
| 48 : context_provider_->ContextGL(); |
45 } | 49 } |
46 | 50 |
47 private: | 51 private: |
48 ContextProvider* const context_provider_; | 52 ContextProvider* const context_provider_; |
49 base::AutoLock context_lock_; | 53 base::AutoLock context_lock_; |
50 }; | 54 }; |
51 // Bind the 3d context to the current thread. This should be called before | 55 // Bind the 3d context to the current thread. This should be called before |
52 // accessing the contexts. Calling it more than once should have no effect. | 56 // accessing the contexts. Calling it more than once should have no effect. |
53 // Once this function has been called, the class should only be accessed | 57 // Once this function has been called, the class should only be accessed |
54 // from the same thread. | 58 // from the same thread unless the function has some explicitly specified |
| 59 // rules for access on a different thread. See SetupLockOnMainThread(), which |
| 60 // can be used to provide access from multiple threads. |
55 virtual bool BindToCurrentThread() = 0; | 61 virtual bool BindToCurrentThread() = 0; |
56 virtual void DetachFromThread() {} | 62 virtual void DetachFromThread() {} |
57 | 63 |
58 virtual gpu::gles2::GLES2Interface* ContextGL() = 0; | 64 virtual gpu::gles2::GLES2Interface* ContextGL() = 0; |
59 virtual gpu::ContextSupport* ContextSupport() = 0; | 65 virtual gpu::ContextSupport* ContextSupport() = 0; |
60 virtual class GrContext* GrContext() = 0; | 66 virtual class GrContext* GrContext() = 0; |
61 | 67 |
62 struct Capabilities { | 68 struct Capabilities { |
63 gpu::Capabilities gpu; | 69 gpu::Capabilities gpu; |
64 size_t max_transfer_buffer_usage_bytes; | 70 size_t max_transfer_buffer_usage_bytes; |
65 | 71 |
66 CC_EXPORT Capabilities(); | 72 CC_EXPORT Capabilities(); |
67 }; | 73 }; |
68 | 74 |
69 // Invalidates the cached OpenGL state in GrContext. | 75 // Invalidates the cached OpenGL state in GrContext. |
70 // See skia GrContext::resetContext for details. | 76 // See skia GrContext::resetContext for details. |
71 virtual void InvalidateGrContext(uint32_t state) = 0; | 77 virtual void InvalidateGrContext(uint32_t state) = 0; |
72 | 78 |
73 // Sets up a lock so this context can be used from multiple threads. | 79 // Sets up a lock so this context can be used from multiple threads. After |
| 80 // calling this, all functions without explicit thread usage constraints can |
| 81 // be used on any thread while the lock returned by GetLock() is acquired. |
74 virtual void SetupLock() = 0; | 82 virtual void SetupLock() = 0; |
75 | 83 |
76 // Returns the lock that should be held if using this context from multiple | 84 // Returns the lock that should be held if using this context from multiple |
77 // threads. | 85 // threads. This can be called on any thread. |
78 virtual base::Lock* GetLock() = 0; | 86 virtual base::Lock* GetLock() = 0; |
79 | 87 |
80 // Returns the capabilities of the currently bound 3d context. | 88 // Returns the capabilities of the currently bound 3d context. |
81 virtual Capabilities ContextCapabilities() = 0; | 89 virtual Capabilities ContextCapabilities() = 0; |
82 | 90 |
83 // Ask the provider to check if the contexts are valid or lost. If they are, | 91 // Ask the provider to check if the contexts are valid or lost. If they are, |
84 // this should invalidate the provider so that it can be replaced with a new | 92 // this should invalidate the provider so that it can be replaced with a new |
85 // one. | 93 // one. |
86 virtual void VerifyContexts() = 0; | 94 virtual void VerifyContexts() = 0; |
87 | 95 |
88 // Delete all cached gpu resources. | 96 // Delete all cached gpu resources. |
89 virtual void DeleteCachedResources() = 0; | 97 virtual void DeleteCachedResources() = 0; |
90 | 98 |
91 // A method to be called from the main thread that should return true if | 99 // A method to be called from the main thread that should return true if |
92 // the context inside the provider is no longer valid. | 100 // the context inside the provider has been lost. |
93 virtual bool DestroyedOnMainThread() = 0; | 101 virtual bool HasBeenLostOnMainThread() = 0; |
94 | 102 |
95 // Sets a callback to be called when the context is lost. This should be | 103 // Sets a callback to be called when the context is lost. This should be |
96 // called from the same thread that the context is bound to. To avoid races, | 104 // called from the same thread that the context is bound to. To avoid races, |
97 // it should be called before BindToCurrentThread(), or VerifyContexts() | 105 // it should be called before BindToCurrentThread(), or VerifyContexts() |
98 // should be called after setting the callback. | 106 // should be called after setting the callback. |
99 typedef base::Closure LostContextCallback; | 107 typedef base::Closure LostContextCallback; |
100 virtual void SetLostContextCallback( | 108 virtual void SetLostContextCallback( |
101 const LostContextCallback& lost_context_callback) = 0; | 109 const LostContextCallback& lost_context_callback) = 0; |
102 | 110 |
103 // Sets a callback to be called when the memory policy changes. This should be | 111 // Sets a callback to be called when the memory policy changes. This should be |
104 // called from the same thread that the context is bound to. | 112 // called from the same thread that the context is bound to. |
105 typedef base::Callback<void(const ManagedMemoryPolicy& policy)> | 113 typedef base::Callback<void(const ManagedMemoryPolicy& policy)> |
106 MemoryPolicyChangedCallback; | 114 MemoryPolicyChangedCallback; |
107 virtual void SetMemoryPolicyChangedCallback( | 115 virtual void SetMemoryPolicyChangedCallback( |
108 const MemoryPolicyChangedCallback& memory_policy_changed_callback) = 0; | 116 const MemoryPolicyChangedCallback& memory_policy_changed_callback) = 0; |
109 | 117 |
| 118 // Return true if the context inside the provider has been destroyed. |
| 119 virtual bool HasBeenDestroyed() = 0; |
| 120 |
110 protected: | 121 protected: |
111 friend class base::RefCountedThreadSafe<ContextProvider>; | 122 friend class base::RefCountedThreadSafe<ContextProvider>; |
112 virtual ~ContextProvider() {} | 123 virtual ~ContextProvider() {} |
113 }; | 124 }; |
114 | 125 |
115 } // namespace cc | 126 } // namespace cc |
116 | 127 |
117 #endif // CC_OUTPUT_CONTEXT_PROVIDER_H_ | 128 #endif // CC_OUTPUT_CONTEXT_PROVIDER_H_ |
OLD | NEW |