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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #if ENABLE(ACCELERATED_2D_CANVAS)
34
35 #include "BicubicShader.h"
36
37 #include "GraphicsContext3D.h"
38
39 namespace WebCore {
40
BicubicShader(GraphicsContext3D * context,unsigned program)41 BicubicShader::BicubicShader(GraphicsContext3D* context, unsigned program)
42 : Shader(context, program)
43 , m_matrixLocation(context->getUniformLocation(program, "matrix"))
44 , m_texMatrixLocation(context->getUniformLocation(program, "texMatrix"))
45 , m_coefficientsLocation(context->getUniformLocation(program, "coefficients"))
46 , m_imageIncrementLocation(context->getUniformLocation(program, "imageIncrement"))
47 , m_imageLocation(context->getUniformLocation(program, "image"))
48 , m_alphaLocation(context->getUniformLocation(program, "alpha"))
49 , m_positionLocation(context->getAttribLocation(program, "position"))
50 {
51 }
52
create(GraphicsContext3D * context)53 PassOwnPtr<BicubicShader> BicubicShader::create(GraphicsContext3D* context)
54 {
55 static const char* vertexShaderSource =
56 "uniform mat3 matrix;\n"
57 "uniform mat3 texMatrix;\n"
58 "attribute vec2 position;\n"
59 "varying vec2 texCoord;\n"
60 "void main() {\n"
61 " vec3 pos = vec3(position, 1.0);\n"
62 " texCoord = (texMatrix * pos).xy;\n"
63 " gl_Position = vec4(matrix * pos, 1.0);\n"
64 "}\n";
65 static const char* fragmentShaderSource =
66 "#ifdef GL_ES\n"
67 "precision mediump float;\n"
68 "#endif\n"
69 "uniform sampler2D image;\n"
70 "uniform vec2 imageIncrement;\n"
71 "uniform mat4 coefficients;\n"
72 "uniform float alpha;\n"
73 "varying vec2 texCoord;\n"
74 "vec4 cubicBlend(float t, vec4 c0, vec4 c1, vec4 c2, vec4 c3) {\n"
75 " vec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
76 " vec4 result = coefficients * ts;\n"
77 " return result.w * c0 + result.z * c1 + result.y * c2 + result.x * c3;\n"
78 "}\n"
79 "void main() {\n"
80 " vec2 imageCoord = texCoord;\n"
81 " vec2 f = fract(imageCoord / imageIncrement) - vec2(0.5, 0.5);\n"
82 " vec4 t00 = texture2D(image, imageCoord + imageIncrement * vec2(-1, -1));\n"
83 " vec4 t10 = texture2D(image, imageCoord + imageIncrement * vec2( 0, -1));\n"
84 " vec4 t20 = texture2D(image, imageCoord + imageIncrement * vec2( 1, -1));\n"
85 " vec4 t30 = texture2D(image, imageCoord + imageIncrement * vec2( 2, -1));\n"
86 " vec4 t0 = cubicBlend(f.x, t00, t10, t20, t30);\n"
87 " vec4 t01 = texture2D(image, imageCoord + imageIncrement * vec2(-1, 0));\n"
88 " vec4 t11 = texture2D(image, imageCoord + imageIncrement * vec2( 0, 0));\n"
89 " vec4 t21 = texture2D(image, imageCoord + imageIncrement * vec2( 1, 0));\n"
90 " vec4 t31 = texture2D(image, imageCoord + imageIncrement * vec2( 2, 0));\n"
91 " vec4 t1 = cubicBlend(f.x, t01, t11, t21, t31);\n"
92 " vec4 t02 = texture2D(image, imageCoord + imageIncrement * vec2(-1, 1));\n"
93 " vec4 t12 = texture2D(image, imageCoord + imageIncrement * vec2( 0, 1));\n"
94 " vec4 t22 = texture2D(image, imageCoord + imageIncrement * vec2( 1, 1));\n"
95 " vec4 t32 = texture2D(image, imageCoord + imageIncrement * vec2( 2, 1));\n"
96 " vec4 t2 = cubicBlend(f.x, t02, t12, t22, t32);\n"
97 " vec4 t03 = texture2D(image, imageCoord + imageIncrement * vec2(-1, 2));\n"
98 " vec4 t13 = texture2D(image, imageCoord + imageIncrement * vec2( 0, 2));\n"
99 " vec4 t23 = texture2D(image, imageCoord + imageIncrement * vec2( 1, 2));\n"
100 " vec4 t33 = texture2D(image, imageCoord + imageIncrement * vec2( 2, 2));\n"
101 " vec4 t3 = cubicBlend(f.x, t03, t13, t23, t33);\n"
102 " gl_FragColor = cubicBlend(f.y, t0, t1, t2, t3);\n"
103 "}\n";
104
105 unsigned program = loadProgram(context, vertexShaderSource, fragmentShaderSource);
106 if (!program)
107 return 0;
108
109 return new BicubicShader(context, program);
110 }
111
use(const AffineTransform & transform,const AffineTransform & texTransform,const float coefficients[16],const float imageIncrement[2],float alpha)112 void BicubicShader::use(const AffineTransform& transform, const AffineTransform& texTransform, const float coefficients[16], const float imageIncrement[2], float alpha)
113 {
114 m_context->useProgram(m_program);
115 float matrix[9];
116 affineTo3x3(transform, matrix);
117 m_context->uniformMatrix3fv(m_matrixLocation, false /*transpose*/, matrix, 1 /*count*/);
118
119 float texMatrix[9];
120 affineTo3x3(texTransform, texMatrix);
121 m_context->uniformMatrix3fv(m_texMatrixLocation, false /*transpose*/, texMatrix, 1 /*count*/);
122 m_context->uniformMatrix4fv(m_coefficientsLocation, false /*transpose*/, const_cast<float *>(coefficients), 1 /*count*/);
123
124 m_context->uniform2f(m_imageIncrementLocation, imageIncrement[0], imageIncrement[1]);
125
126 // For now, we always use texture unit 0. If that ever changes, we should
127 // expose this parameter to the caller.
128 m_context->uniform1i(m_imageLocation, 0);
129 m_context->uniform1f(m_alphaLocation, alpha);
130
131 m_context->vertexAttribPointer(m_positionLocation, 2, GraphicsContext3D::FLOAT, false, 0, 0);
132
133 m_context->enableVertexAttribArray(m_positionLocation);
134 }
135
136 }
137
138 #endif
139