Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / image_transport_factory_android.cc
blobc1ea043c00287e0e808950a3daddfd4dc7512654
1 // Copyright (c) 2012 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 "content/browser/renderer_host/image_transport_factory_android.h"
7 #include "base/memory/singleton.h"
8 #include "base/strings/stringprintf.h"
9 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
10 #include "content/browser/renderer_host/compositor_impl_android.h"
11 #include "content/common/gpu/client/gl_helper.h"
12 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
13 #include "content/common/gpu/gpu_process_launch_causes.h"
14 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
15 #include "third_party/khronos/GLES2/gl2.h"
16 #include "ui/gfx/android/device_display_info.h"
17 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
19 namespace content {
21 namespace {
23 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
25 static ImageTransportFactoryAndroid* g_factory = NULL;
27 class DirectGLImageTransportFactory : public ImageTransportFactoryAndroid {
28 public:
29 DirectGLImageTransportFactory();
30 virtual ~DirectGLImageTransportFactory();
32 virtual uint32_t InsertSyncPoint() OVERRIDE { return 0; }
33 virtual void WaitSyncPoint(uint32_t sync_point) OVERRIDE {}
34 virtual uint32_t CreateTexture() OVERRIDE {
35 return context_->createTexture();
37 virtual void DeleteTexture(uint32_t id) OVERRIDE {
38 context_->deleteTexture(id);
40 virtual void AcquireTexture(
41 uint32 texture_id, const signed char* mailbox_name) OVERRIDE {}
42 virtual WebKit::WebGraphicsContext3D* GetContext3D() OVERRIDE {
43 return context_.get();
45 virtual GLHelper* GetGLHelper() OVERRIDE { return NULL; }
47 private:
48 scoped_ptr<WebKit::WebGraphicsContext3D> context_;
50 DISALLOW_COPY_AND_ASSIGN(DirectGLImageTransportFactory);
53 DirectGLImageTransportFactory::DirectGLImageTransportFactory() {
54 WebKit::WebGraphicsContext3D::Attributes attrs;
55 attrs.shareResources = true;
56 attrs.noAutomaticFlushes = true;
57 context_ = webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl::
58 CreateViewContext(attrs, NULL);
59 if (context_->makeContextCurrent())
60 context_->pushGroupMarkerEXT(
61 base::StringPrintf("DirectGLImageTransportFactory-%p", this).c_str());
64 DirectGLImageTransportFactory::~DirectGLImageTransportFactory() {
67 class CmdBufferImageTransportFactory : public ImageTransportFactoryAndroid {
68 public:
69 CmdBufferImageTransportFactory();
70 virtual ~CmdBufferImageTransportFactory();
72 virtual uint32_t InsertSyncPoint() OVERRIDE;
73 virtual void WaitSyncPoint(uint32_t sync_point) OVERRIDE;
74 virtual uint32_t CreateTexture() OVERRIDE;
75 virtual void DeleteTexture(uint32_t id) OVERRIDE;
76 virtual void AcquireTexture(
77 uint32 texture_id, const signed char* mailbox_name) OVERRIDE;
78 virtual WebKit::WebGraphicsContext3D* GetContext3D() OVERRIDE {
79 return context_.get();
81 virtual GLHelper* GetGLHelper() OVERRIDE;
83 private:
84 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context_;
85 scoped_ptr<GLHelper> gl_helper_;
87 DISALLOW_COPY_AND_ASSIGN(CmdBufferImageTransportFactory);
90 CmdBufferImageTransportFactory::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 static const size_t kBytesPerPixel = 4;
101 gfx::DeviceDisplayInfo display_info;
102 size_t full_screen_texture_size_in_bytes =
103 display_info.GetDisplayHeight() *
104 display_info.GetDisplayWidth() *
105 kBytesPerPixel;
106 context_->Initialize(
107 attrs,
108 false,
109 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
110 64 * 1024, // command buffer size
111 std::min(full_screen_texture_size_in_bytes,
112 kDefaultStartTransferBufferSize),
113 kDefaultMinTransferBufferSize,
114 std::min(3 * full_screen_texture_size_in_bytes,
115 kDefaultMaxTransferBufferSize));
117 if (context_->makeContextCurrent())
118 context_->pushGroupMarkerEXT(
119 base::StringPrintf("CmdBufferImageTransportFactory-%p", this).c_str());
122 CmdBufferImageTransportFactory::~CmdBufferImageTransportFactory() {
125 uint32_t CmdBufferImageTransportFactory::InsertSyncPoint() {
126 if (!context_->makeContextCurrent()) {
127 LOG(ERROR) << "Failed to make helper context current.";
128 return 0;
130 return context_->insertSyncPoint();
133 void CmdBufferImageTransportFactory::WaitSyncPoint(uint32_t sync_point) {
134 if (!context_->makeContextCurrent()) {
135 LOG(ERROR) << "Failed to make helper context current.";
136 return;
138 context_->waitSyncPoint(sync_point);
141 uint32_t CmdBufferImageTransportFactory::CreateTexture() {
142 if (!context_->makeContextCurrent()) {
143 LOG(ERROR) << "Failed to make helper context current.";
144 return false;
146 return context_->createTexture();
149 void CmdBufferImageTransportFactory::DeleteTexture(uint32_t id) {
150 if (!context_->makeContextCurrent()) {
151 LOG(ERROR) << "Failed to make helper context current.";
152 return;
154 context_->deleteTexture(id);
157 void CmdBufferImageTransportFactory::AcquireTexture(
158 uint32 texture_id, const signed char* mailbox_name) {
159 if (!context_->makeContextCurrent()) {
160 LOG(ERROR) << "Failed to make helper context current.";
161 return;
163 context_->bindTexture(GL_TEXTURE_2D, texture_id);
164 context_->consumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox_name);
165 context_->flush();
168 GLHelper* CmdBufferImageTransportFactory::GetGLHelper() {
169 if (!gl_helper_)
170 gl_helper_.reset(new GLHelper(context_.get()));
172 return gl_helper_.get();
175 } // anonymous namespace
177 // static
178 ImageTransportFactoryAndroid* ImageTransportFactoryAndroid::GetInstance() {
179 if (!g_factory) {
180 if (CompositorImpl::UsesDirectGL())
181 g_factory = new DirectGLImageTransportFactory();
182 else
183 g_factory = new CmdBufferImageTransportFactory();
186 return g_factory;
189 ImageTransportFactoryAndroid::ImageTransportFactoryAndroid() {
192 ImageTransportFactoryAndroid::~ImageTransportFactoryAndroid() {
195 } // namespace content