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 auto pixmap = drawable->GetPixelMap();
285
286 napi_value result = Media::PixelMapNapi::CreatePixelMap(env, pixmap);
287 napi_escape_handle(env, scope, result, &result);
288 napi_close_escapable_handle_scope(env, scope);
289 return result;
290 }
291
GetOriginalWidth(napi_env env,napi_callback_info info)292 napi_value JsDrawableDescriptor::GetOriginalWidth(napi_env env, napi_callback_info info)
293 {
294 return nullptr;
295 }
296
GetOriginalHeight(napi_env env,napi_callback_info info)297 napi_value JsDrawableDescriptor::GetOriginalHeight(napi_env env, napi_callback_info info)
298 {
299 return nullptr;
300 }
301
Fetch(napi_env env,napi_callback_info info)302 napi_value JsDrawableDescriptor::Fetch(napi_env env, napi_callback_info info)
303 {
304 napi_escapable_handle_scope scope = nullptr;
305 napi_open_escapable_handle_scope(env, &scope);
306 napi_value thisVar = nullptr;
307 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
308 void* native = nullptr;
309 napi_unwrap(env, thisVar, &native);
310 auto* drawable = reinterpret_cast<Drawable::DrawableDescriptor*>(native);
311 if (drawable) {
312 drawable->Fetch();
313 }
314 return nullptr;
315 }
316
FetchSync(napi_env env,napi_callback_info info)317 napi_value JsDrawableDescriptor::FetchSync(napi_env env, napi_callback_info info)
318 {
319 napi_escapable_handle_scope scope = nullptr;
320 napi_open_escapable_handle_scope(env, &scope);
321 napi_value thisVar = nullptr;
322 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
323 void* native = nullptr;
324 napi_unwrap(env, thisVar, &native);
325 auto* drawable = reinterpret_cast<Drawable::DrawableDescriptor*>(native);
326 if (drawable) {
327 drawable->FetchSync();
328 }
329 return nullptr;
330 }
331
IsRunning(napi_env env,napi_callback_info info)332 napi_value JsDrawableDescriptor::IsRunning(napi_env env, napi_callback_info info)
333 {
334 return nullptr;
335 }
336
Start(napi_env env,napi_callback_info info)337 napi_value JsDrawableDescriptor::Start(napi_env env, napi_callback_info info)
338 {
339 return nullptr;
340 }
341
Stop(napi_env env,napi_callback_info info)342 napi_value JsDrawableDescriptor::Stop(napi_env env, napi_callback_info info)
343 {
344 return nullptr;
345 }
346
Destructor(napi_env,void * nativeObject,void *)347 void JsDrawableDescriptor::Destructor(napi_env /* env */, void* nativeObject, void* /* finalize */)
348 {
349 auto* field = reinterpret_cast<Drawable::DrawableDescriptor*>(nativeObject);
350 delete field;
351 }
352
OldDestructor(napi_env,void * nativeObject,void *)353 void JsDrawableDescriptor::OldDestructor(napi_env /* env */, void* nativeObject, void* /* finalize */)
354 {
355 auto* field = reinterpret_cast<DrawableDescriptor*>(nativeObject);
356 delete field;
357 }
358
AnimatedConstructor(napi_env env,napi_callback_info info)359 napi_value JsDrawableDescriptor::AnimatedConstructor(napi_env env, napi_callback_info info)
360 {
361 napi_escapable_handle_scope scope = nullptr;
362 napi_open_escapable_handle_scope(env, &scope);
363
364 size_t argc = 2;
365 napi_value argv[2] = { nullptr };
366 napi_value thisVar = nullptr;
367 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
368 if (argc < 1) {
369 return nullptr;
370 }
371 std::vector<std::shared_ptr<Media::PixelMap>> pixelMaps;
372 if (!GetPixelMapArray(env, argv[0], pixelMaps)) {
373 return nullptr;
374 }
375
376 // napi_get_named_property
377 napi_value napiDuration;
378 napi_value napiIterations;
379 int32_t duration = -1;
380 int32_t iterations = 1;
381 if (argc > 1 && argv[1]) {
382 napi_get_named_property(env, argv[1], "duration", &napiDuration);
383 napi_get_named_property(env, argv[1], "iterations", &napiIterations);
384 napi_get_value_int32(env, napiDuration, &duration);
385 napi_get_value_int32(env, napiIterations, &iterations);
386 }
387 // create JsDrawable
388 auto* animatedDrawable = new AnimatedDrawableDescriptor(pixelMaps, duration, iterations);
389 // wrap to napi_value
390 auto napi_status = napi_wrap(env, thisVar, animatedDrawable, OldDestructor, nullptr, nullptr);
391 if (napi_status != napi_ok) {
392 delete animatedDrawable;
393 return nullptr;
394 }
395 napi_escape_handle(env, scope, thisVar, &thisVar);
396 napi_close_escapable_handle_scope(env, scope);
397 return thisVar;
398 }
399
LayeredConstructor(napi_env env,napi_callback_info info)400 napi_value JsDrawableDescriptor::LayeredConstructor(napi_env env, napi_callback_info info)
401 {
402 napi_escapable_handle_scope scope = nullptr;
403 napi_open_escapable_handle_scope(env, &scope);
404 size_t argc = PARAMS_NUM_THREE;
405 napi_value argv[argc];
406 napi_value thisVar = nullptr;
407 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
408 auto pos = -1;
409 auto* layeredDrawable = new LayeredDrawableDescriptor;
410 if (argc == 0) {
411 auto napi_status = napi_wrap(env, thisVar, layeredDrawable, OldDestructor, nullptr, nullptr);
412 if (napi_status != napi_ok) {
413 delete layeredDrawable;
414 return thisVar;
415 }
416 napi_escape_handle(env, scope, thisVar, &thisVar);
417 napi_close_escapable_handle_scope(env, scope);
418 return thisVar;
419 }
420 std::shared_ptr<Media::PixelMap> foregroundPixelMap = nullptr;
421 auto updateUndefinedPixelMap = [&pos, &layeredDrawable, &foregroundPixelMap]() -> void {
422 if (pos == MASK_INDEX) {
423 std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
424 layeredDrawable->InitialMask(resMgr);
425 } else if (pos == BACKGROUND_INDEX && foregroundPixelMap) {
426 UpdateLayeredParam(layeredDrawable, pos, foregroundPixelMap);
427 }
428 };
429 napi_valuetype type;
430 for (const auto& arg : argv) {
431 pos++;
432 napi_typeof(env, arg, &type);
433 if (type == napi_undefined) {
434 updateUndefinedPixelMap();
435 continue;
436 }
437 auto pixelMap = GetPixelMapFromJsDrawable(env, arg);
438 if (!pixelMap) {
439 updateUndefinedPixelMap();
440 } else {
441 UpdateLayeredParam(layeredDrawable, pos, pixelMap);
442 if (pos == FOREGROUND_INDEX)
443 foregroundPixelMap = std::move(pixelMap);
444 }
445 }
446 auto napi_status = napi_wrap(env, thisVar, layeredDrawable, OldDestructor, nullptr, nullptr);
447 if (napi_status != napi_ok) {
448 delete layeredDrawable;
449 return thisVar;
450 }
451 napi_escape_handle(env, scope, thisVar, &thisVar);
452 napi_close_escapable_handle_scope(env, scope);
453 return thisVar;
454 }
455
DrawableConstructor(napi_env env,napi_callback_info info)456 napi_value JsDrawableDescriptor::DrawableConstructor(napi_env env, napi_callback_info info)
457 {
458 napi_escapable_handle_scope scope = nullptr;
459 napi_open_escapable_handle_scope(env, &scope);
460 napi_value thisVar = nullptr;
461 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
462 // create JsDrawable
463 auto* drawable = new Drawable::DrawableDescriptor;
464 // wrap to napi_value
465 auto napi_status = napi_wrap(env, thisVar, drawable, Destructor, nullptr, nullptr);
466 if (napi_status != napi_ok) {
467 delete drawable;
468 return nullptr;
469 }
470 napi_escape_handle(env, scope, thisVar, &thisVar);
471 napi_close_escapable_handle_scope(env, scope);
472 return thisVar;
473 }
474
PixelMapConstructor(napi_env env,napi_callback_info info)475 napi_value JsDrawableDescriptor::PixelMapConstructor(napi_env env, napi_callback_info info)
476 {
477 napi_escapable_handle_scope scope = nullptr;
478 napi_open_escapable_handle_scope(env, &scope);
479 size_t argc = PARAMS_NUM_TWO;
480 napi_value argv[argc];
481 napi_value thisVar = nullptr;
482 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
483 auto* drawable = new Drawable::PixelmapDrawableDescriptor;
484 if (argc == 0) {
485 auto napi_status = napi_wrap(env, thisVar, drawable, Destructor, nullptr, nullptr);
486 if (napi_status != napi_ok) {
487 delete drawable;
488 return thisVar;
489 }
490 napi_escape_handle(env, scope, thisVar, &thisVar);
491 napi_close_escapable_handle_scope(env, scope);
492 return thisVar;
493 }
494
495 napi_value res = argv[0];
496 napi_valuetype type = Drawable::NapiUtils::ParseJsType(env, res);
497
498 Drawable::SourceInfo sourceInfo;
499 if (type == napi_string) {
500 auto src = Drawable::NapiUtils::ParseJsString(env, res);
501 sourceInfo.SetSrc(src);
502 }
503
504 if (type == napi_object) {
505 auto pixelMap = GetPixelMapFromJsPixelMap(env, res);
506 if (pixelMap) {
507 drawable->SetPixelMap(pixelMap);
508 sourceInfo.SetSrcType(Drawable::SrcType::PIXMAP);
509 } else {
510 auto resource = Drawable::NapiUtils::ParseJsResource(env, res);
511 sourceInfo.SetResource(resource);
512 sourceInfo.SetSrcType(Drawable::SrcType::RESOURCE);
513 }
514 }
515
516 drawable->SetSourceInfo(sourceInfo);
517 if (argc == 2) {
518 napi_value res = argv[1];
519 napi_valuetype type = Drawable::NapiUtils::ParseJsType(env, res);
520 if (type == napi_object) {
521 napi_value jsFetchSync = Drawable::NapiUtils::GetNamedProperty(env, res, "fetchWhenConstructingWithUri");
522 auto fetchSync = Drawable::NapiUtils::ParseJsBool(env, jsFetchSync);
523 if (fetchSync) {
524 drawable->FetchSync();
525 }
526 }
527 }
528 // wrap to napi_value
529 auto napi_status =
530 napi_wrap_with_size(env, thisVar, drawable, Destructor, nullptr, nullptr, ESTIMATE_DRAWABLE_SIZE);
531 if (napi_status != napi_ok) {
532 delete drawable;
533 return thisVar;
534 }
535 napi_escape_handle(env, scope, thisVar, &thisVar);
536 napi_close_escapable_handle_scope(env, scope);
537 return thisVar;
538 }
539
InitDrawable(napi_env env)540 napi_value JsDrawableDescriptor::InitDrawable(napi_env env)
541 {
542 napi_value cons = nullptr;
543 napi_value typeName = Drawable::NapiUtils::CreateString(env, "DrawableDescriptor");
544 napi_property_descriptor baseDes[] = {
545 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
546 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
547 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
548 DECLARE_NAPI_FUNCTION("fetch", Fetch),
549 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
550 DECLARE_NAPI_PROPERTY("typeName", typeName)
551 };
552 NAPI_CALL(env, napi_define_class(env, DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, DrawableConstructor, nullptr,
553 sizeof(baseDes) / sizeof(napi_property_descriptor), baseDes, &cons));
554 NAPI_CALL(env, napi_create_reference(env, cons, 1, &baseConstructor_));
555 return cons;
556 }
557
InitPixelMapDrawable(napi_env env)558 napi_value JsDrawableDescriptor::InitPixelMapDrawable(napi_env env)
559 {
560 napi_value cons = nullptr;
561 napi_value typeName = Drawable::NapiUtils::CreateString(env, "PixelMapDrawableDescriptor");
562 napi_property_descriptor pixelDes[] = {
563 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
564 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
565 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
566 DECLARE_NAPI_FUNCTION("fetch", Fetch),
567 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
568 DECLARE_NAPI_PROPERTY("typeName", typeName)
569 };
570 NAPI_CALL(env, napi_define_class(env, PIXELMAP_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, PixelMapConstructor,
571 nullptr, sizeof(pixelDes) / sizeof(napi_property_descriptor), pixelDes, &cons));
572 NAPI_CALL(env, napi_create_reference(env, cons, 1, &pixelMapConstructor_));
573 return cons;
574 }
575
InitLayeredDrawable(napi_env env)576 napi_value JsDrawableDescriptor::InitLayeredDrawable(napi_env env)
577 {
578 napi_value cons = nullptr;
579 napi_value typeName = Drawable::NapiUtils::CreateString(env, "LayeredDrawableDescriptor");
580 napi_property_descriptor layeredDes[] = {
581 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
582 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
583 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
584 DECLARE_NAPI_FUNCTION("fetch", Fetch),
585 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
586 DECLARE_NAPI_FUNCTION("getForeground", GetForeground),
587 DECLARE_NAPI_FUNCTION("getBackground", GetBackground),
588 DECLARE_NAPI_FUNCTION("getMask", GetMask),
589 DECLARE_NAPI_STATIC_FUNCTION("getMaskClipPath", GetMaskClipPath),
590 DECLARE_NAPI_PROPERTY("typeName", typeName)
591 };
592 NAPI_CALL(env, napi_define_class(env, LAYERED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, LayeredConstructor,
593 nullptr, sizeof(layeredDes) / sizeof(napi_property_descriptor), layeredDes, &cons));
594 NAPI_CALL(env, napi_create_reference(env, cons, 1, &layeredConstructor_));
595 return cons;
596 }
597
InitAnimatedDrawable(napi_env env)598 napi_value JsDrawableDescriptor::InitAnimatedDrawable(napi_env env)
599 {
600 napi_value cons = nullptr;
601 napi_value typeName = Drawable::NapiUtils::CreateString(env, "AnimatedDrawableDescriptor");
602 napi_property_descriptor animatedDes[] = {
603 DECLARE_NAPI_FUNCTION("getPixelMap", GetPixelMap),
604 DECLARE_NAPI_FUNCTION("getOriginalWidth", GetOriginalWidth),
605 DECLARE_NAPI_FUNCTION("getOriginalHeight", GetOriginalHeight),
606 DECLARE_NAPI_FUNCTION("fetch", Fetch),
607 DECLARE_NAPI_FUNCTION("fetchSync", FetchSync),
608 DECLARE_NAPI_FUNCTION("isRunning", IsRunning),
609 DECLARE_NAPI_FUNCTION("start", Start),
610 DECLARE_NAPI_FUNCTION("stop", Stop),
611 DECLARE_NAPI_PROPERTY("typeName", typeName)
612 };
613 NAPI_CALL(env, napi_define_class(env, ANIMATED_DRAWABLE_DESCRIPTOR_NAME, NAPI_AUTO_LENGTH, AnimatedConstructor,
614 nullptr, sizeof(animatedDes) / sizeof(napi_property_descriptor), animatedDes, &cons));
615 NAPI_CALL(env, napi_create_reference(env, cons, 1, &animatedConstructor_));
616 return cons;
617 }
618 } // namespace Napi
619 } // namespace Ace
620 } // namespace OHOS
621