blob: 83bc4ecf44beff285173643bb4a564beb91ff23c [file] [log] [blame]
caryclark52edc4d2015-02-02 12:55:14 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9
10#include "HelloWorld.h"
11
12#include "gl/GrGLInterface.h"
bsalomonf276ac52015-10-09 13:36:42 -070013#include "GrContext.h"
caryclark52edc4d2015-02-02 12:55:14 -080014#include "SkApplication.h"
15#include "SkCanvas.h"
16#include "SkGradientShader.h"
17#include "SkGraphics.h"
18#include "SkGr.h"
19
20void application_init() {
21 SkGraphics::Init();
22 SkEvent::Init();
23}
24
25void application_term() {
26 SkEvent::Term();
caryclark52edc4d2015-02-02 12:55:14 -080027}
28
29HelloWorldWindow::HelloWorldWindow(void* hwnd)
30 : INHERITED(hwnd) {
31 fType = kGPU_DeviceType;
caryclark52edc4d2015-02-02 12:55:14 -080032 fRotationAngle = 0;
33 this->setTitle();
34 this->setUpBackend();
35}
36
37HelloWorldWindow::~HelloWorldWindow() {
38 tearDownBackend();
39}
40
41void HelloWorldWindow::tearDownBackend() {
mtklein18300a32016-03-16 13:53:35 -070042 INHERITED::release();
caryclark52edc4d2015-02-02 12:55:14 -080043}
44
45void HelloWorldWindow::setTitle() {
46 SkString title("Hello World ");
47 title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl");
48 INHERITED::setTitle(title.c_str());
49}
50
51bool HelloWorldWindow::setUpBackend() {
caryclark52edc4d2015-02-02 12:55:14 -080052 this->setVisibleP(true);
53 this->setClipToBounds(false);
54
brianosman2d1ee792016-05-05 12:24:31 -070055 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo);
caryclark52edc4d2015-02-02 12:55:14 -080056 if (false == result) {
57 SkDebugf("Not possible to create backend.\n");
mtklein18300a32016-03-16 13:53:35 -070058 release();
caryclark52edc4d2015-02-02 12:55:14 -080059 return false;
60 }
61
Jim Van Verthc1720d42017-10-09 10:16:01 -040062 fInterface.reset(GrGLCreateNativeInterface());
caryclark52edc4d2015-02-02 12:55:14 -080063 SkASSERT(NULL != fInterface);
64
Jim Van Verthc1720d42017-10-09 10:16:01 -040065 fContext = GrContext::MakeGL(fInterface.get());
caryclark52edc4d2015-02-02 12:55:14 -080066 SkASSERT(NULL != fContext);
67
robertphillipsecf3dbe2016-07-28 15:17:34 -070068 this->setUpGpuBackedSurface();
caryclark52edc4d2015-02-02 12:55:14 -080069 return true;
70}
71
robertphillipsecf3dbe2016-07-28 15:17:34 -070072void HelloWorldWindow::setUpGpuBackedSurface() {
Jim Van Verthc1720d42017-10-09 10:16:01 -040073 fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface.get(), fContext.get());
caryclark52edc4d2015-02-02 12:55:14 -080074}
75
76void HelloWorldWindow::drawContents(SkCanvas* canvas) {
77 // Clear background
78 canvas->drawColor(SK_ColorWHITE);
79
80 SkPaint paint;
81 paint.setColor(SK_ColorRED);
82
83 // Draw a rectangle with red paint
reedc6f28f72016-03-14 12:22:10 -070084 SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
caryclark52edc4d2015-02-02 12:55:14 -080085 canvas->drawRect(rect, paint);
86
87 // Set up a linear gradient and draw a circle
88 {
89 SkPoint linearPoints[] = {
90 {0, 0},
91 {300, 300}
92 };
93 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
94
reedc6f28f72016-03-14 12:22:10 -070095 paint.setShader(SkGradientShader::MakeLinear(
96 linearPoints, linearColors, nullptr, 2,
97 SkShader::kMirror_TileMode));
caryclark52edc4d2015-02-02 12:55:14 -080098 paint.setFlags(SkPaint::kAntiAlias_Flag);
99
100 canvas->drawCircle(200, 200, 64, paint);
101
102 // Detach shader
reedc6f28f72016-03-14 12:22:10 -0700103 paint.setShader(nullptr);
caryclark52edc4d2015-02-02 12:55:14 -0800104 }
105
106 // Draw a message with a nice black paint.
107 paint.setFlags(
108 SkPaint::kAntiAlias_Flag |
Jim Van Verthc1720d42017-10-09 10:16:01 -0400109 SkPaint::kSubpixelText_Flag); // ... avoid waggly text when rotating.
caryclark52edc4d2015-02-02 12:55:14 -0800110 paint.setColor(SK_ColorBLACK);
111 paint.setTextSize(20);
112
113 canvas->save();
114
115 static const char message[] = "Hello World";
116
117 // Translate and rotate
118 canvas->translate(300, 300);
119 fRotationAngle += 0.2f;
120 if (fRotationAngle > 360) {
121 fRotationAngle -= 360;
122 }
123 canvas->rotate(fRotationAngle);
124
125 // Draw the text:
126 canvas->drawText(message, strlen(message), 0, 0, paint);
127
128 canvas->restore();
129}
130
131void HelloWorldWindow::draw(SkCanvas* canvas) {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700132 this->drawContents(canvas);
caryclark52edc4d2015-02-02 12:55:14 -0800133 // Invalidate the window to force a redraw. Poor man's animation mechanism.
134 this->inval(NULL);
135
136 if (kRaster_DeviceType == fType) {
Mike Reed5cf7b612016-09-29 14:12:11 -0400137 fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr);
caryclark52edc4d2015-02-02 12:55:14 -0800138 }
Mike Reed5cf7b612016-09-29 14:12:11 -0400139 fGpuSurface->getCanvas()->flush();
caryclark52edc4d2015-02-02 12:55:14 -0800140 INHERITED::present();
141}
142
143void HelloWorldWindow::onSizeChange() {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700144 this->setUpGpuBackedSurface();
caryclark52edc4d2015-02-02 12:55:14 -0800145}
146
147bool HelloWorldWindow::onHandleChar(SkUnichar unichar) {
148 if (' ' == unichar) {
149 fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceType;
150 tearDownBackend();
151 setUpBackend();
152 this->setTitle();
153 this->inval(NULL);
154 }
155 return true;
156}
157
158SkOSWindow* create_sk_window(void* hwnd, int , char** ) {
159 return new HelloWorldWindow(hwnd);
160}