OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ | 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |
6 #define CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ | 6 #define CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/linked_ptr.h" | 15 #include "base/linked_ptr.h" |
16 #include "base/lock.h" | 16 #include "base/lock.h" |
17 #include "base/timer.h" | 17 #include "base/timer.h" |
18 #include "chrome/browser/renderer_host/backing_store.h" | 18 #include "chrome/browser/renderer_host/backing_store.h" |
19 #include "chrome/common/notification_observer.h" | 19 #include "chrome/common/notification_observer.h" |
20 #include "chrome/common/notification_registrar.h" | 20 #include "chrome/common/notification_registrar.h" |
21 | 21 |
| 22 class GURL; |
22 class RenderWidgetHost; | 23 class RenderWidgetHost; |
23 class SkBitmap; | 24 class SkBitmap; |
24 class TabContents; | 25 class TabContents; |
25 | 26 |
26 class ThumbnailGenerator : NotificationObserver { | 27 class ThumbnailGenerator : NotificationObserver { |
27 public: | 28 public: |
28 typedef Callback1<const SkBitmap&>::Type ThumbnailReadyCallback; | 29 typedef Callback1<const SkBitmap&>::Type ThumbnailReadyCallback; |
| 30 // The result of clipping. This can be used to determine if the |
| 31 // generated thumbnail is good or not. |
| 32 enum ClipResult { |
| 33 // The source image is smaller. |
| 34 kSourceIsSmaller, |
| 35 // Wider than tall, clip horizontally. |
| 36 kWiderThanTall, |
| 37 // Taller than wide, clip vertically. |
| 38 kTallerThanWide, |
| 39 // The source and destination aspect ratios are identical. |
| 40 kNotClipped, |
| 41 }; |
| 42 |
| 43 // Bitmasks of options for generating a thumbnail. |
| 44 enum ThumbnailOptions { |
| 45 // No options. |
| 46 kNoOptions = 0, |
| 47 // Request a clipped thumbnail with the aspect ratio preserved. |
| 48 kClippedThumbnail = 1 << 0, |
| 49 }; |
| 50 |
29 // This class will do nothing until you call StartThumbnailing. | 51 // This class will do nothing until you call StartThumbnailing. |
30 ThumbnailGenerator(); | 52 ThumbnailGenerator(); |
31 ~ThumbnailGenerator(); | 53 ~ThumbnailGenerator(); |
32 | 54 |
33 // Ensures that we're properly hooked in to generated thumbnails. This can | 55 // Ensures that we're properly hooked in to generated thumbnails. This can |
34 // be called repeatedly and with wild abandon to no ill effect. | 56 // be called repeatedly and with wild abandon to no ill effect. |
35 void StartThumbnailing(); | 57 void StartThumbnailing(); |
36 | 58 |
37 // This registers a callback that can receive the resulting SkBitmap | 59 // This registers a callback that can receive the resulting SkBitmap |
38 // from the renderer when it is done rendering it. This differs | 60 // from the renderer when it is done rendering it. This differs |
(...skipping 15 matching lines...) Expand all Loading... |
54 void AskForSnapshot(RenderWidgetHost* renderer, | 76 void AskForSnapshot(RenderWidgetHost* renderer, |
55 bool prefer_backing_store, | 77 bool prefer_backing_store, |
56 ThumbnailReadyCallback* callback, | 78 ThumbnailReadyCallback* callback, |
57 gfx::Size page_size, | 79 gfx::Size page_size, |
58 gfx::Size desired_size); | 80 gfx::Size desired_size); |
59 | 81 |
60 // This returns a thumbnail of a fixed, small size for the given | 82 // This returns a thumbnail of a fixed, small size for the given |
61 // renderer. | 83 // renderer. |
62 SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const; | 84 SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const; |
63 | 85 |
| 86 // This returns a thumbnail of a fixed, small size for the given |
| 87 // renderer. |options| is a bitmask of ThumbnailOptions. If |
| 88 // |clip_result| is non-NULL, the result of clipping will be written. |
| 89 SkBitmap GetThumbnailForRendererWithOptions(RenderWidgetHost* renderer, |
| 90 int options, |
| 91 ClipResult* clip_result) const; |
| 92 |
64 // Start or stop monitoring notifications for |renderer| based on the value | 93 // Start or stop monitoring notifications for |renderer| based on the value |
65 // of |monitor|. | 94 // of |monitor|. |
66 void MonitorRenderer(RenderWidgetHost* renderer, bool monitor); | 95 void MonitorRenderer(RenderWidgetHost* renderer, bool monitor); |
67 | 96 |
68 #ifdef UNIT_TEST | 97 #ifdef UNIT_TEST |
69 // When true, the class will not use a timeout to do the expiration. This | 98 // When true, the class will not use a timeout to do the expiration. This |
70 // will cause expiration to happen on the next run of the message loop. | 99 // will cause expiration to happen on the next run of the message loop. |
71 // Unit tests case use this to test expiration by choosing when the message | 100 // Unit tests case use this to test expiration by choosing when the message |
72 // loop runs. | 101 // loop runs. |
73 void set_no_timeout(bool no_timeout) { no_timeout_ = no_timeout; } | 102 void set_no_timeout(bool no_timeout) { no_timeout_ = no_timeout; } |
74 #endif | 103 #endif |
75 | 104 |
| 105 // Calculates how "boring" a thumbnail is. The boring score is the |
| 106 // 0,1 ranged percentage of pixels that are the most common |
| 107 // luma. Higher boring scores indicate that a higher percentage of a |
| 108 // bitmap are all the same brightness. |
| 109 static double CalculateBoringScore(SkBitmap* bitmap); |
| 110 |
| 111 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the |
| 112 // desired width and the desired height. For instance, if the input |
| 113 // bitmap is vertically long (ex. 400x900) and the desired size is |
| 114 // square (ex. 100x100), the clipped bitmap will be the top half of the |
| 115 // input bitmap (400x400). |
| 116 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap, |
| 117 int desired_width, |
| 118 int desired_height, |
| 119 ClipResult* clip_result); |
| 120 |
| 121 // Update the thumbnail of the given URL if necessary. |
| 122 static void UpdateThumbnailIfNecessary(TabContents* tab_contents, |
| 123 const GURL& url); |
| 124 |
76 private: | 125 private: |
77 // RenderWidgetHostPaintingObserver implementation. | 126 // RenderWidgetHostPaintingObserver implementation. |
78 virtual void WidgetWillDestroyBackingStore(RenderWidgetHost* widget, | 127 virtual void WidgetWillDestroyBackingStore(RenderWidgetHost* widget, |
79 BackingStore* backing_store); | 128 BackingStore* backing_store); |
80 virtual void WidgetDidUpdateBackingStore(RenderWidgetHost* widget); | 129 virtual void WidgetDidUpdateBackingStore(RenderWidgetHost* widget); |
81 | 130 |
82 virtual void WidgetDidReceivePaintAtSizeAck( | 131 virtual void WidgetDidReceivePaintAtSizeAck( |
83 RenderWidgetHost* widget, | 132 RenderWidgetHost* widget, |
84 int tag, | 133 int tag, |
85 const gfx::Size& size); | 134 const gfx::Size& size); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // Map of callback objects by sequence number. | 171 // Map of callback objects by sequence number. |
123 struct AsyncRequestInfo; | 172 struct AsyncRequestInfo; |
124 typedef std::map<int, | 173 typedef std::map<int, |
125 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap; | 174 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap; |
126 ThumbnailCallbackMap callback_map_; | 175 ThumbnailCallbackMap callback_map_; |
127 | 176 |
128 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator); | 177 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator); |
129 }; | 178 }; |
130 | 179 |
131 #endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ | 180 #endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |
OLD | NEW |