1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/ozone/demo/software_renderer.h"
7 #include "base/trace_event/trace_event.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "third_party/skia/include/core/SkSurface.h"
10 #include "ui/gfx/vsync_provider.h"
11 #include "ui/ozone/public/ozone_platform.h"
12 #include "ui/ozone/public/surface_factory_ozone.h"
13 #include "ui/ozone/public/surface_ozone_canvas.h"
18 const int kFrameDelayMilliseconds
= 16;
22 SoftwareRenderer::SoftwareRenderer(gfx::AcceleratedWidget widget
,
23 const gfx::Size
& size
)
24 : RendererBase(widget
, size
),
25 vsync_period_(base::TimeDelta::FromMilliseconds(kFrameDelayMilliseconds
)),
26 weak_ptr_factory_(this) {
29 SoftwareRenderer::~SoftwareRenderer() {
32 bool SoftwareRenderer::Initialize() {
33 software_surface_
= ui::OzonePlatform::GetInstance()
34 ->GetSurfaceFactoryOzone()
35 ->CreateCanvasForWidget(widget_
);
36 if (!software_surface_
) {
37 LOG(ERROR
) << "Failed to create software surface";
41 software_surface_
->ResizeCanvas(size_
);
42 vsync_provider_
= software_surface_
->CreateVSyncProvider();
47 void SoftwareRenderer::RenderFrame() {
48 TRACE_EVENT0("ozone", "SoftwareRenderer::RenderFrame");
50 float fraction
= NextFraction();
52 skia::RefPtr
<SkSurface
> surface
= software_surface_
->GetSurface();
55 SkColorSetARGB(0xff, 0, 0xff * fraction
, 0xff * (1 - fraction
));
57 surface
->getCanvas()->clear(color
);
59 software_surface_
->PresentCanvas(gfx::Rect(size_
));
61 if (vsync_provider_
) {
62 vsync_provider_
->GetVSyncParameters(
63 base::Bind(&SoftwareRenderer::UpdateVSyncParameters
,
64 weak_ptr_factory_
.GetWeakPtr()));
67 timer_
.Start(FROM_HERE
, vsync_period_
, this, &SoftwareRenderer::RenderFrame
);
70 void SoftwareRenderer::UpdateVSyncParameters(const base::TimeTicks timebase
,
71 const base::TimeDelta interval
) {
72 vsync_period_
= interval
;