Home | History | Annotate | Download | only in compositor
      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 "content/browser/compositor/onscreen_display_client.h"
      6 
      7 #include "base/debug/trace_event.h"
      8 #include "cc/output/output_surface.h"
      9 #include "cc/surfaces/surface_factory.h"
     10 #include "cc/surfaces/surface_manager.h"
     11 #include "content/browser/compositor/surface_display_output_surface.h"
     12 #include "content/common/host_shared_bitmap_manager.h"
     13 
     14 namespace content {
     15 
     16 OnscreenDisplayClient::OnscreenDisplayClient(
     17     scoped_ptr<cc::OutputSurface> output_surface,
     18     cc::SurfaceManager* manager,
     19     scoped_refptr<base::SingleThreadTaskRunner> task_runner)
     20     : output_surface_(output_surface.Pass()),
     21       display_(
     22           new cc::Display(this, manager, HostSharedBitmapManager::current())),
     23       task_runner_(task_runner),
     24       scheduled_draw_(false),
     25       deferred_draw_(false),
     26       pending_frames_(0),
     27       weak_ptr_factory_(this) {
     28 }
     29 
     30 OnscreenDisplayClient::~OnscreenDisplayClient() {
     31 }
     32 
     33 scoped_ptr<cc::OutputSurface> OnscreenDisplayClient::CreateOutputSurface() {
     34   DCHECK(output_surface_.get());
     35   return output_surface_.Pass();
     36 }
     37 
     38 void OnscreenDisplayClient::CommitVSyncParameters(base::TimeTicks timebase,
     39                                                   base::TimeDelta interval) {
     40   surface_display_output_surface_->ReceivedVSyncParameters(timebase, interval);
     41 }
     42 
     43 void OnscreenDisplayClient::DisplayDamaged() {
     44   if (scheduled_draw_ || deferred_draw_)
     45     return;
     46   TRACE_EVENT0("content", "OnscreenDisplayClient::DisplayDamaged");
     47   if (pending_frames_ >= display_->GetMaxFramesPending()) {
     48     deferred_draw_ = true;
     49   } else {
     50     ScheduleDraw();
     51   }
     52 }
     53 
     54 void OnscreenDisplayClient::ScheduleDraw() {
     55   DCHECK(!deferred_draw_);
     56   DCHECK(!scheduled_draw_);
     57   scheduled_draw_ = true;
     58   task_runner_->PostTask(
     59       FROM_HERE,
     60       base::Bind(&OnscreenDisplayClient::Draw, weak_ptr_factory_.GetWeakPtr()));
     61 }
     62 
     63 void OnscreenDisplayClient::Draw() {
     64   TRACE_EVENT0("content", "OnscreenDisplayClient::Draw");
     65   scheduled_draw_ = false;
     66   display_->Draw();
     67 }
     68 
     69 void OnscreenDisplayClient::DidSwapBuffers() {
     70   pending_frames_++;
     71 }
     72 
     73 void OnscreenDisplayClient::DidSwapBuffersComplete() {
     74   pending_frames_--;
     75   if ((pending_frames_ < display_->GetMaxFramesPending()) && deferred_draw_) {
     76     deferred_draw_ = false;
     77     ScheduleDraw();
     78   }
     79 }
     80 
     81 }  // namespace content
     82