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 "HelloWorld.h"
9 
10 #include "GrContext.h"
11 #include "SkCanvas.h"
12 #include "SkFont.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 
16 using namespace sk_app;
17 
Create(int argc,char ** argv,void * platformData)18 Application* Application::Create(int argc, char** argv, void* platformData) {
19     return new HelloWorld(argc, argv, platformData);
20 }
21 
HelloWorld(int argc,char ** argv,void * platformData)22 HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
23         : fBackendType(Window::kNativeGL_BackendType)
24         , fRotationAngle(0) {
25     SkGraphics::Init();
26 
27     fWindow = Window::CreateNativeWindow(platformData);
28     fWindow->setRequestedDisplayParams(DisplayParams());
29 
30     // register callbacks
31     fWindow->pushLayer(this);
32 
33     fWindow->attach(fBackendType);
34 }
35 
~HelloWorld()36 HelloWorld::~HelloWorld() {
37     fWindow->detach();
38     delete fWindow;
39 }
40 
updateTitle()41 void HelloWorld::updateTitle() {
42     if (!fWindow || fWindow->sampleCount() <= 1) {
43         return;
44     }
45 
46     SkString title("Hello World ");
47     title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL");
48     fWindow->setTitle(title.c_str());
49 }
50 
onBackendCreated()51 void HelloWorld::onBackendCreated() {
52     this->updateTitle();
53     fWindow->show();
54     fWindow->inval();
55 }
56 
onPaint(SkCanvas * canvas)57 void HelloWorld::onPaint(SkCanvas* canvas) {
58     // Clear background
59     canvas->clear(SK_ColorWHITE);
60 
61     SkPaint paint;
62     paint.setColor(SK_ColorRED);
63 
64     // Draw a rectangle with red paint
65     SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
66     canvas->drawRect(rect, paint);
67 
68     // Set up a linear gradient and draw a circle
69     {
70         SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
71         SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
72         paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
73                                                      SkShader::kMirror_TileMode));
74         paint.setAntiAlias(true);
75 
76         canvas->drawCircle(200, 200, 64, paint);
77 
78         // Detach shader
79         paint.setShader(nullptr);
80     }
81 
82     // Draw a message with a nice black paint
83     SkFont font;
84     font.setSubpixel(true);
85     font.setSize(20);
86     paint.setColor(SK_ColorBLACK);
87 
88     canvas->save();
89     static const char message[] = "Hello World";
90 
91     // Translate and rotate
92     canvas->translate(300, 300);
93     fRotationAngle += 0.2f;
94     if (fRotationAngle > 360) {
95         fRotationAngle -= 360;
96     }
97     canvas->rotate(fRotationAngle);
98 
99     // Draw the text
100     canvas->drawSimpleText(message, strlen(message), kUTF8_SkTextEncoding, 0, 0, font, paint);
101 
102     canvas->restore();
103 }
104 
onIdle()105 void HelloWorld::onIdle() {
106     // Just re-paint continously
107     fWindow->inval();
108 }
109 
onChar(SkUnichar c,uint32_t modifiers)110 bool HelloWorld::onChar(SkUnichar c, uint32_t modifiers) {
111     if (' ' == c) {
112         fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType
113                                                                    : Window::kRaster_BackendType;
114         fWindow->detach();
115         fWindow->attach(fBackendType);
116     }
117     return true;
118 }
119