Skip to content

Commit

Permalink
feat(🔥): Remove deprecated onTouch prop (#2669)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 1, 2024
1 parent fe24c03 commit d2b5e8b
Show file tree
Hide file tree
Showing 22 changed files with 16 additions and 682 deletions.
3 changes: 0 additions & 3 deletions packages/skia/android/cpp/jni/include/JniSkiaBaseView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class JniSkiaBaseView {
}

protected:
virtual void updateTouchPoints(jni::JArrayDouble touches) {
_skiaAndroidView->updateTouchPoints(touches);
}

virtual void surfaceAvailable(jobject surface, int width, int height) {
_skiaAndroidView->surfaceAvailable(surface, width, height);
Expand Down
6 changes: 0 additions & 6 deletions packages/skia/android/cpp/jni/include/JniSkiaDomView.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ class JniSkiaDomView : public jni::HybridClass<JniSkiaDomView>,
JniSkiaDomView::surfaceSizeChanged),
makeNativeMethod("setMode", JniSkiaDomView::setMode),
makeNativeMethod("setDebugMode", JniSkiaDomView::setDebugMode),
makeNativeMethod("updateTouchPoints",
JniSkiaDomView::updateTouchPoints),
makeNativeMethod("registerView", JniSkiaDomView::registerView),
makeNativeMethod("unregisterView", JniSkiaDomView::unregisterView)});
}

protected:
void updateTouchPoints(jni::JArrayDouble touches) override {
JniSkiaBaseView::updateTouchPoints(touches);
}

void surfaceAvailable(jobject surface, int width, int height) override {
JniSkiaBaseView::surfaceAvailable(surface, width, height);
}
Expand Down
5 changes: 0 additions & 5 deletions packages/skia/android/cpp/jni/include/JniSkiaPictureView.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,12 @@ class JniSkiaPictureView : public jni::HybridClass<JniSkiaPictureView>,
JniSkiaPictureView::surfaceSizeChanged),
makeNativeMethod("setMode", JniSkiaPictureView::setMode),
makeNativeMethod("setDebugMode", JniSkiaPictureView::setDebugMode),
makeNativeMethod("updateTouchPoints",
JniSkiaPictureView::updateTouchPoints),
makeNativeMethod("registerView", JniSkiaPictureView::registerView),
makeNativeMethod("unregisterView",
JniSkiaPictureView::unregisterView)});
}

protected:
void updateTouchPoints(jni::JArrayDouble touches) override {
JniSkiaBaseView::updateTouchPoints(touches);
}

void surfaceAvailable(jobject surface, int width, int height) override {
JniSkiaBaseView::surfaceAvailable(surface, width, height);
Expand Down
20 changes: 0 additions & 20 deletions packages/skia/android/cpp/rnskia-android/RNSkAndroidView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class RNSkBaseAndroidView {

virtual float getPixelDensity() = 0;

virtual void updateTouchPoints(jni::JArrayDouble touches) = 0;

virtual void setMode(std::string mode) = 0;

virtual void setShowDebugInfo(bool show) = 0;
Expand Down Expand Up @@ -76,24 +74,6 @@ class RNSkAndroidView : public T, public RNSkBaseAndroidView {

void viewDidUnmount() override { T::endDrawingLoop(); }

void updateTouchPoints(jni::JArrayDouble touches) override {
// Create touch points
std::vector<RNSkia::RNSkTouchInfo> points;
auto pin = touches.pin();
auto scale = getPixelDensity();
points.reserve(pin.size() / 5);
for (size_t i = 0; i < pin.size(); i += 5) {
RNSkTouchInfo point;
point.x = pin[i] / scale;
point.y = pin[i + 1] / scale;
point.force = pin[i + 2];
point.type = (RNSkia::RNSkTouchInfo::TouchType)pin[i + 3];
point.id = pin[i + 4];
points.push_back(point);
}
T::updateTouchState(points);
}

std::shared_ptr<RNSkView> getSkiaView() override {
return T::shared_from_this();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,76 +53,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
mTexture.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
// https://developer.android.com/training/gestures/multi
int action = ev.getActionMasked();

MotionEvent.PointerCoords r = new MotionEvent.PointerCoords();

double[] points;

// If this is a pointer_up/down event we need to handle it a bit specialized
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP: {
points = new double[5];
int pointerIndex = ev.getActionIndex();
ev.getPointerCoords(pointerIndex, r);
points[0] = r.x;
points[1] = r.y;
points[2] = ev.getPressure(pointerIndex);
points[3] = motionActionToType(action);
points[4] = ev.getPointerId(pointerIndex);

updateTouchPoints(points);

break;
}
default: {
// For the rest we can just handle it like expected
int count = ev.getPointerCount();
int pointerIndex = 0;
points = new double[5 * count];
for (int i = 0; i < count; i++) {
ev.getPointerCoords(i, r);
points[pointerIndex++] = r.x;
points[pointerIndex++] = r.y;
points[pointerIndex++] = ev.getPressure(i);
points[pointerIndex++] = motionActionToType(action);
points[pointerIndex++] = ev.getPointerId(i);
}

updateTouchPoints(points);

break;
}
}

return true;
}

private static int motionActionToType(int action) {
int actionType = 3;
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
actionType = 0;
break;
case MotionEvent.ACTION_MOVE:
actionType = 1;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
actionType = 2;
break;
case MotionEvent.ACTION_CANCEL:
actionType = 3;
break;
}
return actionType;
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i(tag, "onSurfaceTextureAvailable " + width + "/" + height);
Expand Down Expand Up @@ -172,8 +102,6 @@ public void onSurfaceTextureUpdated(SurfaceTexture surface) {

protected abstract void setDebugMode(boolean show);

protected abstract void updateTouchPoints(double[] points);

protected abstract void registerView(int nativeId);

protected abstract void unregisterView();
Expand Down
79 changes: 0 additions & 79 deletions packages/skia/cpp/rnskia/RNSkDomView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ RNSkDomRenderer::RNSkDomRenderer(std::function<void()> requestRedraw,
std::shared_ptr<RNSkPlatformContext> context)
: RNSkRenderer(requestRedraw), _platformContext(std::move(context)),
_renderLock(std::make_shared<std::timed_mutex>()),
_touchCallbackLock(std::make_shared<std::timed_mutex>()),
_renderTimingInfo("SKIA/RENDER") {}

RNSkDomRenderer::~RNSkDomRenderer() {
Expand All @@ -29,10 +28,6 @@ RNSkDomRenderer::~RNSkDomRenderer() {

bool RNSkDomRenderer::tryRender(
std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
// If we have touches we need to call the touch callback as well
if (_currentTouches.size() > 0) {
callOnTouch();
}

// We render on the main thread
if (_renderLock->try_lock()) {
Expand Down Expand Up @@ -70,11 +65,6 @@ void RNSkDomRenderer::setRoot(std::shared_ptr<JsiDomRenderNode> node) {
_root = node;
}

void RNSkDomRenderer::setOnTouchCallback(
std::shared_ptr<jsi::Function> onTouchCallback) {
_touchCallback = onTouchCallback;
}

void RNSkDomRenderer::renderCanvas(SkCanvas *canvas, float scaledWidth,
float scaledHeight) {
_renderTimingInfo.beginTiming();
Expand Down Expand Up @@ -125,75 +115,6 @@ void RNSkDomRenderer::renderCanvas(SkCanvas *canvas, float scaledWidth,
_renderTimingInfo.stopTiming();
}

void RNSkDomRenderer::updateTouches(std::vector<RNSkTouchInfo> &touches) {
std::lock_guard<std::mutex> lock(_touchMutex);
// Add timestamp
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();

for (size_t i = 0; i < touches.size(); i++) {
touches.at(i).timestamp = ms;
}
_currentTouches.push_back(std::move(touches));
}

void RNSkDomRenderer::callOnTouch() {

if (_touchCallback == nullptr) {
return;
}

if (_touchCallbackLock->try_lock()) {

{
std::lock_guard<std::mutex> lock(_touchMutex);
_touchesCache.clear();
_touchesCache.reserve(_currentTouches.size());
for (size_t i = 0; i < _currentTouches.size(); ++i) {
_touchesCache.push_back(_currentTouches.at(i));
}
_currentTouches.clear();
}

// We have an onDraw method - use it to render since we don't have a
// DOM-node yet.
_platformContext->runOnJavascriptThread([weakSelf = weak_from_this()]() {
auto self = weakSelf.lock();
if (self) {
jsi::Runtime &runtime = *self->_platformContext->getJsRuntime();
// Set up touches
auto size = self->_touchesCache.size();
auto ops = jsi::Array(runtime, size);
for (size_t i = 0; i < size; i++) {
auto cur = self->_touchesCache.at(i);
auto curSize = cur.size();
auto touches = jsi::Array(runtime, curSize);
for (size_t n = 0; n < curSize; n++) {
auto touchObj = jsi::Object(runtime);
auto t = cur.at(n);
touchObj.setProperty(runtime, "x", t.x);
touchObj.setProperty(runtime, "y", t.y);
touchObj.setProperty(runtime, "force", t.force);
touchObj.setProperty(runtime, "type", static_cast<double>(t.type));
touchObj.setProperty(runtime, "timestamp",
static_cast<double>(t.timestamp) / 1000.0);
touchObj.setProperty(runtime, "id", static_cast<double>(t.id));
touches.setValueAtIndex(runtime, n, touchObj);
}
ops.setValueAtIndex(runtime, i, touches);
}
// Call on touch callback
self->_touchCallback->call(runtime, ops, 1);
}
self->_touchCallbackLock->unlock();
});
} else {
// We'll try next time - schedule a new redraw
_requestRedraw();
}
}

void RNSkDomRenderer::renderDebugOverlays(SkCanvas *canvas) {
if (!getShowDebugOverlays()) {
return;
Expand Down
39 changes: 1 addition & 38 deletions packages/skia/cpp/rnskia/RNSkDomView.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,19 @@ class RNSkDomRenderer : public RNSkRenderer,

void setRoot(std::shared_ptr<JsiDomRenderNode> node);

void setOnTouchCallback(std::shared_ptr<jsi::Function> onTouchCallback);

void updateTouches(std::vector<RNSkTouchInfo> &touches);

private:
void callOnTouch();
void renderCanvas(SkCanvas *canvas, float scaledWidth, float scaledHeight);
void renderDebugOverlays(SkCanvas *canvas);

std::shared_ptr<RNSkPlatformContext> _platformContext;
std::shared_ptr<jsi::Function> _touchCallback;

std::shared_ptr<std::timed_mutex> _renderLock;
std::shared_ptr<std::timed_mutex> _touchCallbackLock;

std::shared_ptr<JsiDomRenderNode> _root;
std::shared_ptr<DrawingContext> _drawingContext;

RNSkTimingInfo _renderTimingInfo;

std::mutex _touchMutex;
std::vector<std::vector<RNSkTouchInfo>> _currentTouches;
std::vector<std::vector<RNSkTouchInfo>> _touchesCache;
std::mutex _rootLock;
};

Expand All @@ -87,40 +77,13 @@ class RNSkDomView : public RNSkView {
std::make_shared<RNSkDomRenderer>(
std::bind(&RNSkView::requestRedraw, this), context)) {}

void updateTouchState(std::vector<RNSkTouchInfo> &touches) override {
std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
->updateTouches(touches);
RNSkView::updateTouchState(touches);
}

void setJsiProperties(
std::unordered_map<std::string, JsiValueWrapper> &props) override {

RNSkView::setJsiProperties(props);

for (auto &prop : props) {
if (prop.first == "onTouch") {
if (prop.second.isUndefinedOrNull()) {
// Clear touchCallback
std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
->setOnTouchCallback(nullptr);
requestRedraw();
continue;

} else if (prop.second.getType() != JsiWrapperValueType::Function) {
// We expect a function for the draw callback custom property
throw std::runtime_error(
"Expected a function for the onTouch property.");
}

// Save callback
std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
->setOnTouchCallback(prop.second.getAsFunction());

// Request redraw
requestRedraw();

} else if (prop.first == "root") {
if (prop.first == "root") {
// Save root
if (prop.second.isUndefined() || prop.second.isNull()) {
std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
Expand Down
Loading

0 comments on commit d2b5e8b

Please sign in to comment.