electron/atom/browser/osr/osr_output_device.cc

104 lines
2.4 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),
active_(false) {
2016-07-27 17:44:41 +00:00
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
if (viewport_pixel_size_ == pixel_size) return;
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(),
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();
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;
SkIRect damage = SkIRect::MakeXYWH(
damage_rect_.x(),
damage_rect_.y(),
damage_rect_.width(),
damage_rect_.height()
);
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());
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-31 20:05:36 +00:00
if (active_)
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;
rect.Intersect(gfx::Rect(viewport_pixel_size_));
if (rect.IsEmpty())
return;
2016-08-03 04:04:36 +00:00
SkAutoLockPixels bitmap_pixels_lock(*bitmap_);
2016-08-04 04:22:19 +00:00
callback_.Run(rect, *bitmap_);
2016-07-20 09:30:06 +00:00
}
} // namespace atom