summaryrefslogtreecommitdiff
path: root/chromium/ui/android/handle_view_resources.cc
blob: 6bfd48771c5e0c9f1f34f09a4b46612c0b7e6f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/android/handle_view_resources.h"

#include "base/trace_event/trace_event.h"
#include "ui/android/ui_android_jni_headers/HandleViewResources_jni.h"

namespace {

static SkBitmap CreateSkBitmapFromJavaBitmap(
    base::android::ScopedJavaLocalRef<jobject> jbitmap) {
  return jbitmap.is_null()
             ? SkBitmap()
             : CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jbitmap));
}

}  // namespace

namespace ui {

HandleViewResources::HandleViewResources() {}

void HandleViewResources::LoadIfNecessary(const JavaRef<jobject>& context) {
  if (loaded_)
    return;

  loaded_ = true;

  TRACE_EVENT0("ui", "HandleViewResources::Create");
  JNIEnv* env = base::android::AttachCurrentThread();

  left_bitmap_ = CreateSkBitmapFromJavaBitmap(
      Java_HandleViewResources_getLeftHandleBitmap(env, context));
  right_bitmap_ = CreateSkBitmapFromJavaBitmap(
      Java_HandleViewResources_getRightHandleBitmap(env, context));
  center_bitmap_ = CreateSkBitmapFromJavaBitmap(
      Java_HandleViewResources_getCenterHandleBitmap(env, context));

  left_bitmap_.setImmutable();
  right_bitmap_.setImmutable();
  center_bitmap_.setImmutable();

  drawable_horizontal_padding_ratio_ =
      Java_HandleViewResources_getHandleHorizontalPaddingRatio(env);
}

const SkBitmap& HandleViewResources::GetBitmap(
    ui::TouchHandleOrientation orientation) {
  DCHECK(loaded_);
  switch (orientation) {
    case ui::TouchHandleOrientation::LEFT:
      return left_bitmap_;
    case ui::TouchHandleOrientation::RIGHT:
      return right_bitmap_;
    case ui::TouchHandleOrientation::CENTER:
      return center_bitmap_;
    case ui::TouchHandleOrientation::UNDEFINED:
      NOTREACHED() << "Invalid touch handle orientation.";
  };
  return center_bitmap_;
}

float HandleViewResources::GetDrawableHorizontalPaddingRatio() const {
  DCHECK(loaded_);
  return drawable_horizontal_padding_ratio_;
}

}  // namespace ui