• 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         // don't need to remove image from the HashMap, it will unregister
106         // itself by calling onImageTextureDestroy().
107 
108         SkSafeUnref(image);
109     }
110 }
111 
onImageTextureDestroy(unsigned imgCRC)112 void ImagesManager::onImageTextureDestroy(unsigned imgCRC)
113 {
114     // NOTE: all unrefs must go through releaseImage, to ensure that
115     // onImageTextureDestroy is called under the m_imagesLock
116     m_images.remove(imgCRC);
117 }
118 
nbTextures()119 int ImagesManager::nbTextures()
120 {
121     android::Mutex::Autolock lock(m_imagesLock);
122     HashMap<unsigned, ImageTexture*>::iterator end = m_images.end();
123     int i = 0;
124     int nb = 0;
125     for (HashMap<unsigned, ImageTexture*>::iterator it = m_images.begin(); it != end; ++it) {
126         nb += it->second->nbTextures();
127         i++;
128     }
129     return nb;
130 }
131 
prepareTextures(GLWebViewState * state)132 bool ImagesManager::prepareTextures(GLWebViewState* state)
133 {
134     bool ret = false;
135     android::Mutex::Autolock lock(m_imagesLock);
136     HashMap<unsigned, ImageTexture*>::iterator end = m_images.end();
137     for (HashMap<unsigned, ImageTexture*>::iterator it = m_images.begin(); it != end; ++it) {
138         ret |= it->second->prepareGL(state);
139     }
140     return ret;
141 }
142 
143 } // namespace WebCore
144