Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_io_surface.cc
blob088795c974a5e15f39f5d416b902a427287dbf7c
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_io_surface.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
10 #include "ui/gl/gl_bindings.h"
12 namespace content {
13 namespace {
15 void GpuMemoryBufferDeleted(gfx::GpuMemoryBufferId id,
16 int client_id,
17 uint32 sync_point) {
18 GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
19 gfx::IO_SURFACE_BUFFER, id, client_id, sync_point);
22 void GpuMemoryBufferCreated(
23 const gfx::Size& size,
24 gfx::GpuMemoryBuffer::Format format,
25 int client_id,
26 const GpuMemoryBufferImpl::CreationCallback& callback,
27 const gfx::GpuMemoryBufferHandle& handle) {
28 if (handle.is_null()) {
29 callback.Run(scoped_ptr<GpuMemoryBufferImpl>());
30 return;
33 DCHECK_EQ(gfx::IO_SURFACE_BUFFER, handle.type);
34 callback.Run(GpuMemoryBufferImplIOSurface::CreateFromHandle(
35 handle,
36 size,
37 format,
38 base::Bind(&GpuMemoryBufferDeleted, handle.id, client_id)));
41 void GpuMemoryBufferCreatedForChildProcess(
42 const GpuMemoryBufferImpl::AllocationCallback& callback,
43 const gfx::GpuMemoryBufferHandle& handle) {
44 DCHECK_IMPLIES(!handle.is_null(), gfx::IO_SURFACE_BUFFER == handle.type);
46 callback.Run(handle);
49 } // namespace
51 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
52 gfx::GpuMemoryBufferId id,
53 const gfx::Size& size,
54 Format format,
55 const DestructionCallback& callback,
56 IOSurfaceRef io_surface)
57 : GpuMemoryBufferImpl(id, size, format, callback), io_surface_(io_surface) {
60 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
63 // static
64 void GpuMemoryBufferImplIOSurface::Create(gfx::GpuMemoryBufferId id,
65 const gfx::Size& size,
66 Format format,
67 int client_id,
68 const CreationCallback& callback) {
69 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
70 gfx::IO_SURFACE_BUFFER,
71 id,
72 size,
73 format,
74 MAP,
75 client_id,
76 base::Bind(&GpuMemoryBufferCreated, size, format, client_id, callback));
79 // static
80 void GpuMemoryBufferImplIOSurface::AllocateForChildProcess(
81 gfx::GpuMemoryBufferId id,
82 const gfx::Size& size,
83 Format format,
84 int child_client_id,
85 const AllocationCallback& callback) {
86 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
87 gfx::IO_SURFACE_BUFFER,
88 id,
89 size,
90 format,
91 MAP,
92 child_client_id,
93 base::Bind(&GpuMemoryBufferCreatedForChildProcess, callback));
96 // static
97 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplIOSurface::CreateFromHandle(
98 const gfx::GpuMemoryBufferHandle& handle,
99 const gfx::Size& size,
100 Format format,
101 const DestructionCallback& callback) {
102 DCHECK(IsFormatSupported(format));
104 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
105 IOSurfaceLookup(handle.io_surface_id));
106 if (!io_surface)
107 return scoped_ptr<GpuMemoryBufferImpl>();
109 return make_scoped_ptr<GpuMemoryBufferImpl>(new GpuMemoryBufferImplIOSurface(
110 handle.id, size, format, callback, io_surface.get()));
113 // static
114 void GpuMemoryBufferImplIOSurface::DeletedByChildProcess(
115 gfx::GpuMemoryBufferId id,
116 int child_client_id,
117 uint32_t sync_point) {
118 GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
119 gfx::IO_SURFACE_BUFFER, id, child_client_id, sync_point);
122 // static
123 bool GpuMemoryBufferImplIOSurface::IsFormatSupported(Format format) {
124 switch (format) {
125 case BGRA_8888:
126 return true;
127 case RGBA_8888:
128 case RGBX_8888:
129 return false;
132 NOTREACHED();
133 return false;
136 // static
137 bool GpuMemoryBufferImplIOSurface::IsUsageSupported(Usage usage) {
138 switch (usage) {
139 case MAP:
140 return true;
141 case SCANOUT:
142 return false;
145 NOTREACHED();
146 return false;
149 // static
150 bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported(Format format,
151 Usage usage) {
152 return IsFormatSupported(format) && IsUsageSupported(usage);
155 // static
156 uint32 GpuMemoryBufferImplIOSurface::PixelFormat(Format format) {
157 switch (format) {
158 case BGRA_8888:
159 return 'BGRA';
160 case RGBA_8888:
161 case RGBX_8888:
162 NOTREACHED();
163 return 0;
166 NOTREACHED();
167 return 0;
170 void* GpuMemoryBufferImplIOSurface::Map() {
171 DCHECK(!mapped_);
172 IOSurfaceLock(io_surface_, 0, NULL);
173 mapped_ = true;
174 return IOSurfaceGetBaseAddress(io_surface_);
177 void GpuMemoryBufferImplIOSurface::Unmap() {
178 DCHECK(mapped_);
179 IOSurfaceUnlock(io_surface_, 0, NULL);
180 mapped_ = false;
183 uint32 GpuMemoryBufferImplIOSurface::GetStride() const {
184 return IOSurfaceGetBytesPerRow(io_surface_);
187 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const {
188 gfx::GpuMemoryBufferHandle handle;
189 handle.type = gfx::IO_SURFACE_BUFFER;
190 handle.id = id_;
191 handle.io_surface_id = IOSurfaceGetID(io_surface_);
192 return handle;
195 } // namespace content