1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android/frameworks/vr/composer/1.0/IVrComposerClient.h>
18#include <hardware/gralloc.h>
19#include <hardware/gralloc1.h>
20#include <log/log.h>
21
22#include "impl/vr_hwc.h"
23#include "impl/vr_composer_client.h"
24
25namespace android {
26namespace dvr {
27
28using android::hardware::graphics::common::V1_0::PixelFormat;
29using android::frameworks::vr::composer::V1_0::IVrComposerClient;
30
31VrComposerClient::VrComposerClient(dvr::VrHwc& hal)
32    : ComposerClient(&hal), mVrHal(hal) {
33  if (!init()) {
34      LOG_ALWAYS_FATAL("failed to initialize VrComposerClient");
35  }
36}
37
38VrComposerClient::~VrComposerClient() {}
39
40std::unique_ptr<ComposerCommandEngine>
41VrComposerClient::createCommandEngine() {
42  return std::unique_ptr<VrCommandEngine>(new VrCommandEngine(*this));
43}
44
45VrComposerClient::VrCommandEngine::VrCommandEngine(VrComposerClient& client)
46    : ComposerCommandEngine(client.mHal, client.mResources.get()), mVrClient(client),
47      mVrHal(client.mVrHal) {}
48
49VrComposerClient::VrCommandEngine::~VrCommandEngine() {}
50
51bool VrComposerClient::VrCommandEngine::executeCommand(
52    IComposerClient::Command command, uint16_t length) {
53  IVrComposerClient::VrCommand vrCommand =
54      static_cast<IVrComposerClient::VrCommand>(command);
55  switch (vrCommand) {
56    case IVrComposerClient::VrCommand::SET_LAYER_INFO:
57      return executeSetLayerInfo(length);
58    case IVrComposerClient::VrCommand::SET_CLIENT_TARGET_METADATA:
59      return executeSetClientTargetMetadata(length);
60    case IVrComposerClient::VrCommand::SET_LAYER_BUFFER_METADATA:
61      return executeSetLayerBufferMetadata(length);
62    default:
63      return ComposerCommandEngine::executeCommand(command, length);
64  }
65}
66
67bool VrComposerClient::VrCommandEngine::executeSetLayerInfo(uint16_t length) {
68  if (length != 2) {
69    return false;
70  }
71
72  auto err = mVrHal.setLayerInfo(mCurrentDisplay, mCurrentLayer, read(), read());
73  if (err != Error::NONE) {
74    mWriter.setError(getCommandLoc(), err);
75  }
76
77  return true;
78}
79
80bool VrComposerClient::VrCommandEngine::executeSetClientTargetMetadata(
81    uint16_t length) {
82  if (length != 7)
83    return false;
84
85  auto err = mVrHal.setClientTargetMetadata(mCurrentDisplay, readBufferMetadata());
86  if (err != Error::NONE)
87    mWriter.setError(getCommandLoc(), err);
88
89  return true;
90}
91
92bool VrComposerClient::VrCommandEngine::executeSetLayerBufferMetadata(
93    uint16_t length) {
94  if (length != 7)
95    return false;
96
97  auto err = mVrHal.setLayerBufferMetadata(mCurrentDisplay, mCurrentLayer,
98                                           readBufferMetadata());
99  if (err != Error::NONE)
100    mWriter.setError(getCommandLoc(), err);
101
102  return true;
103}
104
105IVrComposerClient::BufferMetadata
106VrComposerClient::VrCommandEngine::readBufferMetadata() {
107  IVrComposerClient::BufferMetadata metadata = {
108    .width = read(),
109    .height = read(),
110    .stride = read(),
111    .layerCount = read(),
112    .format = static_cast<PixelFormat>(readSigned()),
113    .usage = read64(),
114  };
115  return metadata;
116}
117
118}  // namespace dvr
119}  // namespace android
120