public void invalidate(final Rect contentsDirtyRect) { Rect dirtyRect = new Rect(mapFromContents(contentsDirtyRect)); Rect keepRectFitToTileSize = tileRectForCoordinate(tileCoordinateForPoint(mKeepRect.getLeft(), mKeepRect.getTop())); keepRectFitToTileSize.composite( tileRectForCoordinate(tileCoordinateForPoint(mKeepRect.getRight(), mKeepRect.getBottom()))); // Only iterate on the part of the rect that we know we might have tiles. Rect coveredDirtyRect = Rect.intersect(dirtyRect, keepRectFitToTileSize); Coordinate topLeft = tileCoordinateForPoint(coveredDirtyRect.getLeft(), coveredDirtyRect.getTop()); Coordinate bottomRight = tileCoordinateForPoint(coveredDirtyRect.getRight(), coveredDirtyRect.getBottom()); for (int yCoordinate = topLeft.getY(); yCoordinate <= bottomRight.getY(); ++yCoordinate) { for (int xCoordinate = topLeft.getX(); xCoordinate <= bottomRight.getX(); ++xCoordinate) { ITile currentTile = getTileAt(xCoordinate, yCoordinate); if (currentTile == null) continue; // Pass the full rect to each tile as coveredDirtyRect might not // contain them completely and we don't want partial tile redraws. currentTile.invalidate(dirtyRect); } } startTileBufferUpdateTask(); }
@Override public void tbsPaint(Canvas canvas, Rect rect) { canvas.translate(-rect.getLeft(), -rect.getTop()); float contentsScale = mBS.getContentsScale(); LinearGradient mLinearGradient = new LinearGradient( 0, 0, 500 * contentsScale, 500 * contentsScale, new int[] { android.graphics.Color.RED, android.graphics.Color.GREEN, android.graphics.Color.BLUE, android.graphics.Color.WHITE }, null, Shader.TileMode.REPEAT); Paint paint = new Paint(); paint.setShader(mLinearGradient); canvas.clipRect(rect.getLeft(), rect.getTop(), rect.getRight(), rect.getBottom()); canvas.drawRect( mContentsRect.getLeft() * contentsScale, mContentsRect.getTop() * contentsScale, mContentsRect.getRight() * contentsScale, mContentsRect.getBottom() * contentsScale, paint); }
private Rect mapFromContents(final Rect rect, float contentsScale) { int l = (int) (rect.getLeft() * contentsScale); int t = (int) (rect.getTop() * contentsScale); int r = (int) (l + rect.getWidth() * contentsScale + 0.5f); int b = (int) (t + rect.getHeight() * contentsScale + 0.5f); return new Rect(l, t, r - l, b - t); }
public Rect mapToContents(final Rect rect) { int l = (int) (rect.getLeft() / mContentsScale); int t = (int) (rect.getTop() / mContentsScale); int r = (int) (l + rect.getWidth() / mContentsScale + 0.5f); int b = (int) (t + rect.getHeight() / mContentsScale + 0.5f); return new Rect(l, t, r - l, b - t); }
@Override public Rect tbsGetVisibleRect() { Rect visible = new Rect(-mX, -mY, 2560, 1600); visible.intersect(new Rect(0, 0, mWidth, mHeight)); float scale = mWidth / (float) mContentsRect.getWidth(); visible.setLeft((int) (visible.getLeft() / scale)); visible.setTop((int) (visible.getTop() / scale)); visible.setWidth((int) (visible.getWidth() / scale)); visible.setHeight((int) (visible.getHeight() / scale)); return visible; }
private float coverageRatio(final Rect contentsRect) { Rect dirtyRect = mapFromContents(contentsRect); float rectArea = dirtyRect.getWidth() * dirtyRect.getHeight(); float coverArea = 0.0f; Coordinate topLeft = tileCoordinateForPoint(dirtyRect.getLeft(), dirtyRect.getTop()); Coordinate bottomRight = tileCoordinateForPoint(dirtyRect.getRight(), dirtyRect.getBottom()); for (int yCoordinate = topLeft.getY(); yCoordinate <= bottomRight.getY(); ++yCoordinate) { for (int xCoordinate = topLeft.getX(); xCoordinate <= bottomRight.getX(); ++xCoordinate) { ITile currentTile = getTileAt(xCoordinate, yCoordinate); if (currentTile != null && currentTile.isReadyToPaint()) { Rect coverRect = Rect.intersect(dirtyRect, currentTile.rect()); coverArea += coverRect.getWidth() * coverRect.getHeight(); } } } return coverArea / rectArea; }
private void createTiles(BSTimerTask task) { // Guard here as as these can change before the timer fires. if (isBackingStoreUpdatesSuspended()) return; // Update our backing store geometry. final Rect previousRect = mRect.clone(); mapFromContents(mClient.tbsGetContentsRect()).copyTo(mRect); mPendingTrajectoryVector.copyTo(mTrajectoryVector); visibleRect().copyTo(mVisibleRect); if (mRect.isEmpty()) { setCoverRect(new Rect()); setKeepRect(new Rect()); return; } /* We must compute cover and keep rects using the visibleRect, instead of the rect intersecting the visibleRect with m_rect, * because TBS can be used as a backing store of GraphicsLayer and the visible rect usually does not intersect with m_rect. * In the below case, the intersecting rect is an empty. * * +---------------+ * | | * | m_rect | * | +-------|-----------------------+ * | | HERE | cover or keep | * +---------------+ rect | * | +---------+ | * | | visible | | * | | rect | | * | +---------+ | * | | * | | * +-------------------------------+ * * We must create or keep the tiles in the HERE region. */ Rect coverRect = new Rect(); Rect keepRect = new Rect(); computeCoverAndKeepRect(mVisibleRect, coverRect, keepRect); setCoverRect(coverRect); setKeepRect(keepRect); if (coverRect.isEmpty()) return; // Resize tiles at the edge in case the contents size has changed, but only do so // after having dropped tiles outside the keep rect. boolean didResizeTiles = false; if (!previousRect.equals(mRect)) didResizeTiles = resizeEdgeTiles(); // Search for the tile position closest to the viewport center that does not yet contain a tile. // Which position is considered the closest depends on the tileDistance function. double shortestDistance = Double.POSITIVE_INFINITY; List<Coordinate> tilesToCreate = new ArrayList<Coordinate>(); int requiredTileCount = 0; // Cover areas (in tiles) with minimum distance from the visible rect. If the visible rect is // not covered already it will be covered first in one go, due to the distance being 0 for tiles // inside the visible rect. Coordinate topLeft = tileCoordinateForPoint(coverRect.getLeft(), coverRect.getTop()); Coordinate bottomRight = tileCoordinateForPoint(coverRect.getRight(), coverRect.getBottom()); for (int yCoordinate = topLeft.getY(); yCoordinate <= bottomRight.getY(); ++yCoordinate) { for (int xCoordinate = topLeft.getX(); xCoordinate <= bottomRight.getX(); ++xCoordinate) { if (getTileAt(xCoordinate, yCoordinate) != null) continue; ++requiredTileCount; double distance = tileDistance(mVisibleRect, xCoordinate, yCoordinate); if (distance > shortestDistance) continue; if (distance < shortestDistance) { tilesToCreate.clear(); shortestDistance = distance; } tilesToCreate.add(new Coordinate(xCoordinate, yCoordinate)); } } // Now construct the tile(s) within the shortest distance. int tilesToCreateCount = tilesToCreate.size(); for (int n = 0; n < tilesToCreateCount; ++n) { Coordinate coordinate = tilesToCreate.get(n); setTile(coordinate, mBackend.createTile(coordinate)); } requiredTileCount -= tilesToCreateCount; // Paint the content of the newly created tiles or resized tiles. if (tilesToCreateCount != 0 || didResizeTiles) updateTileBuffers(task); // Re-call createTiles on a timer to cover the visible area with the newest shortest distance. mPendingTileCreation = requiredTileCount != 0; if (mPendingTileCreation) { if (!mCommitTileUpdatesOnIdleEventLoop) { mClient.tbsHasPendingTileCreation(); return; } Log.d( "tt", "start BSUpdate scheduleTask in createTiles func scale " + mContentsScale + " pending scale " + mPendingScale); if (task == null || !task.cancelled()) startBSUpdateTask(TILE_CREATION_DELAY_MS); } }
public void paint(GLCanvas canvas, int offsetX, int offsetY, final Rect rect) { synchronized (mScaleBufferTiles) { Iterator<Entry<Coordinate, ITile>> it = mScaleBufferTiles.iterator(); while (it.hasNext()) { Entry<Coordinate, ITile> entry = it.next(); ITile currentTile = entry.getValue(); if (currentTile != null && currentTile.isReadyToPaint()) { float scale = mPendingScale == 0 ? mContentsScale : mPendingScale; currentTile.paint(canvas, offsetX, offsetY, null, scale / mScaleBufferTiles.getScale()); } } } Rect dirtyRect = mapFromContents(rect, mScaleBufferTiles.getScale()); Coordinate topLeft = tileCoordinateForPoint(dirtyRect.getLeft(), dirtyRect.getTop()); Coordinate bottomRight = tileCoordinateForPoint(dirtyRect.getRight(), dirtyRect.getBottom()); // for (int yCoordinate = topLeft.getY(); yCoordinate <= bottomRight.getY(); // ++yCoordinate) { // for (int xCoordinate = topLeft.getX(); xCoordinate <= bottomRight.getX(); // ++xCoordinate) { // Coordinate currentCoordinate = new Coordinate(xCoordinate, yCoordinate); // ITile currentTile = mScaleBufferTiles.get(currentCoordinate); // if (currentTile != null && currentTile.isReadyToPaint()) { // currentTile.paint(canvas, offsetX, offsetY, dirtyRect, mPendingScale / // mScaleBufferTiles.getScale()); // } else { // Rect tileRect = tileRectForCoordinate(currentCoordinate); // Rect target = Rect.intersect(tileRect, dirtyRect); // if (target == null || target.isEmpty()) // continue; // //// float scaleFactor = mOldScale == 0 ? 1 : mContentsScale / mOldScale; //// float left = offsetX + target.getLeft() * scaleFactor; //// float top = offsetY + target.getTop() * scaleFactor; //// float width = target.getWidth() * scaleFactor; //// float height = target.getHeight() * scaleFactor; // // canvas.drawTexture(mTextureBuffer.getCheckerTextureID(canvas.getGL(), // (int)width, (int)height), left, top, width, height); // } // // } // } dirtyRect = mapFromContents(rect); topLeft = tileCoordinateForPoint(dirtyRect.getLeft(), dirtyRect.getTop()); bottomRight = tileCoordinateForPoint(dirtyRect.getRight(), dirtyRect.getBottom()); for (int yCoordinate = topLeft.getY(); yCoordinate <= bottomRight.getY(); ++yCoordinate) { for (int xCoordinate = topLeft.getX(); xCoordinate <= bottomRight.getX(); ++xCoordinate) { Coordinate currentCoordinate = new Coordinate(xCoordinate, yCoordinate); ITile currentTile = mMainTiles.get(currentCoordinate); if (currentTile != null && currentTile.isReadyToPaint()) { float scaleFactor = mPendingScale == 0 ? 1 : mPendingScale / mContentsScale; currentTile.paint(canvas, offsetX, offsetY, dirtyRect, scaleFactor); } else { Rect tileRect = tileRectForCoordinate(currentCoordinate); Rect target = Rect.intersect(tileRect, dirtyRect); if (target == null || target.isEmpty()) continue; float scaleFactor = mPendingScale == 0 ? 1 : mPendingScale / mContentsScale; float left = offsetX + target.getLeft() * scaleFactor; float top = offsetY + target.getTop() * scaleFactor; float width = target.getWidth() * scaleFactor; float height = target.getHeight() * scaleFactor; // // canvas.drawTexture(mTextureBuffer.getCheckerTextureID(canvas.getGL(), (int)width, // (int)height), left, top, width, height); } } } }