/*------------------------------------------------------------------------------ | OMX_CameraSurfaceElement::updatePaintNode +-----------------------------------------------------------------------------*/ QSGNode* OMX_CameraSurfaceElement::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*) { QSGGeometryNode* node = 0; QSGGeometry* geometry = 0; if (!oldNode) { // Create the node. node = new QSGGeometryNode; geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); geometry->setDrawingMode(GL_TRIANGLE_STRIP); node->setGeometry(geometry); node->setFlag(QSGNode::OwnsGeometry); // TODO: Who is freeing this? // TODO: I cannot know the texture size here. QSGOpaqueTextureMaterial* material = new QSGOpaqueTextureMaterial; m_sgtexture = new OMX_SGTexture(0, QSize(640, 480)); material->setTexture(m_sgtexture); node->setMaterial(material); node->setFlag(QSGNode::OwnsMaterial); QtConcurrent::run(this, &OMX_CameraSurfaceElement::videoAcquire); } else { node = static_cast<QSGGeometryNode*>(oldNode); geometry = node->geometry(); geometry->allocate(4); // Update texture in the node if needed. QSGOpaqueTextureMaterial* material = (QSGOpaqueTextureMaterial*)node->material(); QElapsedTimer timer; timer.start(); QSGTexture* texture = window()->createTextureFromImage(m_frame); LOG_VERBOSE(LOG_TAG, "Timer tex: %lld.", timer.elapsed()); material->setTexture(texture); m_semAcquire.release(); #if 0 if (m_texture != (GLuint)material->texture()->textureId()) { // TODO: Does setTextureId frees the prev texture? // TODO: I should the given the texture size. LOG_ERROR(LOG_TAG, "Updating texture to %u!", m_texture); material = new QSGOpaqueTextureMaterial; m_sgtexture->setTexture(m_texture, QSize(1920, 1080)); } #endif } // Create the vertices and map to texture. QRectF bounds = boundingRect(); QSGGeometry::TexturedPoint2D* vertices = geometry->vertexDataAsTexturedPoint2D(); vertices[0].set(bounds.x(), bounds.y() + bounds.height(), 0.0f, 0.0f); vertices[1].set(bounds.x() + bounds.width(), bounds.y() + bounds.height(), 1.0f, 0.0f); vertices[2].set(bounds.x(), bounds.y(), 0.0f, 1.0f); vertices[3].set(bounds.x() + bounds.width(), bounds.y(), 1.0f, 1.0f); return node; }
void ShaderEffectItem::updateGeometry() { QRectF srcRect(0, 1, 1, -1); if (m_mirrored) srcRect = QRectF(0, 0, 1, 1); QRectF dstRect = QRectF(0,0, width(), height()); int vmesh = m_meshResolution.height(); int hmesh = m_meshResolution.width(); QSGGeometry *g = &m_geometry; if (vmesh == 1 && hmesh == 1) { if (g->vertexCount() != 4) g->allocate(4); QSGGeometry::updateTexturedRectGeometry(g, dstRect, srcRect); return; } g->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2)); QSGGeometry::TexturedPoint2D *vdata = g->vertexDataAsTexturedPoint2D(); for (int iy = 0; iy <= vmesh; ++iy) { float fy = iy / float(vmesh); float y = float(dstRect.top()) + fy * float(dstRect.height()); float ty = float(srcRect.top()) + fy * float(srcRect.height()); for (int ix = 0; ix <= hmesh; ++ix) { float fx = ix / float(hmesh); vdata->x = float(dstRect.left()) + fx * float(dstRect.width()); vdata->y = y; vdata->tx = float(srcRect.left()) + fx * float(srcRect.width()); vdata->ty = ty; ++vdata; } } quint16 *indices = (quint16 *)g->indexDataAsUShort(); int i = 0; for (int iy = 0; iy < vmesh; ++iy) { *(indices++) = i + hmesh + 1; for (int ix = 0; ix <= hmesh; ++ix, ++i) { *(indices++) = i + hmesh + 1; *(indices++) = i; } *(indices++) = i - 1; } }
void QQuickAndroid9PatchNode::initialize(QSGTexture *texture, const QRectF &bounds, const QSize &sourceSize, const QQuickAndroid9PatchDivs &xDivs, const QQuickAndroid9PatchDivs &yDivs) { delete m_material.texture(); m_material.setTexture(texture); const int xlen = xDivs.data.size(); const int ylen = yDivs.data.size(); if (xlen > 0 && ylen > 0) { const int quads = (xlen - 1) * (ylen - 1); static const int verticesPerQuad = 6; m_geometry.allocate(xlen * ylen, verticesPerQuad * quads); QSGGeometry::TexturedPoint2D *vertices = m_geometry.vertexDataAsTexturedPoint2D(); QVector<qreal> xCoords = xDivs.coordsForSize(bounds.width()); QVector<qreal> yCoords = yDivs.coordsForSize(bounds.height()); for (int y = 0; y < ylen; ++y) { for (int x = 0; x < xlen; ++x, ++vertices) vertices->set(xCoords[x], yCoords[y], xDivs.data[x] / sourceSize.width(), yDivs.data[y] / sourceSize.height()); } quint16 *indices = m_geometry.indexDataAsUShort(); int n = quads; for (int q = 0; n--; ++q) { if ((q + 1) % xlen == 0) // next row ++q; // Bottom-left half quad triangle indices[0] = q; indices[1] = q + xlen; indices[2] = q + xlen + 1; // Top-right half quad triangle indices[3] = q; indices[4] = q + xlen + 1; indices[5] = q + 1; indices += verticesPerQuad; } } markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial); }
void VideoNode::updateGeometry(const PaintAreas & areas) { QSGGeometry *g = geometry(); if (m_materialType == MaterialTypeVideo) { if (!m_validGeometry) g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D(); // Set geometry first setGeom(v + 0, areas.videoArea.topLeft()); setGeom(v + 1, areas.videoArea.bottomLeft()); setGeom(v + 2, areas.videoArea.topRight()); setGeom(v + 3, areas.videoArea.bottomRight()); // and then texture coordinates setTex(v + 0, areas.sourceRect.topLeft()); setTex(v + 1, areas.sourceRect.bottomLeft()); setTex(v + 2, areas.sourceRect.topRight()); setTex(v + 3, areas.sourceRect.bottomRight()); } else { if (!m_validGeometry) g = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4); QSGGeometry::Point2D *v = g->vertexDataAsPoint2D(); setGeom(v + 0, areas.videoArea.topLeft()); setGeom(v + 1, areas.videoArea.bottomLeft()); setGeom(v + 2, areas.videoArea.topRight()); setGeom(v + 3, areas.videoArea.bottomRight()); } if (!m_validGeometry) { setGeometry(g); m_validGeometry = true; } markDirty(DirtyGeometry); }
QSGNode* OMX_MediaProcessorElement::updatePaintNode(QSGNode*, UpdatePaintNodeData*) { if (!m_texProvider) { m_texProvider = new OMX_TextureProviderQQuickItem(this); m_mediaProc = new OMX_MediaProcessor(m_texProvider); connect(m_mediaProc, SIGNAL(playbackCompleted()), this, SIGNAL(playbackCompleted())); connect(m_mediaProc, SIGNAL(playbackStarted()), this, SIGNAL(playbackStarted())); // Open if filepath is set. // TODO: Handle errors. if (!m_source.isNull()) { //if (QFile(m_source).exists()) { if (openMedia(m_source)) m_mediaProc->play(); //} //else { LOG_WARNING(LOG_TAG, "File does not exist."); //} } } return NULL; #if 0 QSGGeometryNode* node = 0; QSGGeometry* geometry = 0; if (!oldNode) { // Create the node. node = new QSGGeometryNode; geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); geometry->setDrawingMode(GL_TRIANGLE_STRIP); node->setGeometry(geometry); node->setFlag(QSGNode::OwnsGeometry); // TODO: Who is freeing this? // TODO: I cannot know the texture size here. QSGOpaqueTextureMaterial* material = new QSGOpaqueTextureMaterial; m_sgtexture = new OMX_SGTexture(m_texture, QSize(1920, 1080)); material->setTexture(m_sgtexture); node->setMaterial(material); node->setFlag(QSGNode::OwnsMaterial); #ifdef ENABLE_VIDEO_PROCESSOR QPlatformNativeInterface* nativeInterface = QGuiApplicationPrivate::platformIntegration()->nativeInterface(); Q_ASSERT(nativeInterface); EGLDisplay eglDisplay = nativeInterface->nativeResourceForIntegration("egldisplay"); EGLContext eglContext = nativeInterface->nativeResourceForContext( "eglcontext", QOpenGLContext::currentContext() ); #endif // Provider MUST be built in this thread. m_provider = new OMX_TextureProviderQQuickItem(this); #ifdef ENABLE_VIDEO_PROCESSOR m_videoProc = new OMX_VideoProcessor(eglDisplay, eglContext, m_provider); connect(m_videoProc, SIGNAL(textureReady(uint)), this, SLOT(onTextureChanged(uint))); if (!m_source.isNull()) m_videoProc->setVideoPath(m_source); if (m_playScheduled) { m_timer->start(30); m_videoProc->play(); } #elif ENABLE_MEDIA_PROCESSOR LOG_VERBOSE(LOG_TAG, "Starting video using media processor..."); m_mediaProc = new OMX_MediaProcessor(m_provider); m_mediaProc->setFilename("/home/pi/usb/Cars2.mkv", m_texture); //if (m_playScheduled) { m_timer->start(40); m_mediaProc->play(); //} #else LOG_VERBOSE(LOG_TAG, "Starting video..."); QtConcurrent::run(&startVideo, m_provider, this); m_timer->start(30); #endif } else { node = static_cast<QSGGeometryNode*>(oldNode); geometry = node->geometry(); geometry->allocate(4); // Update texture in the node if needed. QSGOpaqueTextureMaterial* material = (QSGOpaqueTextureMaterial*)node->material(); if (m_texture != (GLuint)material->texture()->textureId()) { // TODO: Does setTextureId frees the prev texture? // TODO: I should the given the texture size. LOG_ERROR(LOG_TAG, "Updating texture to %u!", m_texture); material = new QSGOpaqueTextureMaterial; m_sgtexture->setTexture(m_texture, QSize(1920, 1080)); } } // Create the vertices and map to texture. QRectF bounds = boundingRect(); QSGGeometry::TexturedPoint2D* vertices = geometry->vertexDataAsTexturedPoint2D(); vertices[0].set(bounds.x(), bounds.y() + bounds.height(), 0.0f, 0.0f); vertices[1].set(bounds.x() + bounds.width(), bounds.y() + bounds.height(), 1.0f, 0.0f); vertices[2].set(bounds.x(), bounds.y(), 0.0f, 1.0f); vertices[3].set(bounds.x() + bounds.width(), bounds.y(), 1.0f, 1.0f); return node; #endif }
/* Update the vertices and texture coordinates. Orientation must be in {0,90,180,270} */ void QSGVideoNode::setTexturedRectGeometry(const QRectF &rect, const QRectF &textureRect, int orientation) { if (rect == m_rect && textureRect == m_textureRect && orientation == m_orientation) return; m_rect = rect; m_textureRect = textureRect; m_orientation = orientation; QSGGeometry *g = geometry(); if (g == 0) g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4); QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D(); // Set geometry first qSetGeom(v + 0, rect.topLeft()); qSetGeom(v + 1, rect.bottomLeft()); qSetGeom(v + 2, rect.topRight()); qSetGeom(v + 3, rect.bottomRight()); // and then texture coordinates switch (orientation) { default: // tl, bl, tr, br qSetTex(v + 0, textureRect.topLeft()); qSetTex(v + 1, textureRect.bottomLeft()); qSetTex(v + 2, textureRect.topRight()); qSetTex(v + 3, textureRect.bottomRight()); break; case 90: // tr, tl, br, bl qSetTex(v + 0, textureRect.topRight()); qSetTex(v + 1, textureRect.topLeft()); qSetTex(v + 2, textureRect.bottomRight()); qSetTex(v + 3, textureRect.bottomLeft()); break; case 180: // br, tr, bl, tl qSetTex(v + 0, textureRect.bottomRight()); qSetTex(v + 1, textureRect.topRight()); qSetTex(v + 2, textureRect.bottomLeft()); qSetTex(v + 3, textureRect.topLeft()); break; case 270: // bl, br, tl, tr qSetTex(v + 0, textureRect.bottomLeft()); qSetTex(v + 1, textureRect.bottomRight()); qSetTex(v + 2, textureRect.topLeft()); qSetTex(v + 3, textureRect.topRight()); break; } if (!geometry()) setGeometry(g); markDirty(DirtyGeometry); }