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 // Save a screenshot of the currently bound framebuffer. |
| 26 bool SaveScreenshot(const gfx::Size& size); |
| 27 |
| 28 // base::debug::ConvertableToTraceFormat implementation. |
| 29 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; |
| 30 |
| 31 private: |
| 32 explicit Snapshot(const ContextState* state); |
| 33 |
| 34 const ContextState* state_; |
| 35 |
| 36 std::vector<unsigned char> screenshot_pixels_; |
| 37 gfx::Size screenshot_size_; |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(Snapshot); |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 Snapshot::Snapshot(const ContextState* state) : state_(state) {} |
| 45 |
| 46 scoped_ptr<Snapshot> Snapshot::Create(const ContextState* state) { |
| 47 return scoped_ptr<Snapshot>(new Snapshot(state)); |
| 48 } |
| 49 |
| 50 bool Snapshot::SaveScreenshot(const gfx::Size& size) { |
| 51 screenshot_size_ = size; |
| 52 screenshot_pixels_.resize(screenshot_size_.width() * |
| 53 screenshot_size_.height() * kBytesPerPixel); |
| 54 |
| 55 glPixelStorei(GL_PACK_ALIGNMENT, kBytesPerPixel); |
| 56 glReadPixels(0, |
| 57 0, |
| 58 screenshot_size_.width(), |
| 59 screenshot_size_.height(), |
| 60 GL_RGBA, |
| 61 GL_UNSIGNED_BYTE, |
| 62 &screenshot_pixels_[0]); |
| 63 glPixelStorei(GL_PACK_ALIGNMENT, state_->pack_alignment); |
| 64 |
| 65 // Flip the screenshot vertically. |
| 66 int bytes_per_row = screenshot_size_.width() * kBytesPerPixel; |
| 67 for (int y = 0; y < screenshot_size_.height() / 2; y++) { |
| 68 for (int x = 0; x < bytes_per_row; x++) { |
| 69 std::swap(screenshot_pixels_[y * bytes_per_row + x], |
| 70 screenshot_pixels_ |
| 71 [(screenshot_size_.height() - y - 1) * bytes_per_row + x]); |
| 72 } |
| 73 } |
| 74 return true; |
| 75 } |
| 76 |
| 77 void Snapshot::AppendAsTraceFormat(std::string* out) const { |
| 78 *out += "{"; |
| 79 if (screenshot_pixels_.size()) { |
| 80 std::vector<unsigned char> png_data; |
| 81 int bytes_per_row = screenshot_size_.width() * kBytesPerPixel; |
| 82 bool png_ok = gfx::PNGCodec::Encode(&screenshot_pixels_[0], |
| 83 gfx::PNGCodec::FORMAT_RGBA, |
| 84 screenshot_size_, |
| 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::TakeSnapshotWithCurrentFramebuffer(const gfx::Size& size) { |
| 116 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.debug"), |
| 117 "GPUStateTracer::TakeSnapshotWithCurrentFramebuffer"); |
| 118 |
| 119 scoped_ptr<Snapshot> snapshot(Snapshot::Create(state_)); |
| 120 |
| 121 // Only save a screenshot for now. |
| 122 if (!snapshot->SaveScreenshot(size)) |
| 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 |