Support for ASTC compressed textures wrapped in KTX files
[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 #include <dali-toolkit/dali-toolkit.h>
19
20 using namespace Dali;
21 using Dali::Toolkit::TextLabel;
22
23 const char* IMAGE_FILENAME_ETC         = DALI_IMAGE_DIR "tx-etc1.ktx";
24 const char* IMAGE_FILENAME_ASTC_LINEAR = DALI_IMAGE_DIR "tx-astc-4x4-linear.ktx";
25 const char* IMAGE_FILENAME_ASTC_SRGB   = DALI_IMAGE_DIR "tx-astc-4x4-srgb.ktx";
26
27 /**
28  * @brief This example shows 3 images, each of a different compressed texture type.
29  * If built and run on a OpenGL ES 3.1 compatable target, then all 3 images will display.
30  * Otherwise, the top image will display and the other 2 will appear as black squares.
31  */
32 class CompressedTextureFormatsController : public ConnectionTracker
33 {
34 public:
35
36   CompressedTextureFormatsController( Application& application )
37   : mApplication( application )
38   {
39     // Connect to the Application's Init signal
40     mApplication.InitSignal().Connect( this, &CompressedTextureFormatsController::Create );
41   }
42
43   ~CompressedTextureFormatsController()
44   {
45     // Nothing to do here;
46   }
47
48   // The Init signal is received once (only) during the Application lifetime
49   void Create( Application& application )
50   {
51     // Get a handle to the stage
52     Stage stage = Stage::GetCurrent();
53     stage.SetBackgroundColor( Color::WHITE );
54
55     // Setup a TableView to hold a grid of images and labels.
56     Toolkit::TableView table = Toolkit::TableView::New( 3u, 2u );
57     table.SetAnchorPoint( AnchorPoint::CENTER );
58     table.SetParentOrigin( ParentOrigin::CENTER );
59     table.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
60     table.SetFitHeight( 0u );
61     table.SetFitHeight( 1u );
62     table.SetFitHeight( 2u );
63     table.SetRelativeWidth( 0u, 0.5f );
64     table.SetRelativeWidth( 1u, 0.5f );
65
66     // Add text labels.
67     TextLabel textLabel = TextLabel::New( "ETC1:" );
68     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
69     textLabel.SetParentOrigin( ParentOrigin::CENTER );
70     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 0u, 0u ) );
71     table.SetCellAlignment( Toolkit::TableView::CellPosition( 0u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
72
73     textLabel = TextLabel::New( "ASTC 4x4 linear:" );
74     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
75     textLabel.SetParentOrigin( ParentOrigin::CENTER );
76     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 1u, 0u ) );
77     table.SetCellAlignment( Toolkit::TableView::CellPosition( 1u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
78
79     textLabel = TextLabel::New( "ASTC 4x4 sRGB:" );
80     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
81     textLabel.SetParentOrigin( ParentOrigin::CENTER );
82     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 2u, 0u ) );
83     table.SetCellAlignment( Toolkit::TableView::CellPosition( 2u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
84
85     // Add images.
86     Toolkit::ImageView imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ETC ) );
87     imageView.SetAnchorPoint( AnchorPoint::CENTER );
88     imageView.SetParentOrigin( ParentOrigin::CENTER );
89     table.AddChild( imageView, Toolkit::TableView::CellPosition( 0u, 1u ) );
90
91     imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ASTC_LINEAR ) );
92     imageView.SetAnchorPoint( AnchorPoint::CENTER );
93     imageView.SetParentOrigin( ParentOrigin::CENTER );
94     table.AddChild( imageView, Toolkit::TableView::CellPosition( 1u, 1u ) );
95
96     imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ASTC_SRGB ) );
97     imageView.SetAnchorPoint( AnchorPoint::CENTER );
98     imageView.SetParentOrigin( ParentOrigin::CENTER );
99     table.AddChild( imageView, Toolkit::TableView::CellPosition( 2u, 1u ) );
100
101     stage.Add( table );
102
103     // Respond to a click anywhere on the stage
104     stage.GetRootLayer().TouchedSignal().Connect( this, &CompressedTextureFormatsController::OnTouch );
105   }
106
107   bool OnTouch( Actor actor, const TouchEvent& touch )
108   {
109     // quit the application
110     mApplication.Quit();
111     return true;
112   }
113
114 private:
115   Application&  mApplication;
116 };
117
118 void RunTest( Application& application )
119 {
120   CompressedTextureFormatsController test( application );
121
122   application.MainLoop();
123 }
124
125 // Entry point for Linux & Tizen applications
126 //
127 int main( int argc, char **argv )
128 {
129   Application application = Application::New( &argc, &argv );
130
131   RunTest( application );
132
133   return 0;
134 }