Home | History | Annotate | Download | only in texmap
      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)
     24 #if (defined(QT_OPENGL_LIB))
     25     #if defined(QT_OPENGL_ES_2) && !defined(TEXMAP_OPENGL_ES_2)
     26         #define TEXMAP_OPENGL_ES_2
     27     #endif
     28 #endif
     29 
     30 #include "GraphicsContext.h"
     31 #include "IntRect.h"
     32 #include "IntSize.h"
     33 #include "TransformationMatrix.h"
     34 #include <wtf/UnusedParam.h>
     35 
     36 /*
     37     TextureMapper is a mechanism that enables hardware acceleration of CSS animations (accelerated compositing) without
     38     a need for a platform specific scene-graph library like CoreAnimations or QGraphicsView.
     39 */
     40 
     41 namespace WebCore {
     42 
     43 class TextureMapper;
     44 
     45 // A 2D texture that can be the target of software or GL rendering.
     46 class BitmapTexture  : public RefCounted<BitmapTexture> {
     47 public:
     48     BitmapTexture() : m_lockCount(0) {}
     49     virtual ~BitmapTexture() { }
     50 
     51     virtual bool allowOfflineTextureUpload() const { return false; }
     52     virtual void destroy() = 0;
     53     virtual IntSize size() const = 0;
     54     virtual bool isValid() const = 0;
     55     virtual void reset(const IntSize& size, bool opaque = false)
     56     {
     57         m_isOpaque = opaque;
     58         m_contentSize = size;
     59     }
     60 
     61     virtual void pack() { }
     62     virtual void unpack() { }
     63     virtual bool isPacked() const { return false; }
     64 
     65     virtual PlatformGraphicsContext* beginPaint(const IntRect& dirtyRect) = 0;
     66     virtual void endPaint() = 0;
     67     virtual PlatformGraphicsContext* beginPaintMedia()
     68     {
     69         return beginPaint(IntRect(0, 0, size().width(), size().height()));
     70     }
     71     virtual void setContentsToImage(Image*) = 0;
     72     virtual bool save(const String&) { return false; }
     73 
     74     inline void lock() { ++m_lockCount; }
     75     inline void unlock() { --m_lockCount; }
     76     inline bool isLocked() { return m_lockCount; }
     77     inline IntSize contentSize() const { return m_contentSize; }
     78     inline void setOffset(const IntPoint& o) { m_offset = o; }
     79     inline IntPoint offset() const { return m_offset; }
     80 
     81 protected:
     82 
     83 private:
     84     int m_lockCount;
     85     IntSize m_contentSize;
     86     bool m_isOpaque;
     87     IntPoint m_offset;
     88 };
     89 
     90 // A "context" class used to encapsulate accelerated texture mapping functions: i.e. drawing a texture
     91 // onto the screen or into another texture with a specified transform, opacity and mask.
     92 class TextureMapper {
     93     friend class BitmapTexture;
     94 
     95 public:
     96     static PassOwnPtr<TextureMapper> create(GraphicsContext* graphicsContext = 0);
     97     virtual ~TextureMapper() { }
     98 
     99     virtual void drawTexture(const BitmapTexture& texture, const IntRect& target, const TransformationMatrix& matrix = TransformationMatrix(), float opacity = 1.0f, const BitmapTexture* maskTexture = 0) = 0;
    100 
    101     // makes a surface the target for the following drawTexture calls.
    102     virtual void bindSurface(BitmapTexture* surface) = 0;
    103     virtual void paintToTarget(const BitmapTexture& texture, const IntSize&, const TransformationMatrix& matrix, float opacity, const IntRect& visibleRect)
    104     {
    105         UNUSED_PARAM(visibleRect);
    106         drawTexture(texture, IntRect(0, 0, texture.contentSize().width(), texture.contentSize().height()), matrix, opacity, 0);
    107     }
    108 
    109     virtual void setGraphicsContext(GraphicsContext*) { }
    110     virtual void setClip(const IntRect&) = 0;
    111     virtual bool allowSurfaceForRoot() const = 0;
    112     virtual PassRefPtr<BitmapTexture> createTexture() = 0;
    113 
    114     void setImageInterpolationQuality(InterpolationQuality quality) { m_interpolationQuality = quality; }
    115     void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
    116 
    117     InterpolationQuality imageInterpolationQuality() const { return m_interpolationQuality; }
    118     TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
    119 
    120     void setViewportSize(const IntSize&);
    121 
    122 protected:
    123     TextureMapper()
    124         : m_interpolationQuality(InterpolationDefault)
    125         , m_textDrawingMode(TextModeFill)
    126     {}
    127 
    128 private:
    129     InterpolationQuality m_interpolationQuality;
    130     TextDrawingModeFlags m_textDrawingMode;
    131 };
    132 
    133 };
    134 
    135 #endif
    136 
    137 #endif
    138