Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 : #include <vcl/opengl/OpenGLContext.hxx>
22 : #include <vcl/opengl/OpenGLHelper.hxx>
23 :
24 : #include "svdata.hxx"
25 :
26 : #include "vcl/salbtype.hxx"
27 :
28 : #include "opengl/framebuffer.hxx"
29 : #include "opengl/texture.hxx"
30 :
31 : // texture with allocated size
32 0 : ImplOpenGLTexture::ImplOpenGLTexture( int nWidth, int nHeight, bool bAllocate ) :
33 : mnRefCount( 1 ),
34 : mnWidth( nWidth ),
35 : mnHeight( nHeight ),
36 0 : mnFilter( GL_NEAREST )
37 : {
38 0 : glGenTextures( 1, &mnTexture );
39 0 : glBindTexture( GL_TEXTURE_2D, mnTexture );
40 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
41 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
42 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
43 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
44 0 : if( bAllocate )
45 0 : glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, nWidth, nHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
46 0 : glBindTexture( GL_TEXTURE_2D, 0 );
47 :
48 : SAL_INFO( "vcl.opengl", "OpenGLTexture " << mnTexture << " " << nWidth << "x" << nHeight << " allocate" );
49 :
50 0 : CHECK_GL_ERROR();
51 0 : }
52 :
53 : // texture with content retrieved from FBO
54 0 : ImplOpenGLTexture::ImplOpenGLTexture( int nX, int nY, int nWidth, int nHeight ) :
55 : mnRefCount( 1 ),
56 : mnTexture( 0 ),
57 : mnWidth( nWidth ),
58 : mnHeight( nHeight ),
59 0 : mnFilter( GL_NEAREST )
60 : {
61 : // FIXME We need the window height here
62 : // nY = GetHeight() - nHeight - nY;
63 :
64 0 : glGenTextures( 1, &mnTexture );
65 0 : glBindTexture( GL_TEXTURE_2D, mnTexture );
66 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
67 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
68 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
69 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
70 0 : glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, nX, nY, nWidth, nHeight, 0 );
71 0 : CHECK_GL_ERROR();
72 0 : glBindTexture( GL_TEXTURE_2D, 0 );
73 :
74 : SAL_INFO( "vcl.opengl", "OpenGLTexture " << mnTexture << " " << nWidth << "x" << nHeight << " from x" << nX << ", y" << nY );
75 :
76 0 : CHECK_GL_ERROR();
77 0 : }
78 :
79 : // texture from buffer data
80 0 : ImplOpenGLTexture::ImplOpenGLTexture( int nWidth, int nHeight, int nFormat, int nType, sal_uInt8* pData ) :
81 : mnRefCount( 1 ),
82 : mnTexture( 0 ),
83 : mnWidth( nWidth ),
84 : mnHeight( nHeight ),
85 0 : mnFilter( GL_NEAREST )
86 : {
87 0 : if( !mnTexture )
88 0 : glGenTextures( 1, &mnTexture );
89 0 : glBindTexture( GL_TEXTURE_2D, mnTexture );
90 0 : glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
91 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
92 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
93 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
94 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
95 0 : glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mnWidth, mnHeight, 0, nFormat, nType, pData );
96 0 : glBindTexture( GL_TEXTURE_2D, 0 );
97 :
98 : SAL_INFO( "vcl.opengl", "OpenGLTexture " << mnTexture << " " << nWidth << "x" << nHeight << " from data" );
99 :
100 0 : CHECK_GL_ERROR();
101 0 : }
102 :
103 0 : ImplOpenGLTexture::~ImplOpenGLTexture()
104 : {
105 : SAL_INFO( "vcl.opengl", "~OpenGLTexture " << mnTexture );
106 0 : if( mnTexture != 0 )
107 0 : glDeleteTextures( 1, &mnTexture );
108 0 : }
109 :
110 0 : OpenGLTexture::OpenGLTexture() :
111 : maRect( 0, 0, 0, 0 ),
112 0 : mpImpl( NULL )
113 : {
114 0 : }
115 :
116 0 : OpenGLTexture::OpenGLTexture( int nWidth, int nHeight, bool bAllocate ) :
117 0 : maRect( Point( 0, 0 ), Size( nWidth, nHeight ) )
118 : {
119 0 : mpImpl = new ImplOpenGLTexture( nWidth, nHeight, bAllocate );
120 0 : }
121 :
122 0 : OpenGLTexture::OpenGLTexture( int nX, int nY, int nWidth, int nHeight ) :
123 0 : maRect( Point( 0, 0 ), Size( nWidth, nHeight ) )
124 : {
125 0 : mpImpl = new ImplOpenGLTexture( nX, nY, nWidth, nHeight );
126 0 : }
127 :
128 0 : OpenGLTexture::OpenGLTexture( int nWidth, int nHeight, int nFormat, int nType, sal_uInt8* pData ) :
129 0 : maRect( Point( 0, 0 ), Size( nWidth, nHeight ) )
130 : {
131 0 : mpImpl = new ImplOpenGLTexture( nWidth, nHeight, nFormat, nType, pData );
132 0 : }
133 :
134 0 : OpenGLTexture::OpenGLTexture( const OpenGLTexture& rTexture )
135 : {
136 0 : maRect = rTexture.maRect;
137 0 : mpImpl = rTexture.mpImpl;
138 0 : if( mpImpl )
139 0 : mpImpl->mnRefCount++;
140 0 : }
141 :
142 0 : OpenGLTexture::OpenGLTexture( const OpenGLTexture& rTexture,
143 0 : int nX, int nY, int nWidth, int nHeight )
144 : {
145 0 : maRect = Rectangle( Point( rTexture.maRect.Left() + nX, rTexture.maRect.Top() + nY ),
146 0 : Size( nWidth, nHeight ) );
147 0 : mpImpl = rTexture.mpImpl;
148 0 : if( mpImpl )
149 0 : mpImpl->mnRefCount++;
150 : SAL_INFO( "vcl.opengl", "Copying texture " << Id() << " [" << maRect.Left() << "," << maRect.Top() << "] " << GetWidth() << "x" << GetHeight() );
151 0 : }
152 :
153 0 : OpenGLTexture::~OpenGLTexture()
154 : {
155 0 : if( mpImpl )
156 : {
157 0 : if( mpImpl->mnRefCount == 1 )
158 0 : delete mpImpl;
159 : else
160 0 : mpImpl->mnRefCount--;
161 : }
162 0 : }
163 :
164 0 : bool OpenGLTexture::IsUnique() const
165 : {
166 0 : return ( mpImpl == NULL || mpImpl->mnRefCount == 1 );
167 : }
168 :
169 0 : GLuint OpenGLTexture::Id() const
170 : {
171 0 : if( mpImpl )
172 0 : return mpImpl->mnTexture;
173 0 : return 0;
174 : }
175 :
176 0 : int OpenGLTexture::GetWidth() const
177 : {
178 0 : return maRect.GetWidth();
179 : }
180 :
181 0 : int OpenGLTexture::GetHeight() const
182 : {
183 0 : return maRect.GetHeight();
184 : }
185 :
186 0 : void OpenGLTexture::GetCoord( GLfloat* pCoord, const SalTwoRect& rPosAry, bool bInverted ) const
187 : {
188 : SAL_INFO( "vcl.opengl", "Getting coord " << Id() << " [" << maRect.Left() << "," << maRect.Top() << "] " << GetWidth() << "x" << GetHeight() );
189 :
190 0 : if( mpImpl == NULL )
191 : {
192 0 : pCoord[0] = pCoord[1] = pCoord[2] = pCoord[3] = 0.0f;
193 0 : pCoord[4] = pCoord[5] = pCoord[6] = pCoord[7] = 0.0f;
194 0 : return;
195 : }
196 :
197 0 : pCoord[0] = pCoord[2] = (maRect.Left() + rPosAry.mnSrcX) / (double) mpImpl->mnWidth;
198 0 : pCoord[4] = pCoord[6] = (maRect.Left() + rPosAry.mnSrcX + rPosAry.mnSrcWidth) / (double) mpImpl->mnWidth;
199 :
200 0 : if( !bInverted )
201 : {
202 0 : pCoord[3] = pCoord[5] = 1.0f - (maRect.Top() + rPosAry.mnSrcY) / (double) mpImpl->mnHeight;
203 0 : pCoord[1] = pCoord[7] = 1.0f - (maRect.Top() + rPosAry.mnSrcY + rPosAry.mnSrcHeight) / (double) mpImpl->mnHeight;
204 : }
205 : else
206 : {
207 0 : pCoord[1] = pCoord[7] = 1.0f - (maRect.Top() + rPosAry.mnSrcY) / (double) mpImpl->mnHeight;
208 0 : pCoord[3] = pCoord[5] = 1.0f - (maRect.Top() + rPosAry.mnSrcY + rPosAry.mnSrcHeight) / (double) mpImpl->mnHeight;
209 : }
210 : }
211 :
212 0 : void OpenGLTexture::GetWholeCoord( GLfloat* pCoord ) const
213 : {
214 0 : if( GetWidth() != mpImpl->mnWidth || GetHeight() != mpImpl->mnHeight )
215 : {
216 0 : pCoord[0] = pCoord[2] = maRect.Left() / (double) mpImpl->mnWidth;
217 0 : pCoord[4] = pCoord[6] = maRect.Right() / (double) mpImpl->mnWidth;
218 0 : pCoord[3] = pCoord[5] = 1.0f - maRect.Top() / (double) mpImpl->mnHeight;
219 0 : pCoord[1] = pCoord[7] = 1.0f - maRect.Bottom() / (double) mpImpl->mnHeight;
220 : }
221 : else
222 : {
223 0 : pCoord[0] = pCoord[2] = 0;
224 0 : pCoord[4] = pCoord[6] = 1;
225 0 : pCoord[1] = pCoord[7] = 0;
226 0 : pCoord[3] = pCoord[5] = 1;
227 : }
228 0 : }
229 :
230 0 : GLenum OpenGLTexture::GetFilter() const
231 : {
232 0 : if( mpImpl )
233 0 : return mpImpl->mnFilter;
234 0 : return GL_NEAREST;
235 : }
236 :
237 0 : void OpenGLTexture::SetFilter( GLenum nFilter )
238 : {
239 0 : if( mpImpl )
240 : {
241 0 : mpImpl->mnFilter = nFilter;
242 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nFilter );
243 0 : glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nFilter );
244 : }
245 :
246 0 : CHECK_GL_ERROR();
247 0 : }
248 :
249 0 : void OpenGLTexture::Bind()
250 : {
251 0 : if( mpImpl )
252 0 : glBindTexture( GL_TEXTURE_2D, mpImpl->mnTexture );
253 :
254 0 : CHECK_GL_ERROR();
255 0 : }
256 :
257 0 : void OpenGLTexture::Unbind()
258 : {
259 0 : if( mpImpl )
260 0 : glBindTexture( GL_TEXTURE_2D, 0 );
261 :
262 0 : CHECK_GL_ERROR();
263 0 : }
264 :
265 0 : bool OpenGLTexture::Draw()
266 : {
267 0 : GLfloat aPosition[8] = { -1, -1, -1, 1, 1, 1, 1, -1 };
268 : GLfloat aTexCoord[8];
269 :
270 0 : if( mpImpl == NULL )
271 : {
272 : SAL_WARN( "vcl.opengl", "Can't draw invalid texture" );
273 0 : return false;
274 : }
275 :
276 : SAL_INFO( "vcl.opengl", "Drawing texture " << Id() << " [" << maRect.Left() << "," << maRect.Top() << "] " << GetWidth() << "x" << GetHeight() );
277 :
278 0 : GetWholeCoord( aTexCoord );
279 0 : glActiveTexture( GL_TEXTURE0 );
280 0 : glBindTexture( GL_TEXTURE_2D, mpImpl->mnTexture );
281 0 : glEnableVertexAttribArray( 0 );
282 0 : glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, aPosition );
283 0 : glEnableVertexAttribArray( 1 );
284 0 : glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, aTexCoord );
285 0 : glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );
286 0 : glDisableVertexAttribArray( 0 );
287 0 : glDisableVertexAttribArray( 1 );
288 0 : glBindTexture( GL_TEXTURE_2D, 0 );
289 :
290 0 : CHECK_GL_ERROR();
291 0 : return true;
292 : }
293 :
294 0 : void OpenGLTexture::Read( GLenum nFormat, GLenum nType, sal_uInt8* pData )
295 : {
296 0 : if( mpImpl == NULL )
297 : {
298 : SAL_WARN( "vcl.opengl", "Can't read invalid texture" );
299 0 : return;
300 : }
301 :
302 0 : Bind();
303 0 : glPixelStorei( GL_PACK_ALIGNMENT, 1 );
304 :
305 : SAL_INFO( "vcl.opengl", "Reading texture " << Id() << " " << GetWidth() << "x" << GetHeight() );
306 :
307 0 : if( GetWidth() == mpImpl->mnWidth && GetHeight() == mpImpl->mnHeight )
308 : {
309 : // XXX: Call not available with GLES 2.0
310 0 : glGetTexImage( GL_TEXTURE_2D, 0, nFormat, nType, pData );
311 : }
312 : else
313 : {
314 : // Retrieve current context
315 0 : ImplSVData* pSVData = ImplGetSVData();
316 0 : OpenGLContext* pContext = pSVData->maGDIData.mpLastContext;
317 : OpenGLFramebuffer* pFramebuffer;
318 :
319 0 : pFramebuffer = pContext->AcquireFramebuffer( *this );
320 0 : glReadPixels( maRect.Left(), mpImpl->mnHeight - maRect.Top(), GetWidth(), GetHeight(), nFormat, nType, pData );
321 0 : OpenGLContext::ReleaseFramebuffer( pFramebuffer );
322 0 : CHECK_GL_ERROR();
323 : }
324 :
325 0 : Unbind();
326 0 : CHECK_GL_ERROR();
327 : }
328 :
329 0 : OpenGLTexture::operator bool() const
330 : {
331 0 : return ( mpImpl != NULL );
332 : }
333 :
334 0 : OpenGLTexture& OpenGLTexture::operator=( const OpenGLTexture& rTexture )
335 : {
336 0 : if( rTexture.mpImpl )
337 0 : rTexture.mpImpl->mnRefCount++;
338 0 : if( mpImpl )
339 : {
340 0 : if( mpImpl->mnRefCount == 1 )
341 0 : delete mpImpl;
342 : else
343 0 : mpImpl->mnRefCount--;
344 : }
345 :
346 0 : maRect = rTexture.maRect;
347 0 : mpImpl = rTexture.mpImpl;
348 :
349 0 : return *this;
350 : }
351 :
352 0 : bool OpenGLTexture::operator==( const OpenGLTexture& rTexture ) const
353 : {
354 0 : return (mpImpl == rTexture.mpImpl && maRect == rTexture.maRect );
355 : }
356 :
357 0 : bool OpenGLTexture::operator!=( const OpenGLTexture& rTexture ) const
358 : {
359 0 : return !( *this == rTexture );
360 : }
361 :
362 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|