| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2022 James Hogan <[email protected]> |
| 3 | |
| 4 | #include <osgXR/CompositionLayerQuad> |
| 5 | #include <osgXR/Manager> |
| 6 | #include <osgXR/SubImage> |
| 7 | #include <osgXR/Swapchain> |
| 8 | |
| 9 | #include "OpenXR/Compositor.h" |
| 10 | |
| 11 | #include "CompositionLayer.h" |
| 12 | #include "Swapchain.h" |
| 13 | |
| 14 | using namespace osgXR; |
| 15 | |
| 16 | // Internal API |
| 17 | |
| 18 | namespace osgXR { |
| 19 | |
| 20 | class CompositionLayerPrivateQuad : public CompositionLayer::Private |
| 21 | { |
| 22 | public: |
| 23 | |
| 24 | typedef CompositionLayerQuad::EyeVisibility EyeVisibility; |
| 25 | |
| 26 | CompositionLayerPrivateQuad(XRState *state) : |
| 27 | CompositionLayer::Private(state), |
| 28 | _eyeVisibility(EyeVisibility::EYES_BOTH), |
| 29 | _orientation(0.0f, 0.0f, 0.0f, 1.0f), |
| 30 | _position(0.0f, 0.0f, -1.0f), |
| 31 | _size(1.0f, 1.0f) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | ~CompositionLayerPrivateQuad() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void setEyeVisibility(EyeVisibility eyes) |
| 40 | { |
| 41 | _eyeVisibility = eyes; |
| 42 | } |
| 43 | |
| 44 | EyeVisibility getEyeVisibility() const |
| 45 | { |
| 46 | return _eyeVisibility; |
| 47 | } |
| 48 | |
| 49 | void setSubImage(const SubImage &subImage) |
| 50 | { |
| 51 | _subImage = subImage; |
| 52 | } |
| 53 | |
| 54 | const SubImage &getSubImage() const |
| 55 | { |
| 56 | return _subImage; |
| 57 | } |
| 58 | |
| 59 | void setOrientation(const osg::Quat &quat) |
| 60 | { |
| 61 | _orientation = quat; |
| 62 | } |
| 63 | |
| 64 | const osg::Quat &getOrientation() const |
| 65 | { |
| 66 | return _orientation; |
| 67 | } |
| 68 | |
| 69 | void setPosition(const osg::Vec3f &pos) |
| 70 | { |
| 71 | _position = pos; |
| 72 | } |
| 73 | |
| 74 | const osg::Vec3f &getPosition() const |
| 75 | { |
| 76 | return _position; |
| 77 | } |
| 78 | |
| 79 | void setSize(const osg::Vec2f &size) |
| 80 | { |
| 81 | _size = size; |
| 82 | } |
| 83 | |
| 84 | const osg::Vec2f &getSize() const |
| 85 | { |
| 86 | return _size; |
| 87 | } |
| 88 | |
| 89 | bool setup(OpenXR::Session *session) override |
| 90 | { |
| 91 | Swapchain *swapchain = _subImage.getSwapchain(); |
| 92 | if (swapchain) |
| 93 | return Swapchain::Private::get(swapchain)->setup(_state.get(), session); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool writeCompositionLayerQuad(OpenXR::Session *session, |
| 98 | OpenXR::CompositionLayerQuad *layer) const |
| 99 | { |
| 100 | auto swapchain = Swapchain::Private::get(_subImage.getSwapchain()); |
| 101 | bool ret = writeCompositionLayer(session, layer, |
| 102 | swapchain->getForcedAlpha() >= 1.0f); |
| 103 | if (!ret) |
| 104 | return ret; |
| 105 | |
| 106 | _quadLayer->setEyeVisibility(static_cast<XrEyeVisibility>(_eyeVisibility)); |
| 107 | _quadLayer->setSubImage(swapchain->convertSubImage(_subImage)); |
| 108 | _quadLayer->setOrientation(_orientation); |
| 109 | _quadLayer->setPosition(_position); |
| 110 | _quadLayer->setSize(_size); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | void endFrame(OpenXR::Session::Frame *frame) override |
| 115 | { |
| 116 | Swapchain *swapchain = _subImage.getSwapchain(); |
| 117 | if (!swapchain) |
| 118 | return; |
| 119 | auto *swapchainPriv = Swapchain::Private::get(swapchain); |
| 120 | if (!swapchainPriv->released()) |
| 121 | return; |
| 122 | |
| 123 | _quadLayer = new OpenXR::CompositionLayerQuad(); |
| 124 | if (writeCompositionLayerQuad(frame->getSession(), _quadLayer)) |
| 125 | frame->addLayer(_quadLayer.get()); |
| 126 | } |
| 127 | |
| 128 | void cleanupSession() override |
| 129 | { |
| 130 | Swapchain *swapchain = _subImage.getSwapchain(); |
| 131 | if (swapchain) |
| 132 | Swapchain::Private::get(swapchain)->cleanupSession(); |
| 133 | } |
| 134 | |
| 135 | protected: |
| 136 | |
| 137 | EyeVisibility _eyeVisibility; |
| 138 | SubImage _subImage; |
| 139 | osg::Quat _orientation; |
| 140 | osg::Vec3f _position; |
| 141 | osg::Vec2f _size; |
| 142 | |
| 143 | osg::ref_ptr<OpenXR::CompositionLayerQuad> _quadLayer; |
| 144 | }; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | // Public API |
| 149 | |
| 150 | CompositionLayerQuad::CompositionLayerQuad(Manager *manager) : |
| 151 | CompositionLayer(new CompositionLayerPrivateQuad(manager->_getXrState())) |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | CompositionLayerQuad::~CompositionLayerQuad() |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | void CompositionLayerQuad::setEyeVisibility(EyeVisibility eyes) |
| 160 | { |
| 161 | auto priv = static_cast<CompositionLayerPrivateQuad *>(Private::get(this)); |
| 162 | priv->setEyeVisibility(eyes); |
| 163 | } |
| 164 | |
| 165 | CompositionLayerQuad::EyeVisibility CompositionLayerQuad::getEyeVisibility() const |
| 166 | { |
| 167 | auto priv = static_cast<const CompositionLayerPrivateQuad *>(Private::get(this)); |
| 168 | return priv->getEyeVisibility(); |
| 169 | } |
| 170 | |
| 171 | void CompositionLayerQuad::setSubImage(Swapchain *swapchain) |
| 172 | { |
| 173 | setSubImage((SubImage)swapchain); |
| 174 | } |
| 175 | |
| 176 | void CompositionLayerQuad::setSubImage(const SubImage &subImage) |
| 177 | { |
| 178 | auto priv = static_cast<CompositionLayerPrivateQuad *>(Private::get(this)); |
| 179 | priv->setSubImage(subImage); |
| 180 | } |
| 181 | |
| 182 | const SubImage &CompositionLayerQuad::getSubImage() const |
| 183 | { |
| 184 | auto priv = static_cast<const CompositionLayerPrivateQuad *>(Private::get(this)); |
| 185 | return priv->getSubImage(); |
| 186 | } |
| 187 | |
| 188 | void CompositionLayerQuad::setOrientation(const osg::Quat &quat) |
| 189 | { |
| 190 | auto priv = static_cast<CompositionLayerPrivateQuad *>(Private::get(this)); |
| 191 | priv->setOrientation(quat); |
| 192 | } |
| 193 | |
| 194 | const osg::Quat &CompositionLayerQuad::getOrientation() const |
| 195 | { |
| 196 | auto priv = static_cast<const CompositionLayerPrivateQuad *>(Private::get(this)); |
| 197 | return priv->getOrientation(); |
| 198 | } |
| 199 | |
| 200 | void CompositionLayerQuad::setPosition(const osg::Vec3f &pos) |
| 201 | { |
| 202 | auto priv = static_cast<CompositionLayerPrivateQuad *>(Private::get(this)); |
| 203 | priv->setPosition(pos); |
| 204 | } |
| 205 | |
| 206 | const osg::Vec3f &CompositionLayerQuad::getPosition() const |
| 207 | { |
| 208 | auto priv = static_cast<const CompositionLayerPrivateQuad *>(Private::get(this)); |
| 209 | return priv->getPosition(); |
| 210 | } |
| 211 | |
| 212 | void CompositionLayerQuad::setSize(const osg::Vec2f &size) |
| 213 | { |
| 214 | auto priv = static_cast<CompositionLayerPrivateQuad *>(Private::get(this)); |
| 215 | priv->setSize(size); |
| 216 | } |
| 217 | |
| 218 | const osg::Vec2f &CompositionLayerQuad::getSize() const |
| 219 | { |
| 220 | auto priv = static_cast<const CompositionLayerPrivateQuad *>(Private::get(this)); |
| 221 | return priv->getSize(); |
| 222 | } |