• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "ImagesManager"
27 #define LOG_NDEBUG 1
28 
29 #include "config.h"
30 #include "ImagesManager.h"
31 
32 #include "AndroidLog.h"
33 #include "SkCanvas.h"
34 #include "SkDevice.h"
35 #include "SkRefCnt.h"
36 #include "ImageTexture.h"
37 
38 namespace WebCore {
39 
instance()40 ImagesManager* ImagesManager::instance()
41 {
42     if (!gInstance)
43         gInstance = new ImagesManager();
44 
45     return gInstance;
46 }
47 
48 ImagesManager* ImagesManager::gInstance = 0;
49 
setImage(SkBitmapRef * imgRef)50 ImageTexture* ImagesManager::setImage(SkBitmapRef* imgRef)
51 {
52     if (!imgRef)
53         return 0;
54 
55     TRACE_METHOD();
56     SkBitmap* bitmap = &imgRef->bitmap();
57     ImageTexture* image = 0;
58     SkBitmap* img = 0;
59     unsigned crc = 0;
60 
61     crc = ImageTexture::computeCRC(bitmap);
62 
63     {
64         android::Mutex::Autolock lock(m_imagesLock);
65         if (m_images.contains(crc)) {
66             image = m_images.get(crc);
67             SkSafeRef(image);
68             return image;
69         }
70     }
71 
72     // the image is not in the map, we add it
73 
74     img = ImageTexture::convertBitmap(bitmap);
75     image = new ImageTexture(img, crc);
76 
77     android::Mutex::Autolock lock(m_imagesLock);
78     m_images.set(crc, image);
79 
80     return image;
81 }
82 
retainImage(unsigned imgCRC)83 ImageTexture* ImagesManager::retainImage(unsigned imgCRC)
84 {
85     if (!imgCRC)
86         return 0;
87 
88     android::Mutex::Autolock lock(m_imagesLock);
89     ImageTexture* image = 0;
90     if (m_images.contains(imgCRC)) {
91         image = m_images.get(imgCRC);
92         SkSafeRef(image);
93     }
94     return image;
95 }
96 
releaseImage(unsigned imgCRC)97 void ImagesManager::releaseImage(unsigned imgCRC)
98 {
99     if (!imgCRC)
100         return;
101 
102     android::Mutex::Autolock lock(m_imagesLock);
103     if (m_images.contains(imgCRC)) {
104         ImageTexture* image = m_images.get(imgCRC);
105         if (image->getRefCnt() == 1)
106             m_images.remove(imgCRC);
107         SkSafeUnref(image);
108     }
109 }
110 
nbTextures()111 int ImagesManager::nbTextures()
112 {
113     android::Mutex::Autolock lock(m_imagesLock);
114     HashMap<unsigned, ImageTexture*>::iterator end = m_images.end();
115     int i = 0;
116     int nb = 0;
117     for (HashMap<unsigned, ImageTexture*>::iterator it = m_images.begin(); it != end; ++it) {
118         nb += it->second->nbTextures();
119         i++;
120     }
121     return nb;
122 }
123 
prepareTextures(GLWebViewState * state)124 bool ImagesManager::prepareTextures(GLWebViewState* state)
125 {
126     bool ret = false;
127     android::Mutex::Autolock lock(m_imagesLock);
128     HashMap<unsigned, ImageTexture*>::iterator end = m_images.end();
129     for (HashMap<unsigned, ImageTexture*>::iterator it = m_images.begin(); it != end; ++it) {
130         ret |= it->second->prepareGL(state);
131     }
132     return ret;
133 }
134 
135 } // namespace WebCore
136