blob: 308908bcd775c1eda0259cb991d88d689a9c74d6 [file] [log] [blame]
caryclark52edc4d2015-02-02 12:55:14 -08001/*
Brian Osmaneff04b52017-11-21 13:18:02 -05002* Copyright 2017 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
caryclark52edc4d2015-02-02 12:55:14 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "example/HelloWorld.h"
caryclark52edc4d2015-02-02 12:55:14 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Kevin Lubick88c15ae2022-04-29 10:23:46 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFont.h"
Kevin Lubick88c15ae2022-04-29 10:23:46 -040013#include "include/core/SkFontTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkGraphics.h"
Kevin Lubick88c15ae2022-04-29 10:23:46 -040015#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
Kevin Lubick88c15ae2022-04-29 10:23:46 -040021#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkGradientShader.h"
Jim Van Verthfb3f61d2024-01-18 16:59:01 -050023#include "tools/fonts/FontToolUtils.h"
Florin Malita26fa4b32023-06-28 11:04:46 -040024#include "tools/window/DisplayParams.h"
Kevin Lubick88c15ae2022-04-29 10:23:46 -040025
26#include <string.h>
caryclark52edc4d2015-02-02 12:55:14 -080027
Brian Osmaneff04b52017-11-21 13:18:02 -050028using namespace sk_app;
Florin Malita26fa4b32023-06-28 11:04:46 -040029using skwindow::DisplayParams;
Brian Osmaneff04b52017-11-21 13:18:02 -050030
31Application* Application::Create(int argc, char** argv, void* platformData) {
32 return new HelloWorld(argc, argv, platformData);
33}
34
Brian Osmaneff04b52017-11-21 13:18:02 -050035HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
Kevin Lubick7ac74132022-03-08 15:27:12 -050036#if defined(SK_GL)
37 : fBackendType(Window::kNativeGL_BackendType),
38#elif defined(SK_VULKAN)
39 : fBackendType(Window::kVulkan_BackendType),
40#else
41 : fBackendType(Window::kRaster_BackendType),
42#endif
43 fRotationAngle(0) {
caryclark52edc4d2015-02-02 12:55:14 -080044 SkGraphics::Init();
Brian Osmaneff04b52017-11-21 13:18:02 -050045
46 fWindow = Window::CreateNativeWindow(platformData);
47 fWindow->setRequestedDisplayParams(DisplayParams());
48
49 // register callbacks
Brian Osman80fc07e2017-12-08 16:45:43 -050050 fWindow->pushLayer(this);
Brian Osmaneff04b52017-11-21 13:18:02 -050051
52 fWindow->attach(fBackendType);
Jim Van Verthfb3f61d2024-01-18 16:59:01 -050053
54 fTypeface = ToolUtils::CreateTypefaceFromResource("fonts/Roboto-Regular.ttf");
55 if (!fTypeface) {
56 fTypeface = ToolUtils::DefaultPortableTypeface();
57 }
caryclark52edc4d2015-02-02 12:55:14 -080058}
59
Brian Osmaneff04b52017-11-21 13:18:02 -050060HelloWorld::~HelloWorld() {
61 fWindow->detach();
62 delete fWindow;
caryclark52edc4d2015-02-02 12:55:14 -080063}
64
Brian Osmaneff04b52017-11-21 13:18:02 -050065void HelloWorld::updateTitle() {
Kevin Lubick7ac74132022-03-08 15:27:12 -050066 if (!fWindow) {
Brian Osmaneff04b52017-11-21 13:18:02 -050067 return;
caryclark52edc4d2015-02-02 12:55:14 -080068 }
69
Brian Osmaneff04b52017-11-21 13:18:02 -050070 SkString title("Hello World ");
Kevin Lubick7ac74132022-03-08 15:27:12 -050071 if (Window::kRaster_BackendType == fBackendType) {
72 title.append("Raster");
73 } else {
74#if defined(SK_GL)
75 title.append("GL");
76#elif defined(SK_VULKAN)
77 title.append("Vulkan");
Kevin Lubick14abec42022-03-21 13:06:32 -040078#elif defined(SK_DAWN)
79 title.append("Dawn");
Kevin Lubick7ac74132022-03-08 15:27:12 -050080#else
81 title.append("Unknown GPU backend");
82#endif
83 }
84
Brian Osmaneff04b52017-11-21 13:18:02 -050085 fWindow->setTitle(title.c_str());
caryclark52edc4d2015-02-02 12:55:14 -080086}
87
Brian Osmaneff04b52017-11-21 13:18:02 -050088void HelloWorld::onBackendCreated() {
89 this->updateTitle();
90 fWindow->show();
91 fWindow->inval();
caryclark52edc4d2015-02-02 12:55:14 -080092}
93
Robert Phillips9882dae2019-03-04 11:00:10 -050094void HelloWorld::onPaint(SkSurface* surface) {
95 auto canvas = surface->getCanvas();
96
caryclark52edc4d2015-02-02 12:55:14 -080097 // Clear background
Brian Osmaneff04b52017-11-21 13:18:02 -050098 canvas->clear(SK_ColorWHITE);
caryclark52edc4d2015-02-02 12:55:14 -080099
100 SkPaint paint;
101 paint.setColor(SK_ColorRED);
102
103 // Draw a rectangle with red paint
reedc6f28f72016-03-14 12:22:10 -0700104 SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
caryclark52edc4d2015-02-02 12:55:14 -0800105 canvas->drawRect(rect, paint);
106
107 // Set up a linear gradient and draw a circle
108 {
Brian Osmaneff04b52017-11-21 13:18:02 -0500109 SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
110 SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
111 paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -0400112 SkTileMode::kMirror));
Brian Osmaneff04b52017-11-21 13:18:02 -0500113 paint.setAntiAlias(true);
caryclark52edc4d2015-02-02 12:55:14 -0800114
115 canvas->drawCircle(200, 200, 64, paint);
116
117 // Detach shader
reedc6f28f72016-03-14 12:22:10 -0700118 paint.setShader(nullptr);
caryclark52edc4d2015-02-02 12:55:14 -0800119 }
120
Brian Osmaneff04b52017-11-21 13:18:02 -0500121 // Draw a message with a nice black paint
Jim Van Verthfb3f61d2024-01-18 16:59:01 -0500122 SkFont font(fTypeface, 20);
Mike Reeda6d1c0a2019-01-03 23:29:38 -0500123 font.setSubpixel(true);
caryclark52edc4d2015-02-02 12:55:14 -0800124 paint.setColor(SK_ColorBLACK);
caryclark52edc4d2015-02-02 12:55:14 -0800125
126 canvas->save();
Kevin Lubick048545d2022-01-06 14:26:59 -0500127 static const char message[] = "Hello World ";
caryclark52edc4d2015-02-02 12:55:14 -0800128
129 // Translate and rotate
130 canvas->translate(300, 300);
131 fRotationAngle += 0.2f;
132 if (fRotationAngle > 360) {
133 fRotationAngle -= 360;
134 }
135 canvas->rotate(fRotationAngle);
136
Brian Osmaneff04b52017-11-21 13:18:02 -0500137 // Draw the text
Ben Wagner51e15a62019-05-07 15:38:46 -0400138 canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
caryclark52edc4d2015-02-02 12:55:14 -0800139
140 canvas->restore();
141}
142
Brian Osmaneff04b52017-11-21 13:18:02 -0500143void HelloWorld::onIdle() {
Kevin Lubick7a14f782022-01-11 07:35:26 -0500144 // Just re-paint continuously
Brian Osmaneff04b52017-11-21 13:18:02 -0500145 fWindow->inval();
caryclark52edc4d2015-02-02 12:55:14 -0800146}
147
Hal Canaryb1f411a2019-08-29 10:39:22 -0400148bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmaneff04b52017-11-21 13:18:02 -0500149 if (' ' == c) {
Kevin Lubick7ac74132022-03-08 15:27:12 -0500150 if (Window::kRaster_BackendType == fBackendType) {
151#if defined(SK_GL)
152 fBackendType = Window::kNativeGL_BackendType;
153#elif defined(SK_VULKAN)
154 fBackendType = Window::kVulkan_BackendType;
155#else
156 SkDebugf("No GPU backend configured\n");
157 return true;
158#endif
159 } else {
160 fBackendType = Window::kRaster_BackendType;
161 }
Brian Osmaneff04b52017-11-21 13:18:02 -0500162 fWindow->detach();
163 fWindow->attach(fBackendType);
caryclark52edc4d2015-02-02 12:55:14 -0800164 }
165 return true;
166}