perf-view-creation-example.cpp 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/*
 * Copyright (c) 2022 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.
 *
 */

// EXTERNAL INCLUDES
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
#include <dali/integration-api/debug.h>
#include <dali/integration-api/trace.h>

#include <chrono>
#include <iostream>
#include <list>
#include <thread>

// INTERNAL INCLUDES
#include "shared/utility.h"

using namespace Dali;
using namespace Dali::Toolkit;
using namespace std;

namespace
{
enum class ControlTestType
{
  // clang-format off
  COLOR = 0,            ///< Test with simple color
  IMAGE,                ///< Test with simple image
  TEXT,                 ///< Test with simple text label
  ROUNDED_COLOR,        ///< Test with rounded color
  BORDER_COLOR,         ///< Test with borderline color
  ROUNDED_BORDER_COLOR, ///< Test with rounded borderline color
  BLUR_COLOR,           ///< Test with blur color
  ROUNDED_BLUR_COLOR,   ///< Test with rounded blur color
  TYPE_MAX,
  // clang-format on
};

const char* TestTypeString(ControlTestType type)
{
  // clang-format off
  switch(type)
  {
    case ControlTestType::COLOR:               return "COLOR";
    case ControlTestType::IMAGE:               return "IMAGE";
    case ControlTestType::TEXT:                return "TEXT";
    case ControlTestType::ROUNDED_COLOR:       return "ROUNDED COLOR";
    case ControlTestType::BORDER_COLOR:        return "BORDER COLOR";
    case ControlTestType::ROUNDED_BORDER_COLOR:return "ROUNDED BORDER COLOR";
    case ControlTestType::BLUR_COLOR:          return "BLUR COLOR";
    case ControlTestType::ROUNDED_BLUR_COLOR:  return "ROUNDED BLUR COLOR";
    default:                                   return "UNKNOWN";
  }
  // clang-format on
}

// NOTE : Due to the image load is bottleneck on target, we just use single small image.
const char* IMAGE_PATH[] = {
  DEMO_IMAGE_DIR "gallery-small-1.jpg",
};

constexpr uint32_t NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);

constexpr uint32_t ROWS_COUNT(40);
constexpr uint32_t COLUMNS_COUNT(40);
constexpr uint32_t TOTAL_COLUMNS_COUNT(80);
constexpr uint32_t DURATION_PER_COLUMNS(50); // miliseconds.
// Increase animation time cause OnTick time can be delayed.
constexpr uint32_t DURATION_OF_ANIMATION(DURATION_PER_COLUMNS*(COLUMNS_COUNT * 4 / 3)); // miliseconds.

// We should render same type of views in some timing.
static_assert(COLUMNS_COUNT * 2 <= TOTAL_COLUMNS_COUNT);

constexpr float VIEW_MARGIN_RATE = 0.2f;

// copy from dali-adaptor time-service.cpp
void GetNanoseconds(uint64_t& timeInNanoseconds)
{
  // Get the time of a monotonic clock since its epoch.
  auto epoch = std::chrono::steady_clock::now().time_since_epoch();

  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);

  timeInNanoseconds = static_cast<uint64_t>(duration.count());
}

Control CreateColor()
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);
  bgView.SetBackgroundColor(Color::YELLOW);
  return bgView;
}

Control CreateImage(uint32_t& imageCount)
{
  Control bgView = ImageView::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS, IMAGE_PATH[imageCount++ % NUM_IMAGES]);
  return bgView;
}

Control CreateTextLabel()
{
  Control bgView = TextLabel::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS, "Hello, World!");
  return bgView;
}

Control CreateRoundedColor()
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);

  Property::Map map;
  map[Visual::Property::TYPE]                      = Visual::COLOR;
  map[ColorVisual::Property::MIX_COLOR]            = Color::YELLOW;
  map[DevelVisual::Property::CORNER_RADIUS]        = 0.5f;
  map[DevelVisual::Property::CORNER_RADIUS_POLICY] = Visual::Transform::Policy::RELATIVE;

  bgView[Control::Property::BACKGROUND] = map;

  return bgView;
}

Control CreateBorderColor(const float& requiredBorderlineWidth)
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);

  Property::Map map;
  map[Visual::Property::TYPE]                  = Visual::COLOR;
  map[ColorVisual::Property::MIX_COLOR]        = Color::YELLOW;
  map[DevelVisual::Property::BORDERLINE_WIDTH] = requiredBorderlineWidth;
  map[DevelVisual::Property::BORDERLINE_COLOR] = Color::RED;

  bgView[Control::Property::BACKGROUND] = map;

  return bgView;
}

Control CreateRoundedBorderColor(const float& requiredBorderlineWidth)
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);

  Property::Map map;
  map[Visual::Property::TYPE]                      = Visual::COLOR;
  map[ColorVisual::Property::MIX_COLOR]            = Color::YELLOW;
  map[DevelVisual::Property::CORNER_RADIUS]        = 0.5f;
  map[DevelVisual::Property::CORNER_RADIUS_POLICY] = Visual::Transform::Policy::RELATIVE;
  map[DevelVisual::Property::BORDERLINE_WIDTH]     = requiredBorderlineWidth;
  map[DevelVisual::Property::BORDERLINE_COLOR]     = Color::RED;

  bgView[Control::Property::BACKGROUND] = map;

  return bgView;
}

Control CreateBlurColor(const float& requiredBlurRadius)
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);

  Property::Map map;
  map[Visual::Property::TYPE]                  = Visual::COLOR;
  map[ColorVisual::Property::MIX_COLOR]        = Color::YELLOW;
  map[DevelColorVisual::Property::BLUR_RADIUS] = requiredBlurRadius;

  bgView[Control::Property::BACKGROUND] = map;

  return bgView;
}

Control CreateRoundedBlurColor(const float& requiredBlurRadius)
{
  Control bgView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);

  Property::Map map;
  map[Visual::Property::TYPE]                      = Visual::COLOR;
  map[ColorVisual::Property::MIX_COLOR]            = Color::YELLOW;
  map[DevelVisual::Property::CORNER_RADIUS]        = 0.5f;
  map[DevelVisual::Property::CORNER_RADIUS_POLICY] = Visual::Transform::Policy::RELATIVE;
  map[DevelColorVisual::Property::BLUR_RADIUS]     = requiredBlurRadius;

  bgView[Control::Property::BACKGROUND] = map;

  return bgView;
}

/**
 * @brief Statistic container that we can get average / sum / min/ max.
 *
 * @tparam T Type of data. T should define add, compare, div-by-int, numeric_limits<T>::min and max
 */
template<typename T>
struct Statistic
{
  static constexpr double trimRate = 0.34;

  std::vector<T> v;
  int            vcnt;
  T              vsum;
  T              vmax;
  T              vmin;
  Statistic()
  {
    Clear();
  }

  void Clear()
  {
    v.clear();
    vcnt = 0;
    vsum = 0;
    vmax = std::numeric_limits<T>::min();
    vmin = std::numeric_limits<T>::max();
  }

  void Add(T x)
  {
    v.emplace_back(x);
    vsum += x;
    vcnt++;
    vmax = std::max(vmax, x);
    vmin = std::min(vmin, x);
  }

  double GetAverage()
  {
    if(vcnt == 0) return 0.0;
    return static_cast<double>(vsum) / vcnt;
  }

  double GetTrimedAverage()
  {
    if(vcnt == 0) return 0.0;
    std::sort(v.begin(), v.end());
    T   trimVsum   = 0;
    int removedCnt = static_cast<int>(vcnt * trimRate * 0.5); // floor
    int trimVcnt   = vcnt - removedCnt * 2;
    if(trimVcnt == 0)
    {
      trimVcnt += 2;
      removedCnt--;
    }
    for(int i = removedCnt; i < vcnt - removedCnt; i++)
    {
      trimVsum += v[i];
    }

    return static_cast<double>(trimVsum) / trimVcnt;
  }
};

DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERF_VIEW_CREATION_SAMPLE, true);

} // namespace

/**
 * Test application to compare performance between various type of Views creation time & manually created Renderers
 */
class PerfViewCreation : public ConnectionTracker
{
public:
  PerfViewCreation(Application& application)
  : mApplication(application),
    mRowsCount(ROWS_COUNT),
    mColumnsCount(COLUMNS_COUNT),
    mTotalColumnsCount(TOTAL_COLUMNS_COUNT),
    mDurationPerColumns(DURATION_PER_COLUMNS),
    mDurationOfAnimation(DURATION_OF_ANIMATION),
    mTestType(ControlTestType::COLOR)
  {
    // Connect to the Application's Init signal
    mApplication.InitSignal().Connect(this, &PerfViewCreation::Create);
  }

  ~PerfViewCreation() = default;

  // The Init signal is received once (only) during the Application lifetime
  void Create(Application& application)
  {
    GetNanoseconds(mAppStartTime);

    // Get a handle to the window
    mWindow = application.GetWindow();
    mWindow.SetBackgroundColor(Color::WHITE);
    mWindowSize = mWindow.GetSize();

    mWindow.GetRootLayer().SetProperty(Layer::Property::DEPTH_TEST, false);

    mSize = Vector3(mWindowSize.x / mColumnsCount, mWindowSize.y / mRowsCount, 0.0f);

    Timer timer = Timer::New(mDurationPerColumns);
    timer.TickSignal().Connect(this, &PerfViewCreation::OnTick);
    mTimerList.push_back(timer);

    mCreationStatistic.Clear();

    mCreateCount = 0;
    mDeleteCount = 0;
    mImageCount  = 0;

    timer.Start();

    // Respond to key events
    mWindow.KeyEventSignal().Connect(this, &PerfViewCreation::OnKeyEvent);
  }

  bool OnTick()
  {
    CreateColumnView();
    if(mCreateCount < mColumnsCount)
    {
      // Start next phase.
      Timer timer = Timer::New(mDurationPerColumns);
      timer.TickSignal().Connect(this, &PerfViewCreation::OnTick);
      mTimerList.push_back(timer);

      timer.Start();
    }
    return false;
  }
  void CreateColumnView()
  {
    uint64_t startTime;
    uint64_t endTime;

    GetNanoseconds(startTime);
    DALI_TRACE_BEGIN(gTraceFilter, "DALI_SAMPLE_PERF_VIEW_CREATION");

    Control columnView = Control::New(Control::ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS);
    columnView.SetBackgroundColor(Color::BLUE);
    columnView[Actor::Property::PARENT_ORIGIN] = ParentOrigin::TOP_LEFT;
    columnView[Actor::Property::ANCHOR_POINT]  = AnchorPoint::TOP_LEFT;
    columnView[Actor::Property::SIZE]          = Vector2(mSize.x, (float)mWindowSize.y);
    columnView[Actor::Property::POSITION]      = Vector2(mSize.x * (mCreateCount % mColumnsCount), -(float)mWindowSize.y);
    for(uint32_t i = 0; i < mRowsCount; ++i)
    {
      Control bgView;
      switch(mTestType)
      {
        case ControlTestType::COLOR:
        default:
        {
          bgView = CreateColor();
          break;
        }
        case ControlTestType::IMAGE:
        {
          bgView = CreateImage(mImageCount);
          break;
        }
        case ControlTestType::TEXT:
        {
          bgView = CreateTextLabel();
          break;
        }
        case ControlTestType::ROUNDED_COLOR:
        {
          bgView = CreateRoundedColor();
          break;
        }
        case ControlTestType::BORDER_COLOR:
        {
          bgView = CreateBorderColor(std::min(mSize.x, mSize.y) * VIEW_MARGIN_RATE);
          break;
        }
        case ControlTestType::ROUNDED_BORDER_COLOR:
        {
          bgView = CreateRoundedBorderColor(std::min(mSize.x, mSize.y) * VIEW_MARGIN_RATE);
          break;
        }
        case ControlTestType::BLUR_COLOR:
        {
          bgView = CreateBlurColor(std::min(mSize.x, mSize.y) * VIEW_MARGIN_RATE * 0.5f);
          break;
        }
        case ControlTestType::ROUNDED_BLUR_COLOR:
        {
          bgView = CreateRoundedBlurColor(std::min(mSize.x, mSize.y) * VIEW_MARGIN_RATE * 0.5f);
          break;
        }
      }

      bgView[Actor::Property::PARENT_ORIGIN] = ParentOrigin::TOP_LEFT;
      bgView[Actor::Property::ANCHOR_POINT]  = AnchorPoint::TOP_LEFT;
      bgView[Actor::Property::SIZE]          = Vector2(mSize.x * (1.0f - VIEW_MARGIN_RATE), mSize.y * (1.0f - VIEW_MARGIN_RATE));
      bgView[Actor::Property::POSITION]      = Vector2(mSize.x * VIEW_MARGIN_RATE * 0.5f, mSize.y * VIEW_MARGIN_RATE * 0.5f + mSize.y * i);
      columnView.Add(bgView);
    }

    mWindow.GetRootLayer().Add(columnView);
    mCreatingControlList.push_back(columnView);

    // Add appearing animation
    Animation appearingAnimation = Animation::New(mDurationOfAnimation * 0.001f);
    appearingAnimation.AnimateTo(Property(columnView, Actor::Property::POSITION_Y), 0.0f);
    appearingAnimation.FinishedSignal().Connect(this, &PerfViewCreation::OnAppearAnimationFinished);
    appearingAnimation.Play();

    mCreatingAnimationList.push_back(appearingAnimation);

    GetNanoseconds(endTime);

    DALI_TRACE_END(gTraceFilter, "DALI_SAMPLE_PERF_VIEW_CREATION");

    // Append duration of creation time.
    mCreationStatistic.Add((endTime - startTime) / 1000000.0);

    mCreateCount++;

    if(mCreateCount % mTotalColumnsCount == 0)
    {
      DALI_LOG_ERROR("Average of creation %d DALI(%s) : %.6lf ms\n", mRowsCount, TestTypeString(mTestType), mCreationStatistic.GetTrimedAverage());
      mCreationStatistic.Clear();
      mTestType = static_cast<ControlTestType>((static_cast<int>(mTestType) + 1) % static_cast<int>(ControlTestType::TYPE_MAX));
    }
  }

  bool OnTouch(Actor actor, const TouchEvent& touch)
  {
    // quit the application
    mApplication.Quit();
    return true;
  }

  void OnKeyEvent(const KeyEvent& event)
  {
    if(event.GetState() == KeyEvent::DOWN)
    {
      if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
      {
        mApplication.Quit();
      }
    }
  }

  void OnAppearAnimationFinished(Animation& animation)
  {
    // We can assume that front of mControlList must be disappearing.
    auto currentControl = mCreatingControlList.front();
    mCreatingControlList.pop_front();

    // Dereference timer safety
    if(!mTimerList.empty())
    {
      mTimerList.pop_front();
    }

    // Dereference animation safety
    mCreatingAnimationList.pop_back();

    mRemovingControlList.push_back(currentControl);

    if(mCreateCount < mTotalColumnsCount * (static_cast<int>(ControlTestType::TYPE_MAX)))
    {
      CreateColumnView();
    }

    // Add disappearing animation
    Animation disappearingAnimation = Animation::New(mDurationOfAnimation * 0.001f);
    disappearingAnimation.AnimateTo(Property(currentControl, Actor::Property::POSITION_Y), (float)mWindowSize.y);
    disappearingAnimation.FinishedSignal().Connect(this, &PerfViewCreation::OnDisappearAnimationFinished);
    disappearingAnimation.Play();

    mRemovingAnimationList.push_back(disappearingAnimation);
  }
  void OnDisappearAnimationFinished(Animation& animation)
  {
    // We can assume that front of mControlList must be deleted.
    mRemovingControlList.front().Unparent();
    mRemovingControlList.pop_front();

    // Dereference animation safety
    mRemovingAnimationList.pop_back();

    mDeleteCount++;

    // If all controls are deleted, quit this application. byebye~
    if(mDeleteCount == mTotalColumnsCount * (static_cast<int>(ControlTestType::TYPE_MAX)))
    {
      GetNanoseconds(mAppEndTime);

      DALI_LOG_ERROR("Duration of all app running time : %.6lf ms\n", (mAppEndTime - mAppStartTime) / 1000000.0);
      mApplication.Quit();
    }
  }

private:
  Application& mApplication;
  Window       mWindow;
  Vector2      mWindowSize;

  std::list<Control>   mCreatingControlList;
  std::list<Control>   mRemovingControlList;
  std::list<Animation> mCreatingAnimationList;
  std::list<Animation> mRemovingAnimationList;
  std::list<Timer>     mTimerList;

  Vector3 mSize;

  const uint32_t mRowsCount;
  const uint32_t mColumnsCount;
  const uint32_t mTotalColumnsCount;

  const uint32_t mDurationPerColumns;
  const uint32_t mDurationOfAnimation;

  ControlTestType mTestType;

  uint32_t mCreateCount = 0;
  uint32_t mDeleteCount = 0;
  uint32_t mImageCount  = 0;

  uint64_t mAppStartTime = 0;
  uint64_t mAppEndTime   = 0;

  Statistic<double> mCreationStatistic;
};

int DALI_EXPORT_API main(int argc, char** argv)
{
  Application application = Application::New(&argc, &argv);

  PerfViewCreation test(application);
  application.MainLoop();

  return 0;
}