Cleaning up and optimizing scene-graph-material and scene-graph-renderer
[platform/core/uifw/dali-core.git] / dali / internal / event / images / frame-buffer-image-impl.cpp
1 /*
2  * Copyright (c) 2015 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/images/frame-buffer-image-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/thread-local-storage.h>
24 #include <dali/internal/event/resources/resource-client.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace
33 {
34 TypeRegistration mType( typeid( Dali::FrameBufferImage ), typeid( Dali::Image ), NULL );
35 } // unnamed namespace
36
37 FrameBufferImagePtr FrameBufferImage::New( unsigned int width,
38                                            unsigned int height,
39                                            Pixel::Format pixelFormat,
40                                            RenderBuffer::Format bufferformat )
41 {
42   FrameBufferImagePtr image = new FrameBufferImage( width, height, pixelFormat, bufferformat );
43   image->Initialize();
44   return image;
45 }
46
47 FrameBufferImagePtr FrameBufferImage::New( NativeImageInterface& nativeImage )
48 {
49   FrameBufferImagePtr image = new FrameBufferImage( nativeImage );
50   image->Initialize();
51   return image;
52 }
53
54
55 FrameBufferImage::FrameBufferImage( unsigned int width,
56                                     unsigned int height,
57                                     Pixel::Format pixelFormat,
58                                     RenderBuffer::Format bufferformat )
59 : Image(),
60   mNativeImage(0),
61   mPixelFormat( pixelFormat ),
62   mBufferFormat( bufferformat ),
63   mIsNativeFbo( false )
64 {
65   mWidth  = width;
66   mHeight = height;
67 }
68
69 FrameBufferImage::FrameBufferImage( NativeImageInterface& nativeImage )
70 : Image(),
71   mNativeImage( &nativeImage ),
72   mPixelFormat( Pixel::FIRST_VALID_PIXEL_FORMAT ),
73   mBufferFormat( RenderBuffer::COLOR ),
74   mIsNativeFbo( true )
75 {
76   mWidth = nativeImage.GetWidth();
77   mHeight = nativeImage.GetHeight();
78 }
79
80 void FrameBufferImage::Connect()
81 {
82   ++mConnectionCount;
83
84   if (mConnectionCount == 1)
85   {
86     // ticket was thrown away when related actors went offstage
87     if (!mTicket)
88     {
89       ResourceClient& resourceClient = ThreadLocalStorage::Get().GetResourceClient();
90       if (mNativeImage)
91       {
92         mTicket = resourceClient.AddFrameBufferImage(*mNativeImage);
93         mTicket->AddObserver(*this);
94       }
95       else
96       {
97         mTicket = resourceClient.AddFrameBufferImage(mWidth, mHeight, mPixelFormat, mBufferFormat);
98         mTicket->AddObserver(*this);
99       }
100     }
101   }
102 }
103
104 void FrameBufferImage::Disconnect()
105 {
106   if(!mTicket)
107   {
108     return;
109   }
110
111   DALI_ASSERT_DEBUG(mConnectionCount > 0);
112   --mConnectionCount;
113   if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::UNUSED)
114   {
115     // release image memory when it's not visible anymore (decrease ref. count of texture)
116     mTicket->RemoveObserver(*this);
117     mTicket.Reset();
118   }
119 }
120
121 bool FrameBufferImage::IsNativeFbo() const
122 {
123   return mIsNativeFbo;
124 }
125 FrameBufferImage::~FrameBufferImage()
126 {
127 }
128
129 } // namespace Internal
130
131 } // namespace Dali