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
62 // Indicates a separable program's textures or images changed in the ProgramExecutable.
63 ProgramTextureOrImageBindingChanged,
64 // Indicates a program or pipeline is being re-linked. This is used to make sure the Context or
65 // ProgramPipeline that reference the program/pipeline wait for it to finish linking.
66 ProgramUnlinked,
67 // Indicates a program or pipeline was successfully re-linked.
68 ProgramRelinked,
69 // Indicates a separable program's sampler uniforms were updated.
70 SamplerUniformsUpdated,
71 // Indicates a program's uniform block binding has changed (one message per binding)
72 ProgramUniformBlockBindingZeroUpdated,
73 ProgramUniformBlockBindingLastUpdated = ProgramUniformBlockBindingZeroUpdated +
74 gl::IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS -
75 1,
76
77 // Indicates a Storage of back-end in gl::Texture has been released.
78 StorageReleased,
79
80 // Sent when the GLuint ID for a gl::Texture is being deleted via glDeleteTextures. The
81 // texture may stay alive due to orphaning, but will no longer be directly accessible by the GL
82 // API.
83 TextureIDDeleted,
84
85 // Indicates that all pending updates are complete in the subject.
86 InitializationComplete,
87
88 // Indicates a change in foveated rendering state in the subject.
89 FoveatedRenderingStateChanged,
90 };
91
IsProgramUniformBlockBindingUpdatedMessage(SubjectMessage message)92 inline bool IsProgramUniformBlockBindingUpdatedMessage(SubjectMessage message)
93 {
94 return message >= SubjectMessage::ProgramUniformBlockBindingZeroUpdated &&
95 message <= SubjectMessage::ProgramUniformBlockBindingLastUpdated;
96 }
ProgramUniformBlockBindingUpdatedMessageFromIndex(uint32_t blockIndex)97 inline SubjectMessage ProgramUniformBlockBindingUpdatedMessageFromIndex(uint32_t blockIndex)
98 {
99 return static_cast<SubjectMessage>(
100 static_cast<uint32_t>(SubjectMessage::ProgramUniformBlockBindingZeroUpdated) + blockIndex);
101 }
ProgramUniformBlockBindingUpdatedMessageToIndex(SubjectMessage message)102 inline uint32_t ProgramUniformBlockBindingUpdatedMessageToIndex(SubjectMessage message)
103 {
104 return static_cast<uint32_t>(message) -
105 static_cast<uint32_t>(SubjectMessage::ProgramUniformBlockBindingZeroUpdated);
106 }
107
108 // The observing class inherits from this interface class.
109 class ObserverInterface
110 {
111 public:
112 virtual ~ObserverInterface();
113 virtual void onSubjectStateChange(SubjectIndex index, SubjectMessage message) = 0;
114 };
115
116 class ObserverBindingBase
117 {
118 public:
ObserverBindingBase(ObserverInterface * observer,SubjectIndex subjectIndex)119 ObserverBindingBase(ObserverInterface *observer, SubjectIndex subjectIndex)
120 : mObserver(observer), mIndex(subjectIndex)
121 {}
~ObserverBindingBase()122 virtual ~ObserverBindingBase() {}
123
124 ObserverBindingBase(const ObserverBindingBase &other) = default;
125 ObserverBindingBase &operator=(const ObserverBindingBase &other) = default;
126
getObserver()127 ObserverInterface *getObserver() const { return mObserver; }
getSubjectIndex()128 SubjectIndex getSubjectIndex() const { return mIndex; }
129
onSubjectReset()130 virtual void onSubjectReset() {}
131
132 private:
133 ObserverInterface *mObserver;
134 SubjectIndex mIndex;
135 };
136
137 constexpr size_t kMaxFixedObservers = 8;
138
139 // Maintains a list of observer bindings. Sends update messages to the observer.
140 class Subject : NonCopyable
141 {
142 public:
143 Subject();
144 virtual ~Subject();
145
146 void onStateChange(SubjectMessage message) const;
147 bool hasObservers() const;
148 void resetObservers();
getObserversCount()149 ANGLE_INLINE size_t getObserversCount() const { return mObservers.size(); }
150
addObserver(ObserverBindingBase * observer)151 ANGLE_INLINE void addObserver(ObserverBindingBase *observer)
152 {
153 ASSERT(!IsInContainer(mObservers, observer));
154 mObservers.push_back(observer);
155 }
removeObserver(ObserverBindingBase * observer)156 ANGLE_INLINE void removeObserver(ObserverBindingBase *observer)
157 {
158 ASSERT(IsInContainer(mObservers, observer));
159 mObservers.remove_and_permute(observer);
160 }
161
162 private:
163 // Keep a short list of observers so we can allocate/free them quickly. But since we support
164 // unlimited bindings, have a spill-over list of that uses dynamic allocation.
165 angle::FastVector<ObserverBindingBase *, kMaxFixedObservers> mObservers;
166 };
167
168 // Keeps a binding between a Subject and Observer, with a specific subject index.
169 class ObserverBinding final : public ObserverBindingBase
170 {
171 public:
172 ObserverBinding();
173 ObserverBinding(ObserverInterface *observer, SubjectIndex index);
174 ~ObserverBinding() override;
175 ObserverBinding(const ObserverBinding &other);
176 ObserverBinding &operator=(const ObserverBinding &other);
177
178 void bind(Subject *subject);
179
reset()180 ANGLE_INLINE void reset() { bind(nullptr); }
181
182 void onStateChange(SubjectMessage message) const;
183 void onSubjectReset() override;
184
getSubject()185 ANGLE_INLINE const Subject *getSubject() const { return mSubject; }
186
assignSubject(Subject * subject)187 ANGLE_INLINE void assignSubject(Subject *subject) { mSubject = subject; }
188
189 private:
190 Subject *mSubject;
191 };
192
193 } // namespace angle
194
195 #endif // LIBANGLE_OBSERVER_H_
196