Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protected surfaces #1539

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"simple-get": "^3.0.3"
},
"devDependencies": {
"@types/node": "^10.12.18",
"@types/node": "^14.11.10",
"assert-rejects": "^1.0.0",
"dtslint": "^4.0.7",
"express": "^4.16.3",
Expand Down
2 changes: 0 additions & 2 deletions src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,6 @@ Canvas::resurface(Local<Object> canvas) {
Nan::HandleScope scope;
Local<Value> context;

backend()->recreateSurface();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind explaining this change? I'm not to familiar with this part of the code 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the exact details, but reviewing the PR, seems to me like a bug, because recreateSurface() it's not being called anywhere else...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe best to add it back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, or test it first as is, to be sure if it crash or fails.


// Reset context
context = Nan::Get(canvas, Nan::New<String>("context").ToLocalChecked()).ToLocalChecked();
if (!context->IsUndefined()) {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/Backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ void Backend::setCanvas(Canvas* _canvas)
}


cairo_surface_t* Backend::recreateSurface()
void Backend::recreateSurface()
{
this->destroySurface();

return this->createSurface();
this->createSurface();
}

DLL_PUBLIC cairo_surface_t* Backend::getSurface() {
Expand Down
9 changes: 5 additions & 4 deletions src/backend/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Backend : public Nan::ObjectWrap
Canvas* canvas = nullptr;

Backend(std::string name, int width, int height);

virtual void createSurface() = 0;
virtual void destroySurface();
virtual void recreateSurface();

static void init(const Nan::FunctionCallbackInfo<v8::Value> &info);
static Backend *construct(int width, int height){ return nullptr; }

Expand All @@ -30,11 +35,7 @@ class Backend : public Nan::ObjectWrap

void setCanvas(Canvas* canvas);

virtual cairo_surface_t* createSurface() = 0;
virtual cairo_surface_t* recreateSurface();

DLL_PUBLIC cairo_surface_t* getSurface();
virtual void destroySurface();

DLL_PUBLIC std::string getName();

Expand Down
21 changes: 13 additions & 8 deletions src/backend/ImageBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ int32_t ImageBackend::approxBytesPerPixel() {
}
}

cairo_surface_t* ImageBackend::createSurface() {
void ImageBackend::createSurface()
{
assert(!surface);
surface = cairo_image_surface_create(format, width, height);
assert(surface);
Nan::AdjustExternalMemory(approxBytesPerPixel() * width * height);
return surface;
}

void ImageBackend::destroySurface() {
if (surface) {
cairo_surface_destroy(surface);
surface = nullptr;
Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
}
void ImageBackend::recreateSurface()
{
// Re-surface
if (this->surface) {
int old_width = cairo_image_surface_get_width(this->surface);
int old_height = cairo_image_surface_get_height(this->surface);
this->destroySurface();
Nan::AdjustExternalMemory(-approxBytesPerPixel() * old_width * old_height);
}

createSurface();
}

cairo_format_t ImageBackend::getFormat() {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/ImageBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class ImageBackend : public Backend
{
private:
cairo_surface_t* createSurface();
void destroySurface();
void createSurface();
void recreateSurface();
cairo_format_t format = DEFAULT_FORMAT;

public:
Expand Down
8 changes: 3 additions & 5 deletions src/backend/PdfBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ Backend *PdfBackend::construct(int width, int height){
return new PdfBackend(width, height);
}

cairo_surface_t* PdfBackend::createSurface() {
void PdfBackend::createSurface() {
if (!_closure) _closure = new PdfSvgClosure(canvas);

surface = cairo_pdf_surface_create_for_stream(PdfSvgClosure::writeVec, _closure, width, height);
return surface;
}

cairo_surface_t* PdfBackend::recreateSurface() {
void PdfBackend::recreateSurface() {
cairo_pdf_surface_set_size(surface, width, height);

return surface;
}


Expand Down
4 changes: 2 additions & 2 deletions src/backend/PdfBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class PdfBackend : public Backend
{
private:
cairo_surface_t* createSurface();
cairo_surface_t* recreateSurface();
void createSurface();
void recreateSurface();

public:
PdfSvgClosure* _closure = NULL;
Expand Down
11 changes: 5 additions & 6 deletions src/backend/SvgBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ Backend *SvgBackend::construct(int width, int height){
return new SvgBackend(width, height);
}

cairo_surface_t* SvgBackend::createSurface() {
assert(!_closure);
_closure = new PdfSvgClosure(canvas);
void SvgBackend::createSurface() {
if (!_closure) _closure = new PdfSvgClosure(canvas);

surface = cairo_svg_surface_create_for_stream(PdfSvgClosure::writeVec, _closure, width, height);
return surface;
}

cairo_surface_t* SvgBackend::recreateSurface() {
void SvgBackend::recreateSurface() {
cairo_surface_finish(surface);
delete _closure;
_closure = nullptr;
cairo_surface_destroy(surface);

return createSurface();
createSurface();
}


Expand Down
4 changes: 2 additions & 2 deletions src/backend/SvgBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class SvgBackend : public Backend
{
private:
cairo_surface_t* createSurface();
cairo_surface_t* recreateSurface();
void createSurface();
void recreateSurface();

public:
PdfSvgClosure* _closure = NULL;
Expand Down