Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: chrome/browser/ui/app_list/fast_show_pickler.cc

Issue 23684003: Introduce FastShowPickler which will be used for app list cold start. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respond to comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/app_list/fast_show_pickler.h"
6
7 namespace {
8
9 using app_list::AppListItemModel;
10 using app_list::AppListModel;
11
12 // These have the same meaning as SkBitmap::Config. Reproduced here to insure
13 // against their value changing in Skia. If the order of these changes kVersion
14 // should be incremented.
15 enum ImageFormat {
16 NONE,
17 A1,
18 A8,
19 INDEX_8,
20 RGB_565,
21 ARGB_4444,
22 ARGB_8888,
23 };
24
25 bool FormatToConfig(ImageFormat format, SkBitmap::Config* out) {
26 switch (format) {
27 case NONE:
28 *out = SkBitmap::kNo_Config;
29 break;
30 case A1:
31 *out = SkBitmap::kA1_Config;
32 break;
33 case A8:
34 *out = SkBitmap::kA8_Config;
35 break;
36 case INDEX_8:
37 *out = SkBitmap::kIndex8_Config;
38 break;
39 case RGB_565:
40 *out = SkBitmap::kRGB_565_Config;
41 break;
42 case ARGB_4444:
43 *out = SkBitmap::kARGB_4444_Config;
44 break;
45 case ARGB_8888:
46 *out = SkBitmap::kARGB_8888_Config;
47 break;
48 default: return false;
49 }
50 return true;
51 }
52
53 bool ConfigToFormat(SkBitmap::Config config, ImageFormat* out) {
54 switch (config) {
55 case SkBitmap::kNo_Config:
56 *out = NONE;
57 break;
58 case SkBitmap::kA1_Config:
59 *out = A1;
60 break;
61 case SkBitmap::kA8_Config:
62 *out = A8;
63 break;
64 case SkBitmap::kIndex8_Config:
65 *out = INDEX_8;
66 break;
67 case SkBitmap::kRGB_565_Config:
68 *out = RGB_565;
69 break;
70 case SkBitmap::kARGB_4444_Config:
71 *out = ARGB_4444;
72 break;
73 case SkBitmap::kARGB_8888_Config:
74 *out = ARGB_8888;
75 break;
76 default: return false;
77 }
78 return true;
79 }
80
81 bool PickleImage(Pickle* pickle, const gfx::ImageSkia& image) {
82 std::vector<gfx::ImageSkiaRep> reps(image.image_reps());
83 pickle->WriteInt(static_cast<int>(reps.size()));
84 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = reps.begin();
85 it != reps.end(); ++it) {
86 pickle->WriteInt(static_cast<int>(it->scale_factor()));
87 pickle->WriteInt(it->pixel_width());
88 pickle->WriteInt(it->pixel_height());
89 ImageFormat format = NONE;
90 if (!ConfigToFormat(it->sk_bitmap().getConfig(), &format))
91 return false;
92 pickle->WriteInt(static_cast<int>(format));
93 int size = static_cast<int>(it->sk_bitmap().getSafeSize());
94 pickle->WriteInt(size);
95 SkBitmap bitmap = it->sk_bitmap();
96 SkAutoLockPixels lock(bitmap);
97 pickle->WriteBytes(bitmap.getPixels(), size);
98 }
99 return true;
100 }
101
102 bool UnpickleImage(PickleIterator* it, gfx::ImageSkia* out) {
103 int rep_count = 0;
104 if (!it->ReadInt(&rep_count))
105 return false;
106
107 gfx::ImageSkia result;
108 for (int i = 0; i < rep_count; ++i) {
109 int scale_factor = 0;
110 if (!it->ReadInt(&scale_factor))
111 return false;
112
113 int width = 0;
114 if (!it->ReadInt(&width))
115 return false;
116
117 int height = 0;
118 if (!it->ReadInt(&height))
119 return false;
120
121 int format_int = 0;
122 if (!it->ReadInt(&format_int))
123 return false;
124 ImageFormat format = static_cast<ImageFormat>(format_int);
125 SkBitmap::Config config = SkBitmap::kNo_Config;
126 if (!FormatToConfig(format, &config))
127 return false;
128
129 int size = 0;
130 if (!it->ReadInt(&size))
131 return false;
132
133 const char* pixels = NULL;
134 if (!it->ReadBytes(&pixels, size))
135 return false;
136
137 SkBitmap bitmap;
138 bitmap.setConfig(static_cast<SkBitmap::Config>(config), width, height);
139 if (!bitmap.allocPixels())
140 return false;
141 {
142 SkAutoLockPixels lock(bitmap);
143 memcpy(bitmap.getPixels(), pixels, bitmap.getSize());
144 }
145 result.AddRepresentation(
146 gfx::ImageSkiaRep(bitmap, static_cast<ui::ScaleFactor>(scale_factor)));
147 }
148
149 *out = result;
150 return true;
151 }
152
153 scoped_ptr<AppListItemModel> UnpickleAppListItemModel(PickleIterator* it) {
154 scoped_ptr<AppListItemModel> result(new AppListItemModel);
155 std::string id;
156 if (!it->ReadString(&id))
157 return scoped_ptr<AppListItemModel>();
158 result->set_app_id(id);
159 std::string title;
160 if (!it->ReadString(&title))
161 return scoped_ptr<AppListItemModel>();
162 result->SetTitle(title);
163 bool has_shadow = false;
164 if (!it->ReadBool(&has_shadow))
165 return scoped_ptr<AppListItemModel>();
166 gfx::ImageSkia icon;
167 if (!UnpickleImage(it, &icon))
168 return scoped_ptr<AppListItemModel>();
169 result->SetIcon(icon, has_shadow);
170 return result.Pass();
171 }
172
173
174 bool PickleAppListItemModel(Pickle* pickle, AppListItemModel* item) {
175 if (!pickle->WriteString(item->app_id()))
176 return false;
177 if (!pickle->WriteString(item->title()))
178 return false;
179 if (!pickle->WriteBool(item->has_shadow()))
180 return false;
181 if (!PickleImage(pickle, item->icon()))
182 return false;
183 return true;
184 }
185
186 void CopyOverItem(AppListItemModel* src_item, AppListItemModel* dest_item) {
187 dest_item->set_app_id(src_item->app_id());
188 dest_item->SetTitle(src_item->title());
189 dest_item->SetIcon(src_item->icon(), src_item->has_shadow());
190 }
191
192 } // namespace
193
194 // The version of the pickle format defined here. This needs to be incremented
195 // whenever this format is changed so new clients can invalidate old versions.
196 const int FastShowPickler::kVersion = 1;
197
198 scoped_ptr<Pickle> FastShowPickler::PickleAppListModelForFastShow(
199 AppListModel* model) {
200 scoped_ptr<Pickle> result(new Pickle);
201 if (!result->WriteInt(kVersion))
202 return scoped_ptr<Pickle>();
203 if (!result->WriteBool(model->signed_in()))
204 return scoped_ptr<Pickle>();
205 if (!result->WriteInt((int) model->apps()->item_count()))
206 return scoped_ptr<Pickle>();
207 for (size_t i = 0; i < model->apps()->item_count(); ++i) {
208 if (!PickleAppListItemModel(result.get(), model->apps()->GetItemAt(i)))
209 return scoped_ptr<Pickle>();
210 }
211 return result.Pass();
212 }
213
214 void FastShowPickler::CopyOver(AppListModel* src, AppListModel* dest) {
215 dest->apps()->DeleteAll();
216 dest->SetSignedIn(src->signed_in());
217 for (size_t i = 0; i < src->apps()->item_count(); i++) {
218 AppListItemModel* src_item = src->apps()->GetItemAt(i);
219 AppListItemModel* dest_item = new AppListItemModel;
220 CopyOverItem(src_item, dest_item);
221 dest->apps()->Add(dest_item);
222 }
223 }
224
225 scoped_ptr<AppListModel>
226 FastShowPickler::UnpickleAppListModelForFastShow(Pickle* pickle) {
227 PickleIterator it(*pickle);
228 int read_version = 0;
229 if (!it.ReadInt(&read_version))
230 return scoped_ptr<AppListModel>();
231 if (read_version != kVersion)
232 return scoped_ptr<AppListModel>();
233 int app_count = 0;
234 bool signed_in = false;
235 if (!it.ReadBool(&signed_in))
236 return scoped_ptr<AppListModel>();
237 if (!it.ReadInt(&app_count))
238 return scoped_ptr<AppListModel>();
239
240 scoped_ptr<AppListModel> model(new AppListModel);
241 model->SetSignedIn(signed_in);
242 for (int i = 0; i < app_count; ++i) {
243 scoped_ptr<AppListItemModel> item(UnpickleAppListItemModel(&it).Pass());
244 if (!item)
245 return scoped_ptr<AppListModel>();
246 model->apps()->Add(item.release());
247 }
248
249 return model.Pass();
250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698