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() 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
.native_buffer
= g_gl_draw_functions
->get_native_buffer(buffer_id_
);
62 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl
);
67 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
70 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
73 gfx::GpuMemoryBuffer
* GpuMemoryBufferFactoryImpl::CreateGpuMemoryBuffer(
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
);
86 return new GpuMemoryBufferImpl(buffer_id
, gfx::Size(width
, height
));
90 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
91 AwDrawGLFunctionTable
* table
) {
92 g_gl_draw_functions
= table
;
95 } // namespace android_webview