1 // Copyright 2015 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 "chrome/browser/android/compositor/scene_layer/static_tab_scene_layer.h"
7 #include "cc/layers/layer.h"
8 #include "chrome/browser/android/compositor/layer/content_layer.h"
9 #include "chrome/browser/android/compositor/layer_title_cache.h"
10 #include "chrome/browser/android/compositor/tab_content_manager.h"
11 #include "content/public/browser/android/compositor.h"
12 #include "jni/StaticTabSceneLayer_jni.h"
13 #include "third_party/skia/include/core/SkColor.h"
18 StaticTabSceneLayer::StaticTabSceneLayer(JNIEnv
* env
, jobject jobj
)
19 : SceneLayer(env
, jobj
),
21 background_color_(SK_ColorWHITE
),
25 StaticTabSceneLayer::~StaticTabSceneLayer() {
28 bool StaticTabSceneLayer::ShouldShowBackground() {
29 scoped_refptr
<cc::Layer
> parent
= layer_
->parent();
30 return parent
.get() && parent
->bounds() != layer_
->bounds();
33 SkColor
StaticTabSceneLayer::GetBackgroundColor() {
34 return background_color_
;
37 void StaticTabSceneLayer::UpdateTabLayer(JNIEnv
* env
,
39 jfloat content_viewport_x
,
40 jfloat content_viewport_y
,
41 jfloat content_viewport_width
,
42 jfloat content_viewport_height
,
43 jobject jtab_content_manager
,
45 jint toolbar_resource_id
,
46 jboolean can_use_live_layer
,
47 jboolean can_use_ntp_fallback
,
48 jint default_background_color
,
53 jfloat content_offset_y
,
54 jfloat static_to_view_blend
,
57 background_color_
= default_background_color
;
58 gfx::Size
content_viewport_size(content_viewport_width
,
59 content_viewport_height
);
60 gfx::Point
content_viewport_offset(content_viewport_x
, content_viewport_y
);
61 if (!content_layer_
.get()) {
62 chrome::android::TabContentManager
* tab_content_manager
=
63 chrome::android::TabContentManager::FromJavaObject(
64 jtab_content_manager
);
65 content_layer_
= chrome::android::ContentLayer::Create(tab_content_manager
);
68 // Only override the alpha of content layers when the static tab is first
69 // assigned to the layer tree.
70 float content_alpha_override
= 1.f
;
71 bool should_override_content_alpha
= last_set_tab_id_
!= id
;
72 last_set_tab_id_
= id
;
74 // Set up the content layer and move it to the proper position.
75 content_layer_
->layer()->SetBounds(gfx::Size(width
, height
));
76 content_layer_
->layer()->SetPosition(gfx::Point(x
, y
));
77 content_layer_
->SetProperties(
78 id
, can_use_live_layer
, can_use_ntp_fallback
, static_to_view_blend
,
79 should_override_content_alpha
, content_alpha_override
, saturation
,
80 gfx::Rect(content_viewport_size
), content_viewport_size
);
82 gfx::Size
content_bounds(0, 0);
83 content_bounds
= content_layer_
->layer()->bounds();
85 gfx::Size
actual_content_size(content_layer_
->GetContentSize());
87 bool view_and_content_have_same_orientation
=
88 (content_viewport_size
.width() > content_viewport_size
.height()) ==
89 (actual_content_size
.width() > actual_content_size
.height()) &&
90 actual_content_size
.width() > 0 && actual_content_size
.height() > 0 &&
91 content_viewport_size
.width() > 0 && content_viewport_size
.height() > 0;
93 // This may not be true for frames during rotation.
94 bool content_has_consistent_width
=
95 actual_content_size
.width() == content_bounds
.width() &&
96 actual_content_size
.width() == content_viewport_size
.width();
98 if (view_and_content_have_same_orientation
||
99 (content_has_consistent_width
&& content_layer_
->ShowingLiveLayer())) {
100 y
+= content_offset_y
;
102 // If our orientations are off and we have a static texture, or if we have
103 // a live layer of an unexpected width, move the texture in by the
104 // appropriate amount.
105 x
+= content_viewport_offset
.x();
106 y
+= content_viewport_offset
.y();
109 content_layer_
->layer()->SetPosition(gfx::Point(x
, y
));
110 content_layer_
->layer()->SetIsDrawable(true);
112 layer_
->AddChild(content_layer_
->layer());
114 // Only applies the brightness filter if the value has changed and is less
116 if (brightness
!= brightness_
) {
117 brightness_
= brightness
;
118 cc::FilterOperations filters
;
119 if (brightness_
< 1.f
)
120 filters
.Append(cc::FilterOperation::CreateBrightnessFilter(brightness_
));
121 layer_
->SetFilters(filters
);
125 void StaticTabSceneLayer::SetContentSceneLayer(JNIEnv
* env
,
127 jobject jcontent_scene_layer
) {
128 SceneLayer
* content_scene_layer
= FromJavaObject(env
, jcontent_scene_layer
);
129 if (content_scene_layer
&& content_scene_layer
->layer()) {
130 content_scene_layer_
= content_scene_layer
->layer();
131 if (content_scene_layer_
.get())
132 layer_
->AddChild(content_scene_layer_
);
133 } else if (content_scene_layer_
) {
134 content_scene_layer_
->RemoveFromParent();
135 content_scene_layer_
= nullptr;
139 static jlong
Init(JNIEnv
* env
, jobject jobj
) {
140 // This will automatically bind to the Java object and pass ownership there.
141 StaticTabSceneLayer
* scene_layer
= new StaticTabSceneLayer(env
, jobj
);
142 return reinterpret_cast<intptr_t>(scene_layer
);
145 bool RegisterStaticTabSceneLayer(JNIEnv
* env
) {
146 return RegisterNativesImpl(env
);
149 } // namespace android
150 } // namespace chrome