2ec736d6eccb0a26d0b28e614c8399dfed18af64
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / renderer-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
19 // CLASS HEADER
20 #include "renderer-api.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <v8-utils.h>
27 #include <rendering/renderer-wrapper.h>
28 #include <rendering/geometry-api.h>
29 #include <rendering/geometry-wrapper.h>
30 #include <rendering/material-api.h>
31 #include <rendering/material-wrapper.h>
32
33 namespace Dali
34 {
35
36 namespace V8Plugin
37 {
38
39 /**
40  * ## Renderer API
41  *
42  * Renderer is a handle to an object that can be used to provide an image to a material.
43  *
44  * @class Renderer
45  * @extends Handle
46  */
47
48 Renderer RendererApi::GetRenderer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
49 {
50   v8::HandleScope handleScope( isolate );
51
52   v8::Local<v8::Object> object = args.This();
53   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
54   void* ptr = field->Value();
55
56   RendererWrapper* wrapper = static_cast< RendererWrapper *>(ptr);
57   return wrapper->GetRenderer();
58 }
59
60 Renderer RendererApi::GetRendererFromParams( int paramIndex,
61                                              bool& found,
62                                              v8::Isolate* isolate,
63                                              const v8::FunctionCallbackInfo< v8::Value >& args )
64 {
65   found = false;
66
67   v8::HandleScope handleScope( isolate );
68   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDERER, isolate, args );
69   if( wrappedObject )
70   {
71     found = true;
72     RendererWrapper* wrapper = static_cast< RendererWrapper *>(wrappedObject);
73     return wrapper->GetRenderer();
74   }
75   else
76   {
77     return Renderer();
78   }
79 }
80
81 /**
82  * Create a new renderer object.
83  *
84  * @constructor
85  * @method Renderer
86  * @for Renderer
87  * @param {Object} geometry The geometry to be used by this renderer
88  * @param {Object} material The material to be used by this renderer
89  * @return {Object} Renderer
90  */
91 Renderer RendererApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
92 {
93   v8::Isolate* isolate = args.GetIsolate();
94   v8::HandleScope handleScope( isolate );
95
96   bool found( false );
97   Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
98   if( !found )
99   {
100     DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
101     return Renderer();
102   }
103
104   found = false;
105   Material material = MaterialApi::GetMaterialFromParams( 1, found, isolate, args );
106   if( !found )
107   {
108     DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" );
109     return Renderer();
110   }
111
112   return Renderer::New(geometry, material);
113 }
114
115 /**
116  * Sets the geometry to be used by this renderer
117  *
118  * @method setGeometry
119  * @for Renderer
120  * @param {Object} geometry The geometry to be used by this renderer
121  */
122 void RendererApi::SetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args )
123 {
124   v8::Isolate* isolate = args.GetIsolate();
125   v8::HandleScope handleScope( isolate );
126
127   Renderer renderer = GetRenderer( isolate, args );
128
129   bool found( false );
130   Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
131   if( !found )
132   {
133     DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
134   }
135   else
136   {
137     renderer.SetGeometry(geometry);
138   }
139 }
140
141 /**
142  * Gets the geometry used by this renderer
143  *
144  * @method getGeometry
145  * @for Renderer
146  * @return {Object} The geometry used by this renderer
147  */
148 void RendererApi::GetGeometry( const v8::FunctionCallbackInfo<v8::Value>& args )
149 {
150   v8::Isolate* isolate = args.GetIsolate();
151   v8::HandleScope handleScope( isolate );
152
153   Renderer renderer = GetRenderer( isolate, args );
154   Geometry geometry = renderer.GetGeometry();
155
156   // Wrap the geometry
157   v8::Local<v8::Object> localObject = GeometryWrapper::WrapGeometry( isolate, geometry );
158   args.GetReturnValue().Set( localObject );
159 }
160
161 /**
162  * Sets the material to be used by this renderer
163  *
164  * @method setMaterial
165  * @for Renderer
166  * @param {Object} material The material to be used by this renderer
167  */
168 void RendererApi::SetMaterial( const v8::FunctionCallbackInfo< v8::Value >& args )
169 {
170   v8::Isolate* isolate = args.GetIsolate();
171   v8::HandleScope handleScope( isolate );
172
173   Renderer renderer = GetRenderer( isolate, args );
174
175   bool found( false );
176   Material material = MaterialApi::GetMaterialFromParams( 0, found, isolate, args );
177   if( !found )
178   {
179     DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" );
180   }
181   else
182   {
183     renderer.SetMaterial(material);
184   }
185 }
186
187 /**
188  * Gets the material used by this renderer
189  *
190  * @method getMaterial
191  * @for Renderer
192  * @return {Object} The material used by this renderer
193  */
194 void RendererApi::GetMaterial( const v8::FunctionCallbackInfo<v8::Value>& args )
195 {
196   v8::Isolate* isolate = args.GetIsolate();
197   v8::HandleScope handleScope( isolate );
198
199   Renderer renderer = GetRenderer( isolate, args );
200   Material material = renderer.GetMaterial();
201
202   // Wrap the material
203   v8::Local<v8::Object> localObject = MaterialWrapper::WrapMaterial( isolate, material );
204   args.GetReturnValue().Set( localObject );
205 }
206
207 /**
208  * Specify the pixel arithmetic used when the actor is blended.
209  *
210  * @for Renderer
211  * @method setBlendFunc
212  * @param {integer} srcFactorRgb Source Blending RGB
213  * @param {integer} destFactorRgb Destination Blending RGB
214  * @param {integer} srcFactorAlpha Source Blending Alpha
215  * @param {integer} destFactorAlpha Destination Blending Alpha
216  * @example
217  *    //blending constants
218  *    dali.BLEND_FACTOR_ZERO
219  *    dali.BLEND_FACTOR_ONE
220  *    dali.BLEND_FACTOR_SRC_COLOR
221  *    dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
222  *    dali.BLEND_FACTOR_SRC_ALPHA
223  *    dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
224  *    dali.BLEND_FACTOR_DST_ALPHA
225  *    dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
226  *    dali.BLEND_FACTOR_DST_COLOR
227  *    dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
228  *    dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
229  *    dali.BLEND_FACTOR_CONSTANT_COLOR
230  *    dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
231  *    dali.BLEND_FACTOR_CONSTANT_ALPHA
232  *    dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
233  *
234  *    renderer.setBlendFunc( dali.BLEND_FACTOR_CONSTANT_COLOR, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
235  *                           dali.BLEND_FACTOR_CONSTANT_ALPHA, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA );
236  */
237 void RendererApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
238 {
239   v8::Isolate* isolate = args.GetIsolate();
240   v8::HandleScope handleScope( isolate );
241
242   Renderer renderer = GetRenderer( isolate, args );
243
244   int params[4];
245   bool foundAllParams(false);
246   V8Utils::ReadIntegerArguments( foundAllParams, &params[0], 4, args, 0 );
247   if( foundAllParams )
248   {
249     renderer.SetBlendFunc( static_cast< Dali::BlendingFactor::Type>(params[0]),
250                            static_cast< Dali::BlendingFactor::Type>(params[1]),
251                            static_cast< Dali::BlendingFactor::Type>(params[2]),
252                            static_cast< Dali::BlendingFactor::Type>(params[3]) );
253   }
254   else
255   {
256     DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter");
257   }
258 }
259
260 /**
261  * Query the pixel arithmetic used when the actor is blended.
262  *
263  * @for Renderer
264  * @method getBlendFunc
265  * @return {Object} Blend properties
266  * @example Blend properties object has 4 fields
267  *
268  *      blendProperties.sourceRgb // source rgb enum
269  *      blendProperties.destinationRgb // destination rgb enum
270  *      blendProperties.sourceAlpha source // alpha enum
271  *      blendProperties.destinationAlpha // destination alpha enum
272  */
273 void RendererApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
274 {
275   // Pass by reference doesn't work in Javascript
276   // For now just return a vector 4...
277
278   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
279   v8::Isolate* isolate = args.GetIsolate();
280   v8::HandleScope handleScope( isolate );
281
282   Renderer renderer = GetRenderer( isolate, args );
283
284   renderer.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
285
286   v8::Local<v8::Object> blendProperties = v8::Object::New( isolate );
287
288   blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ),        v8::Integer::New( isolate, srcFactorRgb) );
289   blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ),   v8::Integer::New( isolate, destFactorRgb ) );
290   blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ),      v8::Integer::New( isolate, srcFactorAlpha  ) );
291   blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
292
293   args.GetReturnValue().Set( blendProperties );
294 }
295
296 /**
297  * Specify the equation used when the actor is blended.
298  *
299  * @for Renderer
300  * @method setBlendEquation
301  * @param { integer } equationRgb The equation used for combining red, green, and blue components.
302  * @param { integer } equationAlpha The equation used for combining the alpha component.
303  * @example
304  *      // blend equation is one of the following
305  *      dali.BLEND_EQUATION_ADD
306  *      dali.BLEND_EQUATION_SUBTRACT
307  *      dali.BLEND_EQUATION_REVERSE_SUBTRACT
308  *
309  *      renderer.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT );
310  */
311 void RendererApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
312 {
313   v8::Isolate* isolate = args.GetIsolate();
314   v8::HandleScope handleScope( isolate );
315
316   Renderer renderer = GetRenderer( isolate, args );
317
318   int params[2];
319   bool foundAllParams(false);
320   V8Utils::ReadIntegerArguments( foundAllParams, &params[0], 2, args, 0 );
321   if( foundAllParams )
322   {
323     renderer.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) );
324   }
325   else
326   {
327     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter");
328   }
329 }
330
331 /**
332  * Query the equation used when the actor is blended.
333  *
334  * @for Renderer
335  * @method getBlendEquation
336  * @return {Object} Blend equations
337  * @example Blend equations object has 2 fields
338  *
339  *      blendEquations.equationRgb // equation used for combining rgb components
340  *      blendEquations.equationAlpha // equation used for combining alpha components
341  */
342 void RendererApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
343 {
344   // Pass by reference doesn't work in Javascript
345   // For now just return a vector 2...
346
347   BlendingEquation::Type equationRgb, equationAlpha;
348   v8::Isolate* isolate = args.GetIsolate();
349   v8::HandleScope handleScope( isolate );
350
351   Renderer renderer = GetRenderer( isolate, args );
352
353   renderer.GetBlendEquation( equationRgb, equationAlpha );
354
355   v8::Local<v8::Object> blendEquations = v8::Object::New( isolate );
356
357   blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationRgb" ),   v8::Integer::New( isolate, equationRgb) );
358   blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationAlpha" ), v8::Integer::New( isolate, equationAlpha ) );
359
360   args.GetReturnValue().Set( blendEquations );
361 }
362
363 } // namespace V8Plugin
364
365 } // namespace Dali