Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / resource-image-api.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 "resource-image-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <image/image-wrapper.h>
24 #include <image/image-attributes-wrapper.h>
25 #include <image/image-attributes-api.h>
26
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 ResourceImage ResourceImageApi::GetResourceImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
35 {
36   v8::HandleScope handleScope( isolate );
37
38   v8::Local<v8::Object> object = args.This();
39   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
40   void* ptr = field->Value();
41
42   ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
43   return ResourceImage::DownCast( wrapper->GetImage() );
44 }
45
46 /**
47  * Create a new resource image object.
48  *
49  * @constructor
50  * @method ResourceImage
51  * @for ResourceImage
52  * @param {Object} options
53  * @param {String} options.url The URL of the image file to use.
54  * @param {Object} [option.imageAttributes] image attributes object
55  * @param {Integer} [options.loadPolicy] The LoadPolicy to apply when loading the image resource.
56  * @param {Integer} [options.releasePolicy] optionally release memory when image is not visible on screen.
57  * @return {Object} Image
58  */
59 Image ResourceImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
60 {
61   v8::Isolate* isolate = args.GetIsolate();
62   v8::HandleScope handleScope( isolate );
63
64   std::string url;
65   ResourceImage::LoadPolicy loadPolicy( ResourceImage::IMMEDIATE );
66   Image::ReleasePolicy releasePolicy( Image::NEVER);
67   ImageAttributes imageAttributes;
68   bool useImageAttributes = false;
69
70   v8::Local<v8::Value> options( args[0] );
71
72   if( !options->IsObject() )
73   {
74     DALI_SCRIPT_EXCEPTION( isolate, "Missing params" );
75     return Image();
76   }
77
78   v8::Local<v8::Object> optionsObject = options->ToObject();
79
80   v8::Local<v8::Value> urlValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "url" ) );
81   if( urlValue->IsString() )
82   {
83     url = V8Utils::v8StringToStdString( urlValue );
84   }
85   else
86   {
87     DALI_SCRIPT_EXCEPTION( isolate, "Missing url");
88     return Image();
89   }
90
91   v8::Local<v8::Value> imageAttribsValue= optionsObject->Get( v8::String::NewFromUtf8( isolate, "imageAttributes" ) );
92   if( imageAttribsValue->IsObject() )
93   {
94     imageAttributes = ImageAttributesApi::GetImageAttributesFromObject( isolate, imageAttribsValue->ToObject() );
95   }
96
97
98
99   v8::Local<v8::Value> releasePolicyValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "releasePolicy" ) );
100   if( releasePolicyValue->IsUint32() )
101   {
102     releasePolicy = static_cast<Image::ReleasePolicy>( releasePolicyValue->ToUint32()->Value() );
103   }
104
105   v8::Local<v8::Value> loadPolicyValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "loadPolicy" ) );
106   if( loadPolicyValue->IsUint32() )
107   {
108     loadPolicy = static_cast< ResourceImage::LoadPolicy >( loadPolicyValue->ToUint32()->Value());
109   }
110
111   if( useImageAttributes )
112   {
113     return ResourceImage::New( url, imageAttributes, loadPolicy, releasePolicy);
114   }
115   else
116   {
117     return ResourceImage::New( url, loadPolicy, releasePolicy);
118   }
119 }
120
121 /**
122  * Get the load policy
123  *
124  * @method getLoadPolicy
125  * @for ResourceImage
126  * @return {Integer} load policy either dali.IMAGE_LOAD_POLICY_ON_DEMAND or dali.IMAGE_LOAD_POLICY_IMMEDIATE
127  */
128 void ResourceImageApi::GetLoadPolicy( const v8::FunctionCallbackInfo< v8::Value >& args )
129 {
130   v8::Isolate* isolate = args.GetIsolate();
131   v8::HandleScope handleScope( isolate );
132
133   ResourceImage image = GetResourceImage( isolate, args );
134
135   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetLoadPolicy() ) );
136 }
137
138 /**
139  * Query whether the image data has loaded.
140  *
141  * The asynchronous loading begins when the Image object is created.
142  * After the Image object is discarded, the image data will be released from memory
143  * this will occur when the object is garbage collected.
144  * @method getLoadPolicy
145  * @for ResourceImage
146  * @return {Integer} loading state either dali.RESOURCE_LOADING, dali.RESOURCE_LOADING_SUCCEEDED or dali.RESOUCE_LOADING_FAILED
147  */
148 void ResourceImageApi::GetLoadingState( const v8::FunctionCallbackInfo< v8::Value >& args )
149 {
150   v8::Isolate* isolate = args.GetIsolate();
151   v8::HandleScope handleScope( isolate );
152
153   ResourceImage image = GetResourceImage( isolate, args );
154
155   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetLoadingState() ) );
156 }
157
158 /**
159  * Return the image url
160  *
161  * @method getUrl
162  * @for ResourceImage
163  * @return {String} filename
164  */
165 void ResourceImageApi::GetUrl( const v8::FunctionCallbackInfo< v8::Value >& args )
166 {
167   v8::Isolate* isolate = args.GetIsolate();
168   v8::HandleScope handleScope( isolate );
169
170   ResourceImage image = GetResourceImage( isolate, args );
171
172   v8::Local<v8::String> v8String = v8::String::NewFromUtf8( isolate, image.GetUrl().c_str() );
173   args.GetReturnValue().Set( v8String  );
174 }
175
176
177 /**
178  * Reload the image
179  * The set ImageAttributes are used when requesting the image again.
180  * @note if Image is offstage and OnDemand policy is set, reload request is ignored.
181  * @method reload
182  * @for ResourceImage
183  */
184 void ResourceImageApi::Reload( const v8::FunctionCallbackInfo< v8::Value >& args )
185 {
186   v8::Isolate* isolate = args.GetIsolate();
187   v8::HandleScope handleScope( isolate );
188
189   ResourceImage image = GetResourceImage( isolate, args );
190   image.Reload();
191 }
192
193
194 /**
195  * Return attributes for the image
196  * Only to be used after the image has finished loading.
197  * (Ticket's LoadingSucceeded callback was called)
198  * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
199  *
200  * @method getAttributes
201  * @for ResourceImage
202  * @return {Object} ImageAttributes
203  */
204 void ResourceImageApi::GetAttributes( const v8::FunctionCallbackInfo< v8::Value >& args )
205 {
206   v8::Isolate* isolate = args.GetIsolate();
207   v8::HandleScope handleScope( isolate );
208
209   ResourceImage image = GetResourceImage( isolate, args );
210
211   v8::Local<v8::Object> localObject = ImageAttributesWrapper::WrapImageAttributes(isolate, image.GetAttributes());
212
213   args.GetReturnValue().Set( localObject );
214 }
215 } // namespace V8Plugin
216
217 } // namespace Dali