Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / opengl / win / gdiimpl.cxx
blobcc34d670209f7c4f28af357c5889c1439a8c2b1e
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/.
8 */
10 #include <o3tl/lru_map.hxx>
13 #include "opengl/win/gdiimpl.hxx"
15 #include <win/wincomp.hxx>
16 #include <win/saldata.hxx>
17 #include <win/salframe.h>
19 WinOpenGLSalGraphicsImpl::WinOpenGLSalGraphicsImpl(WinSalGraphics& rGraphics,
20 SalGeometryProvider *mpProvider):
21 OpenGLSalGraphicsImpl(rGraphics,mpProvider),
22 mrParent(rGraphics)
26 void WinOpenGLSalGraphicsImpl::copyBits( const SalTwoRect& rPosAry, SalGraphics* pSrcGraphics )
28 OpenGLSalGraphicsImpl *pImpl = pSrcGraphics ? static_cast< OpenGLSalGraphicsImpl* >(pSrcGraphics->GetImpl()) : static_cast< OpenGLSalGraphicsImpl *>(mrParent.GetImpl());
29 OpenGLSalGraphicsImpl::DoCopyBits( rPosAry, *pImpl );
32 rtl::Reference<OpenGLContext> WinOpenGLSalGraphicsImpl::CreateWinContext()
34 rtl::Reference<OpenGLContext> pContext = OpenGLContext::Create();
35 pContext->requestSingleBufferedRendering();
36 pContext->init( mrParent.mhLocalDC, mrParent.mhWnd );
37 return pContext;
40 bool WinOpenGLSalGraphicsImpl::UseContext( const rtl::Reference<OpenGLContext> &pContext )
42 if( !pContext.is() || !pContext->isInitialized() || IsForeignContext( pContext ) )
43 return false;
44 if( IsOffscreen() )
45 return true;
46 return pContext->getOpenGLWindow().hWnd == mrParent.mhWnd &&
47 pContext->getOpenGLWindow().hDC == mrParent.mhLocalDC;
50 void WinOpenGLSalGraphicsImpl::Init()
52 if ( !IsOffscreen() && mpContext.is() && mpContext->isInitialized() &&
53 ( mpContext->getOpenGLWindow().hWnd != mrParent.mhWnd ||
54 mpContext->getOpenGLWindow().hDC == mrParent.mhLocalDC ) )
56 // This can legitimiately happen, SalFrame keeps 2x
57 // SalGraphics which share the same hWnd and hDC.
58 // The shape 'Area' dialog does reparenting to trigger this.
59 SAL_WARN("vcl.opengl", "Unusual: Windows handle / DC changed without DeInit");
60 DeInit();
63 OpenGLSalGraphicsImpl::Init();
66 namespace
69 typedef std::pair<ControlCacheKey, std::unique_ptr<TextureCombo>> ControlCachePair;
70 typedef o3tl::lru_map<ControlCacheKey, std::unique_ptr<TextureCombo>, ControlCacheHashFunction> ControlCacheType;
72 ControlCacheType gTextureCache(200);
76 bool WinOpenGLSalGraphicsImpl::TryRenderCachedNativeControl(ControlCacheKey& rControlCacheKey, int nX, int nY)
78 static bool gbCacheEnabled = !getenv("SAL_WITHOUT_WIDGET_CACHE");
80 if (!gbCacheEnabled)
81 return false;
83 ControlCacheType::const_iterator iterator = gTextureCache.find(rControlCacheKey);
85 if (iterator == gTextureCache.end())
86 return false;
88 const std::unique_ptr<TextureCombo>& pCombo = iterator->second;
90 PreDraw();
92 OpenGLTexture& rTexture = *pCombo->mpTexture;
94 SalTwoRect aPosAry(0, 0, rTexture.GetWidth(), rTexture.GetHeight(),
95 nX, nY, rTexture.GetWidth(), rTexture.GetHeight());
97 if (pCombo->mpMask)
98 DrawTextureDiff(rTexture, *pCombo->mpMask, aPosAry);
99 else
100 DrawTexture(rTexture, aPosAry);
102 PostDraw();
104 return true;
107 bool WinOpenGLSalGraphicsImpl::RenderCompatibleDC(OpenGLCompatibleDC& rWhite, OpenGLCompatibleDC& rBlack,
108 int nX, int nY, TextureCombo& rCombo)
110 PreDraw();
112 rCombo.mpTexture.reset(rWhite.getTexture());
113 rCombo.mpMask.reset(rBlack.getTexture());
116 if (rCombo.mpTexture && rCombo.mpMask)
118 OpenGLTexture& rTexture = *rCombo.mpTexture;
120 SalTwoRect aPosAry(0, 0, rTexture.GetWidth(), rTexture.GetHeight(),
121 nX, nY, rTexture.GetWidth(), rTexture.GetHeight());
123 DrawTextureDiff(*rCombo.mpTexture, *rCombo.mpMask, aPosAry);
126 PostDraw();
127 return true;
130 bool WinOpenGLSalGraphicsImpl::RenderAndCacheNativeControl(OpenGLCompatibleDC& rWhite, OpenGLCompatibleDC& rBlack,
131 int nX, int nY , ControlCacheKey& aControlCacheKey)
133 std::unique_ptr<TextureCombo> pCombo(new TextureCombo);
135 bool bResult = RenderCompatibleDC(rWhite, rBlack, nX, nY, *pCombo);
136 if (!bResult)
137 return false;
139 if (!aControlCacheKey.canCacheControl())
140 return true;
142 ControlCachePair pair(aControlCacheKey, std::move(pCombo));
143 gTextureCache.insert(std::move(pair));
145 return bResult;
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */