public CloseableReference<Bitmap> createBitmap(
      Bitmap source,
      int x,
      int y,
      int width,
      int height,
      @Nullable Matrix matrix,
      boolean filter,
      @Nullable Object callerContext) {
    Preconditions.checkNotNull(source, "Source bitmap cannot be null");
    checkXYSign(x, y);
    checkWidthHeight(width, height);
    checkFinalImageBounds(source, x, y, width, height);

    // assigned because matrix can modify the final width, height
    int newWidth = width;
    int newHeight = height;

    Canvas canvas;
    CloseableReference<Bitmap> bitmapRef;
    Paint paint;

    Rect srcRectangle = new Rect(x, y, x + width, y + height);
    RectF dstRectangle = new RectF(0, 0, width, height);
    Bitmap.Config newConfig = getSuitableBitmapConfig(source);

    if (matrix == null || matrix.isIdentity()) {
      bitmapRef = createBitmap(newWidth, newHeight, newConfig, source.hasAlpha(), callerContext);
      setPropertyFromSourceBitmap(source, bitmapRef.get());
      canvas = new Canvas(bitmapRef.get());
      paint = null; // not needed
    } else {
      boolean transformed = !matrix.rectStaysRect();
      RectF deviceRectangle = new RectF();
      matrix.mapRect(deviceRectangle, dstRectangle);

      newWidth = Math.round(deviceRectangle.width());
      newHeight = Math.round(deviceRectangle.height());
      bitmapRef =
          createBitmap(
              newWidth,
              newHeight,
              transformed ? Bitmap.Config.ARGB_8888 : newConfig,
              transformed || source.hasAlpha(),
              callerContext);

      setPropertyFromSourceBitmap(source, bitmapRef.get());
      canvas = new Canvas(bitmapRef.get());
      canvas.translate(-deviceRectangle.left, -deviceRectangle.top);
      canvas.concat(matrix);

      paint = new Paint();
      paint.setFilterBitmap(filter);
      if (transformed) {
        paint.setAntiAlias(true);
      }
    }

    canvas.drawBitmap(source, srcRectangle, dstRectangle, paint);
    canvas.setBitmap(null);

    return bitmapRef;
  }
Creates a bitmap from subset of the source bitmap, transformed by the optional matrix. It is
initialized with the same density as the original bitmap.

@param source The bitmap we are subsetting
@param x The x coordinate of the first pixel in source
@param y The y coordinate of the first pixel in source
@param width The number of pixels in each row
@param height The number of rows
@param matrix Optional matrix to be applied to the pixels
@param filter true if the source should be filtered. Only applies if the matrix contains more
    than just translation.
@param callerContext the Tag to track who create the Bitmap
@return a reference to the bitmap
@throws IllegalArgumentException if the x, y, width, height values are outside of the
    dimensions of the source bitmap, or width is <= 0, or height is <= 0
@throws TooManyBitmapsException if the pool is full
@throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
imagepipeline-base/src/main/java/com/facebook/imagepipeline/bitmaps/PlatformBitmapFactory.java