2015-02-02 20:55:14 +00:00
|
|
|
/*
|
2017-11-21 18:18:02 +00:00
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "example/HelloWorld.h"
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2022-04-29 14:23:46 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2022-04-29 14:23:46 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkGraphics.h"
|
2022-04-29 14:23:46 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2022-04-29 14:23:46 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2022-04-29 14:23:46 +00:00
|
|
|
#include "tools/sk_app/DisplayParams.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
using namespace sk_app;
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
Application* Application::Create(int argc, char** argv, void* platformData) {
|
|
|
|
return new HelloWorld(argc, argv, platformData);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
|
2022-03-08 20:27:12 +00:00
|
|
|
#if defined(SK_GL)
|
|
|
|
: fBackendType(Window::kNativeGL_BackendType),
|
|
|
|
#elif defined(SK_VULKAN)
|
|
|
|
: fBackendType(Window::kVulkan_BackendType),
|
2022-03-21 17:06:32 +00:00
|
|
|
#elif defined(SK_DAWN)
|
|
|
|
: fBackendType(Window::kDawn_BackendType),
|
2022-03-08 20:27:12 +00:00
|
|
|
#else
|
|
|
|
: fBackendType(Window::kRaster_BackendType),
|
|
|
|
#endif
|
|
|
|
fRotationAngle(0) {
|
2017-11-21 18:18:02 +00:00
|
|
|
SkGraphics::Init();
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow = Window::CreateNativeWindow(platformData);
|
|
|
|
fWindow->setRequestedDisplayParams(DisplayParams());
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// register callbacks
|
2017-12-08 21:45:43 +00:00
|
|
|
fWindow->pushLayer(this);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow->attach(fBackendType);
|
|
|
|
}
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
HelloWorld::~HelloWorld() {
|
|
|
|
fWindow->detach();
|
|
|
|
delete fWindow;
|
|
|
|
}
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::updateTitle() {
|
2022-03-08 20:27:12 +00:00
|
|
|
if (!fWindow) {
|
2017-11-21 18:18:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString title("Hello World ");
|
2022-03-08 20:27:12 +00:00
|
|
|
if (Window::kRaster_BackendType == fBackendType) {
|
|
|
|
title.append("Raster");
|
|
|
|
} else {
|
|
|
|
#if defined(SK_GL)
|
|
|
|
title.append("GL");
|
|
|
|
#elif defined(SK_VULKAN)
|
|
|
|
title.append("Vulkan");
|
2022-03-21 17:06:32 +00:00
|
|
|
#elif defined(SK_DAWN)
|
|
|
|
title.append("Dawn");
|
2022-03-08 20:27:12 +00:00
|
|
|
#else
|
|
|
|
title.append("Unknown GPU backend");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow->setTitle(title.c_str());
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::onBackendCreated() {
|
|
|
|
this->updateTitle();
|
|
|
|
fWindow->show();
|
|
|
|
fWindow->inval();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:00:10 +00:00
|
|
|
void HelloWorld::onPaint(SkSurface* surface) {
|
|
|
|
auto canvas = surface->getCanvas();
|
|
|
|
|
2015-02-02 20:55:14 +00:00
|
|
|
// Clear background
|
2017-11-21 18:18:02 +00:00
|
|
|
canvas->clear(SK_ColorWHITE);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// Draw a rectangle with red paint
|
2016-03-14 19:22:10 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
|
2015-02-02 20:55:14 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
|
|
|
|
// Set up a linear gradient and draw a circle
|
|
|
|
{
|
2017-11-21 18:18:02 +00:00
|
|
|
SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
|
|
|
|
SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
|
|
|
|
paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror));
|
2017-11-21 18:18:02 +00:00
|
|
|
paint.setAntiAlias(true);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
canvas->drawCircle(200, 200, 64, paint);
|
|
|
|
|
|
|
|
// Detach shader
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(nullptr);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// Draw a message with a nice black paint
|
2019-01-04 04:29:38 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setSize(20);
|
2015-02-02 20:55:14 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
canvas->save();
|
2022-01-06 19:26:59 +00:00
|
|
|
static const char message[] = "Hello World ";
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
// Translate and rotate
|
|
|
|
canvas->translate(300, 300);
|
|
|
|
fRotationAngle += 0.2f;
|
|
|
|
if (fRotationAngle > 360) {
|
|
|
|
fRotationAngle -= 360;
|
|
|
|
}
|
|
|
|
canvas->rotate(fRotationAngle);
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// Draw the text
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::onIdle() {
|
2022-01-11 12:35:26 +00:00
|
|
|
// Just re-paint continuously
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow->inval();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
|
2017-11-21 18:18:02 +00:00
|
|
|
if (' ' == c) {
|
2022-03-08 20:27:12 +00:00
|
|
|
if (Window::kRaster_BackendType == fBackendType) {
|
|
|
|
#if defined(SK_GL)
|
|
|
|
fBackendType = Window::kNativeGL_BackendType;
|
|
|
|
#elif defined(SK_VULKAN)
|
|
|
|
fBackendType = Window::kVulkan_BackendType;
|
2022-03-21 17:06:32 +00:00
|
|
|
#elif defined(SK_DAWN)
|
|
|
|
fBackendType = Window::kDawn_BackendType;
|
2022-03-08 20:27:12 +00:00
|
|
|
#else
|
|
|
|
SkDebugf("No GPU backend configured\n");
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
fBackendType = Window::kRaster_BackendType;
|
|
|
|
}
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow->detach();
|
|
|
|
fWindow->attach(fBackendType);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|