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.
4 
5 #ifndef COMPONENTS_EMBEDDER_SUPPORT_ANDROID_VIEW_CONTENT_VIEW_RENDER_VIEW_H_
6 #define COMPONENTS_EMBEDDER_SUPPORT_ANDROID_VIEW_CONTENT_VIEW_RENDER_VIEW_H_
7 
8 #include <memory>
9 
10 #include "base/android/jni_weak_ref.h"
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/android/compositor_client.h"
15 #include "ui/gfx/native_widget_types.h"
16 
17 namespace cc {
18 class Layer;
19 }
20 
21 namespace content {
22 class Compositor;
23 }  // namespace content
24 
25 namespace weblayer {
26 
27 class ContentViewRenderView : public content::CompositorClient {
28  public:
29   ContentViewRenderView(JNIEnv* env,
30                         jobject obj,
31                         gfx::NativeWindow root_window);
32 
compositor()33   content::Compositor* compositor() { return compositor_.get(); }
34 
root_container_layer()35   scoped_refptr<cc::Layer> root_container_layer() {
36     return root_container_layer_;
37   }
38 
39   // Height, in pixels.
height()40   int height() const { return height_; }
41   void SetHeightChangedListener(base::RepeatingClosure callback);
42 
43   // Methods called from Java via JNI -----------------------------------------
44   void Destroy(JNIEnv* env);
45   void SetCurrentWebContents(
46       JNIEnv* env,
47       const base::android::JavaParamRef<jobject>& jweb_contents);
48   void OnPhysicalBackingSizeChanged(
49       JNIEnv* env,
50       const base::android::JavaParamRef<jobject>& jweb_contents,
51       jint width,
52       jint height,
53       jboolean for_config_change);
54   void SurfaceCreated(JNIEnv* env);
55   void SurfaceDestroyed(JNIEnv* env, jboolean cache_back_buffer);
56   void SurfaceChanged(JNIEnv* env,
57                       jboolean can_be_used_with_surface_control,
58                       jint format,
59                       jint width,
60                       jint height,
61                       const base::android::JavaParamRef<jobject>& surface);
62   void SetNeedsRedraw(JNIEnv* env);
63   void EvictCachedSurface(JNIEnv* env);
64   base::android::ScopedJavaLocalRef<jobject> GetResourceManager(JNIEnv* env);
65   void UpdateBackgroundColor(JNIEnv* env);
66 
67   // CompositorClient implementation
68   void UpdateLayerTreeHost() override;
69   void DidSwapFrame(int pending_frames) override;
70   void DidSwapBuffers(const gfx::Size& swap_size) override;
71 
72  private:
73   ~ContentViewRenderView() override;
74 
75   void InitCompositor();
76 
77   base::android::ScopedJavaGlobalRef<jobject> java_obj_;
78 
79   std::unique_ptr<content::Compositor> compositor_;
80 
81   gfx::NativeWindow root_window_;
82 
83   // Set as the root-layer of the compositor. Contains |web_contents_layer_|.
84   scoped_refptr<cc::Layer> root_container_layer_;
85   scoped_refptr<cc::Layer> web_contents_layer_;
86 
87   int current_surface_format_ = 0;
88 
89   base::RepeatingClosure height_changed_listener_;
90   int height_ = 0;
91 
92   DISALLOW_COPY_AND_ASSIGN(ContentViewRenderView);
93 };
94 
95 }  // namespace weblayer
96 
97 #endif  // COMPONENTS_EMBEDDER_SUPPORT_ANDROID_VIEW_CONTENT_VIEW_RENDER_VIEW_H_
98