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 "base/base64.h" |
| 8 #include "base/debug/trace_event.h" |
| 9 #include "context_state.h" |
| 10 #include "ui/gfx/codec/png_codec.h" |
| 11 #include "ui/gl/gl_bindings.h" |
| 12 |
| 13 namespace gpu { |
| 14 namespace gles2 { |
| 15 namespace { |
| 16 |
| 17 const int kBytesPerPixel = 4; |
| 18 |
| 19 class Snapshot : public base::debug::ConvertableToTraceFormat { |
| 20 public: |
| 21 static scoped_ptr<Snapshot> Create(const ContextState* state); |
| 22 |
| 23 // Save a screenshot of the currently bound framebuffer. |
| 24 bool SaveScreenshot(const gfx::Size& size); |
| 25 |
| 26 // base::debug::ConvertableToTraceFormat implementation. |
| 27 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; |
| 28 |
| 29 private: |
| 30 explicit Snapshot(const ContextState* state); |
| 31 |
| 32 const ContextState* state_; |
| 33 |
| 34 std::vector<unsigned char> screenshot_pixels_; |
| 35 gfx::Size screenshot_size_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(Snapshot); |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 Snapshot::Snapshot(const ContextState* state) : state_(state) {} |
| 43 |
| 44 scoped_ptr<Snapshot> Snapshot::Create(const ContextState* state) { |
| 45 return scoped_ptr<Snapshot>(new Snapshot(state)); |
| 46 } |
| 47 |
| 48 bool Snapshot::SaveScreenshot(const gfx::Size& size) { |
| 49 screenshot_size_ = size; |
| 50 screenshot_pixels_.resize(screenshot_size_.width() * |
| 51 screenshot_size_.height() * kBytesPerPixel); |
| 52 |
| 53 glPixelStorei(GL_PACK_ALIGNMENT, kBytesPerPixel); |
| 54 glReadPixels(0, |
| 55 0, |
| 56 screenshot_size_.width(), |
| 57 screenshot_size_.height(), |
| 58 GL_RGBA, |
| 59 GL_UNSIGNED_BYTE, |
| 60 &screenshot_pixels_[0]); |
| 61 glPixelStorei(GL_PACK_ALIGNMENT, state_->pack_alignment); |
| 62 |
| 63 // Flip the screenshot vertically. |
| 64 int bytes_per_row = screenshot_size_.width() * kBytesPerPixel; |
| 65 for (int y = 0; y < screenshot_size_.height() / 2; y++) { |
| 66 for (int x = 0; x < bytes_per_row; x++) { |
| 67 std::swap(screenshot_pixels_[y * bytes_per_row + x], |
| 68 screenshot_pixels_ |
| 69 [(screenshot_size_.height() - y - 1) * bytes_per_row + x]); |
| 70 } |
| 71 } |
| 72 return true; |
| 73 } |
| 74 |
| 75 void Snapshot::AppendAsTraceFormat(std::string* out) const { |
| 76 *out += "{"; |
| 77 if (screenshot_pixels_.size()) { |
| 78 std::vector<unsigned char> png_data; |
| 79 int bytes_per_row = screenshot_size_.width() * kBytesPerPixel; |
| 80 bool png_ok = gfx::PNGCodec::Encode(&screenshot_pixels_[0], |
| 81 gfx::PNGCodec::FORMAT_RGBA, |
| 82 screenshot_size_, |
| 83 bytes_per_row, |
| 84 false, |
| 85 std::vector<gfx::PNGCodec::Comment>(), |
| 86 &png_data); |
| 87 DCHECK(png_ok); |
| 88 |
| 89 base::StringPiece base64_input(reinterpret_cast<const char*>(&png_data[0]), |
| 90 png_data.size()); |
| 91 std::string base64_output; |
| 92 Base64Encode(base64_input, &base64_output); |
| 93 |
| 94 *out += "\"screenshot\":\"" + base64_output + "\""; |
| 95 } |
| 96 *out += "}"; |
| 97 } |
| 98 |
| 99 scoped_ptr<GPUStateTracer> GPUStateTracer::Create(const ContextState* state) { |
| 100 return scoped_ptr<GPUStateTracer>(new GPUStateTracer(state)); |
| 101 } |
| 102 |
| 103 GPUStateTracer::GPUStateTracer(const ContextState* state) : state_(state) { |
| 104 TRACE_EVENT_OBJECT_CREATED_WITH_ID( |
| 105 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", state_); |
| 106 } |
| 107 |
| 108 GPUStateTracer::~GPUStateTracer() { |
| 109 TRACE_EVENT_OBJECT_DELETED_WITH_ID( |
| 110 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), "gpu::State", state_); |
| 111 } |
| 112 |
| 113 void GPUStateTracer::TakeSnapshotWithCurrentFramebuffer(const gfx::Size& size) { |
| 114 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.debug"), |
| 115 "GPUStateTracer::TakeSnapshotWithCurrentFramebuffer"); |
| 116 |
| 117 scoped_ptr<Snapshot> snapshot(Snapshot::Create(state_)); |
| 118 |
| 119 // Only save a screenshot for now. |
| 120 if (!snapshot->SaveScreenshot(size)) |
| 121 return; |
| 122 |
| 123 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| 124 TRACE_DISABLED_BY_DEFAULT("gpu.debug"), |
| 125 "gpu::State", |
| 126 state_, |
| 127 snapshot.PassAs<base::debug::ConvertableToTraceFormat>()); |
| 128 } |
| 129 |
| 130 } // namespace gles2 |
| 131 } // namespace gpu |
OLD | NEW |