blob: dded6881671fa5915e52d7c8d0b0cc7cff52f7bf [file] [log] [blame]
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000010#include "Path2D.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000011#include "Global.h"
12
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000013Global* Path2D::gGlobal = NULL;
jcgregorioe001da22014-10-29 05:33:27 -070014v8::Persistent<v8::ObjectTemplate> Path2D::gPath2DTemplate;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000015
jcgregorioe001da22014-10-29 05:33:27 -070016void weakPath2DCallback(const v8::WeakCallbackData<v8::Object, Path2D>& args) {
17 delete args.GetParameter();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000018}
19
jcgregorioe001da22014-10-29 05:33:27 -070020// Wraps an SkPath* in a Path2D object.
21Path2D::Path2D(SkPath* path) : path_(path) {
22 // Handle scope for temporary handles.
jcgregorioe22f45f2014-10-24 12:49:17 -070023 v8::HandleScope handleScope(gGlobal->getIsolate());
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000024
jcgregorioe001da22014-10-29 05:33:27 -070025 // Just once create the ObjectTemplate for what Path2D looks like in JS.
26 if (gPath2DTemplate.IsEmpty()) {
27 v8::Local<v8::ObjectTemplate> localTemplate = v8::ObjectTemplate::New();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000028
jcgregorioe001da22014-10-29 05:33:27 -070029 // Add a field to store the pointer to a SkPath pointer.
30 localTemplate->SetInternalFieldCount(1);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000031
jcgregorioe001da22014-10-29 05:33:27 -070032 gPath2DTemplate.Reset(gGlobal->getIsolate(), localTemplate);
33 }
34 v8::Handle<v8::ObjectTemplate> templ =
35 v8::Local<v8::ObjectTemplate>::New(gGlobal->getIsolate(), gPath2DTemplate);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000036
jcgregorioe001da22014-10-29 05:33:27 -070037 // Create an empty Path2D wrapper.
38 v8::Local<v8::Object> result = templ->NewInstance();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000039
jcgregorioe001da22014-10-29 05:33:27 -070040 // Store the SkPath pointer in the JavaScript wrapper.
41 result->SetInternalField(0, v8::External::New(gGlobal->getIsolate(), this));
42 gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(SkPath));
43
44 // Make a weak persistent and set up the callback so we can delete the path pointer.
45 // TODO(jcgregorio) Figure out why weakPath2DCallback never gets called and we leak.
46 v8::Persistent<v8::Object> weak(gGlobal->getIsolate(), result);
47 weak.SetWeak(this, weakPath2DCallback);
48 this->handle_.Reset(gGlobal->getIsolate(), weak);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000049}
50
jcgregorioe001da22014-10-29 05:33:27 -070051Path2D::~Path2D() {
52 delete path_;
53 handle_.Reset();
54 gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(-sizeof(SkPath));
commit-bot@chromium.orge1df5652014-02-13 16:00:58 +000055}