• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 */
7 
8 #include "example/HelloWorld.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkGraphics.h"
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"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTileMode.h"
22 #include "include/effects/SkGradientShader.h"
23 #include "tools/sk_app/DisplayParams.h"
24 
25 #include <string.h>
26 
27 using namespace sk_app;
28 
Create(int argc,char ** argv,void * platformData)29 Application* Application::Create(int argc, char** argv, void* platformData) {
30     return new HelloWorld(argc, argv, platformData);
31 }
32 
HelloWorld(int argc,char ** argv,void * platformData)33 HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
34 #if defined(SK_GL)
35         : fBackendType(Window::kNativeGL_BackendType),
36 #elif defined(SK_VULKAN)
37         : fBackendType(Window::kVulkan_BackendType),
38 #elif defined(SK_DAWN)
39         : fBackendType(Window::kDawn_BackendType),
40 #else
41         : fBackendType(Window::kRaster_BackendType),
42 #endif
43         fRotationAngle(0) {
44     SkGraphics::Init();
45 
46     fWindow = Window::CreateNativeWindow(platformData);
47     fWindow->setRequestedDisplayParams(DisplayParams());
48 
49     // register callbacks
50     fWindow->pushLayer(this);
51 
52     fWindow->attach(fBackendType);
53 }
54 
~HelloWorld()55 HelloWorld::~HelloWorld() {
56     fWindow->detach();
57     delete fWindow;
58 }
59 
updateTitle()60 void HelloWorld::updateTitle() {
61     if (!fWindow) {
62         return;
63     }
64 
65     SkString title("Hello World ");
66     if (Window::kRaster_BackendType == fBackendType) {
67         title.append("Raster");
68     } else {
69 #if defined(SK_GL)
70         title.append("GL");
71 #elif defined(SK_VULKAN)
72         title.append("Vulkan");
73 #elif defined(SK_DAWN)
74         title.append("Dawn");
75 #else
76         title.append("Unknown GPU backend");
77 #endif
78     }
79 
80     fWindow->setTitle(title.c_str());
81 }
82 
onBackendCreated()83 void HelloWorld::onBackendCreated() {
84     this->updateTitle();
85     fWindow->show();
86     fWindow->inval();
87 }
88 
onPaint(SkSurface * surface)89 void HelloWorld::onPaint(SkSurface* surface) {
90     auto canvas = surface->getCanvas();
91 
92     // Clear background
93     canvas->clear(SK_ColorWHITE);
94 
95     SkPaint paint;
96     paint.setColor(SK_ColorRED);
97 
98     // Draw a rectangle with red paint
99     SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
100     canvas->drawRect(rect, paint);
101 
102     // Set up a linear gradient and draw a circle
103     {
104         SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
105         SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
106         paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
107                                                      SkTileMode::kMirror));
108         paint.setAntiAlias(true);
109 
110         canvas->drawCircle(200, 200, 64, paint);
111 
112         // Detach shader
113         paint.setShader(nullptr);
114     }
115 
116     // Draw a message with a nice black paint
117     SkFont font;
118     font.setSubpixel(true);
119     font.setSize(20);
120     paint.setColor(SK_ColorBLACK);
121 
122     canvas->save();
123     static const char message[] = "Hello World ";
124 
125     // Translate and rotate
126     canvas->translate(300, 300);
127     fRotationAngle += 0.2f;
128     if (fRotationAngle > 360) {
129         fRotationAngle -= 360;
130     }
131     canvas->rotate(fRotationAngle);
132 
133     // Draw the text
134     canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
135 
136     canvas->restore();
137 }
138 
onIdle()139 void HelloWorld::onIdle() {
140     // Just re-paint continuously
141     fWindow->inval();
142 }
143 
onChar(SkUnichar c,skui::ModifierKey modifiers)144 bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
145     if (' ' == c) {
146         if (Window::kRaster_BackendType == fBackendType) {
147 #if defined(SK_GL)
148             fBackendType = Window::kNativeGL_BackendType;
149 #elif defined(SK_VULKAN)
150             fBackendType = Window::kVulkan_BackendType;
151 #elif defined(SK_DAWN)
152             fBackendType = Window::kDawn_BackendType;
153 #else
154             SkDebugf("No GPU backend configured\n");
155             return true;
156 #endif
157         } else {
158             fBackendType = Window::kRaster_BackendType;
159         }
160         fWindow->detach();
161         fWindow->attach(fBackendType);
162     }
163     return true;
164 }
165