Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 8 namespace { | |
| 9 | |
| 10 using app_list::AppListItemModel; | |
| 11 using app_list::AppListModel; | |
| 12 | |
| 13 gfx::ImageSkia UnpickleImage(PickleIterator* it) { | |
| 14 int rep_count = 0; | |
| 15 if (!it->ReadInt(&rep_count)) | |
| 16 return gfx::ImageSkia(); | |
| 17 | |
| 18 gfx::ImageSkia result; | |
| 19 for (int i = 0; i < rep_count; ++i) { | |
| 20 int scale_factor = 0; | |
| 21 if (!it->ReadInt(&scale_factor)) | |
| 22 return gfx::ImageSkia(); | |
| 23 | |
| 24 int width = 0; | |
| 25 if (!it->ReadInt(&width)) | |
| 26 return gfx::ImageSkia(); | |
| 27 | |
| 28 int height = 0; | |
| 29 if (!it->ReadInt(&height)) | |
| 30 return gfx::ImageSkia(); | |
| 31 | |
| 32 int config = 0; | |
| 33 if (!it->ReadInt(&config)) | |
| 34 return gfx::ImageSkia(); | |
| 35 | |
| 36 int size = 0; | |
| 37 if (!it->ReadInt(&size)) | |
| 38 return gfx::ImageSkia(); | |
| 39 | |
| 40 const char* pixels = NULL; | |
| 41 if (!it->ReadBytes(&pixels, size)) | |
|
xiyuan
2013/08/28 17:00:39
Can we ReadBytes into SkBitmap's pixel buffer to s
koz (OOO until 15th September)
2013/08/29 03:19:01
ReadBytes() has the effect of pointing |pixels| in
xiyuan
2013/08/29 16:52:11
You are right. Apparently, I have never used Pickl
| |
| 42 return gfx::ImageSkia(); | |
| 43 | |
| 44 SkBitmap bitmap; | |
| 45 bitmap.setConfig(static_cast<SkBitmap::Config>(config), width, height); | |
| 46 if (!bitmap.allocPixels()) | |
| 47 return gfx::ImageSkia(); | |
| 48 { | |
| 49 SkAutoLockPixels lock(bitmap); | |
| 50 memcpy(bitmap.getPixels(), pixels, bitmap.getSize()); | |
| 51 } | |
| 52 result.AddRepresentation( | |
| 53 gfx::ImageSkiaRep(bitmap, static_cast<ui::ScaleFactor>(scale_factor))); | |
| 54 } | |
| 55 | |
| 56 return result; | |
| 57 } | |
| 58 | |
| 59 scoped_ptr<AppListItemModel> UnpickleAppListItemModel(PickleIterator* it) { | |
| 60 scoped_ptr<AppListItemModel> result(new AppListItemModel); | |
| 61 std::string id; | |
| 62 if (!it->ReadString(&id)) | |
| 63 return scoped_ptr<AppListItemModel>(); | |
| 64 result->set_app_id(id); | |
| 65 std::string title; | |
| 66 if (!it->ReadString(&title)) | |
| 67 return scoped_ptr<AppListItemModel>(); | |
| 68 result->SetTitle(title); | |
| 69 bool has_shadow = false; | |
| 70 if (!it->ReadBool(&has_shadow)) | |
| 71 return scoped_ptr<AppListItemModel>(); | |
| 72 gfx::ImageSkia icon = UnpickleImage(it); | |
| 73 result->SetIcon(icon, has_shadow); | |
| 74 return result.Pass(); | |
| 75 } | |
| 76 | |
| 77 void PickleImage(Pickle* pickle, const gfx::ImageSkia& image) { | |
| 78 std::vector<gfx::ImageSkiaRep> reps(image.image_reps()); | |
| 79 pickle->WriteInt(static_cast<int>(reps.size())); | |
| 80 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = reps.begin(); | |
| 81 it != reps.end(); ++it) { | |
| 82 pickle->WriteInt(static_cast<int>(it->scale_factor())); | |
| 83 pickle->WriteInt(it->pixel_width()); | |
| 84 pickle->WriteInt(it->pixel_height()); | |
| 85 pickle->WriteInt(it->sk_bitmap().getConfig()); | |
|
xiyuan
2013/08/28 17:00:39
I might be a paranoid so not feel comfortable use
koz (OOO until 15th September)
2013/08/29 03:19:01
Done.
| |
| 86 int size = static_cast<int>(it->sk_bitmap().getSize()); | |
|
xiyuan
2013/08/28 17:00:39
getSafeSize() might be better for the usage here.
koz (OOO until 15th September)
2013/08/29 03:19:01
Done.
| |
| 87 pickle->WriteInt(size); | |
| 88 SkBitmap bitmap = it->sk_bitmap(); | |
| 89 SkAutoLockPixels lock(bitmap); | |
| 90 pickle->WriteBytes(bitmap.getPixels(), size); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void PickleAppListItemModel(Pickle* pickle, AppListItemModel* item) { | |
| 95 pickle->WriteString(item->app_id()); | |
| 96 pickle->WriteString(item->title()); | |
| 97 pickle->WriteBool(item->has_shadow()); | |
| 98 PickleImage(pickle, item->icon()); | |
| 99 } | |
| 100 | |
| 101 void CopyOverItem(AppListItemModel* src_item, AppListItemModel* dest_item) { | |
| 102 dest_item->set_app_id(src_item->app_id()); | |
| 103 dest_item->SetTitle(src_item->title()); | |
| 104 dest_item->SetIcon(src_item->icon(), src_item->has_shadow()); | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 // The version of the pickle format defined here. This needs to be incremented | |
| 110 // whenever this format is changed so new clients can invalidate old versions. | |
| 111 const int FastShowPickler::kVersion = 1; | |
| 112 | |
| 113 scoped_ptr<Pickle> FastShowPickler::PickleAppListModelForFastShow( | |
| 114 AppListModel* model) { | |
| 115 scoped_ptr<Pickle> result(new Pickle); | |
| 116 if (!result->WriteInt(kVersion)) | |
| 117 return scoped_ptr<Pickle>(); | |
| 118 if (!result->WriteInt((int) model->apps()->item_count())) | |
| 119 return scoped_ptr<Pickle>(); | |
| 120 if (!result->WriteBool(model->signed_in())) | |
| 121 return scoped_ptr<Pickle>(); | |
| 122 for (size_t i = 0; i < model->apps()->item_count(); ++i) | |
| 123 PickleAppListItemModel(result.get(), model->apps()->GetItemAt(i)); | |
| 124 return result.Pass(); | |
| 125 } | |
| 126 | |
| 127 void FastShowPickler::CopyOver(AppListModel* src, AppListModel* dest) { | |
| 128 dest->apps()->DeleteAll(); | |
| 129 for (size_t i = 0; i < src->apps()->item_count(); i++) { | |
| 130 AppListItemModel* src_item = src->apps()->GetItemAt(i); | |
| 131 AppListItemModel* dest_item = new AppListItemModel; | |
| 132 CopyOverItem(src_item, dest_item); | |
| 133 dest->apps()->Add(dest_item); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 scoped_ptr<AppListModel> | |
| 138 FastShowPickler::UnpickleAppListModelForFastShow(Pickle* pickle) { | |
| 139 PickleIterator it(*pickle); | |
| 140 int read_version = 0; | |
| 141 if (!it.ReadInt(&read_version)) | |
| 142 return scoped_ptr<AppListModel>(); | |
| 143 if (read_version != kVersion) | |
| 144 return scoped_ptr<AppListModel>(); | |
| 145 int app_count = 0; | |
| 146 if (!it.ReadInt(&app_count)) | |
| 147 return scoped_ptr<AppListModel>(); | |
| 148 bool signed_in = false; | |
| 149 if (!it.ReadBool(&signed_in)) | |
| 150 return scoped_ptr<AppListModel>(); | |
| 151 | |
| 152 scoped_ptr<AppListModel> model(new AppListModel); | |
| 153 for (int i = 0; i < app_count; ++i) { | |
| 154 scoped_ptr<AppListItemModel> item(UnpickleAppListItemModel(&it).Pass()); | |
| 155 if (!item) | |
| 156 return scoped_ptr<AppListModel>(); | |
| 157 model->apps()->Add(item.release()); | |
| 158 } | |
| 159 | |
| 160 return model.Pass(); | |
| 161 } | |
| OLD | NEW |