image_transport_factory_android.cc revision 868fa2fe829687343ffae624259930155e16dbd8
16efa4d6cf9bb214a5e8ddbb224a69b38c4ae6de6Alexey Samsonov// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// Use of this source code is governed by a BSD-style license that can be
341f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov// found in the LICENSE file.
441f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov
541f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/browser/renderer_host/image_transport_factory_android.h"
641f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov
741f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "base/memory/singleton.h"
841f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
941f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/browser/renderer_host/compositor_impl_android.h"
1041f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/common/gpu/client/gl_helper.h"
1141f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
1241f85b9e6a88f801955d99c8a8233b97e64c51ffAlexey Samsonov#include "content/common/gpu/gpu_process_launch_causes.h"
13#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
14#include "third_party/khronos/GLES2/gl2.h"
15#include "webkit/common/gpu/webgraphicscontext3d_in_process_impl.h"
16
17namespace content {
18
19namespace {
20
21static ImageTransportFactoryAndroid* g_factory = NULL;
22
23class DirectGLImageTransportFactory : public ImageTransportFactoryAndroid {
24 public:
25  DirectGLImageTransportFactory();
26  virtual ~DirectGLImageTransportFactory();
27
28  virtual uint32_t InsertSyncPoint() OVERRIDE { return 0; }
29  virtual void WaitSyncPoint(uint32_t sync_point) OVERRIDE {}
30  virtual uint32_t CreateTexture() OVERRIDE {
31    return context_->createTexture();
32  }
33  virtual void DeleteTexture(uint32_t id) OVERRIDE {
34    context_->deleteTexture(id);
35  }
36  virtual void AcquireTexture(
37      uint32 texture_id, const signed char* mailbox_name) OVERRIDE {}
38  virtual void ReleaseTexture(
39      uint32 texture_id, const signed char* mailbox_name) OVERRIDE {}
40  virtual WebKit::WebGraphicsContext3D* GetContext3D() OVERRIDE {
41    return context_.get();
42  }
43  virtual GLHelper* GetGLHelper() OVERRIDE { return NULL; }
44
45 private:
46  scoped_ptr<webkit::gpu::WebGraphicsContext3DInProcessImpl> context_;
47
48  DISALLOW_COPY_AND_ASSIGN(DirectGLImageTransportFactory);
49};
50
51DirectGLImageTransportFactory::DirectGLImageTransportFactory() {
52  WebKit::WebGraphicsContext3D::Attributes attrs;
53  attrs.shareResources = false;
54  attrs.noAutomaticFlushes = true;
55  context_.reset(
56      webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWindow(
57          attrs,
58          NULL,
59          NULL));
60}
61
62DirectGLImageTransportFactory::~DirectGLImageTransportFactory() {
63}
64
65class CmdBufferImageTransportFactory : public ImageTransportFactoryAndroid {
66 public:
67  CmdBufferImageTransportFactory();
68  virtual ~CmdBufferImageTransportFactory();
69
70  virtual uint32_t InsertSyncPoint() OVERRIDE;
71  virtual void WaitSyncPoint(uint32_t sync_point) OVERRIDE;
72  virtual uint32_t CreateTexture() OVERRIDE;
73  virtual void DeleteTexture(uint32_t id) OVERRIDE;
74  virtual void AcquireTexture(
75      uint32 texture_id, const signed char* mailbox_name) OVERRIDE;
76  virtual void ReleaseTexture(
77      uint32 texture_id, const signed char* mailbox_name) OVERRIDE;
78  virtual WebKit::WebGraphicsContext3D* GetContext3D() OVERRIDE {
79    return context_.get();
80  }
81  virtual GLHelper* GetGLHelper() OVERRIDE;
82
83 private:
84  scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context_;
85  scoped_ptr<GLHelper> gl_helper_;
86
87  DISALLOW_COPY_AND_ASSIGN(CmdBufferImageTransportFactory);
88};
89
90CmdBufferImageTransportFactory::CmdBufferImageTransportFactory() {
91  WebKit::WebGraphicsContext3D::Attributes attrs;
92  attrs.shareResources = true;
93  GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance();
94  GURL url("chrome://gpu/ImageTransportFactoryAndroid");
95  base::WeakPtr<WebGraphicsContext3DSwapBuffersClient> swap_client;
96  context_.reset(new WebGraphicsContext3DCommandBufferImpl(0, // offscreen
97                                                           url,
98                                                           factory,
99                                                           swap_client));
100  context_->InitializeWithDefaultBufferSizes(
101      attrs,
102      false,
103      CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE);
104}
105
106CmdBufferImageTransportFactory::~CmdBufferImageTransportFactory() {
107}
108
109uint32_t CmdBufferImageTransportFactory::InsertSyncPoint() {
110  if (!context_->makeContextCurrent()) {
111    LOG(ERROR) << "Failed to make helper context current.";
112    return 0;
113  }
114  return context_->insertSyncPoint();
115}
116
117void CmdBufferImageTransportFactory::WaitSyncPoint(uint32_t sync_point) {
118  if (!context_->makeContextCurrent()) {
119    LOG(ERROR) << "Failed to make helper context current.";
120    return;
121  }
122  context_->waitSyncPoint(sync_point);
123}
124
125uint32_t CmdBufferImageTransportFactory::CreateTexture() {
126  if (!context_->makeContextCurrent()) {
127    LOG(ERROR) << "Failed to make helper context current.";
128    return false;
129  }
130  return context_->createTexture();
131}
132
133void CmdBufferImageTransportFactory::DeleteTexture(uint32_t id) {
134  if (!context_->makeContextCurrent()) {
135    LOG(ERROR) << "Failed to make helper context current.";
136    return;
137  }
138  context_->deleteTexture(id);
139}
140
141void CmdBufferImageTransportFactory::AcquireTexture(
142    uint32 texture_id, const signed char* mailbox_name) {
143  if (!context_->makeContextCurrent()) {
144    LOG(ERROR) << "Failed to make helper context current.";
145    return;
146  }
147  context_->bindTexture(GL_TEXTURE_2D, texture_id);
148  context_->consumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox_name);
149  context_->flush();
150}
151
152void CmdBufferImageTransportFactory::ReleaseTexture(
153    uint32 texture_id, const signed char* mailbox_name) {
154  if (!context_->makeContextCurrent()) {
155    LOG(ERROR) << "Failed to make helper context current.";
156    return;
157  }
158  context_->bindTexture(GL_TEXTURE_2D, texture_id);
159  context_->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox_name);
160}
161
162GLHelper* CmdBufferImageTransportFactory::GetGLHelper() {
163  if (!gl_helper_)
164    gl_helper_.reset(new GLHelper(context_.get()));
165
166  return gl_helper_.get();
167}
168
169}  // anonymous namespace
170
171// static
172ImageTransportFactoryAndroid* ImageTransportFactoryAndroid::GetInstance() {
173  if (!g_factory) {
174    if (CompositorImpl::UsesDirectGL())
175      g_factory = new DirectGLImageTransportFactory();
176    else
177      g_factory = new CmdBufferImageTransportFactory();
178  }
179
180  return g_factory;
181}
182
183ImageTransportFactoryAndroid::ImageTransportFactoryAndroid() {
184}
185
186ImageTransportFactoryAndroid::~ImageTransportFactoryAndroid() {
187}
188
189} // namespace content
190