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 "render-task-api.h"
24 #include <render-tasks/render-task-wrapper.h>
25 #include <image/image-wrapper.h>
26 #include <actors/actor-wrapper.h>
27 #include <dali/public-api/render-tasks/render-task.h>
28 #include <dali/public-api/actors/camera-actor.h>
37 namespace // un named namespace
41 RenderTask GetRenderTask( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
43 v8::HandleScope handleScope( isolate );
45 v8::Local<v8::Object> object = args.This();
46 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
47 void* ptr = field->Value();
49 RenderTaskWrapper* wrapper = static_cast< RenderTaskWrapper *>(ptr);
50 return wrapper->GetRenderTask();
56 * Set the actors to be rendered
57 * @method setSourceActor
59 * @param {Object} actor This actor and its children will be rendered.
61 void RenderTaskApi::SetSourceActor( const v8::FunctionCallbackInfo< v8::Value >& args )
63 v8::Isolate* isolate = args.GetIsolate();
64 v8::HandleScope handleScope( isolate );
65 RenderTask renderTask = GetRenderTask( isolate, args );
67 Actor sourceActor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
71 renderTask.SetSourceActor( sourceActor );
75 DALI_SCRIPT_EXCEPTION( isolate, "source actor not found" );
80 * Get the actor to be rendered
81 * @method getSourceActor
83 * @param {Object} actor source actor
85 void RenderTaskApi::GetSourceActor(const v8::FunctionCallbackInfo< v8::Value >& args )
87 v8::Isolate* isolate = args.GetIsolate();
88 v8::HandleScope handleScope( isolate );
89 RenderTask renderTask = GetRenderTask( isolate, args );
91 Actor source = renderTask.GetSourceActor();
93 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, source );
94 args.GetReturnValue().Set( wrappedActor );
98 * Set whether the RenderTask has exclusive access to the source actors; the default is false.
99 * @method setExclusive
101 * @param {Boolean} exclusive True if the source actors will only be rendered by this render-task.
103 void RenderTaskApi::SetExclusive( const v8::FunctionCallbackInfo< v8::Value >& args )
105 v8::Isolate* isolate = args.GetIsolate();
106 v8::HandleScope handleScope( isolate );
107 RenderTask renderTask = GetRenderTask( isolate, args );
110 bool exclusive = V8Utils::GetBooleanParameter( PARAMETER_0, found, isolate, args );
113 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
116 renderTask.SetExclusive( exclusive );
121 * Query whether the RenderTask has exclusive access to the source actors.
122 * @method isExclusive
124 * @return {Boolean} exclusive True if the source actors will only be rendered by this render-task.
126 void RenderTaskApi::IsExclusive( const v8::FunctionCallbackInfo< v8::Value >& args )
128 v8::Isolate* isolate = args.GetIsolate();
129 v8::HandleScope handleScope( isolate );
130 RenderTask renderTask = GetRenderTask( isolate, args );
132 args.GetReturnValue().Set( v8::Boolean::New( isolate, renderTask.IsExclusive() ) );
137 * Set whether the render-task should be considered for input handling; the default is true.
139 * The task used for input handling will be last task in the RenderTaskList which has input enabled,
140 * and has a valid source & camera actor.
141 * A RenderTask targetting a frame-buffer can still be hit-tested, provided that the screen->frame-buffer
142 * coordinate conversion is successful; see also SetScreenToFrameBufferFunction().
143 * @method setInputEnabled
145 * @param {Boolean} enabled True if the render-task should be considered for input handling.
147 void RenderTaskApi::SetInputEnabled( const v8::FunctionCallbackInfo< v8::Value >& args )
149 v8::Isolate* isolate = args.GetIsolate();
150 v8::HandleScope handleScope( isolate );
151 RenderTask renderTask = GetRenderTask( isolate, args );
154 bool inputEnabled = V8Utils::GetBooleanParameter( PARAMETER_0, found, isolate, args );
157 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
160 renderTask.SetInputEnabled( inputEnabled );
165 * Query whether the render-task should be considered for input handling.
166 * @method getInputEnabled
168 * @return {Boolean} enabled True if the render-task should be considered for input handling.
170 void RenderTaskApi::GetInputEnabled( const v8::FunctionCallbackInfo< v8::Value >& args )
172 v8::Isolate* isolate = args.GetIsolate();
173 v8::HandleScope handleScope( isolate );
174 RenderTask renderTask = GetRenderTask( isolate, args );
176 args.GetReturnValue().Set( v8::Boolean::New( isolate, renderTask.GetInputEnabled() ) );
182 * Set the actor from which the scene is viewed.
183 * @method setCameraActor
185 * @param {Object} cameraActor The scene is viewed from the perspective of this actor.
187 void RenderTaskApi::SetCameraActor( const v8::FunctionCallbackInfo< v8::Value >& args )
189 v8::Isolate* isolate = args.GetIsolate();
190 v8::HandleScope handleScope( isolate );
191 RenderTask renderTask = GetRenderTask( isolate, args );
193 CameraActor cameraActor;
194 Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
197 cameraActor = CameraActor::DownCast( actor );
202 renderTask.SetCameraActor( cameraActor );
206 DALI_SCRIPT_EXCEPTION( isolate, "source actor not found" );
212 * Retrieve the actor from which the scene is viewed..
213 * @method getCameraActor
215 * @return {Object} cameraActor The scene is viewed from the perspective of this actor.
217 void RenderTaskApi::GetCameraActor( const v8::FunctionCallbackInfo< v8::Value >& args )
219 v8::Isolate* isolate = args.GetIsolate();
220 v8::HandleScope handleScope( isolate );
221 RenderTask renderTask = GetRenderTask( isolate, args );
223 Actor cameraActor = renderTask.GetSourceActor();
225 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, cameraActor );
226 args.GetReturnValue().Set( wrappedActor );
231 * Set the frame-buffer used as a render target.
232 * @method setTargetFrameBuffer
234 * @param {Object} frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
236 void RenderTaskApi::SetTargetFrameBuffer(const v8::FunctionCallbackInfo< v8::Value >& args )
238 v8::Isolate* isolate = args.GetIsolate();
239 v8::HandleScope handleScope( isolate );
240 RenderTask renderTask = GetRenderTask( isolate, args );
243 // @TODO Test this, may need to implement a Framebuffer image.?
244 Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
247 DALI_SCRIPT_EXCEPTION( isolate, "bad parameters" );
251 FrameBufferImage frameBufferImage = FrameBufferImage::DownCast(image);
253 renderTask.SetTargetFrameBuffer( frameBufferImage );
259 * Get the frame-buffer used as a render target.
260 * @method getTargetFrameBuffer
262 * @return {Object} frameBuffer A valid frame-buffer handle to enable off-screen rendering, or an uninitialized handle to disable.
264 void RenderTaskApi::GetTargetFrameBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
266 v8::Isolate* isolate = args.GetIsolate();
267 v8::HandleScope handleScope( isolate );
268 RenderTask renderTask = GetRenderTask( isolate, args );
271 // @TODO Test this, may need to implement a Framebuffer image wrapper.?
272 v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, renderTask.GetTargetFrameBuffer(),ImageWrapper::FRAME_BUFFER_IMAGE );
273 args.GetReturnValue().Set( localObject );
278 * Set the actor used to convert screen coordinates to frame-buffer coordinates.
280 * The local coordinates of the actor are mapped as frame-buffer coordinates.
281 * This is useful for hit-testing actors which are rendered off-screen.
282 * Note: The mapping actor needs to be rendered by the default render task to make the mapping work properly.
283 * @method setScreenToFrameBufferMappingActor
285 * @param {Object} mappingActor The actor used for conversion.
287 void RenderTaskApi::SetScreenToFrameBufferMappingActor( const v8::FunctionCallbackInfo< v8::Value >& args )
289 v8::Isolate* isolate = args.GetIsolate();
290 v8::HandleScope handleScope( isolate );
291 RenderTask renderTask = GetRenderTask( isolate, args );
293 Actor sourceActor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
297 renderTask.SetScreenToFrameBufferMappingActor( sourceActor );
301 DALI_SCRIPT_EXCEPTION( isolate, "source actor not found" );
306 * Retrieve the actor used to convert screen coordinates to frame-buffer coordinates.
308 * @method getScreenToFrameBufferMappingActor
310 * @return {Object} actor used for conversion.
312 void RenderTaskApi::GetScreenToFrameBufferMappingActor( const v8::FunctionCallbackInfo< v8::Value >& args )
314 v8::Isolate* isolate = args.GetIsolate();
315 v8::HandleScope handleScope( isolate );
316 RenderTask renderTask = GetRenderTask( isolate, args );
318 Actor source = renderTask.GetScreenToFrameBufferMappingActor();
320 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, source );
321 args.GetReturnValue().Set( wrappedActor );
325 * Retrieve the GL viewport used when rendering.
327 * @method getViewport
329 * @return {Object} object with the properties { x,y,width,height }
331 void RenderTaskApi::GetViewport( const v8::FunctionCallbackInfo< v8::Value >& args )
333 v8::Isolate* isolate = args.GetIsolate();
334 v8::HandleScope handleScope( isolate );
335 RenderTask renderTask = GetRenderTask( isolate, args );
337 v8::Local<v8::Object> viewportObject = v8::Object::New( isolate );
339 Viewport viewPort = renderTask.GetViewport();
341 viewportObject->Set( v8::String::NewFromUtf8( isolate, "x"), v8::Number::New( isolate,viewPort.x ));
342 viewportObject->Set( v8::String::NewFromUtf8( isolate, "y"), v8::Number::New( isolate,viewPort.y ));
343 viewportObject->Set( v8::String::NewFromUtf8( isolate, "width"), v8::Number::New( isolate,viewPort.width ));
344 viewportObject->Set( v8::String::NewFromUtf8( isolate, "height"), v8::Number::New( isolate,viewPort.height ));
346 args.GetReturnValue().Set( viewportObject );
351 * Set the GL viewport used when rendering.
352 * This specifies the transformation between normalized device coordinates and target window (or frame-buffer) coordinates.
353 * By default this will match the target window or frame-buffer size.
354 * Unlike the glViewport method, the x & y coordinates refer to the top-left of the viewport rectangle.
355 * @method setViewport
357 * @param {Object} object with the properties { x,y,width,height }
359 void RenderTaskApi::SetViewport( const v8::FunctionCallbackInfo< v8::Value >& args )
361 v8::Isolate* isolate = args.GetIsolate();
362 v8::HandleScope handleScope( isolate );
363 RenderTask renderTask = GetRenderTask( isolate, args );
365 if( args.Length() != 1 )
367 DALI_SCRIPT_EXCEPTION( isolate, "missing / invalid parameters" );
370 v8::Local<v8::Value > viewportValue = args[0];
371 if( !viewportValue->IsObject() )
373 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters" );
376 v8::Local<v8::Object> obj = viewportValue->ToObject();
377 v8::Local<v8::Value> xValue = obj->Get( v8::String::NewFromUtf8( isolate, "x" ) );
378 v8::Local<v8::Value> yValue = obj->Get( v8::String::NewFromUtf8( isolate, "y" ) );
379 v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
380 v8::Local<v8::Value> heightValue = obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
382 if( xValue->IsNumber() && yValue->IsNumber() && widthValue->IsNumber() && heightValue->IsNumber())
385 viewPort.x= xValue->ToNumber()->Value();
386 viewPort.y = yValue->ToNumber()->Value();
387 viewPort.width = widthValue->ToNumber()->Value();
388 viewPort.height = heightValue->ToNumber()->Value();
389 renderTask.SetViewport( viewPort );
393 DALI_SCRIPT_EXCEPTION( isolate, "missing x,y,width,height property" );
399 * Set whether the render-task will clear the results of previous render-tasks.
401 * The default is false.
403 * The default GL surface is cleared automatically at the
404 * beginning of each frame; this setting is only useful when 2+
405 * render-tasks are used, and the result of the first task needs to
406 * be (partially) cleared before rendering the second. * @method setViewport
408 * @method setClearEnabled
409 * @param {Boolean} enabled True if the render-task should clear
411 void RenderTaskApi::SetClearEnabled( const v8::FunctionCallbackInfo< v8::Value >& args )
413 v8::Isolate* isolate = args.GetIsolate();
414 v8::HandleScope handleScope( isolate );
415 RenderTask renderTask = GetRenderTask( isolate, args );
418 bool clearEnabled = V8Utils::GetBooleanParameter( PARAMETER_0, found, isolate, args );
421 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
424 renderTask.SetClearEnabled( clearEnabled );
429 * Query whether the render-task will clear the results of previous render-tasks.
432 * @method getClearEnabled
433 * @return {Boolean} True if the render-task should clear.
435 void RenderTaskApi::GetClearEnabled( const v8::FunctionCallbackInfo< v8::Value >& args )
437 v8::Isolate* isolate = args.GetIsolate();
438 v8::HandleScope handleScope( isolate );
439 RenderTask renderTask = GetRenderTask( isolate, args );
441 args.GetReturnValue().Set( v8::Boolean::New( isolate, renderTask.GetClearEnabled() ) );
445 * Set whether the render task will cull the actors to the camera's view frustum.
446 * Note that this will only affect image actors that use the default vertex shader.
447 * The default mode is to cull actors.
449 * @method setCullMode
450 * @param {Boolean} cullMode True if the renderers should be culled.
452 void RenderTaskApi::SetCullMode ( const v8::FunctionCallbackInfo< v8::Value >& args )
454 v8::Isolate* isolate = args.GetIsolate();
455 v8::HandleScope handleScope( isolate );
456 RenderTask renderTask = GetRenderTask( isolate, args );
459 bool cullMode = V8Utils::GetBooleanParameter( PARAMETER_0, found, isolate, args );
462 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
465 renderTask.SetCullMode( cullMode );
471 * @method getCullMode
472 * @return {Boolean} cullMode True if the renderers should be culled.
474 void RenderTaskApi::GetCullMode( const v8::FunctionCallbackInfo< v8::Value >& args )
476 v8::Isolate* isolate = args.GetIsolate();
477 v8::HandleScope handleScope( isolate );
478 RenderTask renderTask = GetRenderTask( isolate, args );
480 args.GetReturnValue().Set( v8::Boolean::New( isolate, renderTask.GetCullMode() ) );
484 * Set the refresh-rate of the RenderTask.
486 * The default is dali.REFRESH_ALWAYS (1), meaning that the RenderTask will be processed every frame.
487 * It may be desirable to process less frequently e.g. SetRefreshRate(3) will process once every 3 frames.
488 * The dali.REFRESH_ONCE value means that the RenderTask will be processed once only, to take a snap-shot of the scene.
489 * Repeatedly calling setRefreshRate(dali.REFRESH_ONCE) will cause more snap-shots to be taken.
490 * @method setRefreshRate
491 * @param {Integer} refresh rate (0 = dali.REFRESH_ONCE, 1= dali.REFRESH_ALWAYS)
493 void RenderTaskApi::SetRefreshRate( const v8::FunctionCallbackInfo< v8::Value >& args )
495 v8::Isolate* isolate = args.GetIsolate();
496 v8::HandleScope handleScope( isolate );
497 RenderTask renderTask = GetRenderTask( isolate, args );
500 int rate = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
503 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
506 renderTask.SetRefreshRate( rate );
510 * Query the refresh-rate of the RenderTask.
511 * @method getRefreshRate
512 * @return {Integer} refresh rate (0 = dali.REFRESH_ONCE, 1= dali.REFRESH_ALWAYS)
514 void RenderTaskApi::GetRefreshRate( const v8::FunctionCallbackInfo< v8::Value >& args )
516 v8::Isolate* isolate = args.GetIsolate();
517 v8::HandleScope handleScope( isolate );
518 RenderTask renderTask = GetRenderTask( isolate, args );
520 args.GetReturnValue().Set( v8::Integer::New( isolate, renderTask.GetRefreshRate() ) );
524 } // namespace V8Plugin