Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gpu_memory_buffer_impl_ozone_native_buffer.cc
1 // Copyright 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.
4
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_ozone_native_buffer.h"
6
7 #include "base/bind.h"
8 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
9 #include "ui/gl/gl_bindings.h"
10
11 namespace content {
12 namespace {
13
14 void GpuMemoryBufferDeleted(gfx::GpuMemoryBufferId id,
15                             int client_id,
16                             uint32 sync_point) {
17   GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
18       gfx::OZONE_NATIVE_BUFFER, id, client_id, sync_point);
19 }
20
21 void GpuMemoryBufferCreated(
22     const gfx::Size& size,
23     gfx::GpuMemoryBuffer::Format format,
24     int client_id,
25     const GpuMemoryBufferImpl::CreationCallback& callback,
26     const gfx::GpuMemoryBufferHandle& handle) {
27   if (handle.is_null()) {
28     callback.Run(scoped_ptr<GpuMemoryBufferImpl>());
29     return;
30   }
31
32   DCHECK_EQ(gfx::OZONE_NATIVE_BUFFER, handle.type);
33   callback.Run(GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
34       handle,
35       size,
36       format,
37       base::Bind(&GpuMemoryBufferDeleted, handle.id, client_id)));
38 }
39
40 void GpuMemoryBufferCreatedForChildProcess(
41     const GpuMemoryBufferImpl::AllocationCallback& callback,
42     const gfx::GpuMemoryBufferHandle& handle) {
43   DCHECK_IMPLIES(!handle.is_null(), gfx::OZONE_NATIVE_BUFFER == handle.type);
44
45   callback.Run(handle);
46 }
47
48 }  // namespace
49
50 GpuMemoryBufferImplOzoneNativeBuffer::GpuMemoryBufferImplOzoneNativeBuffer(
51     gfx::GpuMemoryBufferId id,
52     const gfx::Size& size,
53     Format format,
54     const DestructionCallback& callback)
55     : GpuMemoryBufferImpl(id, size, format, callback) {
56 }
57
58 GpuMemoryBufferImplOzoneNativeBuffer::~GpuMemoryBufferImplOzoneNativeBuffer() {
59 }
60
61 // static
62 void GpuMemoryBufferImplOzoneNativeBuffer::Create(
63     gfx::GpuMemoryBufferId id,
64     const gfx::Size& size,
65     Format format,
66     int client_id,
67     const CreationCallback& callback) {
68   GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
69       gfx::OZONE_NATIVE_BUFFER,
70       id,
71       size,
72       format,
73       SCANOUT,
74       client_id,
75       base::Bind(&GpuMemoryBufferCreated, size, format, client_id, callback));
76 }
77
78 // static
79 void GpuMemoryBufferImplOzoneNativeBuffer::AllocateForChildProcess(
80     gfx::GpuMemoryBufferId id,
81     const gfx::Size& size,
82     Format format,
83     int child_client_id,
84     const AllocationCallback& callback) {
85   GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
86       gfx::OZONE_NATIVE_BUFFER,
87       id,
88       size,
89       format,
90       SCANOUT,
91       child_client_id,
92       base::Bind(&GpuMemoryBufferCreatedForChildProcess, callback));
93 }
94
95 // static
96 scoped_ptr<GpuMemoryBufferImpl>
97 GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
98     const gfx::GpuMemoryBufferHandle& handle,
99     const gfx::Size& size,
100     Format format,
101     const DestructionCallback& callback) {
102   DCHECK(IsFormatSupported(format));
103
104   return make_scoped_ptr<GpuMemoryBufferImpl>(
105       new GpuMemoryBufferImplOzoneNativeBuffer(
106           handle.id, size, format, callback));
107 }
108
109 // static
110 void GpuMemoryBufferImplOzoneNativeBuffer::DeletedByChildProcess(
111     gfx::GpuMemoryBufferId id,
112     int child_client_id,
113     uint32_t sync_point) {
114   GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
115       gfx::OZONE_NATIVE_BUFFER, id, child_client_id, sync_point);
116 }
117
118 // static
119 bool GpuMemoryBufferImplOzoneNativeBuffer::IsFormatSupported(Format format) {
120   switch (format) {
121     case RGBA_8888:
122     case RGBX_8888:
123       return true;
124     case BGRA_8888:
125       return false;
126   }
127
128   NOTREACHED();
129   return false;
130 }
131
132 // static
133 bool GpuMemoryBufferImplOzoneNativeBuffer::IsUsageSupported(Usage usage) {
134   switch (usage) {
135     case MAP:
136       return false;
137     case SCANOUT:
138       return true;
139   }
140
141   NOTREACHED();
142   return false;
143 }
144
145 // static
146 bool GpuMemoryBufferImplOzoneNativeBuffer::IsConfigurationSupported(
147     Format format,
148     Usage usage) {
149   return IsFormatSupported(format) && IsUsageSupported(usage);
150 }
151
152 void* GpuMemoryBufferImplOzoneNativeBuffer::Map() {
153   NOTREACHED();
154   return NULL;
155 }
156
157 void GpuMemoryBufferImplOzoneNativeBuffer::Unmap() {
158   NOTREACHED();
159 }
160
161 uint32 GpuMemoryBufferImplOzoneNativeBuffer::GetStride() const {
162   NOTREACHED();
163   return 0;
164 }
165
166 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplOzoneNativeBuffer::GetHandle()
167     const {
168   gfx::GpuMemoryBufferHandle handle;
169   handle.type = gfx::OZONE_NATIVE_BUFFER;
170   handle.id = id_;
171   return handle;
172 }
173
174 }  // namespace content