2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/images/bitmap-image-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/integration-api/bitmap.h>
24 #include <dali/internal/event/images/bitmap-external.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/resources/resource-client.h>
27 #include <dali/internal/update/manager/update-manager.h>
28 #include <dali/internal/event/images/image-factory.h>
37 TypeRegistration mType( typeid( Dali::BitmapImage ), typeid( Dali::Image ), NULL );
38 } // unnamed namespace
40 BitmapImagePtr BitmapImage::New( unsigned int width, unsigned int height, Pixel::Format pixelformat, LoadPolicy loadPol, ReleasePolicy releasePol )
42 BitmapImagePtr internal = new BitmapImage( width, height, pixelformat, loadPol, releasePol );
43 internal->Initialize();
47 BitmapImagePtr BitmapImage::New( PixelBuffer* pixBuf, unsigned int width, unsigned int height, Pixel::Format pixelformat, unsigned int stride, ReleasePolicy releasePol )
49 BitmapImagePtr internal = new BitmapImage( pixBuf, width, height, pixelformat, stride, releasePol );
50 internal->Initialize();
54 BitmapImage::BitmapImage(unsigned int width, unsigned int height, Pixel::Format pixelformat, LoadPolicy loadPol, ReleasePolicy releasePol)
55 : Image(loadPol, releasePol),
56 mIsDataExternal(false)
58 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
59 mResourceClient = &tls.GetResourceClient();
63 const ImageTicketPtr& t = mResourceClient->AllocateBitmapImage(width, height, width, height, pixelformat);
66 mTicket->AddObserver(*this);
69 BitmapImage::BitmapImage(PixelBuffer* pixBuf, unsigned int width, unsigned int height, Pixel::Format pixelformat, unsigned int stride, ReleasePolicy releasePol)
70 : Image(ImageLoadPolicyDefault, releasePol),
73 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
74 mResourceClient = &tls.GetResourceClient();
77 Integration::Bitmap* bitmap = new BitmapExternal(pixBuf, width, height, pixelformat, stride);
78 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(bitmap);
81 mTicket->AddObserver(*this);
84 BitmapImage::~BitmapImage()
88 void BitmapImage::Update( RectArea& updateArea )
93 // If updateArea is empty or same as image size, then pass on.
94 // If updateArea is larger than image size, throw exception
95 // Otherwise, copy updateArea window of pixelBuffer into newly
96 // allocated buffer and pass that to resource client. (it will
97 // tramp through to BitmapTexture eventually!)
98 mResourceClient->UpdateBitmapArea( mTicket, updateArea );
100 else if (mIsDataExternal && mBitmapCached)
102 // previously freed up resource memory, dali was informed about external BitmapImage put back on screen
103 Integration::Bitmap* bitmap = mBitmapCached.Get();
104 mTicket.Reset((mResourceClient->AddBitmapImage(bitmap)).Get());
106 mTicket->AddObserver(*this);
110 bool BitmapImage::IsDataExternal() const
112 return mIsDataExternal;
115 PixelBuffer* BitmapImage::GetBuffer()
117 PixelBuffer* buffer = NULL;
119 Integration::Bitmap* const bitmap = GetBitmap();
123 buffer = bitmap->GetBuffer();
128 unsigned int BitmapImage::GetBufferSize() const
130 unsigned int bufferSize = 0;
132 Integration::Bitmap* const bitmap = GetBitmap();
136 bufferSize = bitmap->GetBufferSize();
141 unsigned int BitmapImage::GetBufferStride() const
143 unsigned int bufferStride = 0;
145 Integration::Bitmap* const bitmap = GetBitmap();
149 Integration::Bitmap::PackedPixelsProfile* packedBitmap = bitmap->GetPackedPixelsProfile();
150 DALI_ASSERT_DEBUG(packedBitmap);
151 bufferStride = packedBitmap->GetBufferStride();
157 Pixel::Format BitmapImage::GetPixelFormat() const
159 Pixel::Format format( Pixel::RGBA8888 );
161 Integration::Bitmap* const bitmap = GetBitmap();
165 format = bitmap->GetPixelFormat();
171 void BitmapImage::Connect()
175 // application owns bitmap buffer, don't do anything. BufferUpdated() has to be called manually.
181 if (mConnectionCount == 1)
183 if (!mTicket && mBitmapCached)
185 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmapCached.Get());
187 mTicket->AddObserver(*this);
192 void BitmapImage::Disconnect()
201 if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused)
203 mBitmapCached = mResourceClient->GetBitmap(mTicket);
204 // release image memory when it's not visible anymore (decrease ref. count of texture)
205 mTicket->RemoveObserver(*this);
210 Integration::Bitmap * BitmapImage::GetBitmap() const
212 Integration::Bitmap* bitmap = NULL;
216 bitmap = mResourceClient->GetBitmap(mTicket);
220 // off screen and freeing memory was requested
221 bitmap = mBitmapCached.Get();
224 DALI_ASSERT_DEBUG(bitmap);
229 } // namespace Internal