summaryrefslogtreecommitdiff
path: root/vcl/skia/packedsurfaceatlas.cxx
blob: b3ce81154856c2a0627aa4b06a08170b8606ef32 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- 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 <skia/packedsurfaceatlas.hxx>

#include <memory>
#include <assert.h>

#include <sal/config.h>
#include <sal/log.hxx>

#include <skia/utils.hxx>

namespace
{
struct Node
{
    tools::Rectangle const mRectangle;
    std::unique_ptr<Node> mLeftNode;
    std::unique_ptr<Node> mRightNode;
    bool mOccupied;

    explicit Node(int nWidth, int nHeight);
    explicit Node(tools::Rectangle const& aRectangle);

    bool isLeaf() const;
    Node* insert(int nWidth, int nHeight, int nPadding);
};
}

Node::Node(int nWidth, int nHeight)
    : mRectangle(tools::Rectangle(Point(), Size(nWidth, nHeight)))
    , mLeftNode()
    , mRightNode()
    , mOccupied(false)
{
}

Node::Node(tools::Rectangle const& aRectangle)
    : mRectangle(aRectangle)
    , mLeftNode()
    , mRightNode()
    , mOccupied(false)
{
}

bool Node::isLeaf() const { return mLeftNode == nullptr && mRightNode == nullptr; }

Node* Node::insert(int nWidth, int nHeight, int nPadding)
{
    if (!isLeaf())
    {
        Node* pNewNode = mLeftNode->insert(nWidth, nHeight, nPadding);

        if (pNewNode != nullptr)
            return pNewNode;

        return mRightNode->insert(nWidth, nHeight, nPadding);
    }
    else
    {
        if (mOccupied)
        {
            return nullptr;
        }

        if (nWidth > mRectangle.GetWidth() || nHeight > mRectangle.GetHeight())
        { // does not fit
            return nullptr;
        }

        if (nWidth == mRectangle.GetWidth() && nHeight == mRectangle.GetHeight())
        { // perfect fit
            mOccupied = true;
            return this;
        }

        int dw = mRectangle.GetWidth() - nWidth;
        int dh = mRectangle.GetHeight() - nHeight;

        tools::Rectangle aLeftRect;
        tools::Rectangle aRightRect;
        if (dw > dh)
        {
            aLeftRect = tools::Rectangle(Point(mRectangle.Left(), mRectangle.Top()),
                                         Size(nWidth, mRectangle.GetHeight()));
            aRightRect = tools::Rectangle(
                Point(nPadding + mRectangle.Left() + nWidth, mRectangle.Top()),
                Size(mRectangle.GetWidth() - nWidth - nPadding, mRectangle.GetHeight()));
        }
        else
        {
            aLeftRect = tools::Rectangle(Point(mRectangle.Left(), mRectangle.Top()),
                                         Size(mRectangle.GetWidth(), nHeight));
            aRightRect = tools::Rectangle(
                Point(mRectangle.Left(), nPadding + mRectangle.Top() + nHeight),
                Size(mRectangle.GetWidth(), mRectangle.GetHeight() - nHeight - nPadding));
        }

        mLeftNode.reset(new Node(aLeftRect));
        mRightNode.reset(new Node(aRightRect));

        return mLeftNode->insert(nWidth, nHeight, nPadding);
    }
}

struct SkiaPackedSurfaceAtlasManager::PackedSurface
{
    sk_sp<SkSurface> mpSurface;
    std::unique_ptr<Node> mpRootNode;

    PackedSurface(int nWidth, int nHeight)
        : mpSurface(SkiaHelper::createSkSurface(nWidth, nHeight))
        , mpRootNode(new Node(nWidth, nHeight))
    {
        assert(mpSurface);
    }
};

SkiaPackedSurfaceAtlasManager::SkiaPackedSurfaceAtlasManager(int nSurfaceWidth, int nSurfaceHeight)
    : mnSurfaceWidth(nSurfaceWidth)
    , mnSurfaceHeight(nSurfaceHeight)
{
}

SkiaPackedSurfaceAtlasManager::~SkiaPackedSurfaceAtlasManager() {}

void SkiaPackedSurfaceAtlasManager::CreateNewSurface()
{
    std::unique_ptr<PackedSurface> pPackedSurface(
        new PackedSurface(mnSurfaceWidth, mnSurfaceHeight));
    maPackedSurfaces.push_back(std::move(pPackedSurface));
    SAL_INFO("vcl.skia",
             "SkiaPackedSurfaceAtlas adding surface, count: " << maPackedSurfaces.size());
}

SkiaPackedSurface SkiaPackedSurfaceAtlasManager::Reserve(int nWidth, int nHeight)
{
    for (std::unique_ptr<PackedSurface>& pPackedSurface : maPackedSurfaces)
    {
        Node* pNode = pPackedSurface->mpRootNode->insert(nWidth, nHeight, 1);
        if (pNode != nullptr)
            return SkiaPackedSurface(pPackedSurface->mpSurface, pNode->mRectangle);
    }
    CreateNewSurface();
    std::unique_ptr<PackedSurface>& pPackedSurface = maPackedSurfaces.back();
    Node* pNode = pPackedSurface->mpRootNode->insert(nWidth, nHeight, 1);
    if (pNode != nullptr)
        return SkiaPackedSurface(pPackedSurface->mpSurface, pNode->mRectangle);
    return SkiaPackedSurface();
}

std::vector<sk_sp<SkSurface>>
SkiaPackedSurfaceAtlasManager::ReduceSurfaceNumber(int nMaxNumberOfSurfaces)
{
    std::vector<sk_sp<SkSurface>> aSurfaces;
    while (int(maPackedSurfaces.size()) > nMaxNumberOfSurfaces)
    {
        // Remove oldest created surface
        aSurfaces.push_back(maPackedSurfaces[0]->mpSurface);
        maPackedSurfaces.erase(maPackedSurfaces.begin());
        SAL_INFO("vcl.skia",
                 "PackedSurfaceAtlas removing surface, count: " << maPackedSurfaces.size());
    }
    return aSurfaces;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */