Index: content/renderer/gpu/compositor_software_output_device.cc |
diff --git a/content/renderer/gpu/compositor_software_output_device.cc b/content/renderer/gpu/compositor_software_output_device.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d0147e3f2d79c1eb54afe90aabbf0346a0ab0974 |
--- /dev/null |
+++ b/content/renderer/gpu/compositor_software_output_device.cc |
@@ -0,0 +1,127 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/renderer/gpu/compositor_software_output_device.h" |
+ |
+#include "base/logging.h" |
+#include "cc/output/software_frame_data.h" |
+#include "third_party/skia/include/core/SkCanvas.h" |
+#include "third_party/skia/include/core/SkDevice.h" |
+#include "third_party/skia/include/core/SkPixelRef.h" |
+#include "ui/gfx/skia_util.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+class CompareByHandle { |
+ public: |
+ CompareByHandle(TransportDIB::Handle handle) |
+ : handle_(handle) { |
+ } |
+ |
+ bool operator()(const TransportDIB* dib) const { |
+ return dib->handle() == handle_; |
+ } |
+ |
+ private: |
+ TransportDIB::Handle handle_; |
+}; |
+ |
+} // namespace |
+ |
+CompositorSoftwareOutputDevice::CompositorSoftwareOutputDevice() |
+ : front_buffer_(0), |
+ last_buffer_(-1), |
+ num_free_buffers_(0), |
+ sequence_num_(0) { |
+} |
+ |
+CompositorSoftwareOutputDevice::~CompositorSoftwareOutputDevice() { |
+} |
+ |
+void CompositorSoftwareOutputDevice::Resize(gfx::Size viewport_size) { |
+ // Reset last_buffer_ so that we don't copy over old damage. |
+ last_buffer_ = -1; |
+ |
+ if (viewport_size_ == viewport_size) |
+ return; |
+ viewport_size_ = viewport_size; |
+ |
+ // Reallocate dibs_ if necessary. |
+ if (dibs_.empty() || dibs_[0]->size() < ViewportSizeInBytes()) { |
+ dibs_.clear(); |
+ for (int i = 0; i < kNumBuffers; i++) |
+ dibs_.push_back(CreateDIB()); |
+ num_free_buffers_ = kNumBuffers; |
+ } |
+} |
+ |
+SkCanvas* CompositorSoftwareOutputDevice::BeginPaint(gfx::Rect damage_rect) { |
+ if (num_free_buffers_ == 0) { |
+ dibs_.insert(dibs_.begin() + front_buffer_, CreateDIB()); |
+ num_free_buffers_++; |
+ } |
+ |
+ TransportDIB* front_dib = dibs_[front_buffer_]; |
+ DCHECK(front_dib); |
+ |
+ // Set up a canvas for the front_dib. |
+ bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
+ viewport_size_.width(), |
+ viewport_size_.height()); |
+ bitmap_.setPixels(front_dib->memory()); |
+ device_ = skia::AdoptRef(new SkDevice(bitmap_)); |
+ canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); |
+ |
+ // Copy damage_rect_ from last_buffer_ to front_buffer_. |
+ if (last_buffer_ != -1 && !damage_rect.Contains(damage_rect_)) { |
+ DCHECK_EQ(std::abs(front_buffer_ - last_buffer_ - 1) % dibs_.size(), 0u); |
+ |
+ TransportDIB* last_dib = dibs_[last_buffer_]; |
+ SkBitmap back_bitmap; |
+ back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
+ viewport_size_.width(), |
+ viewport_size_.height()); |
+ back_bitmap.setPixels(last_dib->memory()); |
+ |
+ SkRect last_damage = gfx::RectToSkRect(damage_rect_); |
+ canvas_->drawBitmapRectToRect(back_bitmap, &last_damage, last_damage, NULL); |
+ } |
+ damage_rect_ = damage_rect; |
+ |
+ return canvas_.get(); |
+} |
+ |
+void CompositorSoftwareOutputDevice::EndPaint( |
+ cc::SoftwareFrameData* frame_data) { |
+ DCHECK_GE(int(dibs_.size()), kNumBuffers); |
+ DCHECK_LE(0, front_buffer_); |
+ DCHECK_LT(front_buffer_, int(dibs_.size())); |
+ |
+ if (frame_data) { |
+ frame_data->size = viewport_size_; |
+ frame_data->damage_rect = damage_rect_; |
+ frame_data->content_dib = dibs_[front_buffer_]->handle(); |
+ } |
+ |
+ last_buffer_ = front_buffer_; |
+ front_buffer_ = (front_buffer_ + 1) % dibs_.size(); |
+ --num_free_buffers_; |
+ DCHECK_GE(num_free_buffers_, 0); |
+} |
+ |
+void CompositorSoftwareOutputDevice::ReclaimDIB( |
+ TransportDIB::Handle handle) { |
+ // The reclaimed handle might not be among the currently |
+ // active dibs if we got a resize event in the mean time. |
+ ScopedVector<TransportDIB>::iterator it = |
+ std::find_if(dibs_.begin(), dibs_.end(), CompareByHandle(handle)); |
+ if (it != dibs_.end()) |
+ ++num_free_buffers_; |
+ |
+ DCHECK_LE(num_free_buffers_, int(dibs_.size())); |
+} |
+ |
+} // namespace content |