1/*
2 * Copyright (C) 2018 Metrological Group B.V.
3 * Copyright (C) 2018 Igalia S.L.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "NicosiaGC3DLayer.h"
31
32#if USE(NICOSIA) && USE(TEXTURE_MAPPER)
33
34#if USE(COORDINATED_GRAPHICS)
35#include "TextureMapperGL.h"
36#include "TextureMapperPlatformLayerBuffer.h"
37#include "TextureMapperPlatformLayerProxy.h"
38#endif
39
40#include "GLContext.h"
41
42namespace Nicosia {
43
44using namespace WebCore;
45
46GC3DLayer::GC3DLayer(GraphicsContext3D& context, GraphicsContext3D::RenderStyle renderStyle)
47 : m_context(context)
48 , m_contentLayer(Nicosia::ContentLayer::create(Nicosia::ContentLayerTextureMapperImpl::createFactory(*this)))
49{
50 switch (renderStyle) {
51 case GraphicsContext3D::RenderOffscreen:
52 m_glContext = GLContext::createOffscreenContext(&PlatformDisplay::sharedDisplayForCompositing());
53 break;
54 case GraphicsContext3D::RenderDirectlyToHostWindow:
55 ASSERT_NOT_REACHED();
56 break;
57 }
58}
59
60GC3DLayer::~GC3DLayer()
61{
62 downcast<ContentLayerTextureMapperImpl>(m_contentLayer->impl()).invalidateClient();
63}
64
65bool GC3DLayer::makeContextCurrent()
66{
67 ASSERT(m_glContext);
68 return m_glContext->makeContextCurrent();
69}
70
71PlatformGraphicsContext3D GC3DLayer::platformContext()
72{
73 ASSERT(m_glContext);
74 return m_glContext->platformContext();
75}
76
77void GC3DLayer::swapBuffersIfNeeded()
78{
79#if USE(COORDINATED_GRAPHICS)
80 if (m_context.layerComposited())
81 return;
82
83 m_context.prepareTexture();
84 IntSize textureSize(m_context.m_currentWidth, m_context.m_currentHeight);
85 TextureMapperGL::Flags flags = TextureMapperGL::ShouldFlipTexture | (m_context.m_attrs.alpha ? TextureMapperGL::ShouldBlend : 0);
86
87 {
88 auto& proxy = downcast<Nicosia::ContentLayerTextureMapperImpl>(m_contentLayer->impl()).proxy();
89
90 LockHolder holder(proxy.lock());
91 proxy.pushNextBuffer(std::make_unique<TextureMapperPlatformLayerBuffer>(m_context.m_compositorTexture, textureSize, flags, m_context.m_internalColorFormat));
92 }
93
94 m_context.markLayerComposited();
95#endif
96}
97
98} // namespace Nicosia
99
100#endif // USE(NICOSIA) && USE(TEXTURE_MAPPER)
101