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 "content/renderer/gpu/compositor_software_output_device.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "cc/output/software_frame_data.h" |
| 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkDevice.h" |
| 11 #include "third_party/skia/include/core/SkPixelRef.h" |
| 12 #include "ui/gfx/skia_util.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class CompareByHandle { |
| 19 public: |
| 20 CompareByHandle(TransportDIB::Handle handle) |
| 21 : handle_(handle) { |
| 22 } |
| 23 |
| 24 bool operator()(const TransportDIB* dib) const { |
| 25 return dib->handle() == handle_; |
| 26 } |
| 27 |
| 28 private: |
| 29 TransportDIB::Handle handle_; |
| 30 }; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 CompositorSoftwareOutputDevice::CompositorSoftwareOutputDevice() |
| 35 : front_buffer_(0), |
| 36 last_buffer_(-1), |
| 37 num_free_buffers_(0), |
| 38 sequence_num_(0) { |
| 39 } |
| 40 |
| 41 CompositorSoftwareOutputDevice::~CompositorSoftwareOutputDevice() { |
| 42 } |
| 43 |
| 44 void CompositorSoftwareOutputDevice::Resize(gfx::Size viewport_size) { |
| 45 // Reset last_buffer_ so that we don't copy over old damage. |
| 46 last_buffer_ = -1; |
| 47 |
| 48 if (viewport_size_ == viewport_size) |
| 49 return; |
| 50 viewport_size_ = viewport_size; |
| 51 |
| 52 // Reallocate dibs_ if necessary. |
| 53 if (dibs_.empty() || dibs_[0]->size() < ViewportSizeInBytes()) { |
| 54 dibs_.clear(); |
| 55 for (int i = 0; i < kNumBuffers; i++) |
| 56 dibs_.push_back(CreateDIB()); |
| 57 num_free_buffers_ = kNumBuffers; |
| 58 } |
| 59 } |
| 60 |
| 61 SkCanvas* CompositorSoftwareOutputDevice::BeginPaint(gfx::Rect damage_rect) { |
| 62 if (num_free_buffers_ == 0) { |
| 63 dibs_.insert(dibs_.begin() + front_buffer_, CreateDIB()); |
| 64 num_free_buffers_++; |
| 65 } |
| 66 |
| 67 TransportDIB* front_dib = dibs_[front_buffer_]; |
| 68 DCHECK(front_dib); |
| 69 |
| 70 // Set up a canvas for the front_dib. |
| 71 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 72 viewport_size_.width(), |
| 73 viewport_size_.height()); |
| 74 bitmap_.setPixels(front_dib->memory()); |
| 75 device_ = skia::AdoptRef(new SkDevice(bitmap_)); |
| 76 canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); |
| 77 |
| 78 // Copy damage_rect_ from last_buffer_ to front_buffer_. |
| 79 if (last_buffer_ != -1 && !damage_rect.Contains(damage_rect_)) { |
| 80 DCHECK_EQ(std::abs(front_buffer_ - last_buffer_ - 1) % dibs_.size(), 0u); |
| 81 |
| 82 TransportDIB* last_dib = dibs_[last_buffer_]; |
| 83 SkBitmap back_bitmap; |
| 84 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 85 viewport_size_.width(), |
| 86 viewport_size_.height()); |
| 87 back_bitmap.setPixels(last_dib->memory()); |
| 88 |
| 89 SkRect last_damage = gfx::RectToSkRect(damage_rect_); |
| 90 canvas_->drawBitmapRectToRect(back_bitmap, &last_damage, last_damage, NULL); |
| 91 } |
| 92 damage_rect_ = damage_rect; |
| 93 |
| 94 return canvas_.get(); |
| 95 } |
| 96 |
| 97 void CompositorSoftwareOutputDevice::EndPaint( |
| 98 cc::SoftwareFrameData* frame_data) { |
| 99 DCHECK_GE(int(dibs_.size()), kNumBuffers); |
| 100 DCHECK_LE(0, front_buffer_); |
| 101 DCHECK_LT(front_buffer_, int(dibs_.size())); |
| 102 |
| 103 if (frame_data) { |
| 104 frame_data->size = viewport_size_; |
| 105 frame_data->damage_rect = damage_rect_; |
| 106 frame_data->content_dib = dibs_[front_buffer_]->handle(); |
| 107 } |
| 108 |
| 109 last_buffer_ = front_buffer_; |
| 110 front_buffer_ = (front_buffer_ + 1) % dibs_.size(); |
| 111 --num_free_buffers_; |
| 112 DCHECK_GE(num_free_buffers_, 0); |
| 113 } |
| 114 |
| 115 void CompositorSoftwareOutputDevice::ReclaimDIB( |
| 116 TransportDIB::Handle handle) { |
| 117 // The reclaimed handle might not be among the currently |
| 118 // active dibs if we got a resize event in the mean time. |
| 119 ScopedVector<TransportDIB>::iterator it = |
| 120 std::find_if(dibs_.begin(), dibs_.end(), CompareByHandle(handle)); |
| 121 if (it != dibs_.end()) |
| 122 ++num_free_buffers_; |
| 123 |
| 124 DCHECK_LE(num_free_buffers_, int(dibs_.size())); |
| 125 } |
| 126 |
| 127 } // namespace content |
OLD | NEW |