Skip to content

Commit f7cb94e

Browse files
harryterkelsenSkCQ
authored and
SkCQ
committed
Add Canvas.quickReject to quickly tell if a Rect is within the clip
Change-Id: I72fe7c22c863992519c47e54b26528c1ae5390f1 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/903702 Reviewed-by: Kaylee Lubick <[email protected]> Commit-Queue: Harry Terkelsen <[email protected]>
1 parent d50cbfd commit f7cb94e

File tree

8 files changed

+700
-209
lines changed

8 files changed

+700
-209
lines changed

modules/canvaskit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Added
2020
- `CanvasKit.Typeface.GetDefault()` as a way to explicitly get the compiled-in typeface (if any).
21+
- `Canvas.quickReject` to quickly check if a Rect is within the current clip region.
2122

2223
## [0.39.1] - 2023-10-12
2324

modules/canvaskit/canvaskit_bindings.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
#include "include/gpu/ganesh/GrTypes.h"
9292
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
9393
#include "include/gpu/ganesh/gl/GrGLDirectContext.h"
94-
#include "include/gpu/ganesh/gl/GrGLMakeWebGLInterface.h"
9594
#include "include/gpu/ganesh/gl/GrGLInterface.h"
95+
#include "include/gpu/ganesh/gl/GrGLMakeWebGLInterface.h"
9696
#include "include/gpu/ganesh/gl/GrGLTypes.h"
9797
#include "src/gpu/RefCntedCallback.h"
9898
#include "src/gpu/ganesh/GrProxyProvider.h"
@@ -1425,6 +1425,12 @@ EMSCRIPTEN_BINDINGS(Skia) {
14251425
}
14261426
self.getDeviceClipBounds(outputRect);
14271427
}))
1428+
1429+
.function("_quickReject", optional_override([](const SkCanvas& self, WASMPointerF32 fPtr)->bool {
1430+
const SkRect* rect = reinterpret_cast<const SkRect*>(fPtr);
1431+
return self.quickReject(*rect);
1432+
}))
1433+
14281434
// 4x4 matrix functions
14291435
// Just like with getTotalMatrix, we allocate the buffer for the 16 floats to go in from
14301436
// interface.js, so it can also free them when its done.

modules/canvaskit/externs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ var CanvasKit = {
322322
drawTextBlob: function() {},
323323
drawVertices: function() {},
324324
getDeviceClipBounds: function() {},
325+
quickReject: function() {},
325326
getLocalToDevice: function() {},
326327
getTotalMatrix: function() {},
327328
readPixels: function() {},
@@ -365,6 +366,7 @@ var CanvasKit = {
365366
_drawTextBlob: function() {},
366367
_drawVertices: function() {},
367368
_getDeviceClipBounds: function() {},
369+
_quickReject: function() {},
368370
_getLocalToDevice: function() {},
369371
_getTotalMatrix: function() {},
370372
_readPixels: function() {},

modules/canvaskit/interface.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,11 @@ CanvasKit.onRuntimeInitialized = function() {
779779
return copyIRectFromWasm(_scratchIRect, outputRect);
780780
};
781781

782+
CanvasKit.Canvas.prototype.quickReject = function(rect) {
783+
var rPtr = copyRectToWasm(rect);
784+
return this._quickReject(rPtr);
785+
};
786+
782787
// getLocalToDevice returns a 4x4 matrix.
783788
CanvasKit.Canvas.prototype.getLocalToDevice = function() {
784789
// _getLocalToDevice will copy the values into the pointer.

modules/canvaskit/npm_build/types/canvaskit-wasm-tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function canvasTests(CK: CanvasKit, canvas?: Canvas, paint?: Paint, path?: Path,
150150
canvas.drawVertices(verts, CK.BlendMode.DstOut, paint);
151151
const irect = canvas.getDeviceClipBounds(); // $ExpectType Int32Array
152152
const irect2 = canvas.getDeviceClipBounds(irect); // $ExpectType Int32Array
153+
const isCulled = canvas.quickReject(someRect); // $ExpectType boolean
153154
const matrTwo = canvas.getLocalToDevice(); // $ExpectType Float32Array
154155
const sc = canvas.getSaveCount(); // $ExpectType number
155156
const matrThree = canvas.getTotalMatrix(); // $ExpectType number[]

modules/canvaskit/npm_build/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,12 @@ export interface Canvas extends EmbindObject<"Canvas"> {
17291729
*/
17301730
getDeviceClipBounds(output?: IRect): IRect;
17311731

1732+
/**
1733+
* Returns true if the given rect, transformed by the current canvas
1734+
* transform, can be quickly determined to fall entirely outside the clip.
1735+
*/
1736+
quickReject(rect: InputRect): boolean;
1737+
17321738
/**
17331739
* Returns the current transform from local coordinates to the 'device', which for most
17341740
* purposes means pixels.

0 commit comments

Comments
 (0)