Send compressed histograms with system logs when sending feedback.
[chromium-blink-merge.git] / android_webview / browser / gpu_memory_buffer_factory_impl.cc
blobc4a6cac748e9049bc47f535831c0b0fb25e84b41
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 {
15 namespace {
17 // Provides hardware rendering functions from the Android glue layer.
18 AwDrawGLFunctionTable* g_gl_draw_functions = NULL;
20 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
21 public:
22 GpuMemoryBufferImpl(int buffer_id, gfx::Size size)
23 : buffer_id_(buffer_id),
24 size_(size),
25 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(gfx::GpuMemoryBuffer::AccessMode mode,
35 void** vaddr) OVERRIDE {
36 AwMapMode map_mode = MAP_READ_ONLY;
37 switch (mode) {
38 case GpuMemoryBuffer::READ_ONLY:
39 map_mode = MAP_READ_ONLY;
40 break;
41 case GpuMemoryBuffer::WRITE_ONLY:
42 map_mode = MAP_WRITE_ONLY;
43 break;
44 case GpuMemoryBuffer::READ_WRITE:
45 map_mode = MAP_READ_WRITE;
46 break;
47 default:
48 LOG(DFATAL) << "Unknown map mode: " << mode;
50 int err = g_gl_draw_functions->map(buffer_id_, map_mode, vaddr);
51 DCHECK(!err);
52 mapped_ = true;
54 virtual void Unmap() OVERRIDE {
55 int err = g_gl_draw_functions->unmap(buffer_id_);
56 DCHECK(!err);
57 mapped_ = false;
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_);
67 return handle;
70 private:
71 int buffer_id_;
72 gfx::Size size_;
73 bool mapped_;
75 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl);
78 } // namespace
80 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() {
83 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() {
86 gfx::GpuMemoryBuffer* GpuMemoryBufferFactoryImpl::CreateGpuMemoryBuffer(
87 size_t width,
88 size_t height,
89 unsigned internalformat) {
90 // For Android WebView we assume the |internalformat| will always be
91 // GL_RGBA8_OES.
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);
95 if (!buffer_id)
96 return NULL;
98 return new GpuMemoryBufferImpl(buffer_id, gfx::Size(width, height));
101 // static
102 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable(
103 AwDrawGLFunctionTable* table) {
104 g_gl_draw_functions = table;
107 } // namespace android_webview