1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Google Inc. All rights reserved. 4 * Copyright (C) 2011 Igalia S.L. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "config.h" 29#include "GraphicsContext3D.h" 30 31#if USE(3D_GRAPHICS) 32 33#include "CairoUtilities.h" 34#include "GraphicsContext3DPrivate.h" 35#include "Image.h" 36#include "ImageSource.h" 37#include "NotImplemented.h" 38#include "PlatformContextCairo.h" 39#include "RefPtrCairo.h" 40#include <cairo.h> 41 42#if PLATFORM(WIN) 43#include "GLSLANG/ShaderLang.h" 44#else 45#include "ShaderLang.h" 46#endif 47 48#if USE(OPENGL_ES_2) 49#include "Extensions3DOpenGLES.h" 50#else 51#include "Extensions3DOpenGL.h" 52#include "OpenGLShims.h" 53#endif 54 55namespace WebCore { 56 57PassRefPtr<GraphicsContext3D> GraphicsContext3D::create(GraphicsContext3D::Attributes attributes, HostWindow* hostWindow, GraphicsContext3D::RenderStyle renderStyle) 58{ 59 // This implementation doesn't currently support rendering directly to the HostWindow. 60 if (renderStyle == RenderDirectlyToHostWindow) 61 return 0; 62 63 static bool initialized = false; 64 static bool success = true; 65 if (!initialized) { 66#if !USE(OPENGL_ES_2) 67 success = initializeOpenGLShims(); 68#endif 69 initialized = true; 70 } 71 if (!success) 72 return 0; 73 74 RefPtr<GraphicsContext3D> context = adoptRef(new GraphicsContext3D(attributes, hostWindow, renderStyle)); 75 return context.release(); 76} 77 78GraphicsContext3D::GraphicsContext3D(GraphicsContext3D::Attributes attributes, HostWindow*, GraphicsContext3D::RenderStyle renderStyle) 79 : m_currentWidth(0) 80 , m_currentHeight(0) 81 , m_compiler(isGLES2Compliant() ? SH_ESSL_OUTPUT : SH_GLSL_OUTPUT) 82 , m_attrs(attributes) 83 , m_texture(0) 84 , m_compositorTexture(0) 85 , m_fbo(0) 86 , m_depthStencilBuffer(0) 87 , m_multisampleFBO(0) 88 , m_multisampleDepthStencilBuffer(0) 89 , m_multisampleColorBuffer(0) 90 , m_private(std::make_unique<GraphicsContext3DPrivate>(this, renderStyle)) 91{ 92 makeContextCurrent(); 93 94 validateAttributes(); 95 96 if (renderStyle == RenderOffscreen) { 97 // Create a texture to render into. 98 ::glGenTextures(1, &m_texture); 99 ::glBindTexture(GL_TEXTURE_2D, m_texture); 100 ::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 101 ::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 102 ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 103 ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 104 ::glBindTexture(GL_TEXTURE_2D, 0); 105 106 // Create an FBO. 107 ::glGenFramebuffers(1, &m_fbo); 108 ::glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); 109 110 m_state.boundFBO = m_fbo; 111 if (!m_attrs.antialias && (m_attrs.stencil || m_attrs.depth)) 112 ::glGenRenderbuffers(1, &m_depthStencilBuffer); 113 114 // Create a multisample FBO. 115 if (m_attrs.antialias) { 116 ::glGenFramebuffers(1, &m_multisampleFBO); 117 ::glBindFramebuffer(GL_FRAMEBUFFER, m_multisampleFBO); 118 m_state.boundFBO = m_multisampleFBO; 119 ::glGenRenderbuffers(1, &m_multisampleColorBuffer); 120 if (m_attrs.stencil || m_attrs.depth) 121 ::glGenRenderbuffers(1, &m_multisampleDepthStencilBuffer); 122 } 123 } 124 125 // ANGLE initialization. 126 ShBuiltInResources ANGLEResources; 127 ShInitBuiltInResources(&ANGLEResources); 128 129 getIntegerv(GraphicsContext3D::MAX_VERTEX_ATTRIBS, &ANGLEResources.MaxVertexAttribs); 130 getIntegerv(GraphicsContext3D::MAX_VERTEX_UNIFORM_VECTORS, &ANGLEResources.MaxVertexUniformVectors); 131 getIntegerv(GraphicsContext3D::MAX_VARYING_VECTORS, &ANGLEResources.MaxVaryingVectors); 132 getIntegerv(GraphicsContext3D::MAX_VERTEX_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxVertexTextureImageUnits); 133 getIntegerv(GraphicsContext3D::MAX_COMBINED_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxCombinedTextureImageUnits); 134 getIntegerv(GraphicsContext3D::MAX_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxTextureImageUnits); 135 getIntegerv(GraphicsContext3D::MAX_FRAGMENT_UNIFORM_VECTORS, &ANGLEResources.MaxFragmentUniformVectors); 136 137 // Always set to 1 for OpenGL ES. 138 ANGLEResources.MaxDrawBuffers = 1; 139 140 GC3Dint range[2], precision; 141 getShaderPrecisionFormat(GraphicsContext3D::FRAGMENT_SHADER, GraphicsContext3D::HIGH_FLOAT, range, &precision); 142 ANGLEResources.FragmentPrecisionHigh = (range[0] || range[1] || precision); 143 144 m_compiler.setResources(ANGLEResources); 145 146#if !USE(OPENGL_ES_2) 147 ::glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); 148 ::glEnable(GL_POINT_SPRITE); 149#endif 150 151 ::glClearColor(0, 0, 0, 0); 152} 153 154GraphicsContext3D::~GraphicsContext3D() 155{ 156 if (m_private->renderStyle() == RenderToCurrentGLContext) 157 return; 158 159 makeContextCurrent(); 160 if (m_texture) 161 ::glDeleteTextures(1, &m_texture); 162 if (m_compositorTexture) 163 ::glDeleteTextures(1, &m_compositorTexture); 164 165 if (m_attrs.antialias) { 166 ::glDeleteRenderbuffers(1, &m_multisampleColorBuffer); 167 if (m_attrs.stencil || m_attrs.depth) 168 ::glDeleteRenderbuffers(1, &m_multisampleDepthStencilBuffer); 169 ::glDeleteFramebuffers(1, &m_multisampleFBO); 170 } else { 171 if (m_attrs.stencil || m_attrs.depth) 172 ::glDeleteRenderbuffers(1, &m_depthStencilBuffer); 173 } 174 ::glDeleteFramebuffers(1, &m_fbo); 175} 176 177GraphicsContext3D::ImageExtractor::~ImageExtractor() 178{ 179 if (m_decoder) 180 delete m_decoder; 181} 182 183bool GraphicsContext3D::ImageExtractor::extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile) 184{ 185 if (!m_image) 186 return false; 187 // We need this to stay in scope because the native image is just a shallow copy of the data. 188 m_decoder = new ImageSource(premultiplyAlpha ? ImageSource::AlphaPremultiplied : ImageSource::AlphaNotPremultiplied, ignoreGammaAndColorProfile ? ImageSource::GammaAndColorProfileIgnored : ImageSource::GammaAndColorProfileApplied); 189 if (!m_decoder) 190 return false; 191 ImageSource& decoder = *m_decoder; 192 193 m_alphaOp = AlphaDoNothing; 194 if (m_image->data()) { 195 decoder.setData(m_image->data(), true); 196 if (!decoder.frameCount() || !decoder.frameIsCompleteAtIndex(0)) 197 return false; 198 m_imageSurface = decoder.createFrameAtIndex(0); 199 } else { 200 m_imageSurface = m_image->nativeImageForCurrentFrame(); 201 // 1. For texImage2D with HTMLVideoElment input, assume no PremultiplyAlpha had been applied and the alpha value is 0xFF for each pixel, 202 // which is true at present and may be changed in the future and needs adjustment accordingly. 203 // 2. For texImage2D with HTMLCanvasElement input in which Alpha is already Premultiplied in this port, 204 // do AlphaDoUnmultiply if UNPACK_PREMULTIPLY_ALPHA_WEBGL is set to false. 205 if (!premultiplyAlpha && m_imageHtmlDomSource != HtmlDomVideo) 206 m_alphaOp = AlphaDoUnmultiply; 207 208 // if m_imageSurface is not an image, extract a copy of the surface 209 if (m_imageSurface && cairo_surface_get_type(m_imageSurface.get()) != CAIRO_SURFACE_TYPE_IMAGE) { 210 RefPtr<cairo_surface_t> tmpSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, m_imageWidth, m_imageHeight)); 211 copyRectFromOneSurfaceToAnother(m_imageSurface.get(), tmpSurface.get(), IntSize(), IntRect(0, 0, m_imageWidth, m_imageHeight), IntSize(), CAIRO_OPERATOR_SOURCE); 212 m_imageSurface = tmpSurface.release(); 213 } 214 } 215 216 if (!m_imageSurface) 217 return false; 218 219 ASSERT(cairo_surface_get_type(m_imageSurface.get()) == CAIRO_SURFACE_TYPE_IMAGE); 220 221 IntSize imageSize = cairoSurfaceSize(m_imageSurface.get()); 222 m_imageWidth = imageSize.width(); 223 m_imageHeight = imageSize.height(); 224 if (!m_imageWidth || !m_imageHeight) 225 return false; 226 227 if (cairo_image_surface_get_format(m_imageSurface.get()) != CAIRO_FORMAT_ARGB32) 228 return false; 229 230 unsigned int srcUnpackAlignment = 1; 231 size_t bytesPerRow = cairo_image_surface_get_stride(m_imageSurface.get()); 232 size_t bitsPerPixel = 32; 233 unsigned padding = bytesPerRow - bitsPerPixel / 8 * m_imageWidth; 234 if (padding) { 235 srcUnpackAlignment = padding + 1; 236 while (bytesPerRow % srcUnpackAlignment) 237 ++srcUnpackAlignment; 238 } 239 240 m_imagePixelData = cairo_image_surface_get_data(m_imageSurface.get()); 241 m_imageSourceFormat = DataFormatBGRA8; 242 m_imageSourceUnpackAlignment = srcUnpackAlignment; 243 return true; 244} 245 246void GraphicsContext3D::paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, PlatformContextCairo* context) 247{ 248 if (!imagePixels || imageWidth <= 0 || imageHeight <= 0 || canvasWidth <= 0 || canvasHeight <= 0 || !context) 249 return; 250 251 cairo_t *cr = context->cr(); 252 context->save(); 253 254 cairo_rectangle(cr, 0, 0, canvasWidth, canvasHeight); 255 cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); 256 cairo_paint(cr); 257 258 RefPtr<cairo_surface_t> imageSurface = adoptRef(cairo_image_surface_create_for_data( 259 const_cast<unsigned char*>(imagePixels), CAIRO_FORMAT_ARGB32, imageWidth, imageHeight, imageWidth * 4)); 260 261 // OpenGL keeps the pixels stored bottom up, so we need to flip the image here. 262 cairo_translate(cr, 0, imageHeight); 263 cairo_scale(cr, 1, -1); 264 265 cairo_set_operator(cr, CAIRO_OPERATOR_OVER); 266 cairo_set_source_surface(cr, imageSurface.get(), 0, 0); 267 cairo_rectangle(cr, 0, 0, canvasWidth, -canvasHeight); 268 269 cairo_fill(cr); 270 context->restore(); 271} 272 273void GraphicsContext3D::setContextLostCallback(std::unique_ptr<ContextLostCallback>) 274{ 275} 276 277void GraphicsContext3D::setErrorMessageCallback(std::unique_ptr<ErrorMessageCallback>) 278{ 279} 280 281bool GraphicsContext3D::makeContextCurrent() 282{ 283 if (!m_private) 284 return false; 285 return m_private->makeContextCurrent(); 286} 287 288PlatformGraphicsContext3D GraphicsContext3D::platformGraphicsContext3D() 289{ 290 return m_private->platformContext(); 291} 292 293Platform3DObject GraphicsContext3D::platformTexture() const 294{ 295 return m_texture; 296} 297 298bool GraphicsContext3D::isGLES2Compliant() const 299{ 300#if USE(OPENGL_ES_2) 301 return true; 302#else 303 return false; 304#endif 305} 306 307PlatformLayer* GraphicsContext3D::platformLayer() const 308{ 309 return m_private.get(); 310} 311 312} // namespace WebCore 313 314#endif // USE(3D_GRAPHICS) 315