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
19 namespace angle
20 {
21 template <typename HaystackT, typename NeedleT>
IsInContainer(const HaystackT & haystack,const NeedleT & needle)22 bool IsInContainer(const HaystackT &haystack, const NeedleT &needle)
23 {
24 return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
25 }
26
27 using SubjectIndex = size_t;
28
29 // Messages are used to distinguish different Subject events that get sent to a single Observer.
30 // It could be possible to improve the handling by using different callback functions instead
31 // of a single handler function. But in some cases we want to share a single binding between
32 // Observer and Subject and handle different types of events.
33 enum class SubjectMessage
34 {
35 // Used by gl::VertexArray to notify gl::Context of a gl::Buffer binding count change. Triggers
36 // a validation cache update. Also used by gl::Texture to notify gl::Framebuffer of loops.
37 BindingChanged,
38
39 // Only the contents (pixels, bytes, etc) changed in this Subject. Distinct from the object
40 // storage.
41 ContentsChanged,
42
43 // Sent by gl::Sampler, gl::Texture, gl::Framebuffer and others to notifiy gl::Context. This
44 // flag indicates to call syncState before next use.
45 DirtyBitsFlagged,
46
47 // Generic state change message. Used in multiple places for different purposes.
48 SubjectChanged,
49
50 // Indicates a bound gl::Buffer is now mapped or unmapped. Passed from gl::Buffer, through
51 // gl::VertexArray, into gl::Context. Used to track validation.
52 SubjectMapped,
53 SubjectUnmapped,
54 };
55
56 // The observing class inherits from this interface class.
57 class ObserverInterface
58 {
59 public:
60 virtual ~ObserverInterface();
61 virtual void onSubjectStateChange(SubjectIndex index, SubjectMessage message) = 0;
62 };
63
64 class ObserverBindingBase
65 {
66 public:
ObserverBindingBase(ObserverInterface * observer,SubjectIndex subjectIndex)67 ObserverBindingBase(ObserverInterface *observer, SubjectIndex subjectIndex)
68 : mObserver(observer), mIndex(subjectIndex)
69 {}
~ObserverBindingBase()70 virtual ~ObserverBindingBase() {}
71
getObserver()72 ObserverInterface *getObserver() const { return mObserver; }
getSubjectIndex()73 SubjectIndex getSubjectIndex() const { return mIndex; }
74
onSubjectReset()75 virtual void onSubjectReset() {}
76
77 private:
78 ObserverInterface *mObserver;
79 SubjectIndex mIndex;
80 };
81
82 // Maintains a list of observer bindings. Sends update messages to the observer.
83 class Subject : NonCopyable
84 {
85 public:
86 Subject();
87 virtual ~Subject();
88
89 void onStateChange(SubjectMessage message) const;
90 bool hasObservers() const;
91 void resetObservers();
92
addObserver(ObserverBindingBase * observer)93 ANGLE_INLINE void addObserver(ObserverBindingBase *observer)
94 {
95 ASSERT(!IsInContainer(mObservers, observer));
96 mObservers.push_back(observer);
97 }
98
removeObserver(ObserverBindingBase * observer)99 ANGLE_INLINE void removeObserver(ObserverBindingBase *observer)
100 {
101 ASSERT(IsInContainer(mObservers, observer));
102 mObservers.remove_and_permute(observer);
103 }
104
105 private:
106 // Keep a short list of observers so we can allocate/free them quickly. But since we support
107 // unlimited bindings, have a spill-over list of that uses dynamic allocation.
108 static constexpr size_t kMaxFixedObservers = 8;
109 angle::FastVector<ObserverBindingBase *, kMaxFixedObservers> mObservers;
110 };
111
112 // Keeps a binding between a Subject and Observer, with a specific subject index.
113 class ObserverBinding final : public ObserverBindingBase
114 {
115 public:
116 ObserverBinding(ObserverInterface *observer, SubjectIndex index);
117 ~ObserverBinding() override;
118 ObserverBinding(const ObserverBinding &other);
119 ObserverBinding &operator=(const ObserverBinding &other);
120
121 void bind(Subject *subject);
122
reset()123 ANGLE_INLINE void reset() { bind(nullptr); }
124
125 void onStateChange(SubjectMessage message) const;
126 void onSubjectReset() override;
127
getSubject()128 ANGLE_INLINE const Subject *getSubject() const { return mSubject; }
129
assignSubject(Subject * subject)130 ANGLE_INLINE void assignSubject(Subject *subject) { mSubject = subject; }
131
132 private:
133 Subject *mSubject;
134 };
135
136 } // namespace angle
137
138 #endif // LIBANGLE_OBSERVER_H_
139