electron/atom/browser/osr/osr_output_device.cc

102 lines
2.5 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/osr_output_device.h"
2016-07-20 09:30:06 +00:00
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/src/core/SkDevice.h"
2016-07-27 17:44:41 +00:00
#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-08-03 04:04:36 +00:00
const OnPaintCallback& callback)
: transparent_(transparent), callback_(callback) {
2016-07-27 17:44:41 +00:00
DCHECK(!callback_.is_null());
2016-07-20 09:30:06 +00:00
}
2018-04-18 01:55:30 +00:00
OffScreenOutputDevice::~OffScreenOutputDevice() {}
2016-07-27 17:19:53 +00:00
2018-04-18 01:55:30 +00:00
void OffScreenOutputDevice::Resize(const gfx::Size& pixel_size,
float scale_factor) {
if (viewport_pixel_size_ == pixel_size)
return;
2016-07-20 09:30:06 +00:00
viewport_pixel_size_ = pixel_size;
2016-08-03 04:04:36 +00:00
canvas_.reset();
2016-07-20 09:30:06 +00:00
bitmap_.reset(new SkBitmap);
bitmap_->allocN32Pixels(viewport_pixel_size_.width(),
2018-04-18 01:55:30 +00:00
viewport_pixel_size_.height(), !transparent_);
2016-07-20 09:30:06 +00:00
if (bitmap_->drawsNothing()) {
NOTREACHED();
2016-08-03 04:04:36 +00:00
bitmap_.reset();
2016-07-20 09:30:06 +00:00
return;
}
2016-07-27 17:59:01 +00:00
if (transparent_) {
bitmap_->eraseColor(SK_ColorTRANSPARENT);
} else {
bitmap_->eraseColor(SK_ColorWHITE);
}
2016-07-20 09:30:06 +00:00
2016-08-03 04:04:36 +00:00
canvas_.reset(new SkCanvas(*bitmap_));
2016-07-20 09:30:06 +00:00
}
SkCanvas* OffScreenOutputDevice::BeginPaint(const gfx::Rect& damage_rect) {
DCHECK(canvas_.get());
DCHECK(bitmap_.get());
damage_rect_ = damage_rect;
2018-04-18 01:55:30 +00:00
SkIRect damage =
SkIRect::MakeXYWH(damage_rect_.x(), damage_rect_.y(),
damage_rect_.width(), damage_rect_.height());
2017-05-10 21:04:19 +00:00
if (transparent_) {
bitmap_->erase(SK_ColorTRANSPARENT, damage);
} else {
bitmap_->erase(SK_ColorWHITE, damage);
}
2016-07-20 09:30:06 +00:00
return canvas_.get();
}
void OffScreenOutputDevice::EndPaint() {
DCHECK(canvas_.get());
DCHECK(bitmap_.get());
2018-04-18 01:55:30 +00:00
if (!bitmap_.get())
return;
2016-07-20 09:30:06 +00:00
viz::SoftwareOutputDevice::EndPaint();
2016-07-20 09:30:06 +00:00
2016-07-27 17:44:41 +00:00
if (active_)
OnPaint(damage_rect_);
}
2016-07-27 17:19:53 +00:00
void OffScreenOutputDevice::SetActive(bool active, bool paint) {
2016-07-27 17:44:41 +00:00
if (active == active_)
return;
active_ = active;
2016-07-27 17:19:53 +00:00
if (!active_ && !pending_damage_rect_.IsEmpty() && paint)
2016-08-03 04:04:36 +00:00
OnPaint(gfx::Rect(viewport_pixel_size_));
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-27 17:19:53 +00:00
rect.Intersect(gfx::Rect(viewport_pixel_size_));
if (rect.IsEmpty())
return;
2016-08-04 04:22:19 +00:00
callback_.Run(rect, *bitmap_);
2016-07-20 09:30:06 +00:00
}
} // namespace atom