private void computeCoverAndKeepRect(final Rect visibleRect, Rect coverRect, Rect keepRect) {
    visibleRect.copyTo(coverRect);
    visibleRect.copyTo(keepRect);

    // If we cover more that the actual viewport we can be smart about which tiles we choose to
    // render.
    if (mCoverAreaMultiplier > 1) {
      // The initial cover area covers equally in each direction, according to the
      // coverAreaMultiplier.
      coverRect.inflate(
          (int) (visibleRect.getWidth() * (mCoverAreaMultiplier - 1) / 2),
          (int) (visibleRect.getHeight() * (mCoverAreaMultiplier - 1) / 2));
      coverRect.copyTo(keepRect);

      if (mPendingTrajectoryVector.getX() != 0 || mPendingTrajectoryVector.getY() != 0) {
        // A null trajectory vector (no motion) means that tiles for the coverArea will be created.
        // A non-null trajectory vector will shrink the covered rect to visibleRect plus its
        // expansion from its
        // center toward the cover area edges in the direction of the given vector.

        // E.g. if visibleRect == (10,10)5x5 and coverAreaMultiplier == 3.0:
        // a (0,0) trajectory vector will create tiles intersecting (5,5)15x15,
        // a (1,0) trajectory vector will create tiles intersecting (10,10)10x5,
        // and a (1,1) trajectory vector will create tiles intersecting (10,10)10x10.

        // Multiply the vector by the distance to the edge of the cover area.
        float trajectoryVectorMultiplier = (mCoverAreaMultiplier - 1) / 2;

        // Unite the visible rect with a "ghost" of the visible rect moved in the direction of the
        // trajectory vector.
        visibleRect.copyTo(coverRect);
        coverRect.offset(
            (int) (coverRect.getWidth() * mTrajectoryVector.getX() * trajectoryVectorMultiplier),
            (int) (coverRect.getHeight() * mTrajectoryVector.getY() * trajectoryVectorMultiplier));

        coverRect.composite(visibleRect);
      }

      assert (keepRect.contains(coverRect));
    }

    adjustForContentsRect(coverRect);

    // The keep rect is an inflated version of the cover rect, inflated in tile dimensions.
    keepRect.composite(coverRect);
    keepRect.inflate(mTileSize.getWidth() / 2, mTileSize.getHeight() / 2);
    keepRect.intersect(mRect);

    assert (coverRect.isEmpty() || keepRect.contains(coverRect));
  }
 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);
 }
  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;
  }
 @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;
 }
  public void setSize(int width, int height) {
    mWidth = width;
    mHeight = height;

    float scaleW = mWidth / (float) mContentsRect.getWidth();
    float scaleH = mHeight / (float) mContentsRect.getHeight();
    if (scaleW > scaleH) {
      mBS.setContentsScale(scaleH);
    } else {
      mBS.setContentsScale(scaleW);
    }
  }
  private void adjustForContentsRect(Rect rect) {
    Rect bounds = mRect;
    Size candidateSize = new Size(rect.getWidth(), rect.getHeight());

    rect.intersect(bounds);

    if (rect.getWidth() == candidateSize.getWidth()
        && rect.getHeight() == candidateSize.getHeight()) return;

    /*
     * In the following case, there is no intersection of the contents rect and the cover rect.
     * Thus the latter should not be inflated.
     *
     *  +---------------+
     *  |   m_rect      |
     *  +---------------+
     *
     *          +-------------------------------+
     *          |          cover rect           |
     *          |         +---------+           |
     *          |         | visible |           |
     *          |         |  rect   |           |
     *          |         +---------+           |
     *          +-------------------------------+
     */
    if (rect.isEmpty()) return;

    // Try to create a cover rect of the same size as the candidate, but within content bounds.
    int pixelsCovered = candidateSize.getWidth() * candidateSize.getHeight();

    if (rect.getWidth() < candidateSize.getWidth())
      rect.inflate(0, ((pixelsCovered / rect.getWidth()) - rect.getHeight()) / 2);
    if (rect.getHeight() < candidateSize.getHeight())
      rect.inflate(((pixelsCovered / rect.getHeight()) - rect.getWidth()) / 2, 0);

    rect.intersect(bounds);
  }
 private boolean resizeEdgeTiles() {
   boolean wasResized = false;
   List<Coordinate> tilesToRemove = new ArrayList<Coordinate>();
   Iterator<Entry<Coordinate, ITile>> it = mMainTiles.iterator();
   while (it.hasNext()) {
     Entry<Coordinate, ITile> entry = it.next();
     Coordinate tileCoordinate = entry.getValue().coordinate();
     Rect tileRect = entry.getValue().rect();
     Rect expectedTileRect = tileRectForCoordinate(tileCoordinate);
     if (expectedTileRect.isEmpty()) tilesToRemove.add(tileCoordinate);
     else if (!expectedTileRect.equals(tileRect)) {
       entry.getValue().resize(expectedTileRect.getWidth(), expectedTileRect.getHeight());
       wasResized = true;
     }
   }
   int removeCount = tilesToRemove.size();
   for (int n = 0; n < removeCount; ++n) removeTile(tilesToRemove.get(n));
   return wasResized;
 }
  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);
        }
      }
    }
  }