2 * Copyright (c) 2020 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-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <dali/dali.h>
24 #include "shared/utility.h"
25 #include "generated/compressed-texture-formats-example-vert.h"
26 #include "generated/compressed-texture-formats-example-frag.h"
29 using Dali::Toolkit::TextLabel;
33 const char* IMAGE_FILENAME_ETC = DEMO_IMAGE_DIR "tx-etc1.ktx";
34 const char* IMAGE_FILENAME_ASTC_LINEAR = DEMO_IMAGE_DIR "tx-astc-4x4-linear.ktx";
35 const char* IMAGE_FILENAME_ASTC_LINEAR_NATIVE = DEMO_IMAGE_DIR "tx-astc-4x4-linear-native.astc";
38 * @brief Create a renderer to render an image and adds it to an actor
39 * @param[in] imagePath The path where the image file is located
40 * @param[in] actor The actor that will be used to render the image
41 * @param[in[ geometry The geometry to use
42 * @param[in] shader The shader to use
44 void AddImage(const char* imagePath, Actor& actor, Geometry& geometry, Shader& shader)
47 Texture texture = DemoHelper::LoadTexture(imagePath);
48 TextureSet textureSet = TextureSet::New();
49 textureSet.SetTexture(0u, texture);
52 Renderer renderer = Renderer::New(geometry, shader);
53 renderer.SetTextures(textureSet);
55 //Set actor size and add the renderer
56 actor.SetProperty(Actor::Property::SIZE, Vector2(texture.GetWidth(), texture.GetHeight()));
57 actor.AddRenderer(renderer);
62 * @brief This example shows 3 images, each of a different compressed texture type.
63 * If built and run on a OpenGL ES 3.1 compatable target, then all 3 images will display.
64 * Otherwise, the top image will display and the other 2 will appear as black squares.
66 class CompressedTextureFormatsController : public ConnectionTracker
69 CompressedTextureFormatsController(Application& application)
70 : mApplication(application)
72 // Connect to the Application's Init signal
73 mApplication.InitSignal().Connect(this, &CompressedTextureFormatsController::Create);
76 ~CompressedTextureFormatsController()
78 // Nothing to do here;
81 // The Init signal is received once (only) during the Application lifetime
82 void Create(Application& application)
84 // Get a handle to the window
85 Window window = application.GetWindow();
86 window.SetBackgroundColor(Color::WHITE);
88 // Setup a TableView to hold a grid of images and labels.
89 Toolkit::TableView table = Toolkit::TableView::New(3u, 2u);
90 table.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
91 table.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
92 table.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
93 table.SetRelativeWidth(0u, 0.5f);
94 table.SetRelativeWidth(1u, 0.5f);
95 table.SetRelativeHeight(0u, 1.0f / 3.0f);
96 table.SetRelativeHeight(1u, 1.0f / 3.0f);
97 table.SetRelativeHeight(2u, 1.0f / 3.0f);
100 TextLabel textLabel = TextLabel::New("ETC1 (KTX):");
101 textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
102 textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
103 textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
104 textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
105 table.AddChild(textLabel, Toolkit::TableView::CellPosition(0u, 0u));
106 table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
108 textLabel = TextLabel::New("ASTC (KTX) 4x4 linear:");
109 textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
110 textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
111 textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
112 textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
113 table.AddChild(textLabel, Toolkit::TableView::CellPosition(1u, 0u));
114 table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
116 textLabel = TextLabel::New("ASTC (Native) 4x4 linear:");
117 textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
118 textLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
119 textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
120 textLabel.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
121 table.AddChild(textLabel, Toolkit::TableView::CellPosition(2u, 0u));
122 table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 0u), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
124 //Create the geometry and the shader renderers will use
125 Geometry geometry = DemoHelper::CreateTexturedQuad();
126 Shader shader = Shader::New(SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_VERT, SHADER_COMPRESSED_TEXTURE_FORMATS_EXAMPLE_FRAG);
129 Actor actor = Actor::New();
130 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
131 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
132 AddImage(IMAGE_FILENAME_ETC, actor, geometry, shader);
133 table.AddChild(actor, Toolkit::TableView::CellPosition(0u, 1u));
134 table.SetCellAlignment(Toolkit::TableView::CellPosition(0u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
136 actor = Actor::New();
137 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
138 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
139 AddImage(IMAGE_FILENAME_ASTC_LINEAR, actor, geometry, shader);
140 table.AddChild(actor, Toolkit::TableView::CellPosition(1u, 1u));
141 table.SetCellAlignment(Toolkit::TableView::CellPosition(1u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
143 actor = Actor::New();
144 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
145 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
146 AddImage(IMAGE_FILENAME_ASTC_LINEAR_NATIVE, actor, geometry, shader);
147 table.AddChild(actor, Toolkit::TableView::CellPosition(2u, 1u));
148 table.SetCellAlignment(Toolkit::TableView::CellPosition(2u, 1u), HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
152 // Respond to touch and key signals
153 window.GetRootLayer().TouchedSignal().Connect(this, &CompressedTextureFormatsController::OnTouch);
154 window.KeyEventSignal().Connect(this, &CompressedTextureFormatsController::OnKeyEvent);
157 bool OnTouch(Actor actor, const TouchEvent& touch)
159 // quit the application
164 void OnKeyEvent(const KeyEvent& event)
166 if(event.GetState() == KeyEvent::DOWN)
168 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
176 Application& mApplication;
179 int DALI_EXPORT_API main(int argc, char** argv)
181 Application application = Application::New(&argc, &argv);
182 CompressedTextureFormatsController test(application);
183 application.MainLoop();