Merge "DALi Version 1.0.30" into tizen
[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, unsigned int height, Pixel::Format pixelFormat, ReleasePolicy releasePolicy)
42 {
43   FrameBufferImagePtr image = new FrameBufferImage(width, height, pixelFormat, releasePolicy);
44   image->Initialize();
45   return image;
46 }
47
48 FrameBufferImagePtr  FrameBufferImage::New( NativeImage& nativeImage )
49 {
50   FrameBufferImagePtr image = new FrameBufferImage(nativeImage);
51   image->Initialize();
52   return image;
53 }
54
55 FrameBufferImagePtr  FrameBufferImage::New( NativeImage& nativeImage, ReleasePolicy releasePolicy )
56 {
57   FrameBufferImagePtr image = new FrameBufferImage(nativeImage, releasePolicy);
58   image->Initialize();
59   return image;
60 }
61
62 FrameBufferImage::FrameBufferImage(unsigned int width, unsigned int height, Pixel::Format pixelFormat, ReleasePolicy releasePolicy)
63 : Image(releasePolicy),
64   mPixelFormat(pixelFormat)
65 {
66   mWidth  = width;
67   mHeight = height;
68 }
69
70 FrameBufferImage::FrameBufferImage( NativeImage& nativeImage )
71 : Image(),
72   mNativeImage(&nativeImage),
73   mPixelFormat(nativeImage.GetPixelFormat())
74 {
75   mWidth = nativeImage.GetWidth();
76   mHeight = nativeImage.GetHeight();
77 }
78
79 FrameBufferImage::FrameBufferImage( NativeImage& nativeImage, ReleasePolicy releasePolicy )
80 : Image(releasePolicy),
81   mNativeImage(&nativeImage),
82   mPixelFormat(nativeImage.GetPixelFormat())
83 {
84   mWidth = nativeImage.GetWidth();
85   mHeight = nativeImage.GetHeight();
86 }
87
88 void FrameBufferImage::Connect()
89 {
90   ++mConnectionCount;
91
92   if (mConnectionCount == 1)
93   {
94     // ticket was thrown away when related actors went offstage
95     if (!mTicket)
96     {
97       ResourceClient& resourceClient = ThreadLocalStorage::Get().GetResourceClient();
98       if (mNativeImage)
99       {
100         mTicket = resourceClient.AddFrameBufferImage(*mNativeImage);
101         mTicket->AddObserver(*this);
102       }
103       else
104       {
105         mTicket = resourceClient.AddFrameBufferImage(mWidth, mHeight, mPixelFormat);
106         mTicket->AddObserver(*this);
107       }
108     }
109   }
110 }
111
112 void FrameBufferImage::Disconnect()
113 {
114   if(!mTicket)
115   {
116     return;
117   }
118
119   DALI_ASSERT_DEBUG(mConnectionCount > 0);
120   --mConnectionCount;
121   if (mConnectionCount == 0 && mReleasePolicy == Dali::Image::UNUSED)
122   {
123     // release image memory when it's not visible anymore (decrease ref. count of texture)
124     mTicket->RemoveObserver(*this);
125     mTicket.Reset();
126   }
127 }
128
129 } // namespace Internal
130
131 } // namespace Dali