2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/images/bitmap-image-impl.h>
21 #include <dali/integration-api/bitmap.h>
22 #include <dali/internal/event/images/bitmap-external.h>
23 #include <dali/internal/event/common/thread-local-storage.h>
24 #include <dali/internal/event/resources/resource-client.h>
25 #include <dali/internal/update/manager/update-manager.h>
26 #include <dali/internal/event/images/image-factory.h>
33 BitmapImage* BitmapImage::New( unsigned int width, unsigned int height, Pixel::Format pixelformat, LoadPolicy loadPol, ReleasePolicy releasePol )
35 BitmapImage* internal = new BitmapImage( width, height, pixelformat, loadPol, releasePol );
36 internal->Initialize();
40 BitmapImage* BitmapImage::New( PixelBuffer* pixBuf, unsigned int width, unsigned int height, Pixel::Format pixelformat, unsigned int stride, ReleasePolicy releasePol )
42 BitmapImage* internal = new BitmapImage( pixBuf, width, height, pixelformat, stride, releasePol );
43 internal->Initialize();
47 BitmapImage::BitmapImage(unsigned int width, unsigned int height, Pixel::Format pixelformat, LoadPolicy loadPol, ReleasePolicy releasePol)
48 : Image(loadPol, releasePol),
49 mIsDataExternal(false)
56 const ImageTicketPtr& t = mResourceClient->AllocateBitmapImage(width, height, width, height, pixelformat);
59 mTicket->AddObserver(*this);
62 BitmapImage::BitmapImage(PixelBuffer* pixBuf, unsigned int width, unsigned int height, Pixel::Format pixelformat, unsigned int stride, ReleasePolicy releasePol)
63 : Image(ImageLoadPolicyDefault, releasePol),
69 Integration::Bitmap* bitmap = new BitmapExternal(pixBuf, width, height, pixelformat, stride);
70 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(bitmap);
73 mTicket->AddObserver(*this);
76 BitmapImage::~BitmapImage()
80 void BitmapImage::Update( RectArea& updateArea )
85 // If updateArea is empty or same as image size, then pass on.
86 // If updateArea is larger than image size, throw exception
87 // Otherwise, copy updateArea window of pixelBuffer into newly
88 // allocated buffer and pass that to resource client. (it will
89 // tramp through to BitmapTexture eventually!)
90 mResourceClient->UpdateBitmapArea( mTicket, updateArea );
92 else if (mIsDataExternal && mBitmapCached)
94 // previously freed up resource memory, dali was informed about external BitmapImage put back on screen
95 Integration::Bitmap* bitmap = mBitmapCached.Get();
96 mTicket.Reset((mResourceClient->AddBitmapImage(bitmap)).Get());
98 mTicket->AddObserver(*this);
102 bool BitmapImage::IsDataExternal() const
104 return mIsDataExternal;
107 PixelBuffer* BitmapImage::GetBuffer()
109 PixelBuffer* buffer = NULL;
111 Integration::Bitmap* const bitmap = GetBitmap();
115 buffer = bitmap->GetBuffer();
120 unsigned int BitmapImage::GetBufferSize() const
122 unsigned int bufferSize = 0;
124 Integration::Bitmap* const bitmap = GetBitmap();
128 bufferSize = bitmap->GetBufferSize();
133 unsigned int BitmapImage::GetBufferStride() const
135 unsigned int bufferStride = 0;
137 Integration::Bitmap* const bitmap = GetBitmap();
141 Integration::Bitmap::PackedPixelsProfile* packedBitmap = bitmap->GetPackedPixelsProfile();
142 DALI_ASSERT_DEBUG(packedBitmap);
143 bufferStride = packedBitmap->GetBufferStride();
149 void BitmapImage::Initialize()
151 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
152 mResourceClient = &tls.GetResourceClient();
155 void BitmapImage::Connect()
159 // application owns bitmap buffer, don't do anything. BufferUpdated() has to be called manually.
165 if (mConnectionCount == 1)
167 if (!mTicket && mBitmapCached)
169 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmapCached.Get());
171 mTicket->AddObserver(*this);
176 void BitmapImage::Disconnect()
185 if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused)
187 mBitmapCached = mResourceClient->GetBitmap(mTicket);
188 // release image memory when it's not visible anymore (decrease ref. count of texture)
189 mTicket->RemoveObserver(*this);
194 Integration::Bitmap * BitmapImage::GetBitmap() const
196 Integration::Bitmap* bitmap = NULL;
200 bitmap = mResourceClient->GetBitmap(mTicket);
204 // off screen and freeing memory was requested
205 bitmap = mBitmapCached.Get();
208 DALI_ASSERT_DEBUG(bitmap);
213 } // namespace Internal