• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FloatPoint.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 BaseRenderer;
42 class GLWebViewState;
43 class TextureInfo;
44 class TileTexture;
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 
isLayerTile()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, BaseRenderer* renderer);
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();
dirtyArea()120     const SkRegion& dirtyArea() { return m_dirtyArea; }
121     virtual bool isRepaintPending();
122     void setRepaintPending(bool pending);
scale()123     float scale() const { return m_scale; }
textureState()124     TextureState textureState() const { return m_state; }
125 
x()126     int x() const { return m_x; }
y()127     int y() const { return m_y; }
frontTexture()128     TileTexture* frontTexture() { return m_frontTexture; }
backTexture()129     TileTexture* backTexture() { return m_backTexture; }
lastDrawnTexture()130     TileTexture* lastDrawnTexture() { return m_lastDrawnTexture; }
131 
132     // only used for prioritization - the higher, the more relevant the tile is
drawCount()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     bool m_isLayerTile;
175 
176     // the most recent GL draw before this tile was prepared. used for
177     // prioritization and caching. tiles with old drawcounts and textures they
178     // own are used for new tiles and rendering
179     unsigned long long m_drawCount;
180 
181     // Tracks the state of painting for the tile. High level overview:
182     // 1) Unpainted - until paint starts (and if marked dirty, in most cases)
183     // 2) PaintingStarted - until paint completes
184     // 3) TransferredUnvalidated - if transferred first
185     //    or ValidatedUntransferred - if validated first
186     // 4) ReadyToSwap - if painted and transferred, but not swapped
187     // 5) UpToDate - until marked dirty again
188     TextureState m_state;
189 };
190 
191 } // namespace WebCore
192 
193 #endif // USE(ACCELERATED_COMPOSITING)
194 #endif // Tile_h
195