| 1 | /* |
| 2 | * Copyright (C) 2009 Apple 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 | * 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. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE 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 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. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "WebGLSharedObject.h" |
| 29 | #include <wtf/Vector.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | class WebGLTexture final : public WebGLSharedObject { |
| 34 | public: |
| 35 | |
| 36 | enum TextureExtensionFlag { |
| 37 | TextureExtensionsDisabled = 0, |
| 38 | TextureExtensionFloatLinearEnabled = 1 << 0, |
| 39 | TextureExtensionHalfFloatLinearEnabled = 2 << 0 |
| 40 | }; |
| 41 | |
| 42 | virtual ~WebGLTexture(); |
| 43 | |
| 44 | static Ref<WebGLTexture> create(WebGLRenderingContextBase&); |
| 45 | |
| 46 | void setTarget(GC3Denum target, GC3Dint maxLevel); |
| 47 | void setParameteri(GC3Denum pname, GC3Dint param); |
| 48 | void setParameterf(GC3Denum pname, GC3Dfloat param); |
| 49 | |
| 50 | GC3Denum getTarget() const { return m_target; } |
| 51 | |
| 52 | int getMinFilter() const { return m_minFilter; } |
| 53 | |
| 54 | void setLevelInfo(GC3Denum target, GC3Dint level, GC3Denum internalFormat, GC3Dsizei width, GC3Dsizei height, GC3Denum type); |
| 55 | |
| 56 | bool canGenerateMipmaps(); |
| 57 | // Generate all level information. |
| 58 | void generateMipmapLevelInfo(); |
| 59 | |
| 60 | GC3Denum getInternalFormat(GC3Denum target, GC3Dint level) const; |
| 61 | GC3Denum getType(GC3Denum target, GC3Dint level) const; |
| 62 | GC3Dsizei getWidth(GC3Denum target, GC3Dint level) const; |
| 63 | GC3Dsizei getHeight(GC3Denum target, GC3Dint level) const; |
| 64 | bool isValid(GC3Denum target, GC3Dint level) const; |
| 65 | void markInvalid(GC3Denum target, GC3Dint level); |
| 66 | |
| 67 | // Whether width/height is NotPowerOfTwo. |
| 68 | static bool isNPOT(GC3Dsizei, GC3Dsizei); |
| 69 | |
| 70 | bool isNPOT() const; |
| 71 | // Determine if texture sampling should always return [0, 0, 0, 1] (OpenGL ES 2.0 Sec 3.8.2). |
| 72 | bool needToUseBlackTexture(TextureExtensionFlag) const; |
| 73 | |
| 74 | bool isCompressed() const; |
| 75 | void setCompressed(); |
| 76 | |
| 77 | bool hasEverBeenBound() const { return object() && m_target; } |
| 78 | |
| 79 | static GC3Dint computeLevelCount(GC3Dsizei width, GC3Dsizei height); |
| 80 | |
| 81 | bool immutable() const { return m_immutable; } |
| 82 | void setImmutable() { m_immutable = true; } |
| 83 | |
| 84 | private: |
| 85 | WebGLTexture(WebGLRenderingContextBase&); |
| 86 | |
| 87 | void deleteObjectImpl(GraphicsContext3D*, Platform3DObject) override; |
| 88 | |
| 89 | class LevelInfo { |
| 90 | public: |
| 91 | LevelInfo() |
| 92 | : valid(false) |
| 93 | , internalFormat(0) |
| 94 | , width(0) |
| 95 | , height(0) |
| 96 | , type(0) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | void setInfo(GC3Denum internalFmt, GC3Dsizei w, GC3Dsizei h, GC3Denum tp) |
| 101 | { |
| 102 | valid = true; |
| 103 | internalFormat = internalFmt; |
| 104 | width = w; |
| 105 | height = h; |
| 106 | type = tp; |
| 107 | } |
| 108 | |
| 109 | bool valid; |
| 110 | GC3Denum internalFormat; |
| 111 | GC3Dsizei width; |
| 112 | GC3Dsizei height; |
| 113 | GC3Denum type; |
| 114 | }; |
| 115 | |
| 116 | bool isTexture() const override { return true; } |
| 117 | |
| 118 | void update(); |
| 119 | |
| 120 | int mapTargetToIndex(GC3Denum) const; |
| 121 | |
| 122 | const LevelInfo* getLevelInfo(GC3Denum target, GC3Dint level) const; |
| 123 | |
| 124 | GC3Denum m_target; |
| 125 | |
| 126 | GC3Denum m_minFilter; |
| 127 | GC3Denum m_magFilter; |
| 128 | GC3Denum m_wrapS; |
| 129 | GC3Denum m_wrapT; |
| 130 | |
| 131 | Vector<Vector<LevelInfo>> m_info; |
| 132 | |
| 133 | bool m_isNPOT; |
| 134 | bool m_isComplete; |
| 135 | bool m_needToUseBlackTexture; |
| 136 | bool m_isCompressed; |
| 137 | bool m_isFloatType; |
| 138 | bool m_isHalfFloatType; |
| 139 | bool m_isForWebGL1; |
| 140 | bool m_immutable { false }; |
| 141 | }; |
| 142 | |
| 143 | } // namespace WebCore |
| 144 | |