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

Side by Side Diff: ui/ozone/demo/ozone_demo.cc

Issue 416893002: ozone: demo: Add software support & use message loop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « ui/ozone/demo/egl_demo.cc ('k') | ui/ozone/demo/ozone_demos.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/timer/timer.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/gfx/geometry/size.h"
13 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_context.h"
15 #include "ui/gl/gl_surface.h"
16 #include "ui/ozone/public/ozone_platform.h"
17 #include "ui/ozone/public/surface_factory_ozone.h"
18 #include "ui/ozone/public/surface_ozone_canvas.h"
19 #include "ui/platform_window/platform_window.h"
20 #include "ui/platform_window/platform_window_delegate.h"
21
22 const int kTestWindowWidth = 800;
23 const int kTestWindowHeight = 600;
24
25 const int kFrameDelayMilliseconds = 16;
26
27 const int kAnimationSteps = 240;
28
29 const char kDisableGpu[] = "disable-gpu";
30
31 class DemoWindow : public ui::PlatformWindowDelegate {
32 public:
33 DemoWindow() : widget_(gfx::kNullAcceleratedWidget), iteration_(0) {
34 platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
35 this, gfx::Rect(kTestWindowWidth, kTestWindowHeight));
36 }
37 virtual ~DemoWindow() {}
38
39 gfx::AcceleratedWidget GetAcceleratedWidget() {
40 // TODO(spang): We should start rendering asynchronously.
41 CHECK_NE(widget_, gfx::kNullAcceleratedWidget)
42 << "widget not available synchronously";
43 return widget_;
44 }
45
46 gfx::Size GetSize() { return platform_window_->GetBounds().size(); }
47
48 void Start() {
49 if (!CommandLine::ForCurrentProcess()->HasSwitch(kDisableGpu) &&
50 gfx::GLSurface::InitializeOneOff() && InitializeGLSurface())
51 StartAnimationGL();
52 else if (InitializeSoftwareSurface())
53 StartAnimationSoftware();
54 else
55 LOG(ERROR) << "failed to create drawing surface";
dnicoara 2014/07/24 17:12:03 nit: Could we have all the messages formatted cons
spang 2014/07/24 17:39:14 Done.
56 }
57
58 // PlatformWindowDelegate:
59 virtual void OnBoundsChanged(const gfx::Rect& new_bounds) OVERRIDE {}
60 virtual void OnDamageRect(const gfx::Rect& damaged_region) OVERRIDE {}
61 virtual void DispatchEvent(ui::Event* event) OVERRIDE {}
62 virtual void OnCloseRequest() OVERRIDE {
63 StopAnimation();
64 base::MessageLoop::current()->PostTask(
65 FROM_HERE, base::Bind(&base::DeletePointer<DemoWindow>, this));
66 }
67 virtual void OnClosed() OVERRIDE {}
68 virtual void OnWindowStateChanged(
69 ui::PlatformWindowState new_state) OVERRIDE {}
70 virtual void OnLostCapture() OVERRIDE {}
71 virtual void OnAcceleratedWidgetAvailable(
72 gfx::AcceleratedWidget widget) OVERRIDE {
73 CHECK_NE(widget, gfx::kNullAcceleratedWidget);
74 widget_ = widget;
75 }
76 virtual void OnActivationChanged(bool active) OVERRIDE {}
77
78 private:
79 bool InitializeGLSurface() {
80 surface_ = gfx::GLSurface::CreateViewGLSurface(GetAcceleratedWidget());
81 if (!surface_) {
82 LOG(ERROR) << "failed to create GL surface";
83 return false;
84 }
85
86 context_ = gfx::GLContext::CreateGLContext(
87 NULL, surface_.get(), gfx::PreferIntegratedGpu);
88 if (!context_) {
89 LOG(ERROR) << "failed to create GL context";
90 surface_ = NULL;
91 return false;
92 }
93
94 surface_->Resize(GetSize());
95
96 if (!context_->MakeCurrent(surface_.get())) {
97 LOG(ERROR) << "failed to make GL context current";
98 surface_ = NULL;
99 context_ = NULL;
100 return false;
101 }
102
103 return true;
104 }
105
106 bool InitializeSoftwareSurface() {
107 software_surface_ =
108 ui::SurfaceFactoryOzone::GetInstance()->CreateCanvasForWidget(
109 GetAcceleratedWidget());
110 if (!software_surface_) {
111 LOG(ERROR) << "failed to create software surface";
112 return false;
113 }
114
115 software_surface_->ResizeCanvas(GetSize());
116 return true;
117 }
118
119 void StartAnimationGL() {
120 timer_.Start(FROM_HERE,
121 base::TimeDelta::FromMicroseconds(kFrameDelayMilliseconds),
122 this,
123 &DemoWindow::RenderFrameGL);
124 }
125
126 void StartAnimationSoftware() {
127 timer_.Start(FROM_HERE,
128 base::TimeDelta::FromMicroseconds(kFrameDelayMilliseconds),
129 this,
130 &DemoWindow::RenderFrameSoftware);
131 }
132
133 void StopAnimation() { timer_.Stop(); }
134
135 float NextFraction() {
136 float fraction = (sinf(iteration_ * 2 * M_PI / kAnimationSteps) + 1) / 2;
137
138 iteration_++;
139 iteration_ %= kAnimationSteps;
140
141 return fraction;
142 }
143
144 void RenderFrameGL() {
145 float fraction = NextFraction();
146 gfx::Size window_size = GetSize();
147
148 glViewport(0, 0, window_size.width(), window_size.height());
149 glClearColor(1 - fraction, fraction, 0.0, 1.0);
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151
152 if (!surface_->SwapBuffers())
153 LOG(FATAL) << "Failed to swap buffers";
154 }
155
156 void RenderFrameSoftware() {
157 float fraction = NextFraction();
158 gfx::Size window_size = GetSize();
159
160 skia::RefPtr<SkCanvas> canvas = software_surface_->GetCanvas();
161
162 SkColor color =
163 SkColorSetARGB(0xff, 0, 0xff * fraction, 0xff * (1 - fraction));
164
165 canvas->clear(color);
166
167 software_surface_->PresentCanvas(gfx::Rect(window_size));
168 }
169
170 // Timer for animation.
171 base::RepeatingTimer<DemoWindow> timer_;
172
173 // Bits for GL rendering.
174 scoped_refptr<gfx::GLSurface> surface_;
175 scoped_refptr<gfx::GLContext> context_;
176
177 // Bits for software rendeirng.
178 scoped_ptr<ui::SurfaceOzoneCanvas> software_surface_;
179
180 // Window-related state.
181 scoped_ptr<ui::PlatformWindow> platform_window_;
182 gfx::AcceleratedWidget widget_;
183
184 // Animation state.
185 int iteration_;
186
187 DISALLOW_COPY_AND_ASSIGN(DemoWindow);
188 };
189
190 int main(int argc, char** argv) {
191 CommandLine::Init(argc, argv);
192 base::AtExitManager exit_manager;
193
194 base::MessageLoopForUI message_loop;
195
196 ui::OzonePlatform::InitializeForUI();
197
198 DemoWindow* window = new DemoWindow;
199 window->Start();
200
201 base::RunLoop run_loop;
202 run_loop.RunUntilIdle();
203
204 return 0;
205 }
OLDNEW
« no previous file with comments | « ui/ozone/demo/egl_demo.cc ('k') | ui/ozone/demo/ozone_demos.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698