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/public-api/images/pixel-data.h>
27 #include <dali/internal/event/common/thread-local-storage.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();
48 internal->Update( RectArea() );
52 BufferImagePtr BufferImage::New( PixelBuffer* pixBuf,
55 Pixel::Format pixelformat,
58 BufferImagePtr internal = new BufferImage( pixBuf, width, height, pixelformat, stride );
59 internal->Initialize();
60 internal->Update( RectArea() );
64 BufferImage::BufferImage(unsigned int width, unsigned int height, Pixel::Format pixelformat)
66 mInternalBuffer( NULL ),
67 mExternalBuffer( 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 ),
93 mPixelFormat( pixelformat ),
94 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
96 SetupBuffer( width, height, pixelformat, stride ? stride: width );
99 BufferImage::~BufferImage()
101 delete[] mInternalBuffer;
104 void BufferImage::SetupBuffer( unsigned int width,
106 Pixel::Format pixelformat,
107 unsigned int byteStride )
111 mPixelFormat = pixelformat;
112 mBytesPerPixel = Pixel::GetBytesPerPixel( pixelformat );
114 mByteStride = byteStride * mBytesPerPixel;
115 mBufferSize = height * mByteStride;
117 // Respect the desired release policy
118 mResourcePolicy = ResourcePolicy::OWNED_RETAIN;
121 bool BufferImage::IsDataExternal() const
123 return ( mExternalBuffer ? true : false );
126 void BufferImage::Update( const RectArea& updateArea )
130 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, mPixelFormat, mWidth, mHeight );
132 DALI_ASSERT_DEBUG( updateArea.x + updateArea.width <= mWidth && updateArea.y + updateArea.height <= mHeight );
133 UploadArea( updateArea );
136 void BufferImage::UploadArea( const RectArea& area )
138 DALI_ASSERT_DEBUG( area.width <= mWidth && area.height <= mHeight );
140 mBufferWidth = area.width ? area.width : mWidth;
141 uint32_t bufferHeight = area.height ? area.height : mHeight;
142 uint32_t bufferSize = mBytesPerPixel * mBufferWidth * bufferHeight;
143 Dali::Integration::PixelBuffer* buffer = reinterpret_cast< Dali::Integration::PixelBuffer* >( malloc( bufferSize ) );
144 DALI_ASSERT_DEBUG(buffer != 0);
146 // Are we uploading from an external or internal buffer ?
147 if ( mExternalBuffer )
149 // Check if we're doing the entire area without stride mismatch between source and dest ?
150 if( ( mByteStride == mWidth * mBytesPerPixel ) && area.IsEmpty() )
152 memcpy( buffer, mExternalBuffer, mBufferSize );
156 UpdateBufferArea( mExternalBuffer, buffer, area );
161 // Check if we're doing the entire internal buffer ?
164 memcpy( buffer, mInternalBuffer, bufferSize );
168 UpdateBufferArea( mInternalBuffer, buffer, area );
171 PixelDataPtr pixelData = PixelData::New( buffer, bufferSize, mBufferWidth, bufferHeight, mPixelFormat, Dali::PixelData::FREE );
172 mTexture->Upload( pixelData, 0u, 0u, area.x, area.y, mBufferWidth, bufferHeight );
175 void BufferImage::UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area )
177 DALI_ASSERT_DEBUG( area.x + area.width <= mWidth && area.y + area.height <= mHeight );
179 uint32_t width = mBufferWidth * mBytesPerPixel;
181 src += ( area.y * mByteStride ) + ( area.x * mBytesPerPixel );
182 for ( uint32_t i = 0; i < area.height; ++i )
184 memcpy( dest, src, width );
190 } // namespace Internal