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 const int kBytesPerPixel = 4; | |
20 | |
21 class Snapshot : public base::debug::ConvertableToTraceFormat { | |
22 public: | |
23 static scoped_ptr<Snapshot> Create(const ContextState* state); | |
24 | |
25 bool SaveScreenshot(); | |
26 | |
27 // base::debug::ConvertableToTraceFormat implementation. | |
28 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; | |
29 | |
30 private: | |
31 explicit Snapshot(const ContextState* state); | |
32 | |
33 const ContextState* state_; | |
34 | |
35 mutable std::vector<unsigned char> screenshot_pixels_; | |
36 gfx::Size screenshot_viewport_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(Snapshot); | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 Snapshot::Snapshot(const ContextState* state) : state_(state) {} | |
44 | |
45 scoped_ptr<Snapshot> Snapshot::Create(const ContextState* state) { | |
46 return scoped_ptr<Snapshot>(new Snapshot(state)); | |
47 } | |
48 | |
49 bool Snapshot::SaveScreenshot() { | |
50 screenshot_viewport_ = | |
51 gfx::Size(state_->viewport_width, state_->viewport_height); | |
piman
2013/09/09 21:25:54
You're dependent on the current GL state, which ma
| |
52 screenshot_pixels_.resize(screenshot_viewport_.width() * | |
53 screenshot_viewport_.height() * kBytesPerPixel); | |
54 | |
55 glPixelStorei(GL_PACK_ALIGNMENT, kBytesPerPixel); | |
56 glReadPixels(state_->viewport_x, | |
57 state_->viewport_y, | |
58 screenshot_viewport_.width(), | |
59 screenshot_viewport_.height(), | |
60 GL_RGBA, | |
61 GL_UNSIGNED_BYTE, | |
62 &screenshot_pixels_[0]); | |
63 glPixelStorei(GL_PACK_ALIGNMENT, state_->pack_alignment); | |
64 return true; | |
65 } | |
66 | |
67 void Snapshot::AppendAsTraceFormat(std::string* out) const { | |
68 *out += "{"; | |
69 if (screenshot_pixels_.size()) { | |
70 // Flip the screenshot vertically. | |
71 int bytes_per_row = screenshot_viewport_.width() * kBytesPerPixel; | |
72 for (int y = 0; y < screenshot_viewport_.height() / 2; y++) { | |
73 for (int x = 0; x < bytes_per_row; x++) { | |
74 std::swap( | |
75 screenshot_pixels_[y * bytes_per_row + x], | |
76 screenshot_pixels_[ | |
77 (screenshot_viewport_.height() - y - 1) * bytes_per_row + x]); | |
piman
2013/09/09 21:25:54
Is this why you need screenshot_pixels_ to be muta
| |
78 } | |
79 } | |
80 | |
81 std::vector<unsigned char> png_data; | |
82 bool png_ok = gfx::PNGCodec::Encode(&screenshot_pixels_[0], | |
83 gfx::PNGCodec::FORMAT_RGBA, | |
84 screenshot_viewport_, | |
85 bytes_per_row, | |
86 false, | |
87 std::vector<gfx::PNGCodec::Comment>(), | |
88 &png_data); | |
89 DCHECK(png_ok); | |
90 | |
91 base::StringPiece base64_input(reinterpret_cast<const char*>(&png_data[0]), | |
92 png_data.size()); | |
93 std::string base64_output; | |
94 Base64Encode(base64_input, &base64_output); | |
95 | |
96 *out += "\"screenshot\":\"" + base64_output + "\""; | |
97 } | |
98 *out += "}"; | |
99 } | |
100 | |
101 scoped_ptr<GPUStateTracer> GPUStateTracer::Create(const ContextState* state) { | |
102 return scoped_ptr<GPUStateTracer>(new GPUStateTracer(state)); | |
103 } | |
104 | |
105 GPUStateTracer::GPUStateTracer(const ContextState* state) : state_(state) { | |
106 TRACE_EVENT_OBJECT_CREATED_WITH_ID( | |
107 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", state_); | |
108 } | |
109 | |
110 GPUStateTracer::~GPUStateTracer() { | |
111 TRACE_EVENT_OBJECT_DELETED_WITH_ID( | |
112 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", state_); | |
113 } | |
114 | |
115 void GPUStateTracer::TakeSnapshot() { | |
116 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.debug"), | |
117 "GPUStateTracer::TakeSnapshot"); | |
118 | |
119 scoped_ptr<Snapshot> snapshot(Snapshot::Create(state_)); | |
120 | |
121 // Only save a screenshot for now. | |
122 if (!snapshot->SaveScreenshot()) | |
123 return; | |
124 | |
125 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
126 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), | |
127 "gpu::State", | |
128 state_, | |
129 snapshot.PassAs<base::debug::ConvertableToTraceFormat>()); | |
130 } | |
131 | |
132 } // namespace gles2 | |
133 } // namespace gpu | |
OLD | NEW |