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 "ui/gfx/gpu_memory_buffer.h"
10 #include "ui/gfx/size.h"
11 #include "ui/gl/gl_bindings.h"
13 namespace android_webview
{
17 // Provides hardware rendering functions from the Android glue layer.
18 AwDrawGLFunctionTable
* g_gl_draw_functions
= NULL
;
20 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
22 GpuMemoryBufferImpl(int buffer_id
, gfx::Size size
)
23 : buffer_id_(buffer_id
),
29 virtual ~GpuMemoryBufferImpl() {
30 g_gl_draw_functions
->release_graphic_buffer(buffer_id_
);
33 // Overridden from gfx::GpuMemoryBuffer:
34 virtual void Map(gfx::GpuMemoryBuffer::AccessMode mode
,
35 void** vaddr
) OVERRIDE
{
36 AwMapMode map_mode
= MAP_READ_ONLY
;
38 case GpuMemoryBuffer::READ_ONLY
:
39 map_mode
= MAP_READ_ONLY
;
41 case GpuMemoryBuffer::WRITE_ONLY
:
42 map_mode
= MAP_WRITE_ONLY
;
44 case GpuMemoryBuffer::READ_WRITE
:
45 map_mode
= MAP_READ_WRITE
;
48 LOG(DFATAL
) << "Unknown map mode: " << mode
;
50 int err
= g_gl_draw_functions
->map(buffer_id_
, map_mode
, vaddr
);
54 virtual void Unmap() OVERRIDE
{
55 int err
= g_gl_draw_functions
->unmap(buffer_id_
);
59 virtual bool IsMapped() const OVERRIDE
{ return mapped_
; }
60 virtual uint32
GetStride() const OVERRIDE
{
61 return g_gl_draw_functions
->get_stride(buffer_id_
);
63 virtual gfx::GpuMemoryBufferHandle
GetHandle() const OVERRIDE
{
64 gfx::GpuMemoryBufferHandle handle
;
65 handle
.type
= gfx::EGL_CLIENT_BUFFER
;
66 handle
.native_buffer
= g_gl_draw_functions
->get_native_buffer(buffer_id_
);
75 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl
);
80 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
83 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
86 gfx::GpuMemoryBuffer
* GpuMemoryBufferFactoryImpl::CreateGpuMemoryBuffer(
89 unsigned internalformat
) {
90 // For Android WebView we assume the |internalformat| will always be
92 CHECK_EQ(static_cast<GLenum
>(GL_RGBA8_OES
), internalformat
);
93 CHECK(g_gl_draw_functions
);
94 int buffer_id
= g_gl_draw_functions
->create_graphic_buffer(width
, height
);
98 return new GpuMemoryBufferImpl(buffer_id
, gfx::Size(width
, height
));
102 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
103 AwDrawGLFunctionTable
* table
) {
104 g_gl_draw_functions
= table
;
107 } // namespace android_webview