2 * Copyright (c) 2015 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/buffer-image-impl.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/resources/resource-client.h>
28 #include <dali/internal/update/manager/update-manager.h>
29 #include <dali/internal/event/images/image-factory.h>
31 using namespace Dali::Integration;
40 TypeRegistration mType( typeid( Dali::BufferImage ), typeid( Dali::Image ), NULL );
41 } // unnamed namespace
43 BufferImagePtr BufferImage::New( unsigned int width,
45 Pixel::Format pixelformat,
46 ReleasePolicy releasePol )
48 BufferImagePtr internal = new BufferImage( width, height, pixelformat, releasePol );
49 internal->Initialize();
53 BufferImagePtr BufferImage::New( PixelBuffer* pixBuf,
56 Pixel::Format pixelformat,
58 ReleasePolicy releasePol )
60 BufferImagePtr internal = new BufferImage( pixBuf, width, height, pixelformat, stride, releasePol );
61 internal->Initialize();
65 BufferImage::BufferImage(unsigned int width, unsigned int height, Pixel::Format pixelformat, ReleasePolicy releasePol)
67 mInternalBuffer(NULL),
70 SetupBuffer( width, height, pixelformat, width, releasePol );
72 // Allocate a persistent internal buffer
73 mInternalBuffer = new PixelBuffer[ mBufferSize ];
76 BufferImage::BufferImage(PixelBuffer* pixBuf,
79 Pixel::Format pixelformat,
81 ReleasePolicy releasePol )
83 mInternalBuffer(NULL),
84 mExternalBuffer(pixBuf)
86 SetupBuffer( width, height, pixelformat, stride ? stride: width, releasePol );
89 BufferImage::~BufferImage()
91 delete[] mInternalBuffer;
94 void BufferImage::SetupBuffer( unsigned int width,
96 Pixel::Format pixelformat,
97 unsigned int byteStride,
98 ReleasePolicy releasePol )
100 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
101 mResourceClient = &tls.GetResourceClient();
104 mPixelFormat = pixelformat;
105 mBytesPerPixel = Pixel::GetBytesPerPixel( pixelformat );
107 mByteStride = byteStride * mBytesPerPixel;
108 mBufferSize = height * mByteStride;
110 // Respect the desired release policy
111 mResourcePolicy = releasePol == Dali::Image::UNUSED ? ResourcePolicy::OWNED_DISCARD : ResourcePolicy::OWNED_RETAIN;
114 bool BufferImage::IsDataExternal() const
116 return ( mExternalBuffer ? true : false );
119 void BufferImage::Update( RectArea& updateArea )
125 DALI_ASSERT_DEBUG( updateArea.x + updateArea.width <= mWidth && updateArea.y + updateArea.height <= mHeight );
126 UploadArea( mTicket->GetId(), updateArea );
129 void BufferImage::CreateHostBitmap()
131 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
132 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
133 DALI_ASSERT_DEBUG(packedBitmap);
135 packedBitmap->ReserveBuffer( mPixelFormat, mWidth, mHeight );
136 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
137 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mHeight * mWidth * mBytesPerPixel );
139 mTicket = mResourceClient->AddBitmapImage( bitmap );
140 mTicket->AddObserver(*this);
143 void BufferImage::UploadArea( ResourceId destId, const RectArea& area )
145 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
146 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
147 DALI_ASSERT_DEBUG(packedBitmap);
148 DALI_ASSERT_DEBUG( area.width <= mWidth && area.height <= mHeight );
150 mBufferWidth = area.width ? area.width : mWidth;
151 packedBitmap->ReserveBuffer( mPixelFormat, mBufferWidth, area.height ? area.height : mHeight );
152 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
153 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mBufferWidth * ( area.height ? area.height : mHeight ) * mBytesPerPixel );
155 // Are we uploading from an external or internal buffer ?
156 if ( mExternalBuffer )
158 // Check if we're doing the entire area without stride mismatch between source and dest ?
159 if( ( mByteStride == mWidth * mBytesPerPixel ) && area.IsEmpty() )
161 memcpy( bitmap->GetBuffer(), mExternalBuffer, mBufferSize );
165 UpdateBufferArea( mExternalBuffer, bitmap->GetBuffer(), area );
170 // Check if we're doing the entire internal buffer ?
173 memcpy( bitmap->GetBuffer(), mInternalBuffer, bitmap->GetBufferSize() );
177 UpdateBufferArea( mInternalBuffer, bitmap->GetBuffer(), area );
180 mResourceClient->UploadBitmap( destId, bitmap, area.x, area.y );
184 void BufferImage::UploadBitmap( ResourceId destId, std::size_t xOffset, std::size_t yOffset )
186 RectArea area( xOffset, yOffset, 0, 0 );
192 UploadArea( destId, area );
195 void BufferImage::UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area )
197 DALI_ASSERT_DEBUG( area.x + area.width <= mWidth && area.y + area.height <= mHeight );
199 uint32_t width = mBufferWidth * mBytesPerPixel;
201 src += ( area.y * mByteStride ) + ( area.x * mBytesPerPixel );
202 for ( uint32_t i = 0; i < area.height; ++i )
204 memcpy( dest, src, width );
210 void BufferImage::Connect()
212 if ( !mConnectionCount++ )
219 void BufferImage::Disconnect()
223 if ( !( --mConnectionCount ) && mReleasePolicy == Dali::Image::UNUSED )
225 mTicket->RemoveObserver(*this);
231 } // namespace Internal