Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / android_webview / browser / gpu_memory_buffer_factory_impl.cc
blob63771c682574c2bd8e39f2343e2aad74cf3e3d00
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 "android_webview/browser/gpu_memory_buffer_factory_impl.h"
7 #include "android_webview/public/browser/draw_gl.h"
8 #include "base/logging.h"
9 #include "gpu/command_buffer/service/in_process_command_buffer.h"
10 #include "ui/gfx/gpu_memory_buffer.h"
11 #include "ui/gfx/size.h"
12 #include "ui/gl/gl_bindings.h"
13 #include "ui/gl/gl_image_android_native_buffer.h"
15 namespace android_webview {
17 namespace {
19 // Provides hardware rendering functions from the Android glue layer.
20 AwDrawGLFunctionTable* g_gl_draw_functions = NULL;
22 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
23 public:
24 GpuMemoryBufferImpl(long buffer_id, const gfx::Size& size)
25 : buffer_id_(buffer_id), size_(size), mapped_(false) {
26 DCHECK(buffer_id_);
29 virtual ~GpuMemoryBufferImpl() {
30 g_gl_draw_functions->release_graphic_buffer(buffer_id_);
33 // Overridden from gfx::GpuMemoryBuffer:
34 virtual void* Map() OVERRIDE {
35 void* vaddr = NULL;
36 int err = g_gl_draw_functions->map(buffer_id_, MAP_READ_WRITE, &vaddr);
37 DCHECK(!err);
38 mapped_ = true;
39 return vaddr;
41 virtual void Unmap() OVERRIDE {
42 int err = g_gl_draw_functions->unmap(buffer_id_);
43 DCHECK(!err);
44 mapped_ = false;
46 virtual bool IsMapped() const OVERRIDE { return mapped_; }
47 virtual uint32 GetStride() const OVERRIDE {
48 return g_gl_draw_functions->get_stride(buffer_id_);
50 virtual gfx::GpuMemoryBufferHandle GetHandle() const OVERRIDE {
51 gfx::GpuMemoryBufferHandle handle;
52 handle.type = gfx::ANDROID_NATIVE_BUFFER;
53 handle.buffer_id = buffer_id_;
54 return handle;
57 private:
58 long buffer_id_;
59 gfx::Size size_;
60 bool mapped_;
62 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl);
65 } // namespace
67 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
70 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
73 scoped_ptr<gfx::GpuMemoryBuffer>
74 GpuMemoryBufferFactoryImpl::AllocateGpuMemoryBuffer(size_t width,
75 size_t height,
76 unsigned internalformat,
77 unsigned usage) {
78 // For Android WebView we assume the |internalformat| will always be
79 // GL_RGBA8_OES.
80 CHECK_EQ(static_cast<GLenum>(GL_RGBA8_OES), internalformat);
81 CHECK(g_gl_draw_functions);
82 long buffer_id = g_gl_draw_functions->create_graphic_buffer(width, height);
83 if (!buffer_id)
84 return scoped_ptr<gfx::GpuMemoryBuffer>();
86 return make_scoped_ptr(
87 new GpuMemoryBufferImpl(buffer_id, gfx::Size(width, height)))
88 .PassAs<gfx::GpuMemoryBuffer>();
91 scoped_refptr<gfx::GLImage>
92 GpuMemoryBufferFactoryImpl::CreateImageForGpuMemoryBuffer(
93 const gfx::GpuMemoryBufferHandle& handle,
94 const gfx::Size& size,
95 unsigned internalformat) {
96 DCHECK_EQ(gfx::ANDROID_NATIVE_BUFFER, handle.type);
98 EGLClientBuffer native_buffer =
99 g_gl_draw_functions->get_native_buffer(handle.buffer_id);
100 DCHECK(native_buffer);
102 scoped_refptr<gfx::GLImageAndroidNativeBuffer> image(
103 new gfx::GLImageAndroidNativeBuffer(size));
104 if (!image->Initialize(native_buffer))
105 return scoped_refptr<gfx::GLImage>();
107 return image;
110 // static
111 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
112 AwDrawGLFunctionTable* table) {
113 g_gl_draw_functions = table;
116 bool GpuMemoryBufferFactoryImpl::Initialize() {
117 if (!g_gl_draw_functions)
118 return false;
120 gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(this);
121 return true;
124 } // namespace android_webview