Using migrated Public Visual API
[platform/core/uifw/dali-demo.git] / examples / compressed-texture-formats / compressed-texture-formats-example.cpp
1 /*
2  * Copyright (c) 2017 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 // EXTERNAL INCLUDES
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/utility.h"
24
25 using namespace Dali;
26 using Dali::Toolkit::TextLabel;
27
28 namespace
29 {
30
31 const char* IMAGE_FILENAME_ETC         =        DEMO_IMAGE_DIR "tx-etc1.ktx";
32 const char* IMAGE_FILENAME_ASTC_LINEAR =        DEMO_IMAGE_DIR "tx-astc-4x4-linear.ktx";
33 const char* IMAGE_FILENAME_ASTC_LINEAR_NATIVE = DEMO_IMAGE_DIR "tx-astc-4x4-linear-native.astc";
34
35
36 static const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
37     attribute mediump vec2 aPosition;\n
38     attribute mediump vec2 aTexCoord;\n
39     uniform mediump mat4 uMvpMatrix;\n
40     uniform mediump vec3 uSize;\n
41     varying mediump vec2 vTexCoord;\n
42     void main()\n
43     {\n
44       vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n
45       gl_Position = uMvpMatrix * position;\n
46       vTexCoord = aTexCoord;\n
47     }\n
48 );
49
50 static const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
51     uniform lowp vec4 uColor;\n
52     uniform sampler2D sTexture;\n
53     varying mediump vec2 vTexCoord;\n
54
55     void main()\n
56     {\n
57       gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
58     }\n
59 );
60
61 /**
62  * @brief Create a renderer to render an image and adds it to an actor
63  * @param[in] imagePath The path where the image file is located
64  * @param[in] actor The actor that will be used to render the image
65  * @param[in[ geometry The geometry to use
66  * @param[in] shader The shader to use
67  */
68 void AddImage( const char*imagePath, Actor& actor, Geometry& geometry, Shader& shader )
69 {
70   //Load the texture
71   Texture texture = DemoHelper::LoadTexture( imagePath );
72   TextureSet textureSet = TextureSet::New();
73   textureSet.SetTexture( 0u, texture );
74
75   //Create the renderer
76   Renderer renderer = Renderer::New( geometry, shader );
77   renderer.SetTextures( textureSet );
78
79   //Set actor size and add the renderer
80   actor.SetSize( texture.GetWidth(), texture.GetHeight() );
81   actor.AddRenderer( renderer );
82 }
83
84 }
85 /**
86  * @brief This example shows 3 images, each of a different compressed texture type.
87  * If built and run on a OpenGL ES 3.1 compatable target, then all 3 images will display.
88  * Otherwise, the top image will display and the other 2 will appear as black squares.
89  */
90 class CompressedTextureFormatsController : public ConnectionTracker
91 {
92 public:
93
94   CompressedTextureFormatsController( Application& application )
95   : mApplication( application )
96   {
97     // Connect to the Application's Init signal
98     mApplication.InitSignal().Connect( this, &CompressedTextureFormatsController::Create );
99   }
100
101   ~CompressedTextureFormatsController()
102   {
103     // Nothing to do here;
104   }
105
106   // The Init signal is received once (only) during the Application lifetime
107   void Create( Application& application )
108   {
109     // Get a handle to the stage
110     Stage stage = Stage::GetCurrent();
111     stage.SetBackgroundColor( Color::WHITE );
112
113     // Setup a TableView to hold a grid of images and labels.
114     Toolkit::TableView table = Toolkit::TableView::New( 3u, 2u );
115     table.SetAnchorPoint( AnchorPoint::CENTER );
116     table.SetParentOrigin( ParentOrigin::CENTER );
117     table.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
118     table.SetRelativeWidth( 0u, 0.5f );
119     table.SetRelativeWidth( 1u, 0.5f );
120     table.SetRelativeHeight( 0u, 1.0f / 3.0f );
121     table.SetRelativeHeight( 1u, 1.0f / 3.0f );
122     table.SetRelativeHeight( 2u, 1.0f / 3.0f );
123
124
125     // Add text labels.
126     TextLabel textLabel = TextLabel::New( "ETC1 (KTX):" );
127     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
128     textLabel.SetParentOrigin( ParentOrigin::CENTER );
129     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
130     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
131     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 0u, 0u ) );
132     table.SetCellAlignment( Toolkit::TableView::CellPosition( 0u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
133
134     textLabel = TextLabel::New( "ASTC (KTX) 4x4 linear:" );
135     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
136     textLabel.SetParentOrigin( ParentOrigin::CENTER );
137     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
138     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
139     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 1u, 0u ) );
140     table.SetCellAlignment( Toolkit::TableView::CellPosition( 1u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
141
142     textLabel = TextLabel::New( "ASTC (Native) 4x4 linear:" );
143     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
144     textLabel.SetParentOrigin( ParentOrigin::CENTER );
145     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
146     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
147     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 2u, 0u ) );
148     table.SetCellAlignment( Toolkit::TableView::CellPosition( 2u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
149
150     //Create the geometry and the shader renderers will use
151     Geometry geometry = DemoHelper::CreateTexturedQuad();
152     Shader shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
153
154     // Add images.
155     Actor actor = Actor::New();
156     actor.SetAnchorPoint( AnchorPoint::CENTER );
157     actor.SetParentOrigin( ParentOrigin::CENTER );
158     AddImage( IMAGE_FILENAME_ETC, actor, geometry, shader  );
159     table.AddChild( actor, Toolkit::TableView::CellPosition( 0u, 1u ) );
160     table.SetCellAlignment( Toolkit::TableView::CellPosition( 0u, 1u ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
161
162     actor = Actor::New();
163     actor.SetAnchorPoint( AnchorPoint::CENTER );
164     actor.SetParentOrigin( ParentOrigin::CENTER );
165     AddImage( IMAGE_FILENAME_ASTC_LINEAR, actor, geometry, shader );
166     table.AddChild( actor, Toolkit::TableView::CellPosition( 1u, 1u ) );
167     table.SetCellAlignment( Toolkit::TableView::CellPosition( 1u, 1u ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
168
169     actor = Actor::New();
170     actor.SetAnchorPoint( AnchorPoint::CENTER );
171     actor.SetParentOrigin( ParentOrigin::CENTER );
172     AddImage( IMAGE_FILENAME_ASTC_LINEAR_NATIVE, actor, geometry, shader );
173     table.AddChild( actor, Toolkit::TableView::CellPosition( 2u, 1u ) );
174     table.SetCellAlignment( Toolkit::TableView::CellPosition( 2u, 1u ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
175
176     stage.Add( table );
177
178     // Respond to touch and key signals
179     stage.GetRootLayer().TouchSignal().Connect( this, &CompressedTextureFormatsController::OnTouch );
180     stage.KeyEventSignal().Connect(this, &CompressedTextureFormatsController::OnKeyEvent);
181   }
182
183   bool OnTouch( Actor actor, const TouchData& touch )
184   {
185     // quit the application
186     mApplication.Quit();
187     return true;
188   }
189
190   void OnKeyEvent(const KeyEvent& event)
191   {
192     if(event.state == KeyEvent::Down)
193     {
194       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
195       {
196         mApplication.Quit();
197       }
198     }
199   }
200
201 private:
202   Application&  mApplication;
203 };
204
205 void RunTest( Application& application )
206 {
207   CompressedTextureFormatsController test( application );
208
209   application.MainLoop();
210 }
211
212 // Entry point for Linux & Tizen applications
213 //
214 int DALI_EXPORT_API main( int argc, char **argv )
215 {
216   Application application = Application::New( &argc, &argv );
217
218   RunTest( application );
219
220   return 0;
221 }