forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gpu: play skp files through the command buffer
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
Showing
11 changed files
with
627 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.