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