nss: upgrade to release 3.73
[LibreOffice.git] / vcl / opengl / FixedTextureAtlas.cxx
blob7425942d127d0075f589fc923d2040c26ce0f07c
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 */
11 #include <memory>
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>
21 struct FixedTexture
23 std::shared_ptr<ImplOpenGLTexture> mpTexture;
24 int mnFreeSlots;
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);
41 ~FixedTexture()
43 mpTexture->ResetSlotDeallocateCallback();
46 void allocateSlot(int nSlot)
48 maAllocatedSlots[nSlot] = true;
49 mnFreeSlots--;
52 void deallocateSlot(int nSlot)
54 maAllocatedSlots[nSlot] = false;
55 mnFreeSlots++;
58 int findAndAllocateFreeSlot()
60 for (size_t i = 0; i < maAllocatedSlots.size(); ++i)
62 if (!maAllocatedSlots[i])
64 allocateSlot(i);
65 return i;
68 return -1;
71 private:
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();
109 else
111 CreateNewTexture();
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)
130 return aTexture;
132 aTexture.CopyData(nWidth, nHeight, nFormat, nType, pData);
134 return aTexture;
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */