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 <tools/stream.hxx>
22 #include <vcl/opengl/OpenGLContext.hxx>
23 #include <vcl/opengl/OpenGLHelper.hxx>
27 #include <vcl/salbtype.hxx>
28 #include <vcl/pngwrite.hxx>
30 #include <opengl/framebuffer.hxx>
31 #include <opengl/texture.hxx>
32 #include <opengl/zone.hxx>
33 #include <opengl/RenderState.hxx>
38 constexpr GLenum constInternalFormat
= GL_RGBA8
;
40 } // end anonymous namespace
42 // texture with allocated size
43 ImplOpenGLTexture::ImplOpenGLTexture( int nWidth
, int nHeight
, bool bAllocate
) :
47 mnFilter( GL_NEAREST
),
50 OpenGLVCLContextZone aContextZone
;
52 auto& rState
= OpenGLContext::getVCLContext()->state();
53 TextureState::generate(mnTexture
);
54 rState
.texture().active(0);
55 rState
.texture().bind(mnTexture
);
57 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
59 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
61 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
63 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
67 glTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, nWidth
, nHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nullptr );
71 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " allocate" );
74 // texture with content retrieved from FBO
75 ImplOpenGLTexture::ImplOpenGLTexture( int nX
, int nY
, int nWidth
, int nHeight
) :
79 mnFilter( GL_NEAREST
),
82 OpenGLVCLContextZone aContextZone
;
84 // FIXME We need the window height here
85 // nY = GetHeight() - nHeight - nY;
87 auto& rState
= OpenGLContext::getVCLContext()->state();
88 TextureState::generate(mnTexture
);
89 rState
.texture().active(0);
90 rState
.texture().bind(mnTexture
);
92 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
94 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
96 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
98 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
100 glCopyTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, nX
, nY
, nWidth
, nHeight
, 0 );
103 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " from x" << nX
<< ", y" << nY
);
106 // texture from buffer data
107 ImplOpenGLTexture::ImplOpenGLTexture( int nWidth
, int nHeight
, int nFormat
, int nType
, void const * pData
) :
111 mnFilter( GL_NEAREST
),
114 OpenGLVCLContextZone aContextZone
;
116 auto& rState
= OpenGLContext::getVCLContext()->state();
117 TextureState::generate(mnTexture
);
118 rState
.texture().active(0);
119 rState
.texture().bind(mnTexture
);
121 glPixelStorei( GL_UNPACK_ALIGNMENT
, 1 );
123 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
125 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
127 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
129 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
131 glTexImage2D( GL_TEXTURE_2D
, 0, constInternalFormat
, mnWidth
, mnHeight
, 0, nFormat
, nType
, pData
);
134 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " " << nWidth
<< "x" << nHeight
<< " from data" );
137 GLuint
ImplOpenGLTexture::AddStencil()
139 assert( mnOptStencil
== 0 );
141 glGenRenderbuffers( 1, &mnOptStencil
);
143 glBindRenderbuffer( GL_RENDERBUFFER
, mnOptStencil
);
145 VCL_GL_INFO( "Allocate stencil " << mnWidth
<< " x " << mnHeight
);
146 glRenderbufferStorage( GL_RENDERBUFFER
, GL_STENCIL_INDEX
,
149 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
155 ImplOpenGLTexture::~ImplOpenGLTexture()
157 VCL_GL_INFO( "~OpenGLTexture " << mnTexture
);
160 // During shutdown GL is already de-initialized, so we should not try to create a new context.
162 rtl::Reference
<OpenGLContext
> xContext
= OpenGLContext::getVCLContext(false);
165 // FIXME: this is really not optimal performance-wise.
167 // Check we have been correctly un-bound from all framebuffers.
168 ImplSVData
* pSVData
= ImplGetSVData();
169 rtl::Reference
<OpenGLContext
> pContext
= pSVData
->maGDIData
.mpLastContext
;
173 pContext
->makeCurrent();
174 pContext
->UnbindTextureFromFramebuffers( mnTexture
);
177 if( mnOptStencil
!= 0 )
179 glDeleteRenderbuffers( 1, &mnOptStencil
);
182 auto& rState
= pContext
->state();
183 rState
.texture().unbindAndDelete(mnTexture
);
194 bool ImplOpenGLTexture::InsertBuffer(int nX
, int nY
, int nWidth
, int nHeight
, int nFormat
, int nType
, sal_uInt8
const * pData
)
196 if (!pData
|| mnTexture
== 0)
199 rtl::Reference
<OpenGLContext
> xContext
= OpenGLContext::getVCLContext();
200 xContext
->state().texture().active(0);
201 xContext
->state().texture().bind(mnTexture
);
203 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
205 glTexSubImage2D(GL_TEXTURE_2D
, 0, nX
, mnHeight
- nY
- nHeight
, nWidth
, nHeight
, nFormat
, nType
, pData
);
208 VCL_GL_INFO( "OpenGLTexture " << mnTexture
<< " Insert buff. to " << nX
<< " " << nY
209 << " size " << nWidth
<< "x" << nHeight
<< " from data" );
214 void ImplOpenGLTexture::InitializeSlotMechanism(int nInitialSlotSize
)
216 if (mpSlotReferences
)
219 mpSlotReferences
.reset(new std::vector
<int>(nInitialSlotSize
, 0));
222 void ImplOpenGLTexture::IncreaseRefCount(int nSlotNumber
)
224 if (mpSlotReferences
&& nSlotNumber
>= 0)
226 if (nSlotNumber
>= int(mpSlotReferences
->size()))
227 mpSlotReferences
->resize(nSlotNumber
+ 1, 0);
229 mpSlotReferences
->at(nSlotNumber
)++;
233 void ImplOpenGLTexture::DecreaseRefCount(int nSlotNumber
)
235 if (mpSlotReferences
&& nSlotNumber
>= 0)
237 if (nSlotNumber
>= int(mpSlotReferences
->size()))
238 mpSlotReferences
->resize(nSlotNumber
, 0);
240 mpSlotReferences
->at(nSlotNumber
)--;
242 if (mpSlotReferences
->at(nSlotNumber
) == 0 && mFunctSlotDeallocateCallback
)
244 mFunctSlotDeallocateCallback(nSlotNumber
);
249 OpenGLTexture::OpenGLTexture() :
250 maRect( 0, 0, 0, 0 ),
256 OpenGLTexture::OpenGLTexture(const std::shared_ptr
<ImplOpenGLTexture
>& rpImpl
, tools::Rectangle aRectangle
, int nSlotNumber
)
259 , mnSlotNumber(nSlotNumber
)
262 mpImpl
->IncreaseRefCount(nSlotNumber
);
265 OpenGLTexture::OpenGLTexture( int nWidth
, int nHeight
, bool bAllocate
)
266 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
267 , mpImpl(new ImplOpenGLTexture(nWidth
, nHeight
, bAllocate
))
272 OpenGLTexture::OpenGLTexture( int nX
, int nY
, int nWidth
, int nHeight
)
273 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
274 , mpImpl(new ImplOpenGLTexture(nX
, nY
, nWidth
, nHeight
))
279 OpenGLTexture::OpenGLTexture( int nWidth
, int nHeight
, int nFormat
, int nType
, void const * pData
)
280 : maRect( Point( 0, 0 ), Size( nWidth
, nHeight
) )
281 , mpImpl(new ImplOpenGLTexture(nWidth
, nHeight
, nFormat
, nType
, pData
))
287 OpenGLTexture::OpenGLTexture(const OpenGLTexture
& rTexture
)
288 : maRect(rTexture
.maRect
)
289 , mpImpl(rTexture
.mpImpl
)
290 , mnSlotNumber(rTexture
.mnSlotNumber
)
293 mpImpl
->IncreaseRefCount(mnSlotNumber
);
296 OpenGLTexture::OpenGLTexture(OpenGLTexture
&& rTexture
)
297 : maRect(rTexture
.maRect
)
298 , mpImpl(std::move(rTexture
.mpImpl
))
299 , mnSlotNumber(rTexture
.mnSlotNumber
)
303 OpenGLTexture::OpenGLTexture( const OpenGLTexture
& rTexture
,
304 int nX
, int nY
, int nWidth
, int nHeight
)
306 maRect
= tools::Rectangle( Point( rTexture
.maRect
.Left() + nX
, rTexture
.maRect
.Top() + nY
),
307 Size( nWidth
, nHeight
) );
308 mpImpl
= rTexture
.mpImpl
;
309 mnSlotNumber
= rTexture
.mnSlotNumber
;
311 mpImpl
->IncreaseRefCount(mnSlotNumber
);
312 VCL_GL_INFO( "Copying texture " << Id() << " [" << maRect
.Left() << "," << maRect
.Top() << "] " << GetWidth() << "x" << GetHeight() );
315 OpenGLTexture::~OpenGLTexture()
318 mpImpl
->DecreaseRefCount(mnSlotNumber
);
321 bool OpenGLTexture::IsUnique() const
323 return !mpImpl
|| (mpImpl
.use_count() == 1);
326 GLuint
OpenGLTexture::Id() const
329 return mpImpl
->mnTexture
;
333 int OpenGLTexture::GetWidth() const
335 return maRect
.GetWidth();
338 int OpenGLTexture::GetHeight() const
340 return maRect
.GetHeight();
343 GLuint
OpenGLTexture::StencilId() const
345 return mpImpl
? mpImpl
->mnOptStencil
: 0;
348 GLuint
OpenGLTexture::AddStencil()
351 return mpImpl
->AddStencil();
356 void OpenGLTexture::GetCoord( GLfloat
* pCoord
, const SalTwoRect
& rPosAry
, bool bInverted
) const
358 VCL_GL_INFO( "Getting coord " << Id() << " [" << maRect
.Left() << "," << maRect
.Top() << "] " << GetWidth() << "x" << GetHeight() );
362 pCoord
[0] = pCoord
[1] = pCoord
[2] = pCoord
[3] = 0.0f
;
363 pCoord
[4] = pCoord
[5] = pCoord
[6] = pCoord
[7] = 0.0f
;
367 pCoord
[0] = pCoord
[2] = (maRect
.Left() + rPosAry
.mnSrcX
) / static_cast<double>(mpImpl
->mnWidth
);
368 pCoord
[4] = pCoord
[6] = (maRect
.Left() + rPosAry
.mnSrcX
+ rPosAry
.mnSrcWidth
) / static_cast<double>(mpImpl
->mnWidth
);
372 pCoord
[3] = pCoord
[5] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / static_cast<double>(mpImpl
->mnHeight
);
373 pCoord
[1] = pCoord
[7] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / static_cast<double>(mpImpl
->mnHeight
);
377 pCoord
[1] = pCoord
[7] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / static_cast<double>(mpImpl
->mnHeight
);
378 pCoord
[3] = pCoord
[5] = 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / static_cast<double>(mpImpl
->mnHeight
);
382 void OpenGLTexture::GetTextureRect(const SalTwoRect
& rPosAry
, bool bInverted
, GLfloat
& x1
, GLfloat
& x2
, GLfloat
& y1
, GLfloat
& y2
) const
386 double fTextureWidth(mpImpl
->mnWidth
);
387 double fTextureHeight(mpImpl
->mnHeight
);
389 x1
= (maRect
.Left() + rPosAry
.mnSrcX
) / fTextureWidth
;
390 x2
= (maRect
.Left() + rPosAry
.mnSrcX
+ rPosAry
.mnSrcWidth
) / fTextureWidth
;
394 y2
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / fTextureHeight
;
395 y1
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / fTextureHeight
;
399 y1
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
) / fTextureHeight
;
400 y2
= 1.0f
- (maRect
.Top() + rPosAry
.mnSrcY
+ rPosAry
.mnSrcHeight
) / fTextureHeight
;
406 void OpenGLTexture::FillCoords
<GL_TRIANGLE_FAN
>(std::vector
<GLfloat
>& rCoords
, const SalTwoRect
& rPosAry
) const
413 GetTextureRect(rPosAry
, false/*bInverted*/, x1
, x2
, y1
, y2
);
415 rCoords
.insert(rCoords
.end(), {
422 void OpenGLTexture::FillCoords
<GL_TRIANGLES
>(std::vector
<GLfloat
>& rCoords
, const SalTwoRect
& rPosAry
) const
429 GetTextureRect(rPosAry
, false/*bInverted*/, x1
, x2
, y1
, y2
);
431 rCoords
.insert(rCoords
.end(), {
432 x1
, y1
, x2
, y1
, x1
, y2
,
433 x1
, y2
, x2
, y1
, x2
, y2
437 void OpenGLTexture::GetWholeCoord( GLfloat
* pCoord
) const
439 if( GetWidth() != mpImpl
->mnWidth
|| GetHeight() != mpImpl
->mnHeight
)
441 pCoord
[0] = pCoord
[2] = maRect
.Left() / static_cast<double>(mpImpl
->mnWidth
);
442 pCoord
[4] = pCoord
[6] = maRect
.Right() / static_cast<double>(mpImpl
->mnWidth
);
443 pCoord
[3] = pCoord
[5] = 1.0f
- maRect
.Top() / static_cast<double>(mpImpl
->mnHeight
);
444 pCoord
[1] = pCoord
[7] = 1.0f
- maRect
.Bottom() / static_cast<double>(mpImpl
->mnHeight
);
448 pCoord
[0] = pCoord
[2] = 0;
449 pCoord
[4] = pCoord
[6] = 1;
450 pCoord
[1] = pCoord
[7] = 0;
451 pCoord
[3] = pCoord
[5] = 1;
455 GLenum
OpenGLTexture::GetFilter() const
458 return mpImpl
->mnFilter
;
462 bool OpenGLTexture::CopyData(int nWidth
, int nHeight
, int nFormat
, int nType
, sal_uInt8
const * pData
)
464 if (!pData
|| !IsValid())
467 int nX
= maRect
.Left();
468 int nY
= maRect
.Top();
470 return mpImpl
->InsertBuffer(nX
, nY
, nWidth
, nHeight
, nFormat
, nType
, pData
);
473 void OpenGLTexture::SetFilter( GLenum nFilter
)
477 mpImpl
->mnFilter
= nFilter
;
478 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, nFilter
);
480 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, nFilter
);
485 void OpenGLTexture::Bind()
489 OpenGLContext::getVCLContext()->state().texture().bind(mpImpl
->mnTexture
);
492 VCL_GL_INFO( "OpenGLTexture::Binding invalid texture" );
497 void OpenGLTexture::Unbind()
501 OpenGLContext::getVCLContext()->state().texture().unbind(mpImpl
->mnTexture
);
505 void OpenGLTexture::SaveToFile(const OUString
& rFileName
)
507 std::vector
<sal_uInt8
> aBuffer(GetWidth() * GetHeight() * 4);
508 Read(GL_BGRA
, GL_UNSIGNED_BYTE
, aBuffer
.data());
509 BitmapEx aBitmap
= OpenGLHelper::ConvertBGRABufferToBitmapEx(aBuffer
.data(), GetWidth(), GetHeight());
512 vcl::PNGWriter
aWriter(aBitmap
);
513 SvFileStream
sOutput(rFileName
, StreamMode::WRITE
);
514 aWriter
.Write(sOutput
);
519 SAL_WARN("vcl.opengl", "Error writing png to " << rFileName
);
523 void OpenGLTexture::Read( GLenum nFormat
, GLenum nType
, sal_uInt8
* pData
)
527 SAL_WARN( "vcl.opengl", "Can't read invalid texture" );
531 OpenGLVCLContextZone aContextZone
;
533 VCL_GL_INFO( "Reading texture " << Id() << " " << GetWidth() << "x" << GetHeight() );
535 if( GetWidth() == mpImpl
->mnWidth
&& GetHeight() == mpImpl
->mnHeight
)
538 glPixelStorei( GL_PACK_ALIGNMENT
, 1 );
540 // XXX: Call not available with GLES 2.0
541 glGetTexImage( GL_TEXTURE_2D
, 0, nFormat
, nType
, pData
);
547 long nWidth
= maRect
.GetWidth();
548 long nHeight
= maRect
.GetHeight();
549 long nX
= maRect
.Left();
550 long nY
= mpImpl
->mnHeight
- maRect
.Top() - nHeight
;
552 // Retrieve current context
553 ImplSVData
* pSVData
= ImplGetSVData();
554 rtl::Reference
<OpenGLContext
> pContext
= pSVData
->maGDIData
.mpLastContext
;
555 OpenGLFramebuffer
* pFramebuffer
= pContext
->AcquireFramebuffer(*this);
556 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
558 glReadPixels(nX
, nY
, nWidth
, nHeight
, nFormat
, nType
, pData
);
560 OpenGLContext::ReleaseFramebuffer(pFramebuffer
);
564 OpenGLTexture::operator bool() const
569 OpenGLTexture
& OpenGLTexture::operator=(const OpenGLTexture
& rTexture
)
572 rTexture
.mpImpl
->IncreaseRefCount(rTexture
.mnSlotNumber
);
575 mpImpl
->DecreaseRefCount(mnSlotNumber
);
577 maRect
= rTexture
.maRect
;
578 mpImpl
= rTexture
.mpImpl
;
579 mnSlotNumber
= rTexture
.mnSlotNumber
;
584 OpenGLTexture
& OpenGLTexture::operator=(OpenGLTexture
&& rTexture
)
587 mpImpl
->DecreaseRefCount(mnSlotNumber
);
589 maRect
= rTexture
.maRect
;
590 mpImpl
= std::move(rTexture
.mpImpl
);
591 mnSlotNumber
= rTexture
.mnSlotNumber
;
596 bool OpenGLTexture::operator==( const OpenGLTexture
& rTexture
) const
598 return (mpImpl
== rTexture
.mpImpl
599 && maRect
== rTexture
.maRect
600 && mnSlotNumber
== rTexture
.mnSlotNumber
);
603 bool OpenGLTexture::operator!=( const OpenGLTexture
& rTexture
) const
605 return !( *this == rTexture
);
608 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */