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/texture-set-api.h>
31 #include <rendering/texture-set-wrapper.h>
32 #include <rendering/shader-api.h>
43 * Renderer is a handle to an object that can be used to provide an image to a material.
49 Renderer RendererApi::GetRenderer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
51 v8::HandleScope handleScope( isolate );
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();
57 RendererWrapper* wrapper = static_cast< RendererWrapper *>(ptr);
58 return wrapper->GetRenderer();
61 Renderer RendererApi::GetRendererFromParams( int paramIndex,
64 const v8::FunctionCallbackInfo< v8::Value >& args )
68 v8::HandleScope handleScope( isolate );
69 BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDERER, isolate, args );
73 RendererWrapper* wrapper = static_cast< RendererWrapper *>(wrappedObject);
74 return wrapper->GetRenderer();
83 * Create a new renderer object.
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
92 Renderer RendererApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
94 v8::Isolate* isolate = args.GetIsolate();
95 v8::HandleScope handleScope( isolate );
98 Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
101 DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
106 Shader shader = ShaderApi::GetShaderFromParams( 1, found, isolate, args );
109 DALI_SCRIPT_EXCEPTION( isolate, "missing shader from param 0" );
113 return Renderer::New(geometry, shader);
117 * Sets the geometry to be used by this renderer
119 * @method setGeometry
121 * @param {Object} geometry The geometry to be used by this renderer
123 void RendererApi::SetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args )
125 v8::Isolate* isolate = args.GetIsolate();
126 v8::HandleScope handleScope( isolate );
128 Renderer renderer = GetRenderer( isolate, args );
131 Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args );
134 DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" );
138 renderer.SetGeometry(geometry);
143 * Gets the geometry used by this renderer
145 * @method getGeometry
147 * @return {Object} The geometry used by this renderer
149 void RendererApi::GetGeometry( const v8::FunctionCallbackInfo<v8::Value>& args )
151 v8::Isolate* isolate = args.GetIsolate();
152 v8::HandleScope handleScope( isolate );
154 Renderer renderer = GetRenderer( isolate, args );
155 Geometry geometry = renderer.GetGeometry();
158 v8::Local<v8::Object> localObject = GeometryWrapper::WrapGeometry( isolate, geometry );
159 args.GetReturnValue().Set( localObject );
163 * Sets the texture set to be used by this renderer
165 * @method setTextures
167 * @param {Object} textureSet The TextureSet to be used by this renderer
169 void RendererApi::SetTextures( const v8::FunctionCallbackInfo< v8::Value >& args )
171 v8::Isolate* isolate = args.GetIsolate();
172 v8::HandleScope handleScope( isolate );
174 Renderer renderer = GetRenderer( isolate, args );
177 TextureSet textureSet = TextureSetApi::GetTextureSetFromParams( 0, found, isolate, args );
180 DALI_SCRIPT_EXCEPTION( isolate, "missing texture set from param 0" );
184 renderer.SetTextures(textureSet);
189 * Gets the texture set used by this renderer
191 * @method getTextures
193 * @return {Object} The texture set used by this renderer
195 void RendererApi::GetTextures( const v8::FunctionCallbackInfo<v8::Value>& args )
197 v8::Isolate* isolate = args.GetIsolate();
198 v8::HandleScope handleScope( isolate );
200 Renderer renderer = GetRenderer( isolate, args );
201 TextureSet textureSet = renderer.GetTextures();
203 // Wrap the textureset
204 v8::Local<v8::Object> localObject = TextureSetWrapper::WrapTextureSet( isolate, textureSet );
205 args.GetReturnValue().Set( localObject );
209 * Specify the pixel arithmetic used when the actor is blended.
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
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
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 );
238 void RendererApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
240 v8::Isolate* isolate = args.GetIsolate();
241 v8::HandleScope handleScope( isolate );
243 Renderer renderer = GetRenderer( isolate, args );
246 bool foundAllParams(false);
247 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 4, args, 0 );
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]) );
257 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter");
262 * Query the pixel arithmetic used when the actor is blended.
265 * @method getBlendFunc
266 * @return {Object} Blend properties
267 * @example Blend properties object has 4 fields
269 * blendProperties.sourceRgb // source rgb enum
270 * blendProperties.destinationRgb // destination rgb enum
271 * blendProperties.sourceAlpha source // alpha enum
272 * blendProperties.destinationAlpha // destination alpha enum
274 void RendererApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
276 // Pass by reference doesn't work in Javascript
277 // For now just return a vector 4...
279 BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
280 v8::Isolate* isolate = args.GetIsolate();
281 v8::HandleScope handleScope( isolate );
283 Renderer renderer = GetRenderer( isolate, args );
285 renderer.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
287 v8::Local<v8::Object> blendProperties = v8::Object::New( isolate );
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 ) );
294 args.GetReturnValue().Set( blendProperties );
298 * Specify the equation used when the actor is blended.
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.
305 * // blend equation is one of the following
306 * dali.BLEND_EQUATION_ADD
307 * dali.BLEND_EQUATION_SUBTRACT
308 * dali.BLEND_EQUATION_REVERSE_SUBTRACT
310 * renderer.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT );
312 void RendererApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
314 v8::Isolate* isolate = args.GetIsolate();
315 v8::HandleScope handleScope( isolate );
317 Renderer renderer = GetRenderer( isolate, args );
320 bool foundAllParams(false);
321 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 2, args, 0 );
324 renderer.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) );
328 DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter");
333 * Query the equation used when the actor is blended.
336 * @method getBlendEquation
337 * @return {Object} Blend equations
338 * @example Blend equations object has 2 fields
340 * blendEquations.equationRgb // equation used for combining rgb components
341 * blendEquations.equationAlpha // equation used for combining alpha components
343 void RendererApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
345 // Pass by reference doesn't work in Javascript
346 // For now just return a vector 2...
348 BlendingEquation::Type equationRgb, equationAlpha;
349 v8::Isolate* isolate = args.GetIsolate();
350 v8::HandleScope handleScope( isolate );
352 Renderer renderer = GetRenderer( isolate, args );
354 renderer.GetBlendEquation( equationRgb, equationAlpha );
356 v8::Local<v8::Object> blendEquations = v8::Object::New( isolate );
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 ) );
361 args.GetReturnValue().Set( blendEquations );
364 } // namespace V8Plugin