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(long 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
) OVERRIDE
{
35 AwMapMode map_mode
= MAP_READ_ONLY
;
37 case GpuMemoryBuffer::READ_ONLY
:
38 map_mode
= MAP_READ_ONLY
;
40 case GpuMemoryBuffer::WRITE_ONLY
:
41 map_mode
= MAP_WRITE_ONLY
;
43 case GpuMemoryBuffer::READ_WRITE
:
44 map_mode
= MAP_READ_WRITE
;
47 LOG(DFATAL
) << "Unknown map mode: " << mode
;
50 int err
= g_gl_draw_functions
->map(buffer_id_
, map_mode
, &vaddr
);
55 virtual void Unmap() OVERRIDE
{
56 int err
= g_gl_draw_functions
->unmap(buffer_id_
);
60 virtual bool IsMapped() const OVERRIDE
{ return mapped_
; }
61 virtual uint32
GetStride() const OVERRIDE
{
62 return g_gl_draw_functions
->get_stride(buffer_id_
);
64 virtual gfx::GpuMemoryBufferHandle
GetHandle() const OVERRIDE
{
65 gfx::GpuMemoryBufferHandle handle
;
66 handle
.type
= gfx::ANDROID_NATIVE_BUFFER
;
67 handle
.native_buffer
= g_gl_draw_functions
->get_native_buffer(buffer_id_
);
76 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl
);
81 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
84 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
87 gfx::GpuMemoryBuffer
* GpuMemoryBufferFactoryImpl::CreateGpuMemoryBuffer(
90 unsigned internalformat
) {
91 // For Android WebView we assume the |internalformat| will always be
93 CHECK_EQ(static_cast<GLenum
>(GL_RGBA8_OES
), internalformat
);
94 CHECK(g_gl_draw_functions
);
95 long buffer_id
= g_gl_draw_functions
->create_graphic_buffer(width
, height
);
99 return new GpuMemoryBufferImpl(buffer_id
, gfx::Size(width
, height
));
103 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
104 AwDrawGLFunctionTable
* table
) {
105 g_gl_draw_functions
= table
;
108 } // namespace android_webview