diff options
Diffstat (limited to 'src/quick/items/context2d/qquickcontext2d.cpp')
-rw-r--r-- | src/quick/items/context2d/qquickcontext2d.cpp | 159 |
1 files changed, 102 insertions, 57 deletions
diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp index b2117d3eb9..2483a8eadb 100644 --- a/src/quick/items/context2d/qquickcontext2d.cpp +++ b/src/quick/items/context2d/qquickcontext2d.cpp @@ -43,6 +43,7 @@ #include <private/qquickcontext2dtexture_p.h> #include <private/qquickitem_p.h> #include <QtQuick/private/qquickshadereffectsource_p.h> +#include <qsgrendererinterface.h> #include <QtQuick/private/qsgcontext_p.h> #include <private/qquicksvgparser_p.h> @@ -74,6 +75,10 @@ #include <private/qguiapplication_p.h> #include <qpa/qplatformintegration.h> +#ifndef QT_NO_OPENGL +# include <private/qsgdefaultrendercontext_p.h> +#endif + #include <cmath> #if defined(Q_OS_QNX) || defined(Q_OS_ANDROID) #include <ctype.h> @@ -484,34 +489,43 @@ namespace QV4 { namespace Heap { struct QQuickJSContext2D : Object { - QQuickJSContext2D() {} + void init() { Object::init(); } QQuickContext2D* context; }; struct QQuickJSContext2DPrototype : Object { - QQuickJSContext2DPrototype() {} + void init() { Object::init(); } }; struct QQuickContext2DStyle : Object { - QQuickContext2DStyle() + void init() { + brush = new QBrush; patternRepeatX = false; patternRepeatY = false; } + void destroy() { + delete brush; + Object::destroy(); + } - QBrush brush; + QBrush *brush; bool patternRepeatX:1; bool patternRepeatY:1; }; struct QQuickJSContext2DPixelData : Object { - QQuickJSContext2DPixelData(); + void init(); + void destroy() { + delete image; + Object::destroy(); + } - QImage image; + QImage *image; }; struct QQuickJSContext2DImageData : Object { - QQuickJSContext2DImageData(); + void init(); QV4::Value pixelData; }; @@ -789,7 +803,7 @@ static QPainter::CompositionMode qt_composite_mode_from_string(const QString &co } else if (compositeOperator == QLatin1String("destination-over")) { return QPainter::CompositionMode_DestinationOver; } else if (compositeOperator == QLatin1String("lighter")) { - return QPainter::CompositionMode_Lighten; + return QPainter::CompositionMode_Plus; } else if (compositeOperator == QLatin1String("copy")) { return QPainter::CompositionMode_Source; } else if (compositeOperator == QLatin1String("xor")) { @@ -852,7 +866,7 @@ static QString qt_composite_mode_to_string(QPainter::CompositionMode op) case QPainter::CompositionMode_Xor: return QStringLiteral("xor"); case QPainter::CompositionMode_Plus: - return QStringLiteral("plus"); + return QStringLiteral("lighter"); case QPainter::CompositionMode_Multiply: return QStringLiteral("qt-multiply"); case QPainter::CompositionMode_Screen: @@ -892,8 +906,10 @@ struct QQuickJSContext2DPixelData : public QV4::Object static QV4::ReturnedValue proto_get_length(QV4::CallContext *ctx); }; -QV4::Heap::QQuickJSContext2DPixelData::QQuickJSContext2DPixelData() +void QV4::Heap::QQuickJSContext2DPixelData::init() { + Object::init(); + image = new QImage; QV4::Scope scope(internalClass->engine); QV4::ScopedObject o(scope, this); o->setArrayType(QV4::Heap::ArrayData::Custom); @@ -915,8 +931,9 @@ struct QQuickJSContext2DImageData : public QV4::Object } }; -QV4::Heap::QQuickJSContext2DImageData::QQuickJSContext2DImageData() +void QV4::Heap::QQuickJSContext2DImageData::init() { + Object::init(); pixelData = QV4::Primitive::undefinedValue(); QV4::Scope scope(internalClass->engine); @@ -938,11 +955,11 @@ static QV4::ReturnedValue qt_create_image_data(qreal w, qreal h, QV4::ExecutionE pixelData->setPrototype(p); if (image.isNull()) { - pixelData->d()->image = QImage(w, h, QImage::Format_ARGB32); - pixelData->d()->image.fill(0x00000000); + *pixelData->d()->image = QImage(w, h, QImage::Format_ARGB32); + pixelData->d()->image->fill(0x00000000); } else { Q_ASSERT(image.width() == qRound(w) && image.height() == qRound(h)); - pixelData->d()->image = image.format() == QImage::Format_ARGB32 ? image : image.convertToFormat(QImage::Format_ARGB32); + *pixelData->d()->image = image.format() == QImage::Format_ARGB32 ? image : image.convertToFormat(QImage::Format_ARGB32); } QV4::Scoped<QQuickJSContext2DImageData> imageData(scope, scope.engine->memoryManager->allocObject<QQuickJSContext2DImageData>()); @@ -1387,9 +1404,9 @@ QV4::ReturnedValue QQuickJSContext2D::method_set_fillStyle(QV4::CallContext *ctx r->d()->context->m_fillStyle.set(scope.engine, value); } else { QV4::Scoped<QQuickContext2DStyle> style(scope, value->as<QQuickContext2DStyle>()); - if (style && style->d()->brush != r->d()->context->state.fillStyle) { - r->d()->context->state.fillStyle = style->d()->brush; - r->d()->context->buffer()->setFillStyle(style->d()->brush, style->d()->patternRepeatX, style->d()->patternRepeatY); + if (style && *style->d()->brush != r->d()->context->state.fillStyle) { + r->d()->context->state.fillStyle = *style->d()->brush; + r->d()->context->buffer()->setFillStyle(*style->d()->brush, style->d()->patternRepeatX, style->d()->patternRepeatY); r->d()->context->m_fillStyle.set(scope.engine, value); r->d()->context->state.fillPatternRepeatX = style->d()->patternRepeatX; r->d()->context->state.fillPatternRepeatY = style->d()->patternRepeatY; @@ -1496,9 +1513,9 @@ QV4::ReturnedValue QQuickJSContext2D::method_set_strokeStyle(QV4::CallContext *c r->d()->context->m_strokeStyle.set(scope.engine, value); } else { QV4::Scoped<QQuickContext2DStyle> style(scope, value->as<QQuickContext2DStyle>()); - if (style && style->d()->brush != r->d()->context->state.strokeStyle) { - r->d()->context->state.strokeStyle = style->d()->brush; - r->d()->context->buffer()->setStrokeStyle(style->d()->brush, style->d()->patternRepeatX, style->d()->patternRepeatY); + if (style && *style->d()->brush != r->d()->context->state.strokeStyle) { + r->d()->context->state.strokeStyle = *style->d()->brush; + r->d()->context->buffer()->setStrokeStyle(*style->d()->brush, style->d()->patternRepeatX, style->d()->patternRepeatY); r->d()->context->m_strokeStyle.set(scope.engine, value); r->d()->context->state.strokePatternRepeatX = style->d()->patternRepeatX; r->d()->context->state.strokePatternRepeatY = style->d()->patternRepeatY; @@ -1556,7 +1573,7 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createLinearGradient(QV4:: QV4::Scoped<QQuickContext2DStyle> gradient(scope, scope.engine->memoryManager->allocObject<QQuickContext2DStyle>()); QV4::ScopedObject p(scope, ed->gradientProto.value()); gradient->setPrototype(p); - gradient->d()->brush = QLinearGradient(x0, y0, x1, y1); + *gradient->d()->brush = QLinearGradient(x0, y0, x1, y1); return gradient.asReturnedValue(); } @@ -1607,7 +1624,7 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createRadialGradient(QV4:: QV4::Scoped<QQuickContext2DStyle> gradient(scope, scope.engine->memoryManager->allocObject<QQuickContext2DStyle>()); QV4::ScopedObject p(scope, ed->gradientProto.value()); gradient->setPrototype(p); - gradient->d()->brush = QRadialGradient(QPointF(x1, y1), r0+r1, QPointF(x0, y0)); + *gradient->d()->brush = QRadialGradient(QPointF(x1, y1), r0+r1, QPointF(x0, y0)); return gradient.asReturnedValue(); } @@ -1650,7 +1667,7 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createConicalGradient(QV4: QV4::Scoped<QQuickContext2DStyle> gradient(scope, scope.engine->memoryManager->allocObject<QQuickContext2DStyle>()); QV4::ScopedObject p(scope, ed->gradientProto.value()); gradient->setPrototype(p); - gradient->d()->brush = QConicalGradient(x, y, angle); + *gradient->d()->brush = QConicalGradient(x, y, angle); return gradient.asReturnedValue(); } @@ -1715,7 +1732,7 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createPattern(QV4::CallCon if (patternMode >= 0 && patternMode < Qt::LinearGradientPattern) { style = static_cast<Qt::BrushStyle>(patternMode); } - pattern->d()->brush = QBrush(color, style); + *pattern->d()->brush = QBrush(color, style); } else { QImage patternTexture; @@ -1723,14 +1740,14 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createPattern(QV4::CallCon QV4::ScopedString s(scope, scope.engine->newString(QStringLiteral("data"))); QV4::Scoped<QQuickJSContext2DPixelData> pixelData(scope, o->get(s)); if (!!pixelData) { - patternTexture = pixelData->d()->image; + patternTexture = *pixelData->d()->image; } } else { patternTexture = r->d()->context->createPixmap(QUrl(ctx->args()[0].toQStringNoThrow()))->image(); } if (!patternTexture.isNull()) { - pattern->d()->brush.setTextureImage(patternTexture); + pattern->d()->brush->setTextureImage(patternTexture); QString repetition = ctx->args()[1].toQStringNoThrow(); if (repetition == QLatin1String("repeat") || repetition.isEmpty()) { @@ -2669,15 +2686,15 @@ QV4::ReturnedValue QQuickJSContext2D::method_set_textAlign(QV4::CallContext *ctx QString textAlign = s->toQString(); QQuickContext2D::TextAlignType ta; - if (textAlign == QStringLiteral("start")) + if (textAlign == QLatin1String("start")) ta = QQuickContext2D::Start; - else if (textAlign == QStringLiteral("end")) + else if (textAlign == QLatin1String("end")) ta = QQuickContext2D::End; - else if (textAlign == QStringLiteral("left")) + else if (textAlign == QLatin1String("left")) ta = QQuickContext2D::Left; - else if (textAlign == QStringLiteral("right")) + else if (textAlign == QLatin1String("right")) ta = QQuickContext2D::Right; - else if (textAlign == QStringLiteral("center")) + else if (textAlign == QLatin1String("center")) ta = QQuickContext2D::Center; else return QV4::Encode::undefined(); @@ -2920,8 +2937,8 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_drawImage(QV4::CallContext QV4::Scoped<QQuickJSContext2DImageData> imageData(scope, arg); if (!!imageData) { QV4::Scoped<QQuickJSContext2DPixelData> pix(scope, imageData->d()->pixelData.as<QQuickJSContext2DPixelData>()); - if (pix && !pix->d()->image.isNull()) { - pixmap.adopt(new QQuickCanvasPixmap(pix->d()->image)); + if (pix && !pix->d()->image->isNull()) { + pixmap.adopt(new QQuickCanvasPixmap(*pix->d()->image)); } else { V4THROW_DOM(DOMEXCEPTION_TYPE_MISMATCH_ERR, "drawImage(), type mismatch"); } @@ -3029,7 +3046,7 @@ QV4::ReturnedValue QQuickJSContext2DImageData::method_get_width(QV4::CallContext QV4::Scoped<QQuickJSContext2DPixelData> r(scope, imageData->d()->pixelData.as<QQuickJSContext2DPixelData>()); if (!r) return QV4::Encode(0); - return QV4::Encode(r->d()->image.width()); + return QV4::Encode(r->d()->image->width()); } /*! @@ -3045,7 +3062,7 @@ QV4::ReturnedValue QQuickJSContext2DImageData::method_get_height(QV4::CallContex QV4::Scoped<QQuickJSContext2DPixelData> r(scope, imageData->d()->pixelData.as<QQuickJSContext2DPixelData>()); if (!r) return QV4::Encode(0); - return QV4::Encode(r->d()->image.height()); + return QV4::Encode(r->d()->image->height()); } /*! @@ -3083,10 +3100,10 @@ QV4::ReturnedValue QQuickJSContext2DPixelData::proto_get_length(QV4::CallContext { QV4::Scope scope(ctx); QV4::Scoped<QQuickJSContext2DPixelData> r(scope, ctx->thisObject().as<QQuickJSContext2DPixelData>()); - if (!r || r->d()->image.isNull()) + if (!r || r->d()->image->isNull()) return QV4::Encode::undefined(); - return QV4::Encode(r->d()->image.width() * r->d()->image.height() * 4); + return QV4::Encode(r->d()->image->width() * r->d()->image->height() * 4); } QV4::ReturnedValue QQuickJSContext2DPixelData::getIndexed(const QV4::Managed *m, uint index, bool *hasProperty) @@ -3096,13 +3113,13 @@ QV4::ReturnedValue QQuickJSContext2DPixelData::getIndexed(const QV4::Managed *m, QV4::Scope scope(v4); QV4::Scoped<QQuickJSContext2DPixelData> r(scope, static_cast<const QQuickJSContext2DPixelData *>(m)); - if (index < static_cast<quint32>(r->d()->image.width() * r->d()->image.height() * 4)) { + if (index < static_cast<quint32>(r->d()->image->width() * r->d()->image->height() * 4)) { if (hasProperty) *hasProperty = true; - const quint32 w = r->d()->image.width(); + const quint32 w = r->d()->image->width(); const quint32 row = (index / 4) / w; const quint32 col = (index / 4) % w; - const QRgb* pixel = reinterpret_cast<const QRgb*>(r->d()->image.constScanLine(row)); + const QRgb* pixel = reinterpret_cast<const QRgb*>(r->d()->image->constScanLine(row)); pixel += col; switch (index % 4) { case 0: @@ -3131,12 +3148,12 @@ void QQuickJSContext2DPixelData::putIndexed(QV4::Managed *m, uint index, const Q QV4::Scoped<QQuickJSContext2DPixelData> r(scope, static_cast<QQuickJSContext2DPixelData *>(m)); const int v = value.toInt32(); - if (r && index < static_cast<quint32>(r->d()->image.width() * r->d()->image.height() * 4) && v >= 0 && v <= 255) { - const quint32 w = r->d()->image.width(); + if (r && index < static_cast<quint32>(r->d()->image->width() * r->d()->image->height() * 4) && v >= 0 && v <= 255) { + const quint32 w = r->d()->image->width(); const quint32 row = (index / 4) / w; const quint32 col = (index / 4) % w; - QRgb* pixel = reinterpret_cast<QRgb*>(r->d()->image.scanLine(row)); + QRgb* pixel = reinterpret_cast<QRgb*>(r->d()->image->scanLine(row)); pixel += col; switch (index % 4) { case 0: @@ -3187,8 +3204,8 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_createImageData(QV4::CallC if (!!imgData) { QV4::Scoped<QQuickJSContext2DPixelData> pa(scope, imgData->d()->pixelData.as<QQuickJSContext2DPixelData>()); if (pa) { - qreal w = pa->d()->image.width(); - qreal h = pa->d()->image.height(); + qreal w = pa->d()->image->width(); + qreal h = pa->d()->image->height(); return qt_create_image_data(w, h, scope.engine, QImage()); } } else if (arg0->isString()) { @@ -3266,8 +3283,8 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_putImageData(QV4::CallCont QV4::Scoped<QQuickJSContext2DPixelData> pixelArray(scope, imageData->d()->pixelData.as<QQuickJSContext2DPixelData>()); if (pixelArray) { - w = pixelArray->d()->image.width(); - h = pixelArray->d()->image.height(); + w = pixelArray->d()->image->width(); + h = pixelArray->d()->image->height(); if (ctx->argc() == 7) { dirtyX = ctx->args()[3].toNumber(); @@ -3316,7 +3333,7 @@ QV4::ReturnedValue QQuickJSContext2DPrototype::method_putImageData(QV4::CallCont dirtyHeight = h; } - QImage image = pixelArray->d()->image.copy(dirtyX, dirtyY, dirtyWidth, dirtyHeight); + QImage image = pixelArray->d()->image->copy(dirtyX, dirtyY, dirtyWidth, dirtyHeight); r->d()->context->buffer()->drawImage(image, QRectF(dirtyX, dirtyY, dirtyWidth, dirtyHeight), QRectF(dx, dy, dirtyWidth, dirtyHeight)); } return ctx->thisObject().asReturnedValue(); @@ -3351,9 +3368,9 @@ QV4::ReturnedValue QQuickContext2DStyle::gradient_proto_addColorStop(QV4::CallCo if (ctx->argc() == 2) { - if (!style->d()->brush.gradient()) + if (!style->d()->brush->gradient()) V4THROW_ERROR("Not a valid CanvasGradient object, can't get the gradient information"); - QGradient gradient = *(style->d()->brush.gradient()); + QGradient gradient = *(style->d()->brush->gradient()); qreal pos = ctx->args()[0].toNumber(); QColor color; @@ -3371,7 +3388,7 @@ QV4::ReturnedValue QQuickContext2DStyle::gradient_proto_addColorStop(QV4::CallCo } else { V4THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "CanvasGradient: parameter color is not a valid color string"); } - style->d()->brush = gradient; + *style->d()->brush = gradient; } return ctx->thisObject().asReturnedValue(); @@ -3980,10 +3997,12 @@ public: ~QQuickContext2DThreadCleanup() { +#ifndef QT_NO_OPENGL context->makeCurrent(surface); delete texture; context->doneCurrent(); delete context; +#endif surface->deleteLater(); } @@ -4019,6 +4038,7 @@ QQuickContext2D::~QQuickContext2D() delete m_buffer; if (m_renderTarget == QQuickCanvasItem::FramebufferObject) { +#ifndef QT_NO_OPENGL if (m_renderStrategy == QQuickCanvasItem::Immediate && m_glContext) { Q_ASSERT(QThread::currentThread() == m_glContext->thread()); m_glContext->makeCurrent(m_surface.data()); @@ -4039,6 +4059,7 @@ QQuickContext2D::~QQuickContext2D() m_texture->deleteLater(); } } +#endif } else { // Image based does not have GL resources, but must still be deleted // on its designated thread after it has completed whatever it might @@ -4064,8 +4085,6 @@ void QQuickContext2D::init(QQuickCanvasItem *canvasItem, const QVariantMap &args m_canvas = canvasItem; m_renderTarget = canvasItem->renderTarget(); - - QQuickWindow *window = canvasItem->window(); m_renderStrategy = canvasItem->renderStrategy(); #ifdef Q_OS_WIN @@ -4084,12 +4103,24 @@ void QQuickContext2D::init(QQuickCanvasItem *canvasItem, const QVariantMap &args m_renderTarget = QQuickCanvasItem::Image; } + // Disable Framebuffer Object based rendering when not running with OpenGL + if (m_renderTarget == QQuickCanvasItem::FramebufferObject) { + QSGRendererInterface *rif = canvasItem->window()->rendererInterface(); + if (rif && rif->graphicsApi() != QSGRendererInterface::OpenGL) + m_renderTarget = QQuickCanvasItem::Image; + } + switch (m_renderTarget) { case QQuickCanvasItem::Image: m_texture = new QQuickContext2DImageTexture; break; case QQuickCanvasItem::FramebufferObject: +#ifndef QT_NO_OPENGL m_texture = new QQuickContext2DFBOTexture; +#else + // It shouldn't be possible to use a FramebufferObject without OpenGL + m_texture = nullptr; +#endif break; } @@ -4103,18 +4134,27 @@ void QQuickContext2D::init(QQuickCanvasItem *canvasItem, const QVariantMap &args m_thread = QThread::currentThread(); QThread *renderThread = m_thread; - QThread *sceneGraphThread = window->openglContext() ? window->openglContext()->thread() : 0; +#ifndef QT_NO_OPENGL + QQuickWindow *window = canvasItem->window(); + QQuickWindowPrivate *wd = QQuickWindowPrivate::get(window); + QThread *sceneGraphThread = wd->context->thread(); if (m_renderStrategy == QQuickCanvasItem::Threaded) renderThread = QQuickContext2DRenderThread::instance(qmlEngine(canvasItem)); else if (m_renderStrategy == QQuickCanvasItem::Cooperative) renderThread = sceneGraphThread; +#else + if (m_renderStrategy == QQuickCanvasItem::Threaded) + renderThread = QQuickContext2DRenderThread::instance(qmlEngine(canvasItem)); +#endif + if (renderThread && renderThread != QThread::currentThread()) m_texture->moveToThread(renderThread); - +#ifndef QT_NO_OPENGL if (m_renderTarget == QQuickCanvasItem::FramebufferObject && renderThread != sceneGraphThread) { - QOpenGLContext *cc = QQuickWindowPrivate::get(window)->context->openglContext(); + auto openglRenderContext = static_cast<const QSGDefaultRenderContext *>(QQuickWindowPrivate::get(window)->context); + QOpenGLContext *cc = openglRenderContext->openglContext(); m_surface.reset(new QOffscreenSurface); m_surface->setFormat(window->format()); m_surface->create(); @@ -4125,7 +4165,7 @@ void QQuickContext2D::init(QQuickCanvasItem *canvasItem, const QVariantMap &args m_glContext->moveToThread(renderThread); m_texture->initializeOpenGL(m_glContext, m_surface.data()); } - +#endif connect(m_texture, SIGNAL(textureChanged()), SIGNAL(textureChanged())); reset(); @@ -4172,6 +4212,7 @@ QImage QQuickContext2D::toImage(const QRectF& bounds) flush(); m_texture->grabImage(bounds); } else { +#ifndef QT_NO_OPENGL QQuickWindow *window = m_canvas->window(); QOpenGLContext *ctx = window ? window->openglContext() : 0; if (ctx && ctx->isValid()) { @@ -4187,6 +4228,10 @@ QImage QQuickContext2D::toImage(const QRectF& bounds) qWarning() << "Cannot read pixels from canvas before opengl context is valid"; return QImage(); } +#else + flush(); + m_texture->grabImage(bounds); +#endif } } else if (m_renderStrategy == QQuickCanvasItem::Cooperative) { qWarning() << "Pixel readback is not supported in Cooperative mode, please try Threaded or Immediate mode"; |