1 #include "vr_composer.h"
2 
3 #include <binder/IPCThreadState.h>
4 #include <binder/PermissionCache.h>
5 
6 namespace android {
7 namespace dvr {
8 namespace {
9 
CheckPermission()10 bool 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 
VrComposer()24 VrComposer::VrComposer() {}
25 
~VrComposer()26 VrComposer::~VrComposer() {}
27 
registerObserver(const sp<IVrComposerCallback> & callback)28 binder::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 
OnNewFrame(const ComposerView::Frame & frame)45 base::unique_fd VrComposer::OnNewFrame(const ComposerView::Frame& frame) {
46   std::lock_guard<std::mutex> guard(mutex_);
47 
48   if (!callback_.get())
49     return base::unique_fd();
50 
51   ParcelableComposerFrame parcelable_frame(frame);
52   ParcelableUniqueFd fence;
53   binder::Status ret = callback_->onNewFrame(parcelable_frame, &fence);
54   if (!ret.isOk())
55     ALOGE("Failed to send new frame: %s", ret.toString8().string());
56 
57   return fence.fence();
58 }
59 
binderDied(const wp<IBinder> &)60 void VrComposer::binderDied(const wp<IBinder>& /* who */) {
61   std::lock_guard<std::mutex> guard(mutex_);
62 
63   callback_ = nullptr;
64 }
65 
66 }  // namespace dvr
67 }  // namespace android
68