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

Unified Diff: gpu/command_buffer/common/discardable_handle_unittest.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 side-by-side diff with in-line comments
Download patch
Index: gpu/command_buffer/common/discardable_handle_unittest.cc
diff --git a/gpu/command_buffer/common/discardable_handle_unittest.cc b/gpu/command_buffer/common/discardable_handle_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..17623589c707461e12aec4cffcf1a8191de90c25
--- /dev/null
+++ b/gpu/command_buffer/common/discardable_handle_unittest.cc
@@ -0,0 +1,141 @@
+// Copyright (c) 2017 The Chromium Authors. All rights reserved.
+// 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 "gpu/command_buffer/common/buffer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gpu {
+namespace {
+
+scoped_refptr<Buffer> MakeBufferForTesting(size_t num_handles) {
+ size_t size = sizeof(base::subtle::Atomic32) * num_handles;
+ std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory);
+ shared_mem->CreateAndMapAnonymous(size);
+ return MakeBufferFromSharedMemory(std::move(shared_mem), size);
+}
+
+} // namespace
+
+TEST(DiscardableHandleTest, BasicUsage) {
+ scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
+
+ uint32_t byte_offset = 0;
+ int32_t shm_id = 1;
+ DiscardableHandle handle;
+ EXPECT_FALSE(handle.IsInitialized());
+
+ handle.InitializeWithNewData(buffer, byte_offset, shm_id);
+ EXPECT_EQ(handle.shm_id(), shm_id);
+ EXPECT_TRUE(handle.IsInitialized());
+ EXPECT_TRUE(handle.IsLockedForTesting());
+
+ EXPECT_FALSE(handle.Delete());
+ EXPECT_FALSE(handle.IsDeleted());
+
+ handle.Unlock();
+ EXPECT_FALSE(handle.IsLockedForTesting());
+
+ EXPECT_TRUE(handle.Lock());
+ EXPECT_TRUE(handle.IsLockedForTesting());
+
+ handle.Unlock();
+ EXPECT_FALSE(handle.IsLockedForTesting());
+
+ EXPECT_TRUE(handle.Delete());
+ EXPECT_TRUE(handle.IsDeleted());
+ EXPECT_FALSE(handle.IsLockedForTesting());
+
+ EXPECT_FALSE(handle.Lock());
+ EXPECT_TRUE(handle.IsDeleted());
+ EXPECT_FALSE(handle.IsLockedForTesting());
+}
+
+TEST(DiscardableHandleTest, MultiLock) {
+ scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
+
+ uint32_t byte_offset = 0;
+ int32_t shm_id = 1;
+ DiscardableHandle handle;
+ EXPECT_FALSE(handle.IsInitialized());
+
+ handle.InitializeWithNewData(buffer, byte_offset, shm_id);
+ EXPECT_EQ(handle.shm_id(), shm_id);
+ EXPECT_TRUE(handle.IsInitialized());
+ EXPECT_TRUE(handle.IsLockedForTesting());
+
+ for (int i = 1; i < 10; ++i) {
+ EXPECT_TRUE(handle.IsLockedForTesting());
+ EXPECT_TRUE(handle.Lock());
+ }
+
+ for (int i = 0; i < 10; ++i) {
+ EXPECT_TRUE(handle.IsLockedForTesting());
+ handle.Unlock();
+ }
+
+ EXPECT_FALSE(handle.IsLockedForTesting());
+}
+
+TEST(DiscardableHandleTest, Suballocations) {
+ static const size_t num_elements = 10;
+ scoped_refptr<Buffer> buffer = MakeBufferForTesting(num_elements);
+
+ DiscardableHandle handles[num_elements];
+ for (int i = 0; i < num_elements; ++i) {
+ EXPECT_FALSE(handles[i].IsInitialized());
+ handles[i].InitializeWithNewData(buffer, sizeof(base::subtle::Atomic32) * i,
+ i + 1);
+ EXPECT_EQ(handles[i].shm_id(), i + 1);
+ EXPECT_TRUE(handles[i].IsInitialized());
+ EXPECT_TRUE(handles[i].IsLockedForTesting());
+ }
+
+ for (int i = 0; i < num_elements; i += 2) {
+ handles[i].Unlock();
+ }
+
+ for (int i = 1; i < num_elements; i += 2) {
+ handles[i].Lock();
+ }
+
+ for (int i = 0; i < num_elements; ++i) {
+ if (i % 2)
+ EXPECT_TRUE(handles[i].IsLockedForTesting());
+ else
+ EXPECT_FALSE(handles[i].IsLockedForTesting());
+ }
+}
+
+TEST(DiscardableHandleTest, ExistingData) {
+ scoped_refptr<Buffer> buffer = MakeBufferForTesting(1);
+
+ uint32_t byte_offset = 0;
+ int32_t shm_id = 1;
+ DiscardableHandle init_handle;
+ EXPECT_FALSE(init_handle.IsInitialized());
+
+ init_handle.InitializeWithNewData(buffer, byte_offset, shm_id);
+ EXPECT_EQ(init_handle.shm_id(), shm_id);
+ EXPECT_TRUE(init_handle.IsInitialized());
+ EXPECT_TRUE(init_handle.IsLockedForTesting());
+
+ init_handle.Unlock();
+ EXPECT_FALSE(init_handle.IsLockedForTesting());
+
+ DiscardableHandle handle;
+ EXPECT_FALSE(handle.IsInitialized());
+
+ handle.InitializeWithExistingData(buffer, init_handle.byte_offset(),
+ init_handle.shm_id());
+ EXPECT_EQ(handle.shm_id(), shm_id);
+ EXPECT_TRUE(handle.IsInitialized());
+ EXPECT_FALSE(handle.IsLockedForTesting());
+
+ EXPECT_TRUE(handle.Delete());
+ EXPECT_TRUE(handle.IsDeleted());
+ EXPECT_TRUE(init_handle.IsDeleted());
+}
+
+} // namespace gpu

Powered by Google App Engine
This is Rietveld 408576698