License conversion from Flora to Apache 2.0
[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/internal/event/common/thread-local-storage.h>
23 #include <dali/internal/event/resources/resource-client.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 FrameBufferImage::~FrameBufferImage()
32 {
33 }
34
35 FrameBufferImage::FrameBufferImage(unsigned int width, unsigned int height, Pixel::Format pixelFormat, ReleasePolicy releasePolicy)
36 : Image(Dali::Image::Immediate, releasePolicy),
37   mPixelFormat(pixelFormat)
38 {
39   mWidth  = width;
40   mHeight = height;
41 }
42
43 FrameBufferImage::FrameBufferImage( NativeImage& nativeImage )
44 : Image(Dali::Image::Immediate),
45   mNativeImage(&nativeImage),
46   mPixelFormat(nativeImage.GetPixelFormat())
47 {
48   mWidth = nativeImage.GetWidth();
49   mHeight = nativeImage.GetHeight();
50 }
51
52 FrameBufferImage::FrameBufferImage( NativeImage& nativeImage, ReleasePolicy releasePolicy )
53 : Image(Dali::Image::Immediate, releasePolicy),
54   mNativeImage(&nativeImage),
55   mPixelFormat(nativeImage.GetPixelFormat())
56 {
57   mWidth = nativeImage.GetWidth();
58   mHeight = nativeImage.GetHeight();
59 }
60
61 void FrameBufferImage::Connect()
62 {
63   ++mConnectionCount;
64
65   if (mConnectionCount == 1)
66   {
67     // ticket was thrown away when related actors went offstage
68     if (!mTicket)
69     {
70       ResourceClient& resourceClient = ThreadLocalStorage::Get().GetResourceClient();
71       if (mNativeImage)
72       {
73         mTicket = resourceClient.AddFrameBufferImage(*mNativeImage);
74         mTicket->AddObserver(*this);
75       }
76       else
77       {
78         mTicket = resourceClient.AddFrameBufferImage(mWidth, mHeight, mPixelFormat);
79         mTicket->AddObserver(*this);
80       }
81     }
82   }
83 }
84
85 void FrameBufferImage::Disconnect()
86 {
87   if(!mTicket)
88   {
89     return;
90   }
91
92   DALI_ASSERT_DEBUG(mConnectionCount > 0);
93   --mConnectionCount;
94   if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused)
95   {
96     // release image memory when it's not visible anymore (decrease ref. count of texture)
97     mTicket->RemoveObserver(*this);
98     mTicket.Reset();
99   }
100 }
101
102 } // namespace Internal
103
104 } // namespace Dali