Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright 2010, The Android Open Source Project
      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  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Tile_h
     27 #define Tile_h
     28 
     29 #if USE(ACCELERATED_COMPOSITING)
     30 
     31 #include "BaseRenderer.h"
     32 #include "FloatPoint.h"
     33 #include "SkRect.h"
     34 #include "SkRegion.h"
     35 #include "TextureOwner.h"
     36 #include "TilePainter.h"
     37 
     38 #include <utils/threads.h>
     39 
     40 namespace WebCore {
     41 
     42 class TextureInfo;
     43 class TileTexture;
     44 class GLWebViewState;
     45 
     46 /**
     47  * An individual tile that is used to construct part of a webpage's BaseLayer of
     48  * content.  Each tile is assigned to a TiledPage and is responsible for drawing
     49  * and displaying their section of the page.  The lifecycle of a tile is:
     50  *
     51  * 1. Each tile is created on the main GL thread and assigned to a specific
     52  *    location within a TiledPage.
     53  * 2. When needed the tile is passed to the background thread where it paints
     54  *    the BaseLayer's most recent PictureSet to a bitmap which is then uploaded
     55  *    to the GPU.
     56  * 3. After the bitmap is uploaded to the GPU the main GL thread then uses the
     57  *    tile's drawGL() function to display the tile to the screen.
     58  * 4. Steps 2-3 are repeated as necessary.
     59  * 5. The tile is destroyed when the user navigates to a new page.
     60  *
     61  */
     62 class Tile : public TextureOwner {
     63 public:
     64 
     65     // eventually, m_dirty might be rolled into the state machine, but note
     66     // that a tile that's continually marked dirty from animation should still
     67     // progress through the state machine and be drawn periodically (esp. for
     68     // layers)
     69 
     70     //                                /->  TransferredUnvalidated (TQ interrupts paint)    -\   (TQ & paint done)
     71     // Unpainted -> PaintingStarted --                                                       ->    ReadyToSwap    -> UpToDate
     72     //     ^                          \->  ValidatedUntransferred (paint finish before TQ) -/
     73     //     |
     74     //     \--... (From any state when marked dirty. should usually come from UpToDate if the updates are locked)
     75     //
     76 
     77     enum TextureState{
     78         // back texture is completely unpainted
     79         Unpainted = 0,
     80         // has started painting, but haven't been transferred or validated
     81         PaintingStarted = 1,
     82         // back texture painted, transferred before validating in PaintBitmap()
     83         TransferredUnvalidated = 2,
     84         // back texture painted, validated before transferring in TransferQueue
     85         ValidatedUntransferred = 3,
     86         // back texture has been blitted, will be swapped when next available
     87         ReadyToSwap = 4,
     88         // has been swapped, is ready to draw, all is well
     89         UpToDate = 5,
     90     };
     91 
     92     Tile(bool isLayerTile = false);
     93     ~Tile();
     94 
     95     bool isLayerTile() { return m_isLayerTile; }
     96 
     97     void setContents(int x, int y, float scale, bool isExpandedPrefetchTile);
     98 
     99     void reserveTexture();
    100 
    101     bool isTileReady();
    102 
    103     // Return false when real draw didn't happen for any reason.
    104     bool drawGL(float opacity, const SkRect& rect, float scale,
    105                 const TransformationMatrix* transform,
    106                 bool forceBlending, bool usePointSampling,
    107                 const FloatRect& fillPortion);
    108 
    109     // the only thread-safe function called by the background thread
    110     void paintBitmap(TilePainter* painter);
    111 
    112     bool intersectWithRect(int x, int y, int tileWidth, int tileHeight,
    113                            float scale, const SkRect& dirtyRect,
    114                            SkRect& realTileRect);
    115     bool isTileVisible(const IntRect& viewTileBounds);
    116 
    117     void markAsDirty();
    118     void markAsDirty(const SkRegion& dirtyArea);
    119     bool isDirty();
    120     const SkRegion& dirtyArea() { return m_dirtyArea; }
    121     virtual bool isRepaintPending();
    122     void setRepaintPending(bool pending);
    123     float scale() const { return m_scale; }
    124     TextureState textureState() const { return m_state; }
    125 
    126     int x() const { return m_x; }
    127     int y() const { return m_y; }
    128     TileTexture* frontTexture() { return m_frontTexture; }
    129     TileTexture* backTexture() { return m_backTexture; }
    130     TileTexture* lastDrawnTexture() { return m_lastDrawnTexture; }
    131 
    132     // only used for prioritization - the higher, the more relevant the tile is
    133     unsigned long long drawCount() { return m_drawCount; }
    134     void discardTextures();
    135     void discardBackTexture();
    136     bool swapTexturesIfNeeded();
    137     void backTextureTransfer();
    138     void backTextureTransferFail();
    139     void onBlitUpdate();
    140 
    141     // TextureOwner implementation
    142     virtual bool removeTexture(TileTexture* texture);
    143 
    144 private:
    145     void markAsDirtyInternal();
    146     void validatePaint();
    147 
    148     int m_x;
    149     int m_y;
    150 
    151     // The remaining variables can be updated throughout the lifetime of the object
    152 
    153     TileTexture* m_frontTexture;
    154     TileTexture* m_backTexture;
    155     TileTexture* m_lastDrawnTexture;
    156     float m_scale;
    157 
    158     // used to signal that the that the tile is out-of-date and needs to be
    159     // redrawn in the backTexture
    160     bool m_dirty;
    161 
    162     // number of repaints pending
    163     int m_repaintsPending;
    164 
    165     // store the dirty region
    166     SkRegion m_dirtyArea;
    167     bool m_fullRepaint;
    168 
    169     // This mutex serves two purposes. (1) It ensures that certain operations
    170     // happen atomically and (2) it makes sure those operations are synchronized
    171     // across all threads and cores.
    172     android::Mutex m_atomicSync;
    173 
    174     BaseRenderer* m_renderer;
    175 
    176     bool m_isLayerTile;
    177 
    178     // the most recent GL draw before this tile was prepared. used for
    179     // prioritization and caching. tiles with old drawcounts and textures they
    180     // own are used for new tiles and rendering
    181     unsigned long long m_drawCount;
    182 
    183     // Tracks the state of painting for the tile. High level overview:
    184     // 1) Unpainted - until paint starts (and if marked dirty, in most cases)
    185     // 2) PaintingStarted - until paint completes
    186     // 3) TransferredUnvalidated - if transferred first
    187     //    or ValidatedUntransferred - if validated first
    188     // 4) ReadyToSwap - if painted and transferred, but not swapped
    189     // 5) UpToDate - until marked dirty again
    190     TextureState m_state;
    191 };
    192 
    193 } // namespace WebCore
    194 
    195 #endif // USE(ACCELERATED_COMPOSITING)
    196 #endif // Tile_h
    197