1/*
2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB.  If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20#ifndef TextureMapper_h
21#define TextureMapper_h
22
23#if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
24
25#if PLATFORM(QT)
26#include <qglobal.h>
27#if defined(QT_OPENGL_ES_2) && !defined(TEXMAP_OPENGL_ES_2)
28    #define TEXMAP_OPENGL_ES_2
29#endif
30#endif
31#if (PLATFORM(GTK) || PLATFORM(EFL)) && USE(OPENGL_ES_2)
32#define TEXMAP_OPENGL_ES_2
33#endif
34
35#include "GraphicsContext.h"
36#include "IntRect.h"
37#include "IntSize.h"
38#include "TransformationMatrix.h"
39
40/*
41    TextureMapper is a mechanism that enables hardware acceleration of CSS animations (accelerated compositing) without
42    a need for a platform specific scene-graph library like CoreAnimations or QGraphicsView.
43*/
44
45namespace WebCore {
46
47class BitmapTexturePool;
48class CustomFilterProgram;
49class GraphicsLayer;
50class TextureMapper;
51class FilterOperations;
52
53// A 2D texture that can be the target of software or GL rendering.
54class BitmapTexture : public RefCounted<BitmapTexture> {
55public:
56    enum Flag {
57        SupportsAlpha = 0x01
58    };
59
60    enum UpdateContentsFlag {
61        UpdateCanModifyOriginalImageData,
62        UpdateCannotModifyOriginalImageData
63    };
64
65    typedef unsigned Flags;
66
67    BitmapTexture()
68        : m_flags(0)
69    {
70    }
71
72    virtual ~BitmapTexture() { }
73    virtual bool isBackedByOpenGL() const { return false; }
74
75    virtual IntSize size() const = 0;
76    virtual void updateContents(Image*, const IntRect&, const IntPoint& offset, UpdateContentsFlag) = 0;
77    virtual void updateContents(TextureMapper*, GraphicsLayer*, const IntRect& target, const IntPoint& offset, UpdateContentsFlag);
78    virtual void updateContents(const void*, const IntRect& target, const IntPoint& offset, int bytesPerLine, UpdateContentsFlag) = 0;
79    virtual bool isValid() const = 0;
80    inline Flags flags() const { return m_flags; }
81
82    virtual int bpp() const { return 32; }
83    virtual bool canReuseWith(const IntSize& /* contentsSize */, Flags = 0) { return false; }
84    void reset(const IntSize& size, Flags flags = 0)
85    {
86        m_flags = flags;
87        m_contentSize = size;
88        didReset();
89    }
90    virtual void didReset() { }
91
92    inline IntSize contentSize() const { return m_contentSize; }
93    inline int numberOfBytes() const { return size().width() * size().height() * bpp() >> 3; }
94    inline bool isOpaque() const { return !(m_flags & SupportsAlpha); }
95
96#if ENABLE(CSS_FILTERS)
97    virtual PassRefPtr<BitmapTexture> applyFilters(TextureMapper*, const FilterOperations&) { return this; }
98#endif
99
100protected:
101    IntSize m_contentSize;
102
103private:
104    Flags m_flags;
105};
106
107// A "context" class used to encapsulate accelerated texture mapping functions: i.e. drawing a texture
108// onto the screen or into another texture with a specified transform, opacity and mask.
109class TextureMapper {
110    WTF_MAKE_FAST_ALLOCATED;
111    friend class BitmapTexture;
112public:
113    enum AccelerationMode { SoftwareMode, OpenGLMode };
114    enum PaintFlag {
115        PaintingMirrored = 1 << 0,
116    };
117
118    enum WrapMode {
119        StretchWrap,
120        RepeatWrap
121    };
122
123    typedef unsigned PaintFlags;
124
125    static PassOwnPtr<TextureMapper> create(AccelerationMode newMode = SoftwareMode);
126    virtual ~TextureMapper();
127
128    enum ExposedEdges {
129        NoEdges = 0,
130        LeftEdge = 1 << 0,
131        RightEdge = 1 << 1,
132        TopEdge = 1 << 2,
133        BottomEdge = 1 << 3,
134        AllEdges = LeftEdge | RightEdge | TopEdge | BottomEdge,
135    };
136
137    virtual void drawBorder(const Color&, float borderWidth, const FloatRect&, const TransformationMatrix&) = 0;
138    virtual void drawNumber(int number, const Color&, const FloatPoint&, const TransformationMatrix&) = 0;
139
140    virtual void drawTexture(const BitmapTexture&, const FloatRect& target, const TransformationMatrix& modelViewMatrix = TransformationMatrix(), float opacity = 1.0f, unsigned exposedEdges = AllEdges) = 0;
141    virtual void drawSolidColor(const FloatRect&, const TransformationMatrix&, const Color&) = 0;
142
143    // makes a surface the target for the following drawTexture calls.
144    virtual void bindSurface(BitmapTexture* surface) = 0;
145    void setGraphicsContext(GraphicsContext* context) { m_context = context; }
146    GraphicsContext* graphicsContext() { return m_context; }
147    virtual void beginClip(const TransformationMatrix&, const FloatRect&) = 0;
148    virtual void endClip() = 0;
149    virtual IntRect clipBounds() = 0;
150    virtual PassRefPtr<BitmapTexture> createTexture() = 0;
151
152    void setImageInterpolationQuality(InterpolationQuality quality) { m_interpolationQuality = quality; }
153    void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
154
155    InterpolationQuality imageInterpolationQuality() const { return m_interpolationQuality; }
156    TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
157    AccelerationMode accelerationMode() const { return m_accelerationMode; }
158
159    virtual void beginPainting(PaintFlags = 0) { }
160    virtual void endPainting() { }
161
162    void setMaskMode(bool m) { m_isMaskMode = m; }
163
164    virtual IntSize maxTextureSize() const = 0;
165
166    virtual PassRefPtr<BitmapTexture> acquireTextureFromPool(const IntSize&);
167
168#if ENABLE(CSS_SHADERS)
169    virtual void removeCachedCustomFilterProgram(CustomFilterProgram*) { }
170#endif
171
172    void setPatternTransform(const TransformationMatrix& p) { m_patternTransform = p; }
173    void setWrapMode(WrapMode m) { m_wrapMode = m; }
174
175protected:
176    explicit TextureMapper(AccelerationMode);
177
178    GraphicsContext* m_context;
179
180    bool isInMaskMode() const { return m_isMaskMode; }
181    WrapMode wrapMode() const { return m_wrapMode; }
182    const TransformationMatrix& patternTransform() const { return m_patternTransform; }
183
184private:
185#if USE(TEXTURE_MAPPER_GL)
186    static PassOwnPtr<TextureMapper> platformCreateAccelerated();
187#else
188    static PassOwnPtr<TextureMapper> platformCreateAccelerated()
189    {
190        return PassOwnPtr<TextureMapper>();
191    }
192#endif
193    InterpolationQuality m_interpolationQuality;
194    TextDrawingModeFlags m_textDrawingMode;
195    OwnPtr<BitmapTexturePool> m_texturePool;
196    AccelerationMode m_accelerationMode;
197    bool m_isMaskMode;
198    TransformationMatrix m_patternTransform;
199    WrapMode m_wrapMode;
200};
201
202}
203
204#endif
205
206#endif
207