1 // Copyright (c) 2014 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.
5 #ifndef GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
6 #define GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/shared_memory.h"
12 #include "base/trace_event/memory_allocator_dump.h"
13 #include "gpu/gpu_export.h"
21 class GPU_EXPORT BufferBacking
{
23 virtual ~BufferBacking() {}
24 virtual void* GetMemory() const = 0;
25 virtual size_t GetSize() const = 0;
28 class GPU_EXPORT SharedMemoryBufferBacking
: public BufferBacking
{
30 SharedMemoryBufferBacking(scoped_ptr
<base::SharedMemory
> shared_memory
,
32 ~SharedMemoryBufferBacking() override
;
33 void* GetMemory() const override
;
34 size_t GetSize() const override
;
35 base::SharedMemory
* shared_memory() { return shared_memory_
.get(); }
38 scoped_ptr
<base::SharedMemory
> shared_memory_
;
40 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBufferBacking
);
43 // Buffer owns a piece of shared-memory of a certain size.
44 class GPU_EXPORT Buffer
: public base::RefCountedThreadSafe
<Buffer
> {
46 explicit Buffer(scoped_ptr
<BufferBacking
> backing
);
48 BufferBacking
* backing() const { return backing_
.get(); }
49 void* memory() const { return memory_
; }
50 size_t size() const { return size_
; }
52 // Returns NULL if the address overflows the memory.
53 void* GetDataAddress(uint32 data_offset
, uint32 data_size
) const;
56 friend class base::RefCountedThreadSafe
<Buffer
>;
59 scoped_ptr
<BufferBacking
> backing_
;
63 DISALLOW_COPY_AND_ASSIGN(Buffer
);
66 static inline scoped_ptr
<BufferBacking
> MakeBackingFromSharedMemory(
67 scoped_ptr
<base::SharedMemory
> shared_memory
,
69 return scoped_ptr
<BufferBacking
>(
70 new SharedMemoryBufferBacking(shared_memory
.Pass(), size
));
73 static inline scoped_refptr
<Buffer
> MakeBufferFromSharedMemory(
74 scoped_ptr
<base::SharedMemory
> shared_memory
,
76 return new Buffer(MakeBackingFromSharedMemory(shared_memory
.Pass(), size
));
79 // Generates GUID which can be used to trace buffer using an Id.
80 GPU_EXPORT
base::trace_event::MemoryAllocatorDumpGuid
GetBufferGUIDForTracing(
81 uint64_t tracing_process_id
,
86 #endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_