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/2.0/IVrComposerClient.h>
18 #include <hardware/gralloc.h>
19 #include <hardware/gralloc1.h>
20 #include <log/log.h>
21
22 #include <memory>
23
24 #include "impl/vr_hwc.h"
25 #include "impl/vr_composer_client.h"
26
27 namespace android {
28 namespace dvr {
29
30 using android::frameworks::vr::composer::V2_0::IVrComposerClient;
31
VrComposerClient(dvr::VrHwc & hal)32 VrComposerClient::VrComposerClient(dvr::VrHwc& hal)
33 : ComposerClient(&hal), mVrHal(hal) {
34 if (!init()) {
35 LOG_ALWAYS_FATAL("failed to initialize VrComposerClient");
36 }
37 }
38
~VrComposerClient()39 VrComposerClient::~VrComposerClient() {}
40
41 std::unique_ptr<ComposerCommandEngine>
createCommandEngine()42 VrComposerClient::createCommandEngine() {
43 return std::make_unique<VrCommandEngine>(*this);
44 }
45
VrCommandEngine(VrComposerClient & client)46 VrComposerClient::VrCommandEngine::VrCommandEngine(VrComposerClient& client)
47 : ComposerCommandEngine(client.mHal, client.mResources.get()),
48 mVrHal(client.mVrHal) {}
49
~VrCommandEngine()50 VrComposerClient::VrCommandEngine::~VrCommandEngine() {}
51
executeCommand(hardware::graphics::composer::V2_1::IComposerClient::Command command,uint16_t length)52 bool VrComposerClient::VrCommandEngine::executeCommand(
53 hardware::graphics::composer::V2_1::IComposerClient::Command command,
54 uint16_t length) {
55 IVrComposerClient::VrCommand vrCommand =
56 static_cast<IVrComposerClient::VrCommand>(command);
57 switch (vrCommand) {
58 case IVrComposerClient::VrCommand::SET_LAYER_INFO:
59 return executeSetLayerInfo(length);
60 case IVrComposerClient::VrCommand::SET_CLIENT_TARGET_METADATA:
61 return executeSetClientTargetMetadata(length);
62 case IVrComposerClient::VrCommand::SET_LAYER_BUFFER_METADATA:
63 return executeSetLayerBufferMetadata(length);
64 default:
65 return ComposerCommandEngine::executeCommand(command, length);
66 }
67 }
68
executeSetLayerInfo(uint16_t length)69 bool VrComposerClient::VrCommandEngine::executeSetLayerInfo(uint16_t length) {
70 if (length != 2) {
71 return false;
72 }
73
74 auto err = mVrHal.setLayerInfo(mCurrentDisplay, mCurrentLayer, read(), read());
75 if (err != Error::NONE) {
76 mWriter->setError(getCommandLoc(), err);
77 }
78
79 return true;
80 }
81
executeSetClientTargetMetadata(uint16_t length)82 bool VrComposerClient::VrCommandEngine::executeSetClientTargetMetadata(
83 uint16_t length) {
84 if (length != 7)
85 return false;
86
87 auto err = mVrHal.setClientTargetMetadata(mCurrentDisplay, readBufferMetadata());
88 if (err != Error::NONE)
89 mWriter->setError(getCommandLoc(), err);
90
91 return true;
92 }
93
executeSetLayerBufferMetadata(uint16_t length)94 bool VrComposerClient::VrCommandEngine::executeSetLayerBufferMetadata(
95 uint16_t length) {
96 if (length != 7)
97 return false;
98
99 auto err = mVrHal.setLayerBufferMetadata(mCurrentDisplay, mCurrentLayer,
100 readBufferMetadata());
101 if (err != Error::NONE)
102 mWriter->setError(getCommandLoc(), err);
103
104 return true;
105 }
106
107 IVrComposerClient::BufferMetadata
readBufferMetadata()108 VrComposerClient::VrCommandEngine::readBufferMetadata() {
109 IVrComposerClient::BufferMetadata metadata = {
110 .width = read(),
111 .height = read(),
112 .stride = read(),
113 .layerCount = read(),
114 .format =
115 static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(
116 readSigned()),
117 .usage = read64(),
118 };
119 return metadata;
120 }
121
122 } // namespace dvr
123 } // namespace android
124