|
1 /* |
|
2 * Copyright (C) 2013 Square, Inc. |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package com.squareup.picasso; |
|
17 |
|
18 import android.graphics.Bitmap; |
|
19 import android.os.Handler; |
|
20 import android.os.HandlerThread; |
|
21 import android.os.Looper; |
|
22 import android.os.Message; |
|
23 |
|
24 import static android.os.Process.THREAD_PRIORITY_BACKGROUND; |
|
25 |
|
26 class Stats { |
|
27 private static final int CACHE_HIT = 0; |
|
28 private static final int CACHE_MISS = 1; |
|
29 private static final int BITMAP_DECODE_FINISHED = 2; |
|
30 private static final int BITMAP_TRANSFORMED_FINISHED = 3; |
|
31 |
|
32 private static final String STATS_THREAD_NAME = Utils.THREAD_PREFIX + "Stats"; |
|
33 |
|
34 final HandlerThread statsThread; |
|
35 final Cache cache; |
|
36 final Handler handler; |
|
37 |
|
38 long cacheHits; |
|
39 long cacheMisses; |
|
40 long totalOriginalBitmapSize; |
|
41 long totalTransformedBitmapSize; |
|
42 long averageOriginalBitmapSize; |
|
43 long averageTransformedBitmapSize; |
|
44 int originalBitmapCount; |
|
45 int transformedBitmapCount; |
|
46 |
|
47 Stats(Cache cache) { |
|
48 this.cache = cache; |
|
49 this.statsThread = new HandlerThread(STATS_THREAD_NAME, THREAD_PRIORITY_BACKGROUND); |
|
50 this.statsThread.start(); |
|
51 this.handler = new StatsHandler(statsThread.getLooper(), this); |
|
52 } |
|
53 |
|
54 void dispatchBitmapDecoded(Bitmap bitmap) { |
|
55 processBitmap(bitmap, BITMAP_DECODE_FINISHED); |
|
56 } |
|
57 |
|
58 void dispatchBitmapTransformed(Bitmap bitmap) { |
|
59 processBitmap(bitmap, BITMAP_TRANSFORMED_FINISHED); |
|
60 } |
|
61 |
|
62 void dispatchCacheHit() { |
|
63 handler.sendEmptyMessage(CACHE_HIT); |
|
64 } |
|
65 |
|
66 void dispatchCacheMiss() { |
|
67 handler.sendEmptyMessage(CACHE_MISS); |
|
68 } |
|
69 |
|
70 void shutdown() { |
|
71 statsThread.quit(); |
|
72 } |
|
73 |
|
74 void performCacheHit() { |
|
75 cacheHits++; |
|
76 } |
|
77 |
|
78 void performCacheMiss() { |
|
79 cacheMisses++; |
|
80 } |
|
81 |
|
82 void performBitmapDecoded(long size) { |
|
83 originalBitmapCount++; |
|
84 totalOriginalBitmapSize += size; |
|
85 averageOriginalBitmapSize = getAverage(originalBitmapCount, totalOriginalBitmapSize); |
|
86 } |
|
87 |
|
88 void performBitmapTransformed(long size) { |
|
89 transformedBitmapCount++; |
|
90 totalTransformedBitmapSize += size; |
|
91 averageTransformedBitmapSize = getAverage(originalBitmapCount, totalTransformedBitmapSize); |
|
92 } |
|
93 |
|
94 synchronized StatsSnapshot createSnapshot() { |
|
95 return new StatsSnapshot(cache.maxSize(), cache.size(), cacheHits, cacheMisses, |
|
96 totalOriginalBitmapSize, totalTransformedBitmapSize, averageOriginalBitmapSize, |
|
97 averageTransformedBitmapSize, originalBitmapCount, transformedBitmapCount, |
|
98 System.currentTimeMillis()); |
|
99 } |
|
100 |
|
101 private void processBitmap(Bitmap bitmap, int what) { |
|
102 // Never send bitmaps to the handler as they could be recycled before we process them. |
|
103 int bitmapSize = Utils.getBitmapBytes(bitmap); |
|
104 handler.sendMessage(handler.obtainMessage(what, bitmapSize, 0)); |
|
105 } |
|
106 |
|
107 private static long getAverage(int count, long totalSize) { |
|
108 return totalSize / count; |
|
109 } |
|
110 |
|
111 private static class StatsHandler extends Handler { |
|
112 |
|
113 private final Stats stats; |
|
114 |
|
115 public StatsHandler(Looper looper, Stats stats) { |
|
116 super(looper); |
|
117 this.stats = stats; |
|
118 } |
|
119 |
|
120 @Override public void handleMessage(final Message msg) { |
|
121 switch (msg.what) { |
|
122 case CACHE_HIT: |
|
123 stats.performCacheHit(); |
|
124 break; |
|
125 case CACHE_MISS: |
|
126 stats.performCacheMiss(); |
|
127 break; |
|
128 case BITMAP_DECODE_FINISHED: |
|
129 stats.performBitmapDecoded(msg.arg1); |
|
130 break; |
|
131 case BITMAP_TRANSFORMED_FINISHED: |
|
132 stats.performBitmapTransformed(msg.arg1); |
|
133 break; |
|
134 default: |
|
135 Picasso.HANDLER.post(new Runnable() { |
|
136 @Override public void run() { |
|
137 throw new AssertionError("Unhandled stats message." + msg.what); |
|
138 } |
|
139 }); |
|
140 } |
|
141 } |
|
142 } |
|
143 } |