[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / android / composited_touch_handle_drawable.cc
blob502189651e72e6e35f7b3f35b91ff506f4b2f02f
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
3 // found in the LICENSE file.
5 #include "content/browser/android/composited_touch_handle_drawable.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/trace_event/trace_event.h"
10 #include "cc/layers/ui_resource_layer.h"
11 #include "content/public/browser/android/compositor.h"
12 #include "jni/HandleViewResources_jni.h"
13 #include "ui/gfx/android/java_bitmap.h"
15 namespace content {
17 namespace {
19 static SkBitmap CreateSkBitmapFromJavaBitmap(
20 base::android::ScopedJavaLocalRef<jobject> jbitmap) {
21 return jbitmap.is_null()
22 ? SkBitmap()
23 : CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jbitmap.obj()));
26 class HandleResources {
27 public:
28 HandleResources() : loaded_(false) {
31 void LoadIfNecessary(jobject context) {
32 if (loaded_)
33 return;
35 loaded_ = true;
37 TRACE_EVENT0("browser", "HandleResources::Create");
38 JNIEnv* env = base::android::AttachCurrentThread();
39 if (!context)
40 context = base::android::GetApplicationContext();
42 left_bitmap_ = CreateSkBitmapFromJavaBitmap(
43 Java_HandleViewResources_getLeftHandleBitmap(env, context));
44 right_bitmap_ = CreateSkBitmapFromJavaBitmap(
45 Java_HandleViewResources_getRightHandleBitmap(env, context));
46 center_bitmap_ = CreateSkBitmapFromJavaBitmap(
47 Java_HandleViewResources_getCenterHandleBitmap(env, context));
49 left_bitmap_.setImmutable();
50 right_bitmap_.setImmutable();
51 center_bitmap_.setImmutable();
54 const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) {
55 DCHECK(loaded_);
56 switch (orientation) {
57 case ui::TouchHandleOrientation::LEFT:
58 return left_bitmap_;
59 case ui::TouchHandleOrientation::RIGHT:
60 return right_bitmap_;
61 case ui::TouchHandleOrientation::CENTER:
62 return center_bitmap_;
63 case ui::TouchHandleOrientation::UNDEFINED:
64 NOTREACHED() << "Invalid touch handle orientation.";
66 return center_bitmap_;
69 private:
70 SkBitmap left_bitmap_;
71 SkBitmap right_bitmap_;
72 SkBitmap center_bitmap_;
73 bool loaded_;
75 DISALLOW_COPY_AND_ASSIGN(HandleResources);
78 base::LazyInstance<HandleResources>::Leaky g_selection_resources;
80 } // namespace
82 CompositedTouchHandleDrawable::CompositedTouchHandleDrawable(
83 cc::Layer* root_layer,
84 float dpi_scale,
85 jobject context)
86 : dpi_scale_(dpi_scale),
87 orientation_(ui::TouchHandleOrientation::UNDEFINED),
88 layer_(cc::UIResourceLayer::Create(Compositor::LayerSettings())) {
89 g_selection_resources.Get().LoadIfNecessary(context);
90 DCHECK(root_layer);
91 root_layer->AddChild(layer_.get());
94 CompositedTouchHandleDrawable::~CompositedTouchHandleDrawable() {
95 DetachLayer();
98 void CompositedTouchHandleDrawable::SetEnabled(bool enabled) {
99 layer_->SetIsDrawable(enabled);
100 // Force a position update in case the disabled layer's properties are stale.
101 if (enabled)
102 UpdateLayerPosition();
105 void CompositedTouchHandleDrawable::SetOrientation(
106 ui::TouchHandleOrientation orientation) {
107 DCHECK(layer_->parent());
108 orientation_ = orientation;
110 const SkBitmap& bitmap = g_selection_resources.Get().GetBitmap(orientation);
111 layer_->SetBitmap(bitmap);
112 layer_->SetBounds(gfx::Size(bitmap.width(), bitmap.height()));
114 switch (orientation_) {
115 case ui::TouchHandleOrientation::LEFT:
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;
129 UpdateLayerPosition();
132 void CompositedTouchHandleDrawable::SetAlpha(float alpha) {
133 DCHECK(layer_->parent());
134 alpha = std::max(0.f, std::min(1.f, alpha));
135 bool hidden = alpha <= 0;
136 layer_->SetOpacity(alpha);
137 layer_->SetHideLayerAndSubtree(hidden);
140 void CompositedTouchHandleDrawable::SetFocus(const gfx::PointF& position) {
141 DCHECK(layer_->parent());
142 focal_position_ = gfx::ScalePoint(position, dpi_scale_);
143 UpdateLayerPosition();
146 gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const {
147 return gfx::ScaleRect(gfx::RectF(layer_->position().x(),
148 layer_->position().y(),
149 layer_->bounds().width(),
150 layer_->bounds().height()),
151 1.f / dpi_scale_);
154 void CompositedTouchHandleDrawable::DetachLayer() {
155 layer_->RemoveFromParent();
158 void CompositedTouchHandleDrawable::UpdateLayerPosition() {
159 layer_->SetPosition(focal_position_ - focal_offset_from_origin_);
162 // static
163 bool CompositedTouchHandleDrawable::RegisterHandleViewResources(JNIEnv* env) {
164 return RegisterNativesImpl(env);
167 } // namespace content