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

Side by Side Diff: examples/sample_app/gles2_client_impl.cc

Issue 940293003: Add a Display and ContextProvider concept to mojom, use to recreate (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/sample_app/gles2_client_impl.h" 5 #include "examples/sample_app/gles2_client_impl.h"
6 6
7 #include <GLES2/gl2.h> 7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2ext.h> 8 #include <GLES2/gl2ext.h>
9 #include <math.h> 9 #include <math.h>
10 #include <stdlib.h> 10 #include <stdlib.h>
(...skipping 10 matching lines...) Expand all
21 return hypot(static_cast<float>(start.x - end.x), 21 return hypot(static_cast<float>(start.x - end.x),
22 static_cast<float>(start.y - end.y)); 22 static_cast<float>(start.y - end.y));
23 } 23 }
24 24
25 float GetRandomColor() { 25 float GetRandomColor() {
26 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); 26 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
27 } 27 }
28 28
29 } 29 }
30 30
31 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) 31 GLES2ClientImpl::GLES2ClientImpl(mojo::ContextProviderPtr context_provider)
32 : last_time_(mojo::GetTimeTicksNow()), waiting_to_draw_(false) { 32 : last_time_(mojo::GetTimeTicksNow()),
33 context_ = 33 waiting_to_draw_(false),
34 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), 34 context_provider_(context_provider.Pass()),
35 &ContextLostThunk, 35 context_(nullptr) {
36 this, 36 context_provider_->Create(nullptr,
37 mojo::Environment::GetDefaultAsyncWaiter()); 37 [this](mojo::CommandBufferPtr command_buffer) {
38 MojoGLES2MakeCurrent(context_); 38 ContextCreated(command_buffer.Pass());
39 });
39 } 40 }
40 41
41 GLES2ClientImpl::~GLES2ClientImpl() { 42 GLES2ClientImpl::~GLES2ClientImpl() {
42 MojoGLES2DestroyContext(context_); 43 MojoGLES2DestroyContext(context_);
43 } 44 }
44 45
45 void GLES2ClientImpl::SetSize(const mojo::Size& size) { 46 void GLES2ClientImpl::SetSize(const mojo::Size& size) {
46 size_ = size; 47 size_ = size;
47 if (size_.width == 0 || size_.height == 0) 48 cube_.set_size(size_.width, size_.height);
49 if (size_.width == 0 || size_.height == 0 || !context_)
48 return; 50 return;
49 static_cast<gpu::gles2::GLES2Interface*>( 51 static_cast<gpu::gles2::GLES2Interface*>(
50 MojoGLES2GetGLES2Interface(context_))->ResizeCHROMIUM(size_.width, 52 MojoGLES2GetGLES2Interface(context_))->ResizeCHROMIUM(size_.width,
51 size_.height, 53 size_.height,
52 1); 54 1);
53 cube_.Init(size_.width, size_.height);
54 WantToDraw(); 55 WantToDraw();
55 } 56 }
56 57
57 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { 58 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) {
58 switch (event.action) { 59 switch (event.action) {
59 case mojo::EVENT_TYPE_MOUSE_PRESSED: 60 case mojo::EVENT_TYPE_MOUSE_PRESSED:
60 case mojo::EVENT_TYPE_TOUCH_PRESSED: 61 case mojo::EVENT_TYPE_TOUCH_PRESSED:
61 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) 62 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)
62 break; 63 break;
63 capture_point_ = *event.location_data->in_view_location; 64 capture_point_ = *event.location_data->in_view_location;
(...skipping 29 matching lines...) Expand all
93 94
94 capture_point_ = last_drag_point_ = mojo::Point(); 95 capture_point_ = last_drag_point_ = mojo::Point();
95 WantToDraw(); 96 WantToDraw();
96 break; 97 break;
97 } 98 }
98 default: 99 default:
99 break; 100 break;
100 } 101 }
101 } 102 }
102 103
104 void GLES2ClientImpl::ContextCreated(mojo::CommandBufferPtr command_buffer) {
105 context_ = MojoGLES2CreateContext(
106 command_buffer.PassMessagePipe().release().value(), &ContextLostThunk,
107 this, mojo::Environment::GetDefaultAsyncWaiter());
108 MojoGLES2MakeCurrent(context_);
109 cube_.Init();
110 WantToDraw();
111 }
112
103 void GLES2ClientImpl::ContextLost() { 113 void GLES2ClientImpl::ContextLost() {
114 MojoGLES2DestroyContext(context_);
115 context_ = nullptr;
116 context_provider_->Create(nullptr,
117 [this](mojo::CommandBufferPtr command_buffer) {
118 ContextCreated(command_buffer.Pass());
119 });
104 } 120 }
105 121
106 void GLES2ClientImpl::ContextLostThunk(void* closure) { 122 void GLES2ClientImpl::ContextLostThunk(void* closure) {
107 static_cast<GLES2ClientImpl*>(closure)->ContextLost(); 123 static_cast<GLES2ClientImpl*>(closure)->ContextLost();
108 } 124 }
109 125
110 struct DrawRunnable {
111 explicit DrawRunnable(GLES2ClientImpl* impl) : impl(impl) {}
112 virtual ~DrawRunnable() {}
113
114 void Run() const { impl->Draw(); }
115
116 GLES2ClientImpl* impl;
117 };
118
119 void GLES2ClientImpl::WantToDraw() { 126 void GLES2ClientImpl::WantToDraw() {
120 if (waiting_to_draw_) 127 if (waiting_to_draw_ || !context_)
121 return; 128 return;
122 waiting_to_draw_ = true; 129 waiting_to_draw_ = true;
123 mojo::RunLoop::current()->PostDelayedTask(mojo::Closure(DrawRunnable(this)), 130 mojo::RunLoop::current()->PostDelayedTask([this]() { Draw(); },
124 MojoTimeTicks(16667)); 131 MojoTimeTicks(16667));
qsr 2015/02/20 11:25:47 You can lose the context between this post task an
jamesr 2015/02/20 22:01:05 fixed
125 } 132 }
126 133
127 void GLES2ClientImpl::Draw() { 134 void GLES2ClientImpl::Draw() {
128 waiting_to_draw_ = false; 135 waiting_to_draw_ = false;
129 MojoTimeTicks now = mojo::GetTimeTicksNow(); 136 MojoTimeTicks now = mojo::GetTimeTicksNow();
130 MojoTimeTicks offset = now - last_time_; 137 MojoTimeTicks offset = now - last_time_;
131 float delta = static_cast<float>(offset) / 1000000.; 138 float delta = static_cast<float>(offset) / 1000000.;
132 last_time_ = now; 139 last_time_ = now;
133 cube_.UpdateForTimeDelta(delta); 140 cube_.UpdateForTimeDelta(delta);
134 cube_.Draw(); 141 cube_.Draw();
135 142
136 MojoGLES2SwapBuffers(); 143 MojoGLES2SwapBuffers();
137 WantToDraw(); 144 WantToDraw();
138 } 145 }
139 146
140 } // namespace examples 147 } // namespace examples
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698