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.
20 #include "renderer-api.h"
23 #include <dali/public-api/object/type-registry.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>
42 * Renderer is a handle to an object that can be used to provide an image to a material.
48 Renderer RendererApi::GetRenderer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
50 v8::HandleScope handleScope( isolate );
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();
56 RendererWrapper* wrapper = static_cast< RendererWrapper *>(ptr);
57 return wrapper->GetRenderer();
60 Renderer RendererApi::GetRendererFromParams( int paramIndex,
63 const v8::FunctionCallbackInfo< v8::Value >& args )
67 v8::HandleScope handleScope( isolate );
68 BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDERER, isolate, args );
72 RendererWrapper* wrapper = static_cast< RendererWrapper *>(wrappedObject);
73 return wrapper->GetRenderer();
82 * Create a new renderer object.
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
91 Renderer RendererApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
93 v8::Isolate* isolate = args.GetIsolate();
94 v8::HandleScope handleScope( isolate );
97 Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
100 DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
105 Material material = MaterialApi::GetMaterialFromParams( 1, found, isolate, args );
108 DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" );
112 return Renderer::New(geometry, material);
116 * Sets the geometry to be used by this renderer
118 * @method setGeometry
120 * @param {Object} geometry The geometry to be used by this renderer
122 void RendererApi::SetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args )
124 v8::Isolate* isolate = args.GetIsolate();
125 v8::HandleScope handleScope( isolate );
127 Renderer renderer = GetRenderer( isolate, args );
130 Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
133 DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
137 renderer.SetGeometry(geometry);
142 * Gets the geometry used by this renderer
144 * @method getGeometry
146 * @return {Object} The geometry used by this renderer
148 void RendererApi::GetGeometry( const v8::FunctionCallbackInfo<v8::Value>& args )
150 v8::Isolate* isolate = args.GetIsolate();
151 v8::HandleScope handleScope( isolate );
153 Renderer renderer = GetRenderer( isolate, args );
154 Geometry geometry = renderer.GetGeometry();
157 v8::Local<v8::Object> localObject = GeometryWrapper::WrapGeometry( isolate, geometry );
158 args.GetReturnValue().Set( localObject );
162 * Sets the material to be used by this renderer
164 * @method setMaterial
166 * @param {Object} material The material to be used by this renderer
168 void RendererApi::SetMaterial( const v8::FunctionCallbackInfo< v8::Value >& args )
170 v8::Isolate* isolate = args.GetIsolate();
171 v8::HandleScope handleScope( isolate );
173 Renderer renderer = GetRenderer( isolate, args );
176 Material material = MaterialApi::GetMaterialFromParams( 0, found, isolate, args );
179 DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" );
183 renderer.SetMaterial(material);
188 * Gets the material used by this renderer
190 * @method getMaterial
192 * @return {Object} The material used by this renderer
194 void RendererApi::GetMaterial( const v8::FunctionCallbackInfo<v8::Value>& args )
196 v8::Isolate* isolate = args.GetIsolate();
197 v8::HandleScope handleScope( isolate );
199 Renderer renderer = GetRenderer( isolate, args );
200 Material material = renderer.GetMaterial();
203 v8::Local<v8::Object> localObject = MaterialWrapper::WrapMaterial( isolate, material );
204 args.GetReturnValue().Set( localObject );
208 * Specify the pixel arithmetic used when the actor is blended.
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
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
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 );
237 void RendererApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
239 v8::Isolate* isolate = args.GetIsolate();
240 v8::HandleScope handleScope( isolate );
242 Renderer renderer = GetRenderer( isolate, args );
245 bool foundAllParams(false);
246 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 4, args, 0 );
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]) );
256 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter");
261 * Query the pixel arithmetic used when the actor is blended.
264 * @method getBlendFunc
265 * @return {Object} Blend properties
266 * @example Blend properties object has 4 fields
268 * blendProperties.sourceRgb // source rgb enum
269 * blendProperties.destinationRgb // destination rgb enum
270 * blendProperties.sourceAlpha source // alpha enum
271 * blendProperties.destinationAlpha // destination alpha enum
273 void RendererApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
275 // Pass by reference doesn't work in Javascript
276 // For now just return a vector 4...
278 BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
279 v8::Isolate* isolate = args.GetIsolate();
280 v8::HandleScope handleScope( isolate );
282 Renderer renderer = GetRenderer( isolate, args );
284 renderer.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
286 v8::Local<v8::Object> blendProperties = v8::Object::New( isolate );
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 ) );
293 args.GetReturnValue().Set( blendProperties );
297 * Specify the equation used when the actor is blended.
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.
304 * // blend equation is one of the following
305 * dali.BLEND_EQUATION_ADD
306 * dali.BLEND_EQUATION_SUBTRACT
307 * dali.BLEND_EQUATION_REVERSE_SUBTRACT
309 * renderer.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT );
311 void RendererApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
313 v8::Isolate* isolate = args.GetIsolate();
314 v8::HandleScope handleScope( isolate );
316 Renderer renderer = GetRenderer( isolate, args );
319 bool foundAllParams(false);
320 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 2, args, 0 );
323 renderer.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) );
327 DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter");
332 * Query the equation used when the actor is blended.
335 * @method getBlendEquation
336 * @return {Object} Blend equations
337 * @example Blend equations object has 2 fields
339 * blendEquations.equationRgb // equation used for combining rgb components
340 * blendEquations.equationAlpha // equation used for combining alpha components
342 void RendererApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
344 // Pass by reference doesn't work in Javascript
345 // For now just return a vector 2...
347 BlendingEquation::Type equationRgb, equationAlpha;
348 v8::Isolate* isolate = args.GetIsolate();
349 v8::HandleScope handleScope( isolate );
351 Renderer renderer = GetRenderer( isolate, args );
353 renderer.GetBlendEquation( equationRgb, equationAlpha );
355 v8::Local<v8::Object> blendEquations = v8::Object::New( isolate );
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 ) );
360 args.GetReturnValue().Set( blendEquations );
363 } // namespace V8Plugin