Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / common / buffer.h
blobe14dabe8e2750963c05750ec2ab691fb35ff0f29
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"
15 namespace base {
16 class SharedMemory;
19 namespace gpu {
21 class GPU_EXPORT BufferBacking {
22 public:
23 virtual ~BufferBacking() {}
24 virtual void* GetMemory() const = 0;
25 virtual size_t GetSize() const = 0;
28 class GPU_EXPORT SharedMemoryBufferBacking : public BufferBacking {
29 public:
30 SharedMemoryBufferBacking(scoped_ptr<base::SharedMemory> shared_memory,
31 size_t size);
32 ~SharedMemoryBufferBacking() override;
33 void* GetMemory() const override;
34 size_t GetSize() const override;
35 base::SharedMemory* shared_memory() { return shared_memory_.get(); }
37 private:
38 scoped_ptr<base::SharedMemory> shared_memory_;
39 size_t size_;
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> {
45 public:
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;
55 private:
56 friend class base::RefCountedThreadSafe<Buffer>;
57 ~Buffer();
59 scoped_ptr<BufferBacking> backing_;
60 void* memory_;
61 size_t size_;
63 DISALLOW_COPY_AND_ASSIGN(Buffer);
66 static inline scoped_ptr<BufferBacking> MakeBackingFromSharedMemory(
67 scoped_ptr<base::SharedMemory> shared_memory,
68 size_t size) {
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,
75 size_t size) {
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,
82 int32_t buffer_id);
84 } // namespace gpu
86 #endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_