| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/android/synchronous_compositor_output_surface.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/auto_reset.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "cc/output/compositor_frame.h" | |
| 17 #include "cc/output/context_provider.h" | |
| 18 #include "cc/output/output_surface_client.h" | |
| 19 #include "cc/output/renderer_settings.h" | |
| 20 #include "cc/output/software_output_device.h" | |
| 21 #include "cc/output/texture_mailbox_deleter.h" | |
| 22 #include "cc/surfaces/display.h" | |
| 23 #include "cc/surfaces/surface_factory.h" | |
| 24 #include "cc/surfaces/surface_id_allocator.h" | |
| 25 #include "cc/surfaces/surface_manager.h" | |
| 26 #include "content/common/android/sync_compositor_messages.h" | |
| 27 #include "content/renderer/android/synchronous_compositor_filter.h" | |
| 28 #include "content/renderer/android/synchronous_compositor_registry.h" | |
| 29 #include "content/renderer/gpu/frame_swap_message_queue.h" | |
| 30 #include "content/renderer/render_thread_impl.h" | |
| 31 #include "gpu/command_buffer/client/context_support.h" | |
| 32 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 33 #include "gpu/command_buffer/common/gpu_memory_allocation.h" | |
| 34 #include "ipc/ipc_message.h" | |
| 35 #include "ipc/ipc_message_macros.h" | |
| 36 #include "ipc/ipc_sender.h" | |
| 37 #include "third_party/skia/include/core/SkCanvas.h" | |
| 38 #include "ui/gfx/geometry/rect_conversions.h" | |
| 39 #include "ui/gfx/skia_util.h" | |
| 40 #include "ui/gfx/transform.h" | |
| 41 | |
| 42 namespace content { | |
| 43 | |
| 44 namespace { | |
| 45 | |
| 46 const int64_t kFallbackTickTimeoutInMilliseconds = 100; | |
| 47 const uint32_t kCompositorClientId = 1; | |
| 48 | |
| 49 // Do not limit number of resources, so use an unrealistically high value. | |
| 50 const size_t kNumResourcesLimit = 10 * 1000 * 1000; | |
| 51 | |
| 52 class SoftwareDevice : public cc::SoftwareOutputDevice { | |
| 53 public: | |
| 54 SoftwareDevice(SkCanvas** canvas) : canvas_(canvas) {} | |
| 55 | |
| 56 void Resize(const gfx::Size& pixel_size, float scale_factor) override { | |
| 57 // Intentional no-op: canvas size is controlled by the embedder. | |
| 58 } | |
| 59 SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override { | |
| 60 DCHECK(*canvas_) << "BeginPaint with no canvas set"; | |
| 61 return *canvas_; | |
| 62 } | |
| 63 void EndPaint() override {} | |
| 64 | |
| 65 private: | |
| 66 SkCanvas** canvas_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice); | |
| 69 }; | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 class SynchronousCompositorOutputSurface::SoftwareOutputSurface | |
| 74 : public cc::OutputSurface { | |
| 75 public: | |
| 76 SoftwareOutputSurface(std::unique_ptr<SoftwareDevice> software_device) | |
| 77 : cc::OutputSurface(nullptr, nullptr, std::move(software_device)) {} | |
| 78 | |
| 79 // cc::OutputSurface implementation. | |
| 80 uint32_t GetFramebufferCopyTextureFormat() override { return 0; } | |
| 81 void SwapBuffers(cc::CompositorFrame frame) override {} | |
| 82 void Reshape(const gfx::Size& size, | |
| 83 float scale_factor, | |
| 84 const gfx::ColorSpace& color_space, | |
| 85 bool has_alpha) override { | |
| 86 // Intentional no-op. Surface size controlled by embedder. | |
| 87 } | |
| 88 | |
| 89 void SetSurfaceSize(const gfx::Size surface_size) { | |
| 90 surface_size_ = surface_size; | |
| 91 } | |
| 92 }; | |
| 93 | |
| 94 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface( | |
| 95 scoped_refptr<cc::ContextProvider> context_provider, | |
| 96 scoped_refptr<cc::ContextProvider> worker_context_provider, | |
| 97 int routing_id, | |
| 98 uint32_t output_surface_id, | |
| 99 std::unique_ptr<cc::BeginFrameSource> begin_frame_source, | |
| 100 SynchronousCompositorRegistry* registry, | |
| 101 scoped_refptr<FrameSwapMessageQueue> frame_swap_message_queue) | |
| 102 : cc::OutputSurface(std::move(context_provider), | |
| 103 std::move(worker_context_provider), | |
| 104 nullptr), | |
| 105 routing_id_(routing_id), | |
| 106 output_surface_id_(output_surface_id), | |
| 107 registry_(registry), | |
| 108 sender_(RenderThreadImpl::current()->sync_compositor_message_filter()), | |
| 109 memory_policy_(0u), | |
| 110 frame_swap_message_queue_(frame_swap_message_queue), | |
| 111 surface_manager_(new cc::SurfaceManager), | |
| 112 surface_id_allocator_(new cc::SurfaceIdAllocator(kCompositorClientId)), | |
| 113 surface_factory_(new cc::SurfaceFactory(surface_manager_.get(), this)), | |
| 114 begin_frame_source_(std::move(begin_frame_source)) { | |
| 115 DCHECK(registry_); | |
| 116 DCHECK(sender_); | |
| 117 DCHECK(begin_frame_source_); | |
| 118 thread_checker_.DetachFromThread(); | |
| 119 capabilities_.adjust_deadline_for_parent = false; | |
| 120 capabilities_.delegated_rendering = true; | |
| 121 memory_policy_.priority_cutoff_when_visible = | |
| 122 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE; | |
| 123 } | |
| 124 | |
| 125 SynchronousCompositorOutputSurface::~SynchronousCompositorOutputSurface() = | |
| 126 default; | |
| 127 | |
| 128 void SynchronousCompositorOutputSurface::SetSyncClient( | |
| 129 SynchronousCompositorOutputSurfaceClient* compositor) { | |
| 130 DCHECK(CalledOnValidThread()); | |
| 131 sync_client_ = compositor; | |
| 132 if (sync_client_) | |
| 133 Send(new SyncCompositorHostMsg_OutputSurfaceCreated(routing_id_)); | |
| 134 } | |
| 135 | |
| 136 bool SynchronousCompositorOutputSurface::OnMessageReceived( | |
| 137 const IPC::Message& message) { | |
| 138 bool handled = true; | |
| 139 IPC_BEGIN_MESSAGE_MAP(SynchronousCompositorOutputSurface, message) | |
| 140 IPC_MESSAGE_HANDLER(SyncCompositorMsg_SetMemoryPolicy, SetMemoryPolicy) | |
| 141 IPC_MESSAGE_HANDLER(SyncCompositorMsg_ReclaimResources, OnReclaimResources) | |
| 142 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 143 IPC_END_MESSAGE_MAP() | |
| 144 return handled; | |
| 145 } | |
| 146 | |
| 147 bool SynchronousCompositorOutputSurface::BindToClient( | |
| 148 cc::OutputSurfaceClient* surface_client) { | |
| 149 DCHECK(CalledOnValidThread()); | |
| 150 if (!cc::OutputSurface::BindToClient(surface_client)) | |
| 151 return false; | |
| 152 | |
| 153 DCHECK(begin_frame_source_); | |
| 154 client_->SetBeginFrameSource(begin_frame_source_.get()); | |
| 155 client_->SetMemoryPolicy(memory_policy_); | |
| 156 client_->SetTreeActivationCallback( | |
| 157 base::Bind(&SynchronousCompositorOutputSurface::DidActivatePendingTree, | |
| 158 base::Unretained(this))); | |
| 159 registry_->RegisterOutputSurface(routing_id_, this); | |
| 160 registered_ = true; | |
| 161 | |
| 162 surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); | |
| 163 surface_manager_->RegisterSurfaceFactoryClient( | |
| 164 surface_id_allocator_->client_id(), this); | |
| 165 | |
| 166 cc::RendererSettings software_renderer_settings; | |
| 167 | |
| 168 std::unique_ptr<SoftwareOutputSurface> output_surface( | |
| 169 new SoftwareOutputSurface( | |
| 170 base::MakeUnique<SoftwareDevice>(¤t_sw_canvas_))); | |
| 171 software_output_surface_ = output_surface.get(); | |
| 172 | |
| 173 // The shared_bitmap_manager and gpu_memory_buffer_manager here are null as | |
| 174 // this Display is only used for resourcesless software draws, where no | |
| 175 // resources are included in the frame swapped from the compositor. So there | |
| 176 // is no need for these. | |
| 177 display_.reset(new cc::Display( | |
| 178 nullptr /* shared_bitmap_manager */, | |
| 179 nullptr /* gpu_memory_buffer_manager */, software_renderer_settings, | |
| 180 nullptr /* begin_frame_source */, std::move(output_surface), | |
| 181 nullptr /* scheduler */, nullptr /* texture_mailbox_deleter */)); | |
| 182 display_->Initialize(&display_client_, surface_manager_.get(), | |
| 183 surface_id_allocator_->client_id()); | |
| 184 display_->SetVisible(true); | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 void SynchronousCompositorOutputSurface::DetachFromClient() { | |
| 189 DCHECK(CalledOnValidThread()); | |
| 190 client_->SetBeginFrameSource(nullptr); | |
| 191 // Destroy the begin frame source on the same thread it was bound on. | |
| 192 begin_frame_source_ = nullptr; | |
| 193 if (registered_) | |
| 194 registry_->UnregisterOutputSurface(routing_id_, this); | |
| 195 client_->SetTreeActivationCallback(base::Closure()); | |
| 196 if (!delegated_surface_id_.is_null()) | |
| 197 surface_factory_->Destroy(delegated_surface_id_); | |
| 198 surface_manager_->UnregisterSurfaceFactoryClient( | |
| 199 surface_id_allocator_->client_id()); | |
| 200 surface_manager_->InvalidateSurfaceClientId( | |
| 201 surface_id_allocator_->client_id()); | |
| 202 software_output_surface_ = nullptr; | |
| 203 display_ = nullptr; | |
| 204 surface_factory_ = nullptr; | |
| 205 surface_id_allocator_ = nullptr; | |
| 206 surface_manager_ = nullptr; | |
| 207 cc::OutputSurface::DetachFromClient(); | |
| 208 CancelFallbackTick(); | |
| 209 } | |
| 210 | |
| 211 void SynchronousCompositorOutputSurface::Reshape( | |
| 212 const gfx::Size& size, | |
| 213 float scale_factor, | |
| 214 const gfx::ColorSpace& color_space, | |
| 215 bool has_alpha) { | |
| 216 // Intentional no-op: surface size is controlled by the embedder. | |
| 217 } | |
| 218 | |
| 219 static void NoOpDrawCallback() {} | |
| 220 | |
| 221 void SynchronousCompositorOutputSurface::SwapBuffers( | |
| 222 cc::CompositorFrame frame) { | |
| 223 DCHECK(CalledOnValidThread()); | |
| 224 DCHECK(sync_client_); | |
| 225 | |
| 226 if (fallback_tick_running_) { | |
| 227 DCHECK(frame.delegated_frame_data->resource_list.empty()); | |
| 228 cc::ReturnedResourceArray return_resources; | |
| 229 ReturnResources(return_resources); | |
| 230 did_swap_ = true; | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 cc::CompositorFrame swap_frame; | |
| 235 | |
| 236 if (in_software_draw_) { | |
| 237 // The frame we send to the client is actually just the metadata. Preserve | |
| 238 // the |frame| for the software path below. | |
| 239 swap_frame.metadata = frame.metadata.Clone(); | |
| 240 | |
| 241 if (delegated_surface_id_.is_null()) { | |
| 242 delegated_surface_id_ = surface_id_allocator_->GenerateId(); | |
| 243 surface_factory_->Create(delegated_surface_id_); | |
| 244 } | |
| 245 | |
| 246 display_->SetSurfaceId(delegated_surface_id_, | |
| 247 frame.metadata.device_scale_factor); | |
| 248 | |
| 249 gfx::Size frame_size = | |
| 250 frame.delegated_frame_data->render_pass_list.back()->output_rect.size(); | |
| 251 display_->Resize(frame_size); | |
| 252 | |
| 253 surface_factory_->SubmitCompositorFrame( | |
| 254 delegated_surface_id_, std::move(frame), base::Bind(&NoOpDrawCallback)); | |
| 255 display_->DrawAndSwap(); | |
| 256 } else { | |
| 257 // For hardware draws we send the whole frame to the client so it can draw | |
| 258 // the content in it. | |
| 259 swap_frame = std::move(frame); | |
| 260 } | |
| 261 | |
| 262 sync_client_->SwapBuffers(output_surface_id_, std::move(swap_frame)); | |
| 263 DeliverMessages(); | |
| 264 did_swap_ = true; | |
| 265 } | |
| 266 | |
| 267 void SynchronousCompositorOutputSurface::CancelFallbackTick() { | |
| 268 fallback_tick_.Cancel(); | |
| 269 fallback_tick_pending_ = false; | |
| 270 } | |
| 271 | |
| 272 void SynchronousCompositorOutputSurface::FallbackTickFired() { | |
| 273 DCHECK(CalledOnValidThread()); | |
| 274 TRACE_EVENT0("renderer", | |
| 275 "SynchronousCompositorOutputSurface::FallbackTickFired"); | |
| 276 base::AutoReset<bool> in_fallback_tick(&fallback_tick_running_, true); | |
| 277 SkBitmap bitmap; | |
| 278 bitmap.allocN32Pixels(1, 1); | |
| 279 bitmap.eraseColor(0); | |
| 280 SkCanvas canvas(bitmap); | |
| 281 fallback_tick_pending_ = false; | |
| 282 DemandDrawSw(&canvas); | |
| 283 } | |
| 284 | |
| 285 void SynchronousCompositorOutputSurface::Invalidate() { | |
| 286 DCHECK(CalledOnValidThread()); | |
| 287 if (sync_client_) | |
| 288 sync_client_->Invalidate(); | |
| 289 | |
| 290 if (!fallback_tick_pending_) { | |
| 291 fallback_tick_.Reset( | |
| 292 base::Bind(&SynchronousCompositorOutputSurface::FallbackTickFired, | |
| 293 base::Unretained(this))); | |
| 294 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 295 FROM_HERE, fallback_tick_.callback(), | |
| 296 base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds)); | |
| 297 fallback_tick_pending_ = true; | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 void SynchronousCompositorOutputSurface::BindFramebuffer() { | |
| 302 // This is a delegating output surface, no framebuffer/direct drawing support. | |
| 303 NOTREACHED(); | |
| 304 } | |
| 305 | |
| 306 uint32_t SynchronousCompositorOutputSurface::GetFramebufferCopyTextureFormat() { | |
| 307 // This is a delegating output surface, no framebuffer/direct drawing support. | |
| 308 NOTREACHED(); | |
| 309 return 0; | |
| 310 } | |
| 311 | |
| 312 void SynchronousCompositorOutputSurface::DemandDrawHw( | |
| 313 const gfx::Size& viewport_size, | |
| 314 const gfx::Rect& viewport_rect_for_tile_priority, | |
| 315 const gfx::Transform& transform_for_tile_priority) { | |
| 316 DCHECK(CalledOnValidThread()); | |
| 317 DCHECK(HasClient()); | |
| 318 DCHECK(context_provider_.get()); | |
| 319 CancelFallbackTick(); | |
| 320 | |
| 321 client_->SetExternalTilePriorityConstraints(viewport_rect_for_tile_priority, | |
| 322 transform_for_tile_priority); | |
| 323 InvokeComposite(gfx::Transform(), gfx::Rect(viewport_size)); | |
| 324 } | |
| 325 | |
| 326 void SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas* canvas) { | |
| 327 DCHECK(CalledOnValidThread()); | |
| 328 DCHECK(canvas); | |
| 329 DCHECK(!current_sw_canvas_); | |
| 330 CancelFallbackTick(); | |
| 331 | |
| 332 base::AutoReset<SkCanvas*> canvas_resetter(¤t_sw_canvas_, canvas); | |
| 333 | |
| 334 SkIRect canvas_clip; | |
| 335 canvas->getClipDeviceBounds(&canvas_clip); | |
| 336 gfx::Rect viewport = gfx::SkIRectToRect(canvas_clip); | |
| 337 | |
| 338 gfx::Transform transform(gfx::Transform::kSkipInitialization); | |
| 339 transform.matrix() = canvas->getTotalMatrix(); // Converts 3x3 matrix to 4x4. | |
| 340 | |
| 341 base::AutoReset<bool> set_in_software_draw(&in_software_draw_, true); | |
| 342 display_->SetExternalViewport(viewport); | |
| 343 display_->SetExternalClip(viewport); | |
| 344 software_output_surface_->SetSurfaceSize( | |
| 345 gfx::SkISizeToSize(canvas->getBaseLayerSize())); | |
| 346 InvokeComposite(transform, viewport); | |
| 347 } | |
| 348 | |
| 349 void SynchronousCompositorOutputSurface::InvokeComposite( | |
| 350 const gfx::Transform& transform, | |
| 351 const gfx::Rect& viewport) { | |
| 352 gfx::Transform adjusted_transform = transform; | |
| 353 adjusted_transform.matrix().postTranslate(-viewport.x(), -viewport.y(), 0); | |
| 354 did_swap_ = false; | |
| 355 client_->OnDraw(adjusted_transform, viewport, in_software_draw_); | |
| 356 | |
| 357 if (did_swap_) { | |
| 358 // This must happen after unwinding the stack and leaving the compositor. | |
| 359 // Usually it is a separate task but we just defer it until OnDraw completes | |
| 360 // instead. | |
| 361 client_->DidSwapBuffersComplete(); | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 void SynchronousCompositorOutputSurface::OnReclaimResources( | |
| 366 uint32_t output_surface_id, | |
| 367 const cc::ReturnedResourceArray& resources) { | |
| 368 // Ignore message if it's a stale one coming from a different output surface | |
| 369 // (e.g. after a lost context). | |
| 370 if (output_surface_id != output_surface_id_) | |
| 371 return; | |
| 372 client_->ReclaimResources(resources); | |
| 373 } | |
| 374 | |
| 375 void SynchronousCompositorOutputSurface::SetMemoryPolicy(size_t bytes_limit) { | |
| 376 DCHECK(CalledOnValidThread()); | |
| 377 bool became_zero = memory_policy_.bytes_limit_when_visible && !bytes_limit; | |
| 378 bool became_non_zero = | |
| 379 !memory_policy_.bytes_limit_when_visible && bytes_limit; | |
| 380 memory_policy_.bytes_limit_when_visible = bytes_limit; | |
| 381 memory_policy_.num_resources_limit = kNumResourcesLimit; | |
| 382 | |
| 383 if (client_) | |
| 384 client_->SetMemoryPolicy(memory_policy_); | |
| 385 | |
| 386 if (became_zero) { | |
| 387 // This is small hack to drop context resources without destroying it | |
| 388 // when this compositor is put into the background. | |
| 389 context_provider()->ContextSupport()->SetAggressivelyFreeResources( | |
| 390 true /* aggressively_free_resources */); | |
| 391 } else if (became_non_zero) { | |
| 392 context_provider()->ContextSupport()->SetAggressivelyFreeResources( | |
| 393 false /* aggressively_free_resources */); | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 void SynchronousCompositorOutputSurface::DidActivatePendingTree() { | |
| 398 DCHECK(CalledOnValidThread()); | |
| 399 if (sync_client_) | |
| 400 sync_client_->DidActivatePendingTree(); | |
| 401 DeliverMessages(); | |
| 402 } | |
| 403 | |
| 404 void SynchronousCompositorOutputSurface::DeliverMessages() { | |
| 405 std::vector<std::unique_ptr<IPC::Message>> messages; | |
| 406 std::unique_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope = | |
| 407 frame_swap_message_queue_->AcquireSendMessageScope(); | |
| 408 frame_swap_message_queue_->DrainMessages(&messages); | |
| 409 for (auto& msg : messages) { | |
| 410 Send(msg.release()); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 bool SynchronousCompositorOutputSurface::Send(IPC::Message* message) { | |
| 415 DCHECK(CalledOnValidThread()); | |
| 416 return sender_->Send(message); | |
| 417 } | |
| 418 | |
| 419 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const { | |
| 420 return thread_checker_.CalledOnValidThread(); | |
| 421 } | |
| 422 | |
| 423 void SynchronousCompositorOutputSurface::ReturnResources( | |
| 424 const cc::ReturnedResourceArray& resources) { | |
| 425 DCHECK(resources.empty()); | |
| 426 client_->ReclaimResources(resources); | |
| 427 } | |
| 428 | |
| 429 void SynchronousCompositorOutputSurface::SetBeginFrameSource( | |
| 430 cc::BeginFrameSource* begin_frame_source) { | |
| 431 // Software output is synchronous and doesn't use a BeginFrameSource. | |
| 432 NOTREACHED(); | |
| 433 } | |
| 434 | |
| 435 } // namespace content | |
| OLD | NEW |