Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 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 "cc/surfaces/onscreen_display_client.h" |
| 6 | |
| 7 | #include "base/trace_event/trace_event.h" |
| 8 | #include "cc/output/output_surface.h" |
| 9 | #include "cc/surfaces/surface_display_output_surface.h" |
| 10 | #include "cc/surfaces/surface_factory.h" |
| 11 | #include "cc/surfaces/surface_manager.h" |
| 12 | |
| 13 | namespace cc { |
| 14 | |
| 15 | OnscreenDisplayClient::OnscreenDisplayClient( |
| 16 | scoped_ptr<OutputSurface> output_surface, |
| 17 | SurfaceManager* manager, |
| 18 | SharedBitmapManager* bitmap_manager, |
| 19 | gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, |
| 20 | const RendererSettings& settings, |
| 21 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 22 | : output_surface_(output_surface.Pass()), |
| 23 | display_(new Display(this, |
| 24 | manager, |
| 25 | bitmap_manager, |
| 26 | gpu_memory_buffer_manager, |
| 27 | settings)), |
| 28 | task_runner_(task_runner), |
| 29 | scheduled_draw_(false), |
| 30 | output_surface_lost_(false), |
| 31 | deferred_draw_(false), |
| 32 | pending_frames_(0), |
| 33 | weak_ptr_factory_(this) { |
| 34 | } |
| 35 | |
| 36 | OnscreenDisplayClient::~OnscreenDisplayClient() { |
| 37 | } |
| 38 | |
| 39 | bool OnscreenDisplayClient::Initialize() { |
| 40 | return display_->Initialize(output_surface_.Pass()); |
| 41 | } |
| 42 | |
| 43 | void OnscreenDisplayClient::CommitVSyncParameters(base::TimeTicks timebase, |
| 44 | base::TimeDelta interval) { |
| 45 | surface_display_output_surface_->ReceivedVSyncParameters(timebase, interval); |
| 46 | } |
| 47 | |
| 48 | void OnscreenDisplayClient::DisplayDamaged() { |
| 49 | if (scheduled_draw_ || deferred_draw_) |
| 50 | return; |
| 51 | TRACE_EVENT0("content", "OnscreenDisplayClient::DisplayDamaged"); |
| 52 | if (pending_frames_ >= display_->GetMaxFramesPending()) { |
| 53 | deferred_draw_ = true; |
| 54 | } else { |
| 55 | ScheduleDraw(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void OnscreenDisplayClient::ScheduleDraw() { |
| 60 | DCHECK(!deferred_draw_); |
| 61 | DCHECK(!scheduled_draw_); |
| 62 | scheduled_draw_ = true; |
| 63 | task_runner_->PostTask(FROM_HERE, base::Bind(&OnscreenDisplayClient::Draw, |
| 64 | weak_ptr_factory_.GetWeakPtr())); |
| 65 | } |
| 66 | |
| 67 | void OnscreenDisplayClient::OutputSurfaceLost() { |
| 68 | output_surface_lost_ = true; |
| 69 | surface_display_output_surface_->DidLoseOutputSurface(); |
| 70 | } |
| 71 | |
| 72 | void OnscreenDisplayClient::Draw() { |
| 73 | TRACE_EVENT0("content", "OnscreenDisplayClient::Draw"); |
| 74 | if (output_surface_lost_) |
| 75 | return; |
| 76 | scheduled_draw_ = false; |
| 77 | display_->Draw(); |
| 78 | } |
| 79 | |
| 80 | void OnscreenDisplayClient::DidSwapBuffers() { |
| 81 | pending_frames_++; |
| 82 | } |
| 83 | |
| 84 | void OnscreenDisplayClient::DidSwapBuffersComplete() { |
| 85 | pending_frames_--; |
| 86 | if ((pending_frames_ < display_->GetMaxFramesPending()) && deferred_draw_) { |
| 87 | deferred_draw_ = false; |
| 88 | ScheduleDraw(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void OnscreenDisplayClient::SetMemoryPolicy(const ManagedMemoryPolicy& policy) { |
| 93 | surface_display_output_surface_->SetMemoryPolicy(policy); |
| 94 | } |
| 95 | |
| 96 | } // namespace cc |