private void updateTileBuffers(BSTimerTask task) {
    if (mContentsFrozen) return;

    mUpdateFlowBreaked = false;
    mClient.tbsPaintBegin();

    List<Rect> paintedArea = new ArrayList<Rect>();
    List<ITile> dirtyTiles = new ArrayList<ITile>();
    Iterator<Entry<Coordinate, ITile>> it = mMainTiles.iterator();
    while (it.hasNext()) {
      Entry<Coordinate, ITile> entry = it.next();
      if (!entry.getValue().isDirty()) continue;
      dirtyTiles.add(entry.getValue());
    }

    if (dirtyTiles.isEmpty()) {
      mClient.tbsPaintEnd(paintedArea);
      return;
    }

    // FIXME: In single threaded case, tile back buffers could be updated asynchronously
    // one by one and then swapped to front in one go. This would minimize the time spent
    // blocking on tile updates.
    int size = dirtyTiles.size();
    for (int n = 0; n < size; ++n) {
      Collection<Rect> paintedRects = dirtyTiles.get(n).updateBackBuffer();
      paintedArea.addAll(paintedRects);
      dirtyTiles.get(n).swapBackBufferToFront();
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      if (task != null && task.cancelled()) {
        mUpdateFlowBreaked = true;
        break;
      }
    }

    mClient.tbsPaintEnd(paintedArea);
  }
  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);
    }
  }