summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/demo/software_renderer.cc
blob: 3214fa4b3e96a9b7af252b517a664656279ae0fc (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/demo/software_renderer.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/trace_event/trace_event.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/vsync_provider.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/platform_window_surface.h"
#include "ui/ozone/public/surface_factory_ozone.h"
#include "ui/ozone/public/surface_ozone_canvas.h"

namespace ui {
namespace {

const int kFrameDelayMilliseconds = 16;

}  // namespace

SoftwareRenderer::SoftwareRenderer(
    gfx::AcceleratedWidget widget,
    std::unique_ptr<PlatformWindowSurface> window_surface,
    const gfx::Size& size)
    : RendererBase(widget, size),
      window_surface_(std::move(window_surface)),
      vsync_period_(
          base::TimeDelta::FromMilliseconds(kFrameDelayMilliseconds)) {}

SoftwareRenderer::~SoftwareRenderer() {}

bool SoftwareRenderer::Initialize() {
  software_surface_ =
      ui::OzonePlatform::GetInstance()
          ->GetSurfaceFactoryOzone()
          ->CreateCanvasForWidget(widget_,
                                  base::ThreadTaskRunnerHandle::Get().get());
  if (!software_surface_) {
    LOG(ERROR) << "Failed to create software surface";
    return false;
  }

  software_surface_->ResizeCanvas(size_);
  vsync_provider_ = software_surface_->CreateVSyncProvider();
  RenderFrame();
  return true;
}

void SoftwareRenderer::RenderFrame() {
  TRACE_EVENT0("ozone", "SoftwareRenderer::RenderFrame");

  float fraction = NextFraction();

  SkCanvas* canvas = software_surface_->GetCanvas();

  SkColor color =
      SkColorSetARGB(0xff, 0, 0xff * fraction, 0xff * (1 - fraction));

  canvas->clear(color);

  software_surface_->PresentCanvas(gfx::Rect(size_));

  if (vsync_provider_) {
    vsync_provider_->GetVSyncParameters(
        base::BindOnce(&SoftwareRenderer::UpdateVSyncParameters,
                       weak_ptr_factory_.GetWeakPtr()));
  }

  timer_.Start(FROM_HERE, vsync_period_, this, &SoftwareRenderer::RenderFrame);
}

void SoftwareRenderer::UpdateVSyncParameters(const base::TimeTicks timebase,
                                             const base::TimeDelta interval) {
  vsync_period_ = interval;
}

}  // namespace ui