1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // Observer:
7 // Implements the Observer pattern for sending state change notifications
8 // from Subject objects to dependent Observer objects.
9 //
10 // See design document:
11 // https://docs.google.com/document/d/15Edfotqg6_l1skTEL8ADQudF_oIdNa7i8Po43k6jMd4/
12
13 #ifndef LIBANGLE_OBSERVER_H_
14 #define LIBANGLE_OBSERVER_H_
15
16 #include "common/FastVector.h"
17 #include "common/angleutils.h"
18 #include "libANGLE/Constants.h"
19
20 namespace angle
21 {
22 template <typename HaystackT, typename NeedleT>
IsInContainer(const HaystackT & haystack,const NeedleT & needle)23 bool IsInContainer(const HaystackT &haystack, const NeedleT &needle)
24 {
25 return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
26 }
27
28 using SubjectIndex = size_t;
29
30 // Messages are used to distinguish different Subject events that get sent to a single Observer.
31 // It could be possible to improve the handling by using different callback functions instead
32 // of a single handler function. But in some cases we want to share a single binding between
33 // Observer and Subject and handle different types of events.
34 enum class SubjectMessage
35 {
36 // Used by gl::VertexArray to notify gl::Context of a gl::Buffer binding count change. Triggers
37 // a validation cache update. Also used by gl::Texture to notify gl::Framebuffer of loops.
38 BindingChanged,
39
40 // Only the contents (pixels, bytes, etc) changed in this Subject. Distinct from the object
41 // storage.
42 ContentsChanged,
43
44 // Sent by gl::Sampler, gl::Texture, gl::Framebuffer and others to notifiy gl::Context. This
45 // flag indicates to call syncState before next use.
46 DirtyBitsFlagged,
47
48 // Generic state change message. Used in multiple places for different purposes.
49 SubjectChanged,
50
51 // Indicates a bound gl::Buffer is now mapped or unmapped. Passed from gl::Buffer, through
52 // gl::VertexArray, into gl::Context. Used to track validation.
53 SubjectMapped,
54 SubjectUnmapped,
55 // Indicates a bound buffer's storage was reallocated due to glBufferData call or optimizations
56 // to prevent having to flush pending commands and waiting for the GPU to become idle.
57 InternalMemoryAllocationChanged,
58
59 // Indicates an external change to the default framebuffer.
60 SurfaceChanged,
61 // Indicates the system framebuffer's swapchain changed, i.e. color buffer changed but no
62 // depth/stencil buffer change.
63 SwapchainImageChanged,
64
65 // Indicates a separable program's textures or images changed in the ProgramExecutable.
66 ProgramTextureOrImageBindingChanged,
67 // Indicates a program or pipeline is being re-linked. This is used to make sure the Context or
68 // ProgramPipeline that reference the program/pipeline wait for it to finish linking.
69 ProgramUnlinked,
70 // Indicates a program or pipeline was successfully re-linked.
71 ProgramRelinked,
72 // Indicates a separable program's sampler uniforms were updated.
73 SamplerUniformsUpdated,
74 // Indicates a program's uniform block binding has changed (one message per binding)
75 ProgramUniformBlockBindingZeroUpdated,
76 ProgramUniformBlockBindingLastUpdated = ProgramUniformBlockBindingZeroUpdated +
77 gl::IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS -
78 1,
79
80 // Indicates a Storage of back-end in gl::Texture has been released.
81 StorageReleased,
82
83 // Sent when the GLuint ID for a gl::Texture is being deleted via glDeleteTextures. The
84 // texture may stay alive due to orphaning, but will no longer be directly accessible by the GL
85 // API.
86 TextureIDDeleted,
87
88 // Indicates that all pending updates are complete in the subject.
89 InitializationComplete,
90
91 // Indicates a change in foveated rendering state in the subject.
92 FoveatedRenderingStateChanged,
93 };
94
IsProgramUniformBlockBindingUpdatedMessage(SubjectMessage message)95 inline bool IsProgramUniformBlockBindingUpdatedMessage(SubjectMessage message)
96 {
97 return message >= SubjectMessage::ProgramUniformBlockBindingZeroUpdated &&
98 message <= SubjectMessage::ProgramUniformBlockBindingLastUpdated;
99 }
ProgramUniformBlockBindingUpdatedMessageFromIndex(uint32_t blockIndex)100 inline SubjectMessage ProgramUniformBlockBindingUpdatedMessageFromIndex(uint32_t blockIndex)
101 {
102 return static_cast<SubjectMessage>(
103 static_cast<uint32_t>(SubjectMessage::ProgramUniformBlockBindingZeroUpdated) + blockIndex);
104 }
ProgramUniformBlockBindingUpdatedMessageToIndex(SubjectMessage message)105 inline uint32_t ProgramUniformBlockBindingUpdatedMessageToIndex(SubjectMessage message)
106 {
107 return static_cast<uint32_t>(message) -
108 static_cast<uint32_t>(SubjectMessage::ProgramUniformBlockBindingZeroUpdated);
109 }
110
111 // The observing class inherits from this interface class.
112 class ObserverInterface
113 {
114 public:
115 virtual ~ObserverInterface();
116 virtual void onSubjectStateChange(SubjectIndex index, SubjectMessage message) = 0;
117 };
118
119 class ObserverBindingBase
120 {
121 public:
ObserverBindingBase(ObserverInterface * observer,SubjectIndex subjectIndex)122 ObserverBindingBase(ObserverInterface *observer, SubjectIndex subjectIndex)
123 : mObserver(observer), mIndex(subjectIndex)
124 {}
~ObserverBindingBase()125 virtual ~ObserverBindingBase() {}
126
127 ObserverBindingBase(const ObserverBindingBase &other) = default;
128 ObserverBindingBase &operator=(const ObserverBindingBase &other) = default;
129
getObserver()130 ObserverInterface *getObserver() const { return mObserver; }
getSubjectIndex()131 SubjectIndex getSubjectIndex() const { return mIndex; }
132
onSubjectReset()133 virtual void onSubjectReset() {}
134
135 private:
136 ObserverInterface *mObserver;
137 SubjectIndex mIndex;
138 };
139
140 constexpr size_t kMaxFixedObservers = 8;
141
142 // Maintains a list of observer bindings. Sends update messages to the observer.
143 class Subject : NonCopyable
144 {
145 public:
146 Subject();
147 virtual ~Subject();
148
149 void onStateChange(SubjectMessage message) const;
150 bool hasObservers() const;
151 void resetObservers();
getObserversCount()152 ANGLE_INLINE size_t getObserversCount() const { return mObservers.size(); }
153
addObserver(ObserverBindingBase * observer)154 ANGLE_INLINE void addObserver(ObserverBindingBase *observer)
155 {
156 ASSERT(!IsInContainer(mObservers, observer));
157 mObservers.push_back(observer);
158 }
removeObserver(ObserverBindingBase * observer)159 ANGLE_INLINE void removeObserver(ObserverBindingBase *observer)
160 {
161 ASSERT(IsInContainer(mObservers, observer));
162 mObservers.remove_and_permute(observer);
163 }
164
165 private:
166 // Keep a short list of observers so we can allocate/free them quickly. But since we support
167 // unlimited bindings, have a spill-over list of that uses dynamic allocation.
168 angle::FastVector<ObserverBindingBase *, kMaxFixedObservers> mObservers;
169 };
170
171 // Keeps a binding between a Subject and Observer, with a specific subject index.
172 class ObserverBinding final : public ObserverBindingBase
173 {
174 public:
175 ObserverBinding();
176 ObserverBinding(ObserverInterface *observer, SubjectIndex index);
177 ~ObserverBinding() override;
178 ObserverBinding(const ObserverBinding &other);
179 ObserverBinding &operator=(const ObserverBinding &other);
180
181 void bind(Subject *subject);
182
reset()183 ANGLE_INLINE void reset() { bind(nullptr); }
184
185 void onStateChange(SubjectMessage message) const;
186 void onSubjectReset() override;
187
getSubject()188 ANGLE_INLINE const Subject *getSubject() const { return mSubject; }
189
assignSubject(Subject * subject)190 ANGLE_INLINE void assignSubject(Subject *subject) { mSubject = subject; }
191
192 private:
193 Subject *mSubject;
194 };
195
196 } // namespace angle
197
198 #endif // LIBANGLE_OBSERVER_H_
199