1 /*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "ImagesManager.h"
28
29 #include "ImageTexture.h"
30
31 #include <cutils/log.h>
32 #include <wtf/CurrentTime.h>
33 #include <wtf/text/CString.h>
34
35 #undef XLOGC
36 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "ImagesManager", __VA_ARGS__)
37
38 #ifdef DEBUG
39
40 #undef XLOG
41 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "ImagesManager", __VA_ARGS__)
42
43 #else
44
45 #undef XLOG
46 #define XLOG(...)
47
48 #endif // DEBUG
49
50 namespace WebCore {
51
instance()52 ImagesManager* ImagesManager::instance()
53 {
54 if (!gInstance)
55 gInstance = new ImagesManager();
56
57 return gInstance;
58 }
59
60 ImagesManager* ImagesManager::gInstance = 0;
61
addImage(SkBitmapRef * imgRef)62 void ImagesManager::addImage(SkBitmapRef* imgRef)
63 {
64 if (!imgRef)
65 return;
66
67 android::Mutex::Autolock lock(m_imagesLock);
68 if (!m_images.contains(imgRef))
69 m_images.set(imgRef, new ImageTexture(imgRef));
70 }
71
removeImage(SkBitmapRef * imgRef)72 void ImagesManager::removeImage(SkBitmapRef* imgRef)
73 {
74 android::Mutex::Autolock lock(m_imagesLock);
75 if (!m_images.contains(imgRef))
76 return;
77
78 ImageTexture* image = m_images.get(imgRef);
79 image->release();
80
81 if (!image->refCount()) {
82 m_images.remove(imgRef);
83 delete image;
84 }
85 }
86
showImages()87 void ImagesManager::showImages()
88 {
89 XLOGC("We have %d images", m_images.size());
90 HashMap<SkBitmapRef*, ImageTexture*>::iterator end = m_images.end();
91 int i = 0;
92 for (HashMap<SkBitmapRef*, ImageTexture*>::iterator it = m_images.begin(); it != end; ++it) {
93 XLOGC("Image %x (%d/%d) has %d references", it->first, i,
94 m_images.size(), it->second->refCount());
95 i++;
96 }
97 }
98
getTextureForImage(SkBitmapRef * img,bool retain)99 ImageTexture* ImagesManager::getTextureForImage(SkBitmapRef* img, bool retain)
100 {
101 android::Mutex::Autolock lock(m_imagesLock);
102 ImageTexture* image = m_images.get(img);
103 if (retain && image)
104 image->retain();
105 return image;
106 }
107
scheduleTextureUpload(ImageTexture * texture)108 void ImagesManager::scheduleTextureUpload(ImageTexture* texture)
109 {
110 if (m_imagesToUpload.contains(texture))
111 return;
112
113 texture->retain();
114 m_imagesToUpload.append(texture);
115 }
116
uploadTextures()117 bool ImagesManager::uploadTextures()
118 {
119 // scheduleUpload and uploadTextures are called on the same thread
120 if (!m_imagesToUpload.size())
121 return false;
122 ImageTexture* texture = m_imagesToUpload.last();
123 texture->uploadGLTexture();
124 m_imagesToUpload.removeLast();
125 removeImage(texture->imageRef());
126 return m_imagesToUpload.size();
127 }
128
129 } // namespace WebCore
130