2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "image-wrapper.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>
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;
43 * pointer to a persistent template handle
47 v8::Persistent<v8::ObjectTemplate>* imageTemplate;
51 * array of templates for each type of image
53 const ImageTemplate ImageTemplateLookup[]=
55 { &ImageWrapper::mImageTemplate }, // IMAGE
56 { &ImageWrapper::mResourceImageTemplate }, // RESOURCE_IMAGE
57 { &ImageWrapper::mBufferImageTemplate }, // BITMAP_IMAGE
58 { &ImageWrapper::mFrameBufferImageTemplate }, // FRAME_BUFFER_IMAGE
63 namespace // un-named name space
67 * Bitmask of API's that an image can support
72 BITMAP_IMAGE_API = 1 << 1,
73 RESOURCE_IMAGE_API = 1 << 2,
74 FRAME_BUFFER_IMAGE_API = 1 << 3,
79 * structure used for the ImageApiLookup.
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
90 * Lookup table to match a image type with a constructor and supported API's.
92 const ImageApiStruct ImageApiLookup[]=
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 },
100 const unsigned int ImageApiLookupCount = sizeof(ImageApiLookup)/sizeof(ImageApiLookup[0]);
104 * given an image type return what api's it supports
106 int GetImageSupportedApis( ImageWrapper::ImageType type )
108 return ImageApiLookup[ type].supportApis;
112 * Used for the ImageFunctionTable to map function names to functions
113 * with for a specific API
115 struct ImageFunctions
117 const char* name; ///< function name
118 void (*function)( const v8::FunctionCallbackInfo< v8::Value >& args);
123 * Contains a list of all functions that can be called
125 const ImageFunctions ImageFunctionTable[]=
127 /**************************************
128 * Image API (in order of image.h)
129 **************************************/
130 { "GetWidth", ImageApi::GetWidth , IMAGE_API },
131 { "GetHeight", ImageApi::GetHeight, IMAGE_API },
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 },
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 },
146 // Frame buffer image has no API
147 // Native image has no API
151 const unsigned int ImageFunctionTableCount = sizeof(ImageFunctionTable)/sizeof(ImageFunctionTable[0]);
155 ImageWrapper::ImageWrapper( const Dali::Image& image, GarbageCollectorInterface& gc )
156 : BaseWrappedObject( BaseWrappedObject::IMAGE , gc )
161 v8::Handle<v8::Object> ImageWrapper::WrapImage(v8::Isolate* isolate, const Dali::Image& image )
163 v8::EscapableHandleScope handleScope( isolate );
165 v8::Local<v8::Object> object = WrapImage( isolate, image, GetImageType( image.GetTypeName() ) );
167 return handleScope.Escape( object );
169 v8::Handle<v8::Object> ImageWrapper::WrapImage(v8::Isolate* isolate, const Dali::Image& image, ImageType imageType )
171 v8::EscapableHandleScope handleScope( isolate );
172 v8::Local<v8::ObjectTemplate> objectTemplate;
174 objectTemplate = GetImageTemplate( isolate, imageType);
176 // create an instance of the template
177 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
179 // create the Image wrapper
180 ImageWrapper* pointer = new ImageWrapper( image, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
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 );
186 return handleScope.Escape( localObject );
189 v8::Local<v8::ObjectTemplate> ImageWrapper::GetImageTemplate( v8::Isolate* isolate, ImageType imageType )
191 v8::EscapableHandleScope handleScope( isolate );
192 v8::Local<v8::ObjectTemplate> objectTemplate;
194 if( ImageTemplateLookup[ imageType ].imageTemplate->IsEmpty() )
196 objectTemplate = MakeImageTemplate( isolate, imageType );
197 ImageTemplateLookup[ imageType ].imageTemplate->Reset( isolate, objectTemplate );
201 // get the object template
202 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ImageTemplateLookup[ imageType ].imageTemplate );
205 return handleScope.Escape( objectTemplate );
208 v8::Handle<v8::ObjectTemplate> ImageWrapper::MakeImageTemplate( v8::Isolate* isolate, ImageType imageType )
210 v8::EscapableHandleScope handleScope( isolate );
212 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
214 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
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 )
220 ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
223 // find out what API's this image supports
224 int supportApis = GetImageSupportedApis( imageType );
226 // add our function properties
227 for( unsigned int i = 0; i < ImageFunctionTableCount; ++i )
229 const ImageFunctions property = ImageFunctionTable[i];
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 )
235 std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
237 objTemplate->Set( v8::String::NewFromUtf8( isolate, funcName.c_str() ),
238 v8::FunctionTemplate::New( isolate, property.function ) );
242 return handleScope.Escape( objTemplate );
246 void ImageWrapper::NewImage( const v8::FunctionCallbackInfo< v8::Value >& args)
248 v8::Isolate* isolate = args.GetIsolate();
249 v8::HandleScope handleScope( isolate);
251 if(!args.IsConstructCall())
253 DALI_SCRIPT_EXCEPTION( isolate, "Image constructor called without 'new'");
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 );
262 ImageType imageType = GetImageType( typeName );
264 if( imageType == UNKNOWN_IMAGE_TYPE )
266 DALI_SCRIPT_EXCEPTION( isolate, "unknown image type");
269 Image image = (ImageApiLookup[imageType].constructor)( args );
273 // a v8 exception will have been thrown by the constructor
277 v8::Local<v8::Object> localObject = WrapImage( isolate, image, imageType );
279 args.GetReturnValue().Set( localObject );
282 Image ImageWrapper::GetImage()
288 * given an image type name, e.g. returns the type, e.g. ImageWrapper::BITMAP_IMAGE
290 ImageWrapper::ImageType ImageWrapper::GetImageType( const std::string& name )
292 for( unsigned int i = 0 ; i < ImageApiLookupCount ; i++ )
294 if( strncmp( ImageApiLookup[i].imageName, name.c_str(), name.length() ) == 0 )
296 return ImageApiLookup[i].imageType;
299 return ImageWrapper::UNKNOWN_IMAGE_TYPE;
304 } // namespace V8Plugin