Skip to content

Commit

Permalink
gpu: play skp files through the command buffer
Browse files Browse the repository at this point in the history
Small utility program that can take an skp, or directory of skps and
GPU rasterizes them to png, through chrome's command buffer.

BUG=

Committed: https://crrev.com/6daedb499182a5b6b0bcd54004afb7ebb3c3443b
Cr-Commit-Position: refs/heads/master@{#334928}

Review URL: https://codereview.chromium.org/1149893010

Cr-Commit-Position: refs/heads/master@{#335042}
  • Loading branch information
hendrikw authored and Commit bot committed Jun 18, 2015
1 parent 7ef5cf5 commit 5c1da6d
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 1 deletion.
4 changes: 4 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ group("both_gn_and_gyp") {
} else if (!is_android) {
deps += [ "//breakpad:symupload" ]
}

if (!is_ios) {
deps += [ "//gpu/skia_runner:skia_runner" ]
}
}

group("gn_only") {
Expand Down
1 change: 1 addition & 0 deletions build/all.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
'dependencies': [
'../device/bluetooth/bluetooth.gyp:*',
'../device/device_tests.gyp:*',
'../gpu/skia_runner/skia_runner.gyp:*',
],
}],
['use_openssl==0 and (OS=="mac" or OS=="ios" or OS=="win")', {
Expand Down
17 changes: 16 additions & 1 deletion gpu/command_buffer/service/in_process_command_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuThread(
}
}

scoped_refptr<InProcessCommandBuffer::Service> GetInitialService(
const scoped_refptr<InProcessCommandBuffer::Service>& service) {
if (service)
return service;

// Call base::ThreadTaskRunnerHandle::IsSet() to ensure that it is
// instantiated before we create the GPU thread, otherwise shutdown order will
// delete the ThreadTaskRunnerHandle before the GPU thread's message loop,
// and when the message loop is shutdown, it will recreate
// ThreadTaskRunnerHandle, which will re-add a new task to the, AtExitManager,
// which causes a deadlock because it's already locked.
base::ThreadTaskRunnerHandle::IsSet();
return g_default_service.Get().gpu_thread;
}

} // anonyous namespace

InProcessCommandBuffer::Service::Service() {}
Expand Down Expand Up @@ -271,7 +286,7 @@ InProcessCommandBuffer::InProcessCommandBuffer(
last_put_offset_(-1),
gpu_memory_buffer_manager_(nullptr),
flush_event_(false, false),
service_(service.get() ? service : g_default_service.Get().gpu_thread),
service_(GetInitialService(service)),
gpu_thread_weak_ptr_factory_(this) {
DCHECK(service_.get());
next_image_id_.GetNext();
Expand Down
29 changes: 29 additions & 0 deletions gpu/skia_runner/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/ui.gni")

# GYP version: //gpu/skia_runner/skia_runner.gyp:skia_runner
executable("skia_runner") {
sources = [
"in_process_graphics_system.cc",
"sk_picture_rasterizer.cc",
"skia_runner.cc",
]

deps = [
"//base",
"//gpu/command_buffer/common",
"//gpu/command_buffer/client:gles2_implementation",
"//gpu/command_buffer/client:gl_in_process_context",
"//gpu/skia_bindings",
"//skia",
"//third_party/WebKit/public:blink",
"//ui/gfx",
"//ui/gl",
]

configs -= [ "//build/config/compiler:chromium_code" ]
configs += [ "//build/config/compiler:no_chromium_code" ]
}
6 changes: 6 additions & 0 deletions gpu/skia_runner/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include_rules = [
"+third_party/khronos",
"+third_party/skia",
"+third_party/WebKit/public/platform",
"+skia/ext",
]
150 changes: 150 additions & 0 deletions gpu/skia_runner/in_process_graphics_system.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/skia_runner/in_process_graphics_system.h"

#include <iostream>

#include "base/lazy_instance.h"
#include "base/memory/discardable_memory.h"
#include "base/memory/discardable_memory_allocator.h"
#include "base/thread_task_runner_handle.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/gles2_lib.h"
#include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"

namespace {

// TODO(hendrikw): Replace TestDiscardableMemoryAllocator and move to base?
class NonDiscardableMemory : public base::DiscardableMemory {
public:
explicit NonDiscardableMemory(size_t size) : data_(new uint8_t[size]) {}
bool Lock() override { return false; }
void Unlock() override {}
void* data() const override { return data_.get(); }

private:
scoped_ptr<uint8_t[]> data_;
};

class NonDiscardableMemoryAllocator : public base::DiscardableMemoryAllocator {
public:
// Overridden from DiscardableMemoryAllocator:
scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
size_t size) override {
return make_scoped_ptr(new NonDiscardableMemory(size));
}
};

// Singleton used to initialize and terminate the gles2 library.
class GLES2Initializer {
public:
GLES2Initializer() { gles2::Initialize(); }
~GLES2Initializer() { gles2::Terminate(); }

private:
DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
};

base::LazyInstance<GLES2Initializer> g_gles2_initializer =
LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<NonDiscardableMemoryAllocator> g_discardable;

} // namespace anonymous

namespace skia_runner {

void BindContext3DGLContextCallback(const GrGLInterface* gl_interface) {
gles2::SetGLContext(reinterpret_cast<gpu::GLInProcessContext*>(
gl_interface->fCallbackData)->GetImplementation());
}

InProcessGraphicsSystem::InProcessGraphicsSystem() {
base::DiscardableMemoryAllocator::SetInstance(&g_discardable.Get());
g_gles2_initializer.Get();

if (!gfx::GLSurface::InitializeOneOff()) {
LOG(ERROR) << "Unable to initialize gfx::GLSurface.";
return;
}

// Create the in process context.
in_process_context_ = CreateInProcessContext();
if (!in_process_context_) {
LOG(ERROR) << "Unable to create in process context.";
return;
}

// Create and set up skia's GL bindings.
gles2::SetGLContext(in_process_context_->GetImplementation());
GrGLInterface* bindings = skia_bindings::CreateCommandBufferSkiaGLBinding();
if (!bindings) {
LOG(ERROR) << "Unable to create skia command buffer bindings.";
return;
}

bindings->fCallback = BindContext3DGLContextCallback;
bindings->fCallbackData =
reinterpret_cast<GrGLInterfaceCallbackData>(in_process_context_.get());

// Create skia's graphics context.
gr_context_ = skia::AdoptRef(GrContext::Create(
kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(bindings)));

if (!gr_context_) {
LOG(ERROR) << "Unable to create skia graphics context.";
return;
}

// Set skia's graphics context cache size.
const int kMaxGaneshResourceCacheCount = 2048;
const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
kMaxGaneshResourceCacheBytes);
}

InProcessGraphicsSystem::~InProcessGraphicsSystem() {
}

bool InProcessGraphicsSystem::IsSuccessfullyInitialized() const {
return gr_context_;
}

int InProcessGraphicsSystem::GetMaxTextureSize() const {
int max_texture_size = 0;
in_process_context_->GetImplementation()->GetIntegerv(GL_MAX_TEXTURE_SIZE,
&max_texture_size);
return max_texture_size;
}

scoped_ptr<gpu::GLInProcessContext>
InProcessGraphicsSystem::CreateInProcessContext() const {
const bool is_offscreen = true;
const bool share_resources = true;
gpu::gles2::ContextCreationAttribHelper attribs;
attribs.alpha_size = 8;
attribs.blue_size = 8;
attribs.green_size = 8;
attribs.red_size = 8;
attribs.depth_size = 24;
attribs.stencil_size = 8;
attribs.samples = 0;
attribs.sample_buffers = 0;
attribs.fail_if_major_perf_caveat = false;
attribs.bind_generates_resource = false;
gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;

scoped_ptr<gpu::GLInProcessContext> context =
make_scoped_ptr(gpu::GLInProcessContext::Create(
nullptr, nullptr, is_offscreen, gfx::kNullAcceleratedWidget,
gfx::Size(1, 1), nullptr, share_resources, attribs, gpu_preference,
gpu::GLInProcessContextSharedMemoryLimits(), nullptr, nullptr));

DCHECK(context);
return context.Pass();
}

} // namespace skia_runner
29 changes: 29 additions & 0 deletions gpu/skia_runner/in_process_graphics_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_ptr.h"
#include "gpu/command_buffer/client/gl_in_process_context.h"
#include "skia/ext/refptr.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/gpu/GrContext.h"

namespace skia_runner {

class InProcessGraphicsSystem {
public:
InProcessGraphicsSystem();
~InProcessGraphicsSystem();

bool IsSuccessfullyInitialized() const;
skia::RefPtr<GrContext> GetGrContext() const { return gr_context_; }
int GetMaxTextureSize() const;

private:
scoped_ptr<gpu::GLInProcessContext> CreateInProcessContext() const;

scoped_ptr<gpu::GLInProcessContext> in_process_context_;
skia::RefPtr<GrContext> gr_context_;
};

} // namespace skia_runner
91 changes: 91 additions & 0 deletions gpu/skia_runner/sk_picture_rasterizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/skia_runner/sk_picture_rasterizer.h"

#include <iostream>

#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"

namespace skia_runner {

SkPictureRasterizer::SkPictureRasterizer(skia::RefPtr<GrContext> gr_context,
int max_texture_size)
: use_lcd_text_(false),
use_distance_field_text_(false),
msaa_sample_count_(0),
max_texture_size_(max_texture_size),
gr_context_(gr_context) {
}

SkPictureRasterizer::~SkPictureRasterizer() {
}

skia::RefPtr<SkImage> SkPictureRasterizer::RasterizeTile(
const SkPicture* picture,
const SkRect& rect) const {
DCHECK(gr_context_);

SkImageInfo info = SkImageInfo::MakeN32Premul(rect.width(), rect.height());
uint32_t flags = 0;
if (use_distance_field_text_)
flags = SkSurfaceProps::kUseDistanceFieldFonts_Flag;

SkSurfaceProps surface_props =
use_lcd_text_
? SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType)
: SkSurfaceProps(flags, kUnknown_SkPixelGeometry);

skia::RefPtr<SkSurface> sk_surface(skia::AdoptRef(
SkSurface::NewRenderTarget(gr_context_.get(), SkSurface::kYes_Budgeted,
info, msaa_sample_count_, &surface_props)));
if (sk_surface) {
SkCanvas* canvas = sk_surface->getCanvas();
canvas->translate(-rect.left(), -rect.top());
canvas->drawPicture(picture);

return skia::AdoptRef(sk_surface->newImageSnapshot());
}
return nullptr;
}

skia::RefPtr<SkImage> SkPictureRasterizer::Rasterize(
const SkPicture* picture) const {
if (!gr_context_)
return nullptr;

SkRect picture_rect = picture->cullRect();
if (picture_rect.width() <= max_texture_size_ &&
picture_rect.height() <= max_texture_size_)
return RasterizeTile(picture, picture_rect);

SkImageInfo info =
SkImageInfo::MakeN32Premul(picture_rect.width(), picture_rect.height());

skia::RefPtr<SkSurface> sk_surface(
skia::AdoptRef(SkSurface::NewRaster(info)));
SkCanvas* canvas = sk_surface->getCanvas();

int num_tiles_x = picture_rect.width() / max_texture_size_ + 1;
int num_tiles_y = picture_rect.height() / max_texture_size_ + 1;
for (int y = 0; y < num_tiles_y; ++y) {
SkRect tile_rect;
tile_rect.fTop = picture_rect.top() + y * max_texture_size_;
tile_rect.fBottom =
std::min(tile_rect.fTop + max_texture_size_, picture_rect.bottom());
for (int x = 0; x < num_tiles_x; ++x) {
tile_rect.fLeft = picture_rect.left() + x * max_texture_size_;
tile_rect.fRight =
std::min(tile_rect.fLeft + max_texture_size_, picture_rect.right());
skia::RefPtr<SkImage> tile(RasterizeTile(picture, tile_rect));
// canvas.drawBitmap(tile, tile_rect.left(), tile_rect.top());
canvas->drawImage(tile.get(), tile_rect.left(), tile_rect.top());
}
}
return skia::AdoptRef(sk_surface->newImageSnapshot());
}

} // namepsace skia_runner
Loading

0 comments on commit 5c1da6d

Please sign in to comment.