Skip to content

Commit

Permalink
fix: datarace on deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Jun 27, 2022
1 parent afcf385 commit f3c710a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
34 changes: 18 additions & 16 deletions ios/CanvasView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,26 @@ - (void) handleLongPress:(UILongPressGestureRecognizer*)recognizer {

-(void) handlePan: (UIPanGestureRecognizer*)recognizer {
if(_lasso && !_disableSelections) {
CGPoint point = [recognizer locationInView:self];
switch(recognizer.state) {
case UIGestureRecognizerStateBegan: {
renderer->startLasso(point.x, point.y);
break;
}
case UIGestureRecognizerStateChanged: {
renderer->updateLasso(point.x, point.y);
break;
}
case UIGestureRecognizerStateEnded:{
renderer->endLasso(point.x, point.y);
break;
@autoreleasepool {
CGPoint point = [recognizer locationInView:self];
switch(recognizer.state) {
case UIGestureRecognizerStateBegan: {
renderer->startLasso(point.x, point.y);
break;
}
case UIGestureRecognizerStateChanged: {
renderer->updateLasso(point.x, point.y);
break;
}
case UIGestureRecognizerStateEnded:{
renderer->endLasso(point.x, point.y);
break;
}
default:
break;
}
default:
break;
renderer->draw();
}
renderer->draw();
}
}

Expand Down
3 changes: 3 additions & 0 deletions ios/MetalLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
}

auto canvas = surface->getCanvas();
if(canvas == nullptr) {
return;
}
canvas->clear(SK_ColorWHITE);
renderView->draw(canvas);
surface->flushAndSubmit();
Expand Down
4 changes: 4 additions & 0 deletions ios/MetalRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "../cpp/jsi/ShapeFactory.h"
#include "../cpp/jsi/SelectionBrush.hpp"
#include <jsi/jsi.h>
#include <mutex>


using namespace facebook;
class MetalLayer;
Expand Down Expand Up @@ -41,6 +43,8 @@ class MetalRenderer {
void syncBrush();

protected:
bool tearingDown = false;
std::mutex drawMutex;
ShapeFactory shapeFactory;
std::string nativeId;
std::shared_ptr<SkiaRenderView> renderView;
Expand Down
14 changes: 7 additions & 7 deletions ios/MetalRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
}

void MetalRenderer::draw() {
@autoreleasepool {
std::lock_guard<std::mutex> lg(drawMutex);
if(!tearingDown) {
metallayer->draw(renderView);
}
}
Expand Down Expand Up @@ -63,7 +64,11 @@
}

void MetalRenderer::tearDown(){
Helium::TheCanvasViewManager::instance()->remove(nativeId);
std::lock_guard<std::mutex> lg(drawMutex);
tearingDown = true;
dispatch_async(dispatch_get_main_queue(), ^{
Helium::TheCanvasViewManager::instance()->remove(nativeId);
});
}

void MetalRenderer::clear(const std::string& vid) {
Expand Down Expand Up @@ -126,11 +131,6 @@

void MetalRenderer::endLasso(float x, float y) {
renderView->endLasso(Helium::toPx(x), Helium::toPx(y));
// endLasso does work on the JS thread, wait until
// its done before drawing.
Helium::TheCanvasViewManager::instance()->runOnJS([this]{
draw();
});
}

void MetalRenderer::updateLasso(float x, float y) {
Expand Down

0 comments on commit f3c710a

Please sign in to comment.