252bf0dbf31dec394dd617397bc82788dd19a366
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / frame-buffer-image-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 // CLASS HEADER
19 #include "frame-buffer-image-api.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/frame-buffer-image.h>
23
24 // INTERNAL INCLUDES
25 #include <v8-utils.h>
26 #include <image/image-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 namespace FrameBufferImageApi
35 {
36
37 /**
38  * FrameBufferImage represents a GLES Frame Buffer Object and contains the result
39  * of an 'off screen' render pass of a RenderTask.
40  * The FrameBufferImage can then be used with a Material (with optional shader
41  * effects) and rendered to the screen.
42  * @class FrameBufferImage
43  * @extends Image
44  */
45
46 /**
47  * @constructor
48  * @method FrameBufferImage
49  * @for FrameBufferImage
50  * @param {Object} options
51  * @param {Uint32Array} options.pixelBuffer Array of RGBA pixel data
52  * @param {Integer} options.width image width
53  * @param {Integer} options.height image height
54  * @param {Object } [options.nativeImage] ** currently not supported **
55  * @param {Integer} [options.pixelFormat] pixel format ( see dali constants, e.g. dali.PIXEL_FORMAT_RGB8888)
56  * @param {Integer} [options.releasePolicy] optionally release memory when image is not visible on screen.
57  * @return {Object} Image
58  */
59 Image New( const v8::FunctionCallbackInfo< v8::Value >& args )
60 {
61   v8::Isolate* isolate = args.GetIsolate();
62   v8::HandleScope handleScope( isolate );
63
64   v8::Local<v8::Value> options( args[0] );
65   if( !options->IsObject() )
66   {
67     DALI_SCRIPT_EXCEPTION( isolate, "Missing param");
68     return FrameBufferImage();
69   }
70
71   v8::Local<v8::Object> obj = options->ToObject();
72
73   v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
74   v8::Local<v8::Value> heightValue= obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
75
76   unsigned int width = 0;
77   unsigned int height = 0;
78
79   if( widthValue->IsUint32() && heightValue->IsUint32() )
80   {
81     width = widthValue->ToUint32()->Value();
82     height = heightValue->ToUint32()->Value();
83   }
84   else
85   {
86     DALI_SCRIPT_EXCEPTION( isolate, "Missing valid width and height params");
87     return FrameBufferImage();
88   }
89
90   Pixel::Format  pixelFormat = Pixel::RGB8888;
91   v8::Local<v8::Value> pixelFormatValue = obj->Get( v8::String::NewFromUtf8( isolate, "pixelFormat" ) );
92   if( pixelFormatValue->IsUint32() )
93   {
94      pixelFormat = static_cast<Pixel::Format>( pixelFormatValue->ToUint32()->Value() );
95   }
96   else
97   {
98     DALI_SCRIPT_EXCEPTION( isolate, "Pixel format not specified");
99     return FrameBufferImage();
100   }
101
102   Image::ReleasePolicy releasePolicy =  Dali::Image::NEVER;
103   v8::Local<v8::Value> releasePolicyValue = obj->Get( v8::String::NewFromUtf8( isolate, "releasePolicy" ) );
104   if( releasePolicyValue->IsUint32() )
105   {
106     releasePolicy = static_cast<Image::ReleasePolicy>( releasePolicyValue->ToUint32()->Value() );
107   }
108
109   return FrameBufferImage::New( width, height, pixelFormat, releasePolicy );
110 }
111
112 } // FrameBufferImageApi
113
114 } // namespace V8Plugin
115
116 } // namespace Dali