OLD | NEW |
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 "content/browser/android/in_process/synchronous_compositor_output_surfa
ce.h" | 5 #include "content/browser/android/in_process/synchronous_compositor_output_surfa
ce.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "cc/output/compositor_frame.h" | 10 #include "cc/output/compositor_frame.h" |
11 #include "cc/output/compositor_frame_ack.h" | 11 #include "cc/output/compositor_frame_ack.h" |
| 12 #include "cc/output/context_provider.h" |
12 #include "cc/output/output_surface_client.h" | 13 #include "cc/output/output_surface_client.h" |
13 #include "cc/output/software_output_device.h" | 14 #include "cc/output/software_output_device.h" |
14 #include "content/browser/android/in_process/synchronous_compositor_impl.h" | 15 #include "content/browser/android/in_process/synchronous_compositor_impl.h" |
15 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 16 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
16 #include "content/public/browser/android/synchronous_compositor_client.h" | 17 #include "content/public/browser/android/synchronous_compositor_client.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
19 #include "skia/ext/refptr.h" | 20 #include "skia/ext/refptr.h" |
20 #include "third_party/skia/include/core/SkCanvas.h" | 21 #include "third_party/skia/include/core/SkCanvas.h" |
21 #include "third_party/skia/include/core/SkDevice.h" | 22 #include "third_party/skia/include/core/SkDevice.h" |
22 #include "third_party/skia/include/core/SkPicture.h" | 23 #include "third_party/skia/include/core/SkPicture.h" |
23 #include "ui/gfx/rect_conversions.h" | 24 #include "ui/gfx/rect_conversions.h" |
24 #include "ui/gfx/skia_util.h" | 25 #include "ui/gfx/skia_util.h" |
25 #include "ui/gfx/transform.h" | 26 #include "ui/gfx/transform.h" |
26 #include "ui/gl/gl_context.h" | 27 #include "ui/gl/gl_context.h" |
27 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | 28 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
28 | 29 |
29 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | 30 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
30 | 31 |
31 namespace content { | 32 namespace content { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 // TODO(boliu): RenderThreadImpl should create in process contexts as well. | 36 // TODO(boliu): RenderThreadImpl should create in process contexts as well. |
36 scoped_ptr<WebKit::WebGraphicsContext3D> CreateWebGraphicsContext3D() { | 37 scoped_ptr<WebKit::WebGraphicsContext3D> CreateWebGraphicsContext3D() { |
37 if (!CommandLine::ForCurrentProcess()->HasSwitch("testing-webview-gl-mode")) | |
38 return scoped_ptr<WebKit::WebGraphicsContext3D>(); | |
39 | |
40 WebKit::WebGraphicsContext3D::Attributes attributes; | 38 WebKit::WebGraphicsContext3D::Attributes attributes; |
41 attributes.antialias = false; | 39 attributes.antialias = false; |
42 attributes.shareResources = true; | 40 attributes.shareResources = true; |
43 attributes.noAutomaticFlushes = true; | 41 attributes.noAutomaticFlushes = true; |
44 | 42 |
45 return scoped_ptr<WebKit::WebGraphicsContext3D>( | 43 return scoped_ptr<WebKit::WebGraphicsContext3D>( |
46 WebGraphicsContext3DInProcessCommandBufferImpl | 44 WebGraphicsContext3DInProcessCommandBufferImpl |
47 ::CreateViewContext(attributes, NULL)); | 45 ::CreateViewContext(attributes, NULL)); |
48 } | 46 } |
49 | 47 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 SynchronousCompositorOutputSurface* surface_; | 82 SynchronousCompositorOutputSurface* surface_; |
85 SkDevice null_device_; | 83 SkDevice null_device_; |
86 SkCanvas null_canvas_; | 84 SkCanvas null_canvas_; |
87 | 85 |
88 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice); | 86 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice); |
89 }; | 87 }; |
90 | 88 |
91 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface( | 89 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface( |
92 int routing_id) | 90 int routing_id) |
93 : cc::OutputSurface( | 91 : cc::OutputSurface( |
94 CreateWebGraphicsContext3D(), | |
95 scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareDevice(this))), | 92 scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareDevice(this))), |
96 routing_id_(routing_id), | 93 routing_id_(routing_id), |
97 needs_begin_frame_(false), | 94 needs_begin_frame_(false), |
98 did_swap_buffer_(false), | 95 did_swap_buffer_(false), |
99 current_sw_canvas_(NULL) { | 96 current_sw_canvas_(NULL) { |
100 capabilities_.deferred_gl_initialization = true; | 97 capabilities_.deferred_gl_initialization = true; |
101 // Cannot call out to GetDelegate() here as the output surface is not | 98 // Cannot call out to GetDelegate() here as the output surface is not |
102 // constructed on the correct thread. | 99 // constructed on the correct thread. |
103 } | 100 } |
104 | 101 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 if (delegate) | 140 if (delegate) |
144 delegate->SetContinuousInvalidate(needs_begin_frame_); | 141 delegate->SetContinuousInvalidate(needs_begin_frame_); |
145 } | 142 } |
146 | 143 |
147 void SynchronousCompositorOutputSurface::SwapBuffers( | 144 void SynchronousCompositorOutputSurface::SwapBuffers( |
148 const ui::LatencyInfo& info) { | 145 const ui::LatencyInfo& info) { |
149 context3d()->shallowFlushCHROMIUM(); | 146 context3d()->shallowFlushCHROMIUM(); |
150 did_swap_buffer_ = true; | 147 did_swap_buffer_ = true; |
151 } | 148 } |
152 | 149 |
153 bool SynchronousCompositorOutputSurface::IsHwReady() { | |
154 return context3d() != NULL; | |
155 } | |
156 | |
157 namespace { | 150 namespace { |
158 void AdjustTransformForClip(gfx::Transform* transform, gfx::Rect clip) { | 151 void AdjustTransformForClip(gfx::Transform* transform, gfx::Rect clip) { |
159 // The system-provided transform translates us from the screen origin to the | 152 // The system-provided transform translates us from the screen origin to the |
160 // origin of the clip rect, but CC's draw origin starts at the clip. | 153 // origin of the clip rect, but CC's draw origin starts at the clip. |
161 transform->matrix().postTranslate(-clip.x(), -clip.y(), 0); | 154 transform->matrix().postTranslate(-clip.x(), -clip.y(), 0); |
162 } | 155 } |
163 } // namespace | 156 } // namespace |
164 | 157 |
165 bool SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas* canvas) { | 158 bool SynchronousCompositorOutputSurface::InitializeHwDraw() { |
166 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
167 DCHECK(canvas); | 160 DCHECK(client_); |
168 DCHECK(!current_sw_canvas_); | 161 DCHECK(!context3d_); |
169 current_sw_canvas_ = canvas; | |
170 | 162 |
171 SkIRect canvas_clip; | 163 // TODO(boliu): Get a context provider in constructor and pass here. |
172 canvas->getClipDeviceBounds(&canvas_clip); | 164 return InitializeAndSetContext3D(CreateWebGraphicsContext3D().Pass(), |
173 gfx::Rect clip = gfx::SkIRectToRect(canvas_clip); | 165 scoped_refptr<cc::ContextProvider>()); |
174 | |
175 gfx::Transform transform(gfx::Transform::kSkipInitialization); | |
176 transform.matrix() = canvas->getTotalMatrix(); // Converts 3x3 matrix to 4x4. | |
177 AdjustTransformForClip(&transform, clip); | |
178 | |
179 surface_size_ = gfx::Size(canvas->getDeviceSize().width(), | |
180 canvas->getDeviceSize().height()); | |
181 client_->SetExternalDrawConstraints(transform, clip); | |
182 | |
183 InvokeComposite(clip.size()); | |
184 | |
185 bool finished_draw = current_sw_canvas_ == NULL; | |
186 current_sw_canvas_ = NULL; | |
187 return finished_draw; | |
188 } | 166 } |
189 | 167 |
190 bool SynchronousCompositorOutputSurface::DemandDrawHw( | 168 bool SynchronousCompositorOutputSurface::DemandDrawHw( |
191 gfx::Size surface_size, | 169 gfx::Size surface_size, |
192 const gfx::Transform& transform, | 170 const gfx::Transform& transform, |
193 gfx::Rect clip) { | 171 gfx::Rect clip) { |
194 DCHECK(CalledOnValidThread()); | 172 DCHECK(CalledOnValidThread()); |
195 DCHECK(client_); | 173 DCHECK(client_); |
196 DCHECK(context3d()); | 174 DCHECK(context3d()); |
197 | 175 |
198 // Force a GL state restore next time a GLContextVirtual is made current. | 176 // Force a GL state restore next time a GLContextVirtual is made current. |
199 // TODO(boliu): Move this to the end of this function after we have fixed | 177 // TODO(boliu): Move this to the end of this function after we have fixed |
200 // all cases of MakeCurrent calls outside of draws. Tracked in | 178 // all cases of MakeCurrent calls outside of draws. Tracked in |
201 // crbug.com/239856. | 179 // crbug.com/239856. |
202 gfx::GLContext* current_context = gfx::GLContext::GetCurrent(); | 180 gfx::GLContext* current_context = gfx::GLContext::GetCurrent(); |
203 if (current_context) | 181 if (current_context) |
204 current_context->ReleaseCurrent(NULL); | 182 current_context->ReleaseCurrent(NULL); |
205 | 183 |
206 did_swap_buffer_ = false; | 184 did_swap_buffer_ = false; |
207 | 185 |
208 gfx::Transform adjusted_transform = transform; | 186 gfx::Transform adjusted_transform = transform; |
209 AdjustTransformForClip(&adjusted_transform, clip); | 187 AdjustTransformForClip(&adjusted_transform, clip); |
210 surface_size_ = surface_size; | 188 surface_size_ = surface_size; |
211 client_->SetExternalDrawConstraints(adjusted_transform, clip); | 189 client_->SetExternalDrawConstraints(adjusted_transform, clip); |
212 InvokeComposite(clip.size()); | 190 InvokeComposite(clip.size()); |
213 | 191 |
| 192 // TODO(boliu): Check if context is lost here. |
| 193 |
214 return did_swap_buffer_; | 194 return did_swap_buffer_; |
215 } | 195 } |
216 | 196 |
| 197 bool SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas* canvas) { |
| 198 DCHECK(CalledOnValidThread()); |
| 199 DCHECK(canvas); |
| 200 DCHECK(!current_sw_canvas_); |
| 201 current_sw_canvas_ = canvas; |
| 202 |
| 203 SkIRect canvas_clip; |
| 204 canvas->getClipDeviceBounds(&canvas_clip); |
| 205 gfx::Rect clip = gfx::SkIRectToRect(canvas_clip); |
| 206 |
| 207 gfx::Transform transform(gfx::Transform::kSkipInitialization); |
| 208 transform.matrix() = canvas->getTotalMatrix(); // Converts 3x3 matrix to 4x4. |
| 209 AdjustTransformForClip(&transform, clip); |
| 210 |
| 211 surface_size_ = gfx::Size(canvas->getDeviceSize().width(), |
| 212 canvas->getDeviceSize().height()); |
| 213 client_->SetExternalDrawConstraints(transform, clip); |
| 214 |
| 215 InvokeComposite(clip.size()); |
| 216 |
| 217 bool finished_draw = current_sw_canvas_ == NULL; |
| 218 current_sw_canvas_ = NULL; |
| 219 return finished_draw; |
| 220 } |
| 221 |
217 void SynchronousCompositorOutputSurface::InvokeComposite( | 222 void SynchronousCompositorOutputSurface::InvokeComposite( |
218 gfx::Size damage_size) { | 223 gfx::Size damage_size) { |
219 client_->SetNeedsRedrawRect(gfx::Rect(damage_size)); | 224 client_->SetNeedsRedrawRect(gfx::Rect(damage_size)); |
220 if (needs_begin_frame_) | 225 if (needs_begin_frame_) |
221 client_->BeginFrame(base::TimeTicks::Now()); | 226 client_->BeginFrame(base::TimeTicks::Now()); |
222 } | 227 } |
223 | 228 |
224 // Not using base::NonThreadSafe as we want to enforce a more exacting threading | 229 // Not using base::NonThreadSafe as we want to enforce a more exacting threading |
225 // requirement: SynchronousCompositorOutputSurface() must only be used on the UI | 230 // requirement: SynchronousCompositorOutputSurface() must only be used on the UI |
226 // thread. | 231 // thread. |
227 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const { | 232 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const { |
228 return BrowserThread::CurrentlyOn(BrowserThread::UI); | 233 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
229 } | 234 } |
230 | 235 |
231 SynchronousCompositorOutputSurfaceDelegate* | 236 SynchronousCompositorOutputSurfaceDelegate* |
232 SynchronousCompositorOutputSurface::GetDelegate() { | 237 SynchronousCompositorOutputSurface::GetDelegate() { |
233 return SynchronousCompositorImpl::FromRoutingID(routing_id_); | 238 return SynchronousCompositorImpl::FromRoutingID(routing_id_); |
234 } | 239 } |
235 | 240 |
236 } // namespace content | 241 } // namespace content |
OLD | NEW |