1 // Copyright 2018 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 "chrome/browser/android/compositor/scene_layer/scrolling_bottom_view_scene_layer.h"
6 
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "cc/layers/ui_resource_layer.h"
10 #include "chrome/android/chrome_jni_headers/ScrollingBottomViewSceneLayer_jni.h"
11 #include "ui/android/resources/resource_manager_impl.h"
12 
13 using base::android::JavaParamRef;
14 using base::android::JavaRef;
15 
16 namespace android {
17 
ScrollingBottomViewSceneLayer(JNIEnv * env,const JavaRef<jobject> & jobj)18 ScrollingBottomViewSceneLayer::ScrollingBottomViewSceneLayer(
19     JNIEnv* env,
20     const JavaRef<jobject>& jobj)
21     : SceneLayer(env, jobj),
22       should_show_background_(false),
23       background_color_(SK_ColorWHITE),
24       view_container_(cc::Layer::Create()),
25       view_layer_(cc::UIResourceLayer::Create()) {
26   layer()->SetIsDrawable(true);
27 
28   view_container_->SetIsDrawable(true);
29   view_container_->SetMasksToBounds(true);
30 
31   view_layer_->SetIsDrawable(true);
32   view_container_->AddChild(view_layer_);
33 }
34 
35 ScrollingBottomViewSceneLayer::~ScrollingBottomViewSceneLayer() = default;
36 
UpdateScrollingBottomViewLayer(JNIEnv * env,const JavaParamRef<jobject> & object,const JavaParamRef<jobject> & jresource_manager,jint view_resource_id,jint shadow_height,jfloat x_offset,jfloat y_offset,bool show_shadow)37 void ScrollingBottomViewSceneLayer::UpdateScrollingBottomViewLayer(
38     JNIEnv* env,
39     const JavaParamRef<jobject>& object,
40     const JavaParamRef<jobject>& jresource_manager,
41     jint view_resource_id,
42     jint shadow_height,
43     jfloat x_offset,
44     jfloat y_offset,
45     bool show_shadow) {
46   ui::ResourceManager* resource_manager =
47       ui::ResourceManagerImpl::FromJavaObject(jresource_manager);
48   ui::Resource* bottom_view_resource = resource_manager->GetResource(
49       ui::ANDROID_RESOURCE_TYPE_DYNAMIC, view_resource_id);
50 
51   // If the resource isn't available, don't bother doing anything else.
52   if (!bottom_view_resource)
53     return;
54 
55   view_layer_->SetUIResourceId(bottom_view_resource->ui_resource()->id());
56 
57   int container_height = bottom_view_resource->size().height();
58   int texture_y_offset = 0;
59 
60   // The view container layer's height depends on whether the shadow is
61   // showing. If the shadow should be clipped, reduce the height of the
62   // container.
63   if (!show_shadow) {
64     container_height -= shadow_height;
65     texture_y_offset -= shadow_height;
66   }
67 
68   view_container_->SetBounds(
69       gfx::Size(bottom_view_resource->size().width(), container_height));
70   view_container_->SetPosition(gfx::PointF(0, y_offset - container_height));
71 
72   // The view's layer should be the same size as the texture.
73   view_layer_->SetBounds(gfx::Size(bottom_view_resource->size().width(),
74                                    bottom_view_resource->size().height()));
75   view_layer_->SetPosition(gfx::PointF(x_offset, texture_y_offset));
76 }
77 
SetContentTree(JNIEnv * env,const JavaParamRef<jobject> & jobj,const JavaParamRef<jobject> & jcontent_tree)78 void ScrollingBottomViewSceneLayer::SetContentTree(
79     JNIEnv* env,
80     const JavaParamRef<jobject>& jobj,
81     const JavaParamRef<jobject>& jcontent_tree) {
82   SceneLayer* content_tree = FromJavaObject(env, jcontent_tree);
83   if (!content_tree || !content_tree->layer())
84     return;
85 
86   if (!content_tree->layer()->parent() ||
87       (content_tree->layer()->parent()->id() != layer_->id())) {
88     layer_->AddChild(content_tree->layer());
89     layer_->AddChild(view_container_);
90   }
91 
92   // Propagate the background color up from the content layer.
93   should_show_background_ = content_tree->ShouldShowBackground();
94   background_color_ = content_tree->GetBackgroundColor();
95 }
96 
GetBackgroundColor()97 SkColor ScrollingBottomViewSceneLayer::GetBackgroundColor() {
98   return background_color_;
99 }
100 
ShouldShowBackground()101 bool ScrollingBottomViewSceneLayer::ShouldShowBackground() {
102   return should_show_background_;
103 }
104 
JNI_ScrollingBottomViewSceneLayer_Init(JNIEnv * env,const JavaParamRef<jobject> & jobj)105 static jlong JNI_ScrollingBottomViewSceneLayer_Init(
106     JNIEnv* env,
107     const JavaParamRef<jobject>& jobj) {
108   // This will automatically bind to the Java object and pass ownership there.
109   ScrollingBottomViewSceneLayer* scene_layer =
110       new ScrollingBottomViewSceneLayer(env, jobj);
111   return reinterpret_cast<intptr_t>(scene_layer);
112 }
113 
114 }  // namespace android
115