Revert "[Tizen] Fix build errors in adaptor-uv by ecore wayland"
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / emscripten / emscripten-callbacks.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 #include "emscripten-callbacks.h"
19
20 // EXTERNAL INCLUDES
21 #include <vector>
22 #include <dali/public-api/dali-core.h>
23 #include "emscripten/emscripten.h"
24 #include "emscripten/bind.h"
25 #include "emscripten/val.h"
26
27 // INTERNAL INCLUDES
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Emscripten
34 {
35
36 Statistics stats;
37
38 // Javascript callbacks
39
40 // Allows the adaptor to get a glyph image from the browser
41 emscripten::val JSGetGlyphImage(emscripten::val::null());
42
43 // Allows the adaptor to request an image from the browser
44 emscripten::val JSGetImage(emscripten::val::null());
45
46 // Allows the adaptor to get image meta data
47 emscripten::val JSGetImageMetaData(emscripten::val::null());
48
49 // Signals to the browser the end of rendering
50 emscripten::val JSRenderFinished(emscripten::val::null());
51
52
53 Integration::BitmapPtr GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character )
54 {
55   Integration::BitmapPtr ret;
56
57   // causes exception in browser if callback isnt set to a function;
58   emscripten::val val = JSGetGlyphImage(fontFamily,
59                                         fontStyle,
60                                         fontSize,
61                                         character);
62
63   std::vector<unsigned char> data = emscripten::vecFromJSArray<unsigned char>(val);
64
65   int step = fontSize * 4;
66
67   if( data.size() )
68   {
69     Integration::Bitmap* bitmap = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,
70                                                            ResourcePolicy::OWNED_DISCARD); // dali manages buffer
71
72     if(bitmap)
73     {
74       Integration::Bitmap::PackedPixelsProfile* profile = bitmap->GetPackedPixelsProfile();
75
76       if(profile)
77       {
78         std::vector<unsigned char> *buffer = new std::vector<unsigned char>;
79
80         buffer->reserve( fontSize * fontSize );
81
82         // take only alpha
83         for(int y = 0; y < fontSize; ++y)
84         {
85           for(int x = 0; x < step; x+=4)
86           {
87             buffer->push_back(data[ x + (y*step) +3]);
88           }
89         }
90
91         if( buffer )
92         {
93           profile->AssignBuffer(Pixel::A8,
94                                 &(*buffer)[0],
95                                 (*buffer).size(),
96                                 fontSize,
97                                 fontSize);
98         }
99
100         ret = Integration::BitmapPtr( bitmap );
101       }
102       else
103       {
104         printf("bitmap has no packedpixelsprofile\n");
105       }
106     }
107     else
108     {
109       printf("bitmap not created\n");
110     }
111   }
112   else
113   {
114     printf("Image data from javascript is empty\n");
115   }
116
117   return ret;
118 }
119
120
121 Integration::BitmapPtr GetImage( const Dali::ImageDimensions& size,
122                                  const Dali::FittingMode::Type& scalingMode,
123                                  const Dali::SamplingMode::Type& samplingMode,
124                                  const bool orientationCorrection,
125                                  const std::string& filename )
126 {
127   Integration::BitmapPtr ret;
128
129   // causes exception in browser if callback isnt set to a function;
130   emscripten::val val = JSGetImage(filename);
131
132   emscripten::val array = val["array"];
133   int w     = val["x"].as<int>();
134   int h     = val["y"].as<int>();
135
136   std::vector<unsigned char> data = emscripten::vecFromJSArray<unsigned char>(array);
137
138   Integration::Bitmap* bitmap = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,
139                                                          Dali::ResourcePolicy::OWNED_DISCARD); // dali manages buffer
140
141   if(bitmap)
142   {
143     Integration::Bitmap::PackedPixelsProfile* profile = bitmap->GetPackedPixelsProfile();
144
145     if(profile)
146     {
147       std::vector<unsigned char> *buffer = new std::vector<unsigned char>(data.begin(), data.end());
148
149       if( buffer )
150       {
151         profile->AssignBuffer(Pixel::RGBA8888,
152                               &(*buffer)[0],
153                               (*buffer).size(),
154                               w,
155                               h);
156       }
157
158       ret = Integration::BitmapPtr( bitmap );
159     }
160     else
161     {
162       printf("bitmap has no packedpixelsprofile\n");
163     }
164   }
165   else
166   {
167     printf("bitmap not created\n");
168   }
169
170   return ret;
171 }
172
173 Dali::ImageDimensions LoadImageMetadata(const std::string filename,
174                                         Dali::ImageDimensions& size,
175                                         Dali::FittingMode::Type fittingMode,
176                                         Dali::SamplingMode::Type samplingMode,
177                                         bool orientationCorrection )
178 {
179   emscripten::val val = JSGetImageMetaData(filename);
180
181   // @todo
182   // size.x = val["w"]
183   // size.y = val["h"]
184   //
185   return Dali::ImageDimensions();
186 }
187
188 void RenderFinished()
189 {
190   if (JSRenderFinished.typeof().as<std::string>() == "function")
191   {
192     emscripten::val val = JSRenderFinished();
193   }
194 }
195
196
197 }; // namespace Emscripten
198 }; // namespace Internal
199 }; // namespace Dali
200