| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2022 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "CompositionLayer.h" |
| 5 | |
| 6 | #include "XRState.h" |
| 7 | |
| 8 | using namespace osgXR; |
| 9 | |
| 10 | // Internal API |
| 11 | |
| 12 | CompositionLayer::Private::Private(XRState *state) : |
| 13 | _state(state), |
| 14 | _visible(true), |
| 15 | _order(1), // in front of perspective layer |
| 16 | _alphaMode(BLEND_NONE) |
| 17 | { |
| 18 | state->addCompositionLayer(this); |
| 19 | } |
| 20 | |
| 21 | CompositionLayer::Private::~Private() |
| 22 | { |
| 23 | XRState *state = _state.get(); |
| 24 | if (state) |
| 25 | state->removeCompositionLayer(this); |
| 26 | } |
| 27 | |
| 28 | void CompositionLayer::Private::setVisible(bool visible) |
| 29 | { |
| 30 | _visible = visible; |
| 31 | } |
| 32 | |
| 33 | bool CompositionLayer::Private::getVisible() const |
| 34 | { |
| 35 | return _visible; |
| 36 | } |
| 37 | |
| 38 | void CompositionLayer::Private::setOrder(int order) |
| 39 | { |
| 40 | _order = order; |
| 41 | // FIXME reorder |
| 42 | } |
| 43 | |
| 44 | int CompositionLayer::Private::getOrder() const |
| 45 | { |
| 46 | return _order; |
| 47 | } |
| 48 | |
| 49 | void CompositionLayer::Private::setAlphaMode(AlphaMode mode) |
| 50 | { |
| 51 | _alphaMode = mode; |
| 52 | } |
| 53 | |
| 54 | CompositionLayer::AlphaMode CompositionLayer::Private::getAlphaMode() const |
| 55 | { |
| 56 | return _alphaMode; |
| 57 | } |
| 58 | |
| 59 | bool CompositionLayer::Private::writeCompositionLayer(OpenXR::Session *session, |
| 60 | OpenXR::CompositionLayer *layer, |
| 61 | bool disableAlpha) const |
| 62 | { |
| 63 | if (!_visible) |
| 64 | return false; |
| 65 | |
| 66 | XrCompositionLayerFlags flags = 0; |
| 67 | AlphaMode alphaMode = disableAlpha ? BLEND_NONE : _alphaMode; |
| 68 | switch (alphaMode) |
| 69 | { |
| 70 | case BLEND_NONE: |
| 71 | break; |
| 72 | case BLEND_ALPHA_PREMULT: |
| 73 | flags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT; |
| 74 | break; |
| 75 | case BLEND_ALPHA_UNPREMULT: |
| 76 | flags |= XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT | |
| 77 | XR_COMPOSITION_LAYER_UNPREMULTIPLIED_ALPHA_BIT; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | layer->setLayerFlags(flags); |
| 82 | layer->setSpace(session->getLocalSpace()); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | // Public API |
| 87 | |
| 88 | CompositionLayer::CompositionLayer(Private *priv) : |
| 89 | _private(priv) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | CompositionLayer::~CompositionLayer() |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | void CompositionLayer::setVisible(bool visible) |
| 98 | { |
| 99 | _private->setVisible(visible); |
| 100 | } |
| 101 | |
| 102 | bool CompositionLayer::getVisible() const |
| 103 | { |
| 104 | return _private->getVisible(); |
| 105 | } |
| 106 | |
| 107 | void CompositionLayer::setOrder(int order) |
| 108 | { |
| 109 | _private->setOrder(order); |
| 110 | } |
| 111 | |
| 112 | int CompositionLayer::getOrder() const |
| 113 | { |
| 114 | return _private->getOrder(); |
| 115 | } |
| 116 | |
| 117 | void CompositionLayer::setAlphaMode(AlphaMode mode) |
| 118 | { |
| 119 | _private->setAlphaMode(mode); |
| 120 | } |
| 121 | |
| 122 | CompositionLayer::AlphaMode CompositionLayer::getAlphaMode() const |
| 123 | { |
| 124 | return _private->getAlphaMode(); |
| 125 | } |