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