1 /*
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "new_js_drawable_descriptor.h"
17
18 #include <memory>
19
20 #ifndef PREVIEW
21 #include "pixel_map_napi.h"
22 #else
23 #include "image_source_preview.h"
24 #endif
25 #include "resource_manager.h"
26
27 #include "base/log.h"
28 #include "base/resource.h"
29 #include "base/source_info.h"
30 #include "core/drawable_descriptor_base.h"
31 #include "core/pixelmap_drawable_descriptor.h"
32 #include "utils/napi_utils.h"
33
34 namespace OHOS {
35 namespace Ace {
36 namespace Napi {
37 namespace {
38 constexpr char DRAWABLE_DESCRIPTOR_NAME[] = "DrawableDescriptor";
39 constexpr char LAYERED_DRAWABLE_DESCRIPTOR_NAME[] = "LayeredDrawableDescriptor";
40 constexpr char ANIMATED_DRAWABLE_DESCRIPTOR_NAME[] = "AnimatedDrawableDescriptor";
41 constexpr char PIXELMAP_DRAWABLE_DESCRIPTOR_NAME[] = "PixelMapDrawableDescriptor";
42 constexpr size_t ESTIMATE_DRAWABLE_SIZE = 10 * 1024 * 1024;
43 constexpr int32_t PARAMS_NUM_TWO = 2;
44 constexpr int32_t PARAMS_NUM_THREE = 3;
45 constexpr int32_t FOREGROUND_INDEX = 0;
46 constexpr int32_t BACKGROUND_INDEX = 1;
47 constexpr int32_t MASK_INDEX = 2;
48
UpdateLayeredParam(LayeredDrawableDescriptor * layeredDrawable,int32_t pos,std::shared_ptr<OHOS::Media::PixelMap> pixelMap)49 void UpdateLayeredParam(
50 LayeredDrawableDescriptor* layeredDrawable, int32_t pos, std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
51 {
52 if (!layeredDrawable || !pixelMap) {
53 return;
54 }
55 switch (pos) {
56 case FOREGROUND_INDEX:
57 layeredDrawable->SetForeground(pixelMap);
58 break;
59 case BACKGROUND_INDEX:
60 layeredDrawable->SetBackground(pixelMap);
61 break;
62 case MASK_INDEX:
63 layeredDrawable->SetMask(pixelMap);
64 break;
65 default:
66 HILOGW("Arg[%{public}d] index error", pos);
67 }
68 }
69
GetPixelMapArray(napi_env env,napi_value arg,std::vector<std::shared_ptr<Media::PixelMap>> & pixelMaps)70 bool GetPixelMapArray(napi_env env, napi_value arg, std::vector<std::shared_ptr<Media::PixelMap>>& pixelMaps)
71 {
72 bool isArray = false;
73 uint32_t length = 0;
74 napi_is_array(env, arg, &isArray);
75 if (!isArray) {
76 return false;
77 }
78 napi_get_array_length(env, arg, &length);
79 if (length < 1) {
80 return false;
81 }
82 for (uint32_t i = 0; i < length; i++) {
83 napi_value pixelMapValue = nullptr;
84 napi_get_element(env, arg, i, &pixelMapValue);
85 if (pixelMapValue == nullptr) {
86 continue;
87 }
88 OHOS::Media::PixelMapNapi* pixmapNapi = nullptr;
89 napi_unwrap(env, pixelMapValue, reinterpret_cast<void**>(&pixmapNapi));
90 if (pixmapNapi == nullptr) {
91 continue;
92 }
93 auto* pixelmap = pixmapNapi->GetPixelMap();
94 if (pixelmap == nullptr) {
95 continue;
96 }
97 pixelMaps.push_back(*(pixelmap));
98 }
99 return !pixelMaps.empty();
100 }
101
GetPixelMapFromJsPixelMap(napi_env env,napi_value value)102 std::shared_ptr<Media::PixelMap> GetPixelMapFromJsPixelMap(napi_env env, napi_value value)
103 {
104 OHOS::Media::PixelMapNapi* pixelMapNapi = nullptr;
105 napi_unwrap(env, value, reinterpret_cast<void**>(&pixelMapNapi));
106 if (pixelMapNapi == nullptr) {
107 HILOGW("Failed to unwrap napi pixel map from js pixel map");
108 return nullptr;
109 }
110 auto pixelMap = *(pixelMapNapi->GetPixelMap());
111 if (pixelMap == nullptr) {
112 HILOGW("Native pixel map is nullptr");
113 return nullptr;
114 }
115 return pixelMap;
116 }
117
GetPixelMapFromJsDrawable(napi_env env,napi_value value)118 std::shared_ptr<Media::PixelMap> GetPixelMapFromJsDrawable(napi_env env, napi_value value)
119 {
120 OHOS::Ace::Drawable::DrawableDescriptor* drawable = nullptr;
121 napi_unwrap(env, value, reinterpret_cast<void**>(&drawable));
122 if (drawable == nullptr) {
123 HILOGW("Failed to unwrap native drawable from js drawable");
124 return nullptr;
125 }
126 auto pixelMap = drawable->GetPixelMap();
127 if (pixelMap == nullptr) {
128 HILOGW("Failed to get pixel map from drawable");
129 return nullptr;
130 }
131 return pixelMap;
132 }
133 } // namespace
134
135 thread_local napi_ref JsDrawableDescriptor::baseConstructor_;
136 thread_local napi_ref JsDrawableDescriptor::layeredConstructor_;
137 thread_local napi_ref JsDrawableDescriptor::animatedConstructor_;
138 thread_local napi_ref JsDrawableDescriptor::pixelMapConstructor_;
139
ToNapi(napi_env env,DrawableDescriptor * drawable,DrawableDescriptor::DrawableType type)140 napi_value JsDrawableDescriptor::ToNapi(
141 napi_env env, DrawableDescriptor* drawable, DrawableDescriptor::DrawableType type)
142 {
143 if (!drawable) {
144 return nullptr;
145 }
146 if (!baseConstructor_ || !layeredConstructor_ || !animatedConstructor_ || !pixelMapConstructor_) {
147 // init js class constructor by importing module manually
148 napi_value globalValue;
149 napi_get_global(env, &globalValue);
150 napi_value func;
151 napi_get_named_property(env, globalValue, "requireNapi", &func);
152
153 napi_value module;
154 napi_create_string_utf8(env, MODULE_NAME, NAPI_AUTO_LENGTH, &module);
155 napi_value returnValue;
156 napi_call_function(env, globalValue, func, 1, &module, &returnValue);
157 }
158
159 napi_value constructor = nullptr;
160 napi_value result = nullptr;
161 napi_status status;
162 switch (type) {
163 case DrawableDescriptor::DrawableType::ANIMATED:
164 status = napi_get_reference_value(env, animatedConstructor_, &constructor);
165 break;
166 case DrawableDescriptor::DrawableType::LAYERED:
167 status = napi_get_reference_value(env, layeredConstructor_, &constructor);
168 break;
169 case DrawableDescriptor::DrawableType::BASE:
170 status = napi_get_reference_value(env, baseConstructor_, &constructor);
171 break;
172 case DrawableDescriptor::DrawableType::PIXELMAP:
173 status = napi_get_reference_value(env, pixelMapConstructor_, &constructor);
174 break;
175 default:
176 status = napi_status::napi_invalid_arg;
177 break;
178 }
179 if (status == napi_status::napi_ok) {
180 NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
181 NAPI_CALL(env, napi_wrap(env, result, drawable, Destructor, nullptr, nullptr));
182 } else {
183 HILOGI("create reference failed, drawable constructor is null");
184 }
185
186 return result;
187 }
188
Export(napi_env env,napi_value exports)189 napi_value JsDrawableDescriptor::Export(napi_env env, napi_value exports)
190 {
191 // export base class
192 napi_value cons = InitDrawable(env);
193 NAPI_CALL(env, napi_set_named_property(env, exports, DRAWABLE_DESCRIPTOR_NAME, cons));
194 cons = InitPixelMapDrawable(env);
195 NAPI_CALL(env, napi_set_named_property(env, exports, PIXELMAP_DRAWABLE_DESCRIPTOR_NAME, cons));
196 // export child class
197 cons = InitLayeredDrawable(env);
198 NAPI_CALL(env, napi_set_named_property(env, exports, LAYERED_DRAWABLE_DESCRIPTOR_NAME, cons));
199 // export child class
200 cons = InitAnimatedDrawable(env);
201 NAPI_CALL(env, napi_set_named_property(env, exports, ANIMATED_DRAWABLE_DESCRIPTOR_NAME, cons));
202 return exports;
203 }
204
GetForeground(napi_env env,napi_callback_info info)205 napi_value JsDrawableDescriptor::GetForeground(napi_env env, napi_callback_info info)
206 {
207 napi_escapable_handle_scope scope = nullptr;
208 napi_open_escapable_handle_scope(env, &scope);
209 napi_value thisVar = nullptr;
210 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
211 void* native = nullptr;
212 napi_unwrap(env, thisVar, &native);
213 auto* drawable = reinterpret_cast<LayeredDrawableDescriptor*>(native);
214 if (!drawable) {
215 return nullptr;
216 }
217
218 auto foreground = drawable->GetForeground();
219 napi_value result = ToNapi(env, foreground.release(), DrawableDescriptor::DrawableType::BASE);
220 napi_escape_handle(env, scope, result, &result);
221 napi_close_escapable_handle_scope(env, scope);
222 return result;
223 }
224
GetBackground(napi_env env,napi_callback_info info)225 napi_value JsDrawableDescriptor::GetBackground(napi_env env, napi_callback_info info)
226 {
227 napi_escapable_handle_scope scope = nullptr;
228 napi_open_escapable_handle_scope(env, &scope);
229 napi_value thisVar = nullptr;
230 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
231 void* native = nullptr;
232 napi_unwrap(env, thisVar, &native);
233 auto* drawable = reinterpret_cast<LayeredDrawableDescriptor*>(native);
234 if (!drawable) {
235 return nullptr;
236 }
237
238 auto background = drawable->GetBackground();
239 napi_value result = ToNapi(env, background.release(), DrawableDescriptor::DrawableType::BASE);
240 napi_escape_handle(env, scope, result, &result);
241 napi_close_escapable_handle_scope(env, scope);
242 return result;
243 }
244
GetMask(napi_env env,napi_callback_info info)245 napi_value JsDrawableDescriptor::GetMask(napi_env env, napi_callback_info info)
246 {
247 napi_escapable_handle_scope scope = nullptr;
248 napi_open_escapable_handle_scope(env, &scope);
249 napi_value thisVar = nullptr;
250 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
251 void* native = nullptr;
252 napi_unwrap(env, thisVar, &native);
253 auto* drawable = reinterpret_cast<LayeredDrawableDescriptor*>(native);
254 if (!drawable) {
255 return nullptr;
256 }
257
258 auto mask = drawable->GetMask();
259 napi_value result = ToNapi(env, mask.release(), DrawableDescriptor::DrawableType::BASE);
260 napi_escape_handle(env, scope, result, &result);
261 napi_close_escapable_handle_scope(env, scope);
262 return result;
263 }
264
GetMaskClipPath(napi_env env,napi_callback_info info)265 napi_value JsDrawableDescriptor::GetMaskClipPath(napi_env env, napi_callback_info info)
266 {
267 auto path = OHOS::Ace::Napi::LayeredDrawableDescriptor::GetStaticMaskClipPath();
268 napi_value result = nullptr;
269 if (napi_ok != napi_create_string_utf8(env, path.c_str(), NAPI_AUTO_LENGTH, &result)) {
270 HILOGI("JsDrawableDescriptor Failed");
271 }
272 return result;
273 }
274
GetPixelMap(napi_env env,napi_callback_info info)275 napi_value JsDrawableDescriptor::GetPixelMap(napi_env env, napi_callback_info info)
276 {
277 napi_escapable_handle_scope scope = nullptr;
278 napi_open_escapable_handle_scope(env, &scope);
279 napi_value thisVar = nullptr;
280 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
281 void* native = nullptr;
282 napi_unwrap(env, thisVar, &native);
283 auto* drawable = reinterpret_cast<DrawableDescriptor*>(native);
284 if (!drawable) {
285 napi_close_escapable_handle_scope(env, scope);
286 return nullptr;
287 }
288 auto pixmap = drawable->GetPixelMap();
289
290 napi_value result = Media::PixelMapNapi::CreatePixelMap(env, pixmap);
291 napi_escape_handle(env, scope, result, &result);
292 napi_close_escapable_handle_scope(env, scope);
293 return result;
294 }
295
GetOriginalWidth(napi_env env,napi_callback_info info)296 napi_value JsDrawableDescriptor::GetOriginalWidth(napi_env env, napi_callback_info info)
297 {
298 return nullptr;
299 }
300
GetOriginalHeight(napi_env env,napi_callback_info info)301 napi_value JsDrawableDescriptor::GetOriginalHeight(napi_env env, napi_callback_info info)
302 {
303 return nullptr;
304 }
305
Fetch(napi_env env,napi_callback_info info)306 napi_value JsDrawableDescriptor::Fetch(napi_env env, napi_callback_info info)
307 {
308 napi_escapable_handle_scope scope = nullptr;
309 napi_open_escapable_handle_scope(env, &scope);
310 napi_value thisVar = nullptr;
311 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
312 void* native = nullptr;
313 napi_unwrap(env, thisVar, &native);
314 auto* drawable = reinterpret_cast<Drawable::DrawableDescriptor*>(native);
315 if (drawable) {
316 drawable->Fetch();
317 }
318 return nullptr;
319 }
320
FetchSync(napi_env env,napi_callback_info info)321 napi_value JsDrawableDescriptor::FetchSync(napi_env env, napi_callback_info info)
322 {
323 napi_escapable_handle_scope scope = nullptr;
324 napi_open_escapable_handle_scope(env, &scope);
325 napi_value thisVar = nullptr;
326 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
327 void* native = nullptr;
328 napi_unwrap(env, thisVar, &native);
329 auto* drawable = reinterpret_cast<Drawable::DrawableDescriptor*>(native);
330 if (drawable) {
331 drawable->FetchSync();
332 }
333 return nullptr;
334 }
335
IsRunning(napi_env env,napi_callback_info info)336 napi_value JsDrawableDescriptor::IsRunning(napi_env env, napi_callback_info info)
337 {
338 return nullptr;
339 }
340
Start(napi_env env,napi_callback_info info)341 napi_value JsDrawableDescriptor::Start(napi_env env, napi_callback_info info)
342 {
343 return nullptr;
344 }
345
Stop(napi_env env,napi_callback_info info)346 napi_value JsDrawableDescriptor::Stop(napi_env env, napi_callback_info info)
347 {
348 return nullptr;
349 }
350
Destructor(napi_env,void * nativeObject,void *)351 void JsDrawableDescriptor::Destructor(napi_env /* env */, void* nativeObject, void* /* finalize */)
352 {
353 auto* field = reinterpret_cast<Drawable::DrawableDescriptor*>(nativeObject);
354 delete field;
355 }
356
OldDestructor(napi_env,void * nativeObject,void *)357 void JsDrawableDescriptor::OldDestructor(napi_env /* env */, void* nativeObject, void* /* finalize */)
358 {
359 auto* field = reinterpret_cast<DrawableDescriptor*>(nativeObject);
360 delete field;
361 }
362
AnimatedConstructor(napi_env env,napi_callback_info info)363 napi_value JsDrawableDescriptor::AnimatedConstructor(napi_env env, napi_callback_info info)
364 {
365 napi_escapable_handle_scope scope = nullptr;
366 napi_open_escapable_handle_scope(env, &scope);
367
368 size_t argc = 2;
369 napi_value argv[2] = { nullptr };
370 napi_value thisVar = nullptr;
371 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
372 if (status != napi_ok) {
373 GET_AND_THROW_LAST_ERROR((env));
374 napi_close_escapable_handle_scope(env, scope);
375 return nullptr;
376 }
377 if (argc < 1) {
378 napi_close_escapable_handle_scope(env, scope);
379 return nullptr;
380 }
381 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
382 if (!GetPixelMapArray(env, argv[0], pixelMaps)) {
383 napi_close_escapable_handle_scope(env, scope);
384 return nullptr;
385 }
386
387 // napi_get_named_property
388 napi_value napiDuration;
389 napi_value napiIterations;
390 int32_t duration = -1;
391 int32_t iterations = 1;
392 if (argc > 1 && argv[1]) {
393 napi_get_named_property(env, argv[1], "duration", &napiDuration);
394 napi_get_named_property(env, argv[1], "iterations", &napiIterations);
395 napi_get_value_int32(env, napiDuration, &duration);
396 napi_get_value_int32(env, napiIterations, &iterations);
397 }
398 // create JsDrawable
399 auto* animatedDrawable = new AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
400 // wrap to napi_value
401 auto napi_status = napi_wrap(env, thisVar, animatedDrawable, OldDestructor, nullptr, nullptr);
402 if (napi_status != napi_ok) {
403 delete animatedDrawable;
404 napi_close_escapable_handle_scope(env, scope);
405 return nullptr;
406 }
407 napi_escape_handle(env, scope, thisVar, &thisVar);
408 napi_close_escapable_handle_scope(env, scope);
409 return thisVar;
410 }
411
LayeredConstructor(napi_env env,napi_callback_info info)412 napi_value JsDrawableDescriptor::LayeredConstructor(napi_env env, napi_callback_info info)
413 {
414 napi_escapable_handle_scope scope = nullptr;
415 napi_open_escapable_handle_scope(env, &scope);
416 size_t argc = PARAMS_NUM_THREE;
417 napi_value argv[argc];
418 napi_value thisVar = nullptr;
419 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
420 auto pos = -1;
421 auto* layeredDrawable = new LayeredDrawableDescriptor;
422 if (argc == 0) {
423 auto napi_status = napi_wrap(env, thisVar, layeredDrawable, OldDestructor, nullptr, nullptr);
424 if (napi_status != napi_ok) {
425 delete layeredDrawable;
426 return thisVar;
427 }
428 napi_escape_handle(env, scope, thisVar, &thisVar);
429 napi_close_escapable_handle_scope(env, scope);
430 return thisVar;
431 }
432 std::shared_ptr<Media::PixelMap> foregroundPixelMap = nullptr;
433 auto updateUndefinedPixelMap = [&pos, &layeredDrawable, &foregroundPixelMap]() -> void {
434 if (pos == MASK_INDEX) {
435 std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
436 layeredDrawable->InitialMask(resMgr);
437 } else if (pos == BACKGROUND_INDEX && foregroundPixelMap) {
438 UpdateLayeredParam(layeredDrawable, pos, foregroundPixelMap);
439 }
440 };
441 napi_valuetype type;
442 for (const auto& arg : argv) {
443 pos++;
444 napi_typeof(env, arg, &type);
445 if (type == napi_undefined) {
446 updateUndefinedPixelMap();
447 continue;
448 }
449 auto pixelMap = GetPixelMapFromJsDrawable(env, arg);
450 if (!pixelMap) {
451 updateUndefinedPixelMap();
452 } else {
453 UpdateLayeredParam(layeredDrawable, pos, pixelMap);
454 if (pos == FOREGROUND_INDEX)
455 foregroundPixelMap = std::move(pixelMap);
456 }
457 }
458 auto napi_status = napi_wrap(env, thisVar, layeredDrawable, OldDestructor, nullptr, nullptr);
459 if (napi_status != napi_ok) {
460 delete layeredDrawable;
461 return thisVar;
462 }
463 napi_escape_handle(env, scope, thisVar, &thisVar);
464 napi_close_escapable_handle_scope(env, scope);
465 return thisVar;
466 }
467
DrawableConstructor(napi_env env,napi_callback_info info)468 napi_value JsDrawableDescriptor::DrawableConstructor(napi_env env, napi_callback_info info)
469 {
470 napi_escapable_handle_scope scope = nullptr;
471 napi_open_escapable_handle_scope(env, &scope);
472 napi_value thisVar = nullptr;
473 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
474 // create JsDrawable
475 auto* drawable = new Drawable::DrawableDescriptor;
476 // wrap to napi_value
477 auto napi_status = napi_wrap(env, thisVar, drawable, Destructor, nullptr, nullptr);
478 if (napi_status != napi_ok) {
479 delete drawable;
480 return nullptr;
481 }
482 napi_escape_handle(env, scope, thisVar, &thisVar);
483 napi_close_escapable_handle_scope(env, scope);
484 return thisVar;
485 }
486
PixelMapConstructor(napi_env env,napi_callback_info info)487 napi_value JsDrawableDescriptor::PixelMapConstructor(napi_env env, napi_callback_info info)
488 {
489 napi_escapable_handle_scope scope = nullptr;
490 napi_open_escapable_handle_scope(env, &scope);
491 size_t argc = PARAMS_NUM_TWO;
492 napi_value argv[argc];
493 napi_value thisVar = nullptr;
494 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
495 auto* drawable = new Drawable::PixelmapDrawableDescriptor;
496 if (argc == 0) {
497 auto napi_status = napi_wrap(env, thisVar, drawable, Destructor, nullptr, nullptr);
498 if (napi_status != napi_ok) {
499 delete drawable;
500 return thisVar;
501 }
502 napi_escape_handle(env, scope, thisVar, &thisVar);
503 napi_close_escapable_handle_scope(env, scope);
504 return thisVar;
505 }
506
507 napi_value res = argv[0];
508 napi_valuetype type = Drawable::NapiUtils::ParseJsType(env, res);
509
510 Drawable::SourceInfo sourceInfo;
511 if (type == napi_string) {
512 auto src = Drawable::NapiUtils::ParseJsString(env, res);
513 sourceInfo.SetSrc(src);
514 }
515
516 if (type == napi_object) {
517 auto pixelMap = GetPixelMapFromJsPixelMap(env, res);
518 if (pixelMap) {
519 drawable->SetPixelMap(pixelMap);
520 sourceInfo.SetSrcType(Drawable::SrcType::PIXMAP);
521 } else {
522 auto resource = Drawable::NapiUtils::ParseJsResource(env, res);
523 sourceInfo.SetResource(resource);
524 sourceInfo.SetSrcType(Drawable::SrcType::RESOURCE);
525 }
526 }
527
528 drawable->SetSourceInfo(sourceInfo);
529 if (argc == 2) {
530 napi_value res = argv[1];
531 napi_valuetype type = Drawable::NapiUtils::ParseJsType(env, res);
532 if (type == napi_object) {
533 napi_value jsFetchSync = Drawable::NapiUtils::GetNamedProperty(env, res, "fetchWhenConstructingWithUri");
534 auto fetchSync = Drawable::NapiUtils::ParseJsBool(env, jsFetchSync);
535 if (fetchSync) {
536 drawable->FetchSync();
537 }
538 }
539 }
540 // wrap to napi_value
541 auto napi_status =
542 napi_wrap_with_size(env, thisVar, drawable, Destructor, nullptr, nullptr, ESTIMATE_DRAWABLE_SIZE);
543 if (napi_status != napi_ok) {
544 delete drawable;
545 return thisVar;
546 }
547 napi_escape_handle(env, scope, thisVar, &thisVar);
548 napi_close_escapable_handle_scope(env, scope);
549 return thisVar;
550 }
551
InitDrawable(napi_env env)552 napi_value JsDrawableDescriptor::InitDrawable(napi_env env)
553 {
554 napi_value cons = nullptr;
555 napi_value typeName = Drawable::NapiUtils::CreateString(env, "DrawableDescriptor");
556 napi_property_descriptor baseDes[] = {
557 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
558 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
559 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
560 DECLARE_NAPI_FUNCTION("fetch", Fetch),
561 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
562 DECLARE_NAPI_PROPERTY("typeName", typeName)
563 };
564 NAPI_CALL(env, napi_define_class(env, DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, DrawableConstructor, nullptr,
565 sizeof(baseDes) / sizeof(napi_property_descriptor), baseDes, &cons));
566 NAPI_CALL(env, napi_create_reference(env, cons, 1, &baseConstructor_));
567 return cons;
568 }
569
InitPixelMapDrawable(napi_env env)570 napi_value JsDrawableDescriptor::InitPixelMapDrawable(napi_env env)
571 {
572 napi_value cons = nullptr;
573 napi_value typeName = Drawable::NapiUtils::CreateString(env, "PixelMapDrawableDescriptor");
574 napi_property_descriptor pixelDes[] = {
575 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
576 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
577 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
578 DECLARE_NAPI_FUNCTION("fetch", Fetch),
579 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
580 DECLARE_NAPI_PROPERTY("typeName", typeName)
581 };
582 NAPI_CALL(env, napi_define_class(env, PIXELMAP_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, PixelMapConstructor,
583 nullptr, sizeof(pixelDes) / sizeof(napi_property_descriptor), pixelDes, &cons));
584 NAPI_CALL(env, napi_create_reference(env, cons, 1, &pixelMapConstructor_));
585 return cons;
586 }
587
InitLayeredDrawable(napi_env env)588 napi_value JsDrawableDescriptor::InitLayeredDrawable(napi_env env)
589 {
590 napi_value cons = nullptr;
591 napi_value typeName = Drawable::NapiUtils::CreateString(env, "LayeredDrawableDescriptor");
592 napi_property_descriptor layeredDes[] = {
593 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
594 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
595 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
596 DECLARE_NAPI_FUNCTION("fetch", Fetch),
597 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
598 DECLARE_NAPI_FUNCTION("getForeground", GetForeground),
599 DECLARE_NAPI_FUNCTION("getBackground", GetBackground),
600 DECLARE_NAPI_FUNCTION("getMask", GetMask),
601 DECLARE_NAPI_STATIC_FUNCTION("getMaskClipPath", GetMaskClipPath),
602 DECLARE_NAPI_PROPERTY("typeName", typeName)
603 };
604 NAPI_CALL(env, napi_define_class(env, LAYERED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, LayeredConstructor,
605 nullptr, sizeof(layeredDes) / sizeof(napi_property_descriptor), layeredDes, &cons));
606 NAPI_CALL(env, napi_create_reference(env, cons, 1, &layeredConstructor_));
607 return cons;
608 }
609
InitAnimatedDrawable(napi_env env)610 napi_value JsDrawableDescriptor::InitAnimatedDrawable(napi_env env)
611 {
612 napi_value cons = nullptr;
613 napi_value typeName = Drawable::NapiUtils::CreateString(env, "AnimatedDrawableDescriptor");
614 napi_property_descriptor animatedDes[] = {
615 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
616 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
617 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
618 DECLARE_NAPI_FUNCTION("fetch", Fetch),
619 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
620 DECLARE_NAPI_FUNCTION("isRunning", IsRunning),
621 DECLARE_NAPI_FUNCTION("start", Start),
622 DECLARE_NAPI_FUNCTION("stop", Stop),
623 DECLARE_NAPI_PROPERTY("typeName", typeName)
624 };
625 NAPI_CALL(env, napi_define_class(env, ANIMATED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, AnimatedConstructor,
626 nullptr, sizeof(animatedDes) / sizeof(napi_property_descriptor), animatedDes, &cons));
627 NAPI_CALL(env, napi_create_reference(env, cons, 1, &animatedConstructor_));
628 return cons;
629 }
630 } // namespace Napi
631 } // namespace Ace
632 } // namespace OHOS
633