OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ |
6 #define GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ | 6 #define GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "gpu/command_buffer/common/types.h" | 11 #include "gpu/command_buffer/common/types.h" |
12 #include "gpu/gpu_export.h" | 12 #include "gpu/gpu_export.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 class SharedMemory; | 15 class SharedMemory; |
16 } | 16 } |
17 | 17 |
18 namespace gpu { | 18 namespace gpu { |
19 | 19 |
| 20 class GPU_EXPORT BufferBacking { |
| 21 public: |
| 22 virtual ~BufferBacking() {} |
| 23 virtual void* GetMemory() const = 0; |
| 24 virtual size_t GetSize() const = 0; |
| 25 }; |
| 26 |
| 27 class GPU_EXPORT SharedMemoryBufferBacking : public BufferBacking { |
| 28 public: |
| 29 SharedMemoryBufferBacking(scoped_ptr<base::SharedMemory> shared_memory, |
| 30 size_t size); |
| 31 virtual ~SharedMemoryBufferBacking(); |
| 32 virtual void* GetMemory() const OVERRIDE; |
| 33 virtual size_t GetSize() const OVERRIDE; |
| 34 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
| 35 |
| 36 private: |
| 37 scoped_ptr<base::SharedMemory> shared_memory_; |
| 38 size_t size_; |
| 39 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBufferBacking); |
| 40 }; |
| 41 |
20 // Buffer owns a piece of shared-memory of a certain size. | 42 // Buffer owns a piece of shared-memory of a certain size. |
21 class GPU_EXPORT Buffer : public base::RefCountedThreadSafe<Buffer> { | 43 class GPU_EXPORT Buffer : public base::RefCountedThreadSafe<Buffer> { |
22 public: | 44 public: |
23 Buffer(scoped_ptr<base::SharedMemory> shared_memory, size_t size); | 45 explicit Buffer(scoped_ptr<BufferBacking> backing); |
24 | 46 |
25 base::SharedMemory* shared_memory() const { return shared_memory_.get(); } | 47 BufferBacking* backing() const { return backing_.get(); } |
26 void* memory() const { return memory_; } | 48 void* memory() const { return memory_; } |
27 size_t size() const { return size_; } | 49 size_t size() const { return size_; } |
28 | 50 |
29 // Returns NULL if the address overflows the memory. | 51 // Returns NULL if the address overflows the memory. |
30 void* GetDataAddress(uint32 data_offset, uint32 data_size) const; | 52 void* GetDataAddress(uint32 data_offset, uint32 data_size) const; |
31 | 53 |
32 private: | 54 private: |
33 friend class base::RefCountedThreadSafe<Buffer>; | 55 friend class base::RefCountedThreadSafe<Buffer>; |
34 ~Buffer(); | 56 ~Buffer(); |
35 | 57 |
36 scoped_ptr<base::SharedMemory> shared_memory_; | 58 scoped_ptr<BufferBacking> backing_; |
37 void* memory_; | 59 void* memory_; |
38 size_t size_; | 60 size_t size_; |
39 | 61 |
40 DISALLOW_COPY_AND_ASSIGN(Buffer); | 62 DISALLOW_COPY_AND_ASSIGN(Buffer); |
41 }; | 63 }; |
42 | 64 |
| 65 static inline scoped_ptr<BufferBacking> MakeBackingFromSharedMemory( |
| 66 scoped_ptr<base::SharedMemory> shared_memory, |
| 67 size_t size) { |
| 68 return scoped_ptr<BufferBacking>( |
| 69 new SharedMemoryBufferBacking(shared_memory.Pass(), size)); |
| 70 } |
| 71 |
| 72 static inline scoped_refptr<Buffer> MakeBufferFromSharedMemory( |
| 73 scoped_ptr<base::SharedMemory> shared_memory, |
| 74 size_t size) { |
| 75 return new Buffer(MakeBackingFromSharedMemory(shared_memory.Pass(), size)); |
| 76 } |
| 77 |
43 } // namespace gpu | 78 } // namespace gpu |
44 | 79 |
45 #endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ | 80 #endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_ |
OLD | NEW |