1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 #include <tools/stream.hxx>
23 #include <vcl/opengl/OpenGLContext.hxx>
24 #include <vcl/opengl/OpenGLHelper.hxx>
28 #include <vcl/salbtype.hxx>
29 #include <vcl/pngwrite.hxx>
31 #include <opengl/framebuffer.hxx>
32 #include <opengl/texture.hxx>
33 #include <opengl/zone.hxx>
34 #include <opengl/RenderState.hxx>
39 constexpr GLenum constInternalFormat
= GL_RGBA8
;
41 } // end anonymous namespace
43 // texture with allocated size
44 ImplOpenGLTexture::ImplOpenGLTexture( int nWidth
, int nHeight
, bool bAllocate
) :
48 mnFilter( GL_NEAREST
),
51 OpenGLVCLContextZone aContextZone
;
53 auto& rState
= OpenGLContext::getVCLContext()->state();
54 TextureState::generate(mnTexture
);
55 rState
.texture().active(0);
56 rState
.texture().bind(mnTexture
);
58 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
60 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
62 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
64 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
68 glTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, nWidth
, nHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nullptr );
72 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " allocate" );
75 // texture with content retrieved from FBO
76 ImplOpenGLTexture::ImplOpenGLTexture( int nX
, int nY
, int nWidth
, int nHeight
) :
80 mnFilter( GL_NEAREST
),
83 OpenGLVCLContextZone aContextZone
;
85 // FIXME We need the window height here
86 // nY = GetHeight() - nHeight - nY;
88 auto& rState
= OpenGLContext::getVCLContext()->state();
89 TextureState::generate(mnTexture
);
90 rState
.texture().active(0);
91 rState
.texture().bind(mnTexture
);
93 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
95 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
97 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
99 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
101 glCopyTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, nX
, nY
, nWidth
, nHeight
, 0 );
104 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " from x" << nX
<< ", y" << nY
);
107 // texture from buffer data
108 ImplOpenGLTexture::ImplOpenGLTexture( int nWidth
, int nHeight
, int nFormat
, int nType
, void const * pData
) :
112 mnFilter( GL_NEAREST
),
115 OpenGLVCLContextZone aContextZone
;
117 auto& rState
= OpenGLContext::getVCLContext()->state();
118 TextureState::generate(mnTexture
);
119 rState
.texture().active(0);
120 rState
.texture().bind(mnTexture
);
122 glPixelStorei( GL_UNPACK_ALIGNMENT
, 1 );
124 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
126 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
128 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
130 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
132 glTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, mnWidth
, mnHeight
, 0, nFormat
, nType
, pData
);
135 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " from data" );
138 GLuint
ImplOpenGLTexture::AddStencil()
140 assert( mnOptStencil
== 0 );
142 glGenRenderbuffers( 1, &mnOptStencil
);
144 glBindRenderbuffer( GL_RENDERBUFFER
, mnOptStencil
);
146 VCL_GL_INFO( "Allocate stencil " << mnWidth
<< " x " << mnHeight
);
147 glRenderbufferStorage( GL_RENDERBUFFER
, GL_STENCIL_INDEX
,
150 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
156 ImplOpenGLTexture::~ImplOpenGLTexture()
158 VCL_GL_INFO( "~OpenGLTexture " << mnTexture
);
161 // During shutdown GL is already de-initialized, so we should not try to create a new context.
163 rtl::Reference
<OpenGLContext
> xContext
= OpenGLContext::getVCLContext(false);
166 // FIXME: this is really not optimal performance-wise.
168 // Check we have been correctly un-bound from all framebuffers.
169 ImplSVData
* pSVData
= ImplGetSVData();
170 rtl::Reference
<OpenGLContext
> pContext
= pSVData
->maGDIData
.mpLastContext
;
174 pContext
->makeCurrent();
175 pContext
->UnbindTextureFromFramebuffers( mnTexture
);
178 if( mnOptStencil
!= 0 )
180 glDeleteRenderbuffers( 1, &mnOptStencil
);
183 auto& rState
= pContext
->state();
184 rState
.texture().unbindAndDelete(mnTexture
);
195 bool ImplOpenGLTexture::InsertBuffer(int nX
, int nY
, int nWidth
, int nHeight
, int nFormat
, int nType
, sal_uInt8
const * pData
)
197 if (!pData
|| mnTexture
== 0)
200 rtl::Reference
<OpenGLContext
> xContext
= OpenGLContext::getVCLContext();
201 xContext
->state().texture().active(0);
202 xContext
->state().texture().bind(mnTexture
);
204 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
206 glTexSubImage2D(GL_TEXTURE_2D
, 0, nX
, mnHeight
- nY
- nHeight
, nWidth
, nHeight
, nFormat
, nType
, pData
);
209 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " Insert buff. to " << nX
<< " " << nY
210 << " size " << nWidth
<< "x" << nHeight
<< " from data" );
215 void ImplOpenGLTexture::InitializeSlotMechanism(int nInitialSlotSize
)
217 if (mpSlotReferences
)
220 mpSlotReferences
.reset(new std::vector
<int>(nInitialSlotSize
, 0));
223 void ImplOpenGLTexture::IncreaseRefCount(int nSlotNumber
)
225 if (mpSlotReferences
&& nSlotNumber
>= 0)
227 if (nSlotNumber
>= int(mpSlotReferences
->size()))
228 mpSlotReferences
->resize(nSlotNumber
+ 1, 0);
230 mpSlotReferences
->at(nSlotNumber
)++;
234 void ImplOpenGLTexture::DecreaseRefCount(int nSlotNumber
)
236 if (mpSlotReferences
&& nSlotNumber
>= 0)
238 if (nSlotNumber
>= int(mpSlotReferences
->size()))
239 mpSlotReferences
->resize(nSlotNumber
, 0);
241 mpSlotReferences
->at(nSlotNumber
)--;
243 if (mpSlotReferences
->at(nSlotNumber
) == 0 && mFunctSlotDeallocateCallback
)
245 mFunctSlotDeallocateCallback(nSlotNumber
);
250 OpenGLTexture::OpenGLTexture() :
251 maRect( 0, 0, 0, 0 ),
257 OpenGLTexture::OpenGLTexture(const std::shared_ptr
<ImplOpenGLTexture
>& rpImpl
, tools::Rectangle aRectangle
, int nSlotNumber
)
260 , mnSlotNumber(nSlotNumber
)
263 mpImpl
->IncreaseRefCount(nSlotNumber
);
266 OpenGLTexture::OpenGLTexture( int nWidth
, int nHeight
, bool bAllocate
)
267 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
268 , mpImpl(new ImplOpenGLTexture(nWidth
, nHeight
, bAllocate
))
273 OpenGLTexture::OpenGLTexture( int nX
, int nY
, int nWidth
, int nHeight
)
274 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
275 , mpImpl(new ImplOpenGLTexture(nX
, nY
, nWidth
, nHeight
))
280 OpenGLTexture::OpenGLTexture( int nWidth
, int nHeight
, int nFormat
, int nType
, void const * pData
)
281 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
282 , mpImpl(new ImplOpenGLTexture(nWidth
, nHeight
, nFormat
, nType
, pData
))
288 OpenGLTexture::OpenGLTexture(const OpenGLTexture
& rTexture
)
289 : maRect(rTexture
.maRect
)
290 , mpImpl(rTexture
.mpImpl
)
291 , mnSlotNumber(rTexture
.mnSlotNumber
)
294 mpImpl
->IncreaseRefCount(mnSlotNumber
);
297 OpenGLTexture::OpenGLTexture(OpenGLTexture
&& rTexture
)
298 : maRect(rTexture
.maRect
)
299 , mpImpl(std::move(rTexture
.mpImpl
))
300 , mnSlotNumber(rTexture
.mnSlotNumber
)
304 OpenGLTexture::OpenGLTexture( const OpenGLTexture
& rTexture
,
305 int nX
, int nY
, int nWidth
, int nHeight
)
307 maRect
= tools::Rectangle( Point( rTexture
.maRect
.Left() + nX
, rTexture
.maRect
.Top() + nY
),
308 Size( nWidth
, nHeight
) );
309 mpImpl
= rTexture
.mpImpl
;
310 mnSlotNumber
= rTexture
.mnSlotNumber
;
312 mpImpl
->IncreaseRefCount(mnSlotNumber
);
313 VCL_GL_INFO( "Copying texture " << Id() << " [" << maRect
.Left() << "," << maRect
.Top() << "] " << GetWidth() << "x" << GetHeight() );
316 OpenGLTexture::~OpenGLTexture()
319 mpImpl
->DecreaseRefCount(mnSlotNumber
);
322 bool OpenGLTexture::IsUnique() const
324 return !mpImpl
|| (mpImpl
.use_count() == 1);
327 GLuint
OpenGLTexture::Id() const
330 return mpImpl
->mnTexture
;
334 int OpenGLTexture::GetWidth() const
336 return maRect
.GetWidth();
339 int OpenGLTexture::GetHeight() const
341 return maRect
.GetHeight();
344 GLuint
OpenGLTexture::StencilId() const
346 return mpImpl
? mpImpl
->mnOptStencil
: 0;
349 GLuint
OpenGLTexture::AddStencil()
352 return mpImpl
->AddStencil();
357 void OpenGLTexture::GetCoord( GLfloat
* pCoord
, const SalTwoRect
& rPosAry
, bool bInverted
) const
359 VCL_GL_INFO( "Getting coord " << Id() << " [" << maRect
.Left() << "," << maRect
.Top() << "] " << GetWidth() << "x" << GetHeight() );
363 pCoord
[0] = pCoord
[1] = pCoord
[2] = pCoord
[3] = 0.0f
;
364 pCoord
[4] = pCoord
[5] = pCoord
[6] = pCoord
[7] = 0.0f
;
368 pCoord
[0] = pCoord
[2] = (maRect
.Left() + rPosAry
.mnSrcX
) / static_cast<double>(mpImpl
->mnWidth
);
369 pCoord
[4] = pCoord
[6] = (maRect
.Left() + rPosAry
.mnSrcX
+ rPosAry
.mnSrcWidth
) / static_cast<double>(mpImpl
->mnWidth
);
373 pCoord
[3] = pCoord
[5] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / static_cast<double>(mpImpl
->mnHeight
);
374 pCoord
[1] = pCoord
[7] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / static_cast<double>(mpImpl
->mnHeight
);
378 pCoord
[1] = pCoord
[7] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / static_cast<double>(mpImpl
->mnHeight
);
379 pCoord
[3] = pCoord
[5] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / static_cast<double>(mpImpl
->mnHeight
);
383 void OpenGLTexture::GetTextureRect(const SalTwoRect
& rPosAry
, GLfloat
& x1
, GLfloat
& x2
, GLfloat
& y1
, GLfloat
& y2
) const
387 double fTextureWidth(mpImpl
->mnWidth
);
388 double fTextureHeight(mpImpl
->mnHeight
);
390 x1
= (maRect
.Left() + rPosAry
.mnSrcX
) / fTextureWidth
;
391 x2
= (maRect
.Left() + rPosAry
.mnSrcX
+ rPosAry
.mnSrcWidth
) / fTextureWidth
;
393 y1
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / fTextureHeight
;
394 y2
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / fTextureHeight
;
399 void OpenGLTexture::FillCoords
<GL_TRIANGLE_FAN
>(std::vector
<GLfloat
>& rCoords
, const SalTwoRect
& rPosAry
) const
406 GetTextureRect(rPosAry
, x1
, x2
, y1
, y2
);
408 rCoords
.insert(rCoords
.end(), {
415 void OpenGLTexture::FillCoords
<GL_TRIANGLES
>(std::vector
<GLfloat
>& rCoords
, const SalTwoRect
& rPosAry
) const
422 GetTextureRect(rPosAry
, x1
, x2
, y1
, y2
);
424 rCoords
.insert(rCoords
.end(), {
425 x1
, y1
, x2
, y1
, x1
, y2
,
426 x1
, y2
, x2
, y1
, x2
, y2
430 void OpenGLTexture::GetWholeCoord( GLfloat
* pCoord
) const
432 if( GetWidth() != mpImpl
->mnWidth
|| GetHeight() != mpImpl
->mnHeight
)
434 pCoord
[0] = pCoord
[2] = maRect
.Left() / static_cast<double>(mpImpl
->mnWidth
);
435 pCoord
[4] = pCoord
[6] = maRect
.Right() / static_cast<double>(mpImpl
->mnWidth
);
436 pCoord
[3] = pCoord
[5] = 1.0f
- maRect
.Top() / static_cast<double>(mpImpl
->mnHeight
);
437 pCoord
[1] = pCoord
[7] = 1.0f
- maRect
.Bottom() / static_cast<double>(mpImpl
->mnHeight
);
441 pCoord
[0] = pCoord
[2] = 0;
442 pCoord
[4] = pCoord
[6] = 1;
443 pCoord
[1] = pCoord
[7] = 0;
444 pCoord
[3] = pCoord
[5] = 1;
448 GLenum
OpenGLTexture::GetFilter() const
451 return mpImpl
->mnFilter
;
455 bool OpenGLTexture::CopyData(int nWidth
, int nHeight
, int nFormat
, int nType
, sal_uInt8
const * pData
)
457 if (!pData
|| !IsValid())
460 int nX
= maRect
.Left();
461 int nY
= maRect
.Top();
463 return mpImpl
->InsertBuffer(nX
, nY
, nWidth
, nHeight
, nFormat
, nType
, pData
);
466 void OpenGLTexture::SetFilter( GLenum nFilter
)
470 mpImpl
->mnFilter
= nFilter
;
471 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, nFilter
);
473 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, nFilter
);
478 void OpenGLTexture::Bind()
482 OpenGLContext::getVCLContext()->state().texture().bind(mpImpl
->mnTexture
);
485 VCL_GL_INFO( "OpenGLTexture::Binding invalid texture" );
490 void OpenGLTexture::Unbind()
494 OpenGLContext::getVCLContext()->state().texture().unbind(mpImpl
->mnTexture
);
498 void OpenGLTexture::SaveToFile(const OUString
& rFileName
)
500 std::vector
<sal_uInt8
> aBuffer(GetWidth() * GetHeight() * 4);
501 Read(GL_BGRA
, GL_UNSIGNED_BYTE
, aBuffer
.data());
502 BitmapEx aBitmap
= OpenGLHelper::ConvertBGRABufferToBitmapEx(aBuffer
.data(), GetWidth(), GetHeight());
505 vcl::PNGWriter
aWriter(aBitmap
);
506 SvFileStream
sOutput(rFileName
, StreamMode::WRITE
);
507 aWriter
.Write(sOutput
);
512 SAL_WARN("vcl.opengl", "Error writing png to " << rFileName
);
516 void OpenGLTexture::Read( GLenum nFormat
, GLenum nType
, sal_uInt8
* pData
)
520 SAL_WARN( "vcl.opengl", "Can't read invalid texture" );
524 OpenGLVCLContextZone aContextZone
;
526 VCL_GL_INFO( "Reading texture " << Id() << " " << GetWidth() << "x" << GetHeight() );
528 if( GetWidth() == mpImpl
->mnWidth
&& GetHeight() == mpImpl
->mnHeight
)
531 glPixelStorei( GL_PACK_ALIGNMENT
, 1 );
533 // XXX: Call not available with GLES 2.0
534 glGetTexImage( GL_TEXTURE_2D
, 0, nFormat
, nType
, pData
);
540 long nWidth
= maRect
.GetWidth();
541 long nHeight
= maRect
.GetHeight();
542 long nX
= maRect
.Left();
543 long nY
= mpImpl
->mnHeight
- maRect
.Top() - nHeight
;
545 // Retrieve current context
546 ImplSVData
* pSVData
= ImplGetSVData();
547 rtl::Reference
<OpenGLContext
> pContext
= pSVData
->maGDIData
.mpLastContext
;
548 OpenGLFramebuffer
* pFramebuffer
= pContext
->AcquireFramebuffer(*this);
549 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
551 glReadPixels(nX
, nY
, nWidth
, nHeight
, nFormat
, nType
, pData
);
553 OpenGLContext::ReleaseFramebuffer(pFramebuffer
);
557 OpenGLTexture::operator bool() const
562 OpenGLTexture
& OpenGLTexture::operator=(const OpenGLTexture
& rTexture
)
565 rTexture
.mpImpl
->IncreaseRefCount(rTexture
.mnSlotNumber
);
568 mpImpl
->DecreaseRefCount(mnSlotNumber
);
570 maRect
= rTexture
.maRect
;
571 mpImpl
= rTexture
.mpImpl
;
572 mnSlotNumber
= rTexture
.mnSlotNumber
;
577 OpenGLTexture
& OpenGLTexture::operator=(OpenGLTexture
&& rTexture
)
580 mpImpl
->DecreaseRefCount(mnSlotNumber
);
582 maRect
= rTexture
.maRect
;
583 mpImpl
= std::move(rTexture
.mpImpl
);
584 mnSlotNumber
= rTexture
.mnSlotNumber
;
589 bool OpenGLTexture::operator==( const OpenGLTexture
& rTexture
) const
591 return (mpImpl
== rTexture
.mpImpl
592 && maRect
== rTexture
.maRect
593 && mnSlotNumber
== rTexture
.mnSlotNumber
);
596 bool OpenGLTexture::operator!=( const OpenGLTexture
& rTexture
) const
598 return !( *this == rTexture
);
601 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */