[dali_1.1.10] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / images / frame-buffer-image-impl.cpp
1 /*
2  * Copyright (c) 2014 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 FrameBufferImage::~FrameBufferImage()
38 {
39 }
40
41 FrameBufferImagePtr  FrameBufferImage::New(unsigned int width,
42                                            unsigned int height,
43                                            Pixel::Format pixelFormat,
44                                            ReleasePolicy releasePolicy,
45                                            RenderBuffer::Format bufferformat)
46 {
47   FrameBufferImagePtr image = new FrameBufferImage(width, height, pixelFormat, releasePolicy, bufferformat);
48   image->Initialize();
49   return image;
50 }
51
52 FrameBufferImagePtr  FrameBufferImage::New( NativeImageInterface& nativeImage )
53 {
54   FrameBufferImagePtr image = new FrameBufferImage(nativeImage);
55   image->Initialize();
56   return image;
57 }
58
59 FrameBufferImagePtr  FrameBufferImage::New( NativeImageInterface& nativeImage, ReleasePolicy releasePolicy )
60 {
61   FrameBufferImagePtr image = new FrameBufferImage(nativeImage, releasePolicy);
62   image->Initialize();
63   return image;
64 }
65
66 FrameBufferImage::FrameBufferImage(unsigned int width, unsigned int height, Pixel::Format pixelFormat, ReleasePolicy releasePolicy, RenderBuffer::Format bufferformat)
67 : Image( releasePolicy ),
68   mNativeImage(),
69   mPixelFormat( pixelFormat ),
70   mBufferFormat( bufferformat )
71 {
72   mWidth  = width;
73   mHeight = height;
74 }
75
76 FrameBufferImage::FrameBufferImage( NativeImageInterface& nativeImage )
77 : Image(),
78   mNativeImage( &nativeImage ),
79   mPixelFormat( Pixel::FIRST_VALID_PIXEL_FORMAT ),
80   mBufferFormat( RenderBuffer::COLOR )
81 {
82   mWidth = nativeImage.GetWidth();
83   mHeight = nativeImage.GetHeight();
84 }
85
86 FrameBufferImage::FrameBufferImage( NativeImageInterface& nativeImage, ReleasePolicy releasePolicy )
87 : Image( releasePolicy ),
88   mNativeImage( &nativeImage ),
89   mPixelFormat( Pixel::FIRST_VALID_PIXEL_FORMAT ),
90   mBufferFormat( RenderBuffer::COLOR )
91 {
92   mWidth = nativeImage.GetWidth();
93   mHeight = nativeImage.GetHeight();
94 }
95
96 void FrameBufferImage::Connect()
97 {
98   ++mConnectionCount;
99
100   if (mConnectionCount == 1)
101   {
102     // ticket was thrown away when related actors went offstage
103     if (!mTicket)
104     {
105       ResourceClient& resourceClient = ThreadLocalStorage::Get().GetResourceClient();
106       if (mNativeImage)
107       {
108         mTicket = resourceClient.AddFrameBufferImage(*mNativeImage);
109         mTicket->AddObserver(*this);
110       }
111       else
112       {
113         mTicket = resourceClient.AddFrameBufferImage(mWidth, mHeight, mPixelFormat, mBufferFormat);
114         mTicket->AddObserver(*this);
115       }
116     }
117   }
118 }
119
120 void FrameBufferImage::Disconnect()
121 {
122   if(!mTicket)
123   {
124     return;
125   }
126
127   DALI_ASSERT_DEBUG(mConnectionCount > 0);
128   --mConnectionCount;
129   if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::UNUSED)
130   {
131     // release image memory when it's not visible anymore (decrease ref. count of texture)
132     mTicket->RemoveObserver(*this);
133     mTicket.Reset();
134   }
135 }
136
137 } // namespace Internal
138
139 } // namespace Dali