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>
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 )
47 BufferImagePtr internal = new BufferImage( width, height, pixelformat );
48 internal->Initialize();
52 BufferImagePtr BufferImage::New( PixelBuffer* pixBuf,
55 Pixel::Format pixelformat,
58 BufferImagePtr internal = new BufferImage( pixBuf, width, height, pixelformat, stride );
59 internal->Initialize();
63 BufferImage::BufferImage(unsigned int width, unsigned int height, Pixel::Format pixelformat)
65 mInternalBuffer( NULL ),
66 mExternalBuffer( NULL ),
67 mResourceClient( NULL ),
72 mPixelFormat( pixelformat ),
73 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
75 SetupBuffer( width, height, pixelformat, width );
77 // Allocate a persistent internal buffer
78 mInternalBuffer = new PixelBuffer[ mBufferSize ];
81 BufferImage::BufferImage(PixelBuffer* pixBuf,
84 Pixel::Format pixelformat,
87 mInternalBuffer( NULL ),
88 mExternalBuffer( pixBuf ),
89 mResourceClient( NULL ),
94 mPixelFormat( pixelformat ),
95 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
97 SetupBuffer( width, height, pixelformat, stride ? stride: width );
100 BufferImage::~BufferImage()
104 mTicket->RemoveObserver(*this);
108 delete[] mInternalBuffer;
111 void BufferImage::SetupBuffer( unsigned int width,
113 Pixel::Format pixelformat,
114 unsigned int byteStride )
116 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
117 mResourceClient = &tls.GetResourceClient();
120 mPixelFormat = pixelformat;
121 mBytesPerPixel = Pixel::GetBytesPerPixel( pixelformat );
123 mByteStride = byteStride * mBytesPerPixel;
124 mBufferSize = height * mByteStride;
126 // Respect the desired release policy
127 mResourcePolicy = ResourcePolicy::OWNED_RETAIN;
130 bool BufferImage::IsDataExternal() const
132 return ( mExternalBuffer ? true : false );
135 void BufferImage::Update( RectArea& updateArea )
141 DALI_ASSERT_DEBUG( updateArea.x + updateArea.width <= mWidth && updateArea.y + updateArea.height <= mHeight );
142 UploadArea( mTicket->GetId(), updateArea );
145 void BufferImage::CreateHostBitmap()
147 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
148 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
149 DALI_ASSERT_DEBUG(packedBitmap);
151 packedBitmap->ReserveBuffer( mPixelFormat, mWidth, mHeight );
152 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
153 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mHeight * mWidth * mBytesPerPixel );
155 mTicket = mResourceClient->AddBitmapImage( bitmap );
156 mTicket->AddObserver(*this);
159 void BufferImage::UploadArea( ResourceId destId, const RectArea& area )
161 Integration::Bitmap* bitmap = Bitmap::New( Bitmap::BITMAP_2D_PACKED_PIXELS, mResourcePolicy );
162 Bitmap::PackedPixelsProfile* const packedBitmap = bitmap->GetPackedPixelsProfile();
163 DALI_ASSERT_DEBUG(packedBitmap);
164 DALI_ASSERT_DEBUG( area.width <= mWidth && area.height <= mHeight );
166 mBufferWidth = area.width ? area.width : mWidth;
167 packedBitmap->ReserveBuffer( mPixelFormat, mBufferWidth, area.height ? area.height : mHeight );
168 DALI_ASSERT_DEBUG(bitmap->GetBuffer() != 0);
169 DALI_ASSERT_DEBUG(bitmap->GetBufferSize() >= mBufferWidth * ( area.height ? area.height : mHeight ) * mBytesPerPixel );
171 // Are we uploading from an external or internal buffer ?
172 if ( mExternalBuffer )
174 // Check if we're doing the entire area without stride mismatch between source and dest ?
175 if( ( mByteStride == mWidth * mBytesPerPixel ) && area.IsEmpty() )
177 memcpy( bitmap->GetBuffer(), mExternalBuffer, mBufferSize );
181 UpdateBufferArea( mExternalBuffer, bitmap->GetBuffer(), area );
186 // Check if we're doing the entire internal buffer ?
189 memcpy( bitmap->GetBuffer(), mInternalBuffer, bitmap->GetBufferSize() );
193 UpdateBufferArea( mInternalBuffer, bitmap->GetBuffer(), area );
196 mResourceClient->UploadBitmap( destId, bitmap, area.x, area.y );
200 void BufferImage::UploadBitmap( ResourceId destId, std::size_t xOffset, std::size_t yOffset )
202 RectArea area( xOffset, yOffset, 0, 0 );
208 UploadArea( destId, area );
211 void BufferImage::UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area )
213 DALI_ASSERT_DEBUG( area.x + area.width <= mWidth && area.y + area.height <= mHeight );
215 uint32_t width = mBufferWidth * mBytesPerPixel;
217 src += ( area.y * mByteStride ) + ( area.x * mBytesPerPixel );
218 for ( uint32_t i = 0; i < area.height; ++i )
220 memcpy( dest, src, width );
226 void BufferImage::Connect()
228 if ( !mConnectionCount++ && !mTicket )
235 void BufferImage::Disconnect()
240 } // namespace Internal