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 "gpu/gpu_export.h"
20 class GPU_EXPORT BufferBacking
{
22 virtual ~BufferBacking() {}
23 virtual void* GetMemory() const = 0;
24 virtual size_t GetSize() const = 0;
27 class GPU_EXPORT SharedMemoryBufferBacking
: public BufferBacking
{
29 SharedMemoryBufferBacking(scoped_ptr
<base::SharedMemory
> shared_memory
,
31 ~SharedMemoryBufferBacking() override
;
32 void* GetMemory() const override
;
33 size_t GetSize() const override
;
34 base::SharedMemory
* shared_memory() { return shared_memory_
.get(); }
37 scoped_ptr
<base::SharedMemory
> shared_memory_
;
39 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBufferBacking
);
42 // Buffer owns a piece of shared-memory of a certain size.
43 class GPU_EXPORT Buffer
: public base::RefCountedThreadSafe
<Buffer
> {
45 explicit Buffer(scoped_ptr
<BufferBacking
> backing
);
47 BufferBacking
* backing() const { return backing_
.get(); }
48 void* memory() const { return memory_
; }
49 size_t size() const { return size_
; }
51 // Returns NULL if the address overflows the memory.
52 void* GetDataAddress(uint32 data_offset
, uint32 data_size
) const;
55 friend class base::RefCountedThreadSafe
<Buffer
>;
58 scoped_ptr
<BufferBacking
> backing_
;
62 DISALLOW_COPY_AND_ASSIGN(Buffer
);
65 static inline scoped_ptr
<BufferBacking
> MakeBackingFromSharedMemory(
66 scoped_ptr
<base::SharedMemory
> shared_memory
,
68 return scoped_ptr
<BufferBacking
>(
69 new SharedMemoryBufferBacking(shared_memory
.Pass(), size
));
72 static inline scoped_refptr
<Buffer
> MakeBufferFromSharedMemory(
73 scoped_ptr
<base::SharedMemory
> shared_memory
,
75 return new Buffer(MakeBackingFromSharedMemory(shared_memory
.Pass(), size
));
80 #endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_