~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/ThirdParty/ANGLE/src/libANGLE/renderer/serial_utils.h

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright 2019 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
// serial_utils:
 
7
//   Utilities for generating unique IDs for resources in ANGLE.
 
8
//
 
9
 
 
10
#ifndef LIBANGLE_RENDERER_SERIAL_UTILS_H_
 
11
#define LIBANGLE_RENDERER_SERIAL_UTILS_H_
 
12
 
 
13
#include <atomic>
 
14
 
 
15
#include "common/angleutils.h"
 
16
#include "common/debug.h"
 
17
 
 
18
namespace rx
 
19
{
 
20
class ResourceSerial
 
21
{
 
22
  public:
 
23
    constexpr ResourceSerial() : mValue(kDirty) {}
 
24
    explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
 
25
    constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; }
 
26
    constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; }
 
27
 
 
28
    void dirty() { mValue = kDirty; }
 
29
    void clear() { mValue = kEmpty; }
 
30
 
 
31
    constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; }
 
32
    constexpr bool empty() const { return mValue == kEmpty; }
 
33
 
 
34
  private:
 
35
    constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
 
36
    constexpr static uintptr_t kEmpty = 0;
 
37
 
 
38
    uintptr_t mValue;
 
39
};
 
40
 
 
41
class Serial final
 
42
{
 
43
  public:
 
44
    constexpr Serial() : mValue(kInvalid) {}
 
45
    constexpr Serial(const Serial &other) = default;
 
46
    Serial &operator=(const Serial &other) = default;
 
47
 
 
48
    constexpr bool operator==(const Serial &other) const
 
49
    {
 
50
        return mValue != kInvalid && mValue == other.mValue;
 
51
    }
 
52
    constexpr bool operator==(uint32_t value) const
 
53
    {
 
54
        return mValue != kInvalid && mValue == static_cast<uint64_t>(value);
 
55
    }
 
56
    constexpr bool operator!=(const Serial &other) const
 
57
    {
 
58
        return mValue == kInvalid || mValue != other.mValue;
 
59
    }
 
60
    constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; }
 
61
    constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; }
 
62
    constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; }
 
63
    constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
 
64
 
 
65
    constexpr bool operator<(uint32_t value) const { return mValue < static_cast<uint64_t>(value); }
 
66
 
 
67
    // Useful for serialization.
 
68
    constexpr uint64_t getValue() const { return mValue; }
 
69
 
 
70
  private:
 
71
    template <typename T>
 
72
    friend class SerialFactoryBase;
 
73
    constexpr explicit Serial(uint64_t value) : mValue(value) {}
 
74
    uint64_t mValue;
 
75
    static constexpr uint64_t kInvalid = 0;
 
76
};
 
77
 
 
78
template <typename SerialBaseType>
 
79
class SerialFactoryBase final : angle::NonCopyable
 
80
{
 
81
  public:
 
82
    SerialFactoryBase() : mSerial(1) {}
 
83
 
 
84
    Serial generate()
 
85
    {
 
86
        ASSERT(mSerial + 1 > mSerial);
 
87
        return Serial(mSerial++);
 
88
    }
 
89
 
 
90
  private:
 
91
    SerialBaseType mSerial;
 
92
};
 
93
 
 
94
using SerialFactory       = SerialFactoryBase<uint64_t>;
 
95
using AtomicSerialFactory = SerialFactoryBase<std::atomic<uint64_t>>;
 
96
}  // namespace rx
 
97
 
 
98
#endif  // LIBANGLE_RENDERER_SERIAL_UTILS_H_