1/*
2 * Copyright 2010, 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 "ClassTracker"
27#define LOG_NDEBUG 1
28
29#include "config.h"
30#include "ClassTracker.h"
31
32#include "AndroidLog.h"
33#include "LayerAndroid.h"
34#include "TilesManager.h"
35
36#include <wtf/text/CString.h>
37
38#define DEBUG_LAYERS
39#undef DEBUG_LAYERS
40
41namespace WebCore {
42
43ClassTracker* ClassTracker::instance()
44{
45    if (!gInstance)
46        gInstance = new ClassTracker();
47    return gInstance;
48}
49
50ClassTracker* ClassTracker::gInstance = 0;
51
52void ClassTracker::increment(String name)
53{
54   android::Mutex::Autolock lock(m_lock);
55   int value = 0;
56   if (m_classes.contains(name))
57       value = m_classes.get(name);
58
59   m_classes.set(name, value + 1);
60}
61
62void ClassTracker::decrement(String name)
63{
64   android::Mutex::Autolock lock(m_lock);
65   int value = 0;
66   if (m_classes.contains(name))
67       value = m_classes.get(name);
68
69   m_classes.set(name, value - 1);
70}
71
72void ClassTracker::add(LayerAndroid* layer)
73{
74   android::Mutex::Autolock lock(m_lock);
75   m_layers.append(layer);
76}
77
78void ClassTracker::remove(LayerAndroid* layer)
79{
80   android::Mutex::Autolock lock(m_lock);
81   m_layers.remove(m_layers.find(layer));
82}
83
84void ClassTracker::show()
85{
86   android::Mutex::Autolock lock(m_lock);
87   ALOGD("*** Tracking %d classes ***", m_classes.size());
88   for (HashMap<String, int>::iterator iter = m_classes.begin(); iter != m_classes.end(); ++iter) {
89       ALOGD("class %s has %d instances",
90             iter->first.ascii().data(), iter->second);
91   }
92   ALOGD("*** %d Layers ***", m_layers.size());
93   int nbTextures = 0;
94   int nbAllocatedTextures = 0;
95   int nbLayerTextures = 0;
96   int nbAllocatedLayerTextures = 0;
97   float textureSize = 256 * 256 * 4 / 1024.0 / 1024.0;
98   TilesManager::instance()->gatherTexturesNumbers(&nbTextures, &nbAllocatedTextures,
99                                                   &nbLayerTextures, &nbAllocatedLayerTextures);
100   ALOGD("*** textures: %d/%d (%.2f Mb), layer textures: %d/%d (%.2f Mb) : total used %.2f Mb",
101         nbAllocatedTextures, nbTextures,
102         nbAllocatedTextures * textureSize,
103         nbAllocatedLayerTextures, nbLayerTextures,
104         nbAllocatedLayerTextures * textureSize,
105         (nbAllocatedTextures + nbAllocatedLayerTextures) * textureSize);
106
107#ifdef DEBUG_LAYERS
108   for (unsigned int i = 0; i < m_layers.size(); i++) {
109       LayerAndroid* layer = m_layers[i];
110       ALOGD("[%d/%d] layer %x (%.2f, %.2f) of type %d, refcount(%d) has texture %x has image ref %x (%x) root: %x parent: %x",
111             i, m_layers.size(), layer,
112             layer->getWidth(), layer->getHeight(),
113             layer->type(), layer->getRefCnt(),
114             layer->texture(), layer->imageRef(),
115             layer->imageTexture(), (LayerAndroid*) layer->getRootLayer(),
116             (LayerAndroid*) layer->getParent());
117   }
118#endif
119}
120
121} // namespace WebCore
122