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