Merge "Merge branch 'tizen' into devel/new_mesh" into devel/new_mesh
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / nine-patch-mask-effect.h
1 #ifndef __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__
2 #define __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__
3
4 /*
5  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/image-actor.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 /**
31  * @brief NinePatchMaskEffect is used to control which parts of an image are visible, using the alpha channel of a separate mask image.
32  *
33  * The mask image is expected to be smaller than the main image being viewed.
34  * Conceptually the mask image is divided into a 3x3 grid (9 patches). The middle patch is stretched whilst the outer border is not.
35  *
36  * Usage example:
37  *
38  * @code
39  *   ImageActor actor = ImageActor::New( Image( EXAMPLE_IMAGE_PATH ) );
40  *   NinePatchMaskEffect::Apply( actor, MASK_IMAGE_PATH );
41  * @endcode
42  *
43  * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
44  */
45 namespace NinePatchMaskEffect
46 {
47
48 static void NinePatchMaskEffectSizeConstraint( Vector2& current, const PropertyInputContainer& inputs )
49 {
50   const Vector3& actorSize = inputs[0]->GetVector3();
51   current.x = actorSize.x;
52   current.y = actorSize.y;
53 }
54
55 inline void DoApply( ImageActor actor, const std::string& maskImage, const Vector2& maskSize, Vector4 maskBorder )
56 {
57   const char* ALPHA_MASK_VERTEX_SHADER_SOURCE =
58   "precision mediump float;\n"
59   "uniform vec2 uImageSize;                                                       \n"
60   "uniform vec2 uMaskSize;                                                        \n"
61   "varying vec2 vMaskTexCoord;                                                    \n"
62   "                                                                               \n"
63   "void main()                                                                    \n"
64   "{                                                                              \n"
65   "  gl_Position = uMvpMatrix * vec4(aPosition, 1.0);                             \n"
66   "                                                                               \n"
67   "  // Ignore mask UVs for image                                                 \n"
68   "                                                                               \n"
69   "  highp vec2 halfImageSize = uImageSize * 0.5;                                 \n"
70   "  vTexCoord = (aPosition.xy + halfImageSize) / uImageSize;                     \n"
71   "                                                                               \n"
72   "  // UVs were calculated for image size, so convert for mask size              \n"
73   "                                                                               \n"
74   "  highp vec2 halfMaskSize  = uMaskSize * 0.5;                                  \n"
75   "  highp vec2 halfSizeDelta = halfImageSize - halfMaskSize;                     \n"
76   "                                                                               \n"
77   "  highp vec2 maskPosition = aPosition.xy;                                      \n"
78   "  maskPosition.x -= halfSizeDelta.x * sign(aPosition.x);                       \n"
79   "  maskPosition.y -= halfSizeDelta.y * sign(aPosition.y);                       \n"
80   "                                                                               \n"
81   "  vMaskTexCoord = (maskPosition + halfMaskSize) / uMaskSize;                   \n"
82   "}                                                                              \n";
83
84   const char* ALPHA_MASK_FRAGMENT_SHADER_SOURCE =
85   "varying mediump vec2 vMaskTexCoord;                                            \n"
86   "                                                                               \n"
87   "void main()                                                                    \n"
88   "{                                                                              \n"
89   "  highp vec4 mask = texture2D(sEffect, vMaskTexCoord);                         \n"
90   "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(1,1,1,mask.a); \n"
91   "}                                                                              \n";
92
93   ShaderEffect maskEffect = ShaderEffect::New(
94     ALPHA_MASK_VERTEX_SHADER_SOURCE,
95     ALPHA_MASK_FRAGMENT_SHADER_SOURCE,
96     GeometryType( GEOMETRY_TYPE_IMAGE ),
97     ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
98
99   maskEffect.SetEffectImage( ResourceImage::New( maskImage ) );
100
101   maskEffect.SetUniform( "uImageSize", Vector2(0,0) /*Constrained to actor size*/ );
102
103   Constraint constraint = Constraint::New<Vector2>( maskEffect, maskEffect.GetPropertyIndex("uImageSize"), NinePatchMaskEffectSizeConstraint );
104   constraint.AddSource( Source(actor, Actor::Property::SIZE) );
105   constraint.Apply();
106
107   maskEffect.SetUniform( "uMaskSize", maskSize );
108
109   // Actor must provide nine-patch style geometry for this effect to work
110   actor.SetStyle( ImageActor::STYLE_NINE_PATCH );
111   actor.SetNinePatchBorder( maskBorder );
112
113   actor.SetShaderEffect( maskEffect );
114 }
115
116 /**
117  * @brief Apply the mask effect to an ImageActor.
118  *
119  * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
120  * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
121  * @param [in] maskImage The path to a file containing the mask. The center pixels of the mask will be stretched.
122  */
123 inline void Apply( ImageActor actor, const std::string& maskImage )
124 {
125   const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
126
127   const float leftRight = (maskSize.GetWidth()  - 1.0f) * 0.5f;
128   const float topBottom = (maskSize.GetHeight() - 1.0f) * 0.5f;
129
130   DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), Vector4( leftRight, topBottom, leftRight, topBottom ) );
131 }
132
133 /**
134  * @brief Apply the mask effect to an ImageActor.
135  *
136  * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
137  * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
138  * @param [in] maskImage The path to a file containing the mask.
139  * @param [in] maskBorder Specifies the part of the mask image that will be stretched (left, top, right, bottom).
140  */
141 inline void Apply( ImageActor actor, const std::string& maskImage, const Vector4& maskBorder )
142 {
143   const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
144
145   DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), maskBorder );
146 }
147
148 } // namespace NinePatchMaskEffect
149
150 } // namespace Toolkit
151
152 } // namespace Dali
153
154 #endif // __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__