OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "gpu/command_buffer/service/gpu_state_tracer.h" | |
6 | |
7 #include <sstream> | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/debug/trace_event.h" | |
11 #include "context_state.h" | |
12 #include "ui/gfx/codec/png_codec.h" | |
13 #include "ui/gl/gl_bindings.h" | |
14 | |
15 namespace gpu { | |
16 namespace gles2 { | |
17 namespace { | |
18 | |
19 class Snapshot : public base::debug::ConvertableToTraceFormat { | |
20 public: | |
21 static scoped_ptr<Snapshot> Create(const ContextState* state); | |
22 | |
23 bool SaveScreenshot(); | |
24 | |
25 // base::debug::ConvertableToTraceFormat implementation. | |
26 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; | |
27 | |
28 private: | |
29 explicit Snapshot(const ContextState* state); | |
30 | |
31 const ContextState* state_; | |
32 std::stringstream snapshot_; | |
33 DISALLOW_COPY_AND_ASSIGN(Snapshot); | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 Snapshot::Snapshot(const ContextState* state) : state_(state) {} | |
39 | |
40 scoped_ptr<Snapshot> Snapshot::Create(const ContextState* state) { | |
41 return scoped_ptr<Snapshot>(new Snapshot(state)); | |
42 } | |
43 | |
44 bool Snapshot::SaveScreenshot() { | |
45 const int kBytesPerPixel = 4; | |
46 gfx::Size viewport(state_->viewport_width, state_->viewport_height); | |
47 std::vector<unsigned char> pixels(viewport.width() * viewport.height() * | |
48 kBytesPerPixel); | |
49 | |
50 glPixelStorei(GL_PACK_ALIGNMENT, kBytesPerPixel); | |
51 glReadPixels(state_->viewport_x, | |
52 state_->viewport_y, | |
53 viewport.width(), | |
54 viewport.height(), | |
55 GL_RGBA, | |
56 GL_UNSIGNED_BYTE, | |
57 &pixels[0]); | |
58 glPixelStorei(GL_PACK_ALIGNMENT, state_->pack_alignment); | |
59 | |
60 // Flip the screenshot vertically. | |
61 int bytes_per_row = viewport.width() * kBytesPerPixel; | |
62 for (int y = 0; y < viewport.height() / 2; y++) { | |
63 for (int x = 0; x < bytes_per_row; x++) { | |
64 std::swap(pixels[y * bytes_per_row + x], | |
65 pixels[(viewport.height() - y - 1) * bytes_per_row + x]); | |
66 } | |
67 } | |
68 | |
69 std::vector<unsigned char> png_data; | |
70 if (!gfx::PNGCodec::Encode(&pixels[0], | |
71 gfx::PNGCodec::FORMAT_RGBA, | |
72 viewport, | |
73 bytes_per_row, | |
74 false, | |
75 std::vector<gfx::PNGCodec::Comment>(), | |
76 &png_data)) | |
77 return false; | |
dsinclair
2013/09/09 14:25:08
Is this something that happens commonly? Should we
Sami
2013/09/09 16:32:15
It shouldn't really happen unless we're really out
| |
78 | |
79 base::StringPiece base64_input(reinterpret_cast<const char*>(&png_data[0]), | |
dsinclair
2013/09/09 14:25:08
Should the conversion of the png_data happen here
Sami
2013/09/09 16:32:15
I first did it this way to avoid keeping too many
| |
80 png_data.size()); | |
81 std::string base64_output; | |
82 Base64Encode(base64_input, &base64_output); | |
83 | |
84 snapshot_ << "\"screenshot\":\"" << base64_output << "\""; | |
85 return true; | |
86 } | |
87 | |
88 void Snapshot::AppendAsTraceFormat(std::string* out) const { | |
89 *out += "{"; | |
90 *out += snapshot_.str(); | |
91 *out += "}"; | |
92 } | |
93 | |
94 scoped_ptr<GPUStateTracer> GPUStateTracer::Create(const ContextState* state) { | |
95 return scoped_ptr<GPUStateTracer>(new GPUStateTracer(state)); | |
96 } | |
97 | |
98 GPUStateTracer::GPUStateTracer(const ContextState* state) : state_(state) { | |
99 TRACE_EVENT_OBJECT_CREATED_WITH_ID( | |
100 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", this); | |
dsinclair
2013/09/09 14:25:08
Why 'this' and not 'state'?
Sami
2013/09/09 16:32:15
Done. Had to change base/debug/trace_event.h to ac
| |
101 } | |
102 | |
103 GPUStateTracer::~GPUStateTracer() { | |
104 TRACE_EVENT_OBJECT_DELETED_WITH_ID( | |
105 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", this); | |
106 } | |
107 | |
108 void GPUStateTracer::TakeSnapshot() { | |
109 bool is_tracing; | |
dsinclair
2013/09/09 14:25:08
Would it make sense to move the CATEGORY_GROUP_ENA
Sami
2013/09/09 16:32:15
Yeah, I can see calls to TakeSnapshot() getting ad
| |
110 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("gpu.debug"), | |
111 &is_tracing); | |
112 if (!is_tracing) | |
113 return; | |
114 | |
115 scoped_ptr<Snapshot> snapshot(Snapshot::Create(state_)); | |
116 | |
117 // Only save a screenshot for now. | |
118 if (!snapshot->SaveScreenshot()) | |
119 return; | |
120 | |
121 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
122 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), | |
123 "gpu::State", | |
124 this, | |
125 snapshot.PassAs<base::debug::ConvertableToTraceFormat>()); | |
126 } | |
127 | |
128 } // namespace gles2 | |
129 } // namespace gpu | |
OLD | NEW |