OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015 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 <iostream> | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_enumerator.h" | |
10 #include "base/files/file_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "gpu/skia_runner/in_process_graphics_system.h" | |
13 #include "gpu/skia_runner/sk_picture_rasterizer.h" | |
14 #include "third_party/WebKit/public/platform/WebData.h" | |
15 #include "third_party/WebKit/public/platform/WebImage.h" | |
16 #include "third_party/WebKit/public/platform/WebSize.h" | |
17 #include "third_party/skia/include/core/SkOSFile.h" | |
18 #include "third_party/skia/include/core/SkPicture.h" | |
19 #include "third_party/skia/include/core/SkStream.h" | |
20 #include "ui/gfx/codec/png_codec.h" | |
21 | |
22 namespace { | |
23 | |
24 bool WriteSkImagePNG(const SkImage* image, const base::FilePath& path) { | |
25 DCHECK(!path.empty()); | |
26 | |
27 if (!image) { | |
28 std::cout << "Unable to write empty bitmap for " << path.value() << ".\n"; | |
29 return false; | |
30 } | |
31 | |
32 std::string file_path = path.MaybeAsASCII(); | |
33 SkFILEWStream stream(file_path.c_str()); | |
34 if (!stream.isValid()) { | |
35 std::cout << "Unable to write to " << file_path.c_str() << ".\n"; | |
36 return false; | |
37 } | |
38 | |
39 SkImageInfo info = SkImageInfo::Make(image->width(), image->height(), | |
40 SkColorType::kBGRA_8888_SkColorType, | |
41 SkAlphaType::kPremul_SkAlphaType); | |
42 SkImageInfo::MakeN32Premul(image->width(), image->height()); | |
43 | |
44 const size_t rowBytes = image->width() * sizeof(SkPMColor); | |
45 std::vector<SkPMColor> pixels(image->width() * image->height()); | |
46 | |
47 if (image->readPixels(info, pixels.data(), rowBytes, 0, 0)) { | |
48 std::vector<unsigned char> png_data; | |
49 | |
50 if (gfx::PNGCodec::Encode( | |
51 reinterpret_cast<const unsigned char*>(pixels.data()), | |
52 gfx::PNGCodec::FORMAT_BGRA, | |
53 gfx::Size(image->width(), image->height()), | |
54 static_cast<int>(rowBytes), false, | |
55 std::vector<gfx::PNGCodec::Comment>(), &png_data)) { | |
56 if (stream.write(png_data.data(), png_data.size())) { | |
57 return true; | |
58 } | |
59 } | |
60 } | |
61 | |
62 return false; | |
63 } | |
64 | |
65 bool onDecode(const void* buffer, size_t size, SkBitmap* bm) { | |
66 blink::WebData web_data(static_cast<const char*>(buffer), size); | |
67 blink::WebImage image = blink::WebImage::fromData(web_data, blink::WebSize()); | |
68 if (!image.isNull()) { | |
69 *bm = image.getSkBitmap(); | |
70 return true; | |
71 } | |
72 std::cout << "Error decoding image.\n"; | |
73 return false; | |
74 } | |
75 | |
76 skia::RefPtr<SkPicture> ReadPicture(const base::FilePath& path) { | |
77 skia::RefPtr<SkPicture> picture; | |
78 std::string file_path = path.MaybeAsASCII(); | |
79 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(file_path.c_str())); | |
80 if (!stream.get()) { | |
81 std::cout << "Unable to read " << file_path.c_str() << ".\n"; | |
82 return picture; | |
83 } | |
84 | |
85 picture = skia::AdoptRef(SkPicture::CreateFromStream(stream.get(), onDecode)); | |
86 if (!picture) { | |
87 std::cout << "Unable to load " << file_path.c_str() | |
88 << " as an SkPicture.\n"; | |
89 } | |
90 return picture; | |
91 } | |
92 | |
93 base::FilePath MakeDestinationFilename( | |
94 base::FilePath source_file, | |
95 base::FilePath destination_folder, | |
96 base::FilePath::StringType new_extension) { | |
97 base::FilePath filename = source_file.BaseName().RemoveExtension(); | |
98 filename = filename.AddExtension(new_extension); | |
99 return destination_folder.AsEndingWithSeparator().Append(filename); | |
100 } | |
101 | |
102 std::vector<std::pair<base::FilePath, base::FilePath>> GetSkpsToRasterize( | |
103 base::FilePath input_path, | |
104 base::FilePath output_path) { | |
105 std::vector<std::pair<base::FilePath, base::FilePath>> files; | |
106 | |
107 if (base::DirectoryExists(input_path)) { | |
108 if (!base::DirectoryExists(output_path)) { | |
109 return files; | |
110 } | |
111 base::FilePath::StringType extension = FILE_PATH_LITERAL(".skp"); | |
112 base::FileEnumerator file_iter(input_path, false, | |
113 base::FileEnumerator::FILES); | |
114 while (!file_iter.Next().empty()) { | |
115 if (file_iter.GetInfo().GetName().MatchesExtension(extension)) { | |
116 base::FilePath skp_file = file_iter.GetInfo().GetName(); | |
117 skp_file = input_path.AsEndingWithSeparator().Append(skp_file); | |
118 base::FilePath png_file = MakeDestinationFilename( | |
119 skp_file, output_path, FILE_PATH_LITERAL("png")); | |
120 files.push_back(std::make_pair(skp_file, png_file)); | |
121 } | |
122 } | |
123 } else { | |
124 // Single file passed. If the output file is a folder, make a name. | |
125 if (base::DirectoryExists(output_path)) { | |
126 output_path = MakeDestinationFilename(input_path, output_path, | |
127 FILE_PATH_LITERAL("png")); | |
128 } | |
129 files.push_back(std::make_pair(input_path, output_path)); | |
130 } | |
131 return files; | |
132 } | |
133 | |
134 static const char kHelpMessage[] = | |
135 "This program renders a skia SKP to a PNG using GPU rasterization via the\n" | |
136 "command buffer.\n\n" | |
137 "following command line flags to control its behavior:\n" | |
138 "\n" | |
139 " --in-skp=skp[:DIRECTORY_PATH|FILE_PATH]\n" | |
140 " Input SKP file. If a directory is provided, all SKP files will be\n" | |
141 " converted.\n" | |
142 " --out-png=png[:DIRECTORY_PATH|:FILE_PATH]\n" | |
143 " Output PNG file. If a directory is provided, the SKP filename is " | |
144 "used.\n\n" | |
145 " --use-lcd-text\n" | |
146 " Turn on lcd text rendering.\n" | |
147 " --use-distance-field-text\n" | |
148 " Turn on distance field text rendering.\n" | |
149 " --msaa-sample-count=(0|2|4|8|16)\n" | |
150 " Turn on multi-sample anti-aliasing.\n" | |
151 " --use-gl=(desktop|osmesa|egl|swiftshader)\n" | |
152 " Specify Gl driver. --swiftshader-path required for swiftshader.\n"; | |
153 | |
154 } // namespace anonymous | |
155 | |
156 int main(int argc, char** argv) { | |
157 base::AtExitManager exit_manager; | |
158 base::CommandLine::Init(argc, argv); | |
159 #if defined(OS_MACOSX) | |
160 base::mac::ScopedNSAutoreleasePool autorelease_pool; | |
161 #endif | |
162 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
163 base::FilePath input_path(command_line->GetSwitchValuePath("in-skp")); | |
164 base::FilePath output_path(command_line->GetSwitchValuePath("out-png")); | |
165 | |
166 if (input_path.empty() || output_path.empty()) { | |
167 std::cout << kHelpMessage; | |
168 return 0; | |
169 } | |
170 | |
171 std::vector<std::pair<base::FilePath, base::FilePath>> files = | |
172 GetSkpsToRasterize(input_path, output_path); | |
173 | |
174 if (files.empty()) { | |
175 if (!base::DirectoryExists(output_path)) | |
176 std::cout << "You must specify an existing directory using '--out-png'\n"; | |
177 else | |
178 std::cout << "No skp files found at " << input_path.value() << "\n"; | |
179 return 0; | |
180 } | |
181 | |
182 skia_runner::InProcessGraphicsSystem graphics_system; | |
183 if (!graphics_system.IsSuccessfullyInitialized()) { | |
184 LOG(ERROR) << "Unable to initialize rasterizer."; | |
185 return 0; | |
186 } | |
187 | |
188 skia_runner::SkPictureRasterizer picture_rasterizer( | |
189 graphics_system.GetGrContext(), graphics_system.GetMaxTextureSize()); | |
190 | |
191 // Set up command-line render options. | |
192 picture_rasterizer.set_use_lcd_text(command_line->HasSwitch("use-lcd-text")); | |
193 picture_rasterizer.set_use_distance_field_text( | |
194 command_line->HasSwitch("use-distance-field-text")); | |
195 | |
196 if (command_line->HasSwitch("msaa-sample-count")) { | |
197 std::string value = command_line->GetSwitchValueASCII("msaa-sample-count"); | |
198 int msaa = 0; | |
199 if (base::StringToInt(value, &msaa)) | |
200 picture_rasterizer.set_msaa_sample_count(msaa); | |
201 | |
202 if (msaa != 0 && msaa != 2 && msaa != 4 && msaa != 8 && msaa != 16) { | |
203 std::cout << "Error: msaa sample count must be 0, 2, 4, 8 or 16.\n"; | |
204 return 0; | |
205 } | |
206 } | |
207 | |
208 for (auto file_pair : files) { | |
209 skia::RefPtr<SkPicture> picture = ReadPicture(file_pair.first); | |
210 if (!picture) { | |
211 std::cout << "Error reading: " << file_pair.first.value() << "\n"; | |
212 continue; | |
213 } | |
214 | |
215 skia::RefPtr<SkImage> image(picture_rasterizer.Rasterize(picture.get())); | |
216 if (!WriteSkImagePNG(image.get(), file_pair.second)) | |
217 std::cout << "Error writing: " << file_pair.second.value() << "\n"; | |
218 else | |
219 std::cout << file_pair.second.value() << " successfully created.\n"; | |
220 } | |
221 } | |
OLD | NEW |