~osomon/oxide/override-touchUngrabEvent

« back to all changes in this revision

Viewing changes to shared/browser/oxide_hybris_utils.cc

  • Committer: Chris Coulson
  • Date: 2016-01-08 21:31:20 UTC
  • Revision ID: chris.coulson@canonical.com-20160108213120-9v54434yvfmsd7lz
Be a bit more intelligent when detecting whether the hybris camera compatibility layer is available, or whether we're running on the Android EGL stack

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// vim:expandtab:shiftwidth=2:tabstop=2:
2
 
// Copyright (C) 2015 Canonical Ltd.
 
2
// Copyright (C) 2015-2016 Canonical Ltd.
3
3
 
4
4
// This library is free software; you can redistribute it and/or
5
5
// modify it under the terms of the GNU Lesser General Public
15
15
// License along with this library; if not, write to the Free Software
16
16
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 
18
 
#include "oxide_android_properties.h"
 
18
#include "oxide_hybris_utils.h"
19
19
 
20
 
#if defined(ENABLE_HYBRIS)
21
20
#include <cstdio>
22
21
#include <hybris/properties/properties.h>
 
22
 
 
23
#include "base/files/file_path.h"
 
24
#include "base/lazy_instance.h"
 
25
#include "base/logging.h"
 
26
#include "base/native_library.h"
23
27
#include "base/strings/stringprintf.h"
24
 
#endif
25
 
 
26
 
#include "base/logging.h"
27
 
#include "base/memory/singleton.h"
28
28
 
29
29
namespace oxide {
30
30
 
 
31
// Copied from base/sys_info_internal.h
 
32
template<typename T, T (*F)(void)>
 
33
class LazyHybrisValue {
 
34
 public:
 
35
  LazyHybrisValue()
 
36
      : value_(F()) { }
 
37
 
 
38
  ~LazyHybrisValue() { }
 
39
 
 
40
  const T& value() { return value_; }
 
41
 
 
42
 private:
 
43
  const T value_;
 
44
 
 
45
  DISALLOW_COPY_AND_ASSIGN(LazyHybrisValue);
 
46
};
 
47
 
 
48
struct DevicePropertyData {
 
49
  HybrisUtils::DeviceProperties properties;
 
50
  bool available;
 
51
};
 
52
 
31
53
namespace {
32
54
 
33
 
#if defined(ENABLE_HYBRIS)
34
55
std::string ParseOSVersion(const char* os_version_str) {
35
56
  int32_t major, minor, bugfix;
36
57
 
52
73
 
53
74
  return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
54
75
}
55
 
#endif
56
 
 
57
 
}
58
 
 
59
 
AndroidProperties::AndroidProperties()
60
 
    : available_(false) {
61
 
#if defined(ENABLE_HYBRIS)
 
76
 
 
77
DevicePropertyData PopulateDeviceProperties() {
 
78
  DevicePropertyData data;
 
79
  data.available = false;
 
80
 
62
81
  char value[PROP_VALUE_MAX];
63
82
 
64
83
  if (::property_get("ro.product.name", value, nullptr) <= 0) {
65
 
    return;
 
84
    return data;
66
85
  }
67
86
 
68
 
  available_ = true;
69
 
  product_ = value;
 
87
  data.available = true;
 
88
  data.properties.product = value;
70
89
 
71
90
  ::property_get("ro.product.device", value, nullptr);
72
 
  device_ = value;
 
91
  data.properties.device = value;
73
92
 
74
93
  ::property_get("ro.product.board", value, nullptr);
75
 
  board_ = value;
 
94
  data.properties.board = value;
76
95
 
77
96
  ::property_get("ro.product.brand", value, nullptr);
78
 
  brand_ = value;
 
97
  data.properties.brand = value;
79
98
 
80
99
  ::property_get("ro.product.model", value, nullptr);
81
 
  model_ = value;
 
100
  data.properties.model = value;
82
101
 
83
102
  ::property_get("ro.build.version.release", value, nullptr);
84
 
  os_version_ = ParseOSVersion(value);
85
 
#endif
86
 
}
87
 
 
88
 
AndroidProperties::~AndroidProperties() {}
89
 
 
90
 
// static
91
 
AndroidProperties* AndroidProperties::GetInstance() {
92
 
  return base::Singleton<AndroidProperties>::get();
93
 
}
94
 
 
95
 
bool AndroidProperties::Available() const {
96
 
  return available_;
97
 
}
98
 
 
99
 
std::string AndroidProperties::GetProduct() const {
100
 
  DCHECK(available_);
101
 
  return product_;
102
 
}
103
 
 
104
 
std::string AndroidProperties::GetDevice() const {
105
 
  DCHECK(available_);
106
 
  return device_;
107
 
}
108
 
 
109
 
std::string AndroidProperties::GetBoard() const {
110
 
  DCHECK(available_);
111
 
  return board_;
112
 
}
113
 
 
114
 
std::string AndroidProperties::GetBrand() const {
115
 
  DCHECK(available_);
116
 
  return brand_;
117
 
}
118
 
 
119
 
std::string AndroidProperties::GetModel() const {
120
 
  DCHECK(available_);
121
 
  return model_;
122
 
}
123
 
 
124
 
std::string AndroidProperties::GetOSVersion() const {
125
 
  DCHECK(available_);
126
 
  return os_version_;
127
 
}
 
103
  data.properties.os_version = ParseOSVersion(value);
 
104
 
 
105
  return data;
 
106
}
 
107
 
 
108
bool UsingAndroidEGL() {
 
109
  base::NativeLibrary egl_lib =
 
110
      base::LoadNativeLibrary(base::FilePath("libEGL.so.1"), nullptr);
 
111
  if (!egl_lib) {
 
112
    return false;
 
113
  }
 
114
 
 
115
  return !!base::GetFunctionPointerFromNativeLibrary(
 
116
      egl_lib, "hybris_egl_display_get_mapping");
 
117
}
 
118
 
 
119
#if defined(ENABLE_HYBRIS_CAMERA)
 
120
bool CameraCompatAvailable() {
 
121
  base::NativeLibrary camera_lib =
 
122
      base::LoadNativeLibrary(base::FilePath("libcamera.so.1"), nullptr);
 
123
  DCHECK(camera_lib);
 
124
 
 
125
  typedef void (*hybris_camera_initialize_fn)();
 
126
  auto hybris_camera_initialize =
 
127
      reinterpret_cast<hybris_camera_initialize_fn>(
 
128
        base::GetFunctionPointerFromNativeLibrary(camera_lib,
 
129
                                                  "hybris_camera_initialize"));
 
130
  DCHECK(hybris_camera_initialize);
 
131
 
 
132
  hybris_camera_initialize();
 
133
 
 
134
  void** handle =
 
135
      reinterpret_cast<void**>(base::GetFunctionPointerFromNativeLibrary(
 
136
        camera_lib, "camera_handle"));
 
137
  DCHECK(handle);
 
138
 
 
139
  return !!*handle;
 
140
}
 
141
#endif
 
142
 
 
143
base::LazyInstance<
 
144
    LazyHybrisValue<DevicePropertyData, PopulateDeviceProperties>>
 
145
      g_device_properties = LAZY_INSTANCE_INITIALIZER;
 
146
base::LazyInstance<LazyHybrisValue<bool, UsingAndroidEGL>>
 
147
    g_using_android_egl = LAZY_INSTANCE_INITIALIZER;
 
148
#if defined(ENABLE_HYBRIS_CAMERA)
 
149
base::LazyInstance<LazyHybrisValue<bool, CameraCompatAvailable>>
 
150
    g_camera_compat_available = LAZY_INSTANCE_INITIALIZER;
 
151
#endif
 
152
 
 
153
}
 
154
 
 
155
// static
 
156
bool HybrisUtils::HasDeviceProperties() {
 
157
  return g_device_properties.Get().value().available;
 
158
}
 
159
 
 
160
// static
 
161
const HybrisUtils::DeviceProperties& HybrisUtils::GetDeviceProperties() {
 
162
  DCHECK(HasDeviceProperties());
 
163
  return g_device_properties.Get().value().properties;
 
164
}
 
165
 
 
166
// static
 
167
bool HybrisUtils::IsUsingAndroidEGL() {
 
168
  return g_using_android_egl.Get().value();
 
169
}
 
170
 
 
171
#if defined(ENABLE_HYBRIS_CAMERA)
 
172
// static
 
173
bool HybrisUtils::IsCameraCompatAvailable() {
 
174
  return g_camera_compat_available.Get().value();
 
175
}
 
176
#endif
128
177
 
129
178
} // namespace oxide