2 * Copyright 2010, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "GLWebViewState.h"
29 #if USE(ACCELERATED_COMPOSITING)
31 #include "BaseLayerAndroid.h"
32 #include "ClassTracker.h"
34 #include "ImagesManager.h"
35 #include "LayerAndroid.h"
37 #include "TilesManager.h"
38 #include "TilesTracker.h"
39 #include <wtf/CurrentTime.h>
41 #include <cutils/log.h>
42 #include <wtf/text/CString.h>
45 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "GLWebViewState", __VA_ARGS__)
50 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "GLWebViewState", __VA_ARGS__)
59 #define FIRST_TILED_PAGE_ID 1
60 #define SECOND_TILED_PAGE_ID 2
62 #define FRAMERATE_CAP 0.01666 // We cap at 60 fps
64 // log warnings if scale goes outside this range
65 #define MIN_SCALE_WARNING 0.1
66 #define MAX_SCALE_WARNING 10
70 using namespace android;
72 GLWebViewState::GLWebViewState(android::Mutex* buttonMutex)
74 , m_paintingBaseLayer(0)
75 , m_currentBaseLayer(0)
76 , m_currentBaseLayerRoot(0)
77 , m_currentPictureCounter(0)
79 , m_frameworkInval(0, 0, 0, 0)
80 , m_frameworkLayersInval(0, 0, 0, 0)
81 , m_globalButtonMutex(buttonMutex)
82 , m_baseLayerUpdate(true)
83 , m_backgroundColor(SK_ColorWHITE)
84 , m_isScrolling(false)
87 , m_expandedTileBoundsX(0)
88 , m_expandedTileBoundsY(0)
91 m_viewport.setEmpty();
92 m_futureViewportTileBounds.setEmpty();
93 m_viewportTileBounds.setEmpty();
94 m_preZoomBounds.setEmpty();
96 m_tiledPageA = new TiledPage(FIRST_TILED_PAGE_ID, this);
97 m_tiledPageB = new TiledPage(SECOND_TILED_PAGE_ID, this);
100 ClassTracker::instance()->increment("GLWebViewState");
104 m_totalTimeCounter = 0;
105 m_measurePerfs = false;
109 GLWebViewState::~GLWebViewState()
111 // Unref the existing tree/PaintedSurfaces
112 if (m_currentBaseLayerRoot)
113 TilesManager::instance()->swapLayersTextures(m_currentBaseLayerRoot, 0);
115 // Take care of the transfer queue such that Tex Gen thread will not stuck
116 TilesManager::instance()->unregisterGLWebViewState(this);
118 // We have to destroy the two tiled pages first as their destructor
119 // may depend on the existence of this GLWebViewState and some of its
120 // instance variables in order to complete.
121 // Explicitely, currently we need to have the m_paintingBaseLayer around
122 // in order to complete any pending paint operations (the tiled pages
123 // will remove any pending operations, and wait if one is underway).
126 SkSafeUnref(m_paintingBaseLayer);
127 SkSafeUnref(m_currentBaseLayer);
128 SkSafeUnref(m_currentBaseLayerRoot);
129 m_paintingBaseLayer = 0;
130 m_currentBaseLayer = 0;
131 m_currentBaseLayerRoot = 0;
133 ClassTracker::instance()->decrement("GLWebViewState");
138 void GLWebViewState::setBaseLayer(BaseLayerAndroid* layer, const SkRegion& inval,
139 bool showVisualIndicator, bool isPictureAfterFirstLayout)
141 android::Mutex::Autolock lock(m_baseLayerLock);
142 if (!layer || isPictureAfterFirstLayout) {
143 m_tiledPageA->discardTextures();
144 m_tiledPageB->discardTextures();
146 if (isPictureAfterFirstLayout) {
147 m_baseLayerUpdate = true;
148 m_invalidateRegion.setEmpty();
150 if (m_currentBaseLayer && layer)
151 m_currentBaseLayer->swapExtra(layer);
154 SkSafeUnref(m_currentBaseLayer);
155 m_currentBaseLayer = layer;
158 // copy content from old composited root to new
159 LayerAndroid* oldRoot = m_currentBaseLayerRoot;
161 layer->setGLWebViewState(this);
162 m_currentBaseLayerRoot = static_cast<LayerAndroid*>(layer->getChild(0));
163 SkSafeRef(m_currentBaseLayerRoot);
165 m_currentBaseLayerRoot = 0;
167 if (m_currentBaseLayerRoot && oldRoot)
168 TilesManager::instance()->swapLayersTextures(oldRoot, m_currentBaseLayerRoot);
169 SkSafeUnref(oldRoot);
172 // We only update the base layer if we are not currently
173 // waiting for a tiledPage to be painted
174 if (m_baseLayerUpdate) {
176 SkSafeUnref(m_paintingBaseLayer);
177 m_paintingBaseLayer = layer;
179 m_glExtras.setDrawExtra(0);
183 if (m_measurePerfs && !showVisualIndicator)
185 m_measurePerfs = showVisualIndicator;
188 TilesManager::instance()->setShowVisualIndicator(showVisualIndicator);
191 void GLWebViewState::invalRegion(const SkRegion& region)
193 SkRegion::Iterator iterator(region);
194 while (!iterator.done()) {
195 SkIRect r = iterator.rect();
196 IntRect ir(r.fLeft, r.fTop, r.width(), r.height());
202 void GLWebViewState::unlockBaseLayerUpdate() {
203 if (m_baseLayerUpdate)
206 m_baseLayerUpdate = true;
207 android::Mutex::Autolock lock(m_baseLayerLock);
208 SkSafeRef(m_currentBaseLayer);
209 SkSafeUnref(m_paintingBaseLayer);
210 m_paintingBaseLayer = m_currentBaseLayer;
212 invalRegion(m_invalidateRegion);
213 m_invalidateRegion.setEmpty();
216 void GLWebViewState::inval(const IntRect& rect)
218 if (m_baseLayerUpdate) {
219 // base layer isn't locked, so go ahead and issue the inval to both tiled pages
220 m_currentPictureCounter++;
221 if (!rect.isEmpty()) {
222 // find which tiles fall within the invalRect and mark them as dirty
223 m_tiledPageA->invalidateRect(rect, m_currentPictureCounter);
224 m_tiledPageB->invalidateRect(rect, m_currentPictureCounter);
225 if (m_frameworkInval.isEmpty())
226 m_frameworkInval = rect;
228 m_frameworkInval.unite(rect);
229 XLOG("intermediate invalRect(%d, %d, %d, %d) after unite with rect %d %d %d %d", m_frameworkInval.x(),
230 m_frameworkInval.y(), m_frameworkInval.width(), m_frameworkInval.height(),
231 rect.x(), rect.y(), rect.width(), rect.height());
234 // base layer is locked, so defer invalidation until unlockBaseLayerUpdate()
235 m_invalidateRegion.op(rect.x(), rect.y(), rect.maxX(), rect.maxY(), SkRegion::kUnion_Op);
237 TilesManager::instance()->getProfiler()->nextInval(rect, zoomManager()->currentScale());
240 unsigned int GLWebViewState::paintBaseLayerContent(SkCanvas* canvas)
242 android::Mutex::Autolock lock(m_baseLayerLock);
243 if (m_paintingBaseLayer) {
244 m_globalButtonMutex->lock();
245 m_paintingBaseLayer->drawCanvas(canvas);
246 m_globalButtonMutex->unlock();
248 return m_currentPictureCounter;
251 TiledPage* GLWebViewState::sibling(TiledPage* page)
253 return (page == m_tiledPageA) ? m_tiledPageB : m_tiledPageA;
256 TiledPage* GLWebViewState::frontPage()
258 android::Mutex::Autolock lock(m_tiledPageLock);
259 return m_usePageA ? m_tiledPageA : m_tiledPageB;
262 TiledPage* GLWebViewState::backPage()
264 android::Mutex::Autolock lock(m_tiledPageLock);
265 return m_usePageA ? m_tiledPageB : m_tiledPageA;
268 void GLWebViewState::swapPages()
270 android::Mutex::Autolock lock(m_tiledPageLock);
272 TiledPage* oldPage = m_usePageA ? m_tiledPageB : m_tiledPageA;
273 zoomManager()->swapPages();
274 oldPage->discardTextures();
277 int GLWebViewState::baseContentWidth()
279 return m_paintingBaseLayer ? m_paintingBaseLayer->content()->width() : 0;
281 int GLWebViewState::baseContentHeight()
283 return m_paintingBaseLayer ? m_paintingBaseLayer->content()->height() : 0;
286 void GLWebViewState::setViewport(SkRect& viewport, float scale)
288 if ((m_viewport == viewport) &&
289 (zoomManager()->futureScale() == scale))
292 m_goingDown = m_viewport.fTop - viewport.fTop <= 0;
293 m_goingLeft = m_viewport.fLeft - viewport.fLeft >= 0;
294 m_viewport = viewport;
296 XLOG("New VIEWPORT %.2f - %.2f %.2f - %.2f (w: %2.f h: %.2f scale: %.2f currentScale: %.2f futureScale: %.2f)",
297 m_viewport.fLeft, m_viewport.fTop, m_viewport.fRight, m_viewport.fBottom,
298 m_viewport.width(), m_viewport.height(), scale,
299 zoomManager()->currentScale(), zoomManager()->futureScale());
301 const float invTileContentWidth = scale / TilesManager::tileWidth();
302 const float invTileContentHeight = scale / TilesManager::tileHeight();
304 m_viewportTileBounds.set(
305 static_cast<int>(floorf(viewport.fLeft * invTileContentWidth)),
306 static_cast<int>(floorf(viewport.fTop * invTileContentHeight)),
307 static_cast<int>(ceilf(viewport.fRight * invTileContentWidth)),
308 static_cast<int>(ceilf(viewport.fBottom * invTileContentHeight)));
310 // allocate max possible number of tiles visible with this viewport
311 int viewMaxTileX = static_cast<int>(ceilf((viewport.width()-1) * invTileContentWidth)) + 1;
312 int viewMaxTileY = static_cast<int>(ceilf((viewport.height()-1) * invTileContentHeight)) + 1;
313 int maxTextureCount = (viewMaxTileX + TILE_PREFETCH_DISTANCE * 2) *
314 (viewMaxTileY + TILE_PREFETCH_DISTANCE * 2) * 2;
315 TilesManager::instance()->setMaxTextureCount(maxTextureCount);
316 m_tiledPageA->updateBaseTileSize();
317 m_tiledPageB->updateBaseTileSize();
321 void GLWebViewState::dumpMeasures()
323 for (int i = 0; i < m_timeCounter; i++) {
324 XLOGC("%d delay: %d ms", m_totalTimeCounter + i,
325 static_cast<int>(m_delayTimes[i]*1000));
328 m_totalTimeCounter += m_timeCounter;
331 #endif // MEASURES_PERF
333 void GLWebViewState::resetFrameworkInval()
335 m_frameworkInval.setX(0);
336 m_frameworkInval.setY(0);
337 m_frameworkInval.setWidth(0);
338 m_frameworkInval.setHeight(0);
341 void GLWebViewState::addDirtyArea(const IntRect& rect)
346 IntRect inflatedRect = rect;
347 inflatedRect.inflate(8);
348 if (m_frameworkLayersInval.isEmpty())
349 m_frameworkLayersInval = inflatedRect;
351 m_frameworkLayersInval.unite(inflatedRect);
354 void GLWebViewState::resetLayersDirtyArea()
356 m_frameworkLayersInval.setX(0);
357 m_frameworkLayersInval.setY(0);
358 m_frameworkLayersInval.setWidth(0);
359 m_frameworkLayersInval.setHeight(0);
362 double GLWebViewState::setupDrawing(IntRect& viewRect, SkRect& visibleRect,
363 IntRect& webViewRect, int titleBarHeight,
364 IntRect& screenClip, float scale)
366 int left = viewRect.x();
367 int top = viewRect.y();
368 int width = viewRect.width();
369 int height = viewRect.height();
371 if (TilesManager::instance()->invertedScreen()) {
372 float color = 1.0 - ((((float) m_backgroundColor.red() / 255.0) +
373 ((float) m_backgroundColor.green() / 255.0) +
374 ((float) m_backgroundColor.blue() / 255.0)) / 3.0);
375 glClearColor(color, color, color, 1);
377 glClearColor((float)m_backgroundColor.red() / 255.0,
378 (float)m_backgroundColor.green() / 255.0,
379 (float)m_backgroundColor.blue() / 255.0, 1);
381 glClear(GL_COLOR_BUFFER_BIT);
383 glViewport(left, top, width, height);
385 ShaderProgram* shader = TilesManager::instance()->shader();
386 if (shader->program() == -1) {
387 XLOG("Reinit shader");
390 shader->setViewRect(viewRect);
391 shader->setViewport(visibleRect);
392 shader->setWebViewRect(webViewRect);
393 shader->setTitleBarHeight(titleBarHeight);
394 shader->setScreenClip(screenClip);
395 shader->resetBlending();
397 double currentTime = WTF::currentTime();
399 setViewport(visibleRect, scale);
400 m_zoomManager.processNewScale(currentTime, scale);
405 bool GLWebViewState::drawGL(IntRect& rect, SkRect& viewport, IntRect* invalRect,
406 IntRect& webViewRect, int titleBarHeight,
407 IntRect& clip, float scale, bool* buffersSwappedPtr)
410 TilesManager::instance()->getProfiler()->nextFrame(viewport.fLeft,
415 TilesManager::instance()->incDrawGLCount();
418 TilesManager::instance()->getTilesTracker()->clear();
421 m_baseLayerLock.lock();
422 BaseLayerAndroid* baseLayer = m_paintingBaseLayer;
423 SkSafeRef(baseLayer);
424 m_baseLayerLock.unlock();
428 float viewWidth = (viewport.fRight - viewport.fLeft) * TILE_PREFETCH_RATIO;
429 float viewHeight = (viewport.fBottom - viewport.fTop) * TILE_PREFETCH_RATIO;
430 bool useHorzPrefetch = viewWidth < baseContentWidth();
431 bool useVertPrefetch = viewHeight < baseContentHeight();
432 m_expandedTileBoundsX = (useHorzPrefetch) ? TILE_PREFETCH_DISTANCE : 0;
433 m_expandedTileBoundsY = (useVertPrefetch) ? TILE_PREFETCH_DISTANCE : 0;
435 XLOG("drawGL, rect(%d, %d, %d, %d), viewport(%.2f, %.2f, %.2f, %.2f)",
436 rect.x(), rect.y(), rect.width(), rect.height(),
437 viewport.fLeft, viewport.fTop, viewport.fRight, viewport.fBottom);
439 resetLayersDirtyArea();
441 LayerAndroid* compositedRoot = m_currentBaseLayerRoot;
442 LayerAndroid* paintingBaseLayerRoot = 0;
443 if (baseLayer && baseLayer->countChildren() >= 1)
444 paintingBaseLayerRoot = static_cast<LayerAndroid*>(baseLayer->getChild(0));
446 // when adding or removing layers, use the the paintingBaseLayer's tree so
447 // that content that moves to the base layer from a layer is synchronized
448 bool paintingHasLayers = (paintingBaseLayerRoot == 0);
449 bool currentHasLayers = (m_currentBaseLayerRoot == 0);
450 if (paintingHasLayers != currentHasLayers)
451 compositedRoot = paintingBaseLayerRoot;
453 if (scale < MIN_SCALE_WARNING || scale > MAX_SCALE_WARNING)
454 XLOGC("WARNING, scale seems corrupted before update: %e", scale);
456 // Here before we draw, update the BaseTile which has updated content.
457 // Inside this function, just do GPU blits from the transfer queue into
458 // the BaseTiles' texture.
459 TilesManager::instance()->transferQueue()->updateDirtyBaseTiles();
461 // Upload any pending ImageTexture
462 // Return true if we still have some images to upload.
463 // TODO: upload as many textures as possible within a certain time limit
464 bool ret = ImagesManager::instance()->uploadTextures();
466 if (scale < MIN_SCALE_WARNING || scale > MAX_SCALE_WARNING) {
467 XLOGC("WARNING, scale seems corrupted after update: %e", scale);
471 // gather the textures we can use
472 TilesManager::instance()->gatherLayerTextures();
474 // set up zoom manager, shaders, etc.
475 m_backgroundColor = baseLayer->getBackgroundColor();
476 double currentTime = setupDrawing(rect, viewport, webViewRect, titleBarHeight, clip, scale);
477 ret |= baseLayer->drawGL(currentTime, compositedRoot, rect,
478 viewport, scale, buffersSwappedPtr);
479 m_glExtras.drawGL(webViewRect, viewport, titleBarHeight);
481 glBindBuffer(GL_ARRAY_BUFFER, 0);
483 ret |= TilesManager::instance()->invertedScreenSwitch();
486 // ret==true && empty inval region means we've inval'd everything,
487 // but don't have new content. Keep redrawing full view (0,0,0,0)
488 // until tile generation catches up and we swap pages.
489 bool fullScreenInval = m_frameworkInval.isEmpty();
491 if (TilesManager::instance()->invertedScreenSwitch()) {
492 fullScreenInval = true;
493 TilesManager::instance()->setInvertedScreenSwitch(false);
496 if (!fullScreenInval) {
497 FloatRect frameworkInval = TilesManager::instance()->shader()->rectInInvScreenCoord(
499 // Inflate the invalidate rect to avoid precision lost.
500 frameworkInval.inflate(1);
501 IntRect inval(frameworkInval.x(), frameworkInval.y(),
502 frameworkInval.width(), frameworkInval.height());
504 inval.unite(m_frameworkLayersInval);
506 invalRect->setX(inval.x());
507 invalRect->setY(inval.y());
508 invalRect->setWidth(inval.width());
509 invalRect->setHeight(inval.height());
511 XLOG("invalRect(%d, %d, %d, %d)", inval.x(),
512 inval.y(), inval.width(), inval.height());
514 if (!invalRect->intersects(rect)) {
515 // invalidate is occurring offscreen, do full inval to guarantee redraw
516 fullScreenInval = true;
520 if (fullScreenInval) {
523 invalRect->setWidth(0);
524 invalRect->setHeight(0);
527 resetFrameworkInval();
531 if (m_measurePerfs) {
532 m_delayTimes[m_timeCounter++] = delta;
533 if (m_timeCounter >= MAX_MEASURES_PERF)
538 SkSafeUnref(baseLayer);
540 TilesManager::instance()->getTilesTracker()->showTrackTextures();
541 ImagesManager::instance()->showImages();
546 } // namespace WebCore
548 #endif // USE(ACCELERATED_COMPOSITING)