Skip to content

Commit 069b5d5

Browse files
committed
Update QNanoPainter
2c2d9d2f08dc0fc04e6781493b6e5f1bd16c4d65
1 parent 90a36a6 commit 069b5d5

File tree

6 files changed

+27
-46
lines changed

6 files changed

+27
-46
lines changed

thirdparty/libqnanopainter/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
cmake_minimum_required(VERSION 3.0.0)
22
project(qnanopainter VERSION 0.1.0)
33

4-
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON" FORCE)
5-
64
option(QNANO_DEBUG_COLLECT "Enable this to get the drawing debug information" OFF)
75
option(QNANO_DEBUG_RENDER "Enable this to render the drawing debug information" OFF)
86
option(QNANO_QT_GL_INCLUDE "Enable this to let Qt include OpenGL headers" ON)
@@ -12,6 +10,7 @@ option(QNANO_ENABLE_TOUCH_SIGNALS "This will enable signalling touch events
1210
option(QNANO_ENABLE_PAINT_SIGNALS "This will enable signalling paint events
1311
Can be useful when using view/widget classes directly" OFF)
1412
option(QNANO_USE_RENDERNODE "Enable this to use QRenderNode (available since Qt 5.8.0) instead of QQuickFramebufferObject" OFF)
13+
option(QNANO_ENABLE_PAINTER_SHARING "Enable this to share QNanoPainter instance between QNanoQuickItemPainters" ON)
1514
option(QNANO_USE_RHI "Enable this to use QRhi (available since Qt 6.7) instead of QQuickFramebufferObject" OFF)
1615

1716
option(QNANO_BUILD_GLES_BACKENDS "When building for embedded devices you can define manually which backends are supported" OFF)
@@ -168,7 +167,9 @@ endif()
168167
target_link_libraries(qnanopainter ${QNANOPAINTER_QT_LIBS} )
169168

170169
# add to INCLUDE PATH
171-
target_include_directories(qnanopainter PUBLIC .)
170+
target_include_directories(qnanopainter
171+
PUBLIC .
172+
PUBLIC nanovg)
172173

173174
# cmake variable ==> c++ #define
174175
if(QNANO_QT_GL_INCLUDE)

thirdparty/libqnanopainter/include.pri

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ DEFINES += QNANO_ENABLE_GLES3
3333
# Enable this to use QRenderNode (available since Qt 5.8.0) instead of QQuickFramebufferObject
3434
#DEFINES += QNANO_USE_RENDERNODE
3535

36+
# This will enable sharing the QNanoPainter instance between QNanoQuickItemPainters
37+
# Sharing the painter saves memory, but can have issues e.g. when using QNanoPainter
38+
# from multiple windows (see https://github.com/QUItCoding/qnanopainter/issues/80)
39+
DEFINES += QNANO_ENABLE_PAINTER_SHARING
40+
3641
# Suppress fontstash warnings about fopen & strncpy usage
3742
DEFINES += _CRT_SECURE_NO_WARNINGS
3843

thirdparty/libqnanopainter/qnanoimage.cpp

+4-35
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,6 @@ QNanoImage::QNanoImage(const QImage &image, const QString &filename, ImageFlags
9999
updateUniqueKey();
100100
}
101101

102-
QNanoImage::QNanoImage(QIODevice *device, const QString &uniqueKey, ImageFlags flags)
103-
: m_device(device)
104-
, m_uniqueKey(uniqueKey)
105-
, m_flags(flags)
106-
{
107-
Q_ASSERT(device);
108-
updateUniqueKey();
109-
}
110-
111102
/*!
112103
\fn void QNanoImage::setFilename(const QString &filename)
113104
@@ -220,21 +211,6 @@ QNanoImage QNanoImage::fromCache(QNanoPainter *painter, const QString &filename,
220211
return image;
221212
}
222213

223-
QNanoImage QNanoImage::fromCache(QNanoPainter *painter, QIODevice *device, const QString &uniqueKey, ImageFlags flags)
224-
{
225-
Q_ASSERT(painter);
226-
Q_ASSERT(device);
227-
QNanoImage image;
228-
image.m_imageData.reset(new QNanoDataElement());
229-
image.m_device = device;
230-
image.m_uniqueKey = uniqueKey;
231-
image.m_flags = flags;
232-
image.updateUniqueKey();
233-
image.m_parentPainter = painter;
234-
image.getID(painter->nvgCtx());
235-
return image;
236-
}
237-
238214
// ***** Private *****
239215

240216
/*!
@@ -280,17 +256,10 @@ int QNanoImage::getID(NVGcontext* nvg)
280256
}
281257
} else {
282258
// Image is not yet in cache, so load and add it
283-
QIODevice *device;
284-
QFile file;
285-
if (m_device)
286-
device = m_device;
287-
else {
288-
file.setFileName(m_filename);
289-
device = &file;
290-
}
291-
if (device->open(QIODevice::ReadOnly)) {
259+
QFile file(m_filename);
260+
if (file.open(QFile::ReadOnly)) {
292261
m_imageData.reset(new QNanoDataElement());
293-
QByteArray array = device->readAll();
262+
QByteArray array = file.readAll();
294263
int length = array.size();
295264
unsigned char* data = reinterpret_cast<unsigned char*>(&array.data()[0]);
296265
m_imageData->id = nvgCreateImageMem(nvg, m_flags, data, length);
@@ -310,7 +279,7 @@ void QNanoImage::updateUniqueKey()
310279
{
311280
if (m_textureId > 0)
312281
m_uniqueKey = QString("%1_").arg(QString::number(m_textureId));
313-
else if(!m_device)
282+
else
314283
m_uniqueKey = m_filename;
315284

316285
m_uniqueKey.append(QString::number(m_flags));

thirdparty/libqnanopainter/qnanoimage.h

-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ class QNanoImage
5555
// Constructs an image from QImage with the filename and flags
5656
QNanoImage(const QImage &image, const QString &filename, ImageFlags flags = {});
5757

58-
// Constructs an image from the data in the QIODevice, unique key and flags
59-
QNanoImage(QIODevice *device, const QString &uniqueKey, ImageFlags flags = {});
60-
6158
// Set the filename of the image
6259
void setFilename(const QString &filename);
6360

@@ -72,7 +69,6 @@ class QNanoImage
7269

7370
static QNanoImage fromFrameBuffer(const QOpenGLFramebufferObject *fbo, ImageFlags flags = QNanoImage::FLIPY);
7471
static QNanoImage fromCache(QNanoPainter *painter, const QString &filename, ImageFlags flags = {});
75-
static QNanoImage fromCache(QNanoPainter *painter, QIODevice *device, const QString &uniqueKey, ImageFlags flags = {});
7672

7773
private:
7874
friend class QNanoPainter;
@@ -89,7 +85,6 @@ class QNanoImage
8985
QNanoPainter *m_parentPainter = nullptr;
9086
QSharedPointer<QNanoDataElement> m_imageData;
9187
std::unique_ptr<QImage> m_image;
92-
QIODevice *m_device = nullptr;
9388
QString m_filename;
9489
GLuint m_textureId = 0;
9590
QNanoImage::ImageFlags m_flags = {};

thirdparty/libqnanopainter/qnanopainter.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ class QNanoPainter
246246
int textBreakLines(const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows);
247247
*/
248248

249+
inline NVGcontext* nvgCtx() const {
250+
return m_nvgContext;
251+
}
252+
249253
private:
250254

251255
friend class QNanoDebug;
@@ -261,9 +265,6 @@ class QNanoPainter
261265

262266
static QNanoPainter *getInstance();
263267

264-
inline NVGcontext* nvgCtx() const {
265-
return m_nvgContext;
266-
}
267268
#ifdef QNANO_USE_RHI
268269
inline void setNvgCtx(NVGcontext* ctx) {
269270
m_nvgContext = ctx;

thirdparty/libqnanopainter/qnanoquickitempainter.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646
*/
4747

4848
QNanoQuickItemPainter::QNanoQuickItemPainter()
49+
#ifdef QNANO_ENABLE_PAINTER_SHARING
4950
: m_painter(QNanoPainter::getInstance())
51+
#else
52+
: m_painter(new QNanoPainter())
53+
#endif
5054
{
5155
#ifndef QNANO_USE_RHI
5256
// Initialize QOpenGLFunctions for the context
@@ -60,6 +64,10 @@ QNanoQuickItemPainter::QNanoQuickItemPainter()
6064

6165
QNanoQuickItemPainter::~QNanoQuickItemPainter()
6266
{
67+
#ifndef QNANO_ENABLE_PAINTER_SHARING
68+
delete m_painter;
69+
m_painter = nullptr;
70+
#endif
6371
}
6472

6573
/*!
@@ -385,7 +393,9 @@ void QNanoQuickItemPainter::render(const RenderState *)
385393
void QNanoQuickItemPainter::render()
386394
#endif
387395
{
396+
#ifdef QNANO_ENABLE_PAINTER_SHARING
388397
m_painter->reset(); // reset context data as painter is shared.
398+
#endif
389399

390400
m_painter->setPixelAlign(static_cast<QNanoPainter::PixelAlign>(m_pixelAlign));
391401
m_painter->setPixelAlignText(static_cast<QNanoPainter::PixelAlign>(m_pixelAlignText));

0 commit comments

Comments
 (0)