1 /* 2 * Copyright (C) 2013 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 #include "core/html/canvas/WebGLDrawBuffers.h" 29 30 namespace WebCore { 31 32 WebGLDrawBuffers::WebGLDrawBuffers(WebGLRenderingContextBase* context) 33 : WebGLExtension(context) 34 { 35 ScriptWrappable::init(this); 36 context->extensionsUtil()->ensureExtensionEnabled("GL_EXT_draw_buffers"); 37 } 38 39 WebGLDrawBuffers::~WebGLDrawBuffers() 40 { 41 } 42 43 WebGLExtensionName WebGLDrawBuffers::name() const 44 { 45 return WebGLDrawBuffersName; 46 } 47 48 PassRefPtr<WebGLDrawBuffers> WebGLDrawBuffers::create(WebGLRenderingContextBase* context) 49 { 50 return adoptRef(new WebGLDrawBuffers(context)); 51 } 52 53 // static 54 bool WebGLDrawBuffers::supported(WebGLRenderingContextBase* context) 55 { 56 return (context->extensionsUtil()->supportsExtension("GL_EXT_draw_buffers") 57 && satisfiesWebGLRequirements(context)); 58 } 59 60 const char* WebGLDrawBuffers::extensionName() 61 { 62 return "WEBGL_draw_buffers"; 63 } 64 65 void WebGLDrawBuffers::drawBuffersWEBGL(const Vector<GLenum>& buffers) 66 { 67 if (isLost()) 68 return; 69 GLsizei n = buffers.size(); 70 const GLenum* bufs = buffers.data(); 71 if (!m_context->m_framebufferBinding) { 72 if (n != 1) { 73 m_context->synthesizeGLError(GL_INVALID_VALUE, "drawBuffersWEBGL", "more than one buffer"); 74 return; 75 } 76 if (bufs[0] != GL_BACK && bufs[0] != GL_NONE) { 77 m_context->synthesizeGLError(GL_INVALID_OPERATION, "drawBuffersWEBGL", "BACK or NONE"); 78 return; 79 } 80 // Because the backbuffer is simulated on all current WebKit ports, we need to change BACK to COLOR_ATTACHMENT0. 81 GLenum value = (bufs[0] == GL_BACK) ? GL_COLOR_ATTACHMENT0 : GL_NONE; 82 m_context->webContext()->drawBuffersEXT(1, &value); 83 m_context->setBackDrawBuffer(bufs[0]); 84 } else { 85 if (n > m_context->maxDrawBuffers()) { 86 m_context->synthesizeGLError(GL_INVALID_VALUE, "drawBuffersWEBGL", "more than max draw buffers"); 87 return; 88 } 89 for (GLsizei i = 0; i < n; ++i) { 90 if (bufs[i] != GL_NONE && bufs[i] != static_cast<GLenum>(GL_COLOR_ATTACHMENT0_EXT + i)) { 91 m_context->synthesizeGLError(GL_INVALID_OPERATION, "drawBuffersWEBGL", "COLOR_ATTACHMENTi_EXT or NONE"); 92 return; 93 } 94 } 95 m_context->m_framebufferBinding->drawBuffers(buffers); 96 } 97 } 98 99 // static 100 bool WebGLDrawBuffers::satisfiesWebGLRequirements(WebGLRenderingContextBase* webglContext) 101 { 102 blink::WebGraphicsContext3D* context = webglContext->webContext(); 103 Extensions3DUtil* extensionsUtil = webglContext->extensionsUtil(); 104 105 // This is called after we make sure GL_EXT_draw_buffers is supported. 106 GLint maxDrawBuffers = 0; 107 GLint maxColorAttachments = 0; 108 context->getIntegerv(GL_MAX_DRAW_BUFFERS_EXT, &maxDrawBuffers); 109 context->getIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAttachments); 110 if (maxDrawBuffers < 4 || maxColorAttachments < 4) 111 return false; 112 113 Platform3DObject fbo = context->createFramebuffer(); 114 context->bindFramebuffer(GL_FRAMEBUFFER, fbo); 115 116 const unsigned char* buffer = 0; // Chromium doesn't allow init data for depth/stencil tetxures. 117 bool supportsDepth = (extensionsUtil->supportsExtension("GL_CHROMIUM_depth_texture") 118 || extensionsUtil->supportsExtension("GL_OES_depth_texture") 119 || extensionsUtil->supportsExtension("GL_ARB_depth_texture")); 120 bool supportsDepthStencil = (extensionsUtil->supportsExtension("GL_EXT_packed_depth_stencil") 121 || extensionsUtil->supportsExtension("GL_OES_packed_depth_stencil")); 122 Platform3DObject depthStencil = 0; 123 if (supportsDepthStencil) { 124 depthStencil = context->createTexture(); 125 context->bindTexture(GL_TEXTURE_2D, depthStencil); 126 context->texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL_OES, 1, 1, 0, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, buffer); 127 } 128 Platform3DObject depth = 0; 129 if (supportsDepth) { 130 depth = context->createTexture(); 131 context->bindTexture(GL_TEXTURE_2D, depth); 132 context->texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer); 133 } 134 135 Vector<Platform3DObject> colors; 136 bool ok = true; 137 GLint maxAllowedBuffers = std::min(maxDrawBuffers, maxColorAttachments); 138 for (GLint i = 0; i < maxAllowedBuffers; ++i) { 139 Platform3DObject color = context->createTexture(); 140 colors.append(color); 141 context->bindTexture(GL_TEXTURE_2D, color); 142 context->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 143 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, color, 0); 144 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 145 ok = false; 146 break; 147 } 148 if (supportsDepth) { 149 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0); 150 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 151 ok = false; 152 break; 153 } 154 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0); 155 } 156 if (supportsDepthStencil) { 157 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthStencil, 0); 158 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthStencil, 0); 159 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 160 ok = false; 161 break; 162 } 163 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0); 164 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0); 165 } 166 } 167 168 webglContext->restoreCurrentFramebuffer(); 169 context->deleteFramebuffer(fbo); 170 webglContext->restoreCurrentTexture2D(); 171 if (supportsDepth) 172 context->deleteTexture(depth); 173 if (supportsDepthStencil) 174 context->deleteTexture(depthStencil); 175 for (size_t i = 0; i < colors.size(); ++i) 176 context->deleteTexture(colors[i]); 177 return ok; 178 } 179 180 } // namespace WebCore 181