[dali_1.2.13] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / shared / utility.h
1 #ifndef DALI_DEMO_UTILITY_H
2 #define DALI_DEMO_UTILITY_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/dali.h>
22 #include <dali/devel-api/images/atlas.h>
23 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/texture.h>
26
27 namespace DemoHelper
28 {
29
30 Dali::PixelData LoadPixelData( const char* imagePath,
31                                Dali::ImageDimensions size,
32                                Dali::FittingMode::Type fittingMode,
33                                Dali::SamplingMode::Type samplingMode )
34 {
35   Dali::BitmapLoader loader = Dali::BitmapLoader::New( imagePath, size, fittingMode, samplingMode );
36   loader.Load();
37   return loader.GetPixelData();
38 }
39
40 /**
41  * @deprecated, dont use this anymore
42  */
43 Dali::Image LoadImage( const char* imagePath,
44                        Dali::ImageDimensions size = Dali::ImageDimensions(),
45                        Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
46                        Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
47 {
48   return Dali::ResourceImage::New( imagePath, size, fittingMode, samplingMode );
49 }
50
51 Dali::Texture LoadTexture( const char* imagePath,
52                            Dali::ImageDimensions size = Dali::ImageDimensions(),
53                            Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
54                            Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
55 {
56   Dali::PixelData pixelData = LoadPixelData(imagePath, size, fittingMode, samplingMode);
57   Dali::Texture texture  = Dali::Texture::New( Dali::TextureType::TEXTURE_2D,
58                                                pixelData.GetPixelFormat(),
59                                                pixelData.GetWidth(),
60                                                pixelData.GetHeight() );
61   texture.Upload( pixelData );
62
63   return texture;
64 }
65
66 /**
67  * @brief Load an bitmap resource.
68  * @deprecated, dont use this anymore
69  *
70  * If it is required to scaled-down to no more than the stage dimensions,
71  * uses image scaling mode FittingMode::SCALE_TO_FILL to resize the image at
72  * load time to cover the entire stage with pixels with no borders,
73  * and filter mode BOX_THEN_LINEAR to sample the image with
74  * maximum quality.
75  */
76
77 Dali::Image LoadStageFillingImage( const char* imagePath )
78 {
79   Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
80   return LoadImage( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
81 }
82
83 Dali::Texture LoadStageFillingTexture( const char* imagePath )
84 {
85   Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
86   return LoadTexture( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
87 }
88
89 Dali::Geometry CreateTexturedQuad()
90 {
91   struct Vertex
92   {
93     Dali::Vector2 position;
94     Dali::Vector2 texCoord;
95   };
96
97   static const Vertex data[] = {{ Dali::Vector2( -0.5f, -0.5f ), Dali::Vector2( 0.0f, 0.0f ) },
98                                 { Dali::Vector2(  0.5f, -0.5f ), Dali::Vector2( 1.0f, 0.0f ) },
99                                 { Dali::Vector2( -0.5f,  0.5f ), Dali::Vector2( 0.0f, 1.0f ) },
100                                 { Dali::Vector2(  0.5f,  0.5f ), Dali::Vector2( 1.0f, 1.0f ) }};
101
102   Dali::PropertyBuffer vertexBuffer;
103   Dali::Property::Map vertexFormat;
104   vertexFormat["aPosition"] = Dali::Property::VECTOR2;
105   vertexFormat["aTexCoord"] = Dali::Property::VECTOR2;
106
107   //Create a vertex buffer for vertex positions and texture coordinates
108   vertexBuffer = Dali::PropertyBuffer::New( vertexFormat );
109   vertexBuffer.SetData( data, 4u );
110
111   //Create the geometry
112   Dali::Geometry geometry = Dali::Geometry::New();
113   geometry.AddVertexBuffer( vertexBuffer );
114   geometry.SetType(Dali::Geometry::TRIANGLE_STRIP );
115
116   return geometry;
117 }
118 } // DemoHelper
119
120 #endif // DALI_DEMO_UTILITY_H