vr_composer.cpp revision 15561b082ff4da3121be20d55b05a074ddd15a5c
1#include "vr_composer.h"
2
3#include <binder/IPCThreadState.h>
4#include <binder/PermissionCache.h>
5
6namespace android {
7namespace dvr {
8namespace {
9
10bool CheckPermission() {
11  const android::IPCThreadState* ipc = android::IPCThreadState::self();
12  const pid_t pid = ipc->getCallingPid();
13  const uid_t uid = ipc->getCallingUid();
14  const bool permission = PermissionCache::checkPermission(
15      String16("android.permission.RESTRICTED_VR_ACCESS"), pid, uid);
16  if (!permission)
17    ALOGE("permission denied to pid=%d uid=%u", pid, uid);
18
19  return permission;
20}
21
22}  // namespace
23
24VrComposer::VrComposer() {}
25
26VrComposer::~VrComposer() {}
27
28binder::Status VrComposer::registerObserver(
29    const sp<IVrComposerCallback>& callback) {
30  std::lock_guard<std::mutex> guard(mutex_);
31
32  if (!CheckPermission())
33    return binder::Status::fromStatusT(PERMISSION_DENIED);
34
35  if (callback_.get()) {
36    ALOGE("Failed to register callback, already registered");
37    return binder::Status::fromStatusT(ALREADY_EXISTS);
38  }
39
40  callback_ = callback;
41  IInterface::asBinder(callback_)->linkToDeath(this);
42  return binder::Status::ok();
43}
44
45binder::Status VrComposer::clearObserver() {
46  std::lock_guard<std::mutex> guard(mutex_);
47  callback_ = nullptr;
48  return binder::Status::ok();
49}
50
51base::unique_fd VrComposer::OnNewFrame(const ComposerView::Frame& frame) {
52  std::lock_guard<std::mutex> guard(mutex_);
53
54  if (!callback_.get())
55    return base::unique_fd();
56
57  ParcelableComposerFrame parcelable_frame(frame);
58  ParcelableUniqueFd fence;
59  binder::Status ret = callback_->onNewFrame(parcelable_frame, &fence);
60  if (!ret.isOk())
61    ALOGE("Failed to send new frame: %s", ret.toString8().string());
62
63  return fence.fence();
64}
65
66void VrComposer::binderDied(const wp<IBinder>& /* who */) {
67  std::lock_guard<std::mutex> guard(mutex_);
68
69  callback_ = nullptr;
70}
71
72}  // namespace dvr
73}  // namespace android
74