OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
boliu
2014/09/29 15:47:18
remove this and header from android_webview/androi
reveman
2014/09/29 18:32:09
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "android_webview/browser/gpu_memory_buffer_factory_impl.h" | |
6 | |
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" | |
14 | |
15 namespace android_webview { | |
16 | |
17 namespace { | |
18 | |
19 // Provides hardware rendering functions from the Android glue layer. | |
20 AwDrawGLFunctionTable* g_gl_draw_functions = NULL; | |
21 | |
22 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { | |
23 public: | |
24 GpuMemoryBufferImpl(long buffer_id, const gfx::Size& size) | |
25 : buffer_id_(buffer_id), size_(size), mapped_(false) { | |
26 DCHECK(buffer_id_); | |
27 } | |
28 | |
29 virtual ~GpuMemoryBufferImpl() { | |
30 g_gl_draw_functions->release_graphic_buffer(buffer_id_); | |
31 } | |
32 | |
33 // Overridden from gfx::GpuMemoryBuffer: | |
34 virtual void* Map() OVERRIDE { | |
35 void* vaddr = NULL; | |
36 int err = g_gl_draw_functions->map(buffer_id_, MAP_READ_WRITE, &vaddr); | |
37 DCHECK(!err); | |
38 mapped_ = true; | |
39 return vaddr; | |
40 } | |
41 virtual void Unmap() OVERRIDE { | |
42 int err = g_gl_draw_functions->unmap(buffer_id_); | |
43 DCHECK(!err); | |
44 mapped_ = false; | |
45 } | |
46 virtual bool IsMapped() const OVERRIDE { return mapped_; } | |
47 virtual uint32 GetStride() const OVERRIDE { | |
48 return g_gl_draw_functions->get_stride(buffer_id_); | |
49 } | |
50 virtual gfx::GpuMemoryBufferHandle GetHandle() const OVERRIDE { | |
51 gfx::GpuMemoryBufferHandle handle; | |
52 handle.type = gfx::ANDROID_NATIVE_BUFFER; | |
53 handle.buffer_id = buffer_id_; | |
54 return handle; | |
55 } | |
56 | |
57 private: | |
58 long buffer_id_; | |
59 gfx::Size size_; | |
60 bool mapped_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl); | |
63 }; | |
64 | |
65 } // namespace | |
66 | |
67 GpuMemoryBufferFactoryImpl::GpuMemoryBufferFactoryImpl() { | |
68 } | |
69 | |
70 GpuMemoryBufferFactoryImpl::~GpuMemoryBufferFactoryImpl() { | |
71 } | |
72 | |
73 scoped_ptr<gfx::GpuMemoryBuffer> | |
74 GpuMemoryBufferFactoryImpl::AllocateGpuMemoryBuffer(size_t width, | |
75 size_t height, | |
76 unsigned internalformat, | |
77 unsigned usage) { | |
78 // For Android WebView we assume the |internalformat| will always be | |
79 // GL_RGBA8_OES. | |
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); | |
83 if (!buffer_id) | |
84 return scoped_ptr<gfx::GpuMemoryBuffer>(); | |
85 | |
86 return make_scoped_ptr( | |
87 new GpuMemoryBufferImpl(buffer_id, gfx::Size(width, height))) | |
88 .PassAs<gfx::GpuMemoryBuffer>(); | |
89 } | |
90 | |
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); | |
97 | |
98 EGLClientBuffer native_buffer = | |
99 g_gl_draw_functions->get_native_buffer(handle.buffer_id); | |
100 DCHECK(native_buffer); | |
101 | |
102 scoped_refptr<gfx::GLImageAndroidNativeBuffer> image( | |
103 new gfx::GLImageAndroidNativeBuffer(size)); | |
104 if (!image->Initialize(native_buffer)) | |
105 return scoped_refptr<gfx::GLImage>(); | |
106 | |
107 return image; | |
108 } | |
109 | |
110 // static | |
111 void GpuMemoryBufferFactoryImpl::SetAwDrawGLFunctionTable( | |
112 AwDrawGLFunctionTable* table) { | |
113 g_gl_draw_functions = table; | |
114 } | |
115 | |
116 bool GpuMemoryBufferFactoryImpl::Initialize() { | |
117 if (!g_gl_draw_functions) | |
118 return false; | |
119 | |
120 gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(this); | |
121 return true; | |
122 } | |
123 | |
124 } // namespace android_webview | |
OLD | NEW |