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.
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_shm.h"
7 #include "base/numerics/safe_math.h"
8 #include "ui/gl/gl_bindings.h"
12 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(const gfx::Size
& size
,
13 unsigned internalformat
)
14 : GpuMemoryBufferImpl(size
, internalformat
) {}
16 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {}
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
;
32 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
33 shared_memory
.GiveToProcess(child_process
, &handle
.handle
);
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();
47 bool GpuMemoryBufferImplShm::IsUsageSupported(unsigned usage
) {
49 case GL_IMAGE_MAP_CHROMIUM
:
57 bool GpuMemoryBufferImplShm::IsConfigurationSupported(const gfx::Size
& size
,
58 unsigned internalformat
,
60 return IsLayoutSupported(size
, internalformat
) && IsUsageSupported(usage
);
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_
)))
69 shared_memory_
= shared_memory
.Pass();
70 DCHECK(!shared_memory_
->memory());
74 bool GpuMemoryBufferImplShm::InitializeFromHandle(
75 gfx::GpuMemoryBufferHandle handle
) {
76 DCHECK(IsLayoutSupported(size_
, internalformat_
));
77 if (!base::SharedMemory::IsHandleValid(handle
.handle
))
79 shared_memory_
.reset(new base::SharedMemory(handle
.handle
, false));
80 DCHECK(!shared_memory_
->memory());
84 void* GpuMemoryBufferImplShm::Map() {
86 if (!shared_memory_
->Map(size_
.GetArea() * BytesPerPixel(internalformat_
)))
89 return shared_memory_
->memory();
92 void GpuMemoryBufferImplShm::Unmap() {
94 shared_memory_
->Unmap();
98 uint32
GpuMemoryBufferImplShm::GetStride() const {
99 return size_
.width() * BytesPerPixel(internalformat_
);
102 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplShm::GetHandle() const {
103 gfx::GpuMemoryBufferHandle handle
;
104 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
105 handle
.handle
= shared_memory_
->handle();
109 } // namespace content