| 1 | /* |
| 2 | * Copyright (C) 2011, 2012, 2017 Igalia S.L. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free |
| 16 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "TextureMapperGC3DPlatformLayer.h" |
| 22 | |
| 23 | #if ENABLE(GRAPHICS_CONTEXT_3D) && USE(TEXTURE_MAPPER) && !USE(NICOSIA) |
| 24 | |
| 25 | #include "BitmapTextureGL.h" |
| 26 | #include "GLContext.h" |
| 27 | #include "TextureMapperGLHeaders.h" |
| 28 | #include "TextureMapperPlatformLayerBuffer.h" |
| 29 | #include "TextureMapperPlatformLayerProxy.h" |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | TextureMapperGC3DPlatformLayer::TextureMapperGC3DPlatformLayer(GraphicsContext3D& context, GraphicsContext3D::RenderStyle renderStyle) |
| 34 | : m_context(context) |
| 35 | { |
| 36 | switch (renderStyle) { |
| 37 | case GraphicsContext3D::RenderOffscreen: |
| 38 | m_glContext = GLContext::createOffscreenContext(&PlatformDisplay::sharedDisplayForCompositing()); |
| 39 | break; |
| 40 | case GraphicsContext3D::RenderDirectlyToHostWindow: |
| 41 | ASSERT_NOT_REACHED(); |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | #if USE(COORDINATED_GRAPHICS) |
| 46 | m_platformLayerProxy = adoptRef(new TextureMapperPlatformLayerProxy()); |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | TextureMapperGC3DPlatformLayer::~TextureMapperGC3DPlatformLayer() |
| 51 | { |
| 52 | #if !USE(COORDINATED_GRAPHICS) |
| 53 | if (client()) |
| 54 | client()->platformLayerWillBeDestroyed(); |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | bool TextureMapperGC3DPlatformLayer::makeContextCurrent() |
| 59 | { |
| 60 | ASSERT(m_glContext); |
| 61 | return m_glContext->makeContextCurrent(); |
| 62 | } |
| 63 | |
| 64 | PlatformGraphicsContext3D TextureMapperGC3DPlatformLayer::platformContext() |
| 65 | { |
| 66 | ASSERT(m_glContext); |
| 67 | return m_glContext->platformContext(); |
| 68 | } |
| 69 | |
| 70 | #if USE(COORDINATED_GRAPHICS) |
| 71 | RefPtr<TextureMapperPlatformLayerProxy> TextureMapperGC3DPlatformLayer::proxy() const |
| 72 | { |
| 73 | return m_platformLayerProxy.copyRef(); |
| 74 | } |
| 75 | |
| 76 | void TextureMapperGC3DPlatformLayer::swapBuffersIfNeeded() |
| 77 | { |
| 78 | if (m_context.layerComposited()) |
| 79 | return; |
| 80 | |
| 81 | m_context.prepareTexture(); |
| 82 | IntSize textureSize(m_context.m_currentWidth, m_context.m_currentHeight); |
| 83 | TextureMapperGL::Flags flags = TextureMapperGL::ShouldFlipTexture | (m_context.m_attrs.alpha ? TextureMapperGL::ShouldBlend : 0); |
| 84 | |
| 85 | { |
| 86 | LockHolder holder(m_platformLayerProxy->lock()); |
| 87 | m_platformLayerProxy->pushNextBuffer(std::make_unique<TextureMapperPlatformLayerBuffer>(m_context.m_compositorTexture, textureSize, flags, m_context.m_internalColorFormat)); |
| 88 | } |
| 89 | |
| 90 | m_context.markLayerComposited(); |
| 91 | } |
| 92 | #else |
| 93 | void TextureMapperGC3DPlatformLayer::paintToTextureMapper(TextureMapper& textureMapper, const FloatRect& targetRect, const TransformationMatrix& matrix, float opacity) |
| 94 | { |
| 95 | ASSERT(m_glContext); |
| 96 | |
| 97 | m_context.markLayerComposited(); |
| 98 | |
| 99 | #if USE(TEXTURE_MAPPER_GL) |
| 100 | if (m_context.m_attrs.antialias && m_context.m_state.boundFBO == m_context.m_multisampleFBO) { |
| 101 | GLContext* previousActiveContext = GLContext::current(); |
| 102 | if (previousActiveContext != m_glContext.get()) |
| 103 | m_context.makeContextCurrent(); |
| 104 | |
| 105 | m_context.resolveMultisamplingIfNecessary(); |
| 106 | ::glBindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_context.m_state.boundFBO); |
| 107 | |
| 108 | if (previousActiveContext && previousActiveContext != m_glContext.get()) |
| 109 | previousActiveContext->makeContextCurrent(); |
| 110 | } |
| 111 | |
| 112 | TextureMapperGL& texmapGL = static_cast<TextureMapperGL&>(textureMapper); |
| 113 | TextureMapperGL::Flags flags = TextureMapperGL::ShouldFlipTexture | (m_context.m_attrs.alpha ? TextureMapperGL::ShouldBlend : 0); |
| 114 | IntSize textureSize(m_context.m_currentWidth, m_context.m_currentHeight); |
| 115 | texmapGL.drawTexture(m_context.m_texture, flags, textureSize, targetRect, matrix, opacity); |
| 116 | #endif // USE(TEXTURE_MAPPER_GL) |
| 117 | } |
| 118 | #endif // USE(COORDINATED_GRAPHICS) |
| 119 | |
| 120 | } // namespace WebCore |
| 121 | |
| 122 | #endif // ENABLE(GRAPHICS_CONTEXT_3D) && USE(TEXTURE_MAPPER) |
| 123 | |