Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: examples/ganesh_app/ganesh_view.cc

Issue 731893002: Add a simple scheduler for animation frames (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Fix math Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/ganesh_app/ganesh_view.h ('k') | mc/scheduler/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "examples/ganesh_app/ganesh_view.h" 5 #include "examples/ganesh_app/ganesh_view.h"
6 6
7 #include "base/message_loop/message_loop.h"
7 #include "examples/ganesh_app/ganesh_texture.h" 8 #include "examples/ganesh_app/ganesh_texture.h"
8 #include "third_party/skia/include/core/SkCanvas.h" 9 #include "third_party/skia/include/core/SkCanvas.h"
9 10
10 namespace examples { 11 namespace examples {
11 namespace { 12 namespace {
12 13
13 mojo::Size ToSize(const mojo::Rect& rect) { 14 mojo::Size ToSize(const mojo::Rect& rect) {
14 mojo::Size size; 15 mojo::Size size;
15 size.width = rect.width; 16 size.width = rect.width;
16 size.height = rect.height; 17 size.height = rect.height;
17 return size; 18 return size;
18 } 19 }
20
21 int RadiusAt(base::TimeTicks base_time, base::TimeTicks frame_time) {
22 return 50 + ((frame_time - base_time).InMilliseconds() % 6000) / 60;
23 }
19 } 24 }
20 25
21 GaneshView::GaneshView(mojo::Shell* shell, mojo::View* view) 26 GaneshView::GaneshView(mojo::Shell* shell, mojo::View* view)
22 : view_(view), 27 : view_(view),
23 gl_context_(shell), 28 gl_context_(shell),
24 gr_context_(gl_context_.context()), 29 gr_context_(gl_context_.context()),
25 texture_uploader_(this, shell, gl_context_.context()) { 30 texture_uploader_(this, shell, gl_context_.context()),
26 Draw(ToSize(view_->bounds())); 31 base_time_(base::TimeTicks::Now()),
32 scheduler_(this, base::MessageLoop::current()->task_runner().get()) {
33 // TODO(abarth): Get the vsync parameters from the output surface.
34 base::TimeDelta sixty_hz = base::TimeDelta::FromSecondsD(1.0 / 60);
35 scheduler_.UpdateVSync(mc::TimeInterval(base_time_, sixty_hz));
36
37 scheduler_.SetNeedsFrame();
27 } 38 }
28 39
29 GaneshView::~GaneshView() { 40 GaneshView::~GaneshView() {
30 } 41 }
31 42
32 void GaneshView::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { 43 void GaneshView::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) {
33 view_->SetSurfaceId(surface_id.Pass()); 44 view_->SetSurfaceId(surface_id.Pass());
34 } 45 }
35 46
36 void GaneshView::OnViewDestroyed(mojo::View* view) { 47 void GaneshView::OnViewDestroyed(mojo::View* view) {
37 delete this; 48 delete this;
38 } 49 }
39 50
40 void GaneshView::OnViewBoundsChanged(mojo::View* view, 51 void GaneshView::OnViewBoundsChanged(mojo::View* view,
41 const mojo::Rect& old_bounds, 52 const mojo::Rect& old_bounds,
42 const mojo::Rect& new_bounds) { 53 const mojo::Rect& new_bounds) {
43 Draw(ToSize(new_bounds)); 54 scheduler_.SetNeedsFrame();
44 } 55 }
45 56
46 void GaneshView::Draw(const mojo::Size& size) { 57 base::TimeDelta GaneshView::Draw(base::TimeTicks frame_time) {
58 base::TimeTicks start = base::TimeTicks::Now();
59
60 mojo::Size size = ToSize(view_->bounds());
61
47 GaneshTexture texture(gr_context_.context(), size); 62 GaneshTexture texture(gr_context_.context(), size);
48 SkCanvas* canvas = texture.canvas(); 63 SkCanvas* canvas = texture.canvas();
49 64
50 SkPaint paint; 65 SkPaint paint;
51 paint.setColor(SK_ColorRED); 66 paint.setColor(SK_ColorRED);
52 paint.setFlags(SkPaint::kAntiAlias_Flag); 67 paint.setFlags(SkPaint::kAntiAlias_Flag);
53 canvas->drawCircle(50, 100, 100, paint); 68 canvas->drawCircle(50, 100, RadiusAt(base_time_, frame_time), paint);
54 canvas->flush(); 69 canvas->flush();
55 70
56 texture_uploader_.Upload(texture.texture_id(), size); 71 texture_uploader_.Upload(texture.texture_id(), size);
72
73 return base::TimeTicks::Now() - start;
74 }
75
76 void GaneshView::BeginFrame(base::TimeTicks frame_time,
77 base::TimeTicks deadline) {
78 scheduler_.UpdateFrameDuration(Draw(frame_time));
79 scheduler_.SetNeedsFrame();
57 } 80 }
58 81
59 } // namespace examples 82 } // namespace examples
OLDNEW
« no previous file with comments | « examples/ganesh_app/ganesh_view.h ('k') | mc/scheduler/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698