Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2010 Google 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  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 
     27 #ifndef LayerTilerChromium_h
     28 #define LayerTilerChromium_h
     29 
     30 #if USE(ACCELERATED_COMPOSITING)
     31 
     32 #include "LayerChromium.h"
     33 #include "LayerTexture.h"
     34 #include "PlatformCanvas.h"
     35 #include "TilingData.h"
     36 #include <wtf/HashTraits.h>
     37 #include <wtf/OwnArrayPtr.h>
     38 #include <wtf/RefCounted.h>
     39 
     40 namespace WebCore {
     41 
     42 class GraphicsContext3D;
     43 class LayerRendererChromium;
     44 
     45 class TilePaintInterface {
     46 public:
     47     virtual void paint(GraphicsContext& context, const IntRect& contentRect) = 0;
     48 };
     49 
     50 class LayerTilerChromium {
     51     WTF_MAKE_NONCOPYABLE(LayerTilerChromium);
     52 public:
     53     enum BorderTexelOption { HasBorderTexels, NoBorderTexels };
     54 
     55     static PassOwnPtr<LayerTilerChromium> create(LayerRendererChromium*, const IntSize& tileSize, BorderTexelOption);
     56 
     57     ~LayerTilerChromium();
     58 
     59     // Set invalidations to be potentially repainted during update().
     60     void invalidateRect(const IntRect& contentRect);
     61     void invalidateEntireLayer();
     62 
     63     // Paint all invalidations and missing tiles needed to draw the contentRect
     64     // into the internal canvas.
     65     void update(TilePaintInterface& painter, const IntRect& contentRect);
     66 
     67     // Reserve and upload tile textures from the internal canvas.
     68     void uploadCanvas();
     69 
     70     // Reserve and upload tile textures from an externally painted buffer.
     71     void updateFromPixels(const IntRect& contentRect, const IntRect& paintRect, const uint8_t* pixels);
     72 
     73     // Draw all tiles that intersect with the content rect.
     74     void draw(const IntRect& contentRect, const TransformationMatrix&, float opacity);
     75 
     76     // Set position of this tiled layer in content space.
     77     void setLayerPosition(const IntPoint& position);
     78     // Change the tile size.  This may invalidate all the existing tiles.
     79     void setTileSize(const IntSize& size);
     80     void setLayerRenderer(LayerRendererChromium*);
     81 
     82     bool skipsDraw() const { return m_skipsDraw; }
     83 
     84     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderTexAlpha> Program;
     85 
     86     // If this tiler has exactly one tile, return its texture. Otherwise, null.
     87     LayerTexture* getSingleTexture();
     88 
     89 private:
     90     LayerTilerChromium(LayerRendererChromium*, const IntSize& tileSize, BorderTexelOption);
     91 
     92     class Tile : public RefCounted<Tile> {
     93         WTF_MAKE_NONCOPYABLE(Tile);
     94     public:
     95         explicit Tile(PassOwnPtr<LayerTexture> tex) : m_tex(tex), m_i(-1), m_j(-1) {}
     96 
     97         LayerTexture* texture() { return m_tex.get(); }
     98 
     99         bool dirty() const { return !m_dirtyLayerRect.isEmpty(); }
    100         void clearDirty() { m_dirtyLayerRect = IntRect(); }
    101 
    102         int i() const { return m_i; }
    103         int j() const { return m_j; }
    104         void moveTo(int i, int j) { m_i = i; m_j = j; }
    105 
    106         // Layer-space dirty rectangle that needs to be repainted.
    107         IntRect m_dirtyLayerRect;
    108     private:
    109         OwnPtr<LayerTexture> m_tex;
    110         int m_i;
    111         int m_j;
    112     };
    113 
    114     void drawTexturedQuad(GraphicsContext3D*, const TransformationMatrix& projectionMatrix, const TransformationMatrix& drawMatrix,
    115                           float width, float height, float opacity,
    116                           float texTranslateX, float texTranslateY,
    117                           float texScaleX, float texScaleY,
    118                           const LayerTilerChromium::Program*);
    119 
    120     // Grow layer size to contain this rectangle.
    121     void growLayerToContain(const IntRect& contentRect);
    122 
    123     LayerRendererChromium* layerRenderer() const { return m_layerRenderer; }
    124     GraphicsContext3D* layerRendererContext() const;
    125     Tile* createTile(int i, int j);
    126     // Invalidate any tiles which do not intersect with the contentRect
    127     void invalidateTiles(const IntRect& contentRect);
    128     void reset();
    129     void contentRectToTileIndices(const IntRect& contentRect, int &left, int &top, int &right, int &bottom) const;
    130     IntRect contentRectToLayerRect(const IntRect& contentRect) const;
    131     IntRect layerRectToContentRect(const IntRect& layerRect) const;
    132 
    133     Tile* tileAt(int, int) const;
    134     IntRect tileContentRect(const Tile*) const;
    135     IntRect tileLayerRect(const Tile*) const;
    136 
    137     IntSize m_tileSize;
    138     IntPoint m_layerPosition;
    139 
    140     bool m_skipsDraw;
    141 
    142     // Default hash key traits for integers disallow 0 and -1 as a key, so
    143     // use a custom hash trait which disallows -1 and -2 instead.
    144     typedef std::pair<int, int> TileMapKey;
    145     struct TileMapKeyTraits : HashTraits<TileMapKey> {
    146         static const bool emptyValueIsZero = false;
    147         static const bool needsDestruction = false;
    148         static TileMapKey emptyValue() { return std::make_pair(-1, -1); }
    149         static void constructDeletedValue(TileMapKey& slot) { slot = std::make_pair(-2, -2); }
    150         static bool isDeletedValue(TileMapKey value) { return value.first == -2 && value.second == -2; }
    151     };
    152     // FIXME: The mapped value in TileMap should really be an OwnPtr, as the
    153     // refcount of a Tile should never be more than 1. However, HashMap
    154     // doesn't easily support OwnPtr as a value.
    155     typedef HashMap<TileMapKey, RefPtr<Tile>, DefaultHash<TileMapKey>::Hash, TileMapKeyTraits> TileMap;
    156     TileMap m_tiles;
    157     // Tightly packed set of unused tiles.
    158     Vector<RefPtr<Tile> > m_unusedTiles;
    159 
    160     // State held between update and uploadCanvas.
    161     IntRect m_paintRect;
    162     IntRect m_updateRect;
    163     PlatformCanvas m_canvas;
    164 
    165     // Cache a tile-sized pixel buffer to draw into.
    166     OwnArrayPtr<uint8_t> m_tilePixels;
    167 
    168     TilingData m_tilingData;
    169 
    170     LayerRendererChromium* m_layerRenderer;
    171 };
    172 
    173 }
    174 
    175 #endif // USE(ACCELERATED_COMPOSITING)
    176 
    177 #endif
    178