Refactoring TextureManager cache as Dali::FreeList
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / image-url-impl.cpp
1 /*
2  * Copyright (c) 2022 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-toolkit/internal/image-loader/image-url-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
23 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
24 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
25 #include <dali-toolkit/internal/visuals/visual-url.h>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal
32 {
33 ImageUrl::ImageUrl(Texture& texture)
34 {
35   mUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
36 }
37
38 ImageUrl::ImageUrl(const EncodedImageBuffer& encodedImageBuffer)
39 : mUrl("")
40 {
41   auto visualFactory = Dali::Toolkit::VisualFactory::Get();
42   if(visualFactory)
43   {
44     auto& textureManager = GetImplementation(visualFactory).GetTextureManager();
45     mUrl                 = textureManager.AddEncodedImageBuffer(encodedImageBuffer);
46   }
47 }
48
49 ImageUrl::~ImageUrl()
50 {
51   if(mUrl.size() > 0)
52   {
53     auto visualFactory = Dali::Toolkit::VisualFactory::Get();
54     if(visualFactory)
55     {
56       auto& textureManager = GetImplementation(visualFactory).GetTextureManager();
57       if(VisualUrl::TEXTURE == VisualUrl::GetProtocolType(mUrl))
58       {
59         textureManager.RemoveExternalTexture(mUrl);
60       }
61       else if(VisualUrl::BUFFER == VisualUrl::GetProtocolType(mUrl))
62       {
63         textureManager.RemoveEncodedImageBuffer(mUrl);
64       }
65     }
66   }
67 }
68
69 ImageUrlPtr ImageUrl::New(Texture& texture)
70 {
71   ImageUrlPtr imageUrlPtr = new ImageUrl(texture);
72   return imageUrlPtr;
73 }
74
75 ImageUrlPtr ImageUrl::New(const EncodedImageBuffer& encodedImageBuffer)
76 {
77   ImageUrlPtr imageUrlPtr = new ImageUrl(encodedImageBuffer);
78   return imageUrlPtr;
79 }
80
81 const std::string& ImageUrl::GetUrl() const
82 {
83   return mUrl;
84 }
85
86 } // End of namespace Internal
87
88 } // End of namespace Toolkit
89
90 } // End of namespace Dali