diff options
author | Brian Osman <brianosman@google.com> | 2017-11-21 13:18:02 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-11-21 18:37:19 +0000 |
commit | eff04b5ec287e0fee0d44207c10d2d11f7eade8a (patch) | |
tree | ea2cf00ea329c81611536aaa9a9f1eca47c67e9a /example/HelloWorld.cpp | |
parent | 2aa09dbe8aced37aa6bb285e62df45deb0e81650 (diff) |
Remove SampleApp and convert HelloWorld to sk_app
There is still a large amount of views code that could be trimmed down,
but which is used to implement samples (in viewer). Seemed simpler to
remove some of this code in pieces.
Bug: skia:
Change-Id: Ia3415060d03c8de604a154e3dc38379b754daab6
Reviewed-on: https://skia-review.googlesource.com/72801
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'example/HelloWorld.cpp')
-rw-r--r-- | example/HelloWorld.cpp | 155 |
1 files changed, 64 insertions, 91 deletions
diff --git a/example/HelloWorld.cpp b/example/HelloWorld.cpp index 83bc4ecf44..fba7f7e5a0 100644 --- a/example/HelloWorld.cpp +++ b/example/HelloWorld.cpp @@ -1,81 +1,78 @@ /* - * Copyright 2015 Google Inc. - * - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - * - */ +* Copyright 2017 Google Inc. +* +* Use of this source code is governed by a BSD-style license that can be +* found in the LICENSE file. +*/ #include "HelloWorld.h" -#include "gl/GrGLInterface.h" #include "GrContext.h" -#include "SkApplication.h" #include "SkCanvas.h" #include "SkGradientShader.h" #include "SkGraphics.h" -#include "SkGr.h" -void application_init() { - SkGraphics::Init(); - SkEvent::Init(); -} +using namespace sk_app; -void application_term() { - SkEvent::Term(); +Application* Application::Create(int argc, char** argv, void* platformData) { + return new HelloWorld(argc, argv, platformData); } -HelloWorldWindow::HelloWorldWindow(void* hwnd) - : INHERITED(hwnd) { - fType = kGPU_DeviceType; - fRotationAngle = 0; - this->setTitle(); - this->setUpBackend(); +static void on_backend_created_func(void* userData) { + HelloWorld* hw = reinterpret_cast<HelloWorld*>(userData); + return hw->onBackendCreated(); } -HelloWorldWindow::~HelloWorldWindow() { - tearDownBackend(); +static void on_paint_handler(SkCanvas* canvas, void* userData) { + HelloWorld* hw = reinterpret_cast<HelloWorld*>(userData); + return hw->onPaint(canvas); } -void HelloWorldWindow::tearDownBackend() { - INHERITED::release(); +static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) { + HelloWorld* hw = reinterpret_cast<HelloWorld*>(userData); + return hw->onChar(c, modifiers); } -void HelloWorldWindow::setTitle() { - SkString title("Hello World "); - title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); - INHERITED::setTitle(title.c_str()); -} +HelloWorld::HelloWorld(int argc, char** argv, void* platformData) + : fBackendType(Window::kNativeGL_BackendType) + , fRotationAngle(0) { + SkGraphics::Init(); -bool HelloWorldWindow::setUpBackend() { - this->setVisibleP(true); - this->setClipToBounds(false); + fWindow = Window::CreateNativeWindow(platformData); + fWindow->setRequestedDisplayParams(DisplayParams()); - bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo); - if (false == result) { - SkDebugf("Not possible to create backend.\n"); - release(); - return false; - } + // register callbacks + fWindow->registerBackendCreatedFunc(on_backend_created_func, this); + fWindow->registerPaintFunc(on_paint_handler, this); + fWindow->registerCharFunc(on_char_handler, this); - fInterface.reset(GrGLCreateNativeInterface()); - SkASSERT(NULL != fInterface); + fWindow->attach(fBackendType); +} - fContext = GrContext::MakeGL(fInterface.get()); - SkASSERT(NULL != fContext); +HelloWorld::~HelloWorld() { + fWindow->detach(); + delete fWindow; +} - this->setUpGpuBackedSurface(); - return true; +void HelloWorld::updateTitle() { + if (!fWindow || fWindow->sampleCount() < 0) { + return; + } + + SkString title("Hello World "); + title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL"); + fWindow->setTitle(title.c_str()); } -void HelloWorldWindow::setUpGpuBackedSurface() { - fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface.get(), fContext.get()); +void HelloWorld::onBackendCreated() { + this->updateTitle(); + fWindow->show(); + fWindow->inval(); } -void HelloWorldWindow::drawContents(SkCanvas* canvas) { +void HelloWorld::onPaint(SkCanvas* canvas) { // Clear background - canvas->drawColor(SK_ColorWHITE); + canvas->clear(SK_ColorWHITE); SkPaint paint; paint.setColor(SK_ColorRED); @@ -86,16 +83,11 @@ void HelloWorldWindow::drawContents(SkCanvas* canvas) { // Set up a linear gradient and draw a circle { - SkPoint linearPoints[] = { - {0, 0}, - {300, 300} - }; - SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; - - paint.setShader(SkGradientShader::MakeLinear( - linearPoints, linearColors, nullptr, 2, - SkShader::kMirror_TileMode)); - paint.setFlags(SkPaint::kAntiAlias_Flag); + SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } }; + SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK }; + paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2, + SkShader::kMirror_TileMode)); + paint.setAntiAlias(true); canvas->drawCircle(200, 200, 64, paint); @@ -103,15 +95,12 @@ void HelloWorldWindow::drawContents(SkCanvas* canvas) { paint.setShader(nullptr); } - // Draw a message with a nice black paint. - paint.setFlags( - SkPaint::kAntiAlias_Flag | - SkPaint::kSubpixelText_Flag); // ... avoid waggly text when rotating. + // Draw a message with a nice black paint + paint.setSubpixelText(true); paint.setColor(SK_ColorBLACK); paint.setTextSize(20); canvas->save(); - static const char message[] = "Hello World"; // Translate and rotate @@ -122,39 +111,23 @@ void HelloWorldWindow::drawContents(SkCanvas* canvas) { } canvas->rotate(fRotationAngle); - // Draw the text: + // Draw the text canvas->drawText(message, strlen(message), 0, 0, paint); canvas->restore(); } -void HelloWorldWindow::draw(SkCanvas* canvas) { - this->drawContents(canvas); - // Invalidate the window to force a redraw. Poor man's animation mechanism. - this->inval(NULL); - - if (kRaster_DeviceType == fType) { - fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr); - } - fGpuSurface->getCanvas()->flush(); - INHERITED::present(); -} - -void HelloWorldWindow::onSizeChange() { - this->setUpGpuBackedSurface(); +void HelloWorld::onIdle() { + // Just re-paint continously + fWindow->inval(); } -bool HelloWorldWindow::onHandleChar(SkUnichar unichar) { - if (' ' == unichar) { - fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceType; - tearDownBackend(); - setUpBackend(); - this->setTitle(); - this->inval(NULL); +bool HelloWorld::onChar(SkUnichar c, uint32_t modifiers) { + if (' ' == c) { + fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType + : Window::kRaster_BackendType; + fWindow->detach(); + fWindow->attach(fBackendType); } return true; } - -SkOSWindow* create_sk_window(void* hwnd, int , char** ) { - return new HelloWorldWindow(hwnd); -} |