1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <memory>
#include <sal/config.h>
#include <vcl/opengl/OpenGLContext.hxx>
#include <vcl/opengl/OpenGLHelper.hxx>
#include <opengl/framebuffer.hxx>
#include <opengl/texture.hxx>
#include <opengl/FixedTextureAtlas.hxx>
struct FixedTexture
{
std::shared_ptr<ImplOpenGLTexture> mpTexture;
int mnFreeSlots;
std::vector<bool> maAllocatedSlots;
FixedTexture(int nTextureWidth, int nTextureHeight, int nNumberOfSlots)
: mpTexture(std::make_shared<ImplOpenGLTexture>(nTextureWidth, nTextureHeight, true))
, mnFreeSlots(nNumberOfSlots)
, maAllocatedSlots(nNumberOfSlots, false)
{
auto aDeallocateFunction = [this] (int nSlotNumber)
{
deallocateSlot(nSlotNumber);
};
mpTexture->SetSlotDeallocateCallback(aDeallocateFunction);
mpTexture->InitializeSlotMechanism(nNumberOfSlots);
}
~FixedTexture()
{
mpTexture->ResetSlotDeallocateCallback();
}
void allocateSlot(int nSlot)
{
maAllocatedSlots[nSlot] = true;
mnFreeSlots--;
}
void deallocateSlot(int nSlot)
{
maAllocatedSlots[nSlot] = false;
mnFreeSlots++;
}
int findAndAllocateFreeSlot()
{
for (size_t i = 0; i < maAllocatedSlots.size(); ++i)
{
if (!maAllocatedSlots[i])
{
allocateSlot(i);
return i;
}
}
return -1;
}
private:
FixedTexture(const FixedTexture&) = delete;
FixedTexture& operator=(const FixedTexture&) = delete;
};
FixedTextureAtlasManager::FixedTextureAtlasManager(int nWidthFactor, int nHeightFactor, int nSubTextureSize)
: mWidthFactor(nWidthFactor)
, mHeightFactor(nHeightFactor)
, mSubTextureSize(nSubTextureSize)
{
}
FixedTextureAtlasManager::~FixedTextureAtlasManager()
{
}
void FixedTextureAtlasManager::CreateNewTexture()
{
int nTextureWidth = mWidthFactor * mSubTextureSize;
int nTextureHeight = mHeightFactor * mSubTextureSize;
maFixedTextures.push_back(std::make_unique<FixedTexture>(nTextureWidth, nTextureHeight, mWidthFactor * mHeightFactor));
}
OpenGLTexture FixedTextureAtlasManager::Reserve(int nWidth, int nHeight)
{
FixedTexture* pFixedTexture = nullptr;
auto funFreeSlot = [] (std::unique_ptr<FixedTexture>& inFixedTexture)
{
return inFixedTexture->mnFreeSlots > 0;
};
auto it = std::find_if(maFixedTextures.begin(), maFixedTextures.end(), funFreeSlot);
if (it != maFixedTextures.end())
{
pFixedTexture = (*it).get();
}
else
{
CreateNewTexture();
pFixedTexture = maFixedTextures.back().get();
}
int nSlot = pFixedTexture->findAndAllocateFreeSlot();
// Calculate coordinates in texture
int nX = (nSlot % mWidthFactor) * mSubTextureSize;
int nY = (nSlot / mWidthFactor) * mSubTextureSize;
tools::Rectangle aRectangle(Point(nX, nY), Size(nWidth, nHeight));
return OpenGLTexture(pFixedTexture->mpTexture, aRectangle, nSlot);
}
OpenGLTexture FixedTextureAtlasManager::InsertBuffer(int nWidth, int nHeight, int nFormat, int nType, sal_uInt8 const * pData)
{
OpenGLTexture aTexture = Reserve(nWidth, nHeight);
if (pData == nullptr)
return aTexture;
aTexture.CopyData(nWidth, nHeight, nFormat, nType, pData);
return aTexture;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|