summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/compositing_iosurface_shader_programs_mac.h
blob: de1987f2e0383754ce145f7a3dc9f75339693fe1 (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 (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.

#ifndef CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_SHADER_PROGRAMS_MAC_H_
#define CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_SHADER_PROGRAMS_MAC_H_

#include <OpenGL/gl.h>

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"

namespace content {

// Provides caching of the compile-and-link step for shader programs at runtime
// since, once compiled and linked, the programs can be shared.  Callers invoke
// one of the UseXXX() methods to glUseProgram() the program and have its
// uniform variables bound with the given parameters.
//
// Note: All public methods must be invoked within the the same GL context!
class CompositingIOSurfaceShaderPrograms {
 public:
  CompositingIOSurfaceShaderPrograms();
  ~CompositingIOSurfaceShaderPrograms();

  // Reset the cache, deleting any references to currently-cached shader
  // programs.  This must be called within an active OpenGL context just before
  // destruction.
  void Reset();

  // Begin using the "blit" program, which is set up to sample the texture at
  // GL_TEXTURE_0.  Returns false on error.
  bool UseBlitProgram();

  // Begin using the program that just draws solid white very efficiently.
  // Returns false on error.
  bool UseSolidWhiteProgram();

  // Begin using one of the two RGB-to-YV12 color conversion programs, as
  // specified by |pass_number| 1 or 2.  The programs will sample the texture at
  // GL_TEXTURE0, and account for scaling in the X direction by |texel_scale_x|.
  // Returns false on error.
  bool UseRGBToYV12Program(int pass_number, float texel_scale_x);

  // |format| argument to use for glReadPixels() when reading back textures
  // generated by the RGBToYV12 program.
  GLenum rgb_to_yv12_output_format() const {
    return rgb_to_yv12_output_format_;
  }

 protected:
  FRIEND_TEST_ALL_PREFIXES(CompositingIOSurfaceTransformerTest,
                           TransformsRGBToYV12);

  // Side effect: Calls Reset(), deleting any cached programs.
  void SetOutputFormatForTesting(GLenum format);

 private:
  enum { kNumShaderPrograms = 4 };

  // Helper methods to cache uniform variable locations.
  GLuint GetShaderProgram(int which);
  void BindUniformTextureVariable(int which, int texture_unit_offset);
  void BindUniformTexelScaleXVariable(int which, float texel_scale_x);

  // Cached values for previously-compiled/linked shader programs, and the
  // locations of their uniform variables.
  GLuint shader_programs_[kNumShaderPrograms];
  GLint texture_var_locations_[kNumShaderPrograms];
  GLint texel_scale_x_var_locations_[kNumShaderPrograms];

  // Byte order of the quads generated by the RGBToYV12 shader program.  Must
  // always be GL_BGRA (default) or GL_RGBA (workaround case).
  GLenum rgb_to_yv12_output_format_;

  DISALLOW_COPY_AND_ASSIGN(CompositingIOSurfaceShaderPrograms);
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_SHADER_PROGRAMS_MAC_H_