2 * Copyright (c) 2016 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>
30 using namespace Dali::Integration;
39 TypeRegistration mType( typeid( Dali::BufferImage ), typeid( Dali::Image ), NULL );
40 } // unnamed namespace
42 BufferImagePtr BufferImage::New( unsigned int width,
44 Pixel::Format pixelformat )
46 BufferImagePtr internal = new BufferImage( width, height, pixelformat );
47 internal->Initialize();
51 BufferImagePtr BufferImage::New( PixelBuffer* pixBuf,
54 Pixel::Format pixelformat,
57 BufferImagePtr internal = new BufferImage( pixBuf, width, height, pixelformat, stride );
58 internal->Initialize();
62 BufferImage::BufferImage(unsigned int width, unsigned int height, Pixel::Format pixelformat)
64 mInternalBuffer( NULL ),
65 mExternalBuffer( NULL ),
66 mResourceClient( NULL ),
71 mPixelFormat( pixelformat ),
72 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
74 SetupBuffer( width, height, pixelformat, width );
76 // Allocate a persistent internal buffer
77 mInternalBuffer = new PixelBuffer[ mBufferSize ];
80 BufferImage::BufferImage(PixelBuffer* pixBuf,
83 Pixel::Format pixelformat,
86 mInternalBuffer( NULL ),
87 mExternalBuffer( pixBuf ),
88 mResourceClient( NULL ),
93 mPixelFormat( pixelformat ),
94 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
96 SetupBuffer( width, height, pixelformat, stride ? stride: width );
99 BufferImage::~BufferImage()
103 mTicket->RemoveObserver(*this);
107 delete[] mInternalBuffer;
110 void BufferImage::SetupBuffer( unsigned int width,
112 Pixel::Format pixelformat,
113 unsigned int byteStride )
115 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
116 mResourceClient = &tls.GetResourceClient();
119 mPixelFormat = pixelformat;
120 mBytesPerPixel = Pixel::GetBytesPerPixel( pixelformat );
122 mByteStride = byteStride * mBytesPerPixel;
123 mBufferSize = height * mByteStride;
125 // Respect the desired release policy
126 mResourcePolicy = ResourcePolicy::OWNED_RETAIN;
129 bool BufferImage::IsDataExternal() const
131 return ( mExternalBuffer ? true : false );
134 void BufferImage::Update( RectArea& updateArea )
140 DALI_ASSERT_DEBUG( updateArea.x + updateArea.width <= mWidth && updateArea.y + updateArea.height <= mHeight );
141 UploadArea( mTicket->GetId(), updateArea );
144 void BufferImage::CreateHostBitmap()
146 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
147 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
148 DALI_ASSERT_DEBUG(packedBitmap);
150 packedBitmap->ReserveBuffer( mPixelFormat, mWidth, mHeight );
151 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
152 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mHeight * mWidth * mBytesPerPixel );
154 mTicket = mResourceClient->AddBitmapImage( bitmap );
155 mTicket->AddObserver(*this);
158 void BufferImage::UploadArea( ResourceId destId, const RectArea& area )
160 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
161 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
162 DALI_ASSERT_DEBUG(packedBitmap);
163 DALI_ASSERT_DEBUG( area.width <= mWidth && area.height <= mHeight );
165 mBufferWidth = area.width ? area.width : mWidth;
166 packedBitmap->ReserveBuffer( mPixelFormat, mBufferWidth, area.height ? area.height : mHeight );
167 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
168 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mBufferWidth * ( area.height ? area.height : mHeight ) * mBytesPerPixel );
170 // Are we uploading from an external or internal buffer ?
171 if ( mExternalBuffer )
173 // Check if we're doing the entire area without stride mismatch between source and dest ?
174 if( ( mByteStride == mWidth * mBytesPerPixel ) && area.IsEmpty() )
176 memcpy( bitmap->GetBuffer(), mExternalBuffer, mBufferSize );
180 UpdateBufferArea( mExternalBuffer, bitmap->GetBuffer(), area );
185 // Check if we're doing the entire internal buffer ?
188 memcpy( bitmap->GetBuffer(), mInternalBuffer, bitmap->GetBufferSize() );
192 UpdateBufferArea( mInternalBuffer, bitmap->GetBuffer(), area );
195 mResourceClient->UploadBitmap( destId, bitmap, area.x, area.y );
199 void BufferImage::UploadBitmap( ResourceId destId, std::size_t xOffset, std::size_t yOffset )
201 RectArea area( xOffset, yOffset, 0, 0 );
207 UploadArea( destId, area );
210 void BufferImage::UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area )
212 DALI_ASSERT_DEBUG( area.x + area.width <= mWidth && area.y + area.height <= mHeight );
214 uint32_t width = mBufferWidth * mBytesPerPixel;
216 src += ( area.y * mByteStride ) + ( area.x * mBytesPerPixel );
217 for ( uint32_t i = 0; i < area.height; ++i )
219 memcpy( dest, src, width );
225 void BufferImage::Connect()
227 if ( !mConnectionCount++ && !mTicket )
234 void BufferImage::Disconnect()
239 } // namespace Internal