Home | History | Annotate | Download | only in android
      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 BaseTile_h
     27 #define BaseTile_h
     28 
     29 #if USE(ACCELERATED_COMPOSITING)
     30 
     31 #include "BaseRenderer.h"
     32 #include "SkRect.h"
     33 #include "SkRegion.h"
     34 #include "TextureOwner.h"
     35 #include "TilePainter.h"
     36 
     37 #include <utils/threads.h>
     38 
     39 namespace WebCore {
     40 
     41 class TextureInfo;
     42 class TiledPage;
     43 class BaseTileTexture;
     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 draw() 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 BaseTile : 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     BaseTile(bool isLayerTile = false);
     93     ~BaseTile();
     94 
     95     bool isLayerTile() { return m_isLayerTile; }
     96 
     97     void setContents(TilePainter* painter, int x, int y, float scale);
     98     void setPage(TiledPage* page) { m_page = page; }
     99 
    100     void reserveTexture();
    101 
    102     bool isTileReady();
    103 
    104     void draw(float transparency, SkRect& rect, float scale);
    105 
    106     // the only thread-safe function called by the background thread
    107     void paintBitmap();
    108 
    109     bool intersectWithRect(int x, int y, int tileWidth, int tileHeight,
    110                            float scale, const SkRect& dirtyRect,
    111                            SkRect& realTileRect);
    112     bool isTileVisible(const IntRect& viewTileBounds);
    113 
    114     void markAsDirty(const unsigned int pictureCount,
    115                      const SkRegion& dirtyArea);
    116     bool isDirty();
    117     bool isRepaintPending();
    118     void setRepaintPending(bool pending);
    119     float scale() const { return m_scale; }
    120     TextureState textureState() const { return m_state; }
    121 
    122     int x() const { return m_x; }
    123     int y() const { return m_y; }
    124     BaseTileTexture* frontTexture() { return m_frontTexture; }
    125     BaseTileTexture* backTexture() { return m_backTexture; }
    126 
    127     // only used for prioritization - the higher, the more relevant the tile is
    128     unsigned long long drawCount() { return m_drawCount; }
    129     void discardTextures();
    130     void discardBackTexture();
    131     bool swapTexturesIfNeeded();
    132     void backTextureTransfer();
    133     void backTextureTransferFail();
    134 
    135     void setGLWebViewState(GLWebViewState* state) { m_glWebViewState = state; }
    136 
    137     // TextureOwner implementation
    138     virtual bool removeTexture(BaseTileTexture* texture);
    139     virtual TiledPage* page() { return m_page; }
    140     virtual GLWebViewState* state() { return m_glWebViewState; }
    141     TilePainter* painter() { return m_painter; }
    142 
    143 private:
    144     void validatePaint();
    145 
    146     GLWebViewState* m_glWebViewState;
    147 
    148     TilePainter* m_painter;
    149     int m_x;
    150     int m_y;
    151 
    152     TiledPage* m_page;
    153 
    154     // The remaining variables can be updated throughout the lifetime of the object
    155 
    156     BaseTileTexture* m_frontTexture;
    157     BaseTileTexture* m_backTexture;
    158     float m_scale;
    159 
    160     // used to signal that the that the tile is out-of-date and needs to be
    161     // redrawn in the backTexture
    162     bool m_dirty;
    163 
    164     // currently only for debugging, to be used for tracking down dropped repaints
    165     bool m_deferredDirty;
    166 
    167     // used to signal that a repaint is pending
    168     bool m_repaintPending;
    169     // stores the id of the latest picture from webkit that caused this tile to
    170     // become dirty. A tile is no longer dirty when it has been painted with a
    171     // picture that is newer than this value.
    172     unsigned int m_lastDirtyPicture;
    173 
    174     // store the dirty region
    175     SkRegion* m_dirtyArea;
    176     bool* m_fullRepaint;
    177     int m_maxBufferNumber;
    178     int m_currentDirtyAreaIndex;
    179 
    180     // flag used to know if we have a texture that was painted at least once
    181     bool m_isTexturePainted;
    182 
    183     // This mutex serves two purposes. (1) It ensures that certain operations
    184     // happen atomically and (2) it makes sure those operations are synchronized
    185     // across all threads and cores.
    186     android::Mutex m_atomicSync;
    187 
    188     BaseRenderer* m_renderer;
    189 
    190     bool m_isLayerTile;
    191 
    192     // the most recent GL draw before this tile was prepared. used for
    193     // prioritization and caching. tiles with old drawcounts and textures they
    194     // own are used for new tiles and rendering
    195     unsigned long long m_drawCount;
    196 
    197     // Tracks the state of painting for the tile. High level overview:
    198     // 1) Unpainted - until paint starts (and if marked dirty, in most cases)
    199     // 2) PaintingStarted - until paint completes
    200     // 3) TransferredUnvalidated - if transferred first
    201     //    or ValidatedUntransferred - if validated first
    202     // 4) ReadyToSwap - if painted and transferred, but not swapped
    203     // 5) UpToDate - until marked dirty again
    204     TextureState m_state;
    205 };
    206 
    207 } // namespace WebCore
    208 
    209 #endif // USE(ACCELERATED_COMPOSITING)
    210 #endif // BaseTile_h
    211