Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / compressed-texture-formats / compressed-texture-formats-example.cpp
1 /*
2  * Copyright (c) 2020 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-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <dali/dali.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 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 // clang-format off
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 // clang-format on
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.SetProperty(Actor::Property::SIZE, Vector2(texture.GetWidth(), texture.GetHeight()));
82   actor.AddRenderer(renderer);
83 }
84
85 } // namespace
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   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 window
110     Window window = application.GetWindow();
111     window.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.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
116     table.SetProperty(Actor::Property::PARENT_ORIGIN, 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     // Add text labels.
125     TextLabel textLabel = TextLabel::New("ETC1 (KTX):");
126     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
127     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
128     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
129     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
130     table.AddChild(textLabel, Toolkit::TableView::CellPosition(0u, 0u));
131     table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
132
133     textLabel = TextLabel::New("ASTC (KTX) 4x4 linear:");
134     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
135     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
136     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
137     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
138     table.AddChild(textLabel, Toolkit::TableView::CellPosition(1u, 0u));
139     table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
140
141     textLabel = TextLabel::New("ASTC (Native) 4x4 linear:");
142     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
143     textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
144     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
145     textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
146     table.AddChild(textLabel, Toolkit::TableView::CellPosition(2u, 0u));
147     table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
148
149     //Create the geometry and the shader renderers will use
150     Geometry geometry = DemoHelper::CreateTexturedQuad();
151     Shader   shader   = Shader::New(VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE);
152
153     // Add images.
154     Actor actor = Actor::New();
155     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
156     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
157     AddImage(IMAGE_FILENAME_ETC, actor, geometry, shader);
158     table.AddChild(actor, Toolkit::TableView::CellPosition(0u, 1u));
159     table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
160
161     actor = Actor::New();
162     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
163     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
164     AddImage(IMAGE_FILENAME_ASTC_LINEAR, actor, geometry, shader);
165     table.AddChild(actor, Toolkit::TableView::CellPosition(1u, 1u));
166     table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
167
168     actor = Actor::New();
169     actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
170     actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
171     AddImage(IMAGE_FILENAME_ASTC_LINEAR_NATIVE, actor, geometry, shader);
172     table.AddChild(actor, Toolkit::TableView::CellPosition(2u, 1u));
173     table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
174
175     window.Add(table);
176
177     // Respond to touch and key signals
178     window.GetRootLayer().TouchedSignal().Connect(this, &CompressedTextureFormatsController::OnTouch);
179     window.KeyEventSignal().Connect(this, &CompressedTextureFormatsController::OnKeyEvent);
180   }
181
182   bool OnTouch(Actor actor, const TouchEvent& touch)
183   {
184     // quit the application
185     mApplication.Quit();
186     return true;
187   }
188
189   void OnKeyEvent(const KeyEvent& event)
190   {
191     if(event.GetState() == KeyEvent::DOWN)
192     {
193       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
194       {
195         mApplication.Quit();
196       }
197     }
198   }
199
200 private:
201   Application& mApplication;
202 };
203
204 int DALI_EXPORT_API main(int argc, char** argv)
205 {
206   Application                        application = Application::New(&argc, &argv);
207   CompressedTextureFormatsController test(application);
208   application.MainLoop();
209   return 0;
210 }