summaryrefslogtreecommitdiff
path: root/common/hal/google_camera_hal/camera_device.cc
blob: 0520626431bf895d0d4ff86f310c14a26eb202a7 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "GCH_CameraDevice"
#define ATRACE_TAG ATRACE_TAG_CAMERA
#include "camera_device.h"

#include <dlfcn.h>
#include <errno.h>
#include <log/log.h>
#include <meminfo/procmeminfo.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <utils/Trace.h>

#include <thread>

#include "utils.h"
#include "vendor_tags.h"

using android::meminfo::ProcMemInfo;
using namespace android::meminfo;

namespace android {

void MadviseFileForRange(size_t madvise_size_limit_bytes, size_t map_size_bytes,
                         const uint8_t* map_begin, const uint8_t* map_end,
                         const std::string& file_name) {
  // Ideal blockTransferSize for madvising files (128KiB)
  static const size_t kIdealIoTransferSizeBytes = 128 * 1024;
  size_t target_size_bytes =
      std::min<size_t>(map_size_bytes, madvise_size_limit_bytes);
  if (target_size_bytes == 0) {
    return;
  }
  std::string trace_tag =
      "madvising " + file_name + " size=" + std::to_string(target_size_bytes);
  ATRACE_NAME(trace_tag.c_str());
  // Based on requested size (target_size_bytes)
  const uint8_t* target_pos = map_begin + target_size_bytes;

  // Clamp endOfFile if its past map_end
  if (target_pos > map_end) {
    target_pos = map_end;
  }

  // Madvise the whole file up to target_pos in chunks of
  // kIdealIoTransferSizeBytes (to MADV_WILLNEED)
  // Note:
  // madvise(MADV_WILLNEED) will prefetch max(fd readahead size, optimal
  // block size for device) per call, hence the need for chunks. (128KB is a
  // good default.)
  for (const uint8_t* madvise_start = map_begin; madvise_start < target_pos;
       madvise_start += kIdealIoTransferSizeBytes) {
    void* madvise_addr =
        const_cast<void*>(reinterpret_cast<const void*>(madvise_start));
    size_t madvise_length =
        std::min(kIdealIoTransferSizeBytes,
                 static_cast<size_t>(target_pos - madvise_start));
    int status = madvise(madvise_addr, madvise_length, MADV_WILLNEED);
    // In case of error we stop madvising rest of the file
    if (status < 0) {
      break;
    }
  }
}

static void ReadAheadVma(const Vma& vma, const size_t madvise_size_limit_bytes) {
  const uint8_t* map_begin = reinterpret_cast<uint8_t*>(vma.start);
  const uint8_t* map_end = reinterpret_cast<uint8_t*>(vma.end);
  MadviseFileForRange(madvise_size_limit_bytes,
                      static_cast<size_t>(map_end - map_begin), map_begin,
                      map_end, vma.name);
}

static void LoadLibraries(const std::vector<std::string>* libs) {
  auto vmaCollectorCb = [&libs](const Vma& vma) {
    const static size_t kMadviseSizeLimitBytes =
        std::numeric_limits<size_t>::max();
    // Read ahead for anonymous VMAs and for specific files.
    // vma.flags represents a VMAs rwx bits.
    if (vma.inode == 0 && !vma.is_shared && vma.flags) {
      ReadAheadVma(vma, kMadviseSizeLimitBytes);
    } else if (vma.inode != 0 && libs != nullptr &&
               std::any_of(libs->begin(), libs->end(),
                           [&vma](std::string lib_name) {
                             return lib_name.compare(vma.name) == 0;
                           })) {
      ReadAheadVma(vma, kMadviseSizeLimitBytes);
    }
    return true;
  };
  ProcMemInfo meminfo(getpid());
  meminfo.ForEachVmaFromMaps(vmaCollectorCb);
}

namespace google_camera_hal {

// HAL external capture session library path
#if GCH_HWL_USE_DLOPEN
#if defined(_LP64)
constexpr char kExternalCaptureSessionDir[] =
    "/vendor/lib64/camera/capture_sessions/";
#else  // defined(_LP64)
constexpr char kExternalCaptureSessionDir[] =
    "/vendor/lib/camera/capture_sessions/";
#endif
#endif

std::unique_ptr<CameraDevice> CameraDevice::Create(
    std::unique_ptr<CameraDeviceHwl> camera_device_hwl,
    CameraBufferAllocatorHwl* camera_allocator_hwl,
    const std::vector<std::string>* configure_streams_libs) {
  ATRACE_CALL();
  auto device = std::unique_ptr<CameraDevice>(new CameraDevice());

  if (device == nullptr) {
    ALOGE("%s: Creating CameraDevice failed.", __FUNCTION__);
    return nullptr;
  }

  status_t res =
      device->Initialize(std::move(camera_device_hwl), camera_allocator_hwl);
  if (res != OK) {
    ALOGE("%s: Initializing CameraDevice failed: %s (%d).", __FUNCTION__,
          strerror(-res), res);
    return nullptr;
  }

  ALOGI("%s: Created a camera device for public(%u)", __FUNCTION__,
        device->GetPublicCameraId());
  device->configure_streams_libs_ = configure_streams_libs;

  return device;
}

status_t CameraDevice::Initialize(
    std::unique_ptr<CameraDeviceHwl> camera_device_hwl,
    CameraBufferAllocatorHwl* camera_allocator_hwl) {
  ATRACE_CALL();
  if (camera_device_hwl == nullptr) {
    ALOGE("%s: camera_device_hwl cannot be nullptr.", __FUNCTION__);
    return BAD_VALUE;
  }

  public_camera_id_ = camera_device_hwl->GetCameraId();
  camera_device_hwl_ = std::move(camera_device_hwl);
  camera_allocator_hwl_ = camera_allocator_hwl;
  status_t res = LoadExternalCaptureSession();
  if (res != OK) {
    ALOGE("%s: Loading external capture sessions failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  std::unique_ptr<HalCameraMetadata> static_metadata;
  res = camera_device_hwl_->GetCameraCharacteristics(&static_metadata);
  if (res != OK) {
    ALOGE("%s: Getting camera characteristics failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  res = utils::GetStreamUseCases(static_metadata.get(), &stream_use_cases_);
  if (res != OK) {
    ALOGE("%s: Getting stream use cases failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  return OK;
}

status_t CameraDevice::GetResourceCost(CameraResourceCost* cost) {
  ATRACE_CALL();
  return camera_device_hwl_->GetResourceCost(cost);
}

status_t CameraDevice::GetCameraCharacteristics(
    std::unique_ptr<HalCameraMetadata>* characteristics) {
  ATRACE_CALL();
  status_t res = camera_device_hwl_->GetCameraCharacteristics(characteristics);
  if (res != OK) {
    ALOGE("%s: GetCameraCharacteristics() failed: %s (%d).", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  return hal_vendor_tag_utils::ModifyCharacteristicsKeys(characteristics->get());
}

status_t CameraDevice::GetPhysicalCameraCharacteristics(
    uint32_t physical_camera_id,
    std::unique_ptr<HalCameraMetadata>* characteristics) {
  ATRACE_CALL();
  status_t res = camera_device_hwl_->GetPhysicalCameraCharacteristics(
      physical_camera_id, characteristics);
  if (res != OK) {
    ALOGE("%s: GetPhysicalCameraCharacteristics() failed: %s (%d).",
          __FUNCTION__, strerror(-res), res);
    return res;
  }

  return hal_vendor_tag_utils::ModifyCharacteristicsKeys(characteristics->get());
}

status_t CameraDevice::SetTorchMode(TorchMode mode) {
  ATRACE_CALL();
  return camera_device_hwl_->SetTorchMode(mode);
}

status_t CameraDevice::TurnOnTorchWithStrengthLevel(int32_t torch_strength) {
  ATRACE_CALL();
  return camera_device_hwl_->TurnOnTorchWithStrengthLevel(torch_strength);
}

status_t CameraDevice::GetTorchStrengthLevel(int32_t& torch_strength) const {
  ATRACE_CALL();
  status_t res = camera_device_hwl_->GetTorchStrengthLevel(torch_strength);
  if (res != OK) {
    ALOGE("%s: GetTorchStrengthLevel() failed: %s (%d).", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  return res;
}

status_t CameraDevice::DumpState(int fd) {
  ATRACE_CALL();
  return camera_device_hwl_->DumpState(fd);
}

status_t CameraDevice::CreateCameraDeviceSession(
    std::unique_ptr<CameraDeviceSession>* session) {
  ATRACE_CALL();
  if (session == nullptr) {
    ALOGE("%s: session is nullptr.", __FUNCTION__);
    return BAD_VALUE;
  }

  std::unique_ptr<CameraDeviceSessionHwl> session_hwl;
  status_t res = camera_device_hwl_->CreateCameraDeviceSessionHwl(
      camera_allocator_hwl_, &session_hwl);
  if (res != OK) {
    ALOGE("%s: Creating a CameraDeviceSessionHwl failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  *session = CameraDeviceSession::Create(std::move(session_hwl),
                                         external_session_factory_entries_,
                                         camera_allocator_hwl_);
  if (*session == nullptr) {
    ALOGE("%s: Creating a CameraDeviceSession failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return UNKNOWN_ERROR;
  }

  std::thread t(LoadLibraries, configure_streams_libs_);
  t.detach();

  return OK;
}

bool CameraDevice::IsStreamCombinationSupported(
    const StreamConfiguration& stream_config) {
  if (!utils::IsStreamUseCaseSupported(stream_config, stream_use_cases_)) {
    return false;
  }

  bool supported =
      camera_device_hwl_->IsStreamCombinationSupported(stream_config);
  if (!supported) {
    ALOGD("%s: stream config is not supported.", __FUNCTION__);
  }

  return supported;
}

status_t CameraDevice::LoadExternalCaptureSession() {
  ATRACE_CALL();

  if (external_session_factory_entries_.size() > 0) {
    ALOGI("%s: External capture session libraries already loaded; skip.",
          __FUNCTION__);
    return OK;
  }

#if GCH_HWL_USE_DLOPEN
  for (const auto& lib_path :
       utils::FindLibraryPaths(kExternalCaptureSessionDir)) {
    ALOGI("%s: Loading %s", __FUNCTION__, lib_path.c_str());
    void* lib_handle = nullptr;
    // load shared library and never unload
    // TODO(b/...): Switch to using build-system based HWL
    //   loading and remove dlopen here?
    lib_handle = dlopen(lib_path.c_str(), RTLD_NOW);
    if (lib_handle == nullptr) {
      ALOGW("Failed loading %s.", lib_path.c_str());
      continue;
    }

    GetCaptureSessionFactoryFunc external_session_factory_t =
        reinterpret_cast<GetCaptureSessionFactoryFunc>(
            dlsym(lib_handle, "GetCaptureSessionFactory"));
    if (external_session_factory_t == nullptr) {
      ALOGE("%s: dlsym failed (%s) when loading %s.", __FUNCTION__,
            "GetCaptureSessionFactory", lib_path.c_str());
      dlclose(lib_handle);
      lib_handle = nullptr;
      continue;
    }

    external_session_factory_entries_.push_back(external_session_factory_t);
    external_capture_session_lib_handles_.push_back(lib_handle);
  }
#else
  if (GetCaptureSessionFactory) {
    external_session_factory_entries_.push_back(GetCaptureSessionFactory);
  }
#endif

  return OK;
}

CameraDevice::~CameraDevice() {
}

std::unique_ptr<google::camera_common::Profiler> CameraDevice::GetProfiler(
    uint32_t camera_id, int option) {
  return camera_device_hwl_->GetProfiler(camera_id, option);
}

}  // namespace google_camera_hal
}  // namespace android