Chromium Code Reviews| 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" |
| 11 #include "jni/HandleViewResources_jni.h" | 11 #include "jni/HandleViewResources_jni.h" |
| 12 #include "ui/gfx/android/java_bitmap.h" | 12 #include "ui/gfx/android/java_bitmap.h" |
| 13 #include "ui/gfx/skbitmap_operations.h" | |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 static SkBitmap CreateSkBitmapFromJavaBitmap( | 19 static SkBitmap CreateSkBitmapFromJavaBitmap( |
| 19 base::android::ScopedJavaLocalRef<jobject> jbitmap) { | 20 base::android::ScopedJavaLocalRef<jobject> jbitmap) { |
| 20 return jbitmap.is_null() | 21 return jbitmap.is_null() |
| 21 ? SkBitmap() | 22 ? SkBitmap() |
| 22 : CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jbitmap.obj())); | 23 : CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jbitmap.obj())); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 37 JNIEnv* env = base::android::AttachCurrentThread(); | 38 JNIEnv* env = base::android::AttachCurrentThread(); |
| 38 if (!context) | 39 if (!context) |
| 39 context = base::android::GetApplicationContext(); | 40 context = base::android::GetApplicationContext(); |
| 40 | 41 |
| 41 left_bitmap_ = CreateSkBitmapFromJavaBitmap( | 42 left_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 42 Java_HandleViewResources_getLeftHandleBitmap(env, context)); | 43 Java_HandleViewResources_getLeftHandleBitmap(env, context)); |
| 43 right_bitmap_ = CreateSkBitmapFromJavaBitmap( | 44 right_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 44 Java_HandleViewResources_getRightHandleBitmap(env, context)); | 45 Java_HandleViewResources_getRightHandleBitmap(env, context)); |
| 45 center_bitmap_ = CreateSkBitmapFromJavaBitmap( | 46 center_bitmap_ = CreateSkBitmapFromJavaBitmap( |
| 46 Java_HandleViewResources_getCenterHandleBitmap(env, context)); | 47 Java_HandleViewResources_getCenterHandleBitmap(env, context)); |
| 48 left_inverted_bitmap_ = SkBitmapOperations::Rotate( | |
|
jdduke (slow)
2015/04/14 00:00:32
There's no need to create new assets for a transfo
AviD
2015/04/23 14:45:16
Done.
| |
| 49 right_bitmap_, SkBitmapOperations::ROTATION_180_CW); | |
| 50 right_inverted_bitmap_ = SkBitmapOperations::Rotate( | |
| 51 left_bitmap_, SkBitmapOperations::ROTATION_180_CW); | |
| 52 center_inverted_bitmap_ = SkBitmapOperations::Rotate( | |
| 53 center_bitmap_, SkBitmapOperations::ROTATION_180_CW); | |
| 47 | 54 |
| 48 left_bitmap_.setImmutable(); | 55 left_bitmap_.setImmutable(); |
| 49 right_bitmap_.setImmutable(); | 56 right_bitmap_.setImmutable(); |
| 50 center_bitmap_.setImmutable(); | 57 center_bitmap_.setImmutable(); |
| 58 left_inverted_bitmap_.setImmutable(); | |
| 59 right_inverted_bitmap_.setImmutable(); | |
| 60 center_inverted_bitmap_.setImmutable(); | |
| 51 } | 61 } |
| 52 | 62 |
| 53 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) { | 63 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation, |
| 64 bool mirror_vertical, | |
| 65 bool mirror_horizontal) { | |
| 54 DCHECK(loaded_); | 66 DCHECK(loaded_); |
| 55 switch (orientation) { | 67 switch (orientation) { |
| 56 case ui::TouchHandleOrientation::LEFT: | 68 case ui::TouchHandleOrientation::LEFT: |
| 57 return left_bitmap_; | 69 if (mirror_vertical && mirror_horizontal) { |
| 70 // Inverted about both X and Y axes | |
| 71 return right_inverted_bitmap_; | |
| 72 } else if (mirror_vertical) { | |
| 73 // Inverted about X axis only | |
| 74 return left_inverted_bitmap_; | |
| 75 } else if (mirror_horizontal) { | |
| 76 // Inverted about Y axis only | |
| 77 return right_bitmap_; | |
| 78 } else { | |
| 79 // Normal Case | |
| 80 return left_bitmap_; | |
| 81 } | |
| 58 case ui::TouchHandleOrientation::RIGHT: | 82 case ui::TouchHandleOrientation::RIGHT: |
| 59 return right_bitmap_; | 83 if (mirror_vertical && mirror_horizontal) { |
| 84 // Inverted about both X and Y axes | |
| 85 return left_inverted_bitmap_; | |
| 86 } else if (mirror_vertical) { | |
| 87 // Inverted about X axis only | |
| 88 return right_inverted_bitmap_; | |
| 89 } else if (mirror_horizontal) { | |
| 90 // Inverted about Y axis only | |
| 91 return left_bitmap_; | |
| 92 } else { | |
| 93 // Normal Case | |
| 94 return right_bitmap_; | |
| 95 } | |
| 60 case ui::TouchHandleOrientation::CENTER: | 96 case ui::TouchHandleOrientation::CENTER: |
| 61 return center_bitmap_; | 97 if (mirror_vertical) { |
| 98 // Inverted about X axis | |
| 99 return center_inverted_bitmap_; | |
| 100 } else { | |
| 101 // Normal Case | |
| 102 return center_bitmap_; | |
| 103 } | |
| 62 case ui::TouchHandleOrientation::UNDEFINED: | 104 case ui::TouchHandleOrientation::UNDEFINED: |
| 63 NOTREACHED() << "Invalid touch handle orientation."; | 105 NOTREACHED() << "Invalid touch handle orientation."; |
| 64 }; | 106 }; |
| 65 return center_bitmap_; | 107 return center_bitmap_; |
| 66 } | 108 } |
| 67 | 109 |
| 68 private: | 110 private: |
| 69 SkBitmap left_bitmap_; | 111 SkBitmap left_bitmap_; |
| 70 SkBitmap right_bitmap_; | 112 SkBitmap right_bitmap_; |
| 71 SkBitmap center_bitmap_; | 113 SkBitmap center_bitmap_; |
| 114 SkBitmap left_inverted_bitmap_; | |
| 115 SkBitmap right_inverted_bitmap_; | |
| 116 SkBitmap center_inverted_bitmap_; | |
| 72 bool loaded_; | 117 bool loaded_; |
| 73 | 118 |
| 74 DISALLOW_COPY_AND_ASSIGN(HandleResources); | 119 DISALLOW_COPY_AND_ASSIGN(HandleResources); |
| 75 }; | 120 }; |
| 76 | 121 |
| 77 base::LazyInstance<HandleResources>::Leaky g_selection_resources; | 122 base::LazyInstance<HandleResources>::Leaky g_selection_resources; |
| 78 | 123 |
| 79 } // namespace | 124 } // namespace |
| 80 | 125 |
| 81 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( | 126 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 95 } | 140 } |
| 96 | 141 |
| 97 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { | 142 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) { |
| 98 layer_->SetIsDrawable(enabled); | 143 layer_->SetIsDrawable(enabled); |
| 99 // Force a position update in case the disabled layer's properties are stale. | 144 // Force a position update in case the disabled layer's properties are stale. |
| 100 if (enabled) | 145 if (enabled) |
| 101 UpdateLayerPosition(); | 146 UpdateLayerPosition(); |
| 102 } | 147 } |
| 103 | 148 |
| 104 void CompositedTouchHandleDrawable::SetOrientation( | 149 void CompositedTouchHandleDrawable::SetOrientation( |
| 105 ui::TouchHandleOrientation orientation) { | 150 ui::TouchHandleOrientation orientation, |
| 151 bool mirror_vertical, | |
| 152 bool mirror_horizontal) { | |
| 106 DCHECK(layer_->parent()); | 153 DCHECK(layer_->parent()); |
| 107 orientation_ = orientation; | 154 orientation_ = orientation; |
| 108 | 155 |
| 109 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation); | 156 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap( |
| 157 orientation, mirror_vertical, mirror_horizontal); | |
| 158 const int bitmap_height = bitmap.height(); | |
|
jdduke (slow)
2015/04/14 00:00:32
To set the layer transform, you can use an approac
AviD
2015/04/23 14:45:16
Done.
| |
| 159 const int bitmap_width = bitmap.width(); | |
| 160 int focal_offset_x = 0; | |
| 161 int focal_offset_y = mirror_vertical ? bitmap_height : 0; | |
| 162 | |
| 110 layer_->SetBitmap(bitmap); | 163 layer_->SetBitmap(bitmap); |
| 111 layer_->SetBounds(gfx::Size(bitmap.width(), bitmap.height())); | 164 layer_->SetBounds(gfx::Size(bitmap_width, bitmap_height)); |
| 112 | 165 |
| 113 switch (orientation_) { | 166 switch (orientation_) { |
| 114 case ui::TouchHandleOrientation::LEFT: | 167 case ui::TouchHandleOrientation::LEFT: |
| 115 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.75f, 0); | 168 focal_offset_x = |
| 169 mirror_horizontal ? bitmap_width * 0.25f : bitmap_width * 0.75f; | |
| 170 focal_offset_from_origin_ = | |
| 171 gfx::Vector2dF(focal_offset_x, focal_offset_y); | |
| 116 break; | 172 break; |
| 117 case ui::TouchHandleOrientation::RIGHT: | 173 case ui::TouchHandleOrientation::RIGHT: |
| 118 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.25f, 0); | 174 focal_offset_x = |
| 175 mirror_horizontal ? bitmap_width * 0.75f : bitmap_width * 0.25f; | |
| 176 focal_offset_from_origin_ = | |
| 177 gfx::Vector2dF(focal_offset_x, focal_offset_y); | |
| 119 break; | 178 break; |
| 120 case ui::TouchHandleOrientation::CENTER: | 179 case ui::TouchHandleOrientation::CENTER: |
| 121 focal_offset_from_origin_ = gfx::Vector2dF(bitmap.width() * 0.5f, 0); | 180 focal_offset_x = bitmap_width * 0.5f; |
| 181 focal_offset_from_origin_ = | |
| 182 gfx::Vector2dF(focal_offset_x, focal_offset_y); | |
| 122 break; | 183 break; |
| 123 case ui::TouchHandleOrientation::UNDEFINED: | 184 case ui::TouchHandleOrientation::UNDEFINED: |
| 124 NOTREACHED() << "Invalid touch handle orientation."; | 185 NOTREACHED() << "Invalid touch handle orientation."; |
| 125 break; | 186 break; |
| 126 }; | 187 }; |
| 127 | 188 |
| 128 UpdateLayerPosition(); | 189 UpdateLayerPosition(); |
| 129 } | 190 } |
| 130 | 191 |
| 131 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { | 192 void CompositedTouchHandleDrawable::SetAlpha(float alpha) { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 157 void CompositedTouchHandleDrawable::UpdateLayerPosition() { | 218 void CompositedTouchHandleDrawable::UpdateLayerPosition() { |
| 158 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); | 219 layer_->SetPosition(focal_position_ - focal_offset_from_origin_); |
| 159 } | 220 } |
| 160 | 221 |
| 161 // static | 222 // static |
| 162 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { | 223 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) { |
| 163 return RegisterNativesImpl(env); | 224 return RegisterNativesImpl(env); |
| 164 } | 225 } |
| 165 | 226 |
| 166 } // namespace content | 227 } // namespace content |
| OLD | NEW |