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

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

Powered by Google App Engine
This is Rietveld 408576698