Commit 0f308937 authored by Yuri Wiitala's avatar Yuri Wiitala Committed by Commit Bot

GLScaler: Add shader programs and tests.

Refactors and cleans-up the code related to the GLHelperScalar shader
programs and adds it to GLScaler. Also, unit testing of the shader
programs was non-existent (only higher-level integration testing was
being done), so much of this CL is about adding testing for each of
them to confirm correct behaviors.

Bug: 870036
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel
Change-Id: I7714b7cf6ebef2cd2778ba146728e6d225b1467a
Reviewed-on: https://chromium-review.googlesource.com/1214662
Commit-Queue: Yuri Wiitala <miu@chromium.org>
Reviewed-by: default avatarXiangjun Zhang <xjz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#591118}
parent 2338e7cc
...@@ -217,6 +217,9 @@ viz_source_set("unit_tests") { ...@@ -217,6 +217,9 @@ viz_source_set("unit_tests") {
"frame_sinks/copy_output_util_unittest.cc", "frame_sinks/copy_output_util_unittest.cc",
"frame_sinks/delay_based_time_source_unittest.cc", "frame_sinks/delay_based_time_source_unittest.cc",
"gl_helper_unittest.cc", "gl_helper_unittest.cc",
"gl_scaler_shader_pixeltest.cc",
"gl_scaler_test_util.cc",
"gl_scaler_test_util.h",
"gl_scaler_unittest.cc", "gl_scaler_unittest.cc",
"gpu/context_cache_controller_unittest.cc", "gpu/context_cache_controller_unittest.cc",
"quads/draw_quad_unittest.cc", "quads/draw_quad_unittest.cc",
...@@ -243,6 +246,9 @@ viz_source_set("unit_tests") { ...@@ -243,6 +246,9 @@ viz_source_set("unit_tests") {
"//media", "//media",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
"//ui/gfx",
"//ui/gfx:color_space",
"//ui/gfx/geometry",
] ]
} }
......
...@@ -22,7 +22,7 @@ specific_include_rules = { ...@@ -22,7 +22,7 @@ specific_include_rules = {
"+gpu/ipc/common", "+gpu/ipc/common",
"+third_party/skia", "+third_party/skia",
], ],
".*_unittest\.cc": [ ".*(_unittest|_pixeltest)\.cc": [
"+cc/test", "+cc/test",
"+gpu/ipc/gl_in_process_context.h", "+gpu/ipc/gl_in_process_context.h",
"+media/base", "+media/base",
......
This diff is collapsed.
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#include <stdint.h> #include <stdint.h>
#include <map>
#include <memory>
#include <tuple>
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
...@@ -17,8 +21,10 @@ ...@@ -17,8 +21,10 @@
#include "ui/gfx/geometry/vector2d.h" #include "ui/gfx/geometry/vector2d.h"
namespace gfx { namespace gfx {
class ColorTransform;
class Vector2dF; class Vector2dF;
class Rect; class Rect;
class RectF;
class Size; class Size;
} // namespace gfx } // namespace gfx
...@@ -266,11 +272,99 @@ class VIZ_COMMON_EXPORT GLScaler : public ContextLostObserver { ...@@ -266,11 +272,99 @@ class VIZ_COMMON_EXPORT GLScaler : public ContextLostObserver {
const gfx::Vector2d& to); const gfx::Vector2d& to);
private: private:
friend class GLScalerShaderPixelTest;
using GLES2Interface = gpu::gles2::GLES2Interface; using GLES2Interface = gpu::gles2::GLES2Interface;
enum Axis { HORIZONTAL, VERTICAL };
// The shaders used by each stage in the scaling pipeline.
enum class Shader : int8_t {
BILINEAR,
BILINEAR2,
BILINEAR3,
BILINEAR4,
BILINEAR2X2,
BICUBIC_UPSCALE,
BICUBIC_HALF_1D,
PLANAR_CHANNEL_0,
PLANAR_CHANNEL_1,
PLANAR_CHANNEL_2,
PLANAR_CHANNEL_3,
I422_NV61_MRT,
DEINTERLEAVE_PAIRWISE_MRT,
};
// A cached, re-usable shader program that performs one step in the scaling
// pipeline.
class VIZ_COMMON_EXPORT ShaderProgram {
public:
ShaderProgram(GLES2Interface* gl,
Shader shader,
GLint texture_format,
const gfx::ColorTransform* color_transform,
const GLenum swizzle[2]);
~ShaderProgram();
Shader shader() const { return shader_; }
GLint texture_format() const { return texture_format_; }
// UseProgram must be called with GL_ARRAY_BUFFER bound to a vertex
// attribute buffer. |src_texture_size| is the size of the entire source
// texture, regardless of which region is to be sampled. |src_rect| is the
// source region, not including overscan pixels past the edges.
// |primary_axis| configures certain programs which scale in only one
// particular direction. |flip_y| causes the |src_rect| to be scanned
// upside-down, to produce a vertically-flipped result.
void UseProgram(const gfx::Size& src_texture_size,
const gfx::RectF& src_rect,
const gfx::Size& dst_size,
Axis primary_axis,
bool flip_y);
// GL_ARRAY_BUFFER data that must be bound when drawing with a
// ShaderProgram. These are the vertex attributes that will sweep the entire
// source area when executing the program. They represent triangle strip
// coordinates: The first two columns are (x,y) values interpolated to
// produce the vertex coordinates in object space, while the latter two
// columns are (s,t) values interpolated to produce the texture coordinates
// that correspond to the vertex coordinates.
static const GLfloat kVertexAttributes[16];
private:
GLES2Interface* const gl_;
const Shader shader_;
const GLint texture_format_;
// A program for copying a source texture into a destination texture.
const GLuint program_;
// The location of the position in the program.
GLint position_location_ = -1;
// The location of the texture coordinate in the program.
GLint texcoord_location_ = -1;
// The location of the source texture in the program.
GLint texture_location_ = -1;
// The location of the texture coordinate of the source rectangle in the
// program.
GLint src_rect_location_ = -1;
// Location of size of source image in pixels.
GLint src_pixelsize_location_ = -1;
// Location of vector for scaling ratio between source and dest textures.
GLint scaling_vector_location_ = -1;
DISALLOW_COPY_AND_ASSIGN(ShaderProgram);
};
// ContextLostObserver implementation. // ContextLostObserver implementation.
void OnContextLost() final; void OnContextLost() final;
// Returns a cached ShaderProgram, creating one on-demand if necessary.
ShaderProgram* GetShaderProgram(Shader shader,
GLint texture_format,
const gfx::ColorTransform* color_transform,
const GLenum swizzle[2]);
// The provider of the GL context. This is non-null while the GL context is // The provider of the GL context. This is non-null while the GL context is
// valid and GLScaler is observing for context loss. // valid and GLScaler is observing for context loss.
scoped_refptr<ContextProvider> context_provider_; scoped_refptr<ContextProvider> context_provider_;
...@@ -282,6 +376,13 @@ class VIZ_COMMON_EXPORT GLScaler : public ContextLostObserver { ...@@ -282,6 +376,13 @@ class VIZ_COMMON_EXPORT GLScaler : public ContextLostObserver {
// GetMaxDrawBuffersSupported(). -1 means "not yet known." // GetMaxDrawBuffersSupported(). -1 means "not yet known."
mutable int max_draw_buffers_ = -1; mutable int max_draw_buffers_ = -1;
// Cache of ShaderPrograms. The cache key consists of fields that correspond
// to the arguments of GetShaderProgram(): the shader, the texture format, the
// source and output color spaces (color transform), and the two swizzles.
using ShaderCacheKey = std::
tuple<Shader, GLint, gfx::ColorSpace, gfx::ColorSpace, GLenum, GLenum>;
std::map<ShaderCacheKey, ShaderProgram> shader_programs_;
DISALLOW_COPY_AND_ASSIGN(GLScaler); DISALLOW_COPY_AND_ASSIGN(GLScaler);
}; };
......
This diff is collapsed.
This diff is collapsed.
// Copyright 2018 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 COMPONENTS_VIZ_COMMON_GL_SCALER_TEST_UTIL_H_
#define COMPONENTS_VIZ_COMMON_GL_SCALER_TEST_UTIL_H_
#include <stdint.h>
#include <vector>
#include "base/macros.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/color_transform.h"
#include "ui/gfx/geometry/size.h"
namespace gfx {
class Rect;
}
namespace viz {
// A collection of utility functions used in the GLScaler-related pixel tests.
class GLScalerTestUtil {
public:
struct ColorBar {
SkIRect rect;
SkColor color;
};
// The patterns that can be created by CreateCyclicalTestImage().
enum CyclicalPattern {
HORIZONTAL_STRIPES,
VERTICAL_STRIPES,
STAGGERED,
};
// Returns an SkBitmap with pixels allocated, having a RGBA format with
// unpremultiplied alpha.
static SkBitmap AllocateRGBABitmap(const gfx::Size& size);
// Returns a |value| in the range [0.0,1.0] as an unsigned integer in the
// range [0,255].
static uint8_t ToClamped255(float value);
// Return the SMPTE color bars that make up a test image, scaled to an image
// of the given |size|.
static std::vector<ColorBar> GetScaledSMPTEColorBars(const gfx::Size& size);
// Create a SMPTE color bar test image, scaled to the given |size|.
static SkBitmap CreateSMPTETestImage(const gfx::Size& size);
// Returns true if the given |image| looks similar-enough to a scaled version
// of a SMPTE color bar test image that was originally of |src_size| and
// cropped to |src_rect|. |fuzzy_pixels| is used to ignore a border of pixels
// surrounding each color bar, since the scaling algorithms may blend
// in-between colors where the bars touch. |max_color_diff| controls what
// "similar-enough" means: 0 for "exact," or otherwise some positive value
// specifying the maximum color value difference; and is updated with the
// the actual maximum.
static bool LooksLikeSMPTETestImage(const SkBitmap& image,
const gfx::Size& src_size,
const gfx::Rect& src_rect,
int fuzzy_pixels,
int* max_color_diff);
// Returns an image of the given |size| with the colors in |cycle| used to
// generate a striped or staggered |pattern|. |rotation| specifies which index
// in the |cycle| to start with.
static SkBitmap CreateCyclicalTestImage(const gfx::Size& size,
CyclicalPattern pattern,
const std::vector<SkColor>& cycle,
size_t rotation);
// Returns the RGB/YUV color spaces used by default when color space
// conversion is requested.
static gfx::ColorSpace DefaultRGBColorSpace();
static gfx::ColorSpace DefaultYUVColorSpace();
// Performs an in-place transform of the given |image| from
// DefaultRGBColorSpace() to DefaultYUVColorSpace(). The color channels (plus
// one alpha) remain interleaved (i.e., no pixel blending or format transform
// is being done).
static void ConvertBitmapToYUV(SkBitmap* image);
// Performs an in-place swizzling of the red and blue color channels in the
// given |image|.
static void SwizzleBitmap(SkBitmap* image);
// Returns a bitmap consisting of one color channel from every 4 pixels in a
// |source| bitmap packed into a single quad. Thus, the resulting bitmap will
// have 1/4 the width of the source bitmap. This is used to create the
// expected output of the single-channel export shaders, and thus can be
// considered a reference implementation of that.
static SkBitmap CreatePackedPlanarBitmap(const SkBitmap& source, int channel);
// The area and color of the bars in a 1920x1080 HD SMPTE color bars test
// image (https://commons.wikimedia.org/wiki/File:SMPTE_Color_Bars_16x9.svg).
// The gray linear gradient bar is defined as half solid 0-level black and
// half solid full-intensity white).
static const ColorBar kSMPTEColorBars[30];
static constexpr gfx::Size kSMPTEFullSize = gfx::Size(1920, 1080);
#ifdef SK_CPU_BENDIAN
// Bit shift offsets (within a uint32_t RGBA quad) to access each color
// channel's byte.
static constexpr int kRedShift = 24;
static constexpr int kGreenShift = 16;
static constexpr int kBlueShift = 8;
static constexpr int kAlphaShift = 0;
#else
static constexpr int kRedShift = 0;
static constexpr int kGreenShift = 8;
static constexpr int kBlueShift = 16;
static constexpr int kAlphaShift = 24;
#endif
};
// A helper for tests to create textures, and download/upload RGBA textures
// to/from SkBitmaps. All textures created by this helper will be deleted when
// its destructor is invoked.
class GLScalerTestTextureHelper {
public:
// |gl| context must outlive this instance.
explicit GLScalerTestTextureHelper(gpu::gles2::GLES2Interface* gl);
~GLScalerTestTextureHelper();
// Creates a fully-defined RGBA texture of the given |size|, returning its GL
// name.
GLuint CreateTexture(const gfx::Size& size);
// Uploads the given RGBA |bitmap| to the GPU into a new texture, returning
// its GL name.
GLuint UploadTexture(const SkBitmap& bitmap);
// Reads-back the |texture| which is of the given |size|, returning the result
// as a RGBA SkBitmap.
SkBitmap DownloadTexture(GLuint texture, const gfx::Size& size);
private:
gpu::gles2::GLES2Interface* const gl_;
std::vector<GLuint> textures_to_delete_;
DISALLOW_COPY_AND_ASSIGN(GLScalerTestTextureHelper);
};
} // namespace viz
#endif // COMPONENTS_VIZ_COMMON_GL_SCALER_TEST_UTIL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment