caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 1 | /* |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 2 | * 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 | */ |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "example/HelloWorld.h" |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Kevin Lubick | 88c15ae | 2022-04-29 10:23:46 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFont.h" |
Kevin Lubick | 88c15ae | 2022-04-29 10:23:46 -0400 | [diff] [blame] | 13 | #include "include/core/SkFontTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkGraphics.h" |
Kevin Lubick | 88c15ae | 2022-04-29 10:23:46 -0400 | [diff] [blame] | 15 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkSurface.h" |
Kevin Lubick | 88c15ae | 2022-04-29 10:23:46 -0400 | [diff] [blame] | 21 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/effects/SkGradientShader.h" |
Jim Van Verth | fb3f61d | 2024-01-18 16:59:01 -0500 | [diff] [blame] | 23 | #include "tools/fonts/FontToolUtils.h" |
Florin Malita | 26fa4b3 | 2023-06-28 11:04:46 -0400 | [diff] [blame] | 24 | #include "tools/window/DisplayParams.h" |
Kevin Lubick | 88c15ae | 2022-04-29 10:23:46 -0400 | [diff] [blame] | 25 | |
| 26 | #include <string.h> |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 27 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 28 | using namespace sk_app; |
Florin Malita | 26fa4b3 | 2023-06-28 11:04:46 -0400 | [diff] [blame] | 29 | using skwindow::DisplayParams; |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 30 | |
| 31 | Application* Application::Create(int argc, char** argv, void* platformData) { |
| 32 | return new HelloWorld(argc, argv, platformData); |
| 33 | } |
| 34 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 35 | HelloWorld::HelloWorld(int argc, char** argv, void* platformData) |
Kevin Lubick | 7ac7413 | 2022-03-08 15:27:12 -0500 | [diff] [blame] | 36 | #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) { |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 44 | SkGraphics::Init(); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 45 | |
| 46 | fWindow = Window::CreateNativeWindow(platformData); |
| 47 | fWindow->setRequestedDisplayParams(DisplayParams()); |
| 48 | |
| 49 | // register callbacks |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 50 | fWindow->pushLayer(this); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 51 | |
| 52 | fWindow->attach(fBackendType); |
Jim Van Verth | fb3f61d | 2024-01-18 16:59:01 -0500 | [diff] [blame] | 53 | |
| 54 | fTypeface = ToolUtils::CreateTypefaceFromResource("fonts/Roboto-Regular.ttf"); |
| 55 | if (!fTypeface) { |
| 56 | fTypeface = ToolUtils::DefaultPortableTypeface(); |
| 57 | } |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 60 | HelloWorld::~HelloWorld() { |
| 61 | fWindow->detach(); |
| 62 | delete fWindow; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 65 | void HelloWorld::updateTitle() { |
Kevin Lubick | 7ac7413 | 2022-03-08 15:27:12 -0500 | [diff] [blame] | 66 | if (!fWindow) { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 67 | return; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 70 | SkString title("Hello World "); |
Kevin Lubick | 7ac7413 | 2022-03-08 15:27:12 -0500 | [diff] [blame] | 71 | 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 Lubick | 14abec4 | 2022-03-21 13:06:32 -0400 | [diff] [blame] | 78 | #elif defined(SK_DAWN) |
| 79 | title.append("Dawn"); |
Kevin Lubick | 7ac7413 | 2022-03-08 15:27:12 -0500 | [diff] [blame] | 80 | #else |
| 81 | title.append("Unknown GPU backend"); |
| 82 | #endif |
| 83 | } |
| 84 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 85 | fWindow->setTitle(title.c_str()); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 88 | void HelloWorld::onBackendCreated() { |
| 89 | this->updateTitle(); |
| 90 | fWindow->show(); |
| 91 | fWindow->inval(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 94 | void HelloWorld::onPaint(SkSurface* surface) { |
| 95 | auto canvas = surface->getCanvas(); |
| 96 | |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 97 | // Clear background |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 98 | canvas->clear(SK_ColorWHITE); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 99 | |
| 100 | SkPaint paint; |
| 101 | paint.setColor(SK_ColorRED); |
| 102 | |
| 103 | // Draw a rectangle with red paint |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 104 | SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 105 | canvas->drawRect(rect, paint); |
| 106 | |
| 107 | // Set up a linear gradient and draw a circle |
| 108 | { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 109 | SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } }; |
| 110 | SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK }; |
| 111 | paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 112 | SkTileMode::kMirror)); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 113 | paint.setAntiAlias(true); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 114 | |
| 115 | canvas->drawCircle(200, 200, 64, paint); |
| 116 | |
| 117 | // Detach shader |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 118 | paint.setShader(nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 121 | // Draw a message with a nice black paint |
Jim Van Verth | fb3f61d | 2024-01-18 16:59:01 -0500 | [diff] [blame] | 122 | SkFont font(fTypeface, 20); |
Mike Reed | a6d1c0a | 2019-01-03 23:29:38 -0500 | [diff] [blame] | 123 | font.setSubpixel(true); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 124 | paint.setColor(SK_ColorBLACK); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 125 | |
| 126 | canvas->save(); |
Kevin Lubick | 048545d | 2022-01-06 14:26:59 -0500 | [diff] [blame] | 127 | static const char message[] = "Hello World "; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 128 | |
| 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 Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 137 | // Draw the text |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 138 | canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 139 | |
| 140 | canvas->restore(); |
| 141 | } |
| 142 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 143 | void HelloWorld::onIdle() { |
Kevin Lubick | 7a14f78 | 2022-01-11 07:35:26 -0500 | [diff] [blame] | 144 | // Just re-paint continuously |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 145 | fWindow->inval(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 148 | bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 149 | if (' ' == c) { |
Kevin Lubick | 7ac7413 | 2022-03-08 15:27:12 -0500 | [diff] [blame] | 150 | 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 Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 162 | fWindow->detach(); |
| 163 | fWindow->attach(fBackendType); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 164 | } |
| 165 | return true; |
| 166 | } |