1 /*
2  * Copyright (c) 2022 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 #ifndef OHOS_ACELITE_IMAGE_MODEL_H
17 #define OHOS_ACELITE_IMAGE_MODEL_H
18 
19 #include "acelite_config.h"
20 #include "image_component.h"
21 #include "gfx_utils/list.h"
22 #include "js_fwk_common.h"
23 namespace OHOS {
24 namespace ACELite {
25 class ImageModule final : public MemoryHeap {
26 enum class ArgsCount { NUM_1 = 1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6 };
27 public:
GetSrc()28     const char*  GetSrc()
29     {
30         return src_;
31     }
32 
GetWidth()33     int16_t GetWidth()
34     {
35         return width_;
36     }
37 
GetHeight()38     int16_t GetHeight()
39     {
40         return height_;
41     }
42 
43     ACE_DISALLOW_COPY(ImageModule);
44     /**
45      * @brief register Image object
46      */
47     static void Init(jerry_value_t intlHandle);
48 
49     static void RegisterAttributeFunc(jerry_value_t canvas2dContext,
50                                                 const char *attributeName,
51                                                 jerry_external_handler_t setterHandler,
52                                                 jerry_external_handler_t getterHandler);
53 #if (defined(ENABLED_DELETE_MODULE_IAMGE) && (ENABLED_DELETE_MODULE_IAMGE == 1))
DeleteImage(void * ptr)54     static void DeleteImage(void* ptr)
55     {
56         ImageModule* image = reinterpret_cast<ImageModule*>(ptr);
57         ACE_DELETE(image);
58     }
59 #endif
60 
61     static void OnCallBack(const jerry_value_t context, const ImageModule *imageModule, bool isSucess, const char* msg);
62     // the handle to release the native value when the js value number format object is not needed.
63     static constexpr jerry_object_native_info_t gcCallback = {
64 #if ENABLED_DELETE_MODULE_IAMGE
65         .free_cb = DeleteImage
66 #endif
67     };
68     static const char * const attrOnload;
69     static const char * const attrOnError;
70     static const char * const attrSrc;
71     static const char * const attrWidth;
72     static const char * const attrHeight;
73     static const char * const className;
74 private:
ImageModule()75     ImageModule()
76         : src_(nullptr),
77           width_(-1),
78           height_(-1),
79           onLoadFunc_(UNDEFINED),
80           onErrorFunc_(UNDEFINED),
81           jerrySrc_(UNDEFINED) {}
82 
~ImageModule()83     ~ImageModule()
84     {
85         jerry_release_value(onLoadFunc_);
86         jerry_release_value(onErrorFunc_);
87         jerry_release_value(jerrySrc_);
88         ACE_FREE(src_);
89     }
90 
91     static jerry_value_t CreateImage(const jerry_value_t func,
92                                 const jerry_value_t context,
93                                 const jerry_value_t args[],
94                                 const jerry_length_t argsNum);
95     static jerry_value_t OnLoadSetter(const jerry_value_t func,
96                                 const jerry_value_t context,
97                                 const jerry_value_t args[],
98                                 const jerry_length_t argsNum);
99     static jerry_value_t OnLoadGetter(const jerry_value_t func,
100                                 const jerry_value_t context,
101                                 const jerry_value_t args[],
102                                 const jerry_length_t argsNum);
103     static jerry_value_t OnErrorSetter(const jerry_value_t func,
104                                 const jerry_value_t context,
105                                 const jerry_value_t args[],
106                                 const jerry_length_t argsNum);
107     static jerry_value_t OnErrorGetter(const jerry_value_t func,
108                                 const jerry_value_t context,
109                                 const jerry_value_t args[],
110                                 const jerry_length_t argsNum);
111     static jerry_value_t OnSrcSetter(const jerry_value_t func,
112                                 const jerry_value_t context,
113                                 const jerry_value_t args[],
114                                 const jerry_length_t argsNum);
115     static jerry_value_t OnSrcGetter(const jerry_value_t func,
116                                 const jerry_value_t context,
117                                 const jerry_value_t args[],
118                                 const jerry_length_t argsNum);
119     static jerry_value_t OnHeightSetter(const jerry_value_t func,
120                                 const jerry_value_t context,
121                                 const jerry_value_t args[],
122                                 const jerry_length_t argsNum);
123     static jerry_value_t OnHeightGetter(const jerry_value_t func,
124                                 const jerry_value_t context,
125                                 const jerry_value_t args[],
126                                 const jerry_length_t argsNum);
127     static jerry_value_t OnWidthSetter(const jerry_value_t func,
128                                 const jerry_value_t context,
129                                 const jerry_value_t args[],
130                                 const jerry_length_t argsNum);
131     static jerry_value_t OnWidthGetter(const jerry_value_t func,
132                                 const jerry_value_t context,
133                                 const jerry_value_t args[],
134                                 const jerry_length_t argsNum);
135 
136 private:
137     char* src_;
138     int16_t width_;
139     int16_t height_;
140     jerry_value_t onLoadFunc_;
141     jerry_value_t onErrorFunc_;
142     jerry_value_t jerrySrc_;
143 };
144 }
145 }
146 #endif // NUMBER_FORMAT_MODEL_H
147