summaryrefslogtreecommitdiff
path: root/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/UpdateAtlas.cpp
blob: e2c4ffeee42ddc6b759d8121ca2a67859b632acc (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
/*
 Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
 Copyright (C) 2012 Company 100, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "UpdateAtlas.h"

#if USE(COORDINATED_GRAPHICS)

#include <WebCore/CoordinatedGraphicsState.h>
#include <WebCore/GraphicsContext.h>
#include <WebCore/IntRect.h>
#include <wtf/MathExtras.h>

using namespace WebCore;

namespace WebKit {

class UpdateAtlasSurfaceClient final : public CoordinatedSurface::Client {
public:
    UpdateAtlasSurfaceClient(CoordinatedSurface::Client& client, const IntSize& size, bool supportsAlpha)
        : m_client(client)
        , m_size(size)
        , m_supportsAlpha(supportsAlpha)
    {
    }

    void paintToSurfaceContext(GraphicsContext& context) override
    {
        if (m_supportsAlpha) {
            context.setCompositeOperation(CompositeCopy);
            context.fillRect(IntRect(IntPoint::zero(), m_size), Color::transparent);
            context.setCompositeOperation(CompositeSourceOver);
        }

        m_client.paintToSurfaceContext(context);
    }

private:
    CoordinatedSurface::Client& m_client;
    IntSize m_size;
    bool m_supportsAlpha;
};

UpdateAtlas::UpdateAtlas(Client& client, int dimension, CoordinatedSurface::Flags flags)
    : m_client(client)
{
    static uint32_t nextID = 0;
    m_ID = ++nextID;
    IntSize size = nextPowerOfTwo(IntSize(dimension, dimension));
    m_surface = CoordinatedSurface::create(size, flags);

    m_client.createUpdateAtlas(m_ID, m_surface.copyRef());
}

UpdateAtlas::~UpdateAtlas()
{
    if (m_surface)
        m_client.removeUpdateAtlas(m_ID);
}

void UpdateAtlas::buildLayoutIfNeeded()
{
    if (m_areaAllocator)
        return;
    m_areaAllocator = std::make_unique<GeneralAreaAllocator>(size());
    m_areaAllocator->setMinimumAllocation(IntSize(32, 32));
}

void UpdateAtlas::didSwapBuffers()
{
    m_areaAllocator = nullptr;
}

bool UpdateAtlas::paintOnAvailableBuffer(const IntSize& size, uint32_t& atlasID, IntPoint& offset, CoordinatedSurface::Client& client)
{
    m_inactivityInSeconds = 0;
    buildLayoutIfNeeded();
    IntRect rect = m_areaAllocator->allocate(size);

    // No available buffer was found.
    if (rect.isEmpty())
        return false;

    if (!m_surface)
        return false;

    atlasID = m_ID;

    // FIXME: Use tri-state buffers, to allow faster updates.
    offset = rect.location();

    UpdateAtlasSurfaceClient surfaceClient(client, size, supportsAlpha());
    m_surface->paintToSurface(rect, surfaceClient);

    return true;
}

} // namespace WebCore
#endif // USE(COORDINATED_GRAPHICS)