summaryrefslogtreecommitdiffstats
path: root/cc/output/output_surface.cc
blob: 83127900acd331bfafeba1867114b38740d2c30c (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2013 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 "cc/output/output_surface.h"

#include <set>
#include <string>
#include <vector>

#include "base/logging.h"
#include "base/string_util.h"
#include "base/strings/string_split.h"
#include "cc/output/output_surface_client.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"

using std::set;
using std::string;
using std::vector;

namespace cc {

class OutputSurfaceCallbacks :
  public WebKit::WebGraphicsContext3D::WebGraphicsSwapBuffersCompleteCallbackCHROMIUM,
  public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
 public:
   explicit OutputSurfaceCallbacks(OutputSurfaceClient* client)
     : client_(client) {}

   // WK:WGC3D::WGSwapBuffersCompleteCallbackCHROMIUM implementation.
   virtual void onSwapBuffersComplete() {
     client_->OnSwapBuffersComplete();
   }

   // WK:WGC3D::WGContextLostCallback implementation.
   virtual void onContextLost() {
     client_->DidLoseOutputSurface();
   }

 private:
  OutputSurfaceClient* client_;
};

OutputSurface::OutputSurface(
    scoped_ptr<WebKit::WebGraphicsContext3D> context3d)
    : client_(NULL),
      context3d_(context3d.Pass()),
      has_gl_discard_backbuffer_(false) {
}

OutputSurface::OutputSurface(
    scoped_ptr<cc::SoftwareOutputDevice> software_device)
    : client_(NULL),
      software_device_(software_device.Pass()),
      has_gl_discard_backbuffer_(false) {
}

OutputSurface::OutputSurface(
    scoped_ptr<WebKit::WebGraphicsContext3D> context3d,
    scoped_ptr<cc::SoftwareOutputDevice> software_device)
    : client_(NULL),
      context3d_(context3d.Pass()),
      software_device_(software_device.Pass()),
      has_gl_discard_backbuffer_(false) {
}

OutputSurface::~OutputSurface() {
}

bool OutputSurface::BindToClient(
    cc::OutputSurfaceClient* client) {
  DCHECK(client);
  client_ = client;
  if (!context3d_)
    return true;
  if (!context3d_->makeContextCurrent())
    return false;

  string extensions_string = UTF16ToASCII(context3d_->getString(GL_EXTENSIONS));
  vector<string> extensions_list;
  base::SplitString(extensions_string, ' ', &extensions_list);
  set<string> extensions(extensions_list.begin(), extensions_list.end());

  has_gl_discard_backbuffer_ =
      extensions.count("GL_CHROMIUM_discard_backbuffer") > 0;

  callbacks_.reset(new OutputSurfaceCallbacks(client_));
  context3d_->setSwapBuffersCompleteCallbackCHROMIUM(callbacks_.get());
  context3d_->setContextLostCallback(callbacks_.get());

  return true;
}

void OutputSurface::SendFrameToParentCompositor(CompositorFrame* frame) {
  NOTIMPLEMENTED();
}

void OutputSurface::EnsureBackbuffer() {
  DCHECK(context3d_);
  if (has_gl_discard_backbuffer_)
    context3d_->ensureBackbufferCHROMIUM();
}

void OutputSurface::DiscardBackbuffer() {
  DCHECK(context3d_);
  if (has_gl_discard_backbuffer_)
    context3d_->discardBackbufferCHROMIUM();
}

void OutputSurface::Reshape(gfx::Size size) {
  DCHECK(context3d_);
  context3d_->reshape(size.width(), size.height());
}

void OutputSurface::BindFramebuffer() {
  DCHECK(context3d_);
  context3d_->bindFramebuffer(GL_FRAMEBUFFER, 0);
}

void OutputSurface::SwapBuffers(const LatencyInfo& latency_info) {
  DCHECK(context3d_);
  // Note that currently this has the same effect as SwapBuffers; we should
  // consider exposing a different entry point on WebGraphicsContext3D.
  context3d_->prepareTexture();
}

void OutputSurface::PostSubBuffer(gfx::Rect rect,
                                  const LatencyInfo& latency_info) {
  DCHECK(context3d_);
  context3d_->postSubBufferCHROMIUM(
      rect.x(), rect.y(), rect.width(), rect.height());
}

}  // namespace cc