Chromium Code Reviews| 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 CompareById { | |
| 19 public: | |
| 20 CompareById(const TransportDIB::Id& id) | |
| 21 : id_(id) { | |
| 22 } | |
| 23 | |
| 24 bool operator()(const TransportDIB* dib) const { | |
| 25 return dib->id() == id_; | |
| 26 } | |
| 27 | |
| 28 private: | |
| 29 TransportDIB::Id id_; | |
| 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 DetachFromThread(); | |
| 40 } | |
| 41 | |
| 42 CompositorSoftwareOutputDevice::~CompositorSoftwareOutputDevice() { | |
| 43 DCHECK(CalledOnValidThread()); | |
| 44 } | |
| 45 | |
| 46 TransportDIB* CompositorSoftwareOutputDevice::CreateDIB() { | |
| 47 const size_t size = 4 * viewport_size_.GetArea(); | |
| 48 TransportDIB* dib = TransportDIB::Create(size, sequence_num_++); | |
| 49 CHECK(dib); | |
| 50 bool success = dib->Map(); | |
| 51 CHECK(success); | |
| 52 return dib; | |
| 53 } | |
| 54 | |
| 55 void CompositorSoftwareOutputDevice::Resize(gfx::Size viewport_size) { | |
| 56 DCHECK(CalledOnValidThread()); | |
| 57 | |
| 58 // Reset last_buffer_ so that we don't copy over old damage. | |
| 59 last_buffer_ = -1; | |
| 60 | |
| 61 if (viewport_size_ == viewport_size) | |
| 62 return; | |
| 63 viewport_size_ = viewport_size; | |
| 64 | |
| 65 // Reallocate dibs_. | |
| 66 dibs_.clear(); | |
|
piman
2013/03/29 00:23:53
From previous review: I still don't understand why
| |
| 67 for (size_t i = 0; i < kNumBuffers; i++) | |
| 68 dibs_.push_back(CreateDIB()); | |
|
piman
2013/03/29 00:23:53
From previous review: why not let BeginPaint reall
| |
| 69 | |
| 70 front_buffer_ = 0; | |
| 71 num_free_buffers_ = kNumBuffers; | |
| 72 } | |
| 73 | |
| 74 SkCanvas* CompositorSoftwareOutputDevice::BeginPaint(gfx::Rect damage_rect) { | |
| 75 DCHECK(CalledOnValidThread()); | |
| 76 | |
| 77 if (num_free_buffers_ == 0) { | |
| 78 dibs_.insert(dibs_.begin() + front_buffer_, CreateDIB()); | |
| 79 num_free_buffers_++; | |
| 80 } | |
| 81 | |
| 82 TransportDIB* front_dib = dibs_[front_buffer_]; | |
| 83 DCHECK(front_dib); | |
| 84 DCHECK(front_dib->memory()); | |
| 85 | |
| 86 // Set up a canvas for the front_dib. | |
| 87 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, | |
| 88 viewport_size_.width(), | |
| 89 viewport_size_.height()); | |
| 90 bitmap_.setPixels(front_dib->memory()); | |
| 91 device_ = skia::AdoptRef(new SkDevice(bitmap_)); | |
| 92 canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); | |
| 93 | |
| 94 // Copy damage_rect_ from last_buffer_ to front_buffer_. | |
| 95 if (last_buffer_ != -1 && !damage_rect.Contains(damage_rect_)) { | |
| 96 DCHECK_EQ(std::abs(front_buffer_ - last_buffer_ - 1) % dibs_.size(), 0u); | |
| 97 | |
| 98 TransportDIB* last_dib = dibs_[last_buffer_]; | |
| 99 SkBitmap back_bitmap; | |
| 100 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
| 101 viewport_size_.width(), | |
| 102 viewport_size_.height()); | |
| 103 back_bitmap.setPixels(last_dib->memory()); | |
| 104 | |
| 105 SkRect last_damage = gfx::RectToSkRect(damage_rect_); | |
| 106 canvas_->drawBitmapRectToRect(back_bitmap, &last_damage, last_damage, NULL); | |
| 107 } | |
| 108 damage_rect_ = damage_rect; | |
| 109 | |
| 110 return canvas_.get(); | |
| 111 } | |
| 112 | |
| 113 void CompositorSoftwareOutputDevice::EndPaint( | |
| 114 cc::SoftwareFrameData* frame_data) { | |
| 115 DCHECK(CalledOnValidThread()); | |
| 116 DCHECK_GE(kNumBuffers, dibs_.size()); | |
| 117 | |
| 118 if (frame_data) { | |
| 119 frame_data->size = viewport_size_; | |
| 120 frame_data->damage_rect = damage_rect_; | |
| 121 frame_data->dib_id = dibs_[front_buffer_]->id(); | |
| 122 } | |
| 123 | |
| 124 last_buffer_ = front_buffer_; | |
| 125 front_buffer_ = (front_buffer_ + 1) % dibs_.size(); | |
| 126 --num_free_buffers_; | |
| 127 DCHECK_GE(num_free_buffers_, 0); | |
| 128 } | |
| 129 | |
| 130 void CompositorSoftwareOutputDevice::ReclaimDIB(const TransportDIB::Id& id) { | |
| 131 DCHECK(CalledOnValidThread()); | |
| 132 | |
| 133 // The reclaimed handle might not be among the currently | |
| 134 // active dibs if we got a resize event in the mean time. | |
| 135 ScopedVector<TransportDIB>::iterator it = | |
| 136 std::find_if(dibs_.begin(), dibs_.end(), CompareById(id)); | |
| 137 if (it != dibs_.end()) | |
| 138 ++num_free_buffers_; | |
| 139 | |
| 140 DCHECK_LE(static_cast<size_t>(num_free_buffers_), dibs_.size()); | |
| 141 } | |
| 142 | |
| 143 } // namespace content | |
| OLD | NEW |