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/atomic_sequence_num.h"
8#include "base/bind.h"
9#include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
10#include "ui/gl/gl_bindings.h"
11
12namespace content {
13namespace {
14
15base::StaticAtomicSequenceNumber g_next_buffer_id;
16
17void Noop() {
18}
19
20void GpuMemoryBufferCreated(
21    const gfx::Size& size,
22    unsigned internalformat,
23    const GpuMemoryBufferImpl::CreationCallback& callback,
24    const gfx::GpuMemoryBufferHandle& handle) {
25  DCHECK_EQ(gfx::OZONE_NATIVE_BUFFER, handle.type);
26
27  callback.Run(GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
28      handle, size, internalformat, base::Bind(&Noop)));
29}
30
31void GpuMemoryBufferCreatedForChildProcess(
32    const GpuMemoryBufferImpl::AllocationCallback& callback,
33    const gfx::GpuMemoryBufferHandle& handle) {
34  DCHECK_EQ(gfx::OZONE_NATIVE_BUFFER, handle.type);
35
36  callback.Run(handle);
37}
38
39}  // namespace
40
41GpuMemoryBufferImplOzoneNativeBuffer::GpuMemoryBufferImplOzoneNativeBuffer(
42    const gfx::Size& size,
43    unsigned internalformat,
44    const DestructionCallback& callback,
45    const gfx::GpuMemoryBufferId& id)
46    : GpuMemoryBufferImpl(size, internalformat, callback), id_(id) {
47}
48
49GpuMemoryBufferImplOzoneNativeBuffer::~GpuMemoryBufferImplOzoneNativeBuffer() {
50}
51
52// static
53void GpuMemoryBufferImplOzoneNativeBuffer::Create(
54    const gfx::Size& size,
55    unsigned internalformat,
56    unsigned usage,
57    int client_id,
58    const CreationCallback& callback) {
59  gfx::GpuMemoryBufferHandle handle;
60  handle.global_id.primary_id = g_next_buffer_id.GetNext();
61  handle.global_id.secondary_id = client_id;
62  handle.type = gfx::OZONE_NATIVE_BUFFER;
63  GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
64      handle,
65      size,
66      internalformat,
67      usage,
68      base::Bind(&GpuMemoryBufferCreated, size, internalformat, callback));
69}
70
71// static
72void GpuMemoryBufferImplOzoneNativeBuffer::AllocateForChildProcess(
73    const gfx::Size& size,
74    unsigned internalformat,
75    unsigned usage,
76    int child_client_id,
77    const AllocationCallback& callback) {
78  gfx::GpuMemoryBufferHandle handle;
79  handle.global_id.primary_id = g_next_buffer_id.GetNext();
80  handle.global_id.secondary_id = child_client_id;
81  handle.type = gfx::OZONE_NATIVE_BUFFER;
82  GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
83      handle,
84      size,
85      internalformat,
86      usage,
87      base::Bind(&GpuMemoryBufferCreatedForChildProcess, callback));
88}
89
90// static
91scoped_ptr<GpuMemoryBufferImpl>
92GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
93    const gfx::GpuMemoryBufferHandle& handle,
94    const gfx::Size& size,
95    unsigned internalformat,
96    const DestructionCallback& callback) {
97  DCHECK(IsFormatSupported(internalformat));
98
99  return make_scoped_ptr<GpuMemoryBufferImpl>(
100      new GpuMemoryBufferImplOzoneNativeBuffer(
101          size, internalformat, callback, handle.global_id));
102}
103
104// static
105bool GpuMemoryBufferImplOzoneNativeBuffer::IsFormatSupported(
106    unsigned internalformat) {
107  switch (internalformat) {
108    case GL_RGBA8_OES:
109    case GL_RGB8_OES:
110      return true;
111    default:
112      return false;
113  }
114}
115
116// static
117bool GpuMemoryBufferImplOzoneNativeBuffer::IsUsageSupported(unsigned usage) {
118  switch (usage) {
119    case GL_IMAGE_SCANOUT_CHROMIUM:
120      return true;
121    default:
122      return false;
123  }
124}
125
126// static
127bool GpuMemoryBufferImplOzoneNativeBuffer::IsConfigurationSupported(
128    unsigned internalformat,
129    unsigned usage) {
130  return IsFormatSupported(internalformat) && IsUsageSupported(usage);
131}
132
133void* GpuMemoryBufferImplOzoneNativeBuffer::Map() {
134  NOTREACHED();
135  return NULL;
136}
137
138void GpuMemoryBufferImplOzoneNativeBuffer::Unmap() {
139  NOTREACHED();
140}
141
142uint32 GpuMemoryBufferImplOzoneNativeBuffer::GetStride() const {
143  NOTREACHED();
144  return 0;
145}
146
147gfx::GpuMemoryBufferHandle GpuMemoryBufferImplOzoneNativeBuffer::GetHandle()
148    const {
149  gfx::GpuMemoryBufferHandle handle;
150  handle.type = gfx::OZONE_NATIVE_BUFFER;
151  handle.global_id = id_;
152  return handle;
153}
154
155}  // namespace content
156