b30b948ea61c565f6688a08f06ed2f3eceb9831f
[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 // EXTERNAL INCLUDES
22 #include <dali/devel-api/images/image-operations.h>
23
24 // INTERNAL INCLUDES
25 #include <v8-utils.h>
26 #include <image/image-wrapper.h>
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 {Integer} [options.loadPolicy] The LoadPolicy to apply when loading the image resource.
55  * @param {Integer} [options.releasePolicy] optionally release memory when image is not visible on screen.
56  * @return {Object} Image
57  */
58 Image ResourceImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
59 {
60   v8::Isolate* isolate = args.GetIsolate();
61   v8::HandleScope handleScope( isolate );
62
63   std::string url;
64   ImageDimensions dimensions;
65   FittingMode::Type fittingMode = FittingMode::DEFAULT;
66   SamplingMode::Type samplingMode = SamplingMode::DEFAULT;
67   bool orientationCorrection = true;
68   ResourceImage::LoadPolicy loadPolicy( ResourceImage::IMMEDIATE );
69   Image::ReleasePolicy releasePolicy( Image::NEVER);
70
71   v8::Local<v8::Value> options( args[0] );
72
73   if( !options->IsObject() )
74   {
75     DALI_SCRIPT_EXCEPTION( isolate, "Missing params" );
76     return Image();
77   }
78
79   v8::Local<v8::Object> optionsObject = options->ToObject();
80
81   v8::Local<v8::Value> urlValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "url" ) );
82   if( urlValue->IsString() )
83   {
84     url = V8Utils::v8StringToStdString( urlValue );
85   }
86   else
87   {
88     DALI_SCRIPT_EXCEPTION( isolate, "Missing url");
89     return Image();
90   }
91
92   v8::Local<v8::Value> widthValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "width" ) );
93   if( widthValue->IsUint32() )
94   {
95     const uint32_t width = widthValue->ToUint32()->Value();
96     dimensions = ImageDimensions( width, dimensions.GetHeight() );
97   }
98
99   v8::Local<v8::Value> heightValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "height" ) );
100   if( heightValue->IsUint32() )
101   {
102     const uint32_t height = heightValue->ToUint32()->Value();
103     dimensions = ImageDimensions( dimensions.GetWidth(), height );
104   }
105
106   v8::Local<v8::Value> fittingModeValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "fittingMode" ) );
107   if( fittingModeValue->IsUint32() )
108   {
109     fittingMode = static_cast<FittingMode::Type>( fittingModeValue->ToUint32()->Value() );
110   }
111
112   v8::Local<v8::Value> samplingModeValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "samplingMode" ) );
113   if( samplingModeValue->IsUint32() )
114   {
115     samplingMode = static_cast<SamplingMode::Type>( samplingModeValue->ToUint32()->Value() );
116   }
117
118   v8::Local<v8::Value> orientationCorrectionValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "orientationCorrection" ) );
119   if( orientationCorrectionValue->IsBoolean() )
120   {
121     orientationCorrection = orientationCorrectionValue->ToBoolean()->Value();
122   }
123
124   v8::Local<v8::Value> releasePolicyValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "releasePolicy" ) );
125   if( releasePolicyValue->IsUint32() )
126   {
127     releasePolicy = static_cast<Image::ReleasePolicy>( releasePolicyValue->ToUint32()->Value() );
128   }
129
130   v8::Local<v8::Value> loadPolicyValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "loadPolicy" ) );
131   if( loadPolicyValue->IsUint32() )
132   {
133     loadPolicy = static_cast< ResourceImage::LoadPolicy >( loadPolicyValue->ToUint32()->Value());
134   }
135
136   return ResourceImage::New( url, loadPolicy, releasePolicy, dimensions, fittingMode, samplingMode, orientationCorrection );
137 }
138
139 /**
140  * Get the load policy
141  *
142  * @method getLoadPolicy
143  * @for ResourceImage
144  * @return {Integer} load policy either dali.IMAGE_LOAD_POLICY_ON_DEMAND or dali.IMAGE_LOAD_POLICY_IMMEDIATE
145  */
146 void ResourceImageApi::GetLoadPolicy( const v8::FunctionCallbackInfo< v8::Value >& args )
147 {
148   v8::Isolate* isolate = args.GetIsolate();
149   v8::HandleScope handleScope( isolate );
150
151   ResourceImage image = GetResourceImage( isolate, args );
152
153   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetLoadPolicy() ) );
154 }
155
156 /**
157  * Query whether the image data has loaded.
158  *
159  * The asynchronous loading begins when the Image object is created.
160  * After the Image object is discarded, the image data will be released from memory
161  * this will occur when the object is garbage collected.
162  * @method getLoadPolicy
163  * @for ResourceImage
164  * @return {Integer} loading state either dali.RESOURCE_LOADING, dali.RESOURCE_LOADING_SUCCEEDED or dali.RESOUCE_LOADING_FAILED
165  */
166 void ResourceImageApi::GetLoadingState( const v8::FunctionCallbackInfo< v8::Value >& args )
167 {
168   v8::Isolate* isolate = args.GetIsolate();
169   v8::HandleScope handleScope( isolate );
170
171   ResourceImage image = GetResourceImage( isolate, args );
172
173   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetLoadingState() ) );
174 }
175
176 /**
177  * Return the image url
178  *
179  * @method getUrl
180  * @for ResourceImage
181  * @return {String} filename
182  */
183 void ResourceImageApi::GetUrl( const v8::FunctionCallbackInfo< v8::Value >& args )
184 {
185   v8::Isolate* isolate = args.GetIsolate();
186   v8::HandleScope handleScope( isolate );
187
188   ResourceImage image = GetResourceImage( isolate, args );
189
190   v8::Local<v8::String> v8String = v8::String::NewFromUtf8( isolate, image.GetUrl().c_str() );
191   args.GetReturnValue().Set( v8String  );
192 }
193
194
195 /**
196  * Reload the image
197  * @note if Image is offstage and OnDemand policy is set, reload request is ignored.
198  * @method reload
199  * @for ResourceImage
200  */
201 void ResourceImageApi::Reload( const v8::FunctionCallbackInfo< v8::Value >& args )
202 {
203   v8::Isolate* isolate = args.GetIsolate();
204   v8::HandleScope handleScope( isolate );
205
206   ResourceImage image = GetResourceImage( isolate, args );
207   image.Reload();
208 }
209
210 } // namespace V8Plugin
211
212 } // namespace Dali