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 #ifndef ANDROID_WEBVIEW_BROWSER_SHARED_RENDERER_STATE_H_ 6 #define ANDROID_WEBVIEW_BROWSER_SHARED_RENDERER_STATE_H_ 7 8 #include "android_webview/browser/parent_compositor_draw_constraints.h" 9 #include "base/cancelable_callback.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/synchronization/lock.h" 13 #include "cc/output/compositor_frame.h" 14 #include "cc/output/compositor_frame_ack.h" 15 #include "ui/gfx/geometry/rect.h" 16 #include "ui/gfx/geometry/vector2d.h" 17 18 namespace cc { 19 class CompositorFrameAck; 20 } 21 22 namespace gpu { 23 class GLInProcessContext; 24 } 25 26 namespace android_webview { 27 28 namespace internal { 29 class RequestDrawGLTracker; 30 } 31 32 class BrowserViewRendererClient; 33 class InsideHardwareReleaseReset; 34 35 // Set by BrowserViewRenderer and read by HardwareRenderer. 36 struct DrawGLInput { 37 gfx::Vector2d scroll_offset; 38 int width; 39 int height; 40 cc::CompositorFrame frame; 41 42 DrawGLInput(); 43 ~DrawGLInput(); 44 }; 45 46 // This class is used to pass data between UI thread and RenderThread. 47 class SharedRendererState { 48 public: 49 SharedRendererState(scoped_refptr<base::MessageLoopProxy> ui_loop, 50 BrowserViewRendererClient* client); 51 ~SharedRendererState(); 52 53 void ClientRequestDrawGL(); 54 void DidDrawGLProcess(); 55 56 void SetDrawGLInput(scoped_ptr<DrawGLInput> input); 57 scoped_ptr<DrawGLInput> PassDrawGLInput(); 58 59 bool IsInsideHardwareRelease() const; 60 // Returns true if the draw constraints are updated. 61 bool UpdateDrawConstraints( 62 const ParentCompositorDrawConstraints& parent_draw_constraints); 63 void PostExternalDrawConstraintsToChildCompositor( 64 const ParentCompositorDrawConstraints& parent_draw_constraints); 65 66 const ParentCompositorDrawConstraints ParentDrawConstraints() const; 67 68 void SetSharedContext(gpu::GLInProcessContext* context); 69 gpu::GLInProcessContext* GetSharedContext() const; 70 71 void SetForceInvalidateOnNextDrawGL( 72 bool needs_force_invalidate_on_next_draw_gl); 73 bool NeedsForceInvalidateOnNextDrawGL() const; 74 75 void InsertReturnedResources(const cc::ReturnedResourceArray& resources); 76 void SwapReturnedResources(cc::ReturnedResourceArray* resources); 77 bool ReturnedResourcesEmpty() const; 78 79 private: 80 friend class InsideHardwareReleaseReset; 81 friend class internal::RequestDrawGLTracker; 82 83 void ResetRequestDrawGLCallback(); 84 void ClientRequestDrawGLOnUIThread(); 85 void UpdateParentDrawConstraintsOnUIThread(); 86 void SetInsideHardwareRelease(bool inside); 87 88 scoped_refptr<base::MessageLoopProxy> ui_loop_; 89 BrowserViewRendererClient* client_on_ui_; 90 base::WeakPtrFactory<SharedRendererState> weak_factory_on_ui_thread_; 91 base::WeakPtr<SharedRendererState> ui_thread_weak_ptr_; 92 base::CancelableClosure request_draw_gl_cancelable_closure_; 93 94 // Accessed by both UI and RT thread. 95 mutable base::Lock lock_; 96 scoped_ptr<DrawGLInput> draw_gl_input_; 97 bool inside_hardware_release_; 98 bool needs_force_invalidate_on_next_draw_gl_; 99 ParentCompositorDrawConstraints parent_draw_constraints_; 100 gpu::GLInProcessContext* share_context_; 101 cc::ReturnedResourceArray returned_resources_; 102 base::Closure request_draw_gl_closure_; 103 104 DISALLOW_COPY_AND_ASSIGN(SharedRendererState); 105 }; 106 107 class InsideHardwareReleaseReset { 108 public: 109 explicit InsideHardwareReleaseReset( 110 SharedRendererState* shared_renderer_state); 111 ~InsideHardwareReleaseReset(); 112 113 private: 114 SharedRendererState* shared_renderer_state_; 115 116 DISALLOW_COPY_AND_ASSIGN(InsideHardwareReleaseReset); 117 }; 118 119 } // namespace android_webview 120 121 #endif // ANDROID_WEBVIEW_BROWSER_SHARED_RENDERER_STATE_H_ 122