2014-01-06 18:17:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
#include "Path2D.h"
|
2014-01-06 18:17:24 +00:00
|
|
|
#include "Global.h"
|
|
|
|
|
2014-02-27 20:20:21 +00:00
|
|
|
Global* Path2D::gGlobal = NULL;
|
2014-10-29 12:33:27 +00:00
|
|
|
v8::Persistent<v8::ObjectTemplate> Path2D::gPath2DTemplate;
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
void weakPath2DCallback(const v8::WeakCallbackData<v8::Object, Path2D>& args) {
|
|
|
|
delete args.GetParameter();
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Wraps an SkPath* in a Path2D object.
|
|
|
|
Path2D::Path2D(SkPath* path) : path_(path) {
|
|
|
|
// Handle scope for temporary handles.
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::HandleScope handleScope(gGlobal->getIsolate());
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Just once create the ObjectTemplate for what Path2D looks like in JS.
|
|
|
|
if (gPath2DTemplate.IsEmpty()) {
|
|
|
|
v8::Local<v8::ObjectTemplate> localTemplate = v8::ObjectTemplate::New();
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Add a field to store the pointer to a SkPath pointer.
|
|
|
|
localTemplate->SetInternalFieldCount(1);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
gPath2DTemplate.Reset(gGlobal->getIsolate(), localTemplate);
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
2014-10-29 12:33:27 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> templ =
|
|
|
|
v8::Local<v8::ObjectTemplate>::New(gGlobal->getIsolate(), gPath2DTemplate);
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Create an empty Path2D wrapper.
|
|
|
|
v8::Local<v8::Object> result = templ->NewInstance();
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Store the SkPath pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, v8::External::New(gGlobal->getIsolate(), this));
|
|
|
|
gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(SkPath));
|
2014-01-06 18:17:24 +00:00
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
// Make a weak persistent and set up the callback so we can delete the path pointer.
|
|
|
|
// TODO(jcgregorio) Figure out why weakPath2DCallback never gets called and we leak.
|
|
|
|
v8::Persistent<v8::Object> weak(gGlobal->getIsolate(), result);
|
|
|
|
weak.SetWeak(this, weakPath2DCallback);
|
|
|
|
this->handle_.Reset(gGlobal->getIsolate(), weak);
|
2014-01-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
Path2D::~Path2D() {
|
|
|
|
delete path_;
|
|
|
|
handle_.Reset();
|
|
|
|
gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(-sizeof(SkPath));
|
2014-02-13 16:00:58 +00:00
|
|
|
}
|