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