| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickcontext2dtile_p.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | QQuickContext2DTile::QQuickContext2DTile() |
| 9 | : m_dirty(true) |
| 10 | , m_rect(QRect(0, 0, 1, 1)) |
| 11 | , m_device(nullptr) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | QQuickContext2DTile::~QQuickContext2DTile() |
| 16 | { |
| 17 | if (m_painter.isActive()) |
| 18 | m_painter.end(); |
| 19 | } |
| 20 | |
| 21 | QPainter* QQuickContext2DTile::createPainter(bool smooth, bool antialiasing) |
| 22 | { |
| 23 | if (m_painter.isActive()) |
| 24 | m_painter.end(); |
| 25 | |
| 26 | aboutToDraw(); |
| 27 | if (m_device) { |
| 28 | m_painter.begin(m_device); |
| 29 | m_painter.resetTransform(); |
| 30 | m_painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 31 | |
| 32 | #ifdef QQUICKCONTEXT2D_DEBUG |
| 33 | int v = 100; |
| 34 | int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2; |
| 35 | if (gray) |
| 36 | v = 150; |
| 37 | m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255)); |
| 38 | #endif |
| 39 | |
| 40 | if (antialiasing) |
| 41 | m_painter.setRenderHints(hints: QPainter::Antialiasing | QPainter::TextAntialiasing, on: true); |
| 42 | else |
| 43 | m_painter.setRenderHints(hints: QPainter::Antialiasing | QPainter::TextAntialiasing, on: false); |
| 44 | |
| 45 | if (smooth) |
| 46 | m_painter.setRenderHint(hint: QPainter::SmoothPixmapTransform, on: true); |
| 47 | else |
| 48 | m_painter.setRenderHint(hint: QPainter::SmoothPixmapTransform, on: false); |
| 49 | |
| 50 | m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 51 | m_painter.translate(dx: -m_rect.left(), dy: -m_rect.top()); |
| 52 | m_painter.setClipRect(m_rect); |
| 53 | m_painter.setClipping(false); |
| 54 | return &m_painter; |
| 55 | } |
| 56 | |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | QQuickContext2DImageTile::QQuickContext2DImageTile() |
| 61 | : QQuickContext2DTile() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | QQuickContext2DImageTile::~QQuickContext2DImageTile() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void QQuickContext2DImageTile::setRect(const QRect& r) |
| 70 | { |
| 71 | if (m_rect == r) |
| 72 | return; |
| 73 | m_rect = r; |
| 74 | m_dirty = true; |
| 75 | if (m_image.size() != r.size()) { |
| 76 | m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied); |
| 77 | } |
| 78 | m_device = &m_image; |
| 79 | } |
| 80 | |
| 81 | QT_END_NAMESPACE |
| 82 | |
| 83 | |