Merge branch 'devel/master' 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     ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
97
98   maskEffect.SetEffectImage( ResourceImage::New( maskImage ) );
99
100   maskEffect.SetUniform( "uImageSize", Vector2(0,0) /*Constrained to actor size*/ );
101
102   Constraint constraint = Constraint::New<Vector2>( maskEffect, maskEffect.GetPropertyIndex("uImageSize"), NinePatchMaskEffectSizeConstraint );
103   constraint.AddSource( Source(actor, Actor::Property::SIZE) );
104   constraint.Apply();
105
106   maskEffect.SetUniform( "uMaskSize", maskSize );
107
108   // Actor must provide nine-patch style geometry for this effect to work
109   actor.SetStyle( ImageActor::STYLE_NINE_PATCH );
110   actor.SetNinePatchBorder( maskBorder );
111
112   actor.SetShaderEffect( maskEffect );
113 }
114
115 /**
116  * @brief Apply the mask effect to an ImageActor.
117  *
118  * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
119  * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
120  * @param [in] maskImage The path to a file containing the mask. The center pixels of the mask will be stretched.
121  */
122 inline void Apply( ImageActor actor, const std::string& maskImage )
123 {
124   const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
125
126   const float leftRight = (maskSize.GetWidth()  - 1.0f) * 0.5f;
127   const float topBottom = (maskSize.GetHeight() - 1.0f) * 0.5f;
128
129   DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), Vector4( leftRight, topBottom, leftRight, topBottom ) );
130 }
131
132 /**
133  * @brief Apply the mask effect to an ImageActor.
134  *
135  * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
136  * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
137  * @param [in] maskImage The path to a file containing the mask.
138  * @param [in] maskBorder Specifies the part of the mask image that will be stretched (left, top, right, bottom).
139  */
140 inline void Apply( ImageActor actor, const std::string& maskImage, const Vector4& maskBorder )
141 {
142   const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
143
144   DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), maskBorder );
145 }
146
147 } // namespace NinePatchMaskEffect
148
149 } // namespace Toolkit
150
151 } // namespace Dali
152
153 #endif // __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__