Commit bfb7c7f7 authored by Peng Huang's avatar Peng Huang Committed by Commit Bot

ozone_demo: Copy code from Skia's hello world example to SkiaRenderer

The Skia's hello world example looks nicer, so use it to replace
the current Draw function.

Bug: None
Change-Id: I61fad12dae5a57bca0b37b6e9db3eb908f7e45b1
Reviewed-on: https://chromium-review.googlesource.com/935159Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539148}
parent 810bd94c
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/effects/SkGradientShader.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h" #include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h" #include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
#include "third_party/skia/include/gpu/gl/GrGLInterface.h" #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
...@@ -29,7 +29,6 @@ const GrGLInterface* GrGLCreateNativeInterface() { ...@@ -29,7 +29,6 @@ const GrGLInterface* GrGLCreateNativeInterface() {
}); });
} }
const char kDrawText[] = "draw-text";
const char kUseDDL[] = "use-ddl"; const char kUseDDL[] = "use-ddl";
} // namespace } // namespace
...@@ -40,7 +39,6 @@ SkiaRenderer::SkiaRenderer(gfx::AcceleratedWidget widget, ...@@ -40,7 +39,6 @@ SkiaRenderer::SkiaRenderer(gfx::AcceleratedWidget widget,
: RendererBase(widget, size), : RendererBase(widget, size),
gl_surface_(surface), gl_surface_(surface),
use_ddl_(base::CommandLine::ForCurrentProcess()->HasSwitch(kUseDDL)), use_ddl_(base::CommandLine::ForCurrentProcess()->HasSwitch(kUseDDL)),
draw_text_(base::CommandLine::ForCurrentProcess()->HasSwitch(kDrawText)),
condition_variable_(&lock_), condition_variable_(&lock_),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
...@@ -126,27 +124,52 @@ void SkiaRenderer::PostRenderFrameTask(gfx::SwapResult result) { ...@@ -126,27 +124,52 @@ void SkiaRenderer::PostRenderFrameTask(gfx::SwapResult result) {
void SkiaRenderer::Draw(SkCanvas* canvas, float fraction) { void SkiaRenderer::Draw(SkCanvas* canvas, float fraction) {
TRACE_EVENT0("ozone", "SkiaRenderer::Draw"); TRACE_EVENT0("ozone", "SkiaRenderer::Draw");
canvas->clear(SkColorSetARGB(255, 255 * (1 - fraction), 255 * fraction, 0)); // Clear background
if (draw_text_) { canvas->clear(SkColorSetARGB(255, 255 * fraction, 255 * (1 - fraction), 0));
SkPaint paint;
paint.setTextSize(48.f); SkPaint paint;
paint.setColor( paint.setColor(SK_ColorRED);
SkColorSetARGB(255, 255 * fraction, 255 * (1 - fraction), 0));
paint.setTypeface( // Draw a rectangle with red paint
SkTypeface::MakeFromName("monospace", SkFontStyle::Bold())); SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
canvas->setMatrix(SkMatrix::MakeScale(1 + fraction / 2, 1 - fraction / 2)); canvas->drawRect(rect, paint);
std::string text = use_ddl_ ? "Ozone Demo with DDL" : "Ozone Demo"; // Set up a linear gradient and draw a circle
canvas->drawText(text.c_str(), text.length(), 150, 150, paint); {
canvas->resetMatrix(); SkPoint linearPoints[] = {{0, 0}, {300, 300}};
} else { SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
SkPaint paint; paint.setShader(SkGradientShader::MakeLinear(
paint.setColor( linearPoints, linearColors, nullptr, 2, SkShader::kMirror_TileMode));
SkColorSetARGB(255, 255 * fraction, 255 * (1 - fraction), 0)); paint.setAntiAlias(true);
canvas->setMatrix(SkMatrix::MakeScale(1 + fraction / 2, 1 - fraction / 2));
canvas->drawCircle(200, 200, 50, paint); canvas->drawCircle(200, 200, 64, paint);
canvas->resetMatrix();
// Detach shader
paint.setShader(nullptr);
} }
// Draw a message with a nice black paint
paint.setSubpixelText(true);
paint.setColor(SK_ColorBLACK);
paint.setTextSize(32);
canvas->save();
static const char message[] = "Hello Ozone";
static const char message_ddl[] = "Hello Ozone + DDL";
// Translate and rotate
canvas->translate(300, 300);
rotation_angle_ += 0.2f;
if (rotation_angle_ > 360) {
rotation_angle_ -= 360;
}
canvas->rotate(rotation_angle_);
const char* text = use_ddl_ ? message_ddl : message;
// Draw the text
canvas->drawText(text, strlen(text), 0, 0, paint);
canvas->restore();
} }
void SkiaRenderer::StartDDLRenderThreadIfNecessary(SkSurface* sk_surface) { void SkiaRenderer::StartDDLRenderThreadIfNecessary(SkSurface* sk_surface) {
......
...@@ -54,7 +54,6 @@ class SkiaRenderer : public RendererBase, ...@@ -54,7 +54,6 @@ class SkiaRenderer : public RendererBase,
sk_sp<GrContext> gr_context_; sk_sp<GrContext> gr_context_;
const bool use_ddl_; const bool use_ddl_;
const bool draw_text_;
private: private:
// base::DelegateSimpleThread::Delegate: // base::DelegateSimpleThread::Delegate:
...@@ -64,6 +63,8 @@ class SkiaRenderer : public RendererBase, ...@@ -64,6 +63,8 @@ class SkiaRenderer : public RendererBase,
sk_sp<SkSurface> sk_surface_; sk_sp<SkSurface> sk_surface_;
float rotation_angle_ = 0.f;
std::unique_ptr<base::SimpleThread> ddl_render_thread_; std::unique_ptr<base::SimpleThread> ddl_render_thread_;
// The lock to protect |surface_charaterization_| and |ddls_|. // The lock to protect |surface_charaterization_| and |ddls_|.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment