From: Xiangyin Ma Date: Wed, 25 May 2016 14:22:58 +0000 (+0100) Subject: Change PixelData to use the handle/body pattern X-Git-Tag: dali_1.1.38~5^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F13%2F71513%2F5;p=platform%2Fcore%2Fuifw%2Fdali-core.git Change PixelData to use the handle/body pattern Change-Id: I54d2c96b1111f90dd214ac257b82ade79cdacb9a --- diff --git a/automated-tests/src/dali-devel/utc-Dali-Atlas.cpp b/automated-tests/src/dali-devel/utc-Dali-Atlas.cpp index bff4bab..78e49c2 100644 --- a/automated-tests/src/dali-devel/utc-Dali-Atlas.cpp +++ b/automated-tests/src/dali-devel/utc-Dali-Atlas.cpp @@ -46,12 +46,12 @@ void PrepareResourceImage( TestApplication& application, unsigned int imageWidth platform.SetSynchronouslyLoadedResource( resourcePtr ); } -PixelDataPtr CreatePixelData(unsigned int width, unsigned int height, Pixel::Format pixelFormat) +PixelData CreatePixelData(unsigned int width, unsigned int height, Pixel::Format pixelFormat) { unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( pixelFormat ); unsigned char* buffer = new unsigned char [ bufferSize ]; - return new PixelData( buffer, width, height, pixelFormat, PixelData::DELETE_ARRAY ); + return PixelData::New( buffer, width, height, pixelFormat, PixelData::DELETE_ARRAY ); } } @@ -168,7 +168,7 @@ int UtcDaliAtlasUpload01P(void) PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 ); DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 0, 16 ) ); - PixelDataPtr pixelData = CreatePixelData( 6,8,Pixel::RGBA8888 ); + PixelData pixelData = CreatePixelData( 6,8,Pixel::RGBA8888 ); DALI_TEST_CHECK( atlas.Upload( pixelData, 2, 32 ) ); TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); @@ -205,7 +205,7 @@ int UtcDaliAtlasUpload02P(void) PrepareResourceImage( application, 12, 12, Pixel::A8 ); DALI_TEST_CHECK( atlas.Upload( gTestImageFilename, 6, 6 ) ); - PixelDataPtr pixelData = CreatePixelData( 8,8,Pixel::LA88 ); + PixelData pixelData = CreatePixelData( 8,8,Pixel::LA88 ); DALI_TEST_CHECK( atlas.Upload( pixelData, 10, 10 ) ); TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); @@ -242,7 +242,7 @@ int UtcDaliAtlasUploadN(void) PrepareResourceImage( application, 16, 16, Pixel::RGBA8888 ); DALI_TEST_CHECK( !atlas.Upload( gTestImageFilename, 10, 10 ) ); - PixelDataPtr pixelData = CreatePixelData( 6,6,Pixel::RGBA8888 ); + PixelData pixelData = CreatePixelData( 6,6,Pixel::RGBA8888 ); DALI_TEST_CHECK( !atlas.Upload( pixelData, 11, 11 ) ); TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace(); diff --git a/automated-tests/src/dali-devel/utc-Dali-PixelData.cpp b/automated-tests/src/dali-devel/utc-Dali-PixelData.cpp index e22f41b..dda4296 100644 --- a/automated-tests/src/dali-devel/utc-Dali-PixelData.cpp +++ b/automated-tests/src/dali-devel/utc-Dali-PixelData.cpp @@ -33,13 +33,12 @@ int UtcDaliPixelData01(void) unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::RGB888 ); unsigned char* buffer= reinterpret_cast( malloc( bufferSize ) ); - PixelDataPtr pixelData = PixelData::New( buffer, width, height, Pixel::RGB888, PixelData::FREE ); + PixelData pixelData = PixelData::New( buffer, width, height, Pixel::RGB888, PixelData::FREE ); DALI_TEST_CHECK( pixelData ); - DALI_TEST_CHECK( pixelData->GetWidth() == width ); - DALI_TEST_CHECK( pixelData->GetHeight() == height ); - DALI_TEST_CHECK( pixelData->GetPixelFormat() == Pixel::RGB888 ); - DALI_TEST_CHECK( pixelData->GetBuffer() == buffer ); + DALI_TEST_CHECK( pixelData.GetWidth() == width ); + DALI_TEST_CHECK( pixelData.GetHeight() == height ); + DALI_TEST_CHECK( pixelData.GetPixelFormat() == Pixel::RGB888 ); END_TEST; } @@ -54,23 +53,48 @@ int UtcDaliPixelData02(void) unsigned char* buffer = new unsigned char [ bufferSize ]; buffer[0] = 'a'; - PixelDataPtr pixelData2 = PixelData::New( buffer, width, height, Pixel::L8, PixelData::DELETE_ARRAY ); + PixelData pixelData = PixelData::New( buffer, width, height, Pixel::L8, PixelData::DELETE_ARRAY ); - DALI_TEST_CHECK( pixelData2 ); - DALI_TEST_CHECK( pixelData2->GetBuffer()[0] == 'a' ); + DALI_TEST_CHECK( pixelData); + DALI_TEST_CHECK( pixelData.GetWidth() == width ); + DALI_TEST_CHECK( pixelData.GetHeight() == height ); + DALI_TEST_CHECK( pixelData.GetPixelFormat() == Pixel::L8 ); - buffer[0] = 'b'; - DALI_TEST_CHECK( pixelData2->GetBuffer()[0] == 'b' ); + END_TEST; +} + +int UtcDaliPixelDataCopyConstructor(void) +{ + TestApplication application; + + unsigned int width = 10u; + unsigned int height = 10u; + unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 ); + unsigned char* buffer = new unsigned char [ bufferSize ]; + PixelData pixelData = PixelData::New( buffer, width, height, Pixel::L8, PixelData::DELETE_ARRAY ); + PixelData pixelDataCopy(pixelData); + + DALI_TEST_EQUALS( (bool)pixelDataCopy, true, TEST_LOCATION ); END_TEST; } -int UtcDaliPixelDataNonCopyable(void) +int UtcDaliPixelDataAssignmentOperator(void) { - // we want to make sure that PixelData is not copyable (its copy constructor is not defined) - // this test will stop compiling if PixelData has compiler generated copy constructor - DALI_COMPILE_TIME_ASSERT( !__has_trivial_copy( PixelData ) ); + TestApplication application; + + unsigned int width = 10u; + unsigned int height = 10u; + unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 ); + unsigned char* buffer = new unsigned char [ bufferSize ]; + PixelData pixelData = PixelData::New( buffer, width, height, Pixel::L8, PixelData::DELETE_ARRAY ); + + PixelData pixelData2; + DALI_TEST_EQUALS( (bool)pixelData2, false, TEST_LOCATION ); + + pixelData2 = pixelData; + DALI_TEST_EQUALS( (bool)pixelData2, true, TEST_LOCATION ); - DALI_TEST_CHECK( true ); END_TEST; } + diff --git a/dali/devel-api/images/atlas.cpp b/dali/devel-api/images/atlas.cpp index cc0357b..38d9ac9 100644 --- a/dali/devel-api/images/atlas.cpp +++ b/dali/devel-api/images/atlas.cpp @@ -21,6 +21,7 @@ // INTERNAL INCLUDES #include #include +#include namespace Dali { @@ -59,11 +60,12 @@ bool Atlas::Upload( const std::string& url, return GetImplementation(*this).Upload( url, xOffset, yOffset ); } -bool Atlas::Upload( PixelDataPtr pixelData, +bool Atlas::Upload( PixelData pixelData, SizeType xOffset, SizeType yOffset ) { - return GetImplementation(*this).Upload( pixelData, xOffset, yOffset ); + Internal::PixelData& internalPixelData = GetImplementation( pixelData ); + return GetImplementation(*this).Upload( &internalPixelData, xOffset, yOffset ); } Atlas Atlas::DownCast( BaseHandle handle ) diff --git a/dali/devel-api/images/atlas.h b/dali/devel-api/images/atlas.h index caa785d..6c87678 100644 --- a/dali/devel-api/images/atlas.h +++ b/dali/devel-api/images/atlas.h @@ -125,7 +125,7 @@ public: * @param [in] xOffset Specifies an offset in the x direction within the atlas. * @param [in] yOffset Specifies an offset in the y direction within the atlas. */ - bool Upload( PixelDataPtr pixelData, + bool Upload( PixelData pixelData, SizeType xOffset, SizeType yOffset ); /** diff --git a/dali/devel-api/images/pixel-data.cpp b/dali/devel-api/images/pixel-data.cpp index 7b426ce..c9febc0 100644 --- a/dali/devel-api/images/pixel-data.cpp +++ b/dali/devel-api/images/pixel-data.cpp @@ -21,64 +21,59 @@ // EXTERNAL INLCUDES #include +// INTERNAL INCLUDES +#include + namespace Dali { -PixelData::PixelData( unsigned char* buffer, - unsigned int width, - unsigned int height, - Pixel::Format pixelFormat, - ReleaseFunction releaseFunction ) -: mBuffer( buffer ), - mWidth( width ), - mHeight( height ), - mPixelFormat( pixelFormat ), - mReleaseFunction( releaseFunction ) +PixelData PixelData::New(unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + ReleaseFunction releaseFunction) +{ + IntrusivePtr internal = Internal::PixelData::New( buffer, width, height, pixelFormat, releaseFunction ); + return PixelData( internal.Get() ); +} + +PixelData::PixelData() { } PixelData::~PixelData() { - if( mBuffer ) - { - if( mReleaseFunction == FREE) - { - free( mBuffer ); - } - else - { - delete[] mBuffer; - } - } - } +} -PixelDataPtr PixelData::New(unsigned char* buffer, - unsigned int width, - unsigned int height, - Pixel::Format pixelFormat, - ReleaseFunction releaseFunction) +PixelData::PixelData( Internal::PixelData* internal ) +: BaseHandle( internal ) { - return new PixelData( buffer, width, height, pixelFormat, releaseFunction ); } -unsigned int PixelData::GetWidth() const +PixelData::PixelData(const PixelData& handle) +: BaseHandle( handle ) { - return mWidth; } -unsigned int PixelData::GetHeight() const +PixelData& PixelData::operator=(const PixelData& rhs) { - return mHeight; + BaseHandle::operator=(rhs); + return *this; } -Pixel::Format PixelData::GetPixelFormat() const +unsigned int PixelData::GetWidth() const { - return mPixelFormat; + return GetImplementation(*this).GetWidth(); } -unsigned char* PixelData::GetBuffer() const +unsigned int PixelData::GetHeight() const +{ + return GetImplementation(*this).GetHeight(); +} + +Pixel::Format PixelData::GetPixelFormat() const { - return mBuffer; + return GetImplementation(*this).GetPixelFormat(); } } // namespace Dali diff --git a/dali/devel-api/images/pixel-data.h b/dali/devel-api/images/pixel-data.h index 9835a16..cecc3e6 100644 --- a/dali/devel-api/images/pixel-data.h +++ b/dali/devel-api/images/pixel-data.h @@ -19,21 +19,23 @@ */ #include -#include +#include namespace Dali { +namespace Internal +{ class PixelData; -typedef IntrusivePtr PixelDataPtr; +} /** - * @brief Reference counted pixel data . + * @brief The PixelData object holds a pixel buffer . * * The PixelData takes over the ownership of the pixel buffer. * The buffer memory must NOT be released outside of this class, instead, the PixelData object will release it automatically when the reference count falls to zero. */ -class DALI_IMPORT_API PixelData : public RefObject +class DALI_IMPORT_API PixelData : public BaseHandle { public: @@ -52,80 +54,60 @@ public: * @param [in] pixelFormat The pixel format * @param [in] releaseFunction The function used to release the memory. */ - static PixelDataPtr New( unsigned char* buffer, - unsigned int width, - unsigned int height, - Pixel::Format pixelFormat, - ReleaseFunction releaseFunction); + static PixelData New( unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + ReleaseFunction releaseFunction); /** - * Get the width of the buffer in pixels. - * @return The width of the buffer in pixels + * @brief Create an empty handle. + * + * Use PixelData::New() to create an initialized object. */ - unsigned int GetWidth() const; + PixelData(); /** - * Get the height of the buffer in pixels - * @return The height of the buffer in pixels + * Destructor */ - unsigned int GetHeight() const; - - /** - * Get the pixel format - * @return The pixel format - */ - Pixel::Format GetPixelFormat() const; + ~PixelData(); /** - * Get the pixel buffer if it's present. - * @return The buffer if exits, or NULL if there is no pixel buffer. + * @brief This copy constructor is required for (smart) pointer semantics. + * + * @param [in] handle A reference to the copied handle */ - unsigned char* GetBuffer() const; - -public: + PixelData(const PixelData& handle); /** - * @brief Constructor. + * @brief This assignment operator is required for (smart) pointer semantics. * - * @param [in] buffer The raw pixel data. - * @param [in] width Buffer width in pixels - * @param [in] height Buffer height in pixels - * @param [in] pixelFormat The pixel format - * @param [in] releaseFunction The function used to release the memory. + * @param [in] rhs A reference to the copied handle + * @return A reference to this */ - PixelData( unsigned char* buffer, - unsigned int width, - unsigned int height, - Pixel::Format pixelFormat, - ReleaseFunction releaseFunction ); + PixelData& operator=(const PixelData& rhs); /** - * @brief Destructor. - * - * Release the pixel buffer if exists. + * Get the width of the buffer in pixels. + * @return The width of the buffer in pixels */ - ~PixelData(); - - -private: + unsigned int GetWidth() const; - /* - * Undefined copy constructor. + /** + * Get the height of the buffer in pixels + * @return The height of the buffer in pixels */ - PixelData(const PixelData& other); + unsigned int GetHeight() const; - /* - * Undefined assignment operator. + /** + * Get the pixel format + * @return The pixel format */ - PixelData& operator = (const PixelData& other); + Pixel::Format GetPixelFormat() const; -private: +public: // Not intended for application developers - unsigned char* mBuffer; ///< The raw pixel data. - unsigned int mWidth; ///< Buffer width in pixels. - unsigned int mHeight; ///< Buffer height in pixels. - Pixel::Format mPixelFormat; ///< Pixel format - ReleaseFunction mReleaseFunction; ///< Function for releasing memory + explicit DALI_INTERNAL PixelData( Internal::PixelData* ); }; diff --git a/dali/internal/event/images/atlas-impl.h b/dali/internal/event/images/atlas-impl.h index 68caeb3..d7e704b 100644 --- a/dali/internal/event/images/atlas-impl.h +++ b/dali/internal/event/images/atlas-impl.h @@ -78,7 +78,7 @@ public: SizeType yOffset ); /** - * @copydoc Dali::Atlas::Upload( Dali::Atlas::PixelDataPtr, uint32_t ) + * @copydoc Dali::Atlas::Upload( Dali::PixelData, uint32_t, uint32_t ) */ bool Upload( PixelDataPtr pixelData, SizeType xOffset, diff --git a/dali/internal/event/images/pixel-data-impl.cpp b/dali/internal/event/images/pixel-data-impl.cpp new file mode 100644 index 0000000..479b0f9 --- /dev/null +++ b/dali/internal/event/images/pixel-data-impl.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace Internal +{ + +PixelData::PixelData( unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + Dali::PixelData::ReleaseFunction releaseFunction ) +: mBuffer( buffer ), + mWidth( width ), + mHeight( height ), + mPixelFormat( pixelFormat ), + mReleaseFunction( releaseFunction ) +{ +} + +PixelData::~PixelData() +{ + if( mBuffer ) + { + if( mReleaseFunction == Dali::PixelData::FREE) + { + free( mBuffer ); + } + else + { + delete[] mBuffer; + } + } + } + +PixelDataPtr PixelData::New(unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + Dali::PixelData::ReleaseFunction releaseFunction) +{ + return new PixelData( buffer, width, height, pixelFormat, releaseFunction ); +} + +unsigned int PixelData::GetWidth() const +{ + return mWidth; +} + +unsigned int PixelData::GetHeight() const +{ + return mHeight; +} + +Pixel::Format PixelData::GetPixelFormat() const +{ + return mPixelFormat; +} + +unsigned char* PixelData::GetBuffer() const +{ + return mBuffer; +} + +}// namespace Internal + +}// namespace Dali diff --git a/dali/internal/event/images/pixel-data-impl.h b/dali/internal/event/images/pixel-data-impl.h new file mode 100644 index 0000000..cf2b299 --- /dev/null +++ b/dali/internal/event/images/pixel-data-impl.h @@ -0,0 +1,149 @@ +#ifndef __DALI_INTERNAL_PIXEL_DATA_H__ +#define __DALI_INTERNAL_PIXEL_DATA_H__ + +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace Internal +{ + +class PixelData; +typedef IntrusivePtr PixelDataPtr; + +class PixelData : public BaseObject +{ +public: + + /** + * @brief Create a PixelData object. + * + * @param [in] buffer The raw pixel data. + * @param [in] width Buffer width in pixels + * @param [in] height Buffer height in pixels + * @param [in] pixelFormat The pixel format + * @param [in] releaseFunction The function used to release the memory. + */ + static PixelDataPtr New( unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + Dali::PixelData::ReleaseFunction releaseFunction); + + /** + * @brief Constructor. + * + * @param [in] buffer The raw pixel data. + * @param [in] width Buffer width in pixels + * @param [in] height Buffer height in pixels + * @param [in] pixelFormat The pixel format + * @param [in] releaseFunction The function used to release the memory. + */ + PixelData( unsigned char* buffer, + unsigned int width, + unsigned int height, + Pixel::Format pixelFormat, + Dali::PixelData::ReleaseFunction releaseFunction ); + +protected: + + /** + * @brief Destructor. + * + * Release the pixel buffer if exists. + */ + ~PixelData(); + +public: + + /** + * Get the width of the buffer in pixels. + * @return The width of the buffer in pixels + */ + unsigned int GetWidth() const; + + /** + * Get the height of the buffer in pixels + * @return The height of the buffer in pixels + */ + unsigned int GetHeight() const; + + /** + * Get the pixel format + * @return The pixel format + */ + Pixel::Format GetPixelFormat() const; + + /** + * Get the pixel buffer if it's present. + * @return The buffer if exists, or NULL if there is no pixel buffer. + */ + unsigned char* GetBuffer() const; + +private: + + /* + * Undefined copy constructor. + */ + PixelData(const PixelData& other); + + /* + * Undefined assignment operator. + */ + PixelData& operator = (const PixelData& other); + +private: + + unsigned char* mBuffer; ///< The raw pixel data. + unsigned int mWidth; ///< Buffer width in pixels. + unsigned int mHeight; ///< Buffer height in pixels. + Pixel::Format mPixelFormat; ///< Pixel format + Dali::PixelData::ReleaseFunction mReleaseFunction; ///< Function for releasing memory +}; + +} // namespace Internal + +/** + * Helper methods for public API + */ +inline Internal::PixelData& GetImplementation( Dali::PixelData& handle ) +{ + DALI_ASSERT_ALWAYS( handle && "handle is empty" ); + + BaseObject& object = handle.GetBaseObject(); + + return static_cast( object ); +} + +inline const Internal::PixelData& GetImplementation( const Dali::PixelData& handle ) +{ + DALI_ASSERT_ALWAYS( handle && "handle is empty" ); + + const BaseObject& object = handle.GetBaseObject(); + + return static_cast( object ); +} + +} // namespace Dali + +#endif // __DALI_INTERNAL_PIXEL_DATA_H__ diff --git a/dali/internal/event/resources/resource-client.h b/dali/internal/event/resources/resource-client.h index 78e9950..1de28df 100644 --- a/dali/internal/event/resources/resource-client.h +++ b/dali/internal/event/resources/resource-client.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/dali/internal/file.list b/dali/internal/file.list index b7a4fb0..98eba58 100644 --- a/dali/internal/file.list +++ b/dali/internal/file.list @@ -76,6 +76,7 @@ internal_src_files = \ $(internal_src_dir)/event/images/nine-patch-image-impl.cpp \ $(internal_src_dir)/event/images/resource-image-impl.cpp \ $(internal_src_dir)/event/images/native-image-impl.cpp \ + $(internal_src_dir)/event/images/pixel-data-impl.cpp \ $(internal_src_dir)/event/object/custom-object-internal.cpp \ $(internal_src_dir)/event/render-tasks/render-task-impl.cpp \ $(internal_src_dir)/event/render-tasks/render-task-list-impl.cpp \ diff --git a/dali/internal/render/gl-resources/gl-texture.h b/dali/internal/render/gl-resources/gl-texture.h index 1d18db0..0504dad 100644 --- a/dali/internal/render/gl-resources/gl-texture.h +++ b/dali/internal/render/gl-resources/gl-texture.h @@ -25,9 +25,9 @@ #include #include #include -#include #include #include +#include #include #include diff --git a/dali/internal/update/common/texture-cache-dispatcher.h b/dali/internal/update/common/texture-cache-dispatcher.h index 37b0039..e006754 100644 --- a/dali/internal/update/common/texture-cache-dispatcher.h +++ b/dali/internal/update/common/texture-cache-dispatcher.h @@ -26,8 +26,8 @@ #include #include #include -#include #include +#include #include // For RectArea #include #include diff --git a/dali/internal/update/resources/resource-manager.h b/dali/internal/update/resources/resource-manager.h index c87aee4..a8b9abb 100644 --- a/dali/internal/update/resources/resource-manager.h +++ b/dali/internal/update/resources/resource-manager.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include @@ -36,6 +35,7 @@ #include #include #include +#include #include #include #include