2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
20 #ifndef TextureMapper_h
21 #define TextureMapper_h
23 #if USE(ACCELERATED_COMPOSITING)
27 #if defined(QT_OPENGL_ES_2) && !defined(TEXMAP_OPENGL_ES_2)
28 #define TEXMAP_OPENGL_ES_2
31 #if (PLATFORM(GTK) || PLATFORM(EFL)) && USE(OPENGL_ES_2)
32 #define TEXMAP_OPENGL_ES_2
35 #include "FilterOperations.h"
36 #include "GraphicsContext.h"
39 #include "TransformationMatrix.h"
40 #include <wtf/UnusedParam.h>
43 TextureMapper is a mechanism that enables hardware acceleration of CSS animations (accelerated compositing) without
44 a need for a platform specific scene-graph library like CoreAnimations or QGraphicsView.
49 class BitmapTexturePool;
50 class CustomFilterProgram;
54 // A 2D texture that can be the target of software or GL rendering.
55 class BitmapTexture : public RefCounted<BitmapTexture> {
61 enum UpdateContentsFlag {
62 UpdateCanModifyOriginalImageData,
63 UpdateCannotModifyOriginalImageData
66 typedef unsigned Flags;
73 virtual ~BitmapTexture() { }
74 virtual bool isBackedByOpenGL() const { return false; }
76 virtual IntSize size() const = 0;
77 virtual void updateContents(Image*, const IntRect&, const IntPoint& offset, UpdateContentsFlag) = 0;
78 virtual void updateContents(TextureMapper*, GraphicsLayer*, const IntRect& target, const IntPoint& offset, UpdateContentsFlag);
79 virtual void updateContents(const void*, const IntRect& target, const IntPoint& offset, int bytesPerLine, UpdateContentsFlag) = 0;
80 virtual bool isValid() const = 0;
81 inline Flags flags() const { return m_flags; }
83 virtual int bpp() const { return 32; }
84 virtual bool canReuseWith(const IntSize& /* contentsSize */, Flags = 0) { return false; }
85 void reset(const IntSize& size, Flags flags = 0)
91 virtual void didReset() { }
93 inline IntSize contentSize() const { return m_contentSize; }
94 inline int numberOfBytes() const { return size().width() * size().height() * bpp() >> 3; }
95 inline bool isOpaque() const { return !(m_flags & SupportsAlpha); }
97 #if ENABLE(CSS_FILTERS)
98 virtual PassRefPtr<BitmapTexture> applyFilters(TextureMapper*, const BitmapTexture& contentTexture, const FilterOperations&) { return this; }
102 IntSize m_contentSize;
108 // A "context" class used to encapsulate accelerated texture mapping functions: i.e. drawing a texture
109 // onto the screen or into another texture with a specified transform, opacity and mask.
110 class TextureMapper {
111 WTF_MAKE_FAST_ALLOCATED;
112 friend class BitmapTexture;
114 enum AccelerationMode { SoftwareMode, OpenGLMode };
116 PaintingMirrored = 1 << 0,
118 typedef unsigned PaintFlags;
120 static PassOwnPtr<TextureMapper> create(AccelerationMode newMode = SoftwareMode);
121 virtual ~TextureMapper();
129 AllEdges = LeftEdge | RightEdge | TopEdge | BottomEdge,
132 virtual void drawBorder(const Color&, float borderWidth, const FloatRect&, const TransformationMatrix&) = 0;
133 virtual void drawNumber(int number, const Color&, const FloatPoint&, const TransformationMatrix&) = 0;
135 virtual void drawTexture(const BitmapTexture&, const FloatRect& target, const TransformationMatrix& modelViewMatrix = TransformationMatrix(), float opacity = 1.0f, unsigned exposedEdges = AllEdges) = 0;
136 virtual void drawSolidColor(const FloatRect&, const TransformationMatrix&, const Color&) = 0;
138 // makes a surface the target for the following drawTexture calls.
139 virtual void bindSurface(BitmapTexture* surface) = 0;
140 void setGraphicsContext(GraphicsContext* context) { m_context = context; }
141 GraphicsContext* graphicsContext() { return m_context; }
142 virtual void beginClip(const TransformationMatrix&, const FloatRect&) = 0;
143 virtual void endClip() = 0;
144 virtual PassRefPtr<BitmapTexture> createTexture() = 0;
146 void setImageInterpolationQuality(InterpolationQuality quality) { m_interpolationQuality = quality; }
147 void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
149 InterpolationQuality imageInterpolationQuality() const { return m_interpolationQuality; }
150 TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
151 AccelerationMode accelerationMode() const { return m_accelerationMode; }
153 virtual void beginPainting(PaintFlags = 0) { }
154 virtual void endPainting() { }
156 void setMaskMode(bool m) { m_isMaskMode = m; }
158 virtual IntSize maxTextureSize() const = 0;
160 virtual PassRefPtr<BitmapTexture> acquireTextureFromPool(const IntSize&);
162 #if ENABLE(CSS_SHADERS)
163 virtual void removeCachedCustomFilterProgram(CustomFilterProgram*) { }
167 explicit TextureMapper(AccelerationMode);
169 GraphicsContext* m_context;
171 bool isInMaskMode() const { return m_isMaskMode; }
174 #if USE(TEXTURE_MAPPER_GL)
175 static PassOwnPtr<TextureMapper> platformCreateAccelerated();
177 static PassOwnPtr<TextureMapper> platformCreateAccelerated()
179 return PassOwnPtr<TextureMapper>();
182 InterpolationQuality m_interpolationQuality;
183 TextDrawingModeFlags m_textDrawingMode;
184 OwnPtr<BitmapTexturePool> m_texturePool;
185 AccelerationMode m_accelerationMode;