Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / opengl / texture.cxx
blobc26017e791189a8e27f4362923b79c500f02f16e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <svdata.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>
35 namespace
38 constexpr GLenum constInternalFormat = GL_RGBA8;
40 } // end anonymous namespace
42 // texture with allocated size
43 ImplOpenGLTexture::ImplOpenGLTexture( int nWidth, int nHeight, bool bAllocate ) :
44 mnTexture( 0 ),
45 mnWidth( nWidth ),
46 mnHeight( nHeight ),
47 mnFilter( GL_NEAREST ),
48 mnOptStencil( 0 )
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 );
58 CHECK_GL_ERROR();
59 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
60 CHECK_GL_ERROR();
61 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
62 CHECK_GL_ERROR();
63 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
64 CHECK_GL_ERROR();
65 if( bAllocate )
67 glTexImage2D( GL_TEXTURE_2D, 0, constInternalFormat, nWidth, nHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr );
68 CHECK_GL_ERROR();
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 ) :
76 mnTexture( 0 ),
77 mnWidth( nWidth ),
78 mnHeight( nHeight ),
79 mnFilter( GL_NEAREST ),
80 mnOptStencil( 0 )
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 );
93 CHECK_GL_ERROR();
94 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
95 CHECK_GL_ERROR();
96 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
97 CHECK_GL_ERROR();
98 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
99 CHECK_GL_ERROR();
100 glCopyTexImage2D( GL_TEXTURE_2D, 0, constInternalFormat, nX, nY, nWidth, nHeight, 0 );
101 CHECK_GL_ERROR();
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 ) :
108 mnTexture( 0 ),
109 mnWidth( nWidth ),
110 mnHeight( nHeight ),
111 mnFilter( GL_NEAREST ),
112 mnOptStencil( 0 )
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 );
122 CHECK_GL_ERROR();
123 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
124 CHECK_GL_ERROR();
125 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
126 CHECK_GL_ERROR();
127 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
128 CHECK_GL_ERROR();
129 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
130 CHECK_GL_ERROR();
131 glTexImage2D( GL_TEXTURE_2D, 0, constInternalFormat, mnWidth, mnHeight, 0, nFormat, nType, pData );
132 CHECK_GL_ERROR();
134 VCL_GL_INFO( "OpenGLTexture " << mnTexture << " " << nWidth << "x" << nHeight << " from data" );
137 GLuint ImplOpenGLTexture::AddStencil()
139 assert( mnOptStencil == 0 );
141 glGenRenderbuffers( 1, &mnOptStencil );
142 CHECK_GL_ERROR();
143 glBindRenderbuffer( GL_RENDERBUFFER, mnOptStencil );
144 CHECK_GL_ERROR();
145 VCL_GL_INFO( "Allocate stencil " << mnWidth << " x " << mnHeight );
146 glRenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX,
147 mnWidth, mnHeight );
148 CHECK_GL_ERROR();
149 glBindRenderbuffer(GL_RENDERBUFFER, 0);
150 CHECK_GL_ERROR();
152 return mnOptStencil;
155 ImplOpenGLTexture::~ImplOpenGLTexture()
157 VCL_GL_INFO( "~OpenGLTexture " << mnTexture );
158 if( mnTexture != 0 )
160 // During shutdown GL is already de-initialized, so we should not try to create a new context.
161 OpenGLZone aZone;
162 rtl::Reference<OpenGLContext> xContext = OpenGLContext::getVCLContext(false);
163 if( xContext.is() )
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;
171 if( pContext.is() )
173 pContext->makeCurrent();
174 pContext->UnbindTextureFromFramebuffers( mnTexture );
177 if( mnOptStencil != 0 )
179 glDeleteRenderbuffers( 1, &mnOptStencil );
180 mnOptStencil = 0;
182 auto& rState = pContext->state();
183 rState.texture().unbindAndDelete(mnTexture);
184 mnTexture = 0;
186 else
188 mnOptStencil = 0;
189 mnTexture = 0;
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)
197 return false;
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);
204 CHECK_GL_ERROR();
205 glTexSubImage2D(GL_TEXTURE_2D, 0, nX, mnHeight - nY - nHeight, nWidth, nHeight, nFormat, nType, pData);
206 CHECK_GL_ERROR();
208 VCL_GL_INFO( "OpenGLTexture " << mnTexture << " Insert buff. to " << nX << " " << nY
209 << " size " << nWidth << "x" << nHeight << " from data" );
211 return true;
214 void ImplOpenGLTexture::InitializeSlotMechanism(int nInitialSlotSize)
216 if (mpSlotReferences)
217 return;
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 ),
251 mpImpl(),
252 mnSlotNumber(-1)
256 OpenGLTexture::OpenGLTexture(const std::shared_ptr<ImplOpenGLTexture>& rpImpl, tools::Rectangle aRectangle, int nSlotNumber)
257 : maRect(aRectangle)
258 , mpImpl(rpImpl)
259 , mnSlotNumber(nSlotNumber)
261 if (mpImpl)
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))
268 , mnSlotNumber(-1)
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))
275 , mnSlotNumber(-1)
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))
282 , mnSlotNumber(-1)
287 OpenGLTexture::OpenGLTexture(const OpenGLTexture& rTexture)
288 : maRect(rTexture.maRect)
289 , mpImpl(rTexture.mpImpl)
290 , mnSlotNumber(rTexture.mnSlotNumber)
292 if (mpImpl)
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;
310 if (mpImpl)
311 mpImpl->IncreaseRefCount(mnSlotNumber);
312 VCL_GL_INFO( "Copying texture " << Id() << " [" << maRect.Left() << "," << maRect.Top() << "] " << GetWidth() << "x" << GetHeight() );
315 OpenGLTexture::~OpenGLTexture()
317 if (mpImpl)
318 mpImpl->DecreaseRefCount(mnSlotNumber);
321 bool OpenGLTexture::IsUnique() const
323 return !mpImpl || (mpImpl.use_count() == 1);
326 GLuint OpenGLTexture::Id() const
328 if (mpImpl)
329 return mpImpl->mnTexture;
330 return 0;
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()
350 if (mpImpl)
351 return mpImpl->AddStencil();
352 else
353 return 0;
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() );
360 if (!IsValid())
362 pCoord[0] = pCoord[1] = pCoord[2] = pCoord[3] = 0.0f;
363 pCoord[4] = pCoord[5] = pCoord[6] = pCoord[7] = 0.0f;
364 return;
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);
370 if( !bInverted )
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);
375 else
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
384 if (IsValid())
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;
392 if (bInverted)
394 y2 = 1.0f - (maRect.Top() + rPosAry.mnSrcY) / fTextureHeight;
395 y1 = 1.0f - (maRect.Top() + rPosAry.mnSrcY + rPosAry.mnSrcHeight) / fTextureHeight;
397 else
399 y1 = 1.0f - (maRect.Top() + rPosAry.mnSrcY) / fTextureHeight;
400 y2 = 1.0f - (maRect.Top() + rPosAry.mnSrcY + rPosAry.mnSrcHeight) / fTextureHeight;
405 template <>
406 void OpenGLTexture::FillCoords<GL_TRIANGLE_FAN>(std::vector<GLfloat>& rCoords, const SalTwoRect& rPosAry) const
408 GLfloat x1 = 0.0f;
409 GLfloat x2 = 0.0f;
410 GLfloat y1 = 0.0f;
411 GLfloat y2 = 0.0f;
413 GetTextureRect(rPosAry, false/*bInverted*/, x1, x2, y1, y2);
415 rCoords.insert(rCoords.end(), {
416 x1, y2, x1, y1,
417 x2, y1, x2, y2
421 template <>
422 void OpenGLTexture::FillCoords<GL_TRIANGLES>(std::vector<GLfloat>& rCoords, const SalTwoRect& rPosAry) const
424 GLfloat x1 = 0.0f;
425 GLfloat x2 = 0.0f;
426 GLfloat y1 = 0.0f;
427 GLfloat y2 = 0.0f;
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);
446 else
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
457 if( mpImpl )
458 return mpImpl->mnFilter;
459 return GL_NEAREST;
462 bool OpenGLTexture::CopyData(int nWidth, int nHeight, int nFormat, int nType, sal_uInt8 const * pData)
464 if (!pData || !IsValid())
465 return false;
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 )
475 if( mpImpl )
477 mpImpl->mnFilter = nFilter;
478 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nFilter );
479 CHECK_GL_ERROR();
480 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nFilter );
481 CHECK_GL_ERROR();
485 void OpenGLTexture::Bind()
487 if (IsValid())
489 OpenGLContext::getVCLContext()->state().texture().bind(mpImpl->mnTexture);
491 else
492 VCL_GL_INFO( "OpenGLTexture::Binding invalid texture" );
494 CHECK_GL_ERROR();
497 void OpenGLTexture::Unbind()
499 if (IsValid())
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);
515 sOutput.Close();
517 catch (...)
519 SAL_WARN("vcl.opengl", "Error writing png to " << rFileName);
523 void OpenGLTexture::Read( GLenum nFormat, GLenum nType, sal_uInt8* pData )
525 if (!IsValid())
527 SAL_WARN( "vcl.opengl", "Can't read invalid texture" );
528 return;
531 OpenGLVCLContextZone aContextZone;
533 VCL_GL_INFO( "Reading texture " << Id() << " " << GetWidth() << "x" << GetHeight() );
535 if( GetWidth() == mpImpl->mnWidth && GetHeight() == mpImpl->mnHeight )
537 Bind();
538 glPixelStorei( GL_PACK_ALIGNMENT, 1 );
539 CHECK_GL_ERROR();
540 // XXX: Call not available with GLES 2.0
541 glGetTexImage( GL_TEXTURE_2D, 0, nFormat, nType, pData );
542 CHECK_GL_ERROR();
543 Unbind();
545 else
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);
557 CHECK_GL_ERROR();
558 glReadPixels(nX, nY, nWidth, nHeight, nFormat, nType, pData);
559 CHECK_GL_ERROR();
560 OpenGLContext::ReleaseFramebuffer(pFramebuffer);
564 OpenGLTexture::operator bool() const
566 return IsValid();
569 OpenGLTexture& OpenGLTexture::operator=(const OpenGLTexture& rTexture)
571 if (rTexture.mpImpl)
572 rTexture.mpImpl->IncreaseRefCount(rTexture.mnSlotNumber);
574 if (mpImpl)
575 mpImpl->DecreaseRefCount(mnSlotNumber);
577 maRect = rTexture.maRect;
578 mpImpl = rTexture.mpImpl;
579 mnSlotNumber = rTexture.mnSlotNumber;
581 return *this;
584 OpenGLTexture& OpenGLTexture::operator=(OpenGLTexture&& rTexture)
586 if (mpImpl)
587 mpImpl->DecreaseRefCount(mnSlotNumber);
589 maRect = rTexture.maRect;
590 mpImpl = std::move(rTexture.mpImpl);
591 mnSlotNumber = rTexture.mnSlotNumber;
593 return *this;
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: */