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;
37 BufferImagePtr BufferImage::New( unsigned int width,
39 Pixel::Format pixelformat )
41 BufferImagePtr internal = new BufferImage( width, height, pixelformat );
42 internal->Initialize();
43 internal->Update( RectArea() );
47 BufferImagePtr BufferImage::New( PixelBuffer* pixBuf,
50 Pixel::Format pixelformat,
53 BufferImagePtr internal = new BufferImage( pixBuf, width, height, pixelformat, stride );
54 internal->Initialize();
55 internal->Update( RectArea() );
59 BufferImage::BufferImage(unsigned int width, unsigned int height, Pixel::Format pixelformat)
61 mInternalBuffer( NULL ),
62 mExternalBuffer( NULL ),
67 mPixelFormat( pixelformat ),
68 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
70 SetupBuffer( width, height, pixelformat, width );
72 // Allocate a persistent internal buffer
73 mInternalBuffer = new PixelBuffer[ mBufferSize ];
76 BufferImage::BufferImage(PixelBuffer* pixBuf,
79 Pixel::Format pixelformat,
82 mInternalBuffer( NULL ),
83 mExternalBuffer( pixBuf ),
88 mPixelFormat( pixelformat ),
89 mResourcePolicy( ResourcePolicy::OWNED_DISCARD )
91 SetupBuffer( width, height, pixelformat, stride ? stride: width );
94 BufferImage::~BufferImage()
96 delete[] mInternalBuffer;
99 void BufferImage::SetupBuffer( unsigned int width,
101 Pixel::Format pixelformat,
102 unsigned int byteStride )
106 mPixelFormat = pixelformat;
107 mBytesPerPixel = Pixel::GetBytesPerPixel( pixelformat );
109 mByteStride = byteStride * mBytesPerPixel;
110 mBufferSize = height * mByteStride;
112 // Respect the desired release policy
113 mResourcePolicy = ResourcePolicy::OWNED_RETAIN;
116 bool BufferImage::IsDataExternal() const
118 return ( mExternalBuffer ? true : false );
121 void BufferImage::Update( const RectArea& updateArea )
125 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, mPixelFormat, mWidth, mHeight );
127 DALI_ASSERT_DEBUG( updateArea.x + updateArea.width <= mWidth && updateArea.y + updateArea.height <= mHeight );
128 UploadArea( updateArea );
131 void BufferImage::UploadArea( const RectArea& area )
133 DALI_ASSERT_DEBUG( area.width <= mWidth && area.height <= mHeight );
135 mBufferWidth = area.width ? area.width : mWidth;
136 uint32_t bufferHeight = area.height ? area.height : mHeight;
137 uint32_t bufferSize = mBytesPerPixel * mBufferWidth * bufferHeight;
138 Dali::Integration::PixelBuffer* buffer = reinterpret_cast< Dali::Integration::PixelBuffer* >( malloc( bufferSize ) );
139 DALI_ASSERT_DEBUG(buffer != 0);
141 // Are we uploading from an external or internal buffer ?
142 if ( mExternalBuffer )
144 // Check if we're doing the entire area without stride mismatch between source and dest ?
145 if( ( mByteStride == mWidth * mBytesPerPixel ) && area.IsEmpty() )
147 memcpy( buffer, mExternalBuffer, mBufferSize );
151 UpdateBufferArea( mExternalBuffer, buffer, area );
156 // Check if we're doing the entire internal buffer ?
159 memcpy( buffer, mInternalBuffer, bufferSize );
163 UpdateBufferArea( mInternalBuffer, buffer, area );
166 PixelDataPtr pixelData = PixelData::New( buffer, bufferSize, mBufferWidth, bufferHeight, mPixelFormat, Dali::PixelData::FREE );
167 mTexture->Upload( pixelData, 0u, 0u, area.x, area.y, mBufferWidth, bufferHeight );
170 void BufferImage::UpdateBufferArea( PixelBuffer* src, PixelBuffer* dest, const RectArea& area )
172 DALI_ASSERT_DEBUG( area.x + area.width <= mWidth && area.y + area.height <= mHeight );
174 uint32_t width = mBufferWidth * mBytesPerPixel;
176 src += ( area.y * mByteStride ) + ( area.x * mBytesPerPixel );
177 for ( uint32_t i = 0; i < area.height; ++i )
179 memcpy( dest, src, width );
185 } // namespace Internal