+ // Get the number of child scissors in the stack (do not include layer or root box).
+ size_t childStackDepth = mScissorStack.size() - 1u;
+ const uint32_t scissorDepth = item.mNode->GetScissorDepth();
+ const bool clippingNode = item.mNode->GetClippingMode() == Dali::ClippingMode::CLIP_TO_BOUNDING_BOX;
+ bool traversedUpTree = false;
+
+ // If we are using scissor clipping and we are at the same depth (or less), we need to undo previous clips.
+ // We do this by traversing up the scissor clip stack and then apply the appropriate clip for the current render item.
+ // To know this, we use clippingDepth. This value is set on *every* node, but only increased as clipping nodes are hit depth-wise.
+ // So we know if we are at depth 4 and the stackDepth is 5, that we have gone up.
+ // If the depth is the same then we are effectively part of a different sub-tree from the parent, we must also remove the current clip.
+ // Note: Stack depth must always be at least 1, as we will have the layer or stage size as the root value.
+ if( ( childStackDepth > 0u ) && ( scissorDepth < childStackDepth ) )
+ {
+ while( scissorDepth < childStackDepth )
+ {
+ mScissorStack.pop_back();
+ --childStackDepth;
+ }
+
+ // We traversed up the tree, we need to apply a new scissor rectangle (unless we are at the root).
+ traversedUpTree = true;
+ }
+ if( clippingNode && childStackDepth > 0u && childStackDepth == scissorDepth ) // case of sibling clip area
+ {
+ mScissorStack.pop_back();
+ --childStackDepth;
+ }
+
+ // If we are on a clipping node, or we have traveled up the tree and gone back past a clipping node, may need to apply a new scissor clip.
+ if( clippingNode || traversedUpTree )
+ {
+ // First, check if we are a clipping node.
+ if( clippingNode )
+ {
+ // This is a clipping node. We generate the AABB for this node and intersect it with the previous intersection further up the tree.
+
+ // Get the AABB bounding box for the current render item.
+ const ClippingBox scissorBox( item.CalculateViewportSpaceAABB( mViewportRectangle.width, mViewportRectangle.height ) );
+
+ // Get the AABB for the parent item that we must intersect with.
+ const ClippingBox& parentBox( mScissorStack.back() );
+
+ // We must reduce the clipping area based on the parents area to allow nested clips. This is a set intersection function.
+ // We add the new scissor box to the stack so we can return to it if needed.
+ mScissorStack.emplace_back( IntersectAABB( parentBox, scissorBox ) );
+ }
+
+ // The scissor test is enabled if we have any children on the stack, OR, if there are none but it is a user specified layer scissor box.
+ // IE. It is not enabled if we are at the top of the stack and the layer does not have a specified clipping box.
+ const bool scissorEnabled = ( mScissorStack.size() > 0u ) || mHasLayerScissor;
+
+ // Enable the scissor test based on the above calculation
+ context.SetScissorTest( scissorEnabled );
+
+ // If scissor is enabled, we use the calculated screen-space coordinates (now in the stack).
+ if( scissorEnabled )
+ {
+ ClippingBox useScissorBox( mScissorStack.back() );
+ GLint x = useScissorBox.x;
+ GLint y = useScissorBox.y;
+ if( orientation == 90 )
+ {
+ x = mViewportRectangle.height - (useScissorBox.y + useScissorBox.height);
+ y = useScissorBox.x;
+ context.Scissor( x, y, useScissorBox.height, useScissorBox.width );
+ }
+ else if( orientation == 180 )
+ {
+ x = mViewportRectangle.width - (useScissorBox.x + useScissorBox.width);
+ y = mViewportRectangle.height - (useScissorBox.y + useScissorBox.height);
+ context.Scissor( x, y, useScissorBox.width, useScissorBox.height );
+ }
+ else if( orientation == 270 )
+ {
+ x = useScissorBox.y;
+ y = mViewportRectangle.width - (useScissorBox.x + useScissorBox.width);
+ context.Scissor( x, y, useScissorBox.height, useScissorBox.width );
+ }
+ else
+ {
+ context.Scissor( x, y, useScissorBox.width, useScissorBox.height );
+ }
+ }
+ }
+}
+
+inline void RenderAlgorithms::SetupClipping( const RenderItem& item,
+ Context& context,
+ bool& usedStencilBuffer,
+ uint32_t& lastClippingDepth,
+ uint32_t& lastClippingId,
+ Integration::StencilBufferAvailable stencilBufferAvailable,
+ int orientation )
+{
+ RenderMode::Type renderMode = RenderMode::AUTO;
+ const Renderer *renderer = item.mRenderer;
+ if( renderer )
+ {
+ renderMode = renderer->GetRenderMode();
+ }
+
+ // Setup the stencil using either the automatic clipping feature, or, the manual per-renderer stencil API.
+ // Note: This switch is in order of most likely value first.
+ switch( renderMode )
+ {
+ case RenderMode::AUTO:
+ {
+ // Turn the color buffer on as we always want to render this renderer, regardless of clipping hierarchy.
+ context.ColorMask( true );