1 /* 2 * Copyright 2012, 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 #define LOG_TAG "Surface" 27 #define LOG_NDEBUG 1 28 29 #include "config.h" 30 #include "Surface.h" 31 32 #include "AndroidLog.h" 33 #include "BaseLayerAndroid.h" 34 #include "ClassTracker.h" 35 #include "LayerAndroid.h" 36 #include "LayerContent.h" 37 #include "GLWebViewState.h" 38 #include "PrerenderedInval.h" 39 #include "SkCanvas.h" 40 #include "SurfaceBacking.h" 41 #include "Tile.h" 42 #include "TileTexture.h" 43 #include "TilesManager.h" 44 45 #include <wtf/text/CString.h> 46 47 // Surfaces with an area larger than 2048*2048 should never be unclipped 48 #define MAX_FULL_CONTENT_AREA 4194304 49 50 namespace WebCore { 51 52 Surface::Surface() 53 : m_surfaceBacking(0) 54 , m_needsTexture(false) 55 , m_maxZoomScale(1) 56 { 57 #ifdef DEBUG_COUNT 58 ClassTracker::instance()->increment("Surface"); 59 #endif 60 } 61 62 Surface::~Surface() 63 { 64 for (unsigned int i = 0; i < m_layers.size(); i++) 65 SkSafeUnref(m_layers[i]); 66 if (m_surfaceBacking) 67 SkSafeUnref(m_surfaceBacking); 68 #ifdef DEBUG_COUNT 69 ClassTracker::instance()->decrement("Surface"); 70 #endif 71 } 72 73 bool Surface::tryUpdateSurface(Surface* oldSurface) 74 { 75 if (!needsTexture() || !oldSurface->needsTexture()) 76 return false; 77 78 // merge surfaces based on first layer ID 79 if (getFirstLayer()->uniqueId() != oldSurface->getFirstLayer()->uniqueId()) 80 return false; 81 82 m_surfaceBacking = oldSurface->m_surfaceBacking; 83 SkSafeRef(m_surfaceBacking); 84 85 ALOGV("%p taking old SurfBack %p from surface %p, nt %d", 86 this, m_surfaceBacking, oldSurface, oldSurface->needsTexture()); 87 88 if (!m_surfaceBacking) { 89 // no SurfBack to inval, so don't worry about it. 90 return true; 91 } 92 93 SkRegion invalRegion; 94 bool fullInval = false; 95 if (singleLayer() && oldSurface->singleLayer()) { 96 // both are single matching layers, simply apply inval 97 SkRegion* layerInval = getFirstLayer()->getInvalRegion(); 98 invalRegion = *layerInval; 99 100 if (isBase()) { 101 // the base layer paints outside it's content area to ensure the 102 // viewport is convered, so fully invalidate all tiles if its size 103 // changes to ensure no stale content remains 104 LayerContent* newContent = getFirstLayer()->content(); 105 LayerContent* oldContent = oldSurface->getFirstLayer()->content(); 106 fullInval = newContent->width() != oldContent->width() 107 || newContent->height() != oldContent->height(); 108 } 109 } else { 110 fullInval = m_layers.size() != oldSurface->m_layers.size(); 111 if (!fullInval) { 112 for (unsigned int i = 0; i < m_layers.size(); i++) { 113 if ((m_layers[i]->uniqueId() != oldSurface->m_layers[i]->uniqueId()) 114 || (m_layers[i]->fullContentAreaMapped() != oldSurface->m_layers[i]->fullContentAreaMapped())) { 115 // layer list has changed, fully invalidate 116 // TODO: partially invalidate based on layer size/position 117 fullInval = true; 118 break; 119 } else if (!m_layers[i]->getInvalRegion()->isEmpty()) { 120 // merge layer inval - translate the layer's inval region into surface coordinates 121 // TODO: handle scale/3d transform mapping 122 FloatRect layerPos = m_layers[i]->fullContentAreaMapped(); 123 m_layers[i]->getInvalRegion()->translate(layerPos.x(), layerPos.y()); 124 invalRegion.op(*(m_layers[i]->getInvalRegion()), SkRegion::kUnion_Op); 125 } 126 } 127 } 128 } 129 130 if (fullInval) 131 invalRegion.setRect(-1e8, -1e8, 2e8, 2e8); 132 133 m_surfaceBacking->markAsDirty(invalRegion); 134 return true; 135 } 136 137 void Surface::addLayer(LayerAndroid* layer, const TransformationMatrix& transform) 138 { 139 m_layers.append(layer); 140 SkSafeRef(layer); 141 142 m_needsTexture |= layer->needsTexture(); 143 m_maxZoomScale = std::max(m_maxZoomScale, layer->maxZoomScale()); 144 145 // add this layer's size to the surface's area 146 // TODO: handle scale/3d transform mapping 147 IntRect rect = enclosingIntRect(layer->fullContentAreaMapped()); 148 149 if (layer->needsTexture()) { 150 if (m_fullContentArea.isEmpty()) { 151 m_drawTransform = transform; 152 m_drawTransform.translate3d(-rect.x(), -rect.y(), 0); 153 m_fullContentArea = rect; 154 } else 155 m_fullContentArea.unite(rect); 156 ALOGV("Surf %p adding LA %p, size " INT_RECT_FORMAT 157 " now fullContentArea " INT_RECT_FORMAT, 158 this, layer, INT_RECT_ARGS(rect), INT_RECT_ARGS(m_fullContentArea)); 159 } 160 161 if (isBase()) 162 m_background = static_cast<BaseLayerAndroid*>(layer)->getBackgroundColor(); 163 } 164 165 IntRect Surface::visibleContentArea(bool force3dContentVisible) const 166 { 167 if (singleLayer()) 168 return getFirstLayer()->visibleContentArea(force3dContentVisible); 169 170 IntRect rect = m_fullContentArea; 171 172 // clip with the viewport in content coordinate 173 IntRect contentViewport(TilesManager::instance()->shader()->contentViewport()); 174 rect.intersect(contentViewport); 175 176 // TODO: handle recursive layer clip 177 178 return rect; 179 } 180 181 IntRect Surface::fullContentArea() 182 { 183 if (singleLayer()) 184 return getFirstLayer()->fullContentArea(); 185 return m_fullContentArea; 186 } 187 188 bool Surface::useAggressiveRendering() 189 { 190 // When the background is semi-opaque, 0 < alpha < 255, we had to turn off 191 // low res to avoid artifacts from double drawing. 192 // TODO: avoid double drawing for low res tiles. 193 return isBase() 194 && (!m_background.alpha() 195 || !m_background.hasAlpha()); 196 } 197 198 void Surface::prepareGL(bool layerTilesDisabled, bool updateWithBlit) 199 { 200 bool tilesDisabled = layerTilesDisabled && !isBase(); 201 if (!m_surfaceBacking) { 202 ALOGV("prepareGL on Surf %p, no SurfBack, needsTexture? %d", 203 this, m_surfaceBacking, needsTexture()); 204 205 if (needsTexture() || (isBase() && layerTilesDisabled)) 206 m_surfaceBacking = new SurfaceBacking(isBase()); 207 else 208 return; 209 } 210 211 if (tilesDisabled) { 212 m_surfaceBacking->discardTextures(); 213 } else { 214 IntRect prepareArea = computePrepareArea(); 215 IntRect fullArea = fullContentArea(); 216 217 ALOGV("prepareGL on Surf %p with SurfBack %p, %d layers, first layer %s (%d) " 218 "prepareArea(%d, %d - %d x %d) fullArea(%d, %d - %d x %d)", 219 this, m_surfaceBacking, m_layers.size(), 220 getFirstLayer()->subclassName(), 221 getFirstLayer()->uniqueId(), 222 prepareArea.x(), prepareArea.y(), prepareArea.width(), prepareArea.height(), 223 fullArea.x(), fullArea.y(), fullArea.width(), fullArea.height()); 224 225 m_surfaceBacking->prepareGL(getFirstLayer()->state(), m_maxZoomScale, 226 prepareArea, fullArea, 227 this, useAggressiveRendering(), updateWithBlit); 228 } 229 for (size_t i = 0; i < m_layers.size(); i++) { 230 LayerContent* content = m_layers[i]->content(); 231 if (content) 232 content->clearPrerenders(); 233 } 234 } 235 236 bool Surface::drawGL(bool layerTilesDisabled) 237 { 238 bool tilesDisabled = layerTilesDisabled && !isBase(); 239 if (singleLayer() && !getFirstLayer()->visible()) 240 return false; 241 242 if (!isBase()) { 243 FloatRect drawClip = getFirstLayer()->drawClip(); 244 if (!singleLayer()) { 245 for (unsigned int i = 1; i < m_layers.size(); i++) 246 drawClip.unite(m_layers[i]->drawClip()); 247 } 248 FloatRect clippingRect = TilesManager::instance()->shader()->rectInInvViewCoord(drawClip); 249 TilesManager::instance()->shader()->clip(clippingRect); 250 } 251 252 bool askRedraw = false; 253 if (m_surfaceBacking && !tilesDisabled) { 254 ALOGV("drawGL on Surf %p with SurfBack %p, first layer %s (%d)", this, m_surfaceBacking, 255 getFirstLayer()->subclassName(), getFirstLayer()->uniqueId()); 256 257 bool force3dContentVisible = true; 258 IntRect drawArea = visibleContentArea(force3dContentVisible); 259 m_surfaceBacking->drawGL(drawArea, opacity(), drawTransform(), 260 useAggressiveRendering(), background()); 261 } 262 263 // draw member layers (draws image textures, glextras) 264 for (unsigned int i = 0; i < m_layers.size(); i++) { 265 if (m_layers[i]->drawGL(tilesDisabled)) { 266 m_layers[i]->addDirtyArea(); 267 askRedraw = true; 268 } 269 } 270 271 return askRedraw; 272 } 273 274 void Surface::swapTiles(bool calculateFrameworkInvals) 275 { 276 if (!m_surfaceBacking) 277 return; 278 279 if (m_surfaceBacking->swapTiles() && calculateFrameworkInvals) 280 addFrameworkInvals(); 281 } 282 283 void Surface::addFrameworkInvals() 284 { 285 // Let's return an inval area to framework that will 286 // contain all of our layers' areas 287 for (unsigned int i = 0; i < m_layers.size(); i++) 288 m_layers[i]->addDirtyArea(); 289 } 290 291 bool Surface::isReady() 292 { 293 if (!m_surfaceBacking) 294 return true; 295 296 return m_surfaceBacking->isReady(); 297 } 298 299 bool Surface::isMissingContent() 300 { 301 if (!m_surfaceBacking) 302 return true; 303 304 return m_surfaceBacking->isMissingContent(); 305 } 306 307 bool Surface::canUpdateWithBlit() 308 { 309 // If we don't have a texture, we have nothing to update and thus can take 310 // the fast path 311 if (!needsTexture()) 312 return true; 313 // If we have a surface backing that isn't ready, we can't update with a blit 314 // If it is ready, then check to see if it is dirty. We can only call isDirty() 315 // if isReady() returns true 316 if (!m_surfaceBacking) 317 return false; 318 if (!m_surfaceBacking->isReady()) 319 return false; 320 if (!m_surfaceBacking->isDirty()) 321 return true; 322 if (!singleLayer()) 323 return false; 324 return getFirstLayer()->canUpdateWithBlit(); 325 } 326 327 IntRect Surface::computePrepareArea() 328 { 329 IntRect area; 330 331 if (!getFirstLayer()->contentIsScrollable() 332 && !isBase() 333 && getFirstLayer()->state()->layersRenderingMode() == GLWebViewState::kAllTextures) { 334 335 area = fullContentArea(); 336 337 double total = ((double) area.width()) * ((double) area.height()); 338 if (total > MAX_FULL_CONTENT_AREA) 339 area = visibleContentArea(); 340 } else 341 area = visibleContentArea(); 342 343 return area; 344 } 345 346 void Surface::computeTexturesAmount(TexturesResult* result) 347 { 348 if (!m_surfaceBacking || isBase()) 349 return; 350 351 352 LayerAndroid* layer = 0; 353 if (singleLayer()) 354 layer = getFirstLayer(); 355 356 m_surfaceBacking->computeTexturesAmount(result, visibleContentArea(), 357 fullContentArea(), layer); 358 } 359 360 bool Surface::isBase() 361 { 362 // base layer surface 363 // - doesn't use layer tiles (disables blending, doesn't compute textures amount) 364 // - ignores clip rects 365 // - only prepares clippedArea 366 return getFirstLayer()->subclassType() == LayerAndroid::BaseLayer; 367 } 368 369 bool Surface::paint(SkCanvas* canvas) 370 { 371 if (singleLayer()) { 372 getFirstLayer()->contentDraw(canvas, Layer::UnmergedLayers); 373 374 // TODO: double buffer by disabling SurfaceCollection swaps and position 375 // updates until painting complete 376 377 // In single surface mode, draw layer content onto the base layer 378 if (isBase() 379 && getFirstLayer()->countChildren() 380 && getFirstLayer()->state()->isSingleSurfaceRenderingMode()) { 381 for (int i = 0; i < getFirstLayer()->countChildren(); i++) 382 getFirstLayer()->getChild(i)->drawCanvas(canvas, true, Layer::FlattenedLayers); 383 } 384 } else { 385 SkAutoCanvasRestore acr(canvas, true); 386 SkMatrix matrix; 387 GLUtils::toSkMatrix(matrix, m_drawTransform); 388 389 SkMatrix inverse; 390 inverse.reset(); 391 matrix.invert(&inverse); 392 393 SkMatrix canvasMatrix = canvas->getTotalMatrix(); 394 inverse.postConcat(canvasMatrix); 395 canvas->setMatrix(inverse); 396 397 for (unsigned int i=0; i<m_layers.size(); i++) 398 m_layers[i]->drawCanvas(canvas, false, Layer::MergedLayers); 399 } 400 return true; 401 } 402 403 float Surface::opacity() 404 { 405 if (singleLayer()) 406 return getFirstLayer()->drawOpacity(); 407 return 1.0; 408 } 409 410 Color* Surface::background() 411 { 412 if (!isBase() || !m_background.isValid()) 413 return 0; 414 return &m_background; 415 } 416 417 bool Surface::blitFromContents(Tile* tile) 418 { 419 if (!singleLayer() || !tile || !getFirstLayer() || !getFirstLayer()->content()) 420 return false; 421 422 if (tile->frontTexture() != tile->lastDrawnTexture()) { 423 // the below works around an issue where glTexSubImage2d can't update a 424 // texture that hasn't drawn yet by drawing it off screen. 425 // glFlush() and glFinish() work also, but are likely more wasteful. 426 SkRect rect = SkRect::MakeXYWH(-100, -100, 0, 0); 427 FloatRect fillPortion(0, 0, 0, 0); 428 tile->frontTexture()->drawGL(false, rect, 1.0f, 0, false, true, fillPortion); 429 } 430 LayerContent* content = getFirstLayer()->content(); 431 // Extract the dirty rect from the region. Note that this is *NOT* constrained 432 // to this tile 433 IntRect dirtyRect = tile->dirtyArea().getBounds(); 434 IntRect tileRect = IntRect(tile->x() * TilesManager::tileWidth(), 435 tile->y() * TilesManager::tileHeight(), 436 TilesManager::tileWidth(), 437 TilesManager::tileHeight()); 438 FloatRect tileRectInDoc = tileRect; 439 tileRectInDoc.scale(1 / tile->scale()); 440 dirtyRect.intersect(enclosingIntRect(tileRectInDoc)); 441 PrerenderedInval* prerenderedInval = content->prerenderForRect(dirtyRect); 442 if (!prerenderedInval || prerenderedInval->bitmap.isNull()) 443 return false; 444 SkBitmap sourceBitmap = prerenderedInval->bitmap; 445 // Calculate the screen rect that is dirty, then intersect it with the 446 // tile's screen rect so that we end up with the pixels we need to blit 447 FloatRect screenDirty = dirtyRect; 448 screenDirty.scale(tile->scale()); 449 IntRect enclosingScreenDirty = enclosingIntRect(screenDirty); 450 enclosingScreenDirty.intersect(tileRect); 451 if (enclosingScreenDirty.isEmpty()) 452 return false; 453 // Make sure the screen area we want to blit is contained by the 454 // prerendered screen area 455 if (!prerenderedInval->screenArea.contains(enclosingScreenDirty)) { 456 ALOGD("prerendered->screenArea " INT_RECT_FORMAT " doesn't contain " 457 "enclosingScreenDirty " INT_RECT_FORMAT, 458 INT_RECT_ARGS(prerenderedInval->screenArea), 459 INT_RECT_ARGS(enclosingScreenDirty)); 460 return false; 461 } 462 IntPoint origin = prerenderedInval->screenArea.location(); 463 SkBitmap subset; 464 subset.setConfig(sourceBitmap.config(), enclosingScreenDirty.width(), 465 enclosingScreenDirty.height()); 466 subset.allocPixels(); 467 468 int topOffset = enclosingScreenDirty.y() - prerenderedInval->screenArea.y(); 469 int leftOffset = enclosingScreenDirty.x() - prerenderedInval->screenArea.x(); 470 if (!GLUtils::deepCopyBitmapSubset(sourceBitmap, subset, leftOffset, topOffset)) 471 return false; 472 // Now upload 473 SkIRect textureInval = SkIRect::MakeXYWH(enclosingScreenDirty.x() - tileRect.x(), 474 enclosingScreenDirty.y() - tileRect.y(), 475 enclosingScreenDirty.width(), 476 enclosingScreenDirty.height()); 477 GLUtils::updateTextureWithBitmap(tile->frontTexture()->m_ownTextureId, 478 subset, textureInval); 479 tile->onBlitUpdate(); 480 return true; 481 } 482 483 const TransformationMatrix* Surface::drawTransform() 484 { 485 // single layer surfaces query the layer's draw transform, while multi-layer 486 // surfaces copy the draw transform once, during initialization 487 // TODO: support fixed multi-layer surfaces by querying the changing drawTransform 488 if (singleLayer()) 489 return getFirstLayer()->drawTransform(); 490 491 return &m_drawTransform; 492 } 493 494 } // namespace WebCore 495