Remove JavaScript binding for NativeImage, NinePatchImage, ImageActor and ShaderEffect
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / image-wrapper.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "image-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <image/image-api.h>
25 #include <image/frame-buffer-image-api.h>
26 #include <image/resource-image-api.h>
27 #include <image/buffer-image-api.h>
28 #include <shared/api-function.h>
29 #include <shared/object-template-helper.h>
30
31 namespace Dali
32 {
33
34 namespace V8Plugin
35 {
36
37 v8::Persistent<v8::ObjectTemplate> ImageWrapper::mImageTemplate;
38 v8::Persistent<v8::ObjectTemplate> ImageWrapper::mResourceImageTemplate;
39 v8::Persistent<v8::ObjectTemplate> ImageWrapper::mBufferImageTemplate;
40 v8::Persistent<v8::ObjectTemplate> ImageWrapper::mFrameBufferImageTemplate;
41
42 /**
43  * pointer to a persistent template handle
44  */
45 struct ImageTemplate
46 {
47   v8::Persistent<v8::ObjectTemplate>* imageTemplate;
48 };
49
50 /**
51  * array of templates for each type of image
52  */
53 const ImageTemplate ImageTemplateLookup[]=
54 {
55     { &ImageWrapper::mImageTemplate },              // IMAGE
56     { &ImageWrapper::mResourceImageTemplate },      // RESOURCE_IMAGE
57     { &ImageWrapper::mBufferImageTemplate  },       // BITMAP_IMAGE
58     { &ImageWrapper::mFrameBufferImageTemplate },   // FRAME_BUFFER_IMAGE
59
60 };
61
62
63 namespace // un-named name space
64 {
65
66 /**
67  * Bitmask of API's that an image can support
68  */
69 enum ImageApiBitMask
70 {
71   IMAGE_API              = 1 << 0,
72   BITMAP_IMAGE_API       = 1 << 1,
73   RESOURCE_IMAGE_API     = 1 << 2,
74   FRAME_BUFFER_IMAGE_API = 1 << 3,
75
76 };
77
78 /**
79  * structure used for the ImageApiLookup.
80  */
81 struct ImageApiStruct
82 {
83   const char* imageName;                  ///< name of the image, used to find out what type of image to construct
84   ImageWrapper::ImageType imageType;      ///< image type
85   Image (*constructor)( const v8::FunctionCallbackInfo< v8::Value >& args); ///< constructor
86   int supportApis;                        ///< supported API's
87 };
88
89 /**
90  * Lookup table to match a image type with a constructor and supported API's.
91  */
92 const ImageApiStruct ImageApiLookup[]=
93 {
94   {"Image",           ImageWrapper::IMAGE,              ImageApi::New,            IMAGE_API },
95   {"ResourceImage",   ImageWrapper::RESOURCE_IMAGE,     ResourceImageApi::New,    IMAGE_API | RESOURCE_IMAGE_API },
96   {"BufferImage",     ImageWrapper::BITMAP_IMAGE,       BufferImageApi::New,      IMAGE_API | BITMAP_IMAGE_API },
97   {"FrameBufferImage",ImageWrapper::FRAME_BUFFER_IMAGE, FrameBufferImageApi::New, IMAGE_API | FRAME_BUFFER_IMAGE_API },
98 };
99
100 const unsigned int ImageApiLookupCount = sizeof(ImageApiLookup)/sizeof(ImageApiLookup[0]);
101
102
103 /**
104  * given an image type return what api's it supports
105  */
106 int GetImageSupportedApis( ImageWrapper::ImageType type )
107 {
108   return ImageApiLookup[ type].supportApis;
109 }
110
111 /**
112  * Used for the ImageFunctionTable to map function names to functions
113  * with for a specific API
114  */
115 struct ImageFunctions
116 {
117   const char* name;               ///< function name
118   void (*function)( const v8::FunctionCallbackInfo< v8::Value >& args);
119   ImageApiBitMask api;
120 };
121
122 /**
123  * Contains a list of all functions that can be called
124  */
125 const ImageFunctions ImageFunctionTable[]=
126 {
127     /**************************************
128     * Image API (in order of image.h)
129     **************************************/
130     { "GetWidth",                ImageApi::GetWidth , IMAGE_API },
131     { "GetHeight",               ImageApi::GetHeight, IMAGE_API },
132
133     // resource-image API
134     { "GetLoadingState",         ResourceImageApi::GetLoadingState,     RESOURCE_IMAGE_API },
135     { "GetUrl",                  ResourceImageApi::GetUrl,              RESOURCE_IMAGE_API },
136     { "Reload",                  ResourceImageApi::Reload,              RESOURCE_IMAGE_API },
137
138     // buffer image API
139     { "GetBuffer",              BufferImageApi::GetBuffer,           BITMAP_IMAGE_API },
140     { "GetBufferSize",          BufferImageApi::GetBufferSize,       BITMAP_IMAGE_API },
141     { "GetBufferStride",        BufferImageApi::GetBufferStride,     BITMAP_IMAGE_API },
142     { "GetPixelFormat",         BufferImageApi::GetPixelFormat,      BITMAP_IMAGE_API },
143     { "Update",                 BufferImageApi::Update,              BITMAP_IMAGE_API },
144     { "IsDataExternal",         BufferImageApi::IsDataExternal,      BITMAP_IMAGE_API },
145
146     // Frame buffer image has no API
147     // Native image has no API
148
149 };
150
151 const unsigned int ImageFunctionTableCount = sizeof(ImageFunctionTable)/sizeof(ImageFunctionTable[0]);
152 } //un-named space
153
154
155 ImageWrapper::ImageWrapper( const Dali::Image& image, GarbageCollectorInterface& gc )
156 : BaseWrappedObject( BaseWrappedObject::IMAGE , gc )
157 {
158     mImage = image;
159 }
160
161 v8::Handle<v8::Object> ImageWrapper::WrapImage(v8::Isolate* isolate, const Dali::Image& image )
162 {
163   v8::EscapableHandleScope handleScope( isolate );
164
165   v8::Local<v8::Object> object = WrapImage( isolate, image, GetImageType( image.GetTypeName() ) );
166
167   return handleScope.Escape( object );
168 }
169 v8::Handle<v8::Object> ImageWrapper::WrapImage(v8::Isolate* isolate, const Dali::Image& image, ImageType imageType )
170 {
171   v8::EscapableHandleScope handleScope( isolate );
172   v8::Local<v8::ObjectTemplate> objectTemplate;
173
174   objectTemplate = GetImageTemplate( isolate, imageType);
175
176   // create an instance of the template
177   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
178
179   // create the Image wrapper
180   ImageWrapper* pointer =  new ImageWrapper( image, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
181
182   // assign the JavaScript object to the wrapper.
183   // This also stores the Image object, in an internal field inside the JavaScript object.
184   pointer->SetJavascriptObject( isolate, localObject );
185
186   return handleScope.Escape( localObject );
187 }
188
189 v8::Local<v8::ObjectTemplate> ImageWrapper::GetImageTemplate( v8::Isolate* isolate, ImageType imageType )
190 {
191   v8::EscapableHandleScope handleScope( isolate );
192   v8::Local<v8::ObjectTemplate> objectTemplate;
193
194   if( ImageTemplateLookup[ imageType ].imageTemplate->IsEmpty() )
195   {
196     objectTemplate = MakeImageTemplate( isolate, imageType );
197     ImageTemplateLookup[ imageType ].imageTemplate->Reset( isolate, objectTemplate );
198   }
199   else
200   {
201     // get the object template
202     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ImageTemplateLookup[ imageType ].imageTemplate );
203   }
204
205   return handleScope.Escape( objectTemplate );
206 }
207
208 v8::Handle<v8::ObjectTemplate> ImageWrapper::MakeImageTemplate( v8::Isolate* isolate, ImageType imageType )
209 {
210   v8::EscapableHandleScope handleScope( isolate );
211
212   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
213
214   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
215
216   // add intercepts for Signals on ResourceImage, we can't use HandleWrapper::AddIntercepts because Image doesn't inherit
217   // from Handle ( just baseHandle)
218   if ( imageType == RESOURCE_IMAGE )
219   {
220      ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
221   }
222
223   // find out what API's this image supports
224   int supportApis = GetImageSupportedApis( imageType );
225
226   // add our function properties
227   for( unsigned int i = 0; i < ImageFunctionTableCount; ++i )
228   {
229     const ImageFunctions property =  ImageFunctionTable[i];
230
231     // check to see if the image supports a certain type of API
232     // e.g. Bitmap will support IMAGE_API and BITMAP_IMAGE_API
233     if( supportApis &  property.api )
234     {
235       std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
236
237       objTemplate->Set( v8::String::NewFromUtf8(   isolate, funcName.c_str() ),
238                       v8::FunctionTemplate::New( isolate, property.function ) );
239     }
240   }
241
242   return handleScope.Escape( objTemplate );
243 }
244
245
246 void ImageWrapper::NewImage( const v8::FunctionCallbackInfo< v8::Value >& args)
247 {
248   v8::Isolate* isolate = args.GetIsolate();
249   v8::HandleScope handleScope( isolate);
250
251   if(!args.IsConstructCall())
252   {
253       DALI_SCRIPT_EXCEPTION( isolate, "Image constructor called without 'new'");
254       return;
255   }
256
257   // find out the callee function name...e.g. BufferImage, ResourceImage
258   v8::Local<v8::Function> callee = args.Callee();
259   v8::Local<v8::Value> v8String = callee->GetName();
260   std::string typeName = V8Utils::v8StringToStdString( v8String );
261
262   ImageType imageType = GetImageType( typeName );
263
264   if( imageType == UNKNOWN_IMAGE_TYPE )
265   {
266     DALI_SCRIPT_EXCEPTION( isolate, "unknown image type");
267     return;
268   }
269   Image image = (ImageApiLookup[imageType].constructor)( args );
270
271   if( ! image )
272   {
273     // a v8 exception will have been thrown by the constructor
274     return;
275   }
276
277   v8::Local<v8::Object> localObject = WrapImage( isolate, image, imageType );
278
279   args.GetReturnValue().Set( localObject );
280 }
281
282 Image ImageWrapper::GetImage()
283 {
284   return mImage;
285 }
286
287 /**
288  * given an image type name, e.g. returns the type, e.g. ImageWrapper::BITMAP_IMAGE
289  */
290 ImageWrapper::ImageType ImageWrapper::GetImageType( const std::string& name )
291 {
292   for( unsigned int i = 0 ; i < ImageApiLookupCount ; i++ )
293   {
294     if( strncmp( ImageApiLookup[i].imageName, name.c_str(), name.length() ) == 0 )
295     {
296       return ImageApiLookup[i].imageType;
297     }
298   }
299   return ImageWrapper::UNKNOWN_IMAGE_TYPE;
300 }
301
302
303
304 } // namespace V8Plugin
305
306 } // namespace Dali