Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / internal / event / images / atlas-impl.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 // CLASS HEADER
19 #include <dali/internal/event/images/atlas-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/thread-local-storage.h>
24 #include <dali/internal/event/images/bitmap-image-impl.h>
25 #include <dali/internal/event/resources/resource-client.h>
26 #include <dali/integration-api/bitmap.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36 TypeRegistration mType( typeid( Dali::Atlas ), typeid( Dali::Image ), NULL );
37 }
38
39 Atlas* Atlas::New( std::size_t width,
40                    std::size_t height,
41                    Pixel::Format pixelFormat )
42 {
43   return new Atlas( width, height, pixelFormat );
44 }
45
46 bool Atlas::Upload( const BitmapImage& bitmapImage,
47                     std::size_t xOffset,
48                     std::size_t yOffset )
49 {
50   bool uploadSuccess( false );
51
52   if( IsWithin(bitmapImage, xOffset, yOffset) )
53   {
54     ResourceId destId = GetResourceId();
55     ResourceId srcId = bitmapImage.GetResourceId();
56
57     if( destId && srcId )
58     {
59       mResourceClient.UploadBitmap( destId, srcId, xOffset, yOffset );
60       uploadSuccess = true;
61     }
62   }
63
64   return uploadSuccess;
65 }
66
67 Atlas::~Atlas()
68 {
69   ReleaseAtlas();
70 }
71
72 Atlas::Atlas( std::size_t width,
73               std::size_t height,
74               Pixel::Format pixelFormat )
75 : mResourceClient( ThreadLocalStorage::Get().GetResourceClient() ),
76   mPixelFormat( pixelFormat )
77 {
78   mWidth  = width;
79   mHeight = height;
80
81   if( Dali::Image::Immediate == mLoadPolicy )
82   {
83     AllocateAtlas();
84   }
85 }
86
87 void Atlas::Connect()
88 {
89   ++mConnectionCount;
90
91   if( Dali::Image::OnDemand == mLoadPolicy &&
92       mConnectionCount == 1 )
93   {
94     AllocateAtlas();
95   }
96 }
97
98 void Atlas::Disconnect()
99 {
100   if( mConnectionCount )
101   {
102     --mConnectionCount;
103
104     if ( Dali::Image::Unused == mReleasePolicy &&
105          mConnectionCount == 0 )
106     {
107       ReleaseAtlas();
108     }
109   }
110 }
111
112 bool Atlas::IsWithin( const BitmapImage& bitmapImage,
113                       std::size_t xOffset,
114                       std::size_t yOffset )
115 {
116   bool within(false);
117
118   if( mPixelFormat != bitmapImage.GetPixelFormat() )
119   {
120     DALI_LOG_ERROR( "Pixel format %d does not match Atlas format %d\n", bitmapImage.GetPixelFormat(), mPixelFormat );
121   }
122   else
123   {
124     const unsigned int width  = bitmapImage.GetWidth();
125     const unsigned int height = bitmapImage.GetHeight();
126
127     if( xOffset < mWidth  &&
128         yOffset < mHeight &&
129         xOffset+width  <= mWidth &&
130         yOffset+height <= mHeight )
131     {
132       within = true;
133     }
134     else
135     {
136       DALI_LOG_ERROR( "%dx%d image does not fit at offset %d,%d\n", width, height, xOffset, yOffset );
137     }
138   }
139
140   return within;
141 }
142
143 void Atlas::AllocateAtlas()
144 {
145   if( !mTicket )
146   {
147     mTicket = mResourceClient.AllocateTexture( mWidth, mHeight, mPixelFormat );
148   }
149 }
150
151 void Atlas::ReleaseAtlas()
152 {
153   mTicket.Reset();
154 }
155
156 } // namespace Internal
157
158 } // namespace Dali