Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 
     28 #if USE(ACCELERATED_COMPOSITING)
     29 
     30 #include "ShaderChromium.h"
     31 
     32 #include "GraphicsContext.h"
     33 #include "GraphicsContext3D.h"
     34 
     35 #define SHADER0(Src) #Src
     36 #define SHADER(Src) SHADER0(Src)
     37 
     38 namespace WebCore {
     39 
     40 VertexShaderPosTex::VertexShaderPosTex()
     41     : m_matrixLocation(-1)
     42 {
     43 }
     44 
     45 bool VertexShaderPosTex::init(GraphicsContext3D* context, unsigned program)
     46 {
     47     m_matrixLocation = context->getUniformLocation(program, "matrix");
     48     return m_matrixLocation != -1;
     49 }
     50 
     51 String VertexShaderPosTex::getShaderString() const
     52 {
     53     return SHADER(
     54         attribute vec4 a_position;
     55         attribute vec2 a_texCoord;
     56         uniform mat4 matrix;
     57         varying vec2 v_texCoord;
     58         void main()
     59         {
     60             gl_Position = matrix * a_position;
     61             v_texCoord = a_texCoord;
     62         }
     63     );
     64 }
     65 
     66 VertexShaderPosTexYUVStretch::VertexShaderPosTexYUVStretch()
     67     : m_matrixLocation(-1)
     68     , m_yWidthScaleFactorLocation(-1)
     69     , m_uvWidthScaleFactorLocation(-1)
     70 {
     71 }
     72 
     73 bool VertexShaderPosTexYUVStretch::init(GraphicsContext3D* context, unsigned program)
     74 {
     75     m_matrixLocation = context->getUniformLocation(program, "matrix");
     76     m_yWidthScaleFactorLocation = context->getUniformLocation(program, "y_widthScaleFactor");
     77     m_uvWidthScaleFactorLocation = context->getUniformLocation(program, "uv_widthScaleFactor");
     78     return m_matrixLocation != -1 && m_yWidthScaleFactorLocation != -1 && m_uvWidthScaleFactorLocation != -1;
     79 }
     80 
     81 String VertexShaderPosTexYUVStretch::getShaderString() const
     82 {
     83     return SHADER(
     84         precision mediump float;
     85         attribute vec4 a_position;
     86         attribute vec2 a_texCoord;
     87         uniform mat4 matrix;
     88         varying vec2 y_texCoord;
     89         varying vec2 uv_texCoord;
     90         uniform float y_widthScaleFactor;
     91         uniform float uv_widthScaleFactor;
     92         void main()
     93         {
     94             gl_Position = matrix * a_position;
     95             y_texCoord = vec2(y_widthScaleFactor * a_texCoord.x, a_texCoord.y);
     96             uv_texCoord = vec2(uv_widthScaleFactor * a_texCoord.x, a_texCoord.y);
     97         }
     98     );
     99 }
    100 
    101 VertexShaderPos::VertexShaderPos()
    102     : m_matrixLocation(-1)
    103 {
    104 }
    105 
    106 bool VertexShaderPos::init(GraphicsContext3D* context, unsigned program)
    107 {
    108     m_matrixLocation = context->getUniformLocation(program, "matrix");
    109     return m_matrixLocation != -1;
    110 }
    111 
    112 String VertexShaderPos::getShaderString() const
    113 {
    114     return SHADER(
    115         attribute vec4 a_position;
    116         uniform mat4 matrix;
    117         void main()
    118         {
    119             gl_Position = matrix * a_position;
    120         }
    121     );
    122 }
    123 
    124 VertexShaderPosTexTransform::VertexShaderPosTexTransform()
    125     : m_matrixLocation(-1)
    126     , m_texTransformLocation(-1)
    127 {
    128 }
    129 
    130 bool VertexShaderPosTexTransform::init(GraphicsContext3D* context, unsigned program)
    131 {
    132     m_matrixLocation = context->getUniformLocation(program, "matrix");
    133     m_texTransformLocation = context->getUniformLocation(program, "texTransform");
    134     return m_matrixLocation != -1 && m_texTransformLocation != -1;
    135 }
    136 
    137 String VertexShaderPosTexTransform::getShaderString() const
    138 {
    139     return SHADER(
    140         attribute vec4 a_position;
    141         attribute vec2 a_texCoord;
    142         uniform mat4 matrix;
    143         uniform vec4 texTransform;
    144         varying vec2 v_texCoord;
    145         void main()
    146         {
    147             gl_Position = matrix * a_position;
    148             v_texCoord = a_texCoord * texTransform.zw + texTransform.xy;
    149         }
    150     );
    151 }
    152 
    153 FragmentTexAlphaBinding::FragmentTexAlphaBinding()
    154     : m_samplerLocation(-1)
    155     , m_alphaLocation(-1)
    156 {
    157 }
    158 
    159 bool FragmentTexAlphaBinding::init(GraphicsContext3D* context, unsigned program)
    160 {
    161     m_samplerLocation = context->getUniformLocation(program, "s_texture");
    162     m_alphaLocation = context->getUniformLocation(program, "alpha");
    163 
    164     return m_samplerLocation != -1 && m_alphaLocation != -1;
    165 }
    166 
    167 String FragmentShaderRGBATexFlipAlpha::getShaderString() const
    168 {
    169     return SHADER(
    170         precision mediump float;
    171         varying vec2 v_texCoord;
    172         uniform sampler2D s_texture;
    173         uniform float alpha;
    174         void main()
    175         {
    176             vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texCoord.y));
    177             gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha;
    178         }
    179     );
    180 }
    181 
    182 String FragmentShaderRGBATexAlpha::getShaderString() const
    183 {
    184     return SHADER(
    185         precision mediump float;
    186         varying vec2 v_texCoord;
    187         uniform sampler2D s_texture;
    188         uniform float alpha;
    189         void main()
    190         {
    191             vec4 texColor = texture2D(s_texture, v_texCoord);
    192             gl_FragColor = texColor * alpha;
    193         }
    194     );
    195 }
    196 
    197 String FragmentShaderBGRATexAlpha::getShaderString() const
    198 {
    199     return SHADER(
    200         precision mediump float;
    201         varying vec2 v_texCoord;
    202         uniform sampler2D s_texture;
    203         uniform float alpha;
    204         void main()
    205         {
    206             vec4 texColor = texture2D(s_texture, v_texCoord);
    207             gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha;
    208         }
    209     );
    210 }
    211 
    212 FragmentShaderRGBATexAlphaMask::FragmentShaderRGBATexAlphaMask()
    213     : m_samplerLocation(-1)
    214     , m_maskSamplerLocation(-1)
    215     , m_alphaLocation(-1)
    216 {
    217 }
    218 
    219 bool FragmentShaderRGBATexAlphaMask::init(GraphicsContext3D* context, unsigned program)
    220 {
    221     m_samplerLocation = context->getUniformLocation(program, "s_texture");
    222     m_maskSamplerLocation = context->getUniformLocation(program, "s_mask");
    223     m_alphaLocation = context->getUniformLocation(program, "alpha");
    224 
    225     return m_samplerLocation != -1 && m_maskSamplerLocation != -1 && m_alphaLocation != -1;
    226 }
    227 
    228 String FragmentShaderRGBATexAlphaMask::getShaderString() const
    229 {
    230     return SHADER(
    231         precision mediump float;
    232         varying vec2 v_texCoord;
    233         uniform sampler2D s_texture;
    234         uniform sampler2D s_mask;
    235         uniform float alpha;
    236         void main()
    237         {
    238             vec4 texColor = texture2D(s_texture, v_texCoord);
    239             vec4 maskColor = texture2D(s_mask, v_texCoord);
    240             gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha * maskColor.w;
    241         }
    242     );
    243 }
    244 
    245 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
    246     : m_yTextureLocation(-1)
    247     , m_uTextureLocation(-1)
    248     , m_vTextureLocation(-1)
    249     , m_alphaLocation(-1)
    250     , m_ccMatrixLocation(-1)
    251     , m_yuvAdjLocation(-1)
    252 {
    253 }
    254 
    255 bool FragmentShaderYUVVideo::init(GraphicsContext3D* context, unsigned program)
    256 {
    257     m_yTextureLocation = context->getUniformLocation(program, "y_texture");
    258     m_uTextureLocation = context->getUniformLocation(program, "u_texture");
    259     m_vTextureLocation = context->getUniformLocation(program, "v_texture");
    260     m_alphaLocation = context->getUniformLocation(program, "alpha");
    261     m_ccMatrixLocation = context->getUniformLocation(program, "cc_matrix");
    262     m_yuvAdjLocation = context->getUniformLocation(program, "yuv_adj");
    263 
    264     return m_yTextureLocation != -1 && m_uTextureLocation != -1 && m_vTextureLocation != -1
    265            && m_alphaLocation != -1 && m_ccMatrixLocation != -1 && m_yuvAdjLocation != -1;
    266 }
    267 
    268 String FragmentShaderYUVVideo::getShaderString() const
    269 {
    270     return SHADER(
    271         precision mediump float;
    272         precision mediump int;
    273         varying vec2 y_texCoord;
    274         varying vec2 uv_texCoord;
    275         uniform sampler2D y_texture;
    276         uniform sampler2D u_texture;
    277         uniform sampler2D v_texture;
    278         uniform float alpha;
    279         uniform vec3 yuv_adj;
    280         uniform mat3 cc_matrix;
    281         void main()
    282         {
    283             float y_raw = texture2D(y_texture, y_texCoord).x;
    284             float u_unsigned = texture2D(u_texture, uv_texCoord).x;
    285             float v_unsigned = texture2D(v_texture, uv_texCoord).x;
    286             vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned) + yuv_adj;
    287             vec3 rgb = cc_matrix * yuv;
    288             gl_FragColor = vec4(rgb, float(1)) * alpha;
    289         }
    290     );
    291 }
    292 
    293 FragmentShaderColor::FragmentShaderColor()
    294     : m_colorLocation(-1)
    295 {
    296 }
    297 
    298 bool FragmentShaderColor::init(GraphicsContext3D* context, unsigned program)
    299 {
    300     m_colorLocation = context->getUniformLocation(program, "color");
    301     return m_colorLocation != -1;
    302 }
    303 
    304 String FragmentShaderColor::getShaderString() const
    305 {
    306     return SHADER(
    307         precision mediump float;
    308         uniform vec4 color;
    309         void main()
    310         {
    311             gl_FragColor = vec4(color.xyz * color.w, color.w);
    312         }
    313     );
    314 }
    315 
    316 } // namespace WebCore
    317 
    318 #endif // USE(ACCELERATED_COMPOSITING)
    319