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 "third_party/skia/include/core/SkCanvas.h"
8 #include "third_party/skia/include/core/SkSurface.h"
9 #include "ui/gfx/vsync_provider.h"
10 #include "ui/ozone/public/ozone_platform.h"
11 #include "ui/ozone/public/surface_factory_ozone.h"
12 #include "ui/ozone/public/surface_ozone_canvas.h"
17 const int kFrameDelayMilliseconds
= 16;
21 SoftwareRenderer::SoftwareRenderer(gfx::AcceleratedWidget widget
,
22 const gfx::Size
& size
)
23 : RendererBase(widget
, size
),
24 vsync_period_(base::TimeDelta::FromMilliseconds(kFrameDelayMilliseconds
)),
25 weak_ptr_factory_(this) {
28 SoftwareRenderer::~SoftwareRenderer() {
31 bool SoftwareRenderer::Initialize() {
32 software_surface_
= ui::OzonePlatform::GetInstance()
33 ->GetSurfaceFactoryOzone()
34 ->CreateCanvasForWidget(widget_
);
35 if (!software_surface_
) {
36 LOG(ERROR
) << "Failed to create software surface";
40 software_surface_
->ResizeCanvas(size_
);
41 vsync_provider_
= software_surface_
->CreateVSyncProvider();
46 void SoftwareRenderer::RenderFrame() {
47 float fraction
= NextFraction();
49 skia::RefPtr
<SkSurface
> surface
= software_surface_
->GetSurface();
52 SkColorSetARGB(0xff, 0, 0xff * fraction
, 0xff * (1 - fraction
));
54 surface
->getCanvas()->clear(color
);
56 software_surface_
->PresentCanvas(gfx::Rect(size_
));
58 if (vsync_provider_
) {
59 vsync_provider_
->GetVSyncParameters(
60 base::Bind(&SoftwareRenderer::UpdateVSyncParameters
,
61 weak_ptr_factory_
.GetWeakPtr()));
64 timer_
.Start(FROM_HERE
, vsync_period_
, this, &SoftwareRenderer::RenderFrame
);
67 void SoftwareRenderer::UpdateVSyncParameters(const base::TimeTicks timebase
,
68 const base::TimeDelta interval
) {
69 vsync_period_
= interval
;