LibreOffice Module libreofficekit (master) 1
tilebuffer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
10#include "tilebuffer.hxx"
11
13
14
15/* ------------------
16 Utility functions
17 ------------------
18*/
19// We know that VirtualDevices use a DPI of 96.
20float pixelToTwip(float fInput, float zoom)
21{
22 return o3tl::toTwips(fInput / zoom, o3tl::Length::px);
23}
24
25float twipToPixel(float fInput, float zoom)
26{
28}
29
30/* ----------------------------
31 Tile class member functions
32 ----------------------------
33*/
35{
36 return m_pBuffer;
37}
38
40{
41 if (m_pBuffer == buffer)
42 return;
43 if (m_pBuffer)
44 cairo_surface_destroy(m_pBuffer);
45 if (buffer != nullptr)
46 cairo_surface_reference(buffer);
47 m_pBuffer = buffer;
48}
49
50/* ----------------------------------
51 TileBuffer class member functions
52 ----------------------------------
53*/
55{
56 for (auto & tile : m_mTiles)
57 {
58 tile.second.valid = false;
59 }
60}
61
62void TileBuffer::setInvalid(int x, int y, float fZoom, GTask* task,
63 GThreadPool* lokThreadPool)
64{
65 int index = x * m_nWidth + y;
66 GError* error = nullptr;
67 if (m_mTiles.find(index) == m_mTiles.end())
68 return;
69
70 m_mTiles[index].valid = false;
71
72 LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE);
73 pLOEvent->m_nPaintTileX = x;
74 pLOEvent->m_nPaintTileY = y;
75 pLOEvent->m_fPaintTileZoom = fZoom;
76 g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
77 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
78 if (error != nullptr)
79 {
80 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
81 g_clear_error(&error);
82 }
83}
84
85Tile& TileBuffer::getTile(int x, int y, GTask* task,
86 GThreadPool* lokThreadPool)
87{
88 int index = x * m_nWidth + y;
89 GError* error = nullptr;
90
91 if (m_mTiles.find(index) != m_mTiles.end() && !m_mTiles[index].valid)
92 {
93 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
94 if (error != nullptr)
95 {
96 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
97 g_clear_error(&error);
98 }
99 return m_mTiles[index];
100 }
101 else if(m_mTiles.find(index) == m_mTiles.end())
102 {
103 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
104 if (error != nullptr)
105 {
106 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
107 g_clear_error(&error);
108 }
109 return m_DummyTile;
110 }
111
112 return m_mTiles[index];
113}
114
115void TileBuffer::setTile(int x, int y, cairo_surface_t *surface)
116{
117 int index = x * m_nWidth + y;
118
119 m_mTiles[index].setSurface(surface);
120 m_mTiles[index].valid = true;
121}
122
123bool TileBuffer::hasValidTile(int x, int y)
124{
125 int index = x * m_nWidth + y;
126 auto it = m_mTiles.find(index);
127 return (it != m_mTiles.end()) && it->second.valid;
128}
129
130void LOEvent::destroy(void* pMemory)
131{
132 LOEvent* pLOEvent = static_cast<LOEvent*>(pMemory);
133 delete pLOEvent;
134}
135
136GQuark
138{
139 return g_quark_from_static_string("lok-tilebuffer-error");
140}
141
142/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _cairo_surface cairo_surface_t
void setInvalid(int x, int y, float zoom, GTask *task, GThreadPool *)
Marks the tile as invalid.
Definition: tilebuffer.cxx:62
void setTile(int x, int y, cairo_surface_t *surface)
Definition: tilebuffer.cxx:115
void resetAllTiles()
Destroys all the tiles in the tile buffer; also frees the memory allocated for all the Tile objects.
Definition: tilebuffer.cxx:54
bool hasValidTile(int x, int y)
Returns true if a valid tile exists at this location.
Definition: tilebuffer.cxx:123
int m_nWidth
Width of the current tile buffer (number of columns)
Definition: tilebuffer.hxx:150
Tile m_DummyTile
Dummy tile.
Definition: tilebuffer.hxx:152
Tile & getTile(int x, int y, GTask *task, GThreadPool *pool)
Gets the underlying Tile object for given position.
Definition: tilebuffer.cxx:85
std::map< int, Tile > m_mTiles
Stores all the tiles cached by this tile buffer.
Definition: tilebuffer.hxx:148
This class represents a single tile in the tile buffer.
Definition: tilebuffer.hxx:54
cairo_surface_t * getBuffer()
Function to get the pointer to enclosing cairo_surface_t.
Definition: tilebuffer.cxx:34
cairo_surface_t * m_pBuffer
Pixel buffer data for this tile.
Definition: tilebuffer.hxx:80
void setSurface(cairo_surface_t *)
Used to set the pixel buffer of this object.
Definition: tilebuffer.cxx:39
float y
float x
index
constexpr auto toTwips(N number, Length from)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
A struct that we use to store the data about the LOK call.
Definition: tilebuffer.hxx:183
float m_fPaintTileZoom
Definition: tilebuffer.hxx:214
static void destroy(void *pMemory)
Wrapper around delete to help GLib.
Definition: tilebuffer.cxx:130
int m_nPaintTileY
Definition: tilebuffer.hxx:213
int m_nPaintTileX
Definition: tilebuffer.hxx:212
float pixelToTwip(float fInput, float zoom)
Converts the pixel value to zoom independent twip value.
Definition: tilebuffer.cxx:20
float twipToPixel(float fInput, float zoom)
Converts the zoom independent twip value pixel value.
Definition: tilebuffer.cxx:25
GQuark LOKTileBufferErrorQuark()
Gets GQuark identifying this tile buffer errors.
Definition: tilebuffer.cxx:137
@ LOK_PAINT_TILE
Definition: tilebuffer.hxx:163