feat: implement filter region lengths directly on RNSVGFilterRegion (#2485)

# Summary

[apple] Use filter region directly instead of creating new one on every
rerender.
[android] rename some variables and add temporary fix for null lengths
This commit is contained in:
Jakub Grzywacz
2024-10-14 14:47:44 +02:00
committed by GitHub
parent 2b905c4b41
commit 3aae632d1f
10 changed files with 85 additions and 77 deletions

View File

@@ -9,30 +9,30 @@ import java.util.HashMap;
@SuppressLint("ViewConstructor")
class FilterPrimitiveView extends DefinitionView {
private String mResult;
public final FilterRegion mFilterRegion;
public final FilterRegion mFilterSubregion;
public FilterPrimitiveView(ReactContext reactContext) {
super(reactContext);
mFilterRegion = new FilterRegion();
mFilterSubregion = new FilterRegion();
}
public void setX(Dynamic x) {
mFilterRegion.setX(x);
mFilterSubregion.setX(x);
invalidate();
}
public void setY(Dynamic y) {
mFilterRegion.setY(y);
mFilterSubregion.setY(y);
invalidate();
}
public void setWidth(Dynamic width) {
mFilterRegion.setWidth(width);
mFilterSubregion.setWidth(width);
invalidate();
}
public void setHeight(Dynamic height) {
mFilterRegion.setHeight(height);
mFilterSubregion.setHeight(height);
invalidate();
}

View File

@@ -26,24 +26,24 @@ public class FilterRegion {
mH = SVGLength.from(height);
}
public Rect getCropRect(VirtualView view, FilterProperties.Units units, RectF renderableBounds) {
public Rect getCropRect(VirtualView view, FilterProperties.Units units, RectF bounds) {
double x, y, width, height;
if (units == FilterProperties.Units.USER_SPACE_ON_USE) {
x = view.relativeOn(this.mX, view.getSvgView().getCanvasWidth());
y = view.relativeOn(this.mY, view.getSvgView().getCanvasHeight());
width = view.relativeOn(this.mW, view.getSvgView().getCanvasWidth());
height = view.relativeOn(this.mH, view.getSvgView().getCanvasHeight());
x = this.mX == null ? 0 : view.relativeOn(this.mX, view.getSvgView().getCanvasWidth());
y = this.mY == null ? 0 : view.relativeOn(this.mY, view.getSvgView().getCanvasHeight());
width = this.mW == null ? 0 : view.relativeOn(this.mW, view.getSvgView().getCanvasWidth());
height = this.mH == null ? 0 : view.relativeOn(this.mH, view.getSvgView().getCanvasHeight());
return new Rect((int) x, (int) y, (int) (x + width), (int) (y + height));
} else { // FilterProperties.Units.OBJECT_BOUNDING_BOX
x = view.relativeOnFraction(this.mX, renderableBounds.width());
y = view.relativeOnFraction(this.mY, renderableBounds.height());
width = view.relativeOnFraction(this.mW, renderableBounds.width());
height = view.relativeOnFraction(this.mH, renderableBounds.height());
x = view.relativeOnFraction(this.mX, bounds.width());
y = view.relativeOnFraction(this.mY, bounds.height());
width = view.relativeOnFraction(this.mW, bounds.width());
height = view.relativeOnFraction(this.mH, bounds.height());
return new Rect(
(int) (renderableBounds.left + x),
(int) (renderableBounds.top + y),
(int) (renderableBounds.left + x + width),
(int) (renderableBounds.top + y + height));
(int) (bounds.left + x),
(int) (bounds.top + y),
(int) (bounds.left + x + width),
(int) (bounds.top + y + height));
}
}
}

View File

@@ -55,6 +55,10 @@ class FilterView extends DefinitionView {
invalidate();
}
public FilterRegion getFilterRegion() {
return mFilterRegion;
}
@Override
void saveDefinition() {
if (mName != null) {
@@ -75,6 +79,8 @@ class FilterView extends DefinitionView {
Bitmap res = source;
Bitmap resultBitmap = Bitmap.createBitmap(res.getWidth(), res.getHeight(), res.getConfig());
Canvas canvas = new Canvas(resultBitmap);
Rect filterRegionRect =
this.mFilterRegion.getCropRect(this, this.mFilterUnits, renderableBounds);
Rect cropRect;
for (int i = 0; i < getChildCount(); i++) {
@@ -83,7 +89,7 @@ class FilterView extends DefinitionView {
FilterPrimitiveView currentFilter = (FilterPrimitiveView) node;
resultBitmap.eraseColor(Color.TRANSPARENT);
cropRect =
currentFilter.mFilterRegion.getCropRect(
currentFilter.mFilterSubregion.getCropRect(
currentFilter, this.mPrimitiveUnits, renderableBounds);
canvas.drawBitmap(currentFilter.applyFilter(mResultsMap, res), cropRect, cropRect, null);
res = resultBitmap.copy(Bitmap.Config.ARGB_8888, true);
@@ -98,8 +104,8 @@ class FilterView extends DefinitionView {
// crop Bitmap to filter coordinates
resultBitmap.eraseColor(Color.TRANSPARENT);
cropRect = this.mFilterRegion.getCropRect(this, this.mFilterUnits, renderableBounds);
canvas.drawBitmap(res, cropRect, cropRect, null);
canvas.drawBitmap(res, filterRegionRect, filterRegionRect, null);
return resultBitmap;
}
}