summaryrefslogtreecommitdiffstats
path: root/content/browser/compositor/onscreen_display_client.cc
blob: f7b6fcd359e0526e27ed4800d833f90ead318cd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/compositor/onscreen_display_client.h"

#include "base/debug/trace_event.h"
#include "cc/output/output_surface.h"
#include "cc/surfaces/surface_factory.h"
#include "cc/surfaces/surface_manager.h"
#include "content/browser/compositor/surface_display_output_surface.h"
#include "content/common/host_shared_bitmap_manager.h"

namespace content {

OnscreenDisplayClient::OnscreenDisplayClient(
    scoped_ptr<cc::OutputSurface> output_surface,
    cc::SurfaceManager* manager,
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : output_surface_(output_surface.Pass()),
      display_(
          new cc::Display(this, manager, HostSharedBitmapManager::current())),
      task_runner_(task_runner),
      scheduled_draw_(false),
      deferred_draw_(false),
      pending_frames_(0),
      weak_ptr_factory_(this) {
}

OnscreenDisplayClient::~OnscreenDisplayClient() {
}

scoped_ptr<cc::OutputSurface> OnscreenDisplayClient::CreateOutputSurface() {
  DCHECK(output_surface_.get());
  return output_surface_.Pass();
}

void OnscreenDisplayClient::CommitVSyncParameters(base::TimeTicks timebase,
                                                  base::TimeDelta interval) {
  surface_display_output_surface_->ReceivedVSyncParameters(timebase, interval);
}

void OnscreenDisplayClient::DisplayDamaged() {
  if (scheduled_draw_ || deferred_draw_)
    return;
  TRACE_EVENT0("content", "OnscreenDisplayClient::DisplayDamaged");
  if (pending_frames_ >= display_->GetMaxFramesPending()) {
    deferred_draw_ = true;
  } else {
    ScheduleDraw();
  }
}

void OnscreenDisplayClient::ScheduleDraw() {
  DCHECK(!deferred_draw_);
  DCHECK(!scheduled_draw_);
  scheduled_draw_ = true;
  task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&OnscreenDisplayClient::Draw, weak_ptr_factory_.GetWeakPtr()));
}

void OnscreenDisplayClient::Draw() {
  TRACE_EVENT0("content", "OnscreenDisplayClient::Draw");
  scheduled_draw_ = false;
  display_->Draw();
}

void OnscreenDisplayClient::DidSwapBuffers() {
  pending_frames_++;
}

void OnscreenDisplayClient::DidSwapBuffersComplete() {
  pending_frames_--;
  if ((pending_frames_ < display_->GetMaxFramesPending()) && deferred_draw_) {
    deferred_draw_ = false;
    ScheduleDraw();
  }
}

}  // namespace content