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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
gpu / command_buffer / common / discardable_handle.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/common/discardable_handle.h"
#include "base/atomicops.h"
#include "base/logging.h"
#include "gpu/command_buffer/common/buffer.h"
namespace gpu {
namespace {
const int32_t kHandleDeleted = 0;
const int32_t kHandleUnlocked = 1;
const int32_t kHandleLockedStart = 2;
} // namespace
DiscardableHandleBase::DiscardableHandleBase(scoped_refptr<Buffer> buffer,
uint32_t byte_offset,
int32_t shm_id)
: buffer_(std::move(buffer)), byte_offset_(byte_offset), shm_id_(shm_id) {}
DiscardableHandleBase::DiscardableHandleBase(
const DiscardableHandleBase& other) = default;
DiscardableHandleBase::DiscardableHandleBase(DiscardableHandleBase&& other) =
default;
DiscardableHandleBase::~DiscardableHandleBase() = default;
DiscardableHandleBase& DiscardableHandleBase::operator=(
const DiscardableHandleBase& other) = default;
DiscardableHandleBase& DiscardableHandleBase::operator=(
DiscardableHandleBase&& other) = default;
bool DiscardableHandleBase::ValidateParameters(const Buffer* buffer,
uint32_t byte_offset) {
if (!buffer)
return false;
if (byte_offset % sizeof(base::subtle::Atomic32))
return false;
if (!buffer->GetDataAddress(byte_offset, sizeof(base::subtle::Atomic32)))
return false;
return true;
}
bool DiscardableHandleBase::IsDeletedForTracing() const {
return kHandleDeleted == base::subtle::NoBarrier_Load(AsAtomic());
}
bool DiscardableHandleBase::IsLockedForTesting() const {
return kHandleLockedStart <= base::subtle::NoBarrier_Load(AsAtomic());
}
bool DiscardableHandleBase::IsDeletedForTesting() const {
return IsDeletedForTracing();
}
scoped_refptr<Buffer> DiscardableHandleBase::BufferForTesting() const {
return buffer_;
}
volatile base::subtle::Atomic32* DiscardableHandleBase::AsAtomic() const {
return reinterpret_cast<volatile base::subtle::Atomic32*>(
buffer_->GetDataAddress(byte_offset_, sizeof(base::subtle::Atomic32)));
}
ClientDiscardableHandle::ClientDiscardableHandle()
: DiscardableHandleBase(nullptr, 0, 0) {}
ClientDiscardableHandle::ClientDiscardableHandle(scoped_refptr<Buffer> buffer,
uint32_t byte_offset,
int32_t shm_id)
: DiscardableHandleBase(std::move(buffer), byte_offset, shm_id) {
// Handle always starts locked.
base::subtle::NoBarrier_Store(AsAtomic(), kHandleLockedStart);
}
ClientDiscardableHandle::ClientDiscardableHandle(
const ClientDiscardableHandle& other) = default;
ClientDiscardableHandle::ClientDiscardableHandle(
ClientDiscardableHandle&& other) = default;
ClientDiscardableHandle& ClientDiscardableHandle::operator=(
const ClientDiscardableHandle& other) = default;
ClientDiscardableHandle& ClientDiscardableHandle::operator=(
ClientDiscardableHandle&& other) = default;
bool ClientDiscardableHandle::Lock() {
while (true) {
base::subtle::Atomic32 current_value =
base::subtle::NoBarrier_Load(AsAtomic());
if (current_value == kHandleDeleted) {
// Once a handle is deleted, it cannot be modified further.
return false;
}
base::subtle::Atomic32 new_value = current_value + 1;
// No barrier is needed, as any commands which depend on this operation
// will flow over the command buffer, which ensures a memory barrier
// between here and where these commands are executed on the GPU process.
base::subtle::Atomic32 previous_value =
base::subtle::NoBarrier_CompareAndSwap(AsAtomic(), current_value,
new_value);
if (current_value == previous_value) {
return true;
}
}
}
bool ClientDiscardableHandle::CanBeReUsed() const {
return kHandleDeleted == base::subtle::Acquire_Load(AsAtomic());
}
ServiceDiscardableHandle::ServiceDiscardableHandle()
: DiscardableHandleBase(nullptr, 0, 0) {}
ServiceDiscardableHandle::ServiceDiscardableHandle(scoped_refptr<Buffer> buffer,
uint32_t byte_offset,
int32_t shm_id)
: DiscardableHandleBase(std::move(buffer), byte_offset, shm_id) {}
ServiceDiscardableHandle::ServiceDiscardableHandle(
const ServiceDiscardableHandle& other) = default;
ServiceDiscardableHandle::ServiceDiscardableHandle(
ServiceDiscardableHandle&& other) = default;
ServiceDiscardableHandle& ServiceDiscardableHandle::operator=(
const ServiceDiscardableHandle& other) = default;
ServiceDiscardableHandle& ServiceDiscardableHandle::operator=(
ServiceDiscardableHandle&& other) = default;
void ServiceDiscardableHandle::Unlock() {
// No barrier is needed as all GPU process access happens on a single thread,
// and communication of dependent data between the GPU process and the
// renderer process happens across the command buffer and includes barriers.
// This check notifies a non-malicious caller that they've issued unbalanced
// lock/unlock calls.
DLOG_IF(ERROR, kHandleLockedStart > base::subtle::NoBarrier_Load(AsAtomic()));
base::subtle::NoBarrier_AtomicIncrement(AsAtomic(), -1);
}
bool ServiceDiscardableHandle::Delete() {
// No barrier is needed as all GPU process access happens on a single thread,
// and communication of dependent data between the GPU process and the
// renderer process happens across the command buffer and includes barriers.
return kHandleUnlocked == base::subtle::NoBarrier_CompareAndSwap(
AsAtomic(), kHandleUnlocked, kHandleDeleted);
}
void ServiceDiscardableHandle::ForceDelete() {
// No barrier is needed as all GPU process access happens on a single thread,
// and communication of dependent data between the GPU process and the
// renderer process happens across the command buffer and includes barriers.
base::subtle::NoBarrier_Store(AsAtomic(), kHandleDeleted);
}
} // namespace gpu