<commit_before>/*
 * Copyright (c) 2014 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// FILE HEADER

#include "relayout-controller-impl.h"

// EXTERNAL INCLUDES
#if defined(DEBUG_ENABLED)
#include <sstream>
#include <dali/internal/event/common/system-overlay-impl.h>
#endif // defined(DEBUG_ENABLED)

// INTERNAL INCLUDES
#include <dali/public-api/actors/layer.h>
#include <dali/public-api/common/stage.h>
#include <dali/integration-api/debug.h>
#include <dali/integration-api/render-controller.h>
#include <dali/public-api/object/type-registry.h>
#include <dali/public-api/object/object-registry.h>
#include <dali/internal/event/actors/actor-impl.h>
#include <dali/internal/event/common/thread-local-storage.h>

namespace Dali
{

namespace Internal
{

namespace
{
#if defined(DEBUG_ENABLED)

Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );

/**
 * Prints out all the children of the given actor when debug is enabled.
 *
 * @param[in]  actor  The actor whose children to print.
 * @param[in]  level  The number of " | " to put in front of the children.
 */
void PrintChildren( Dali::Actor actor, int level )
{
  std::ostringstream output;

  for ( int t = 0; t < level; ++t )
  {
    output << " | ";
  }

  output << actor.GetTypeName();

  output << ", " << actor.GetName();

  output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();

  output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
  output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
  output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );

  output << ", (" << actor.GetObjectPtr() << ")" << std::endl;

  DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );

  ++level;
  unsigned int numChildren = actor.GetChildCount();
  for( unsigned int i=0; i<numChildren; ++i )
  {
    PrintChildren( actor.GetChildAt(i), level );
  }
  --level;
}

/**
 * Prints the entire hierarchy of the scene.
 */
void PrintHierarchy()
{
  if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
  {
    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
    PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
  }
}

#define PRINT_HIERARCHY PrintHierarchy()

#else // defined(DEBUG_ENABLED)

#define PRINT_HIERARCHY

#endif // defined(DEBUG_ENABLED)

} // unnamed namespace

RelayoutController* RelayoutController::Get()
{
  return &ThreadLocalStorage::Get().GetRelayoutController();
}

RelayoutController::RelayoutController( Integration::RenderController& controller )
: mRenderController( controller ),
  mRelayoutInfoAllocator(),
  mSlotDelegate( this ),
  mRelayoutStack( new MemoryPoolRelayoutContainer( mRelayoutInfoAllocator ) ),
  mRelayoutConnection( false ),
  mRelayoutFlag( false ),
  mEnabled( false ),
  mPerformingRelayout( false ),
  mProcessingCoreEvents( false )
{
  // Make space for 32 controls to avoid having to copy construct a lot in the beginning
  mRelayoutStack->Reserve( 32 );
}

RelayoutController::~RelayoutController()
{
  delete mRelayoutStack;
}

void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
{
  if( GetImplementation( actor ).RelayoutRequired() )
  {
    actors.Add( actor, size );
  }
}

void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
{
  if( !mEnabled )
  {
    return;
  }

  std::vector< Dali::Actor > potentialRedundantSubRoots;
  std::vector< Dali::Actor > topOfSubTreeStack;

  topOfSubTreeStack.push_back( actor );

  // Propagate on all dimensions
  for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
  {
    if( dimension & ( 1 << i ) )
    {
      // Do the propagation
      PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
    }
  }

  // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
  Dali::Actor subTreeActor = topOfSubTreeStack.back();
  Dali::Actor parent = subTreeActor.GetParent();
  if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
  {
    // Add sub tree root to relayout list
    AddRequest( subTreeActor );

    // Flag request for end of frame
    Request();
  }
  else
  {
    potentialRedundantSubRoots.push_back( subTreeActor );
  }

  // Remove any redundant sub-tree heads
  for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
  {
    Dali::Actor subRoot = *it;<commit_msg>Fix for SizeNegotiation.<commit_after>/*
 * Copyright (c) 2014 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// FILE HEADER

#include "relayout-controller-impl.h"

// EXTERNAL INCLUDES
#if defined(DEBUG_ENABLED)
#include <sstream>
#include <dali/internal/event/common/system-overlay-impl.h>
#endif // defined(DEBUG_ENABLED)

// INTERNAL INCLUDES
#include <dali/public-api/actors/layer.h>
#include <dali/public-api/common/stage.h>
#include <dali/integration-api/debug.h>
#include <dali/integration-api/render-controller.h>
#include <dali/public-api/object/type-registry.h>
#include <dali/public-api/object/object-registry.h>
#include <dali/internal/event/actors/actor-impl.h>
#include <dali/internal/event/common/thread-local-storage.h>

namespace Dali
{

namespace Internal
{

namespace
{
#if defined(DEBUG_ENABLED)

Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );

/**
 * Prints out all the children of the given actor when debug is enabled.
 *
 * @param[in]  actor  The actor whose children to print.
 * @param[in]  level  The number of " | " to put in front of the children.
 */
void PrintChildren( Dali::Actor actor, int level )
{
  std::ostringstream output;

  for ( int t = 0; t < level; ++t )
  {
    output << " | ";
  }

  output << actor.GetTypeName();

  output << ", " << actor.GetName();

  output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetTargetSize();

  output << ", Dirty: (" << ( GetImplementation( actor ).IsLayoutDirty( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutDirty( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
  output << ", Negotiated: (" << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::WIDTH ) ? "TRUE" : "FALSE" ) << "," << ( GetImplementation( actor ).IsLayoutNegotiated( Dimension::HEIGHT ) ? "TRUE" : "FALSE" ) << ")";
  output << ", Enabled: " << ( GetImplementation( actor ).IsRelayoutEnabled() ? "TRUE" : "FALSE" );

  output << ", (" << actor.GetObjectPtr() << ")" << std::endl;

  DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );

  ++level;
  unsigned int numChildren = actor.GetChildCount();
  for( unsigned int i=0; i<numChildren; ++i )
  {
    PrintChildren( actor.GetChildAt(i), level );
  }
  --level;
}

/**
 * Prints the entire hierarchy of the scene.
 */
void PrintHierarchy()
{
  if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
  {
    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "---------- ROOT LAYER ----------\n" );
    PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
  }
}

#define PRINT_HIERARCHY PrintHierarchy()

#else // defined(DEBUG_ENABLED)

#define PRINT_HIERARCHY

#endif // defined(DEBUG_ENABLED)

} // unnamed namespace

RelayoutController* RelayoutController::Get()
{
  return &ThreadLocalStorage::Get().GetRelayoutController();
}

RelayoutController::RelayoutController( Integration::RenderController& controller )
: mRenderController( controller ),
  mRelayoutInfoAllocator(),
  mSlotDelegate( this ),
  mRelayoutStack( new MemoryPoolRelayoutContainer( mRelayoutInfoAllocator ) ),
  mRelayoutConnection( false ),
  mRelayoutFlag( false ),
  mEnabled( false ),
  mPerformingRelayout( false ),
  mProcessingCoreEvents( false )
{
  // Make space for 32 controls to avoid having to copy construct a lot in the beginning
  mRelayoutStack->Reserve( 32 );
}

RelayoutController::~RelayoutController()
{
  delete mRelayoutStack;
}

void RelayoutController::QueueActor( Dali::Actor& actor, RelayoutContainer& actors, Vector2 size )
{
  if( GetImplementation( actor ).RelayoutRequired() )
  {
    actors.Add( actor, size );
  }
}

void RelayoutController::RequestRelayout( Dali::Actor& actor, Dimension::Type dimension )
{
  if( !mEnabled )
  {
    return;
  }

  std::vector< Dali::Actor > potentialRedundantSubRoots;
  std::vector< Dali::Actor > topOfSubTreeStack;

  topOfSubTreeStack.push_back( actor );

  // Propagate on all dimensions
  for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
  {
    if( dimension & ( 1 << i ) )
    {
      // Do the propagation
      PropagateAll( actor, static_cast< Dimension::Type >( 1 << i ), topOfSubTreeStack, potentialRedundantSubRoots );
    }
  }

  while( !topOfSubTreeStack.empty() )
  {
    // Request this actor as head of sub-tree if it is not dependent on a parent that is dirty
    Dali::Actor subTreeActor = topOfSubTreeStack.back();
    topOfSubTreeStack.pop_back();

    Dali::Actor parent = subTreeActor.GetParent();
    if( !parent || !( GetImplementation( subTreeActor ).RelayoutDependentOnParent() && GetImplementation( parent ).RelayoutRequired() ) )
    {
      // Add sub tree root to relayout list
      AddRequest( subTreeActor );

      // Flag request for end of frame
      Request();
    }
    else
    {
      potentialRedundantSubRoots.push_back( subTreeActor );
    }
  }

  // Remove any redundant sub-tree heads
  for( std::vector< Dali::Actor >::iterator it = potentialRedundantSubRoots.begin(), itEnd = potentialRedundantSubRoots.end(); it != itEnd; ++it )
  {
    Dali::Actor subRoot = *it;
{'fraction_non_alphanumeric': 0.1422344778509162, 'fraction_numerical': 0.0033801814623732433, 'mean_word_length': 4.126766985864113, 'pattern_counts': {'":': 0, '<': 141, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2404052', 'n_tokens_mistral': 3621, 'n_tokens_neox': 3554, 'n_words': 910}