OSDN Git Service

Merge "Remove shouldOverrideUrlLoading restrictions"
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / TiledTexture.cpp
1 /*
2  * Copyright 2011, 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 #include "config.h"
27 #include "TiledTexture.h"
28
29 #include "TilesManager.h"
30 #include "TilesTracker.h"
31
32 #include "PaintedSurface.h"
33 #include "PaintTileOperation.h"
34 #include "SkCanvas.h"
35
36 #ifdef DEBUG
37
38 #include <cutils/log.h>
39 #include <wtf/CurrentTime.h>
40 #include <wtf/text/CString.h>
41
42 #undef XLOG
43 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "TiledTexture", __VA_ARGS__)
44
45 #else
46
47 #undef XLOG
48 #define XLOG(...)
49
50 #endif // DEBUG
51
52 namespace WebCore {
53
54 void TiledTexture::prepare(GLWebViewState* state, bool repaint)
55 {
56     if (!m_surface)
57         return;
58
59     if (!m_surface->layer())
60         return;
61
62     // first, how many tiles do we need
63     IntRect visibleArea = m_surface->visibleArea();
64     IntRect area(visibleArea.x() * m_surface->scale(),
65                  visibleArea.y() * m_surface->scale(),
66                  ceilf(visibleArea.width() * m_surface->scale()),
67                  ceilf(visibleArea.height() * m_surface->scale()));
68
69     for (unsigned int i = 0; i < m_tiles.size(); i++) {
70         BaseTile* tile = m_tiles[i];
71         tile->setUsedLevel(-1);
72         if (!m_dirtyRegion.isEmpty())
73             tile->markAsDirty(1, m_dirtyRegion);
74     }
75     m_dirtyRegion.setEmpty();
76
77     if (area.width() == 0 && area.height() == 0) {
78         m_area.setWidth(0);
79         m_area.setHeight(0);
80         return;
81     }
82
83     int tileWidth = TilesManager::instance()->layerTileWidth();
84     int tileHeight = TilesManager::instance()->layerTileHeight();
85
86     m_area.setX(area.x() / tileWidth);
87     m_area.setY(area.y() / tileHeight);
88     float right = (area.x() + area.width()) / (float) tileWidth;
89     float bottom = (area.y() + area.height()) / (float) tileHeight;
90     m_area.setWidth(ceilf(right) - m_area.x());
91     m_area.setHeight(ceilf(bottom) - m_area.y());
92
93     XLOG("for TiledTexture %x, we have a visible area of %d, %d - %d x %d, corresponding to %d, %d x - %d x %d tiles",
94          this,
95          visibleArea.x(), visibleArea.y(),
96          visibleArea.width(), visibleArea.height(),
97          m_area.x(), m_area.y(),
98          m_area.width(), m_area.height());
99
100     bool goingDown = m_prevTileY < m_area.y();
101     m_prevTileY = m_area.y();
102
103     if (m_surface->scale() != m_prevScale)
104         TilesManager::instance()->removeOperationsForFilter(new ScaleFilter(m_surface->scale()));
105
106     m_prevScale = m_surface->scale();
107
108     for (int i = 0; i < m_area.width(); i++) {
109         if (goingDown) {
110             for (int j = 0; j < m_area.height(); j++) {
111                 prepareTile(repaint, m_area.x() + i, m_area.y() + j);
112             }
113         } else {
114             for (int j = m_area.height() - 1; j >= 0; j--) {
115                 prepareTile(repaint, m_area.x() + i, m_area.y() + j);
116             }
117         }
118     }
119 }
120
121 void TiledTexture::markAsDirty(const SkRegion& dirtyArea)
122 {
123     m_dirtyRegion.op(dirtyArea, SkRegion::kUnion_Op);
124 }
125
126 void TiledTexture::prepareTile(bool repaint, int x, int y)
127 {
128     BaseTile* tile = getTile(x, y);
129     if (!tile) {
130         tile = new BaseTile(true);
131         m_tiles.append(tile);
132     }
133     tile->reserveTexture();
134     if (!tile->texture())
135         return;
136
137     tile->setContents(this, x, y, m_surface->scale());
138     tile->setUsedLevel(0);
139
140     bool schedule = false;
141     if (tile->isDirty())
142         schedule = true;
143
144     LayerAndroid* layer = m_surface->layer();
145     if (schedule && layer && !tile->isRepaintPending()) {
146         PaintTileOperation *operation = new PaintTileOperation(tile, m_surface);
147         TilesManager::instance()->scheduleOperation(operation);
148     }
149 }
150
151 BaseTile* TiledTexture::getTile(int x, int y)
152 {
153     for (unsigned int i = 0; i <m_tiles.size(); i++) {
154         BaseTile* tile = m_tiles[i];
155         if (tile->x() == x && tile->y() == y)
156             return tile;
157     }
158     return 0;
159 }
160
161 bool TiledTexture::draw()
162 {
163 #ifdef DEBUG
164     TilesManager::instance()->getTilesTracker()->trackLayer();
165 #endif
166
167     bool askRedraw = false;
168     if (m_area.width() == 0 || m_area.height() == 0)
169         return askRedraw;
170
171 #ifdef DEBUG
172     TilesManager::instance()->getTilesTracker()->trackVisibleLayer();
173 #endif
174
175     float m_invScale = 1 / m_surface->scale();
176     const float tileWidth = TilesManager::layerTileWidth() * m_invScale;
177     const float tileHeight = TilesManager::layerTileHeight() * m_invScale;
178     XLOG("draw tile %x, tiles %d", this, m_tiles.size());
179     for (unsigned int i = 0; i <m_tiles.size(); i++) {
180         BaseTile* tile = m_tiles[i];
181         if (tile->x() >= m_area.x()
182             && tile->x() < m_area.x() + m_area.width()
183             && tile->y() >= m_area.y()
184             && tile->y() < m_area.y() + m_area.height()) {
185             SkRect rect;
186             rect.fLeft = tile->x() * tileWidth;
187             rect.fTop = tile->y() * tileHeight;
188             rect.fRight = rect.fLeft + tileWidth;
189             rect.fBottom = rect.fTop + tileHeight;
190             XLOG(" - [%d], { painter %x vs %x }, tile %x %d,%d at scale %.2f [ready: %d] dirty: %d", i, this, tile->painter(), tile, tile->x(), tile->y(), tile->scale(), tile->isTileReady(), tile->isDirty());
191             askRedraw |= !tile->isTileReady();
192             tile->draw(m_surface->opacity(), rect, m_surface->scale());
193 #ifdef DEBUG
194             TilesManager::instance()->getTilesTracker()->track(tile->isTileReady(), tile->texture());
195 #endif
196         }
197     }
198     return askRedraw;
199 }
200
201 bool TiledTexture::paint(BaseTile* tile, SkCanvas* canvas, unsigned int* pictureUsed)
202 {
203     if (!m_surface)
204         return false;
205
206     XLOG("painting scheduled tile(%x : %d, %d, %.2f, %x) for %x",
207          tile, tile->x(), tile->y(), tile->scale(), tile->painter(), this);
208     return m_surface->paint(tile, canvas, pictureUsed);
209 }
210
211 void TiledTexture::paintExtra(SkCanvas* canvas)
212 {
213     m_surface->paintExtra(canvas);
214 }
215
216 const TransformationMatrix* TiledTexture::transform()
217 {
218     return m_surface->transform();
219 }
220
221 void TiledTexture::beginPaint()
222 {
223     if (m_surface)
224         m_surface->beginPaint();
225 }
226
227 void TiledTexture::endPaint()
228 {
229     if (m_surface)
230         m_surface->endPaint();
231 }
232
233 void TiledTexture::removeTiles()
234 {
235     for (unsigned int i = 0; i < m_tiles.size(); i++) {
236         delete m_tiles[i];
237     }
238 }
239
240 bool TiledTexture::owns(BaseTileTexture* texture)
241 {
242     for (unsigned int i = 0; i < m_tiles.size(); i++) {
243         BaseTile* tile = m_tiles[i];
244         if (tile->texture() == texture)
245             return true;
246     }
247     return false;
248 }
249
250 } // namespace WebCore