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 TextureMapperNode_h
     21 #define TextureMapperNode_h
     22 
     23 #include "FloatRect.h"
     24 #include "GraphicsContext.h"
     25 #include "GraphicsLayer.h"
     26 #include "Image.h"
     27 #include "TextureMapper.h"
     28 #include "TextureMapperPlatformLayer.h"
     29 #include "Timer.h"
     30 #include "TransformOperations.h"
     31 #include "TranslateTransformOperation.h"
     32 #include "UnitBezier.h"
     33 #include <wtf/CurrentTime.h>
     34 #include <wtf/HashMap.h>
     35 #include <wtf/RefCounted.h>
     36 
     37 namespace WebCore {
     38 
     39 class TextureMapperNode;
     40 class TextureMapperCache;
     41 class GraphicsLayerTextureMapper;
     42 
     43 struct TexmapPaintOptions {
     44     BitmapTexture* surface;
     45     TextureMapper* textureMapper;
     46     TextureMapperNode* rootLayer;
     47     float opacity;
     48     IntRect scissorRect;
     49     IntRect visibleRect;
     50     bool isSurface;
     51     TextureMapperCache* cache;
     52 };
     53 
     54 class TextureMapperNode : public TextureMapperContentLayer {
     55 
     56 public:
     57     // This set of flags help us defer which properties of the layer have been
     58     // modified by the compositor, so we can know what to look for in the next flush.
     59     enum ChangeMask {
     60         NoChanges =                 0,
     61 
     62         ParentChange =              (1L << 0),
     63         ChildrenChange =            (1L << 1),
     64         MaskLayerChange =           (1L << 2),
     65         PositionChange =            (1L << 3),
     66 
     67         AnchorPointChange =         (1L << 4),
     68         SizeChange  =               (1L << 5),
     69         TransformChange =           (1L << 6),
     70         ContentChange =             (1L << 7),
     71 
     72         ContentsOrientationChange = (1L << 9),
     73         OpacityChange =             (1L << 10),
     74         ContentsRectChange =        (1L << 11),
     75 
     76         Preserves3DChange =         (1L << 12),
     77         MasksToBoundsChange =       (1L << 13),
     78         DrawsContentChange =        (1L << 14),
     79         ContentsOpaqueChange =      (1L << 15),
     80 
     81         BackfaceVisibilityChange =  (1L << 16),
     82         ChildrenTransformChange =   (1L << 17),
     83         DisplayChange =             (1L << 18),
     84         BackgroundColorChange =     (1L << 19),
     85 
     86         ReplicaLayerChange =        (1L << 20)
     87     };
     88     // The compositor lets us special-case images and colors, so we try to do so.
     89     enum ContentType { HTMLContentType, DirectImageContentType, ColorContentType, MediaContentType, Canvas3DContentType};
     90     struct ContentData {
     91         IntRect needsDisplayRect;
     92         bool needsDisplay;
     93         Color backgroundColor;
     94 
     95         ContentType contentType;
     96         RefPtr<Image> image;
     97         TextureMapperMediaLayer* media;
     98 
     99         ContentData()
    100             : needsDisplay(false)
    101             , contentType(HTMLContentType)
    102             , image(0)
    103             , media(0)
    104         {
    105         }
    106     };
    107 
    108 
    109     TextureMapperNode();
    110     virtual ~TextureMapperNode();
    111 
    112     void syncCompositingState(GraphicsLayerTextureMapper*, bool recursive);
    113 
    114 protected:
    115     // Reimps from TextureMapperContentLayer
    116     virtual IntSize size() const { return m_size; }
    117     virtual void setPlatformLayerClient(TextureMapperLayerClient*);
    118     virtual void paint(TextureMapper*, const TextureMapperContentLayer::PaintOptions&);
    119 
    120 private:
    121     TextureMapperNode* rootLayer();
    122     void clearDirectImage();
    123     void computeTransformations();
    124     IntSize nearestSurfaceSize() const;
    125     void computeReplicaTransform();
    126     void computeLayerType();
    127     void computeLocalTransform();
    128     void flattenTo2DSpaceIfNecessary();
    129     void initializeTextureMapper(TextureMapper*);
    130     void invalidateTransform();
    131     void notifyChange(ChangeMask);
    132     void setNeedsDisplay();
    133     void setNeedsDisplayInRect(IntRect);
    134     void performPostSyncOperations();
    135     void syncCompositingStateInternal(GraphicsLayerTextureMapper*, bool recursive, TextureMapper*);
    136     void syncCompositingStateSelf(GraphicsLayerTextureMapper* graphicsLayer, TextureMapper* textureMapper);
    137     TextureMapperCache* cache();
    138 
    139     void paintRecursive(TexmapPaintOptions options);
    140     bool paintReplica(const TexmapPaintOptions& options);
    141     void paintSurface(const TexmapPaintOptions& options);
    142     void paintSelf(const TexmapPaintOptions& options);
    143     void paintSelfAndChildren(const TexmapPaintOptions& options, TexmapPaintOptions& optionsForDescendants);
    144     void uploadTextureFromContent(TextureMapper* textureMapper, const IntRect& visibleRect, GraphicsLayer* layer);
    145 
    146     int countDescendantsWithContent() const;
    147     bool hasSurfaceDescendants() const;
    148 
    149     TextureMapper* textureMapper();
    150 
    151 
    152     static TextureMapperNode* toTextureMapperNode(GraphicsLayer*);
    153     static int compareGraphicsLayersZValue(const void* a, const void* b);
    154     static void sortByZOrder(Vector<TextureMapperNode* >& array, int first, int last);
    155     struct TransformData {
    156         TransformationMatrix base, target, replica, forDescendants, perspective, local;
    157         IntRect targetBoundingRect;
    158         float centerZ;
    159         bool dirty, localDirty, perspectiveDirty;
    160         IntRect boundingRectFromRoot;
    161         TransformData() : dirty(true), localDirty(true), perspectiveDirty(true) { }
    162     };
    163 
    164     TransformData m_transforms;
    165 
    166     enum LayerType {
    167         DefaultLayer,
    168         RootLayer,
    169         ScissorLayer,
    170         ClipLayer,
    171         TransparencyLayer
    172     };
    173 
    174     LayerType m_layerType;
    175 
    176     inline IntRect targetRect() const
    177     {
    178         return m_currentContent.contentType == HTMLContentType ? entireRect() : m_state.contentsRect;
    179     }
    180 
    181     inline IntRect entireRect() const
    182     {
    183         return IntRect(0, 0, m_size.width(), m_size.height());
    184     }
    185 
    186     inline IntRect replicaRect() const
    187     {
    188         return m_layerType == TransparencyLayer ? IntRect(0, 0, m_nearestSurfaceSize.width(), m_nearestSurfaceSize.height()) : entireRect();
    189     }
    190 
    191     RefPtr<BitmapTexture> m_texture;
    192     RefPtr<BitmapTexture> m_surface, m_replicaSurface;
    193 
    194     ContentData m_currentContent;
    195 
    196     Vector<TextureMapperNode*> m_children;
    197     TextureMapperNode* m_parent;
    198     TextureMapperNode* m_effectTarget;
    199     IntSize m_size, m_nearestSurfaceSize;
    200     String m_name;
    201     TextureMapperLayerClient* m_platformClient;
    202 
    203     struct State {
    204         FloatPoint pos;
    205         FloatPoint3D anchorPoint;
    206         FloatSize size;
    207         TransformationMatrix transform;
    208         TransformationMatrix childrenTransform;
    209         Color backgroundColor;
    210         Color currentColor;
    211         GraphicsLayer::CompositingCoordinatesOrientation geoOrientation;
    212         GraphicsLayer::CompositingCoordinatesOrientation contentsOrientation;
    213         float opacity;
    214         IntRect contentsRect;
    215         int descendantsWithContent;
    216         TextureMapperNode* maskLayer;
    217         TextureMapperNode* replicaLayer;
    218         bool preserves3D;
    219         bool masksToBounds;
    220         bool drawsContent;
    221         bool contentsOpaque;
    222         bool backfaceVisibility;
    223         bool visible;
    224         bool dirty;
    225         bool tiled;
    226         bool hasSurfaceDescendants;
    227         IntRect visibleRect;
    228 
    229         State()
    230             : opacity(1.f)
    231             , descendantsWithContent(0)
    232             , maskLayer(0)
    233             , replicaLayer(0)
    234             , preserves3D(false)
    235             , masksToBounds(false)
    236             , drawsContent(false)
    237             , contentsOpaque(false)
    238             , backfaceVisibility(false)
    239             , visible(true)
    240             , dirty(true)
    241             , tiled(false)
    242             , hasSurfaceDescendants(false)
    243         {
    244         }
    245     };
    246 
    247     State m_state;
    248     TextureMapperCache* m_cache;
    249 };
    250 
    251 }
    252 #endif // TextureMapperNode_h
    253