electron/atom/browser/osr_output_device.cc

95 lines
2.2 KiB
C++
Raw Normal View History

2016-07-30 19:35:14 +00:00
// Copyright (c) 2016 GitHub, Inc.
2016-07-20 09:30:06 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr_output_device.h"
2016-07-27 17:44:41 +00:00
#include "third_party/skia/include/core/SkDevice.h"
#include "ui/gfx/skia_util.h"
2016-07-20 09:30:06 +00:00
namespace atom {
2016-07-27 17:59:01 +00:00
OffScreenOutputDevice::OffScreenOutputDevice(bool transparent,
2016-07-30 19:35:14 +00:00
const OnPaintCallback& callback):
transparent_(transparent),
2016-07-27 17:59:01 +00:00
callback_(callback),
2016-07-27 17:44:41 +00:00
active_(false) {
DCHECK(!callback_.is_null());
2016-07-20 09:30:06 +00:00
}
2016-07-31 15:10:53 +00:00
OffScreenOutputDevice::~OffScreenOutputDevice() {
}
2016-07-27 17:19:53 +00:00
2016-07-20 09:30:06 +00:00
void OffScreenOutputDevice::Resize(
2016-07-30 19:35:14 +00:00
const gfx::Size& pixel_size, float scale_factor) {
2016-07-20 09:30:06 +00:00
scale_factor_ = scale_factor;
if (viewport_pixel_size_ == pixel_size) return;
viewport_pixel_size_ = pixel_size;
canvas_.reset(NULL);
bitmap_.reset(new SkBitmap);
bitmap_->allocN32Pixels(viewport_pixel_size_.width(),
viewport_pixel_size_.height(),
2016-07-27 17:59:01 +00:00
!transparent_);
2016-07-20 09:30:06 +00:00
if (bitmap_->drawsNothing()) {
NOTREACHED();
bitmap_.reset(NULL);
return;
}
2016-07-27 17:59:01 +00:00
if (transparent_)
bitmap_->eraseARGB(0, 0, 0, 0);
2016-07-20 09:30:06 +00:00
canvas_.reset(new SkCanvas(*bitmap_.get()));
}
SkCanvas* OffScreenOutputDevice::BeginPaint(const gfx::Rect& damage_rect) {
DCHECK(canvas_.get());
DCHECK(bitmap_.get());
damage_rect_ = damage_rect;
return canvas_.get();
}
void OffScreenOutputDevice::EndPaint() {
DCHECK(canvas_.get());
DCHECK(bitmap_.get());
if (!bitmap_.get()) return;
cc::SoftwareOutputDevice::EndPaint();
2016-07-27 17:44:41 +00:00
if (active_)
OnPaint(damage_rect_);
}
2016-07-27 17:19:53 +00:00
2016-07-27 17:44:41 +00:00
void OffScreenOutputDevice::SetActive(bool active) {
if (active == active_)
return;
active_ = active;
2016-07-27 17:19:53 +00:00
2016-07-27 17:44:41 +00:00
if (!active_ && !pending_damage_rect_.IsEmpty())
OnPaint(pending_damage_rect_);
2016-07-27 17:19:53 +00:00
}
2016-07-20 09:30:06 +00:00
2016-07-27 17:19:53 +00:00
void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) {
gfx::Rect rect = damage_rect;
if (!pending_damage_rect_.IsEmpty()) {
rect.Union(pending_damage_rect_);
pending_damage_rect_.SetRect(0, 0, 0, 0);
2016-07-20 09:30:06 +00:00
}
2016-07-27 17:19:53 +00:00
rect.Intersect(gfx::Rect(viewport_pixel_size_));
if (rect.IsEmpty())
return;
SkAutoLockPixels bitmap_pixels_lock(*bitmap_.get());
2016-07-27 17:44:41 +00:00
callback_.Run(rect, bitmap_->width(), bitmap_->height(),
bitmap_->getPixels());
2016-07-20 09:30:06 +00:00
}
} // namespace atom