X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Frender%2Fcommon%2Frender-manager.cpp;h=3f3d3194081c5ade384ccde7672d7cdc6c727896;hb=5b712a1b5fda4ee48e03f5ce462dd4c506fec014;hp=2eec97ac34a8f2032a99c32b5cc27ba42429aaea;hpb=0b21b1cdb38fd603e92722185ba9bfc180741f21;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/render/common/render-manager.cpp b/dali/internal/render/common/render-manager.cpp old mode 100644 new mode 100755 index 2eec97a..3f3d319 --- a/dali/internal/render/common/render-manager.cpp +++ b/dali/internal/render/common/render-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,28 +22,14 @@ #include // INTERNAL INCLUDES -#include -#include -#include -#include #include -#include #include #include -#include +#include #include #include #include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include #include namespace Dali @@ -71,16 +57,15 @@ struct RenderManager::Impl Integration::GlSyncAbstraction& glSyncAbstraction, Integration::GlContextHelperAbstraction& glContextHelperAbstraction, Integration::DepthBufferAvailable depthBufferAvailableParam, - Integration::StencilBufferAvailable stencilBufferAvailableParam ) - : context( glAbstraction ), + Integration::StencilBufferAvailable stencilBufferAvailableParam, + Integration::PartialUpdateAvailable partialUpdateAvailableParam ) + : context( glAbstraction, &sceneContextContainer ), currentContext( &context ), glAbstraction( glAbstraction ), glSyncAbstraction( glSyncAbstraction ), glContextHelperAbstraction( glContextHelperAbstraction ), renderQueue(), - instructions(), renderAlgorithms(), - backgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ), frameCount( 0u ), renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ), defaultSurfaceRect(), @@ -91,7 +76,9 @@ struct RenderManager::Impl lastFrameWasRendered( false ), programController( glAbstraction ), depthBufferAvailable( depthBufferAvailableParam ), - stencilBufferAvailable( stencilBufferAvailableParam ) + stencilBufferAvailable( stencilBufferAvailableParam ), + partialUpdateAvailable( partialUpdateAvailableParam ), + defaultSurfaceOrientation(0) { // Create thread pool with just one thread ( there may be a need to create more threads in the future ). threadPool = std::unique_ptr( new Dali::ThreadPool() ); @@ -114,15 +101,31 @@ struct RenderManager::Impl mRenderTrackers.EraseObject( renderTracker ); } - Context* CreateSurfaceContext() + Context* CreateSceneContext() { - surfaceContextContainer.PushBack( new Context( glAbstraction ) ); - return surfaceContextContainer[ surfaceContextContainer.Count() - 1 ]; + Context* context = new Context( glAbstraction ); + sceneContextContainer.PushBack( context ); + return context; } - void DestroySurfaceContext( Context* surfaceContext ) + void DestroySceneContext( Context* sceneContext ) { - surfaceContextContainer.EraseObject( surfaceContext ); + auto iter = std::find( sceneContextContainer.Begin(), sceneContextContainer.End(), sceneContext ); + if( iter != sceneContextContainer.End() ) + { + ( *iter )->GlContextDestroyed(); + sceneContextContainer.Erase( iter ); + } + } + + Context* ReplaceSceneContext( Context* oldSceneContext ) + { + Context* newContext = new Context( glAbstraction ); + + oldSceneContext->GlContextDestroyed(); + + std::replace( sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext ); + return newContext; } void UpdateTrackers() @@ -137,18 +140,15 @@ struct RenderManager::Impl // programs are owned by context at the moment. Context context; ///< Holds the GL state of the share resource context Context* currentContext; ///< Holds the GL state of the current context for rendering - OwnerContainer< Context* > surfaceContextContainer; ///< List of owned contexts holding the GL state per surface + OwnerContainer< Context* > sceneContextContainer; ///< List of owned contexts holding the GL state per scene Integration::GlAbstraction& glAbstraction; ///< GL abstraction Integration::GlSyncAbstraction& glSyncAbstraction; ///< GL sync abstraction Integration::GlContextHelperAbstraction& glContextHelperAbstraction; ///< GL context helper abstraction RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread. - // Render instructions describe what should be rendered during RenderManager::Render() - // Owned by RenderManager. Update manager updates instructions for the next frame while we render the current one - RenderInstructionContainer instructions; - Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction + std::vector< SceneGraph::Scene* > sceneContainer; ///< List of pointers to the scene graph objects of the scenes - Vector4 backgroundColor; ///< The glClear color used at the beginning of each frame. + Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction uint32_t frameCount; ///< The current frame count BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer @@ -159,7 +159,7 @@ struct RenderManager::Impl OwnerContainer< Render::Sampler* > samplerContainer; ///< List of owned samplers OwnerContainer< Render::Texture* > textureContainer; ///< List of owned textures OwnerContainer< Render::FrameBuffer* > frameBufferContainer; ///< List of owned framebuffers - OwnerContainer< Render::PropertyBuffer* > propertyBufferContainer; ///< List of owned property buffers + OwnerContainer< Render::VertexBuffer* > vertexBufferContainer; ///< List of owned vertex buffers OwnerContainer< Render::Geometry* > geometryContainer; ///< List of owned Geometries bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions @@ -170,29 +170,35 @@ struct RenderManager::Impl Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available + Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available std::unique_ptr threadPool; ///< The thread pool Vector boundTextures; ///< The textures bound for rendering Vector textureDependencyList; ///< The dependency list of binded textures + + int defaultSurfaceOrientation; ///< defaultSurfaceOrientation for the default surface we are rendering to + }; RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction, Integration::GlSyncAbstraction& glSyncAbstraction, Integration::GlContextHelperAbstraction& glContextHelperAbstraction, Integration::DepthBufferAvailable depthBufferAvailable, - Integration::StencilBufferAvailable stencilBufferAvailable ) + Integration::StencilBufferAvailable stencilBufferAvailable, + Integration::PartialUpdateAvailable partialUpdateAvailable ) { RenderManager* manager = new RenderManager; manager->mImpl = new Impl( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, - stencilBufferAvailable ); + stencilBufferAvailable, + partialUpdateAvailable ); return manager; } RenderManager::RenderManager() -: mImpl(NULL) +: mImpl(nullptr) { } @@ -237,6 +243,12 @@ void RenderManager::ContextDestroyed() { renderer->GlContextDestroyed(); } + + // inform context + for( auto&& context : mImpl->sceneContextContainer ) + { + context->GlContextDestroyed(); + } } void RenderManager::SetShaderSaver( ShaderSaver& upstream ) @@ -244,19 +256,14 @@ void RenderManager::SetShaderSaver( ShaderSaver& upstream ) mImpl->programController.SetShaderSaver( upstream ); } -RenderInstructionContainer& RenderManager::GetRenderInstructionContainer() +void RenderManager::SetDefaultSurfaceRect(const Rect& rect) { - return mImpl->instructions; + mImpl->defaultSurfaceRect = rect; } -void RenderManager::SetBackgroundColor( const Vector4& color ) +void RenderManager::SetDefaultSurfaceOrientation(int orientation) { - mImpl->backgroundColor = color; -} - -void RenderManager::SetDefaultSurfaceRect(const Rect& rect) -{ - mImpl->defaultSurfaceRect = rect; + mImpl->defaultSurfaceOrientation = orientation; } void RenderManager::AddRenderer( OwnerPointer< Render::Renderer >& renderer ) @@ -331,14 +338,7 @@ void RenderManager::AddFrameBuffer( OwnerPointer< Render::FrameBuffer >& frameBu { Render::FrameBuffer* frameBufferPtr = frameBuffer.Release(); mImpl->frameBufferContainer.PushBack( frameBufferPtr ); - if ( frameBufferPtr->IsSurfaceBacked() ) - { - frameBufferPtr->Initialize( *mImpl->CreateSurfaceContext() ); - } - else - { - frameBufferPtr->Initialize( mImpl->context ); - } + frameBufferPtr->Initialize( mImpl->context ); } void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer ) @@ -351,47 +351,68 @@ void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer ) if ( iter == frameBuffer ) { frameBuffer->Destroy( mImpl->context ); - - if ( frameBuffer->IsSurfaceBacked() ) - { - auto surfaceFrameBuffer = static_cast( frameBuffer ); - mImpl->DestroySurfaceContext( surfaceFrameBuffer->GetContext() ); - } - mImpl->frameBufferContainer.Erase( &iter ); // frameBuffer found; now destroy it break; } } } +void RenderManager::InitializeScene( SceneGraph::Scene* scene ) +{ + scene->Initialize( *mImpl->CreateSceneContext() ); + mImpl->sceneContainer.push_back( scene ); +} -void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer ) +void RenderManager::UninitializeScene( SceneGraph::Scene* scene ) { - if ( !frameBuffer->IsSurfaceBacked() ) + mImpl->DestroySceneContext( scene->GetContext() ); + + auto iter = std::find( mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene ); + if( iter != mImpl->sceneContainer.end() ) { - auto textureFrameBuffer = static_cast( frameBuffer ); - textureFrameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer ); + mImpl->sceneContainer.erase( iter ); } } -void RenderManager::AddPropertyBuffer( OwnerPointer< Render::PropertyBuffer >& propertyBuffer ) +void RenderManager::SurfaceReplaced( SceneGraph::Scene* scene ) +{ + Context* newContext = mImpl->ReplaceSceneContext( scene->GetContext() ); + scene->Initialize( *newContext ); +} + +void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer ) { - mImpl->propertyBufferContainer.PushBack( propertyBuffer.Release() ); + frameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer ); } -void RenderManager::RemovePropertyBuffer( Render::PropertyBuffer* propertyBuffer ) +void RenderManager::AttachDepthTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel ) { - mImpl->propertyBufferContainer.EraseObject( propertyBuffer ); + frameBuffer->AttachDepthTexture( mImpl->context, texture, mipmapLevel ); } -void RenderManager::SetPropertyBufferFormat( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Render::PropertyBuffer::Format>& format ) +void RenderManager::AttachDepthStencilTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel ) { - propertyBuffer->SetFormat( format.Release() ); + frameBuffer->AttachDepthStencilTexture( mImpl->context, texture, mipmapLevel ); } -void RenderManager::SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Vector >& data, uint32_t size ) +void RenderManager::AddVertexBuffer( OwnerPointer< Render::VertexBuffer >& vertexBuffer ) { - propertyBuffer->SetData( data.Release(), size ); + mImpl->vertexBufferContainer.PushBack( vertexBuffer.Release() ); +} + +void RenderManager::RemoveVertexBuffer( Render::VertexBuffer* vertexBuffer ) +{ + mImpl->vertexBufferContainer.EraseObject( vertexBuffer ); +} + +void RenderManager::SetVertexBufferFormat( Render::VertexBuffer* vertexBuffer, OwnerPointer< Render::VertexBuffer::Format>& format ) +{ + vertexBuffer->SetFormat( format.Release() ); +} + +void RenderManager::SetVertexBufferData( Render::VertexBuffer* vertexBuffer, OwnerPointer< Vector >& data, uint32_t size ) +{ + vertexBuffer->SetData( data.Release(), size ); } void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector& indices ) @@ -409,7 +430,7 @@ void RenderManager::RemoveGeometry( Render::Geometry* geometry ) mImpl->geometryContainer.EraseObject( geometry ); } -void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer ) +void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer ) { DALI_ASSERT_DEBUG( NULL != geometry ); @@ -418,13 +439,13 @@ void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::Prop { if ( iter == geometry ) { - iter->AddPropertyBuffer( propertyBuffer ); + iter->AddVertexBuffer( vertexBuffer ); break; } } } -void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer ) +void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer ) { DALI_ASSERT_DEBUG( NULL != geometry ); @@ -433,7 +454,7 @@ void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::Prop { if ( iter == geometry ) { - iter->RemovePropertyBuffer( propertyBuffer ); + iter->RemoveVertexBuffer( vertexBuffer ); break; } } @@ -459,7 +480,7 @@ ProgramCache* RenderManager::GetProgramCache() return &(mImpl->programController); } -void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) +void RenderManager::PreRender( Integration::RenderStatus& status, bool forceClear, bool uploadOnly ) { DALI_PRINT_RENDER_START( mImpl->renderBufferIndex ); @@ -472,7 +493,12 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) // Process messages queued during previous update mImpl->renderQueue.ProcessMessages( mImpl->renderBufferIndex ); - const uint32_t count = mImpl->instructions.Count( mImpl->renderBufferIndex ); + uint32_t count = 0u; + for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i ) + { + count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex ); + } + const bool haveInstructions = count > 0u; DALI_LOG_INFO( gLogFilter, Debug::General, @@ -486,9 +512,6 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) { DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" ); - // Mark that we will require a post-render step to be performed (includes swap-buffers). - status.SetNeedsPostRender( true ); - // Switch to the shared context if ( mImpl->currentContext != &mImpl->context ) { @@ -504,220 +527,400 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) } // Upload the geometries - for( uint32_t i = 0; i < count; ++i ) + for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i ) { - RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i ); - - const Matrix* viewMatrix = instruction.GetViewMatrix( mImpl->renderBufferIndex ); - const Matrix* projectionMatrix = instruction.GetProjectionMatrix( mImpl->renderBufferIndex ); + RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions(); + for ( uint32_t j = 0; j < instructions.Count( mImpl->renderBufferIndex ); ++j ) + { + RenderInstruction& instruction = instructions.At( mImpl->renderBufferIndex, j ); - DALI_ASSERT_DEBUG( viewMatrix ); - DALI_ASSERT_DEBUG( projectionMatrix ); + const Matrix* viewMatrix = instruction.GetViewMatrix( mImpl->renderBufferIndex ); + const Matrix* projectionMatrix = instruction.GetProjectionMatrix( mImpl->renderBufferIndex ); - if( viewMatrix && projectionMatrix ) - { - const RenderListContainer::SizeType renderListCount = instruction.RenderListCount(); + DALI_ASSERT_DEBUG( viewMatrix ); + DALI_ASSERT_DEBUG( projectionMatrix ); - // Iterate through each render list. - for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index ) + if( viewMatrix && projectionMatrix ) { - const RenderList* renderList = instruction.GetRenderList( index ); + const RenderListContainer::SizeType renderListCount = instruction.RenderListCount(); - if( renderList && !renderList->IsEmpty() ) + // Iterate through each render list. + for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index ) { - const std::size_t itemCount = renderList->Count(); - for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex ) + const RenderList* renderList = instruction.GetRenderList( index ); + + if( renderList && !renderList->IsEmpty() ) { - const RenderItem& item = renderList->GetItem( itemIndex ); - if( DALI_LIKELY( item.mRenderer ) ) + const std::size_t itemCount = renderList->Count(); + for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex ) { - item.mRenderer->Upload( *mImpl->currentContext ); + const RenderItem& item = renderList->GetItem( itemIndex ); + if( DALI_LIKELY( item.mRenderer ) ) + { + item.mRenderer->Upload( *mImpl->currentContext ); + } } } } } } } + } +} - for( uint32_t i = 0; i < count; ++i ) - { - RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i ); - - DoRender( instruction ); - } +void RenderManager::PreRender( Integration::Scene& scene, std::vector>& damagedRects ) +{ + if (mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE) + { + return; + } - if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + class DamagedRectsCleaner + { + public: + DamagedRectsCleaner(std::vector>& damagedRects) + : mDamagedRects(damagedRects), + mCleanOnReturn(true) { - mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); } - GLenum attachments[] = { GL_DEPTH, GL_STENCIL }; - mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); - for ( auto&& context : mImpl->surfaceContextContainer ) + void SetCleanOnReturn(bool cleanOnReturn) { - context->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); + mCleanOnReturn = cleanOnReturn; } - //Notify RenderGeometries that rendering has finished - for ( auto&& iter : mImpl->geometryContainer ) + ~DamagedRectsCleaner() { - iter->OnRenderFinished(); + if (mCleanOnReturn) + { + mDamagedRects.clear(); + } } - } - else - { - DALI_LOG_RELEASE_INFO( "RenderManager::Render: Skip rendering [%d, %d, %d]\n", haveInstructions, mImpl->lastFrameWasRendered, forceClear ); - } - mImpl->UpdateTrackers(); + private: + std::vector>& mDamagedRects; + bool mCleanOnReturn; + }; - // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame. - mImpl->lastFrameWasRendered = haveInstructions; + Rect surfaceRect = Rect(0, 0, static_cast( scene.GetSize().width ), static_cast( scene.GetSize().height )); - /** - * The rendering has finished; swap to the next buffer. - * Ideally the update has just finished using this buffer; otherwise the render thread - * should block until the update has finished. - */ - mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1; + // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions. + DamagedRectsCleaner damagedRectCleaner(damagedRects); - DALI_PRINT_RENDER_END(); -} -void RenderManager::DoRender( RenderInstruction& instruction ) -{ - Rect viewportRect; - Vector4 clearColor; - if ( instruction.mIsClearColorSet ) - { - clearColor = instruction.mClearColor; - } - else + Internal::Scene& sceneInternal = GetImplementation(scene); + SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject(); + + // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number. + // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end()); + std::vector& itemsDirtyRects = sceneInternal.GetItemsDirtyRects(); + for (DirtyRect& dirtyRect : itemsDirtyRects) { - clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR; + dirtyRect.visited = false; } - Rect surfaceRect = mImpl->defaultSurfaceRect; - Vector4 backgroundColor = mImpl->backgroundColor; - Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable; - Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable; - - Render::SurfaceFrameBuffer* surfaceFrameBuffer = nullptr; - if ( instruction.mFrameBuffer != 0 ) + uint32_t count = sceneObject->GetRenderInstructions().Count( mImpl->renderBufferIndex ); + for (uint32_t i = 0; i < count; ++i) { - if ( instruction.mFrameBuffer->IsSurfaceBacked() ) + RenderInstruction& instruction = sceneObject->GetRenderInstructions().At( mImpl->renderBufferIndex, i ); + + if (instruction.mFrameBuffer) { - surfaceFrameBuffer = static_cast( instruction.mFrameBuffer ); + return; // TODO: reset, we don't deal with render tasks with framebuffers (for now) + } - if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + const Camera* camera = instruction.GetCamera(); + if (camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION) + { + const Node* node = instruction.GetCamera()->GetNode(); + if (node) { - Context* surfaceContext = surfaceFrameBuffer->GetContext(); - if ( mImpl->currentContext != surfaceContext ) + Vector3 position; + Vector3 scale; + Quaternion orientation; + node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale); + + Vector3 orientationAxis; + Radian orientationAngle; + orientation.ToAxisAngle( orientationAxis, orientationAngle ); + + if (position.x > Math::MACHINE_EPSILON_10000 || + position.y > Math::MACHINE_EPSILON_10000 || + orientationAxis != Vector3(0.0f, 1.0f, 0.0f) || + orientationAngle != ANGLE_180 || + scale != Vector3(1.0f, 1.0f, 1.0f)) { - // Switch the correct context if rendering to a surface - mImpl->currentContext = surfaceContext; - surfaceFrameBuffer->MakeContextCurrent(); - - // Clear the current cached program when the context is switched - mImpl->programController.ClearCurrentProgram(); + return; } } - - surfaceRect = Rect( 0, 0, static_cast( surfaceFrameBuffer->GetWidth() ), static_cast( surfaceFrameBuffer->GetHeight() ) ); - backgroundColor = surfaceFrameBuffer->GetBackgroundColor(); } else { - // Switch to shared context for off-screen buffer - mImpl->currentContext = &mImpl->context; + return; + } - if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + Rect viewportRect; + if (instruction.mIsViewportSet) + { + const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y; + viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height); + if (viewportRect.IsEmpty() || !viewportRect.IsValid()) { - mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + return; // just skip funny use cases for now, empty viewport means it is set somewhere else } } - } + else + { + viewportRect = surfaceRect; + } - DALI_ASSERT_DEBUG( mImpl->currentContext->IsGlContextCreated() ); + const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex); + const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex); + if (viewMatrix && projectionMatrix) + { + const RenderListContainer::SizeType count = instruction.RenderListCount(); + for (RenderListContainer::SizeType index = 0u; index < count; ++index) + { + const RenderList* renderList = instruction.GetRenderList( index ); + if (renderList && !renderList->IsEmpty()) + { + const std::size_t count = renderList->Count(); + for (uint32_t index = 0u; index < count; ++index) + { + RenderItem& item = renderList->GetItem( index ); + // If the item does 3D transformation, do early exit and clean the damaged rect array + if (item.mUpdateSize == Vector3::ZERO) + { + return; + } - // reset the program matrices for all programs once per frame - // this ensures we will set view and projection matrix once per program per camera - mImpl->programController.ResetProgramMatrices(); + Rect rect; + DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect); + // If the item refers to updated node or renderer. + if (item.mIsUpdated || + (item.mNode && + (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode))))) + { + item.mIsUpdated = false; + item.mNode->SetUpdated(false); - if( instruction.mFrameBuffer ) - { - instruction.mFrameBuffer->Bind( *mImpl->currentContext ); + rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height); + if (rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty()) + { + const int left = rect.x; + const int top = rect.y; + const int right = rect.x + rect.width; + const int bottom = rect.y + rect.height; + rect.x = (left / 16) * 16; + rect.y = (top / 16) * 16; + rect.width = ((right + 16) / 16) * 16 - rect.x; + rect.height = ((bottom + 16) / 16) * 16 - rect.y; + + // Found valid dirty rect. + // 1. Insert it in the sorted array of the dirty rects. + // 2. Mark the related dirty rects as visited so they will not be removed below. + // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1). + dirtyRect.rect = rect; + auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); + dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect); + + int c = 1; + while (++dirtyRectPos != itemsDirtyRects.end()) + { + if (dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer) + { + break; + } + + dirtyRectPos->visited = true; + Rect& dirtRect = dirtyRectPos->rect; + rect.Merge(dirtRect); + + c++; + if (c > 3) // no more then 3 previous rects + { + itemsDirtyRects.erase(dirtyRectPos); + break; + } + } + + damagedRects.push_back(rect); + } + } + else + { + // 1. The item is not dirty, the node and renderer referenced by the item are still exist. + // 2. Mark the related dirty rects as visited so they will not be removed below. + auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); + while (dirtyRectPos != itemsDirtyRects.end()) + { + if (dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer) + { + break; + } - if ( !instruction.mFrameBuffer->IsSurfaceBacked() ) - { - // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer. - Render::TextureFrameBuffer* textureFrameBuffer = static_cast( instruction.mFrameBuffer ); - mImpl->textureDependencyList.PushBack( textureFrameBuffer->GetTextureId() ); + dirtyRectPos->visited = true; + dirtyRectPos++; + } + } + } + } + } } } - else - { - mImpl->currentContext->BindFramebuffer( GL_FRAMEBUFFER, 0u ); - } - if ( surfaceFrameBuffer ) + // Check removed nodes or removed renderers dirty rects + auto i = itemsDirtyRects.begin(); + auto j = itemsDirtyRects.begin(); + while (i != itemsDirtyRects.end()) { - mImpl->currentContext->Viewport( surfaceRect.x, - surfaceRect.y, - surfaceRect.width, - surfaceRect.height ); - - mImpl->currentContext->ClearColor( backgroundColor.r, - backgroundColor.g, - backgroundColor.b, - backgroundColor.a ); + if (i->visited) + { + *j++ = *i; + } + else + { + Rect& dirtRect = i->rect; + damagedRects.push_back(dirtRect); + } + i++; } - // Clear the entire color, depth and stencil buffers for the default framebuffer, if required. - // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers - // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit", - // and then stall. That problem is only noticeable when rendering a large number of vertices per frame. + itemsDirtyRects.resize(j - itemsDirtyRects.begin()); + damagedRectCleaner.SetCleanOnReturn(false); +} - mImpl->currentContext->SetScissorTest( false ); +void RenderManager::RenderScene( Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo ) +{ + Rect clippingRect; + RenderScene( status, scene, renderToFbo, clippingRect); +} - GLbitfield clearMask = GL_COLOR_BUFFER_BIT; +void RenderManager::RenderScene( Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect& clippingRect ) +{ + Internal::Scene& sceneInternal = GetImplementation( scene ); + SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject(); - mImpl->currentContext->ColorMask( true ); + uint32_t count = sceneObject->GetRenderInstructions().Count( mImpl->renderBufferIndex ); - if( depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ) + for( uint32_t i = 0; i < count; ++i ) { - mImpl->currentContext->DepthMask( true ); - clearMask |= GL_DEPTH_BUFFER_BIT; - } + RenderInstruction& instruction = sceneObject->GetRenderInstructions().At( mImpl->renderBufferIndex, i ); - if( stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) - { - mImpl->currentContext->ClearStencil( 0 ); - mImpl->currentContext->StencilMask( 0xFF ); // 8 bit stencil mask, all 1's - clearMask |= GL_STENCIL_BUFFER_BIT; - } + if ( ( renderToFbo && !instruction.mFrameBuffer ) || ( !renderToFbo && instruction.mFrameBuffer ) ) + { + continue; // skip + } - mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR ); + // Mark that we will require a post-render step to be performed (includes swap-buffers). + status.SetNeedsPostRender( true ); - if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != 0 ) ) - { - if ( instruction.mFrameBuffer->IsSurfaceBacked() ) // Surface rendering + Rect viewportRect; + Vector4 clearColor; + + if ( instruction.mIsClearColorSet ) { - if ( instruction.mIsViewportSet ) + clearColor = instruction.mClearColor; + } + else + { + clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR; + } + + Rect surfaceRect = mImpl->defaultSurfaceRect; + Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable; + Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable; + int surfaceOrientation = sceneInternal.GetSurfaceOrientation(); + + if ( instruction.mFrameBuffer ) + { + // offscreen buffer + if ( mImpl->currentContext != &mImpl->context ) { - // For glViewport the lower-left corner is (0,0) - // For glViewport the lower-left corner is (0,0) - const int32_t y = ( surfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y; - viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height ); + // Switch to shared context for off-screen buffer + mImpl->currentContext = &mImpl->context; + + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + } + + // Clear the current cached program when the context is switched + mImpl->programController.ClearCurrentProgram(); } - else + } + else + { + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) { - viewportRect = surfaceRect; + if ( mImpl->currentContext != sceneObject->GetContext() ) + { + // Switch the correct context if rendering to a surface + mImpl->currentContext = sceneObject->GetContext(); + + // Clear the current cached program when the context is switched + mImpl->programController.ClearCurrentProgram(); + } } + + surfaceRect = Rect( 0, 0, static_cast( scene.GetSize().width ), static_cast( scene.GetSize().height ) ); } - else // Offscreen buffer rendering + + // Make sure that GL context must be created + mImpl->currentContext->GlContextCreated(); + + // reset the program matrices for all programs once per frame + // this ensures we will set view and projection matrix once per program per camera + mImpl->programController.ResetProgramMatrices(); + + if( instruction.mFrameBuffer ) { + instruction.mFrameBuffer->Bind( *mImpl->currentContext ); + + // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer. + for (unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0) + { + mImpl->textureDependencyList.PushBack( instruction.mFrameBuffer->GetTextureId(i0) ); + } + } + else + { + mImpl->currentContext->BindFramebuffer( GL_FRAMEBUFFER, 0u ); + } + + if ( !instruction.mFrameBuffer ) + { + mImpl->currentContext->Viewport( surfaceRect.x, + surfaceRect.y, + surfaceRect.width, + surfaceRect.height ); + } + + // Clear the entire color, depth and stencil buffers for the default framebuffer, if required. + // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers + // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit", + // and then stall. That problem is only noticeable when rendering a large number of vertices per frame. + GLbitfield clearMask = GL_COLOR_BUFFER_BIT; + + mImpl->currentContext->ColorMask( true ); + + if( depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ) + { + mImpl->currentContext->DepthMask( true ); + clearMask |= GL_DEPTH_BUFFER_BIT; + } + + if( stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) + { + mImpl->currentContext->ClearStencil( 0 ); + mImpl->currentContext->StencilMask( 0xFF ); // 8 bit stencil mask, all 1's + clearMask |= GL_STENCIL_BUFFER_BIT; + } + + if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != nullptr ) ) + { + // Offscreen buffer rendering if ( instruction.mIsViewportSet ) { // For glViewport the lower-left corner is (0,0) @@ -728,138 +931,212 @@ void RenderManager::DoRender( RenderInstruction& instruction ) { viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() ); } + surfaceOrientation = 0; } - } - else // No Offscreen frame buffer rendering - { - // Check whether a viewport is specified, otherwise the full surface size is used - if ( instruction.mFrameBuffer != 0 ) + else // No Offscreen frame buffer rendering { + // Check whether a viewport is specified, otherwise the full surface size is used if ( instruction.mIsViewportSet ) { // For glViewport the lower-left corner is (0,0) - const int32_t y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y; + const int32_t y = ( surfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y; viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height ); } else { - viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() ); + viewportRect = surfaceRect; } } + + // Set surface orientation + mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation); + + bool clearFullFrameRect = true; + if( instruction.mFrameBuffer != nullptr ) + { + Viewport frameRect( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() ); + clearFullFrameRect = ( frameRect == viewportRect ); + } else { - viewportRect = surfaceRect; + clearFullFrameRect = ( surfaceRect == viewportRect ); } - } - mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height); - - if ( instruction.mIsClearColorSet ) - { - mImpl->currentContext->ClearColor( clearColor.r, - clearColor.g, - clearColor.b, - clearColor.a ); - - // Clear the viewport area only - mImpl->currentContext->SetScissorTest( true ); - mImpl->currentContext->Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height ); - mImpl->currentContext->ColorMask( true ); - mImpl->currentContext->Clear( GL_COLOR_BUFFER_BIT , Context::CHECK_CACHED_VALUES ); - mImpl->currentContext->SetScissorTest( false ); - } - - // Clear the list of bound textures - mImpl->boundTextures.Clear(); - - mImpl->renderAlgorithms.ProcessRenderInstruction( - instruction, - *mImpl->currentContext, - mImpl->renderBufferIndex, - depthBufferAvailable, - stencilBufferAvailable, - mImpl->boundTextures ); + if (!clippingRect.IsEmpty()) + { + if (!clippingRect.Intersect(viewportRect)) + { + DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height); + clippingRect = Rect(); + } + clearFullFrameRect = false; + } - // Synchronise the FBO/Texture access when there are multiple contexts - if ( mImpl->currentContext->IsSurfacelessContextSupported() ) - { - // Check whether any binded texture is in the dependency list - bool textureFound = false; + mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height); - if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u ) + if (instruction.mIsClearColorSet) { - for ( auto textureId : mImpl->textureDependencyList ) + mImpl->currentContext->ClearColor(clearColor.r, + clearColor.g, + clearColor.b, + clearColor.a); + if (!clearFullFrameRect) { - - textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), - [textureId]( GLuint id ) - { - return textureId == id; - } ) != mImpl->boundTextures.End(); + if (!clippingRect.IsEmpty()) + { + mImpl->currentContext->SetScissorTest(true); + mImpl->currentContext->Scissor(clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height); + mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR); + mImpl->currentContext->SetScissorTest(false); + } + else + { + mImpl->currentContext->SetScissorTest(true); + mImpl->currentContext->Scissor(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height); + mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR); + mImpl->currentContext->SetScissorTest(false); + } + } + else + { + mImpl->currentContext->SetScissorTest(false); + mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR); } } - if ( textureFound ) + // Clear the list of bound textures + mImpl->boundTextures.Clear(); + + mImpl->renderAlgorithms.ProcessRenderInstruction( + instruction, + *mImpl->currentContext, + mImpl->renderBufferIndex, + depthBufferAvailable, + stencilBufferAvailable, + mImpl->boundTextures, + clippingRect ); + + // Synchronise the FBO/Texture access when there are multiple contexts + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) { + // Check whether any binded texture is in the dependency list + bool textureFound = false; - if ( !instruction.mFrameBuffer || !instruction.mFrameBuffer->IsSurfaceBacked() ) + if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u ) { - // For off-screen buffer - - // Wait until all rendering calls for the currently context are executed - mImpl->glContextHelperAbstraction.WaitClient(); + for ( auto textureId : mImpl->textureDependencyList ) + { - // Clear the dependency list - mImpl->textureDependencyList.Clear(); + textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), + [textureId]( GLuint id ) + { + return textureId == id; + } ) != mImpl->boundTextures.End(); + } } - else + + if ( textureFound ) { - // For surface-backed frame buffer + if ( instruction.mFrameBuffer ) + { + // For off-screen buffer + + // Wait until all rendering calls for the currently context are executed + mImpl->glContextHelperAbstraction.WaitClient(); - // Worker thread lambda function - auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction; - auto workerFunction = [&glContextHelperAbstraction]( int workerThread ) + // Clear the dependency list + mImpl->textureDependencyList.Clear(); + } + else { - // Switch to the shared context in the worker thread - glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + // Worker thread lambda function + auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction; + auto workerFunction = [&glContextHelperAbstraction]( int workerThread ) + { + // Switch to the shared context in the worker thread + glContextHelperAbstraction.MakeSurfacelessContextCurrent(); - // Wait until all rendering calls for the shared context are executed - glContextHelperAbstraction.WaitClient(); + // Wait until all rendering calls for the shared context are executed + glContextHelperAbstraction.WaitClient(); - // Must clear the context in the worker thread - // Otherwise the shared context cannot be switched to from the render thread - glContextHelperAbstraction.MakeContextNull(); - }; + // Must clear the context in the worker thread + // Otherwise the shared context cannot be switched to from the render thread + glContextHelperAbstraction.MakeContextNull(); + }; - auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction ); - if ( future ) - { - mImpl->threadPool->Wait(); + auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction ); + if ( future ) + { + mImpl->threadPool->Wait(); - // Clear the dependency list - mImpl->textureDependencyList.Clear(); + // Clear the dependency list + mImpl->textureDependencyList.Clear(); + } } } } + + if( instruction.mRenderTracker && instruction.mFrameBuffer ) + { + // This will create a sync object every frame this render tracker + // is alive (though it should be now be created only for + // render-once render tasks) + instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction ); + instruction.mRenderTracker = nullptr; // Only create once. + } + + if ( renderToFbo ) + { + mImpl->currentContext->Flush(); + } } - if( instruction.mRenderTracker && ( instruction.mFrameBuffer != 0 ) ) + GLenum attachments[] = { GL_DEPTH, GL_STENCIL }; + mImpl->currentContext->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); + +} + +void RenderManager::PostRender( bool uploadOnly ) +{ + if ( !uploadOnly ) { - // This will create a sync object every frame this render tracker - // is alive (though it should be now be created only for - // render-once render tasks) - instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction ); - instruction.mRenderTracker = NULL; // Only create once. + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + } + + GLenum attachments[] = { GL_DEPTH, GL_STENCIL }; + mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); } - if ( surfaceFrameBuffer ) + //Notify RenderGeometries that rendering has finished + for ( auto&& iter : mImpl->geometryContainer ) { - surfaceFrameBuffer->PostRender(); + iter->OnRenderFinished(); } - else + + mImpl->UpdateTrackers(); + + + uint32_t count = 0u; + for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i ) { - mImpl->currentContext->Flush(); + count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex ); } + + const bool haveInstructions = count > 0u; + + // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame. + mImpl->lastFrameWasRendered = haveInstructions; + + /** + * The rendering has finished; swap to the next buffer. + * Ideally the update has just finished using this buffer; otherwise the render thread + * should block until the update has finished. + */ + mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1; + + DALI_PRINT_RENDER_END(); } } // namespace SceneGraph