OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 "content/renderer/android/synchronous_compositor_output_surface.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "base/time.h" | |
10 #include "cc/output/compositor_frame.h" | |
11 #include "cc/output/compositor_frame_ack.h" | |
12 #include "cc/output/output_surface_client.h" | |
13 #include "cc/output/software_output_device.h" | |
14 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | |
15 #include "content/public/common/content_switches.h" | |
16 #include "content/public/renderer/android/synchronous_compositor_client.h" | |
17 #include "content/public/renderer/content_renderer_client.h" | |
18 #include "skia/ext/refptr.h" | |
19 #include "third_party/skia/include/core/SkCanvas.h" | |
20 #include "third_party/skia/include/core/SkDevice.h" | |
21 #include "third_party/skia/include/core/SkPicture.h" | |
22 #include "ui/gfx/rect_conversions.h" | |
23 #include "ui/gfx/skia_util.h" | |
24 #include "ui/gfx/transform.h" | |
25 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | |
26 | |
27 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | |
28 | |
29 namespace content { | |
30 | |
31 namespace { | |
32 | |
33 // TODO(boliu): RenderThreadImpl should create in process contexts as well. | |
34 scoped_ptr<WebKit::WebGraphicsContext3D> CreateWebGraphicsContext3D() { | |
35 if (!CommandLine::ForCurrentProcess()->HasSwitch("testing-webview-gl-mode")) | |
36 return scoped_ptr<WebKit::WebGraphicsContext3D>(); | |
37 | |
38 WebKit::WebGraphicsContext3D::Attributes attributes; | |
39 attributes.antialias = false; | |
40 attributes.shareResources = true; | |
41 attributes.noAutomaticFlushes = true; | |
42 | |
43 return scoped_ptr<WebKit::WebGraphicsContext3D>( | |
44 WebGraphicsContext3DInProcessCommandBufferImpl | |
45 ::CreateViewContext(attributes, NULL)); | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 class SynchronousCompositorOutputSurface::SoftwareDevice | |
51 : public cc::SoftwareOutputDevice { | |
52 public: | |
53 SoftwareDevice(SynchronousCompositorOutputSurface* surface) | |
54 : surface_(surface), | |
55 null_device_(SkBitmap::kARGB_8888_Config, 1, 1), | |
56 null_canvas_(&null_device_) { | |
57 } | |
58 virtual void Resize(gfx::Size size) OVERRIDE { | |
59 // Intentional no-op: canvas size is controlled by the embedder. | |
60 } | |
61 virtual SkCanvas* BeginPaint(gfx::Rect damage_rect) OVERRIDE { | |
62 DCHECK(surface_->current_sw_canvas_); | |
63 if (surface_->current_sw_canvas_) | |
64 return surface_->current_sw_canvas_; | |
65 return &null_canvas_; | |
66 } | |
67 virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE { | |
68 surface_->current_sw_canvas_ = NULL; | |
69 } | |
70 virtual void CopyToBitmap(gfx::Rect rect, SkBitmap* output) OVERRIDE { | |
71 NOTIMPLEMENTED(); | |
72 } | |
73 virtual void Scroll(gfx::Vector2d delta, | |
74 gfx::Rect clip_rect) OVERRIDE { | |
75 NOTIMPLEMENTED(); | |
76 } | |
77 virtual void ReclaimDIB(const TransportDIB::Id& id) OVERRIDE { | |
78 NOTIMPLEMENTED(); | |
79 } | |
80 | |
81 private: | |
82 SynchronousCompositorOutputSurface* surface_; | |
83 SkDevice null_device_; | |
84 SkCanvas null_canvas_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice); | |
87 }; | |
88 | |
89 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface( | |
90 SynchronousCompositorOutputSurfaceDelegate* delegate) | |
91 : cc::OutputSurface( | |
92 CreateWebGraphicsContext3D(), | |
93 scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareDevice(this))), | |
94 delegate_(delegate), | |
95 needs_begin_frame_(false), | |
96 did_swap_buffer_(false), | |
97 current_sw_canvas_(NULL) { | |
98 capabilities_.deferred_gl_initialization = true; | |
99 } | |
100 | |
101 SynchronousCompositorOutputSurface::~SynchronousCompositorOutputSurface() { | |
102 DCHECK(CalledOnValidThread()); | |
103 delegate_->DidDestroySynchronousOutputSurface(); | |
104 } | |
105 | |
106 bool SynchronousCompositorOutputSurface::ForcedDrawToSoftwareDevice() const { | |
107 return current_sw_canvas_ != NULL; | |
108 } | |
109 | |
110 bool SynchronousCompositorOutputSurface::BindToClient( | |
111 cc::OutputSurfaceClient* surface_client) { | |
112 DCHECK(CalledOnValidThread()); | |
113 if (!cc::OutputSurface::BindToClient(surface_client)) | |
114 return false; | |
115 delegate_->DidCreateSynchronousOutputSurface(); | |
116 return true; | |
117 } | |
118 | |
119 void SynchronousCompositorOutputSurface::Reshape( | |
120 gfx::Size size, float scale_factor) { | |
121 // Intentional no-op: surface size is controlled by the embedder. | |
122 } | |
123 | |
124 void SynchronousCompositorOutputSurface::SendFrameToParentCompositor( | |
125 cc::CompositorFrame* frame) { | |
126 NOTREACHED(); | |
127 // TODO(joth): Route page scale to the client, see http://crbug.com/237006 | |
128 } | |
129 | |
130 void SynchronousCompositorOutputSurface::SetNeedsBeginFrame( | |
131 bool enable) { | |
132 DCHECK(CalledOnValidThread()); | |
133 needs_begin_frame_ = enable; | |
134 delegate_->SetContinuousInvalidate(needs_begin_frame_); | |
135 } | |
136 | |
137 void SynchronousCompositorOutputSurface::SwapBuffers( | |
138 const ui::LatencyInfo& info) { | |
139 context3d()->shallowFlushCHROMIUM(); | |
140 did_swap_buffer_ = true; | |
141 } | |
142 | |
143 bool SynchronousCompositorOutputSurface::IsHwReady() { | |
144 return context3d() != NULL; | |
145 } | |
146 | |
147 bool SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas* canvas) { | |
148 DCHECK(CalledOnValidThread()); | |
149 DCHECK(canvas); | |
150 DCHECK(!current_sw_canvas_); | |
151 current_sw_canvas_ = canvas; | |
152 | |
153 SkRect canvas_clip; | |
154 gfx::Rect damage_area; | |
155 if (canvas->getClipBounds(&canvas_clip)) { | |
156 damage_area = gfx::ToEnclosedRect(gfx::SkRectToRectF(canvas_clip)); | |
157 } else { | |
158 damage_area = gfx::Rect(kint16max, kint16max); | |
159 } | |
160 | |
161 gfx::Transform transform; | |
162 transform.matrix() = canvas->getTotalMatrix(); // Converts 3x3 matrix to 4x4. | |
163 | |
164 InvokeComposite(transform, damage_area); | |
165 | |
166 bool finished_draw = current_sw_canvas_ == NULL; | |
167 current_sw_canvas_ = NULL; | |
168 return finished_draw; | |
169 } | |
170 | |
171 bool SynchronousCompositorOutputSurface::DemandDrawHw( | |
172 gfx::Size view_size, | |
173 const gfx::Transform& transform, | |
174 gfx::Rect damage_area) { | |
175 DCHECK(CalledOnValidThread()); | |
176 DCHECK(client_); | |
177 | |
178 did_swap_buffer_ = false; | |
179 | |
180 InvokeComposite(transform, damage_area); | |
181 | |
182 return did_swap_buffer_; | |
183 } | |
184 | |
185 void SynchronousCompositorOutputSurface::InvokeComposite( | |
186 const gfx::Transform& transform, | |
187 gfx::Rect damage_area) { | |
188 // TODO(boliu): This assumes |transform| is identity and |damage_area| is the | |
189 // whole view. Tracking bug to implement this: crbug.com/230463. | |
190 client_->SetNeedsRedrawRect(damage_area); | |
191 if (needs_begin_frame_) | |
192 client_->BeginFrame(base::TimeTicks::Now()); | |
193 } | |
194 | |
195 // Not using base::NonThreadSafe as we want to enforce a more exacting threading | |
196 // requirement: SynchronousCompositorOutputSurface() must only be used by | |
197 // embedders that supply their own compositor loop via | |
198 // OverrideCompositorMessageLoop(). | |
199 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const { | |
200 return base::MessageLoop::current() && (base::MessageLoop::current() == | |
201 GetContentClient()->renderer()->OverrideCompositorMessageLoop()); | |
202 } | |
203 | |
204 } // namespace content | |
OLD | NEW |