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 BitmapImagePtr BitmapImage::New( unsigned int width, unsigned int height, Pixel::Format pixelformat, LoadPolicy loadPol, ReleasePolicy releasePol )
35 BitmapImagePtr internal = new BitmapImage( width, height, pixelformat, loadPol, releasePol );
36 internal->Initialize();
40 BitmapImagePtr BitmapImage::New( PixelBuffer* pixBuf, unsigned int width, unsigned int height, Pixel::Format pixelformat, unsigned int stride, ReleasePolicy releasePol )
42 BitmapImagePtr 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)
51 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
52 mResourceClient = &tls.GetResourceClient();
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),
66 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
67 mResourceClient = &tls.GetResourceClient();
70 Integration::Bitmap* bitmap = new BitmapExternal(pixBuf, width, height, pixelformat, stride);
71 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(bitmap);
74 mTicket->AddObserver(*this);
77 BitmapImage::~BitmapImage()
81 void BitmapImage::Update( RectArea& updateArea )
86 // If updateArea is empty or same as image size, then pass on.
87 // If updateArea is larger than image size, throw exception
88 // Otherwise, copy updateArea window of pixelBuffer into newly
89 // allocated buffer and pass that to resource client. (it will
90 // tramp through to BitmapTexture eventually!)
91 mResourceClient->UpdateBitmapArea( mTicket, updateArea );
93 else if (mIsDataExternal && mBitmapCached)
95 // previously freed up resource memory, dali was informed about external BitmapImage put back on screen
96 Integration::Bitmap* bitmap = mBitmapCached.Get();
97 mTicket.Reset((mResourceClient->AddBitmapImage(bitmap)).Get());
99 mTicket->AddObserver(*this);
103 bool BitmapImage::IsDataExternal() const
105 return mIsDataExternal;
108 PixelBuffer* BitmapImage::GetBuffer()
110 PixelBuffer* buffer = NULL;
112 Integration::Bitmap* const bitmap = GetBitmap();
116 buffer = bitmap->GetBuffer();
121 unsigned int BitmapImage::GetBufferSize() const
123 unsigned int bufferSize = 0;
125 Integration::Bitmap* const bitmap = GetBitmap();
129 bufferSize = bitmap->GetBufferSize();
134 unsigned int BitmapImage::GetBufferStride() const
136 unsigned int bufferStride = 0;
138 Integration::Bitmap* const bitmap = GetBitmap();
142 Integration::Bitmap::PackedPixelsProfile* packedBitmap = bitmap->GetPackedPixelsProfile();
143 DALI_ASSERT_DEBUG(packedBitmap);
144 bufferStride = packedBitmap->GetBufferStride();
150 void BitmapImage::Connect()
154 // application owns bitmap buffer, don't do anything. BufferUpdated() has to be called manually.
160 if (mConnectionCount == 1)
162 if (!mTicket && mBitmapCached)
164 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmapCached.Get());
166 mTicket->AddObserver(*this);
171 void BitmapImage::Disconnect()
180 if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused)
182 mBitmapCached = mResourceClient->GetBitmap(mTicket);
183 // release image memory when it's not visible anymore (decrease ref. count of texture)
184 mTicket->RemoveObserver(*this);
189 Integration::Bitmap * BitmapImage::GetBitmap() const
191 Integration::Bitmap* bitmap = NULL;
195 bitmap = mResourceClient->GetBitmap(mTicket);
199 // off screen and freeing memory was requested
200 bitmap = mBitmapCached.Get();
203 DALI_ASSERT_DEBUG(bitmap);
208 } // namespace Internal