Commit f5eaf118 authored by eric@webkit.org's avatar eric@webkit.org

2010-02-17 Kenneth Russell <kbr@google.com>

        Reviewed by Oliver Hunt.

        Refactor texImage2D and texSubImage2D taking Image to use common code
        https://bugs.webkit.org/show_bug.cgi?id=34458

        Merged the Safari and Chromium code which extracts the data from
        Image objects into common entry points on GraphicsContext3D. This
        immediately fixes the following three problems:
          - Chromium not implementing texSubImage2D taking Image.
          - Safari not obeying the flipY parameter to texImage2D or
            texSubImage2D taking Image.
          - Safari not obeying the premultipyAlpha parameter to texImage2D
            or texSubImage2D taking Image.
        Added new test verifying the behavior of texImage2D and
        texSubImage2D and the flipY parameter. The premultiplyAlpha
        parameter can not be tested yet as the implementation is not yet
        spec compliant. This will be fixed in a follow-on bug.

        Ran all WebGL demos in demo repository on Safari and Chromium;
        textures are now the right way up in both browsers, and
        transparent textures in Particles demo now look correct in Safari.

        * fast/canvas/webgl/resources/red-green.png: Added.
        * fast/canvas/webgl/tex-image-and-sub-image-2d-with-image-expected.txt: Added.
        * fast/canvas/webgl/tex-image-and-sub-image-2d-with-image.html: Added.
2010-02-17  Kenneth Russell  <kbr@google.com>

        Reviewed by Oliver Hunt.

        Refactor texImage2D and texSubImage2D taking Image to use common code
        https://bugs.webkit.org/show_bug.cgi?id=34458

        Merged the Safari and Chromium code which extracts the data from
        Image objects into common entry points on GraphicsContext3D. This
        immediately fixes the following three problems:
          - Chromium not implementing texSubImage2D taking Image.
          - Safari not obeying the flipY parameter to texImage2D or
            texSubImage2D taking Image.
          - Safari not obeying the premultipyAlpha parameter to texImage2D
            or texSubImage2D taking Image.
        Added new test verifying the behavior of texImage2D and
        texSubImage2D and the flipY parameter. The premultiplyAlpha
        parameter can not be tested yet as the implementation is not yet
        spec compliant. This will be fixed in a follow-on bug.

        Ran all WebGL demos in demo repository on Safari and Chromium;
        textures are now the right way up in both browsers, and
        transparent textures in Particles demo now look correct in Safari.

        Test: fast/canvas/webgl/tex-image-and-sub-image-2d-with-image.html

        * WebCore.gyp/WebCore.gyp:
        * WebCore.gypi:
        * WebCore.xcodeproj/project.pbxproj:
        * platform/graphics/GraphicsContext3D.cpp: Added.
        (WebCore::GraphicsContext3D::extractImageData):
        (WebCore::GraphicsContext3D::processImageData):
        (WebCore::GraphicsContext3D::premultiplyAlpha):
        (WebCore::GraphicsContext3D::unmultiplyAlpha):
        * platform/graphics/GraphicsContext3D.h:
        (WebCore::GraphicsContext3D::):
        * platform/graphics/cg/GraphicsContext3DCG.cpp: Added.
        (WebCore::GraphicsContext3D::getImageData):
        * platform/graphics/mac/GraphicsContext3DMac.cpp:
        (WebCore::GraphicsContext3D::texImage2D):
        (WebCore::GraphicsContext3D::texSubImage2D):
        * platform/graphics/skia/GraphicsContext3DSkia.cpp: Added.
        (WebCore::GraphicsContext3D::getImageData):
2010-02-17  Kenneth Russell  <kbr@google.com>

        Reviewed by Oliver Hunt.

        Refactor texImage2D and texSubImage2D taking Image to use common code
        https://bugs.webkit.org/show_bug.cgi?id=34458

        Merged the Safari and Chromium code which extracts the data from
        Image objects into common entry points on GraphicsContext3D. This
        immediately fixes the following three problems:
          - Chromium not implementing texSubImage2D taking Image.
          - Safari not obeying the flipY parameter to texImage2D or
            texSubImage2D taking Image.
          - Safari not obeying the premultipyAlpha parameter to texImage2D
            or texSubImage2D taking Image.
        Added new test verifying the behavior of texImage2D and
        texSubImage2D and the flipY parameter. The premultiplyAlpha
        parameter can not be tested yet as the implementation is not yet
        spec compliant. This will be fixed in a follow-on bug.

        Ran all WebGL demos in demo repository on Safari and Chromium;
        textures are now the right way up in both browsers, and
        transparent textures in Particles demo now look correct in Safari.

        * src/GraphicsContext3D.cpp:
        (WebCore::GraphicsContext3D::texImage2D):
        (WebCore::GraphicsContext3D::texSubImage2D):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@54907 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 06afd293
2010-02-17 Kenneth Russell <kbr@google.com>
Reviewed by Oliver Hunt.
Refactor texImage2D and texSubImage2D taking Image to use common code
https://bugs.webkit.org/show_bug.cgi?id=34458
Merged the Safari and Chromium code which extracts the data from
Image objects into common entry points on GraphicsContext3D. This
immediately fixes the following three problems:
- Chromium not implementing texSubImage2D taking Image.
- Safari not obeying the flipY parameter to texImage2D or
texSubImage2D taking Image.
- Safari not obeying the premultipyAlpha parameter to texImage2D
or texSubImage2D taking Image.
Added new test verifying the behavior of texImage2D and
texSubImage2D and the flipY parameter. The premultiplyAlpha
parameter can not be tested yet as the implementation is not yet
spec compliant. This will be fixed in a follow-on bug.
Ran all WebGL demos in demo repository on Safari and Chromium;
textures are now the right way up in both browsers, and
transparent textures in Particles demo now look correct in Safari.
* fast/canvas/webgl/resources/red-green.png: Added.
* fast/canvas/webgl/tex-image-and-sub-image-2d-with-image-expected.txt: Added.
* fast/canvas/webgl/tex-image-and-sub-image-2d-with-image.html: Added.
2010-02-17 Nikolas Zimmermann <nzimmermann@rim.com>
Not reviewed. Try to turn windows slaves again, by skipping two websocket tests that fail reliable.
......
Verify texImage2D and texSubImage2D code paths taking Images, in particular flipY argument
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Regression test for https://bugs.webkit.org/show_bug.cgi?id=34458 : Refactor texImage2D and texSubImage2D taking Image to use common code
Testing texImage2D with flipY=true
Checking lower left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Checking upper left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Testing texImage2D with flipY=false
Checking lower left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Checking upper left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Testing texSubImage2D with flipY=true
Checking lower left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Checking upper left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Testing texSubImage2D with flipY=false
Checking lower left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
Checking upper left corner
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS pixel is correctColor
PASS successfullyParsed is true
TEST COMPLETE
<html>
<head>
<link rel="stylesheet" href="../../js/resources/js-test-style.css"/>
<script src="../../js/resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/utils3d.js"> </script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 g_Position;
attribute vec2 g_TexCoord0;
varying vec2 texCoord;
void main()
{
gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);
texCoord = g_TexCoord0;
}
</script>
<script id="fshader" type="x-shader/x-fragment">
uniform sampler2D tex;
varying vec2 texCoord;
void main()
{
gl_FragColor = texture2D(tex, texCoord);
}
</script>
<script>
var gl = null;
var textureLoc = null;
var successfullyParsed = false;
function init()
{
if (window.layoutTestController) {
layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
layoutTestController.dumpAsText();
layoutTestController.waitUntilDone();
}
description('Verify texImage2D and texSubImage2D code paths taking Images, in particular flipY argument');
debug('Regression test for <a href="https://bugs.webkit.org/show_bug.cgi?id=34458">https://bugs.webkit.org/show_bug.cgi?id=34458</a> : <code>Refactor texImage2D and texSubImage2D taking Image to use common code</code>');
gl = initWebGL("example", "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1);
gl.viewport(0, 0, 32, 32);
textureLoc = gl.getUniformLocation(gl.program, "tex");
var vertices = new WebGLFloatArray([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0]);
var texCoords = new WebGLFloatArray([
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0]);
var texCoordOffset = vertices.byteLength;
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER,
texCoordOffset + texCoords.byteLength,
gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
gl.bufferSubData(gl.ARRAY_BUFFER, texCoordOffset, texCoords);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 0, texCoordOffset);
loadTexture("resources/red-green.png");
}
function loadTexture(src) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
var image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, image, true);
runTest(image);
};
image.src = src;
return texture;
}
// These two declarations need to be global for "shouldBe" to see them
var buf = null;
var idx = 0;
var pixel = [0, 0, 0];
var correctColor = null;
function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor)
{
debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
' with flipY=' + flipY);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Disable any writes to the alpha channel
gl.colorMask(1, 1, 1, 0);
var texture = gl.createTexture();
// Bind the texture to texture unit 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set up texture parameters
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// Upload the image into the texture
if (useTexSubImage2D) {
// Initialize the texture to black first
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, image.width, image.height, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, image, flipY, false);
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, image, flipY, false);
}
// Point the uniform sampler to texture unit 0
gl.uniform1i(textureLoc, 0);
// Draw the triangles
gl.drawArrays(gl.TRIANGLES, 0, 6);
// Read back the rendering results
var width = 32;
var height = 32;
buf = gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE);
// Check a few pixels near the top and bottom and make sure they have
// the right color.
var queryWidth = 2;
var queryHeight = 2;
debug("Checking lower left corner");
var xoff = 4;
var yoff = 4;
correctColor = bottomColor;
for (var y = 0; y < queryHeight; y++) {
for (var x = 0; x < queryWidth; x++) {
idx = ((yoff + y) * width * 4 +
(xoff + x) * 4);
pixel[0] = buf[idx];
pixel[1] = buf[idx + 1];
pixel[2] = buf[idx + 2];
shouldBe("pixel", "correctColor");
}
}
debug("Checking upper left corner");
yoff = height - 8;
correctColor = topColor;
for (var y = 0; y < queryHeight; y++) {
for (var x = 0; x < queryWidth; x++) {
idx = ((yoff + y) * width * 4 +
(xoff + x) * 4);
pixel[0] = buf[idx];
pixel[1] = buf[idx + 1];
pixel[2] = buf[idx + 2];
shouldBe("pixel", "correctColor");
}
}
}
function runTest(image)
{
var red = [255, 0, 0];
var green = [0, 255, 0];
runOneIteration(image, false, true, red, green);
runOneIteration(image, false, false, green, red);
runOneIteration(image, true, true, red, green);
runOneIteration(image, true, false, green, red);
successfullyParsed = true;
var epilogue = document.createElement("script");
epilogue.onload = finish;
epilogue.src = "../../js/resources/js-test-post.js";
document.body.appendChild(epilogue);
}
function finish() {
if (window.layoutTestController) {
layoutTestController.notifyDone();
}
}
</script>
</head>
<body onload="init()">
<canvas id="example" width="32px" height="32px"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>
2010-02-17 Kenneth Russell <kbr@google.com>
Reviewed by Oliver Hunt.
Refactor texImage2D and texSubImage2D taking Image to use common code
https://bugs.webkit.org/show_bug.cgi?id=34458
Merged the Safari and Chromium code which extracts the data from
Image objects into common entry points on GraphicsContext3D. This
immediately fixes the following three problems:
- Chromium not implementing texSubImage2D taking Image.
- Safari not obeying the flipY parameter to texImage2D or
texSubImage2D taking Image.
- Safari not obeying the premultipyAlpha parameter to texImage2D
or texSubImage2D taking Image.
Added new test verifying the behavior of texImage2D and
texSubImage2D and the flipY parameter. The premultiplyAlpha
parameter can not be tested yet as the implementation is not yet
spec compliant. This will be fixed in a follow-on bug.
Ran all WebGL demos in demo repository on Safari and Chromium;
textures are now the right way up in both browsers, and
transparent textures in Particles demo now look correct in Safari.
Test: fast/canvas/webgl/tex-image-and-sub-image-2d-with-image.html
* WebCore.gyp/WebCore.gyp:
* WebCore.gypi:
* WebCore.xcodeproj/project.pbxproj:
* platform/graphics/GraphicsContext3D.cpp: Added.
(WebCore::GraphicsContext3D::extractImageData):
(WebCore::GraphicsContext3D::processImageData):
(WebCore::GraphicsContext3D::premultiplyAlpha):
(WebCore::GraphicsContext3D::unmultiplyAlpha):
* platform/graphics/GraphicsContext3D.h:
(WebCore::GraphicsContext3D::):
* platform/graphics/cg/GraphicsContext3DCG.cpp: Added.
(WebCore::GraphicsContext3D::getImageData):
* platform/graphics/mac/GraphicsContext3DMac.cpp:
(WebCore::GraphicsContext3D::texImage2D):
(WebCore::GraphicsContext3D::texSubImage2D):
* platform/graphics/skia/GraphicsContext3DSkia.cpp: Added.
(WebCore::GraphicsContext3D::getImageData):
2010-02-17 Noam Rosenthal <noam.rosenthal@nokia.com>
Reviewed by Ariya Hidayat.
......
......@@ -956,6 +956,7 @@
'../platform/graphics/skia/FloatPointSkia.cpp',
'../platform/graphics/skia/FloatRectSkia.cpp',
'../platform/graphics/skia/GradientSkia.cpp',
'../platform/graphics/skia/GraphicsContext3DSkia.cpp',
'../platform/graphics/skia/GraphicsContextSkia.cpp',
'../platform/graphics/skia/ImageBufferSkia.cpp',
'../platform/graphics/skia/ImageSkia.cpp',
......
......@@ -1977,6 +1977,7 @@
'platform/graphics/cg/FloatRectCG.cpp',
'platform/graphics/cg/FloatSizeCG.cpp',
'platform/graphics/cg/GradientCG.cpp',
'platform/graphics/cg/GraphicsContext3DCG.cpp',
'platform/graphics/cg/GraphicsContextCG.cpp',
'platform/graphics/cg/GraphicsContextPlatformPrivateCG.h',
'platform/graphics/cg/ImageBufferCG.cpp',
......@@ -2140,6 +2141,7 @@
'platform/graphics/skia/GradientSkia.cpp',
'platform/graphics/skia/GraphicsContextPlatformPrivate.h',
'platform/graphics/skia/GraphicsContextSkia.cpp',
'platform/graphics/skia/GraphicsContext3DSkia.cpp',
'platform/graphics/skia/ImageBufferSkia.cpp',
'platform/graphics/skia/ImageSkia.cpp',
'platform/graphics/skia/IntPointSkia.cpp',
......@@ -2277,6 +2279,7 @@
'platform/graphics/Gradient.h',
'platform/graphics/GraphicsContext.cpp',
'platform/graphics/GraphicsContext.h',
'platform/graphics/GraphicsContext3D.cpp',
'platform/graphics/GraphicsContext3D.h',
'platform/graphics/GraphicsContextPrivate.h',
'platform/graphics/GraphicsLayer.cpp',
......
......@@ -1237,6 +1237,8 @@
65DF323C09D1DE65000BE325 /* JSCanvasPattern.h in Headers */ = {isa = PBXBuildFile; fileRef = 65DF323609D1DE65000BE325 /* JSCanvasPattern.h */; };
65DF326109D1E199000BE325 /* UserAgentStyleSheetsData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 656581AF09D14EE6000E61D7 /* UserAgentStyleSheetsData.cpp */; };
65FEA86909833ADE00BED4AB /* Page.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65FEA86809833ADE00BED4AB /* Page.cpp */; };
6E21C6C01126338500A7BE02 /* GraphicsContext3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6E21C6BF1126338500A7BE02 /* GraphicsContext3D.cpp */; };
6E21C6C21126339900A7BE02 /* GraphicsContext3DCG.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6E21C6C11126339900A7BE02 /* GraphicsContext3DCG.cpp */; };
6E47E66010B7944B00B186C8 /* WebGLGetInfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6E47E65E10B7944B00B186C8 /* WebGLGetInfo.cpp */; };
6E47E66110B7944B00B186C8 /* WebGLGetInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E47E65F10B7944B00B186C8 /* WebGLGetInfo.h */; };
6E4E91AC10F7FB3100A2779C /* CanvasContextAttributes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6E4E91A710F7FB3100A2779C /* CanvasContextAttributes.cpp */; };
......@@ -6650,6 +6652,8 @@
65DF323609D1DE65000BE325 /* JSCanvasPattern.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = JSCanvasPattern.h; sourceTree = "<group>"; };
65F80697054D9F86008BF776 /* BlockExceptions.mm */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = BlockExceptions.mm; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
65FEA86809833ADE00BED4AB /* Page.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = Page.cpp; sourceTree = "<group>"; };
6E21C6BF1126338500A7BE02 /* GraphicsContext3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GraphicsContext3D.cpp; sourceTree = "<group>"; };
6E21C6C11126339900A7BE02 /* GraphicsContext3DCG.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GraphicsContext3DCG.cpp; sourceTree = "<group>"; };
6E47E65E10B7944B00B186C8 /* WebGLGetInfo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WebGLGetInfo.cpp; path = canvas/WebGLGetInfo.cpp; sourceTree = "<group>"; };
6E47E65F10B7944B00B186C8 /* WebGLGetInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WebGLGetInfo.h; path = canvas/WebGLGetInfo.h; sourceTree = "<group>"; };
6E4E91A710F7FB3100A2779C /* CanvasContextAttributes.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CanvasContextAttributes.cpp; path = canvas/CanvasContextAttributes.cpp; sourceTree = "<group>"; };
......@@ -14319,6 +14323,7 @@
B275352C0B053814002CE64F /* FloatRectCG.cpp */,
B275352D0B053814002CE64F /* FloatSizeCG.cpp */,
BC53C60A0DA56CF10021EB5D /* GradientCG.cpp */,
6E21C6C11126339900A7BE02 /* GraphicsContext3DCG.cpp */,
B2ED97700B1F55CE00257D0F /* GraphicsContextCG.cpp */,
A80D67070E9E9DEB00E420F0 /* GraphicsContextPlatformPrivateCG.h */,
B2A10B930B3818D700099AA4 /* ImageBufferCG.cpp */,
......@@ -14434,6 +14439,7 @@
BC53C5F40DA56B920021EB5D /* Gradient.h */,
B2A015920AF6CD53006BCE0E /* GraphicsContext.cpp */,
B2A015930AF6CD53006BCE0E /* GraphicsContext.h */,
6E21C6BF1126338500A7BE02 /* GraphicsContext3D.cpp */,
49C7B9FB1042D3650009D447 /* GraphicsContext3D.h */,
A88FE3330E5EEE87008D8C0F /* GraphicsContextPrivate.h */,
0F580B090F12A2690051D689 /* GraphicsLayer.cpp */,
......@@ -20711,6 +20717,8 @@
76FF17E311235673001D61B5 /* PluginViewNone.cpp in Sources */,
9F2A322B1125A0A2003C3056 /* JavaScriptProfile.cpp in Sources */,
9F2A322D1125A0A2003C3056 /* JavaScriptProfileNode.cpp in Sources */,
6E21C6C01126338500A7BE02 /* GraphicsContext3D.cpp in Sources */,
6E21C6C21126339900A7BE02 /* GraphicsContext3DCG.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(3D_CANVAS)
#include "GraphicsContext3D.h"
#include "Image.h"
namespace WebCore {
bool GraphicsContext3D::extractImageData(Image* image,
bool flipY,
bool premultiplyAlpha,
Vector<uint8_t>& imageData,
unsigned int* format,
unsigned int* internalFormat)
{
if (!image)
return false;
AlphaOp alphaOp = kAlphaDoNothing;
bool hasAlphaChannel = true;
if (!getImageData(image, imageData, premultiplyAlpha,
&hasAlphaChannel, &alphaOp, format))
return false;
processImageData(imageData.data(),
image->width(),
image->height(),
flipY,
alphaOp);
*internalFormat = (hasAlphaChannel ? RGBA : RGB);
return true;
}
void GraphicsContext3D::processImageData(uint8_t* imageData,
unsigned width,
unsigned height,
bool flipVertically,
AlphaOp alphaOp)
{
switch (alphaOp) {
case kAlphaDoPremultiply:
premultiplyAlpha(imageData, width * height);
break;
case kAlphaDoUnmultiply:
unmultiplyAlpha(imageData, width * height);
break;
default:
break;
}
if (flipVertically && width && height) {
int rowBytes = width * 4;
uint8_t* tempRow = new uint8_t[rowBytes];
for (unsigned i = 0; i < height / 2; i++) {
uint8_t* lowRow = imageData + (rowBytes * i);
uint8_t* highRow = imageData + (rowBytes * (height - i - 1));
memcpy(tempRow, lowRow, rowBytes);
memcpy(lowRow, highRow, rowBytes);
memcpy(highRow, tempRow, rowBytes);
}
delete[] tempRow;
}
}
// Premultiply alpha into color channels.
void GraphicsContext3D::premultiplyAlpha(unsigned char* rgbaData, int numPixels)
{
for (int j = 0; j < numPixels; j++) {
float r = rgbaData[4*j+0] / 255.0f;
float g = rgbaData[4*j+1] / 255.0f;
float b = rgbaData[4*j+2] / 255.0f;
float a = rgbaData[4*j+3] / 255.0f;
rgbaData[4*j+0] = (unsigned char) (r * a * 255.0f);
rgbaData[4*j+1] = (unsigned char) (g * a * 255.0f);
rgbaData[4*j+2] = (unsigned char) (b * a * 255.0f);
}
}
// Remove premultiplied alpha from color channels.
// FIXME: this is lossy. Must retrieve original values from HTMLImageElement.
void GraphicsContext3D::unmultiplyAlpha(unsigned char* rgbaData, int numPixels)
{
for (int j = 0; j < numPixels; j++) {
float r = rgbaData[4*j+0] / 255.0f;
float g = rgbaData[4*j+1] / 255.0f;
float b = rgbaData[4*j+2] / 255.0f;
float a = rgbaData[4*j+3] / 255.0f;
if (a > 0.0f) {
r /= a;
g /= a;
b /= a;
r = (r > 1.0f) ? 1.0f : r;
g = (g > 1.0f) ? 1.0f : g;
b = (b > 1.0f) ? 1.0f : b;
rgbaData[4*j+0] = (unsigned char) (r * 255.0f);
rgbaData[4*j+1] = (unsigned char) (g * 255.0f);
rgbaData[4*j+2] = (unsigned char) (b * 255.0f);
}
}
}
} // namespace WebCore
#endif // ENABLE(3D_CANVAS)
......@@ -419,6 +419,43 @@ namespace WebCore {
// like GL_FLOAT, GL_INT, etc.
int sizeInBytes(int type);
//----------------------------------------------------------------------
// Helpers for texture uploading.
//
// Extracts the contents of the given Image into the passed
// Vector, obeying the flipY and premultiplyAlpha flags.
// Returns the applicable OpenGL format and internalFormat for
// the subsequent glTexImage2D or glTexSubImage2D call.
// Returns true upon success.
bool extractImageData(Image* image,
bool flipY,
bool premultiplyAlpha,
Vector<uint8_t>& imageData,
unsigned int* format,
unsigned int* internalFormat);
// Processes the given image data in preparation for uploading
// via texImage2D or texSubImage2D. The input data must be in
// 4-component format with the alpha channel last (i.e., RGBA
// or BGRA), tightly packed, with no space between rows.
enum AlphaOp {
kAlphaDoNothing = 0,
kAlphaDoPremultiply = 1,
kAlphaDoUnmultiply = 2
};
void processImageData(uint8_t* imageData,
unsigned width,
unsigned height,
bool flipVertically,
AlphaOp alphaOp);
//----------------------------------------------------------------------
// Entry points for WebGL.
//
void activeTexture(unsigned long texture);
void attachShader(WebGLProgram* program, WebGLShader* shader);
void bindAttribLocation(WebGLProgram*, unsigned long index, const String& name);
......@@ -624,6 +661,34 @@ namespace WebCore {
private:
GraphicsContext3D(Attributes attrs);
// Helpers for texture uploading.
void premultiplyAlpha(unsigned char* rgbaData, int numPixels);
void unmultiplyAlpha(uint8_t* imageData, int numPixels);
// Each platform must provide an implementation of this method.
//
// Gets the data for the given Image into outputVector,
// without doing any processing of the data (vertical flip or
// otherwise).
//
// premultiplyAlpha indicates whether the user will eventually
// want the alpha channel multiplied into the color channels.
//
// The out parameter hasAlphaChannel indicates whether the
// image actually had an alpha channel.
//
// The out parameter neededAlphaOp should be passed to a
// subsequent call of processImageData.
//
// The out parameter format should be passed to subsequent
// invocations of texImage2D and texSubImage2D.
bool getImageData(Image* image,
Vector<uint8_t>& outputVector,
bool premultiplyAlpha,
bool* hasAlphaChannel,
AlphaOp* neededAlphaOp,