Remove JavaScript binding for NativeImage, NinePatchImage, ImageActor and ShaderEffect
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / 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 "buffer-image-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <image/image-wrapper.h>
24
25
26 namespace Dali
27 {
28
29 namespace V8Plugin
30 {
31
32 /**
33  * ## BufferImage
34  * Bitmap represents an image resource that can be added to Material.
35  * Its pixel buffer data is provided by the application developer.
36  *
37  * If the pixel format of the pixel buffer contains an alpha channel,
38  * then the image is considered to be have transparent pixels without
39  * regard for the actual content of the channel, and will be blended.
40  * @class BufferImage
41  * @extends Image
42  */
43 BufferImage BufferImageApi::GetBufferImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
44 {
45   v8::HandleScope handleScope( isolate );
46
47   v8::Local<v8::Object> object = args.This();
48   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
49   void* ptr = field->Value();
50
51   ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
52   return BufferImage::DownCast( wrapper->GetImage() );
53 }
54
55 /**
56  * Create a new buffer image object ** ( work in progress, will currently only work with no parameters ) **
57  * If no parameters are passed in, a single pixel white buffer image is created.
58  *
59  *
60  * For better performance and portability use power of two dimensions.
61  * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
62  *
63  * @note in case releasePolicy is "Unused", application has to call
64  * BufferImage::Update() whenever image is re-added to the stage
65  *
66  * Example of creating a Uint32Array buffer
67  * ```
68  * "var ab = new ArrayBuffer(256 x 256 );"
69  *  var pixelBuffer = new Uint32Array(ab );
70  * ```
71  * @constructor
72  * @method BufferImage
73  * @for BufferImage
74  * @param {Object} options
75  * @param {Uint32Array} options.pixelBuffer Array of RGBA pixel data
76  * @param {Integer} options.width image width
77  * @param {Integer} options.height image height
78  * @param {Integer} options.pixelFormat pixel format ( see dali constants, e.g. dali.PIXEL_FORMAT_RGB8888)
79  * @param {Integer} [options.stride the] internal stride of the pixelbuffer in pixels (normally the width)
80  * @param {Integer} [options.releasePolicy] optionally release memory when image is not visible on screen.
81  * @return {Object} Image
82  */
83 Image BufferImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
84 {
85   v8::Isolate* isolate = args.GetIsolate();
86   v8::HandleScope handleScope( isolate );
87   v8::Local<v8::Value> options( args[0] );
88   if( !options->IsObject() )
89   {
90     return BufferImage::WHITE();
91   }
92
93   v8::Local<v8::Object> obj = options->ToObject();
94
95   v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
96   v8::Local<v8::Value> heightValue= obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
97
98   unsigned int width = 0;
99   unsigned int height = 0;
100
101   if( widthValue->IsUint32() && heightValue->IsUint32() )
102   {
103     width = widthValue->ToUint32()->Value();
104     height = heightValue->ToUint32()->Value();
105   }
106   else
107   {
108     DALI_SCRIPT_EXCEPTION( isolate, "Missing valid width and height params");
109     return BufferImage();
110   }
111
112   Pixel::Format  pixelFormat = Pixel::RGB8888;
113   v8::Local<v8::Value> pixelFormatValue = obj->Get( v8::String::NewFromUtf8( isolate, "pixelFormat" ) );
114   if( pixelFormatValue->IsUint32() )
115   {
116      pixelFormat = static_cast<Pixel::Format>( pixelFormatValue->ToUint32()->Value() );
117   }
118   else
119   {
120     DALI_SCRIPT_EXCEPTION( isolate, "Pixel format not specified");
121     return BufferImage();
122   }
123
124   v8::Local<v8::Value> pixelArray= obj->Get( v8::String::NewFromUtf8( isolate, "pixelBuffer" ) );
125   if( pixelArray->IsUint32Array() )
126   {
127     //v8::Local<v8::Uint32Array> v8Array = v8::Local<v8::Uint32Array>::Cast( pixelArray );
128    // uint32_t elementCount = v8Array->Length();
129     DALI_SCRIPT_EXCEPTION( isolate, "pixel buffer currently not supported \n");
130     return BufferImage::WHITE();
131    }
132
133
134   unsigned int stride = width;
135   v8::Local<v8::Value> strideValue = obj->Get( v8::String::NewFromUtf8( isolate, "stride" ) );
136   if( strideValue->IsUint32() )
137   {
138     stride = strideValue->ToUint32()->Value();
139   }
140
141   Image::ReleasePolicy releasePolicy =  Dali::Image::NEVER;
142   v8::Local<v8::Value> releasePolicyValue = obj->Get( v8::String::NewFromUtf8( isolate, "releasePolicy" ) );
143   if( releasePolicyValue->IsUint32() )
144   {
145     releasePolicy = static_cast<Image::ReleasePolicy>( releasePolicyValue->ToUint32()->Value() );
146   }
147
148   DALI_SCRIPT_EXCEPTION( isolate, "pixel buffer currently not supported \n");
149
150   return BufferImage::New( NULL, width, height, pixelFormat, stride, releasePolicy);
151
152 }
153 /**
154  * @brief Returns the pixel buffer of the Image **( currently not supported ) **
155  * @method getBuffer
156  * @for BufferImage
157  * @return {Object}
158  */
159 void BufferImageApi::GetBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
160 {
161   v8::Isolate* isolate = args.GetIsolate();
162   v8::HandleScope handleScope( isolate );
163
164   BufferImage image = GetBufferImage( isolate, args );
165
166   //@todo figure out what the best thing to do here is...
167   // we could copy the data into a javascript array
168 }
169
170 /**
171  * @brief Returns buffer size in bytes.
172  * @method getBufferSize
173  * @for BufferImage
174  * @return {Object} buffer
175  */
176 void BufferImageApi::GetBufferSize( const v8::FunctionCallbackInfo< v8::Value >& args )
177 {
178   v8::Isolate* isolate = args.GetIsolate();
179   v8::HandleScope handleScope( isolate );
180
181   BufferImage image = GetBufferImage( isolate, args );
182
183   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferSize() ) );
184
185 }
186 /**
187  * @brief Returns buffer stride in bytes.
188  * @method getBufferStride
189  * @for BufferImage
190  * @return {Object}
191  */
192 void BufferImageApi::GetBufferStride( const v8::FunctionCallbackInfo< v8::Value >& args )
193 {
194   v8::Isolate* isolate = args.GetIsolate();
195   v8::HandleScope handleScope( isolate );
196
197   BufferImage image = GetBufferImage( isolate, args );
198
199   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferStride() ) );
200
201 }
202 /**
203  * @brief Returns pixel format
204  * @method getPixelFormat
205  * @for BufferImage
206  * @return {Integer} pixel format
207  */
208 void BufferImageApi::GetPixelFormat( const v8::FunctionCallbackInfo< v8::Value >& args )
209 {
210   v8::Isolate* isolate = args.GetIsolate();
211   v8::HandleScope handleScope( isolate );
212
213   BufferImage image = GetBufferImage( isolate, args );
214
215   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetPixelFormat() ) );
216
217 }
218
219 /**
220  * @brief  Inform Dali that the contents of the buffer have changed
221  * @todo support update an area
222  * @method update
223  * @for BufferImage
224  */
225 void BufferImageApi::Update( const v8::FunctionCallbackInfo< v8::Value >& args )
226 {
227   v8::Isolate* isolate = args.GetIsolate();
228   v8::HandleScope handleScope( isolate );
229
230   BufferImage image = GetBufferImage( isolate, args );
231
232   image.Update();
233
234 }
235 /**
236  * @brief  returns whether BufferImage uses external data source or not.
237  * @method isDataExternal
238  * @for BufferImage
239  * @return {Boolean} true if data is external
240  */
241 void BufferImageApi::IsDataExternal( const v8::FunctionCallbackInfo< v8::Value >& args )
242 {
243   v8::Isolate* isolate = args.GetIsolate();
244   v8::HandleScope handleScope( isolate );
245
246   BufferImage image = GetBufferImage( isolate, args );
247
248   args.GetReturnValue().Set( v8::Boolean::New( isolate, image.IsDataExternal() ) );
249
250 }
251 } // namespace V8Plugin
252
253 } // namespace Dali