summaryrefslogtreecommitdiffstats
path: root/cc/output/texture_copier.cc
blob: 1ad0ef0d19e068ed0ed02350620bc7cab828f1cb (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
// Copyright 2012 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/texture_copier.h"

#include "base/debug/trace_event.h"
#include "build/build_config.h"
#include "cc/output/gl_renderer.h"  // For the GLC() macro.
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"

namespace cc {

AcceleratedTextureCopier::AcceleratedTextureCopier(
    WebKit::WebGraphicsContext3D* context,
    bool using_bind_uniforms,
    int highp_threshold_min)
    : context_(context)
    , using_bind_uniforms_(using_bind_uniforms)
    , highp_threshold_min_(highp_threshold_min) {
  DCHECK(context_);
  GLC(context_, fbo_ = context_->createFramebuffer());
  GLC(context_, position_buffer_ = context_->createBuffer());

  static const float kPositions[4][4] = { { -1, -1, 0, 1 }, { 1, -1, 0, 1 },
                                          { 1, 1, 0, 1 }, { -1, 1, 0, 1 } };

  GLC(context_, context_->bindBuffer(GL_ARRAY_BUFFER, position_buffer_));
  GLC(context_,
      context_->bufferData(
          GL_ARRAY_BUFFER, sizeof(kPositions), kPositions, GL_STATIC_DRAW));
  GLC(context_, context_->bindBuffer(GL_ARRAY_BUFFER, 0));

  blit_program_.reset(new BlitProgram(context_, TexCoordPrecisionMedium));
  blit_program_highp_.reset(new BlitProgram(context_, TexCoordPrecisionHigh));
}

AcceleratedTextureCopier::~AcceleratedTextureCopier() {
  if (blit_program_)
    blit_program_->Cleanup(context_);
  if (blit_program_highp_)
    blit_program_highp_->Cleanup(context_);
  if (position_buffer_)
    GLC(context_, context_->deleteBuffer(position_buffer_));
  if (fbo_)
    GLC(context_, context_->deleteFramebuffer(fbo_));
}

void AcceleratedTextureCopier::CopyTexture(Parameters parameters) {
  TRACE_EVENT0("cc", "TextureCopier::CopyTexture");

  GLC(context_, context_->disable(GL_SCISSOR_TEST));

  // Note: this code does not restore the viewport, bound program, 2D texture,
  // framebuffer, buffer or blend enable.
  GLC(context_, context_->bindFramebuffer(GL_FRAMEBUFFER, fbo_));
  GLC(context_,
      context_->framebufferTexture2D(GL_FRAMEBUFFER,
                                     GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_2D,
                                     parameters.dest_texture,
                                     0));

#if defined(OS_ANDROID)
  // Clear destination to improve performance on tiling GPUs.
  // TODO(epenner): Use EXT_discard_framebuffer or skip clearing if it isn't
  // available.
  GLC(context_, context_->clear(GL_COLOR_BUFFER_BIT));
#endif

  GLC(context_,
      context_->bindTexture(GL_TEXTURE_2D, parameters.source_texture));
  GLC(context_,
      context_->texParameteri(
          GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
  GLC(context_,
      context_->texParameteri(
          GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));

  TexCoordPrecision texCoordPrecision = TexCoordPrecisionRequired(
      context_, highp_threshold_min_, parameters.size);
  if (texCoordPrecision == TexCoordPrecisionHigh) {
    if (!blit_program_highp_->initialized())
      blit_program_highp_->Initialize(context_, using_bind_uniforms_);

    // TODO(danakj): Use EXT_framebuffer_blit if available.
    GLC(context_, context_->useProgram(blit_program_highp_->program()));
  } else {
    if (!blit_program_->initialized())
      blit_program_->Initialize(context_, using_bind_uniforms_);

    // TODO(danakj: Use EXT_framebuffer_blit if available.
    GLC(context_, context_->useProgram(blit_program_->program()));
  }

  const int kPositionAttribute = 0;
  GLC(context_, context_->bindBuffer(GL_ARRAY_BUFFER, position_buffer_));
  GLC(context_,
      context_->vertexAttribPointer(
          kPositionAttribute, 4, GL_FLOAT, false, 0, 0));
  GLC(context_, context_->enableVertexAttribArray(kPositionAttribute));
  GLC(context_, context_->bindBuffer(GL_ARRAY_BUFFER, 0));

  GLC(context_,
      context_->viewport(
          0, 0, parameters.size.width(), parameters.size.height()));
  GLC(context_, context_->disable(GL_BLEND));
  GLC(context_, context_->drawArrays(GL_TRIANGLE_FAN, 0, 4));

  GLC(context_,
      context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
  GLC(context_,
      context_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
  GLC(context_, context_->disableVertexAttribArray(kPositionAttribute));

  GLC(context_, context_->useProgram(0));

  GLC(context_,
      context_->framebufferTexture2D(
          GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0));
  GLC(context_, context_->bindFramebuffer(GL_FRAMEBUFFER, 0));
  GLC(context_, context_->bindTexture(GL_TEXTURE_2D, 0));

  GLC(context_, context_->enable(GL_SCISSOR_TEST));
}

void AcceleratedTextureCopier::Flush() {
  GLC(context_, context_->flush());
}

}  // namespace cc