electron/atom/browser/osr_output_device.cc

101 lines
2.5 KiB
C++
Raw Normal View History

2016-07-20 09:30:06 +00:00
// Copyright (c) 2013 GitHub, Inc.
// 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"
#include <iostream>
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,
const OnPaintCallback& callback)
: transparent_(transparent),
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-27 17:59:01 +00:00
OffScreenOutputDevice::~OffScreenOutputDevice() { }
2016-07-27 17:19:53 +00:00
2016-07-20 09:30:06 +00:00
void OffScreenOutputDevice::Resize(
const gfx::Size& pixel_size, float scale_factor) {
std::cout << pixel_size.width() << "x" << pixel_size.height() << std::endl;
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) {
2016-07-27 17:19:53 +00:00
// std::cout << "BeginPaint" << std::endl;
2016-07-20 09:30:06 +00:00
DCHECK(canvas_.get());
DCHECK(bitmap_.get());
damage_rect_ = damage_rect;
return canvas_.get();
}
void OffScreenOutputDevice::EndPaint() {
2016-07-27 17:19:53 +00:00
// std::cout << "EndPaint" << std::endl;
2016-07-20 09:30:06 +00:00
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
// Call OnPaint immediately if deactivated while a damage rect is pending.
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