Hash :
3418fe80
Author :
Date :
2018-04-09T14:08:19
Optimize Subject's binding method. Using FixedVector, we can remove the need for dynamic allocation in most of the use cases. Only when we exceed a certain amount of fixed storage do we need to spill into the heap. Bug: angleproject:2389 Bug: chromium:829906 Change-Id: Ib5f7073b58876bfd2400bd87b743bddcee5e5dc8 Reviewed-on: https://chromium-review.googlesource.com/1002884 Reviewed-by: Yuly Novikov <ynovikov@chromium.org> Reviewed-by: Luc Ferron <lucferron@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Observer:
// Implements the Observer pattern for sending state change notifications
// from Subject objects to dependent Observer objects.
//
// See design document:
// https://docs.google.com/document/d/15Edfotqg6_l1skTEL8ADQudF_oIdNa7i8Po43k6jMd4/
#ifndef LIBANGLE_OBSERVER_H_
#define LIBANGLE_OBSERVER_H_
#include "common/FixedVector.h"
#include "common/angleutils.h"
namespace gl
{
class Context;
} // namespace gl
namespace angle
{
using SubjectIndex = size_t;
enum class SubjectMessage
{
CONTENTS_CHANGED,
STORAGE_CHANGED,
DEPENDENT_DIRTY_BITS,
};
// The observing class inherits from this interface class.
class ObserverInterface
{
public:
virtual ~ObserverInterface();
virtual void onSubjectStateChange(const gl::Context *context,
SubjectIndex index,
SubjectMessage message) = 0;
};
class ObserverBinding;
// Maintains a list of observer bindings. Sends update messages to the observer.
class Subject : NonCopyable
{
public:
Subject();
virtual ~Subject();
void onStateChange(const gl::Context *context, SubjectMessage message) const;
bool hasObservers() const;
void resetObservers();
private:
// Only the ObserverBinding class should add or remove observers.
friend class ObserverBinding;
void addObserver(ObserverBinding *observer);
void removeObserver(ObserverBinding *observer);
// Keep a short list of observers so we can allocate/free them quickly. But since we support
// unlimited bindings, have a spill-over list of that uses dynamic allocation.
static constexpr size_t kMaxFixedObservers = 8;
angle::FixedVector<ObserverBinding *, kMaxFixedObservers> mFastObservers;
std::vector<ObserverBinding *> mSlowObservers;
};
// Keeps a binding between a Subject and Observer, with a specific subject index.
class ObserverBinding final
{
public:
ObserverBinding(ObserverInterface *observer, SubjectIndex index);
~ObserverBinding();
ObserverBinding(const ObserverBinding &other);
ObserverBinding &operator=(const ObserverBinding &other);
void bind(Subject *subject);
void reset();
void onStateChange(const gl::Context *context, SubjectMessage message) const;
void onSubjectReset();
const Subject *getSubject() const;
private:
Subject *mSubject;
ObserverInterface *mObserver;
SubjectIndex mIndex;
};
} // namespace angle
#endif // LIBANGLE_OBSERVER_H_