Remove current and future memory leaks with messages by forcing the use of OwnerPoint...
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-impl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/texture-impl.h> // Dali::Internal::Texture
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/update-manager.h>
23 #include <dali/internal/event/common/stage-impl.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29
30 TexturePtr Texture::New(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
31 {
32   TexturePtr texture( new Texture( type, format, width, height ) );
33   texture->Initialize();
34   return texture;
35 }
36
37 TexturePtr Texture::New( NativeImageInterface& nativeImageInterface )
38 {
39   TexturePtr texture( new Texture( &nativeImageInterface ) );
40   texture->Initialize();
41   return texture;
42 }
43
44 Render::Texture* Texture::GetRenderObject() const
45 {
46   return mRenderObject;
47 }
48
49 Texture::Texture(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
50 : mEventThreadServices( *Stage::GetCurrent() ),
51   mRenderObject( NULL ),
52   mNativeImage(),
53   mType( type ),
54   mFormat( format ),
55   mWidth( width ),
56   mHeight( height )
57 {
58 }
59
60 Texture::Texture( NativeImageInterfacePtr nativeImageInterface )
61 : mEventThreadServices( *Stage::GetCurrent() ),
62   mRenderObject( NULL ),
63   mNativeImage( nativeImageInterface ),
64   mType( TextureType::TEXTURE_2D ),
65   mFormat( Pixel::RGB888 ),
66   mWidth( nativeImageInterface->GetWidth() ),
67   mHeight( nativeImageInterface->GetHeight() )
68 {
69 }
70
71 void Texture::Initialize()
72 {
73   if( EventThreadServices::IsCoreRunning() )
74   {
75     if( mNativeImage )
76     {
77       mRenderObject = new Render::Texture( mNativeImage );
78     }
79     else
80     {
81       mRenderObject = new Render::Texture( mType, mFormat, mWidth, mHeight );
82     }
83
84     OwnerPointer< Render::Texture > transferOwnership( mRenderObject );
85     AddTexture( mEventThreadServices.GetUpdateManager(), transferOwnership );
86   }
87 }
88
89 Texture::~Texture()
90 {
91   if( EventThreadServices::IsCoreRunning() && mRenderObject )
92   {
93     RemoveTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
94   }
95 }
96
97 bool Texture::Upload( PixelDataPtr pixelData )
98 {
99   return Upload( pixelData, 0u, 0u, 0u, 0u, pixelData->GetWidth(), pixelData->GetHeight() );
100 }
101
102 bool Texture::Upload( PixelDataPtr pixelData,
103                          unsigned int layer, unsigned int mipmap,
104                          unsigned int xOffset, unsigned int yOffset,
105                          unsigned int width, unsigned int height )
106 {
107   bool result(false);
108   if( EventThreadServices::IsCoreRunning() && mRenderObject )
109   {
110     if( mNativeImage )
111     {
112       DALI_LOG_ERROR( "OpenGL ES does not support uploading data to native texture\n");
113     }
114     else
115     {
116       unsigned int pixelDataSize = pixelData->GetWidth()*pixelData->GetHeight();
117       if( pixelData->GetBuffer() == NULL || pixelDataSize == 0 )
118       {
119         DALI_LOG_ERROR( "PixelData is empty\n");
120       }
121       else
122       {
123         Pixel::Format pixelDataFormat = pixelData->GetPixelFormat();
124         if( ( pixelDataFormat == mFormat ) || ( (pixelDataFormat == Pixel::RGB888 ) && ( mFormat == Pixel::RGBA8888 ) ) )
125         {
126           if( pixelDataSize < width * height )
127           {
128             DALI_LOG_ERROR( "PixelData of an incorrect size when trying to update texture\n");
129           }
130           else if( ( xOffset + width  > ( mWidth  / (1<<mipmap) ) ) ||
131               ( yOffset + height > ( mHeight / (1<<mipmap) ) ) )
132           {
133             DALI_LOG_ERROR( "Texture update area out of bounds\n");
134           }
135           else
136           {
137             //Parameters are correct. Send message to upload data to the texture
138             UploadParams params = { layer, mipmap, xOffset, yOffset, width, height };
139             UploadTextureMessage( mEventThreadServices.GetUpdateManager(), *mRenderObject, pixelData, params );
140             result = true;
141           }
142         }
143         else
144         {
145           DALI_LOG_ERROR( "Bad format\n");
146         }
147       }
148     }
149   }
150
151   return result;
152 }
153
154 void Texture::GenerateMipmaps()
155 {
156   if( EventThreadServices::IsCoreRunning() && mRenderObject )
157   {
158     GenerateMipmapsMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject );
159   }
160 }
161
162 unsigned int Texture::GetWidth() const
163 {
164   return mWidth;
165 }
166
167 unsigned int Texture::GetHeight() const
168 {
169   return mHeight;
170 }
171
172 } // namespace Internal
173 } // namespace Dali