Tile.h revision e859a34171f2a36877d95197d118d962078f8aa0
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
40namespace WebCore {
41
42class TextureInfo;
43class TileTexture;
44class 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 */
62class Tile : public TextureOwner {
63public:
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(const SkRegion& dirtyArea);
118    bool isDirty();
119    const SkRegion& dirtyArea() { return m_dirtyArea; }
120    virtual bool isRepaintPending();
121    void setRepaintPending(bool pending);
122    float scale() const { return m_scale; }
123    TextureState textureState() const { return m_state; }
124
125    int x() const { return m_x; }
126    int y() const { return m_y; }
127    TileTexture* frontTexture() { return m_frontTexture; }
128    TileTexture* backTexture() { return m_backTexture; }
129
130    // only used for prioritization - the higher, the more relevant the tile is
131    unsigned long long drawCount() { return m_drawCount; }
132    void discardTextures();
133    void discardBackTexture();
134    bool swapTexturesIfNeeded();
135    void backTextureTransfer();
136    void backTextureTransferFail();
137    void onBlitUpdate();
138
139    // TextureOwner implementation
140    virtual bool removeTexture(TileTexture* texture);
141
142private:
143    void validatePaint();
144
145    int m_x;
146    int m_y;
147
148    // The remaining variables can be updated throughout the lifetime of the object
149
150    TileTexture* m_frontTexture;
151    TileTexture* m_backTexture;
152    float m_scale;
153
154    // used to signal that the that the tile is out-of-date and needs to be
155    // redrawn in the backTexture
156    bool m_dirty;
157
158    // used to signal that a repaint is pending
159    bool m_repaintPending;
160
161    // store the dirty region
162    SkRegion m_dirtyArea;
163    bool m_fullRepaint;
164
165    // This mutex serves two purposes. (1) It ensures that certain operations
166    // happen atomically and (2) it makes sure those operations are synchronized
167    // across all threads and cores.
168    android::Mutex m_atomicSync;
169
170    BaseRenderer* m_renderer;
171
172    bool m_isLayerTile;
173
174    // the most recent GL draw before this tile was prepared. used for
175    // prioritization and caching. tiles with old drawcounts and textures they
176    // own are used for new tiles and rendering
177    unsigned long long m_drawCount;
178
179    // Tracks the state of painting for the tile. High level overview:
180    // 1) Unpainted - until paint starts (and if marked dirty, in most cases)
181    // 2) PaintingStarted - until paint completes
182    // 3) TransferredUnvalidated - if transferred first
183    //    or ValidatedUntransferred - if validated first
184    // 4) ReadyToSwap - if painted and transferred, but not swapped
185    // 5) UpToDate - until marked dirty again
186    TextureState m_state;
187};
188
189} // namespace WebCore
190
191#endif // USE(ACCELERATED_COMPOSITING)
192#endif // Tile_h
193