OLD | NEW |
| (Empty) |
1 // Copyright 2013 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. | |
4 | |
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_shm.h" | |
6 | |
7 #include "base/numerics/safe_math.h" | |
8 #include "ui/gl/gl_bindings.h" | |
9 | |
10 namespace content { | |
11 | |
12 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(const gfx::Size& size, | |
13 unsigned internalformat) | |
14 : GpuMemoryBufferImpl(size, internalformat) {} | |
15 | |
16 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} | |
17 | |
18 // static | |
19 void GpuMemoryBufferImplShm::AllocateSharedMemoryForChildProcess( | |
20 const gfx::Size& size, | |
21 unsigned internalformat, | |
22 base::ProcessHandle child_process, | |
23 const AllocationCallback& callback) { | |
24 DCHECK(IsLayoutSupported(size, internalformat)); | |
25 gfx::GpuMemoryBufferHandle handle; | |
26 base::SharedMemory shared_memory; | |
27 if (!shared_memory.CreateAnonymous(size.GetArea() * | |
28 BytesPerPixel(internalformat))) { | |
29 handle.type = gfx::EMPTY_BUFFER; | |
30 return; | |
31 } | |
32 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
33 shared_memory.GiveToProcess(child_process, &handle.handle); | |
34 callback.Run(handle); | |
35 } | |
36 | |
37 // static | |
38 bool GpuMemoryBufferImplShm::IsLayoutSupported(const gfx::Size& size, | |
39 unsigned internalformat) { | |
40 base::CheckedNumeric<int> buffer_size = size.width(); | |
41 buffer_size *= size.height(); | |
42 buffer_size *= BytesPerPixel(internalformat); | |
43 return buffer_size.IsValid(); | |
44 } | |
45 | |
46 // static | |
47 bool GpuMemoryBufferImplShm::IsUsageSupported(unsigned usage) { | |
48 switch (usage) { | |
49 case GL_IMAGE_MAP_CHROMIUM: | |
50 return true; | |
51 default: | |
52 return false; | |
53 } | |
54 } | |
55 | |
56 // static | |
57 bool GpuMemoryBufferImplShm::IsConfigurationSupported(const gfx::Size& size, | |
58 unsigned internalformat, | |
59 unsigned usage) { | |
60 return IsLayoutSupported(size, internalformat) && IsUsageSupported(usage); | |
61 } | |
62 | |
63 bool GpuMemoryBufferImplShm::Initialize() { | |
64 DCHECK(IsLayoutSupported(size_, internalformat_)); | |
65 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | |
66 if (!shared_memory->CreateAnonymous(size_.GetArea() * | |
67 BytesPerPixel(internalformat_))) | |
68 return false; | |
69 shared_memory_ = shared_memory.Pass(); | |
70 DCHECK(!shared_memory_->memory()); | |
71 return true; | |
72 } | |
73 | |
74 bool GpuMemoryBufferImplShm::InitializeFromHandle( | |
75 gfx::GpuMemoryBufferHandle handle) { | |
76 DCHECK(IsLayoutSupported(size_, internalformat_)); | |
77 if (!base::SharedMemory::IsHandleValid(handle.handle)) | |
78 return false; | |
79 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); | |
80 DCHECK(!shared_memory_->memory()); | |
81 return true; | |
82 } | |
83 | |
84 void* GpuMemoryBufferImplShm::Map() { | |
85 DCHECK(!mapped_); | |
86 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) | |
87 return NULL; | |
88 mapped_ = true; | |
89 return shared_memory_->memory(); | |
90 } | |
91 | |
92 void GpuMemoryBufferImplShm::Unmap() { | |
93 DCHECK(mapped_); | |
94 shared_memory_->Unmap(); | |
95 mapped_ = false; | |
96 } | |
97 | |
98 uint32 GpuMemoryBufferImplShm::GetStride() const { | |
99 return size_.width() * BytesPerPixel(internalformat_); | |
100 } | |
101 | |
102 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { | |
103 gfx::GpuMemoryBufferHandle handle; | |
104 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
105 handle.handle = shared_memory_->handle(); | |
106 return handle; | |
107 } | |
108 | |
109 } // namespace content | |
OLD | NEW |