summaryrefslogtreecommitdiffstats
path: root/libreofficekit/source/gtk/tilebuffer.cxx
blob: 82f0253d9e8207af3fe961cc7cdc46085320cae7 (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
/* -*- 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 "tilebuffer.hxx"


/* ------------------
   Utility functions
   ------------------
*/
float pixelToTwip(float fInput, float zoom)
{
    return (fInput / DPI / zoom) * 1440.0f;
}

float twipToPixel(float fInput, float zoom)
{
    return fInput / 1440.0f * DPI * zoom;
}

/* ----------------------------
   Tile class member functions
   ----------------------------
*/
cairo_surface_t* Tile::getBuffer()
{
    return m_pBuffer;
}

void Tile::setSurface(cairo_surface_t *buffer)
{
    if (m_pBuffer == buffer)
        return;
    if (m_pBuffer)
        cairo_surface_destroy(m_pBuffer);
    if (buffer != nullptr)
        cairo_surface_reference(buffer);
    m_pBuffer = buffer;
}

/* ----------------------------------
   TileBuffer class member functions
   ----------------------------------
*/
void TileBuffer::resetAllTiles()
{
    for (auto & tile : m_mTiles)
    {
        tile.second.valid = false;
    }
}

void TileBuffer::setInvalid(int x, int y, float fZoom, GTask* task,
                            GThreadPool* lokThreadPool)
{
    int index = x * m_nWidth + y;
    GError* error = nullptr;
    if (m_mTiles.find(index) != m_mTiles.end())
    {
        m_mTiles[index].valid = false;

        LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE);
        pLOEvent->m_nPaintTileX = x;
        pLOEvent->m_nPaintTileY = y;
        pLOEvent->m_fPaintTileZoom = fZoom;
        g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
        g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
        if (error != nullptr)
        {
            g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
            g_clear_error(&error);
        }
    }
}

Tile& TileBuffer::getTile(int x, int y, GTask* task,
                          GThreadPool* lokThreadPool)
{
    int index = x * m_nWidth + y;
    GError* error = nullptr;

    if (m_mTiles.find(index) != m_mTiles.end() && !m_mTiles[index].valid)
    {
        g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
        if (error != nullptr)
        {
            g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
            g_clear_error(&error);
        }
        return m_mTiles[index];
    }
    else if(m_mTiles.find(index) == m_mTiles.end())
    {
        g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
        if (error != nullptr)
        {
            g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
            g_clear_error(&error);
        }
        return m_DummyTile;
    }

    return m_mTiles[index];
}

void TileBuffer::setTile(int x, int y, cairo_surface_t *surface)
{
    int index = x * m_nWidth + y;

    m_mTiles[index].setSurface(surface);
    m_mTiles[index].valid = true;
}

bool TileBuffer::hasValidTile(int x, int y)
{
    int index = x * m_nWidth + y;
    auto it = m_mTiles.find(index);
    return (it != m_mTiles.end()) && it->second.valid;
}

void LOEvent::destroy(void* pMemory)
{
    LOEvent* pLOEvent = static_cast<LOEvent*>(pMemory);
    delete pLOEvent;
}

GQuark
LOKTileBufferErrorQuark()
{
    return g_quark_from_static_string("lok-tilebuffer-error");
}

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