OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/composited_touch_handle_drawable.h" | 5 #include "content/browser/android/composited_touch_handle_drawable.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "cc/layers/ui_resource_layer.h" | 10 #include "cc/layers/ui_resource_layer.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 left_bitmap_ = CreateSkBitmapFromJavaBitmap( | 41 left_bitmap_ = CreateSkBitmapFromJavaBitmap( |
42 Java_HandleViewResources_getLeftHandleBitmap(env, context)); | 42 Java_HandleViewResources_getLeftHandleBitmap(env, context)); |
43 right_bitmap_ = CreateSkBitmapFromJavaBitmap( | 43 right_bitmap_ = CreateSkBitmapFromJavaBitmap( |
44 Java_HandleViewResources_getRightHandleBitmap(env, context)); | 44 Java_HandleViewResources_getRightHandleBitmap(env, context)); |
45 center_bitmap_ = CreateSkBitmapFromJavaBitmap( | 45 center_bitmap_ = CreateSkBitmapFromJavaBitmap( |
46 Java_HandleViewResources_getCenterHandleBitmap(env, context)); | 46 Java_HandleViewResources_getCenterHandleBitmap(env, context)); |
47 | 47 |
48 left_bitmap_.setImmutable(); | 48 left_bitmap_.setImmutable(); |
49 right_bitmap_.setImmutable(); | 49 right_bitmap_.setImmutable(); |
50 center_bitmap_.setImmutable(); | 50 center_bitmap_.setImmutable(); |
51 | |
52 drawable_padding_.set_height( | |
53 Java_HandleViewResources_getHandleVerticalPadding(env)); | |
54 drawable_padding_.set_width( | |
55 Java_HandleViewResources_getHandleHorizontalPadding(env)); | |
51 } | 56 } |
52 | 57 |
53 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { | 58 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { |
54 DCHECK(loaded_); | 59 DCHECK(loaded_); |
55 switch (orientation) { | 60 switch (orientation) { |
56 case ui::TouchHandleOrientation::LEFT: | 61 case ui::TouchHandleOrientation::LEFT: |
57 return left_bitmap_; | 62 return left_bitmap_; |
58 case ui::TouchHandleOrientation::RIGHT: | 63 case ui::TouchHandleOrientation::RIGHT: |
59 return right_bitmap_; | 64 return right_bitmap_; |
60 case ui::TouchHandleOrientation::CENTER: | 65 case ui::TouchHandleOrientation::CENTER: |
61 return center_bitmap_; | 66 return center_bitmap_; |
62 case ui::TouchHandleOrientation::UNDEFINED: | 67 case ui::TouchHandleOrientation::UNDEFINED: |
63 NOTREACHED() << "Invalid touch handle orientation."; | 68 NOTREACHED() << "Invalid touch handle orientation."; |
64 }; | 69 }; |
65 return center_bitmap_; | 70 return center_bitmap_; |
66 } | 71 } |
67 | 72 |
73 gfx::SizeF GetDrawablePadding() const { | |
74 DCHECK(loaded_); | |
75 return drawable_padding_; | |
76 } | |
77 | |
68 private: | 78 private: |
69 SkBitmap left_bitmap_; | 79 SkBitmap left_bitmap_; |
70 SkBitmap right_bitmap_; | 80 SkBitmap right_bitmap_; |
71 SkBitmap center_bitmap_; | 81 SkBitmap center_bitmap_; |
82 gfx::SizeF drawable_padding_; | |
72 bool loaded_; | 83 bool loaded_; |
73 | 84 |
74 DISALLOW_COPY_AND_ASSIGN(HandleResources); | 85 DISALLOW_COPY_AND_ASSIGN(HandleResources); |
75 }; | 86 }; |
76 | 87 |
77 base::LazyInstance<HandleResources>::Leaky g_selection_resources; | 88 base::LazyInstance<HandleResources>::Leaky g_selection_resources; |
78 | 89 |
79 } // namespace | 90 } // namespace |
80 | 91 |
81 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( | 92 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( |
82 cc::Layer* root_layer, | 93 cc::Layer* root_layer, |
83 float dpi_scale, | 94 float dpi_scale, |
84 jobject context) | 95 jobject context) |
85 : dpi_scale_(dpi_scale), | 96 : dpi_scale_(dpi_scale), |
86 orientation_(ui::TouchHandleOrientation::UNDEFINED), | 97 orientation_(ui::TouchHandleOrientation::UNDEFINED), |
87 layer_(cc::UIResourceLayer::Create()) { | 98 layer_(cc::UIResourceLayer::Create()) { |
88 g_selection_resources.Get().LoadIfNecessary(context); | 99 g_selection_resources.Get().LoadIfNecessary(context); |
100 drawable_padding_ = g_selection_resources.Get().GetDrawablePadding(); | |
89 DCHECK(root_layer); | 101 DCHECK(root_layer); |
90 root_layer->AddChild(layer_.get()); | 102 root_layer->AddChild(layer_.get()); |
91 } | 103 } |
92 | 104 |
93 CompositedTouchHandleDrawable::~CompositedTouchHandleDrawable() { | 105 CompositedTouchHandleDrawable::~CompositedTouchHandleDrawable() { |
94 DetachLayer(); | 106 DetachLayer(); |
95 } | 107 } |
96 | 108 |
97 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { | 109 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { |
98 layer_->SetIsDrawable(enabled); | 110 layer_->SetIsDrawable(enabled); |
99 // Force a position update in case the disabled layer's properties are stale. | 111 // Force a position update in case the disabled layer's properties are stale. |
100 if (enabled) | 112 if (enabled) |
101 UpdateLayerPosition(); | 113 UpdateLayerPosition(); |
102 } | 114 } |
103 | 115 |
104 void CompositedTouchHandleDrawable::SetOrientation( | 116 void CompositedTouchHandleDrawable::SetLayout( |
105 ui::TouchHandleOrientation orientation) { | 117 const gfx::PointF& position, |
118 ui::TouchHandleOrientation orientation, | |
119 bool mirror_vertical, | |
120 bool mirror_horizontal) { | |
106 DCHECK(layer_->parent()); | 121 DCHECK(layer_->parent()); |
122 bool orientation_changed = orientation_ != orientation; | |
123 focal_position_ = gfx::ScalePoint(position, dpi_scale_); | |
124 | |
107 orientation_ = orientation; | 125 orientation_ = orientation; |
108 | 126 |
109 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); | 127 if (orientation_changed) { |
110 layer_->SetBitmap(bitmap); | 128 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); |
111 layer_->SetBounds(gfx::Size(bitmap.width(), bitmap.height())); | 129 const int bitmap_height = bitmap.height(); |
130 const int bitmap_width = bitmap.width(); | |
131 layer_->SetBitmap(bitmap); | |
132 layer_->SetBounds(gfx::Size(bitmap_width, bitmap_height)); | |
133 } | |
112 | 134 |
135 const int layer_height = layer_->bounds().height(); | |
136 const int layer_width = layer_->bounds().width(); | |
137 | |
138 // Invert about X and Y axis based on the mirror values | |
139 gfx::Transform transform; | |
140 float scale_x = mirror_horizontal ? -1.f : 1.f; | |
141 float scale_y = mirror_vertical ? -1.f : 1.f; | |
142 | |
143 layer_->SetTransformOrigin( | |
144 gfx::Point3F(layer_width * 0.5f, layer_height * 0.5f, 0)); | |
145 transform.Scale(scale_x, scale_y); | |
146 layer_->SetTransform(transform); | |
147 | |
148 // Set the Focal offsets for the composited handle layer | |
149 // based on the orientation | |
150 int focal_offset_x = 0; | |
151 int focal_offset_y = mirror_vertical ? layer_height : 0; | |
113 switch (orientation_) { | 152 switch (orientation_) { |
114 case ui::TouchHandleOrientation::LEFT: | 153 case ui::TouchHandleOrientation::LEFT: |
115 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.75f, 0); | 154 focal_offset_x = |
155 mirror_horizontal ? layer_width * 0.25f : layer_width * 0.75f; | |
mfomitchev
2015/06/18 16:16:31
.25/.75 shouldn't be hard coded, but should be bas
AviD
2015/07/30 15:09:55
Done.
This logic is moved to TouchHandle
| |
116 break; | 156 break; |
117 case ui::TouchHandleOrientation::RIGHT: | 157 case ui::TouchHandleOrientation::RIGHT: |
118 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.25f, 0); | 158 focal_offset_x = |
159 mirror_horizontal ? layer_width * 0.75f : layer_width * 0.25f; | |
119 break; | 160 break; |
120 case ui::TouchHandleOrientation::CENTER: | 161 case ui::TouchHandleOrientation::CENTER: |
121 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.5f, 0); | 162 focal_offset_x = layer_width * 0.5f; |
122 break; | 163 break; |
123 case ui::TouchHandleOrientation::UNDEFINED: | 164 case ui::TouchHandleOrientation::UNDEFINED: |
124 NOTREACHED() << "Invalid touch handle orientation."; | 165 NOTREACHED() << "Invalid touch handle orientation."; |
125 break; | 166 break; |
126 }; | 167 }; |
127 | 168 |
169 focal_offset_from_origin_ = gfx::Vector2dF(focal_offset_x, focal_offset_y); | |
128 UpdateLayerPosition(); | 170 UpdateLayerPosition(); |
129 } | 171 } |
130 | 172 |
131 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { | 173 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { |
132 DCHECK(layer_->parent()); | 174 DCHECK(layer_->parent()); |
133 alpha = std::max(0.f, std::min(1.f, alpha)); | 175 alpha = std::max(0.f, std::min(1.f, alpha)); |
134 bool hidden = alpha <= 0; | 176 bool hidden = alpha <= 0; |
135 layer_->SetOpacity(alpha); | 177 layer_->SetOpacity(alpha); |
136 layer_->SetHideLayerAndSubtree(hidden); | 178 layer_->SetHideLayerAndSubtree(hidden); |
137 } | 179 } |
138 | 180 |
139 void CompositedTouchHandleDrawable::SetFocus(const gfx::PointF& position) { | |
140 DCHECK(layer_->parent()); | |
141 focal_position_ = gfx::ScalePoint(position, dpi_scale_); | |
142 UpdateLayerPosition(); | |
143 } | |
144 | |
145 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { | 181 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { |
146 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), | 182 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), |
147 layer_->position().y(), | 183 layer_->position().y(), |
148 layer_->bounds().width(), | 184 layer_->bounds().width(), |
149 layer_->bounds().height()), | 185 layer_->bounds().height()), |
150 1.f / dpi_scale_); | 186 1.f / dpi_scale_); |
151 } | 187 } |
152 | 188 |
189 gfx::SizeF CompositedTouchHandleDrawable::GetDrawablePadding() const { | |
190 return drawable_padding_; | |
191 } | |
192 | |
153 void CompositedTouchHandleDrawable::DetachLayer() { | 193 void CompositedTouchHandleDrawable::DetachLayer() { |
154 layer_->RemoveFromParent(); | 194 layer_->RemoveFromParent(); |
155 } | 195 } |
156 | 196 |
157 void CompositedTouchHandleDrawable::UpdateLayerPosition() { | 197 void CompositedTouchHandleDrawable::UpdateLayerPosition() { |
158 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); | 198 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); |
159 } | 199 } |
160 | 200 |
161 // static | 201 // static |
162 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { | 202 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { |
163 return RegisterNativesImpl(env); | 203 return RegisterNativesImpl(env); |
164 } | 204 } |
165 | 205 |
166 } // namespace content | 206 } // namespace content |
OLD | NEW |