| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 left_bitmap_ = CreateSkBitmapFromJavaBitmap( | 42 left_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 43 Java_HandleViewResources_getLeftHandleBitmap(env, context)); | 43 Java_HandleViewResources_getLeftHandleBitmap(env, context)); |
| 44 right_bitmap_ = CreateSkBitmapFromJavaBitmap( | 44 right_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 45 Java_HandleViewResources_getRightHandleBitmap(env, context)); | 45 Java_HandleViewResources_getRightHandleBitmap(env, context)); |
| 46 center_bitmap_ = CreateSkBitmapFromJavaBitmap( | 46 center_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 47 Java_HandleViewResources_getCenterHandleBitmap(env, context)); | 47 Java_HandleViewResources_getCenterHandleBitmap(env, context)); |
| 48 | 48 |
| 49 left_bitmap_.setImmutable(); | 49 left_bitmap_.setImmutable(); |
| 50 right_bitmap_.setImmutable(); | 50 right_bitmap_.setImmutable(); |
| 51 center_bitmap_.setImmutable(); | 51 center_bitmap_.setImmutable(); |
| 52 |
| 53 drawable_horizontal_padding_ratio_ = |
| 54 Java_HandleViewResources_getHandleHorizontalPaddingRatio(env); |
| 52 } | 55 } |
| 53 | 56 |
| 54 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { | 57 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { |
| 55 DCHECK(loaded_); | 58 DCHECK(loaded_); |
| 56 switch (orientation) { | 59 switch (orientation) { |
| 57 case ui::TouchHandleOrientation::LEFT: | 60 case ui::TouchHandleOrientation::LEFT: |
| 58 return left_bitmap_; | 61 return left_bitmap_; |
| 59 case ui::TouchHandleOrientation::RIGHT: | 62 case ui::TouchHandleOrientation::RIGHT: |
| 60 return right_bitmap_; | 63 return right_bitmap_; |
| 61 case ui::TouchHandleOrientation::CENTER: | 64 case ui::TouchHandleOrientation::CENTER: |
| 62 return center_bitmap_; | 65 return center_bitmap_; |
| 63 case ui::TouchHandleOrientation::UNDEFINED: | 66 case ui::TouchHandleOrientation::UNDEFINED: |
| 64 NOTREACHED() << "Invalid touch handle orientation."; | 67 NOTREACHED() << "Invalid touch handle orientation."; |
| 65 }; | 68 }; |
| 66 return center_bitmap_; | 69 return center_bitmap_; |
| 67 } | 70 } |
| 68 | 71 |
| 72 float GetDrawableHorizontalPaddingRatio() const { |
| 73 DCHECK(loaded_); |
| 74 return drawable_horizontal_padding_ratio_; |
| 75 } |
| 76 |
| 69 private: | 77 private: |
| 70 SkBitmap left_bitmap_; | 78 SkBitmap left_bitmap_; |
| 71 SkBitmap right_bitmap_; | 79 SkBitmap right_bitmap_; |
| 72 SkBitmap center_bitmap_; | 80 SkBitmap center_bitmap_; |
| 81 float drawable_horizontal_padding_ratio_; |
| 73 bool loaded_; | 82 bool loaded_; |
| 74 | 83 |
| 75 DISALLOW_COPY_AND_ASSIGN(HandleResources); | 84 DISALLOW_COPY_AND_ASSIGN(HandleResources); |
| 76 }; | 85 }; |
| 77 | 86 |
| 78 base::LazyInstance<HandleResources>::Leaky g_selection_resources; | 87 base::LazyInstance<HandleResources>::Leaky g_selection_resources; |
| 79 | 88 |
| 80 } // namespace | 89 } // namespace |
| 81 | 90 |
| 82 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( | 91 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( |
| 83 cc::Layer* root_layer, | 92 cc::Layer* root_layer, |
| 84 float dpi_scale, | 93 float dpi_scale, |
| 85 jobject context) | 94 jobject context) |
| 86 : dpi_scale_(dpi_scale), | 95 : dpi_scale_(dpi_scale), |
| 87 orientation_(ui::TouchHandleOrientation::UNDEFINED), | 96 orientation_(ui::TouchHandleOrientation::UNDEFINED), |
| 88 layer_(cc::UIResourceLayer::Create(Compositor::LayerSettings())) { | 97 layer_(cc::UIResourceLayer::Create(Compositor::LayerSettings())) { |
| 89 g_selection_resources.Get().LoadIfNecessary(context); | 98 g_selection_resources.Get().LoadIfNecessary(context); |
| 99 drawable_horizontal_padding_ratio_ = |
| 100 g_selection_resources.Get().GetDrawableHorizontalPaddingRatio(); |
| 90 DCHECK(root_layer); | 101 DCHECK(root_layer); |
| 91 root_layer->AddChild(layer_.get()); | 102 root_layer->AddChild(layer_.get()); |
| 92 } | 103 } |
| 93 | 104 |
| 94 CompositedTouchHandleDrawable::~CompositedTouchHandleDrawable() { | 105 CompositedTouchHandleDrawable::~CompositedTouchHandleDrawable() { |
| 95 DetachLayer(); | 106 DetachLayer(); |
| 96 } | 107 } |
| 97 | 108 |
| 98 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { | 109 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { |
| 99 layer_->SetIsDrawable(enabled); | 110 layer_->SetIsDrawable(enabled); |
| 100 // 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. |
| 101 if (enabled) | 112 if (enabled) |
| 102 UpdateLayerPosition(); | 113 UpdateLayerPosition(); |
| 103 } | 114 } |
| 104 | 115 |
| 105 void CompositedTouchHandleDrawable::SetOrientation( | 116 void CompositedTouchHandleDrawable::SetOrientation( |
| 106 ui::TouchHandleOrientation orientation) { | 117 ui::TouchHandleOrientation orientation, |
| 118 bool mirror_vertical, |
| 119 bool mirror_horizontal) { |
| 107 DCHECK(layer_->parent()); | 120 DCHECK(layer_->parent()); |
| 121 bool orientation_changed = orientation_ != orientation; |
| 122 |
| 108 orientation_ = orientation; | 123 orientation_ = orientation; |
| 109 | 124 |
| 110 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); | 125 if (orientation_changed) { |
| 111 layer_->SetBitmap(bitmap); | 126 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); |
| 112 layer_->SetBounds(gfx::Size(bitmap.width(), bitmap.height())); | 127 const int bitmap_height = bitmap.height(); |
| 128 const int bitmap_width = bitmap.width(); |
| 129 layer_->SetBitmap(bitmap); |
| 130 layer_->SetBounds(gfx::Size(bitmap_width, bitmap_height)); |
| 131 } |
| 113 | 132 |
| 114 switch (orientation_) { | 133 const int layer_height = layer_->bounds().height(); |
| 115 case ui::TouchHandleOrientation::LEFT: | 134 const int layer_width = layer_->bounds().width(); |
| 116 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.75f, 0); | |
| 117 break; | |
| 118 case ui::TouchHandleOrientation::RIGHT: | |
| 119 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.25f, 0); | |
| 120 break; | |
| 121 case ui::TouchHandleOrientation::CENTER: | |
| 122 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.5f, 0); | |
| 123 break; | |
| 124 case ui::TouchHandleOrientation::UNDEFINED: | |
| 125 NOTREACHED() << "Invalid touch handle orientation."; | |
| 126 break; | |
| 127 }; | |
| 128 | 135 |
| 136 // Invert about X and Y axis based on the mirror values |
| 137 gfx::Transform transform; |
| 138 float scale_x = mirror_horizontal ? -1.f : 1.f; |
| 139 float scale_y = mirror_vertical ? -1.f : 1.f; |
| 140 |
| 141 layer_->SetTransformOrigin( |
| 142 gfx::Point3F(layer_width * 0.5f, layer_height * 0.5f, 0)); |
| 143 transform.Scale(scale_x, scale_y); |
| 144 layer_->SetTransform(transform); |
| 145 } |
| 146 |
| 147 void CompositedTouchHandleDrawable::SetOrigin(const gfx::PointF& origin) { |
| 148 origin_position_ = gfx::ScalePoint(origin, dpi_scale_); |
| 129 UpdateLayerPosition(); | 149 UpdateLayerPosition(); |
| 130 } | 150 } |
| 131 | 151 |
| 132 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { | 152 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { |
| 133 DCHECK(layer_->parent()); | 153 DCHECK(layer_->parent()); |
| 134 alpha = std::max(0.f, std::min(1.f, alpha)); | 154 alpha = std::max(0.f, std::min(1.f, alpha)); |
| 135 bool hidden = alpha <= 0; | 155 bool hidden = alpha <= 0; |
| 136 layer_->SetOpacity(alpha); | 156 layer_->SetOpacity(alpha); |
| 137 layer_->SetHideLayerAndSubtree(hidden); | 157 layer_->SetHideLayerAndSubtree(hidden); |
| 138 } | 158 } |
| 139 | 159 |
| 140 void CompositedTouchHandleDrawable::SetFocus(const gfx::PointF& position) { | |
| 141 DCHECK(layer_->parent()); | |
| 142 focal_position_ = gfx::ScalePoint(position, dpi_scale_); | |
| 143 UpdateLayerPosition(); | |
| 144 } | |
| 145 | |
| 146 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { | 160 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const { |
| 147 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), | 161 return gfx::ScaleRect(gfx::RectF(layer_->position().x(), |
| 148 layer_->position().y(), | 162 layer_->position().y(), |
| 149 layer_->bounds().width(), | 163 layer_->bounds().width(), |
| 150 layer_->bounds().height()), | 164 layer_->bounds().height()), |
| 151 1.f / dpi_scale_); | 165 1.f / dpi_scale_); |
| 152 } | 166 } |
| 153 | 167 |
| 168 float CompositedTouchHandleDrawable::GetDrawableHorizontalPaddingRatio() const { |
| 169 return drawable_horizontal_padding_ratio_; |
| 170 } |
| 171 |
| 154 void CompositedTouchHandleDrawable::DetachLayer() { | 172 void CompositedTouchHandleDrawable::DetachLayer() { |
| 155 layer_->RemoveFromParent(); | 173 layer_->RemoveFromParent(); |
| 156 } | 174 } |
| 157 | 175 |
| 158 void CompositedTouchHandleDrawable::UpdateLayerPosition() { | 176 void CompositedTouchHandleDrawable::UpdateLayerPosition() { |
| 159 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); | 177 layer_->SetPosition(origin_position_); |
| 160 } | 178 } |
| 161 | 179 |
| 162 // static | 180 // static |
| 163 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { | 181 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { |
| 164 return RegisterNativesImpl(env); | 182 return RegisterNativesImpl(env); |
| 165 } | 183 } |
| 166 | 184 |
| 167 } // namespace content | 185 } // namespace content |
| OLD | NEW |