2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/html/canvas/WebGLTexture.h"
30 #include "core/html/canvas/WebGLRenderingContextBase.h"
34 PassRefPtrWillBeRawPtr<WebGLTexture> WebGLTexture::create(WebGLRenderingContextBase* ctx)
36 return adoptRefWillBeNoop(new WebGLTexture(ctx));
39 WebGLTexture::WebGLTexture(WebGLRenderingContextBase* ctx)
40 : WebGLSharedObject(ctx)
42 , m_minFilter(GL_NEAREST_MIPMAP_LINEAR)
43 , m_magFilter(GL_LINEAR)
47 , m_isCubeComplete(false)
49 , m_needToUseBlackTexture(false)
50 , m_isFloatType(false)
51 , m_isHalfFloatType(false)
53 setObject(ctx->webContext()->createTexture());
56 WebGLTexture::~WebGLTexture()
58 // Always perform detach here to ensure that platform object
59 // deletion happens with Oilpan enabled. It keeps the code regular
60 // to do it with or without Oilpan enabled.
62 // See comment in WebGLBuffer's destructor for additional
63 // information on why this is done for WebGLSharedObject-derived
65 detachAndDeleteObject();
68 void WebGLTexture::setTarget(GLenum target, GLint maxLevel)
72 // Target is finalized the first time bindTexture() is called.
79 m_info[0].resize(maxLevel);
81 case GL_TEXTURE_CUBE_MAP:
84 for (int ii = 0; ii < 6; ++ii)
85 m_info[ii].resize(maxLevel);
90 void WebGLTexture::setParameteri(GLenum pname, GLint param)
92 if (!object() || !m_target)
95 case GL_TEXTURE_MIN_FILTER:
99 case GL_NEAREST_MIPMAP_NEAREST:
100 case GL_LINEAR_MIPMAP_NEAREST:
101 case GL_NEAREST_MIPMAP_LINEAR:
102 case GL_LINEAR_MIPMAP_LINEAR:
107 case GL_TEXTURE_MAG_FILTER:
115 case GL_TEXTURE_WRAP_S:
117 case GL_CLAMP_TO_EDGE:
118 case GL_MIRRORED_REPEAT:
124 case GL_TEXTURE_WRAP_T:
126 case GL_CLAMP_TO_EDGE:
127 case GL_MIRRORED_REPEAT:
139 void WebGLTexture::setParameterf(GLenum pname, GLfloat param)
141 if (!object() || !m_target)
143 GLint iparam = static_cast<GLint>(param);
144 setParameteri(pname, iparam);
147 void WebGLTexture::setLevelInfo(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum type)
149 if (!object() || !m_target)
151 // We assume level, internalFormat, width, height, and type have all been
152 // validated already.
153 int index = mapTargetToIndex(target);
156 m_info[index][level].setInfo(internalFormat, width, height, type);
160 void WebGLTexture::generateMipmapLevelInfo()
162 if (!object() || !m_target)
164 if (!canGenerateMipmaps())
167 for (size_t ii = 0; ii < m_info.size(); ++ii) {
168 const LevelInfo& info0 = m_info[ii][0];
169 GLsizei width = info0.width;
170 GLsizei height = info0.height;
171 GLint levelCount = computeLevelCount(width, height);
172 for (GLint level = 1; level < levelCount; ++level) {
173 width = std::max(1, width >> 1);
174 height = std::max(1, height >> 1);
175 LevelInfo& info = m_info[ii][level];
176 info.setInfo(info0.internalFormat, width, height, info0.type);
181 m_needToUseBlackTexture = false;
184 GLenum WebGLTexture::getInternalFormat(GLenum target, GLint level) const
186 const LevelInfo* info = getLevelInfo(target, level);
189 return info->internalFormat;
192 GLenum WebGLTexture::getType(GLenum target, GLint level) const
194 const LevelInfo* info = getLevelInfo(target, level);
200 GLsizei WebGLTexture::getWidth(GLenum target, GLint level) const
202 const LevelInfo* info = getLevelInfo(target, level);
208 GLsizei WebGLTexture::getHeight(GLenum target, GLint level) const
210 const LevelInfo* info = getLevelInfo(target, level);
216 bool WebGLTexture::isValid(GLenum target, GLint level) const
218 const LevelInfo* info = getLevelInfo(target, level);
224 bool WebGLTexture::isNPOT(GLsizei width, GLsizei height)
226 ASSERT(width >= 0 && height >= 0);
227 if (!width || !height)
229 if ((width & (width - 1)) || (height & (height - 1)))
234 bool WebGLTexture::isNPOT() const
241 bool WebGLTexture::needToUseBlackTexture(TextureExtensionFlag flag) const
245 if (m_needToUseBlackTexture)
247 if ((m_isFloatType && !(flag & TextureFloatLinearExtensionEnabled)) || (m_isHalfFloatType && !(flag && TextureHalfFloatLinearExtensionEnabled))) {
248 if (m_magFilter != GL_NEAREST || (m_minFilter != GL_NEAREST && m_minFilter != GL_NEAREST_MIPMAP_NEAREST))
254 void WebGLTexture::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
256 context3d->deleteTexture(object);
259 int WebGLTexture::mapTargetToIndex(GLenum target) const
261 if (m_target == GL_TEXTURE_2D) {
262 if (target == GL_TEXTURE_2D)
264 } else if (m_target == GL_TEXTURE_CUBE_MAP) {
266 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
268 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
270 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
272 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
274 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
276 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
283 bool WebGLTexture::canGenerateMipmaps()
287 const LevelInfo& first = m_info[0][0];
288 for (size_t ii = 0; ii < m_info.size(); ++ii) {
289 const LevelInfo& info = m_info[ii][0];
291 || info.width != first.width || info.height != first.height
292 || info.internalFormat != first.internalFormat || info.type != first.type
293 || (m_info.size() > 1 && !m_isCubeComplete))
299 GLint WebGLTexture::computeLevelCount(GLsizei width, GLsizei height)
301 // return 1 + log2Floor(std::max(width, height));
302 GLsizei n = std::max(width, height);
307 for (int ii = 4; ii >= 0; --ii) {
308 int shift = (1 << ii);
309 GLsizei x = (value >> shift);
319 void WebGLTexture::update()
322 for (size_t ii = 0; ii < m_info.size(); ++ii) {
323 if (isNPOT(m_info[ii][0].width, m_info[ii][0].height)) {
329 m_isCubeComplete = true;
330 const LevelInfo& first = m_info[0][0];
331 GLint levelCount = computeLevelCount(first.width, first.height);
333 m_isComplete = false;
335 for (size_t ii = 0; ii < m_info.size() && m_isComplete; ++ii) {
336 const LevelInfo& info0 = m_info[ii][0];
338 || info0.width != first.width || info0.height != first.height
339 || info0.internalFormat != first.internalFormat || info0.type != first.type
340 || (m_info.size() > 1 && info0.width != info0.height)) {
341 if (m_info.size() > 1)
342 m_isCubeComplete = false;
343 m_isComplete = false;
346 GLsizei width = info0.width;
347 GLsizei height = info0.height;
348 for (GLint level = 1; level < levelCount; ++level) {
349 width = std::max(1, width >> 1);
350 height = std::max(1, height >> 1);
351 const LevelInfo& info = m_info[ii][level];
353 || info.width != width || info.height != height
354 || info.internalFormat != info0.internalFormat || info.type != info0.type) {
355 m_isComplete = false;
362 m_isFloatType = m_info[0][0].type == GL_FLOAT;
363 m_isHalfFloatType = m_info[0][0].type == GL_HALF_FLOAT_OES;
365 m_needToUseBlackTexture = false;
367 if (m_isNPOT && ((m_minFilter != GL_NEAREST && m_minFilter != GL_LINEAR)
368 || m_wrapS != GL_CLAMP_TO_EDGE || m_wrapT != GL_CLAMP_TO_EDGE))
369 m_needToUseBlackTexture = true;
370 // If it is a Cube texture, check Cube Completeness first
371 if (m_info.size() > 1 && !m_isCubeComplete)
372 m_needToUseBlackTexture = true;
374 if (!m_isComplete && m_minFilter != GL_NEAREST && m_minFilter != GL_LINEAR)
375 m_needToUseBlackTexture = true;
378 const WebGLTexture::LevelInfo* WebGLTexture::getLevelInfo(GLenum target, GLint level) const
380 if (!object() || !m_target)
382 int targetIndex = mapTargetToIndex(target);
383 if (targetIndex < 0 || targetIndex >= static_cast<int>(m_info.size()))
385 if (level < 0 || level >= static_cast<GLint>(m_info[targetIndex].size()))
387 return &(m_info[targetIndex][level]);