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 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.
24 */
25
26#ifndef WebGLTexture_h
27#define WebGLTexture_h
28
29#include "bindings/core/v8/ScriptWrappable.h"
30#include "core/html/canvas/WebGLSharedObject.h"
31#include "wtf/PassRefPtr.h"
32#include "wtf/Vector.h"
33
34namespace blink {
35
36class WebGLTexture FINAL : public WebGLSharedObject, public ScriptWrappable {
37    DEFINE_WRAPPERTYPEINFO();
38public:
39    enum TextureExtensionFlag {
40        NoTextureExtensionEnabled = 0,
41        TextureFloatLinearExtensionEnabled = 1 << 0,
42        TextureHalfFloatLinearExtensionEnabled = 1 << 1
43    };
44    virtual ~WebGLTexture();
45
46    static PassRefPtrWillBeRawPtr<WebGLTexture> create(WebGLRenderingContextBase*);
47
48    void setTarget(GLenum target, GLint maxLevel);
49    void setParameteri(GLenum pname, GLint param);
50    void setParameterf(GLenum pname, GLfloat param);
51
52    GLenum getTarget() const { return m_target; }
53
54    int getMinFilter() const { return m_minFilter; }
55
56    void setLevelInfo(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum type);
57
58    bool canGenerateMipmaps();
59    // Generate all level information.
60    void generateMipmapLevelInfo();
61
62    GLenum getInternalFormat(GLenum target, GLint level) const;
63    GLenum getType(GLenum target, GLint level) const;
64    GLsizei getWidth(GLenum target, GLint level) const;
65    GLsizei getHeight(GLenum target, GLint level) const;
66    bool isValid(GLenum target, GLint level) const;
67
68    // Whether width/height is NotPowerOfTwo.
69    static bool isNPOT(GLsizei, GLsizei);
70
71    bool isNPOT() const;
72    // Determine if texture sampling should always return [0, 0, 0, 1] (OpenGL ES 2.0 Sec 3.8.2).
73    bool needToUseBlackTexture(TextureExtensionFlag) const;
74
75    bool hasEverBeenBound() const { return object() && m_target; }
76
77    static GLint computeLevelCount(GLsizei width, GLsizei height);
78
79protected:
80    explicit WebGLTexture(WebGLRenderingContextBase*);
81
82    virtual void deleteObjectImpl(blink::WebGraphicsContext3D*, Platform3DObject) OVERRIDE;
83
84private:
85    class LevelInfo {
86    public:
87        LevelInfo()
88            : valid(false)
89            , internalFormat(0)
90            , width(0)
91            , height(0)
92            , type(0)
93        {
94        }
95
96        void setInfo(GLenum internalFmt, GLsizei w, GLsizei h, GLenum tp)
97        {
98            valid = true;
99            internalFormat = internalFmt;
100            width = w;
101            height = h;
102            type = tp;
103        }
104
105        bool valid;
106        GLenum internalFormat;
107        GLsizei width;
108        GLsizei height;
109        GLenum type;
110    };
111
112    virtual bool isTexture() const OVERRIDE { return true; }
113
114    void update();
115
116    int mapTargetToIndex(GLenum) const;
117
118    const LevelInfo* getLevelInfo(GLenum target, GLint level) const;
119
120    GLenum m_target;
121
122    GLenum m_minFilter;
123    GLenum m_magFilter;
124    GLenum m_wrapS;
125    GLenum m_wrapT;
126
127    Vector<Vector<LevelInfo> > m_info;
128
129    bool m_isNPOT;
130    bool m_isCubeComplete;
131    bool m_isComplete;
132    bool m_needToUseBlackTexture;
133    bool m_isFloatType;
134    bool m_isHalfFloatType;
135};
136
137} // namespace blink
138
139#endif // WebGLTexture_h
140