Stop CreateSolidActor from creating an RGBA BufferImage when it isn't needed
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / default-controls / solid-color-actor.cpp
1 /*
2  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
19 #include <dali/public-api/images/buffer-image.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
23
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace
32 {
33 const unsigned int MAX_BORDER_SIZE( 9 );
34 }
35
36 ImageActor CreateSolidColorActor( const Vector4& color, bool border, const Vector4& borderColor, const unsigned int borderSize )
37 {
38   ImageActor image;
39   if( borderSize > MAX_BORDER_SIZE )
40   {
41     return image;
42   }
43
44   const unsigned int bitmapWidth = borderSize * 2 + 2;
45   bool needAlphaChannel = (color.a < 1.0f) || ( border && borderColor.a < 1.0f );
46   BufferImage imageData;
47   if( needAlphaChannel )
48   {
49     imageData = BufferImage::New( bitmapWidth, bitmapWidth, Pixel::RGBA8888 );
50   }
51   else
52   {
53     imageData = BufferImage::New( bitmapWidth, bitmapWidth, Pixel::RGB888 );
54   }
55
56   // Create the image
57   PixelBuffer* pixbuf = imageData.GetBuffer();
58   if( !pixbuf )
59   {
60     return image;
61   }
62
63   Vector4 outerColor = color;
64   if ( border )
65   {
66     outerColor = borderColor;
67   }
68
69   // Using a (2 + border) x (2 + border) image gives a better blend with the GL implementation
70   // than a (1 + border) x (1 + border) image
71   const unsigned int bitmapSize = bitmapWidth * bitmapWidth;
72   const unsigned int topLeft = bitmapWidth * borderSize + borderSize;
73   const unsigned int topRight = topLeft + 1;
74   const unsigned int bottomLeft = bitmapWidth * (borderSize + 1) + borderSize;
75   const unsigned int bottomRight = bottomLeft + 1;
76
77   if( needAlphaChannel )
78   {
79     for( size_t i = 0; i < bitmapSize; ++i )
80     {
81       if( i == topLeft ||
82           i == topRight ||
83           i == bottomLeft ||
84           i == bottomRight )
85       {
86         pixbuf[i*4+0] = 0xFF * color.r;
87         pixbuf[i*4+1] = 0xFF * color.g;
88         pixbuf[i*4+2] = 0xFF * color.b;
89         pixbuf[i*4+3] = 0xFF * color.a;
90       }
91       else
92       {
93         pixbuf[i*4+0] = 0xFF * outerColor.r;
94         pixbuf[i*4+1] = 0xFF * outerColor.g;
95         pixbuf[i*4+2] = 0xFF * outerColor.b;
96         pixbuf[i*4+3] = 0xFF * outerColor.a;
97       }
98     }
99   }
100   else
101   {
102     for( size_t i = 0; i < bitmapSize; ++i )
103     {
104       if( i == topLeft ||
105           i == topRight ||
106           i == bottomLeft ||
107           i == bottomRight )
108       {
109         pixbuf[i*3+0] = 0xFF * color.r;
110         pixbuf[i*3+1] = 0xFF * color.g;
111         pixbuf[i*3+2] = 0xFF * color.b;
112       }
113       else
114       {
115         pixbuf[i*3+0] = 0xFF * outerColor.r;
116         pixbuf[i*3+1] = 0xFF * outerColor.g;
117         pixbuf[i*3+2] = 0xFF * outerColor.b;
118       }
119     }
120   }
121
122   imageData.Update();
123   image = ImageActor::New( imageData );
124   image.SetParentOrigin( ParentOrigin::CENTER );
125
126   if( border )
127   {
128     image.SetStyle( ImageActor::STYLE_NINE_PATCH );
129     image.SetNinePatchBorder( Vector4::ONE * (float)borderSize * 2.0f );
130   }
131
132   return image;
133 }
134
135 } // namespace Toolkit
136
137 } // namespace Dali