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

Side by Side Diff: gpu/skia_runner/skia_runner.cc

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

Powered by Google App Engine
This is Rietveld 408576698