Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: gpu/command_buffer/common/discardable_handle.cc

Issue 2732223004: DiscardableHandle Implementation (Closed)
Patch Set: byte offset Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gpu/command_buffer/common/discardable_handle.h"
6 #include "base/atomicops.h"
7
8 namespace gpu {
9 namespace {
10 const uint32_t kHandleDeleted = 0;
11 const uint32_t kHandleUnlocked = 1;
12 const uint32_t kHandleLockedStart = 2;
13
14 } // namespace
15
16 void DiscardableHandle::InitializeWithNewData(scoped_refptr<Buffer> buffer,
17 uint32_t byte_offset,
18 int32_t shm_id) {
19 buffer_ = std::move(buffer);
20 byte_offset_ = byte_offset;
21 shm_id_ = shm_id;
22
23 // Handle always starts locked.
24 base::subtle::NoBarrier_Store(AsAtomic(), kHandleLockedStart);
25 }
26
27 void DiscardableHandle::InitializeWithExistingData(scoped_refptr<Buffer> buffer,
28 uint32_t byte_offset,
29 int32_t shm_id) {
30 buffer_ = std::move(buffer);
31 byte_offset_ = byte_offset;
32 shm_id_ = shm_id;
33 }
34
35 void DiscardableHandle::Unlock() {
36 DCHECK(base::subtle::NoBarrier_Load(AsAtomic()) >= kHandleLockedStart);
piman 2017/03/20 23:51:44 Will this be called by the service? If so, a malic
ericrk 2017/03/27 22:58:13 True, I wasn't so concerned with the DCHECK, as it
piman 2017/03/28 00:43:00 Generally, I prefer avoiding service-side DCHECKs,
ericrk 2017/03/28 01:12:10 Fair enough, I'll remove the DCHECK. It was more t
ericrk 2017/03/28 01:19:44 hmm, plus, a malicious client could already just d
piman 2017/03/28 21:47:25 Right, if the worse that could happen is that the
37 // Barrier prevents gl operations from being re-ordered across Unlock.
38 base::subtle::Barrier_AtomicIncrement(AsAtomic(), -1);
piman 2017/03/20 23:51:44 Here and other places where we use barriers (and/o
ericrk 2017/03/27 22:58:13 I may have been over-thinking things. I was sort o
39 }
40
41 bool DiscardableHandle::Delete() {
42 // Acquire semantics prevent GL operations after Delete from being re-ordered
43 // before.
44 return kHandleUnlocked == base::subtle::Acquire_CompareAndSwap(
45 AsAtomic(), kHandleUnlocked, kHandleDeleted);
46 }
47
48 bool DiscardableHandle::Lock() {
49 while (true) {
piman 2017/03/20 23:51:44 Will this be called on the service side? If so, ca
ericrk 2017/03/27 22:58:13 This should be called client-side only. I'll updat
50 base::subtle::Atomic32 current_value =
51 base::subtle::NoBarrier_Load(AsAtomic());
52 if (current_value == kHandleDeleted) {
53 // Once a handle is deleted, it cannot be modified further.
54 return false;
55 }
56 base::subtle::Atomic32 new_value = current_value + 1;
57 // Acquire semantics prevent gl operations after Lock from being re-ordered
58 // before.
59 base::subtle::Atomic32 previous_value =
60 base::subtle::Acquire_CompareAndSwap(AsAtomic(), current_value,
61 new_value);
62 if (current_value == previous_value) {
63 return true;
64 }
65 }
66 }
67
68 bool DiscardableHandle::IsDeleted() const {
69 return kHandleDeleted == base::subtle::Acquire_Load(AsAtomic());
piman 2017/03/20 23:51:44 Which side is planning to call this? From the clie
ericrk 2017/03/27 22:58:13 We only call this from the client side, to check i
70 }
71
72 bool DiscardableHandle::IsLockedForTesting() {
73 return kHandleLockedStart <= base::subtle::Acquire_Load(AsAtomic());
74 }
75
76 volatile base::subtle::Atomic32* DiscardableHandle::AsAtomic() const {
77 return reinterpret_cast<volatile base::subtle::Atomic32*>(
78 buffer_->GetDataAddress(byte_offset_, sizeof(base::subtle::Atomic32)));
79 }
80
81 } // namespace gpu
OLDNEW
« no previous file with comments | « gpu/command_buffer/common/discardable_handle.h ('k') | gpu/command_buffer/common/discardable_handle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698