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/.
12 #include <sal/config.h>
13 #include <vcl/opengl/OpenGLContext.hxx>
14 #include <vcl/opengl/OpenGLHelper.hxx>
16 #include <opengl/framebuffer.hxx>
17 #include <opengl/texture.hxx>
19 #include <opengl/FixedTextureAtlas.hxx>
23 std::shared_ptr
<ImplOpenGLTexture
> mpTexture
;
25 std::vector
<bool> maAllocatedSlots
;
27 FixedTexture(int nTextureWidth
, int nTextureHeight
, int nNumberOfSlots
)
28 : mpTexture(std::make_shared
<ImplOpenGLTexture
>(nTextureWidth
, nTextureHeight
, true))
29 , mnFreeSlots(nNumberOfSlots
)
30 , maAllocatedSlots(nNumberOfSlots
, false)
32 auto aDeallocateFunction
= [this] (int nSlotNumber
)
34 deallocateSlot(nSlotNumber
);
37 mpTexture
->SetSlotDeallocateCallback(aDeallocateFunction
);
38 mpTexture
->InitializeSlotMechanism(nNumberOfSlots
);
43 mpTexture
->ResetSlotDeallocateCallback();
46 void allocateSlot(int nSlot
)
48 maAllocatedSlots
[nSlot
] = true;
52 void deallocateSlot(int nSlot
)
54 maAllocatedSlots
[nSlot
] = false;
58 int findAndAllocateFreeSlot()
60 for (size_t i
= 0; i
< maAllocatedSlots
.size(); ++i
)
62 if (!maAllocatedSlots
[i
])
72 FixedTexture(const FixedTexture
&) = delete;
73 FixedTexture
& operator=(const FixedTexture
&) = delete;
76 FixedTextureAtlasManager::FixedTextureAtlasManager(int nWidthFactor
, int nHeightFactor
, int nSubTextureSize
)
77 : mWidthFactor(nWidthFactor
)
78 , mHeightFactor(nHeightFactor
)
79 , mSubTextureSize(nSubTextureSize
)
83 FixedTextureAtlasManager::~FixedTextureAtlasManager()
87 void FixedTextureAtlasManager::CreateNewTexture()
89 int nTextureWidth
= mWidthFactor
* mSubTextureSize
;
90 int nTextureHeight
= mHeightFactor
* mSubTextureSize
;
91 maFixedTextures
.push_back(std::make_unique
<FixedTexture
>(nTextureWidth
, nTextureHeight
, mWidthFactor
* mHeightFactor
));
94 OpenGLTexture
FixedTextureAtlasManager::Reserve(int nWidth
, int nHeight
)
96 FixedTexture
* pFixedTexture
= nullptr;
98 auto funFreeSlot
= [] (std::unique_ptr
<FixedTexture
>& inFixedTexture
)
100 return inFixedTexture
->mnFreeSlots
> 0;
103 auto it
= std::find_if(maFixedTextures
.begin(), maFixedTextures
.end(), funFreeSlot
);
105 if (it
!= maFixedTextures
.end())
107 pFixedTexture
= (*it
).get();
112 pFixedTexture
= maFixedTextures
.back().get();
115 int nSlot
= pFixedTexture
->findAndAllocateFreeSlot();
117 // Calculate coordinates in texture
118 int nX
= (nSlot
% mWidthFactor
) * mSubTextureSize
;
119 int nY
= (nSlot
/ mWidthFactor
) * mSubTextureSize
;
121 tools::Rectangle
aRectangle(Point(nX
, nY
), Size(nWidth
, nHeight
));
123 return OpenGLTexture(pFixedTexture
->mpTexture
, aRectangle
, nSlot
);
126 OpenGLTexture
FixedTextureAtlasManager::InsertBuffer(int nWidth
, int nHeight
, int nFormat
, int nType
, sal_uInt8
const * pData
)
128 OpenGLTexture aTexture
= Reserve(nWidth
, nHeight
);
129 if (pData
== nullptr)
132 aTexture
.CopyData(nWidth
, nHeight
, nFormat
, nType
, pData
);
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */