electron/atom/browser/osr_output_device.cc

101 lines
2.6 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>
namespace atom {
2016-07-27 17:19:53 +00:00
OffScreenOutputDevice::OffScreenOutputDevice() : callback_(nullptr) {
2016-07-20 09:30:06 +00:00
std::cout << "OffScreenOutputDevice" << std::endl;
}
OffScreenOutputDevice::~OffScreenOutputDevice() {
std::cout << "~OffScreenOutputDevice" << std::endl;
}
2016-07-27 17:19:53 +00:00
void OffScreenOutputDevice::SetPaintCallback(const OnPaintCallback* callback) {
callback_.reset(callback);
}
2016-07-20 09:30:06 +00:00
void OffScreenOutputDevice::Resize(
const gfx::Size& pixel_size, float scale_factor) {
std::cout << "Resize" << std::endl;
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(),
false);
if (bitmap_->drawsNothing()) {
std::cout << "drawsNothing" << std::endl;
NOTREACHED();
bitmap_.reset(NULL);
return;
}
bitmap_->eraseARGB(0, 0, 0, 0);
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:19:53 +00:00
OnPaint(damage_rect_);
// SkAutoLockPixels bitmap_pixels_lock(*bitmap_.get());
// saveSkBitmapToBMPFile(*(bitmap_.get()), "test.bmp");
// uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap_->getPixels());
// for (int i = 0; i<16; i++) {
// int x = static_cast<int>(pixels[i]);
// std::cout << std::hex << x << std::dec << std::endl;
// }
}
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());
// std::cout << "Paint" << std::endl;
if (callback_.get())
callback_->Run(rect, bitmap_->width(), bitmap_->height(),
bitmap_->getPixels());
2016-07-20 09:30:06 +00:00
}
} // namespace atom