Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / shared_renderer_state.cc
1 // Copyright 2014 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.
4
5 #include "android_webview/browser/shared_renderer_state.h"
6
7 #include "android_webview/browser/browser_view_renderer_client.h"
8 #include "base/bind.h"
9 #include "base/location.h"
10
11 namespace android_webview {
12
13 DrawGLInput::DrawGLInput() : width(0), height(0) {
14 }
15
16 DrawGLInput::~DrawGLInput() {
17 }
18
19 SharedRendererState::SharedRendererState(
20     scoped_refptr<base::MessageLoopProxy> ui_loop,
21     BrowserViewRendererClient* client)
22     : ui_loop_(ui_loop),
23       client_on_ui_(client),
24       weak_factory_on_ui_thread_(this),
25       ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()),
26       hardware_allowed_(false),
27       share_context_(NULL) {
28   DCHECK(ui_loop_->BelongsToCurrentThread());
29   DCHECK(client_on_ui_);
30 }
31
32 SharedRendererState::~SharedRendererState() {
33   DCHECK(ui_loop_->BelongsToCurrentThread());
34 }
35
36 void SharedRendererState::ClientRequestDrawGL() {
37   if (ui_loop_->BelongsToCurrentThread()) {
38     ClientRequestDrawGLOnUIThread();
39   } else {
40     ui_loop_->PostTask(
41         FROM_HERE,
42         base::Bind(&SharedRendererState::ClientRequestDrawGLOnUIThread,
43                    ui_thread_weak_ptr_));
44   }
45 }
46
47 void SharedRendererState::ClientRequestDrawGLOnUIThread() {
48   DCHECK(ui_loop_->BelongsToCurrentThread());
49   if (!client_on_ui_->RequestDrawGL(NULL, false)) {
50     LOG(ERROR) << "Failed to request GL process. Deadlock likely";
51   }
52 }
53
54 void SharedRendererState::SetDrawGLInput(scoped_ptr<DrawGLInput> input) {
55   base::AutoLock lock(lock_);
56   DCHECK(!draw_gl_input_.get());
57   draw_gl_input_ = input.Pass();
58 }
59
60 scoped_ptr<DrawGLInput> SharedRendererState::PassDrawGLInput() {
61   base::AutoLock lock(lock_);
62   return draw_gl_input_.Pass();
63 }
64
65 void SharedRendererState::SetHardwareAllowed(bool allowed) {
66   base::AutoLock lock(lock_);
67   hardware_allowed_ = allowed;
68 }
69
70 bool SharedRendererState::IsHardwareAllowed() const {
71   base::AutoLock lock(lock_);
72   return hardware_allowed_;
73 }
74
75 void SharedRendererState::SetSharedContext(gpu::GLInProcessContext* context) {
76   base::AutoLock lock(lock_);
77   DCHECK(!share_context_ || !context);
78   share_context_ = context;
79 }
80
81 gpu::GLInProcessContext* SharedRendererState::GetSharedContext() const {
82   base::AutoLock lock(lock_);
83   DCHECK(share_context_);
84   return share_context_;
85 }
86
87 void SharedRendererState::InsertReturnedResources(
88     const cc::ReturnedResourceArray& resources) {
89   base::AutoLock lock(lock_);
90   returned_resources_.insert(
91       returned_resources_.end(), resources.begin(), resources.end());
92 }
93
94 void SharedRendererState::SwapReturnedResources(
95     cc::ReturnedResourceArray* resources) {
96   DCHECK(resources->empty());
97   base::AutoLock lock(lock_);
98   resources->swap(returned_resources_);
99 }
100
101 bool SharedRendererState::ReturnedResourcesEmpty() const {
102   base::AutoLock lock(lock_);
103   return returned_resources_.empty();
104 }
105
106 }  // namespace android_webview