Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: experimental/SkV8Example/BaseContext.cpp

Issue 673223002: Get SkV8Example running again. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/SkV8Example/BaseContext.h ('k') | experimental/SkV8Example/Global.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 * 7 *
8 */ 8 */
9 #include <v8.h> 9 #include <v8.h>
10 10
11 using namespace v8;
12
13 #include "Global.h" 11 #include "Global.h"
14 #include "BaseContext.h" 12 #include "BaseContext.h"
15 #include "Path2D.h" 13 #include "Path2D.h"
16 #include "SkCanvas.h" 14 #include "SkCanvas.h"
17 15
18 16
19 BaseContext* BaseContext::Unwrap(Handle<Object> obj) { 17 BaseContext* BaseContext::Unwrap(v8::Handle<v8::Object> obj) {
20 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); 18 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInte rnalField(0));
21 void* ptr = field->Value(); 19 void* ptr = field->Value();
22 return static_cast<BaseContext*>(ptr); 20 return static_cast<BaseContext*>(ptr);
23 } 21 }
24 22
25 void BaseContext::FillRect(const v8::FunctionCallbackInfo<Value>& args) { 23 void BaseContext::FillRect(const v8::FunctionCallbackInfo<v8::Value>& args) {
26 BaseContext* BaseContext = Unwrap(args.This()); 24 BaseContext* BaseContext = Unwrap(args.This());
27 SkCanvas* canvas = BaseContext->getCanvas(); 25 SkCanvas* canvas = BaseContext->getCanvas();
28 if (NULL == canvas) { 26 if (NULL == canvas) {
29 return; 27 return;
30 } 28 }
31 29
32 if (args.Length() != 4) { 30 if (args.Length() != 4) {
33 args.GetIsolate()->ThrowException( 31 args.GetIsolate()->ThrowException(
34 v8::String::NewFromUtf8( 32 v8::String::NewFromUtf8(
35 args.GetIsolate(), "Error: 4 arguments required.")); 33 args.GetIsolate(), "Error: 4 arguments required."));
36 return; 34 return;
37 } 35 }
38 double x = args[0]->NumberValue(); 36 double x = args[0]->NumberValue();
39 double y = args[1]->NumberValue(); 37 double y = args[1]->NumberValue();
40 double w = args[2]->NumberValue(); 38 double w = args[2]->NumberValue();
41 double h = args[3]->NumberValue(); 39 double h = args[3]->NumberValue();
42 40
43 SkRect rect = { 41 SkRect rect = {
44 SkDoubleToScalar(x), 42 SkDoubleToScalar(x),
45 SkDoubleToScalar(y), 43 SkDoubleToScalar(y),
46 SkDoubleToScalar(x) + SkDoubleToScalar(w), 44 SkDoubleToScalar(x) + SkDoubleToScalar(w),
47 SkDoubleToScalar(y) + SkDoubleToScalar(h) 45 SkDoubleToScalar(y) + SkDoubleToScalar(h)
48 }; 46 };
49 canvas->drawRect(rect, BaseContext->fFillStyle); 47 canvas->drawRect(rect, BaseContext->fFillStyle);
50 } 48 }
51 49
52 void BaseContext::Save(const v8::FunctionCallbackInfo<Value>& args) { 50 void BaseContext::Save(const v8::FunctionCallbackInfo<v8::Value>& args) {
53 BaseContext* BaseContext = Unwrap(args.This()); 51 BaseContext* BaseContext = Unwrap(args.This());
54 SkCanvas* canvas = BaseContext->getCanvas(); 52 SkCanvas* canvas = BaseContext->getCanvas();
55 if (NULL == canvas) { 53 if (NULL == canvas) {
56 return; 54 return;
57 } 55 }
58 56
59 canvas->save(); 57 canvas->save();
60 } 58 }
61 59
62 void BaseContext::Restore(const v8::FunctionCallbackInfo<Value>& args) { 60 void BaseContext::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) {
63 BaseContext* BaseContext = Unwrap(args.This()); 61 BaseContext* BaseContext = Unwrap(args.This());
64 SkCanvas* canvas = BaseContext->getCanvas(); 62 SkCanvas* canvas = BaseContext->getCanvas();
65 if (NULL == canvas) { 63 if (NULL == canvas) {
66 return; 64 return;
67 } 65 }
68 66
69 canvas->restore(); 67 canvas->restore();
70 } 68 }
71 69
72 void BaseContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) { 70 void BaseContext::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) {
73 BaseContext* BaseContext = Unwrap(args.This()); 71 BaseContext* BaseContext = Unwrap(args.This());
74 SkCanvas* canvas = BaseContext->getCanvas(); 72 SkCanvas* canvas = BaseContext->getCanvas();
75 if (NULL == canvas) { 73 if (NULL == canvas) {
76 return; 74 return;
77 } 75 }
78 76
79 if (args.Length() != 1) { 77 if (args.Length() != 1) {
80 args.GetIsolate()->ThrowException( 78 args.GetIsolate()->ThrowException(
81 v8::String::NewFromUtf8( 79 v8::String::NewFromUtf8(
82 args.GetIsolate(), "Error: 1 arguments required.")); 80 args.GetIsolate(), "Error: 1 arguments required."));
83 return; 81 return;
84 } 82 }
85 double angle = args[0]->NumberValue(); 83 double angle = args[0]->NumberValue();
86 canvas->rotate(SkRadiansToDegrees(angle)); 84 canvas->rotate(SkRadiansToDegrees(angle));
87 } 85 }
88 86
89 void BaseContext::Translate(const v8::FunctionCallbackInfo<Value>& args) { 87 void BaseContext::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) {
90 BaseContext* BaseContext = Unwrap(args.This()); 88 BaseContext* BaseContext = Unwrap(args.This());
91 SkCanvas* canvas = BaseContext->getCanvas(); 89 SkCanvas* canvas = BaseContext->getCanvas();
92 if (NULL == canvas) { 90 if (NULL == canvas) {
93 return; 91 return;
94 } 92 }
95 93
96 if (args.Length() != 2) { 94 if (args.Length() != 2) {
97 args.GetIsolate()->ThrowException( 95 args.GetIsolate()->ThrowException(
98 v8::String::NewFromUtf8( 96 v8::String::NewFromUtf8(
99 args.GetIsolate(), "Error: 2 arguments required.")); 97 args.GetIsolate(), "Error: 2 arguments required."));
100 return; 98 return;
101 } 99 }
102 double dx = args[0]->NumberValue(); 100 double dx = args[0]->NumberValue();
103 double dy = args[1]->NumberValue(); 101 double dy = args[1]->NumberValue();
104 canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy)); 102 canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
105 } 103 }
106 104
107 void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<Value>& args) { 105 void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args ) {
108 BaseContext* BaseContext = Unwrap(args.This()); 106 BaseContext* BaseContext = Unwrap(args.This());
109 SkCanvas* canvas = BaseContext->getCanvas(); 107 SkCanvas* canvas = BaseContext->getCanvas();
110 if (NULL == canvas) { 108 if (NULL == canvas) {
111 return; 109 return;
112 } 110 }
113 111
114 canvas->resetMatrix(); 112 canvas->resetMatrix();
115 } 113 }
116 114
117 void BaseContext::Stroke(const v8::FunctionCallbackInfo<Value>& args) { 115 void BaseContext::Stroke(const v8::FunctionCallbackInfo<v8::Value>& args) {
118 BaseContext* BaseContext = Unwrap(args.This()); 116 BaseContext* BaseContext = Unwrap(args.This());
119 SkCanvas* canvas = BaseContext->getCanvas(); 117 SkCanvas* canvas = BaseContext->getCanvas();
120 if (NULL == canvas) { 118 if (NULL == canvas) {
121 return; 119 return;
122 } 120 }
123 121
124 if (args.Length() != 1) { 122 if (args.Length() != 1) {
125 args.GetIsolate()->ThrowException( 123 args.GetIsolate()->ThrowException(
126 v8::String::NewFromUtf8( 124 v8::String::NewFromUtf8(
127 args.GetIsolate(), "Error: 1 arguments required.")); 125 args.GetIsolate(), "Error: 1 arguments required."));
128 return; 126 return;
129 } 127 }
130 128
131 Handle<External> field = Handle<External>::Cast( 129 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
132 args[0]->ToObject()->GetInternalField(0)); 130 args[0]->ToObject()->GetInternalField(0));
133 void* ptr = field->Value(); 131 void* ptr = field->Value();
134 Path2D* path = static_cast<Path2D*>(ptr); 132 Path2D* path = static_cast<Path2D*>(ptr);
135 133
136 canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle); 134 canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle);
137 } 135 }
138 136
139 void BaseContext::Fill(const v8::FunctionCallbackInfo<Value>& args) { 137 void BaseContext::Fill(const v8::FunctionCallbackInfo<v8::Value>& args) {
140 BaseContext* BaseContext = Unwrap(args.This()); 138 BaseContext* BaseContext = Unwrap(args.This());
141 SkCanvas* canvas = BaseContext->getCanvas(); 139 SkCanvas* canvas = BaseContext->getCanvas();
142 if (NULL == canvas) { 140 if (NULL == canvas) {
143 return; 141 return;
144 } 142 }
145 143
146 if (args.Length() != 1) { 144 if (args.Length() != 1) {
147 args.GetIsolate()->ThrowException( 145 args.GetIsolate()->ThrowException(
148 v8::String::NewFromUtf8( 146 v8::String::NewFromUtf8(
149 args.GetIsolate(), "Error: 1 arguments required.")); 147 args.GetIsolate(), "Error: 1 arguments required."));
150 return; 148 return;
151 } 149 }
152 150
153 Handle<External> field = Handle<External>::Cast( 151 v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
154 args[0]->ToObject()->GetInternalField(0)); 152 args[0]->ToObject()->GetInternalField(0));
155 void* ptr = field->Value(); 153 void* ptr = field->Value();
156 Path2D* path = static_cast<Path2D*>(ptr); 154 Path2D* path = static_cast<Path2D*>(ptr);
157 155
158 canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle); 156 canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle);
159 } 157 }
160 158
161 void BaseContext::GetStyle(Local<String> name, 159 void BaseContext::GetStyle(v8::Local<v8::String> name,
162 const PropertyCallbackInfo<Value>& info, 160 const v8::PropertyCallbackInfo<v8::Value>& info,
163 const SkPaint& style) { 161 const SkPaint& style) {
164 char buf[8]; 162 char buf[8];
165 SkColor color = style.getColor(); 163 SkColor color = style.getColor();
166 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), 164 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
167 SkColorGetB(color)); 165 SkColorGetB(color));
168 166
169 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf)); 167 info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), buf));
170 } 168 }
171 169
172 void BaseContext::SetStyle(Local<String> name, Local<Value> value, 170 void BaseContext::SetStyle(v8::Local<v8::String> name, v8::Local<v8::Value> valu e,
173 const PropertyCallbackInfo<void>& info, 171 const v8::PropertyCallbackInfo<void>& info,
174 SkPaint& style) { 172 SkPaint& style) {
175 Local<String> s = value->ToString(); 173 v8::Local<v8::String> s = value->ToString();
176 if (s->Length() != 7 && s->Length() != 9) { 174 if (s->Length() != 7 && s->Length() != 9) {
177 info.GetIsolate()->ThrowException( 175 info.GetIsolate()->ThrowException(
178 v8::String::NewFromUtf8( 176 v8::String::NewFromUtf8(
179 info.GetIsolate(), 177 info.GetIsolate(),
180 "Invalid fill style format length.")); 178 "Invalid fill style format length."));
181 return; 179 return;
182 } 180 }
183 char buf[10]; 181 char buf[10];
184 s->WriteUtf8(buf, sizeof(buf)); 182 s->WriteUtf8(buf, sizeof(buf));
185 183
186 if (buf[0] != '#') { 184 if (buf[0] != '#') {
187 info.GetIsolate()->ThrowException( 185 info.GetIsolate()->ThrowException(
188 v8::String::NewFromUtf8( 186 v8::String::NewFromUtf8(
189 info.GetIsolate(), "Invalid fill style format.")); 187 info.GetIsolate(), "Invalid fill style format."));
190 return; 188 return;
191 } 189 }
192 190
193 // Colors can be RRGGBBAA, but SkColor uses ARGB. 191 // Colors can be RRGGBBAA, but SkColor uses ARGB.
194 long color = strtol(buf+1, NULL, 16); 192 long color = strtol(buf+1, NULL, 16);
195 uint32_t alpha = SK_AlphaOPAQUE; 193 uint32_t alpha = SK_AlphaOPAQUE;
196 if (s->Length() == 9) { 194 if (s->Length() == 9) {
197 alpha = color & 0xFF; 195 alpha = color & 0xFF;
198 color >>= 8; 196 color >>= 8;
199 } 197 }
200 style.setColor(SkColorSetA(SkColor(color), alpha)); 198 style.setColor(SkColorSetA(SkColor(color), alpha));
201 } 199 }
202 200
203 void BaseContext::GetFillStyle(Local<String> name, 201 void BaseContext::GetFillStyle(v8::Local<v8::String> name,
204 const PropertyCallbackInfo<Value>& info) { 202 const v8::PropertyCallbackInfo<v8::Value>& info) {
205 BaseContext* baseContext = Unwrap(info.This()); 203 BaseContext* baseContext = Unwrap(info.This());
206 GetStyle(name, info, baseContext->fFillStyle); 204 GetStyle(name, info, baseContext->fFillStyle);
207 } 205 }
208 206
209 void BaseContext::GetStrokeStyle(Local<String> name, 207 void BaseContext::GetStrokeStyle(v8::Local<v8::String> name,
210 const PropertyCallbackInfo<Value>& info) { 208 const v8::PropertyCallbackInfo<v8::Value>& info) {
211 BaseContext* baseContext = Unwrap(info.This()); 209 BaseContext* baseContext = Unwrap(info.This());
212 GetStyle(name, info, baseContext->fStrokeStyle); 210 GetStyle(name, info, baseContext->fStrokeStyle);
213 } 211 }
214 212
215 void BaseContext::SetFillStyle(Local<String> name, Local<Value> value, 213 void BaseContext::SetFillStyle(v8::Local<v8::String> name, v8::Local<v8::Value> value,
216 const PropertyCallbackInfo<void>& info) { 214 const v8::PropertyCallbackInfo<void>& info) {
217 BaseContext* baseContext = Unwrap(info.This()); 215 BaseContext* baseContext = Unwrap(info.This());
218 SetStyle(name, value, info, baseContext->fFillStyle); 216 SetStyle(name, value, info, baseContext->fFillStyle);
219 } 217 }
220 218
221 void BaseContext::SetStrokeStyle(Local<String> name, Local<Value> value, 219 void BaseContext::SetStrokeStyle(v8::Local<v8::String> name, v8::Local<v8::Value > value,
222 const PropertyCallbackInfo<void>& info) { 220 const v8::PropertyCallbackInfo<void>& info) {
223 BaseContext* baseContext = Unwrap(info.This()); 221 BaseContext* baseContext = Unwrap(info.This());
224 SetStyle(name, value, info, baseContext->fStrokeStyle); 222 SetStyle(name, value, info, baseContext->fStrokeStyle);
225 } 223 }
226 224
227 225
228 void BaseContext::GetWidth(Local<String> name, 226 void BaseContext::GetWidth(v8::Local<v8::String> name,
229 const PropertyCallbackInfo<Value>& info) { 227 const v8::PropertyCallbackInfo<v8::Value>& info) {
230 BaseContext* baseContext = Unwrap(info.This()); 228 BaseContext* baseContext = Unwrap(info.This());
231 SkCanvas* canvas = baseContext->getCanvas(); 229 SkCanvas* canvas = baseContext->getCanvas();
232 if (NULL == canvas) { 230 if (NULL == canvas) {
233 return; 231 return;
234 } 232 }
235 SkISize size = canvas->getDeviceSize(); 233 SkISize size = canvas->getDeviceSize();
236 234
237 info.GetReturnValue().Set( 235 info.GetReturnValue().Set(
238 Int32::New(baseContext->fGlobal->getIsolate(), size.fWidth)); 236 v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fWidth));
239 } 237 }
240 238
241 void BaseContext::GetHeight(Local<String> name, 239 void BaseContext::GetHeight(v8::Local<v8::String> name,
242 const PropertyCallbackInfo<Value>& info) { 240 const v8::PropertyCallbackInfo<v8::Value>& info) {
243 BaseContext* baseContext = Unwrap(info.This()); 241 BaseContext* baseContext = Unwrap(info.This());
244 SkCanvas* canvas = baseContext->getCanvas(); 242 SkCanvas* canvas = baseContext->getCanvas();
245 if (NULL == canvas) { 243 if (NULL == canvas) {
246 return; 244 return;
247 } 245 }
248 SkISize size = canvas->getDeviceSize(); 246 SkISize size = canvas->getDeviceSize();
249 247
250 info.GetReturnValue().Set( 248 info.GetReturnValue().Set(
251 Int32::New(baseContext->fGlobal->getIsolate(), size.fHeight)); 249 v8::Int32::New(baseContext->fGlobal->getIsolate(), size.fHeight));
252 } 250 }
253 251
254 #define ADD_METHOD(name, fn) \ 252 #define ADD_METHOD(name, fn) \
255 tmpl->Set(String::NewFromUtf8( \ 253 tmpl->Set(v8::String::NewFromUtf8( \
256 fGlobal->getIsolate(), name, \ 254 fGlobal->getIsolate(), name, \
257 String::kInternalizedString), \ 255 v8::String::kInternalizedString), \
258 FunctionTemplate::New(fGlobal->getIsolate(), fn)) 256 v8::FunctionTemplate::New(fGlobal->getIsolate(), fn))
259 257
260 void BaseContext::addAttributesAndMethods(Handle<ObjectTemplate> tmpl) { 258 void BaseContext::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) {
261 HandleScope scope(fGlobal->getIsolate()); 259 v8::HandleScope scope(fGlobal->getIsolate());
262 260
263 // Add accessors for each of the fields of the context object. 261 // Add accessors for each of the fields of the context object.
264 tmpl->SetAccessor(String::NewFromUtf8( 262 tmpl->SetAccessor(v8::String::NewFromUtf8(
265 fGlobal->getIsolate(), "fillStyle", String::kInternalizedString), 263 fGlobal->getIsolate(), "fillStyle", v8::String::kInternalizedString),
266 GetFillStyle, SetFillStyle); 264 GetFillStyle, SetFillStyle);
267 tmpl->SetAccessor(String::NewFromUtf8( 265 tmpl->SetAccessor(v8::String::NewFromUtf8(
268 fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString), 266 fGlobal->getIsolate(), "strokeStyle", v8::String::kInternalizedString),
269 GetStrokeStyle, SetStrokeStyle); 267 GetStrokeStyle, SetStrokeStyle);
270 tmpl->SetAccessor(String::NewFromUtf8( 268 tmpl->SetAccessor(v8::String::NewFromUtf8(
271 fGlobal->getIsolate(), "width", String::kInternalizedString), 269 fGlobal->getIsolate(), "width", v8::String::kInternalizedString),
272 GetWidth); 270 GetWidth);
273 tmpl->SetAccessor(String::NewFromUtf8( 271 tmpl->SetAccessor(v8::String::NewFromUtf8(
274 fGlobal->getIsolate(), "height", String::kInternalizedString), 272 fGlobal->getIsolate(), "height", v8::String::kInternalizedString),
275 GetHeight); 273 GetHeight);
276 274
277 // Add methods. 275 // Add methods.
278 ADD_METHOD("fillRect", FillRect); 276 ADD_METHOD("fillRect", FillRect);
279 ADD_METHOD("stroke", Stroke); 277 ADD_METHOD("stroke", Stroke);
280 ADD_METHOD("fill", Fill); 278 ADD_METHOD("fill", Fill);
281 ADD_METHOD("rotate", Rotate); 279 ADD_METHOD("rotate", Rotate);
282 ADD_METHOD("save", Save); 280 ADD_METHOD("save", Save);
283 ADD_METHOD("restore", Restore); 281 ADD_METHOD("restore", Restore);
284 ADD_METHOD("translate", Translate); 282 ADD_METHOD("translate", Translate);
285 ADD_METHOD("resetTransform", ResetTransform); 283 ADD_METHOD("resetTransform", ResetTransform);
286 } 284 }
OLDNEW
« no previous file with comments | « experimental/SkV8Example/BaseContext.h ('k') | experimental/SkV8Example/Global.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698