2 * Copyright (c) 2017 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.
19 #include <dali/dali.h>
20 #include <dali/devel-api/images/native-image-interface-extension.h>
21 #include <dali-toolkit/dali-toolkit.h>
25 #include "shared/utility.h"
28 using namespace Toolkit;
33 const float BUTTON_HEIGHT = 100.0f;
34 const float BUTTON_COUNT = 5.0f;
36 const std::string JPG_FILENAME = DEMO_IMAGE_DIR "gallery-medium-4.jpg";
37 const std::string CAPTURE_FILENAME = "/tmp/native-image-capture.png";
40 * @brief Creates a shader used to render a native image
41 * @param[in] nativeImageInterface The native image interface
42 * @return A shader to render the native image
44 Shader CreateShader( NativeImageInterface& nativeImageInterface )
46 static const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
48 static const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
49 attribute mediump vec2 aPosition;\n
50 attribute mediump vec2 aTexCoord;\n
51 uniform mediump mat4 uMvpMatrix;\n
52 uniform mediump vec3 uSize;\n
53 varying mediump vec2 vTexCoord;\n
56 vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n
57 gl_Position = uMvpMatrix * position;\n
58 vTexCoord = aTexCoord;\n
62 static const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
63 uniform lowp vec4 uColor;\n
64 uniform sampler2D sTexture;\n
65 varying mediump vec2 vTexCoord;\n
69 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
73 NativeImageInterface::Extension* extension( nativeImageInterface.GetExtension() );
76 std::string fragmentShader;
78 //Get custom fragment shader prefix
79 const char* fragmentPreFix = extension->GetCustomFragmentPreFix();
82 fragmentShader = fragmentPreFix;
83 fragmentShader += FRAGMENT_SHADER_TEXTURE;
87 fragmentShader = FRAGMENT_SHADER_TEXTURE;
90 //Get custom sampler type name
91 const char* customSamplerTypename = extension->GetCustomSamplerTypename();
92 if( customSamplerTypename )
94 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen(DEFAULT_SAMPLER_TYPENAME), customSamplerTypename );
97 return Shader::New( VERTEX_SHADER_TEXTURE, fragmentShader );
101 return Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
107 // This example shows how to create and use a NativeImageSource as the target of the render task.
109 class NativeImageSourceController : public ConnectionTracker
113 NativeImageSourceController( Application& application )
114 : mApplication( application ),
115 mRefreshAlways( true )
117 // Connect to the Application's Init signal
118 mApplication.InitSignal().Connect( this, &NativeImageSourceController::Create );
121 ~NativeImageSourceController()
123 // Nothing to do here;
126 // The Init signal is received once (only) during the Application lifetime
127 void Create( Application& application )
129 // Get a handle to the stage
130 Stage stage = Stage::GetCurrent();
131 stage.SetBackgroundColor( Color::WHITE );
133 // Hide the indicator bar
134 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
136 stage.KeyEventSignal().Connect(this, &NativeImageSourceController::OnKeyEvent);
140 CreateContentAreas();
143 void CreateButtonArea()
145 Stage stage = Stage::GetCurrent();
146 Vector2 stageSize = stage.GetSize();
148 mButtonArea = Layer::New();
149 mButtonArea.SetSize( stageSize.x, BUTTON_HEIGHT );
150 mButtonArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
151 mButtonArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
152 stage.Add( mButtonArea );
154 mButtonShow = PushButton::New();
155 mButtonShow.SetProperty( Button::Property::TOGGLABLE, true );
156 mButtonShow.SetProperty( Toolkit::Button::Property::LABEL, "SHOW" );
157 mButtonShow.SetParentOrigin( ParentOrigin::TOP_LEFT );
158 mButtonShow.SetAnchorPoint( AnchorPoint::TOP_LEFT );
159 mButtonShow.SetSize( stageSize.x / BUTTON_COUNT, BUTTON_HEIGHT );
160 mButtonShow.ClickedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
161 mButtonArea.Add( mButtonShow );
163 mButtonRefreshAlways = PushButton::New();
164 mButtonRefreshAlways.SetProperty( Button::Property::TOGGLABLE, true );
165 mButtonRefreshAlways.SetProperty( Toolkit::Button::Property::LABEL, "ALWAYS" );
166 mButtonRefreshAlways.SetParentOrigin( ParentOrigin::TOP_LEFT );
167 mButtonRefreshAlways.SetAnchorPoint( AnchorPoint::TOP_LEFT );
168 mButtonRefreshAlways.SetSize( stageSize.x / BUTTON_COUNT, BUTTON_HEIGHT );
169 mButtonRefreshAlways.SetPosition( (stageSize.x / BUTTON_COUNT)*1.0f, 0.0f );
170 mButtonRefreshAlways.StateChangedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
171 mButtonArea.Add( mButtonRefreshAlways );
173 mButtonRefreshOnce = PushButton::New();
174 mButtonRefreshOnce.SetProperty( Toolkit::Button::Property::LABEL, "ONCE" );
175 mButtonRefreshOnce.SetParentOrigin( ParentOrigin::TOP_LEFT );
176 mButtonRefreshOnce.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177 mButtonRefreshOnce.SetSize( stageSize.x / BUTTON_COUNT, BUTTON_HEIGHT );
178 mButtonRefreshOnce.SetPosition( (stageSize.x / BUTTON_COUNT)*2.0f, 0.0f );
179 mButtonRefreshOnce.ClickedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
180 mButtonArea.Add( mButtonRefreshOnce );
182 mButtonCapture = PushButton::New();
183 mButtonCapture.SetProperty( Toolkit::Button::Property::LABEL, "CAPTURE" );
184 mButtonCapture.SetParentOrigin( ParentOrigin::TOP_LEFT );
185 mButtonCapture.SetAnchorPoint( AnchorPoint::TOP_LEFT );
186 mButtonCapture.SetSize( stageSize.x / BUTTON_COUNT, BUTTON_HEIGHT );
187 mButtonCapture.SetPosition( (stageSize.x / BUTTON_COUNT)*3.0f, 0.0f );
188 mButtonCapture.ClickedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
189 mButtonArea.Add( mButtonCapture );
191 mButtonReset = PushButton::New();
192 mButtonReset.SetProperty( Toolkit::Button::Property::LABEL, "RESET" );
193 mButtonReset.SetParentOrigin( ParentOrigin::TOP_LEFT );
194 mButtonReset.SetAnchorPoint( AnchorPoint::TOP_LEFT );
195 mButtonReset.SetSize( stageSize.x / BUTTON_COUNT, BUTTON_HEIGHT );
196 mButtonReset.SetPosition( (stageSize.x / BUTTON_COUNT)*4.0f, 0.0f );
197 mButtonReset.ClickedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
198 mButtonArea.Add( mButtonReset );
201 void CreateContentAreas()
203 Stage stage = Stage::GetCurrent();
204 Vector2 stageSize = stage.GetSize();
206 float contentHeight( (stageSize.y - BUTTON_HEIGHT)/2.0f );
208 mTopContentArea = Actor::New();
209 mTopContentArea.SetSize( stageSize.x, contentHeight );
210 mTopContentArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
211 mTopContentArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
212 mTopContentArea.SetY( BUTTON_HEIGHT );
213 stage.Add( mTopContentArea );
215 mBottomContentArea = Actor::New();
216 mBottomContentArea.SetSize( stageSize.x, contentHeight );
217 mBottomContentArea.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
218 mBottomContentArea.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
219 stage.Add( mBottomContentArea );
221 mSourceActor = ImageView::New(JPG_FILENAME);
222 mSourceActor.SetParentOrigin( ParentOrigin::CENTER);
223 mSourceActor.SetAnchorPoint( AnchorPoint::CENTER );
224 mTopContentArea.Add( mSourceActor );
226 Animation animation = Animation::New(2.f);
227 Degree relativeRotationDegrees(90.0f);
228 Radian relativeRotationRadians(relativeRotationDegrees);
229 animation.AnimateTo( Property( mSourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(0.f, 0.5f));
230 animation.AnimateBy( Property( mSourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(0.5f, 0.5f));
231 animation.AnimateBy( Property( mSourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(1.f, 0.5f));
232 animation.AnimateBy( Property( mSourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(1.5f, 0.5f));
233 animation.SetLooping(true);
236 TextLabel textLabel1 = TextLabel::New( "Image" );
237 textLabel1.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
238 textLabel1.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
239 mTopContentArea.Add( textLabel1 );
241 // Wait until button press before creating mOffscreenRenderTask
243 TextLabel textLabel2 = TextLabel::New( "Native Image" );
244 textLabel2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
245 textLabel2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
246 mBottomContentArea.Add( textLabel2 );
249 void SetupNativeImage()
251 if( ! mOffscreenRenderTask )
253 Stage stage = Stage::GetCurrent();
254 Vector2 stageSize = stage.GetSize();
256 float contentHeight( (stageSize.y - BUTTON_HEIGHT)/2.0f );
257 Vector2 imageSize( stageSize.x, contentHeight );
259 mNativeImageSourcePtr = NativeImageSource::New( imageSize.width, imageSize.height, NativeImageSource::COLOR_DEPTH_DEFAULT );
260 mNativeTexture = Texture::New( *mNativeImageSourcePtr );
262 mFrameBuffer = FrameBuffer::New( mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), FrameBuffer::Attachment::NONE );
263 mFrameBuffer.AttachColorTexture( mNativeTexture );
265 mCameraActor = CameraActor::New( imageSize );
266 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
267 mCameraActor.SetParentOrigin( AnchorPoint::CENTER );
268 mTopContentArea.Add( mCameraActor );
270 RenderTaskList taskList = stage.GetRenderTaskList();
271 mOffscreenRenderTask = taskList.CreateTask();
272 mOffscreenRenderTask.SetSourceActor( mSourceActor );
273 mOffscreenRenderTask.SetClearColor( Color::WHITE );
274 mOffscreenRenderTask.SetClearEnabled( true );
275 mOffscreenRenderTask.SetCameraActor( mCameraActor );
276 mOffscreenRenderTask.GetCameraActor().SetInvertYAxis( true );
277 mOffscreenRenderTask.SetFrameBuffer( mFrameBuffer );
282 mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
286 mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
290 void SetupDisplayActor( bool show )
294 if( ! mDisplayActor )
296 // Make sure we have something to display
299 mDisplayActor = Actor::New();
300 mDisplayActor.SetParentOrigin( ParentOrigin::CENTER );
301 mDisplayActor.SetAnchorPoint( AnchorPoint::CENTER );
303 Geometry geometry = DemoHelper::CreateTexturedQuad();
305 Shader shader = CreateShader( *mNativeImageSourcePtr );
307 Renderer renderer = Renderer::New( geometry, shader );
309 TextureSet textureSet = TextureSet::New();
310 textureSet.SetTexture( 0u, mNativeTexture );
311 renderer.SetTextures( textureSet );
313 mDisplayActor.AddRenderer( renderer );
314 mDisplayActor.SetSize( mNativeTexture.GetWidth(), mNativeTexture.GetHeight() );
316 mBottomContentArea.Add( mDisplayActor );
321 UnparentAndReset( mDisplayActor );
327 mRefreshAlways = false;
330 mOffscreenRenderTask.FinishedSignal().Connect( this, &NativeImageSourceController::DoCapture );
333 void DoCapture(RenderTask& task)
335 task.FinishedSignal().Disconnect( this, &NativeImageSourceController::DoCapture );
337 mNativeImageSourcePtr->EncodeToFile( CAPTURE_FILENAME );
342 SetupDisplayActor( false );
344 Stage stage = Stage::GetCurrent();
345 RenderTaskList taskList = stage.GetRenderTaskList();
346 taskList.RemoveTask( mOffscreenRenderTask );
347 mOffscreenRenderTask.Reset();
348 mCameraActor.Reset();
350 mFrameBuffer.Reset();
351 mNativeTexture.Reset();
352 mNativeImageSourcePtr.Reset();
355 bool OnButtonSelected( Toolkit::Button button )
357 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
359 if( pushButton == mButtonShow )
361 bool isSelected = mButtonShow.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>();
363 SetupDisplayActor( isSelected );
365 else if( pushButton == mButtonRefreshAlways )
367 bool isSelected = mButtonRefreshAlways.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>();
369 mRefreshAlways = isSelected;
372 else if( pushButton == mButtonRefreshOnce )
374 bool isSelected = mButtonRefreshAlways.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>();
378 mButtonRefreshAlways.SetProperty( Button::Property::SELECTED, false );
381 mRefreshAlways = false;
384 else if( pushButton == mButtonCapture )
388 else if( pushButton == mButtonReset )
396 void OnKeyEvent(const KeyEvent& event)
398 if(event.state == KeyEvent::Down)
400 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
409 Application& mApplication;
412 Actor mTopContentArea;
413 Actor mBottomContentArea;
415 PushButton mButtonShow;
416 PushButton mButtonRefreshAlways;
417 PushButton mButtonRefreshOnce;
418 PushButton mButtonCapture;
419 PushButton mButtonReset;
423 NativeImageSourcePtr mNativeImageSourcePtr;
424 Texture mNativeTexture;
425 FrameBuffer mFrameBuffer;
427 RenderTask mOffscreenRenderTask;
428 CameraActor mCameraActor;
435 int DALI_EXPORT_API main( int argc, char **argv )
437 Application application = Application::New( &argc, &argv );
438 NativeImageSourceController test( application );
439 application.MainLoop();