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
{
19 // Provides hardware rendering functions from the Android glue layer.
20 AwDrawGLFunctionTable
* g_gl_draw_functions
= NULL
;
22 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
24 GpuMemoryBufferImpl(long buffer_id
, const gfx::Size
& size
)
25 : buffer_id_(buffer_id
), size_(size
), mapped_(false) {
29 virtual ~GpuMemoryBufferImpl() {
30 g_gl_draw_functions
->release_graphic_buffer(buffer_id_
);
33 // Overridden from gfx::GpuMemoryBuffer:
34 virtual void* Map() OVERRIDE
{
36 int err
= g_gl_draw_functions
->map(buffer_id_
, MAP_READ_WRITE
, &vaddr
);
41 virtual void Unmap() OVERRIDE
{
42 int err
= g_gl_draw_functions
->unmap(buffer_id_
);
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_
;
62 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl
);
67 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
70 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
73 scoped_ptr
<gfx::GpuMemoryBuffer
>
74 GpuMemoryBufferFactoryImpl::AllocateGpuMemoryBuffer(size_t width
,
76 unsigned internalformat
,
78 // For Android WebView we assume the |internalformat| will always be
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
);
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
>();
111 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
112 AwDrawGLFunctionTable
* table
) {
113 g_gl_draw_functions
= table
;
116 bool GpuMemoryBufferFactoryImpl::Initialize() {
117 if (!g_gl_draw_functions
)
120 gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(this);
124 } // namespace android_webview