caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 1 | /* |
| 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" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 14 | #include "SkApplication.h" |
| 15 | #include "SkCanvas.h" |
| 16 | #include "SkGradientShader.h" |
| 17 | #include "SkGraphics.h" |
| 18 | #include "SkGr.h" |
| 19 | |
| 20 | void application_init() { |
| 21 | SkGraphics::Init(); |
| 22 | SkEvent::Init(); |
| 23 | } |
| 24 | |
| 25 | void application_term() { |
| 26 | SkEvent::Term(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | HelloWorldWindow::HelloWorldWindow(void* hwnd) |
| 30 | : INHERITED(hwnd) { |
| 31 | fType = kGPU_DeviceType; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 32 | fRotationAngle = 0; |
| 33 | this->setTitle(); |
| 34 | this->setUpBackend(); |
| 35 | } |
| 36 | |
| 37 | HelloWorldWindow::~HelloWorldWindow() { |
| 38 | tearDownBackend(); |
| 39 | } |
| 40 | |
| 41 | void HelloWorldWindow::tearDownBackend() { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 42 | INHERITED::release(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void HelloWorldWindow::setTitle() { |
| 46 | SkString title("Hello World "); |
| 47 | title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); |
| 48 | INHERITED::setTitle(title.c_str()); |
| 49 | } |
| 50 | |
| 51 | bool HelloWorldWindow::setUpBackend() { |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 52 | this->setVisibleP(true); |
| 53 | this->setClipToBounds(false); |
| 54 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 55 | bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 56 | if (false == result) { |
| 57 | SkDebugf("Not possible to create backend.\n"); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 58 | release(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
Jim Van Verth | c1720d4 | 2017-10-09 10:16:01 -0400 | [diff] [blame^] | 62 | fInterface.reset(GrGLCreateNativeInterface()); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 63 | SkASSERT(NULL != fInterface); |
| 64 | |
Jim Van Verth | c1720d4 | 2017-10-09 10:16:01 -0400 | [diff] [blame^] | 65 | fContext = GrContext::MakeGL(fInterface.get()); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 66 | SkASSERT(NULL != fContext); |
| 67 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 68 | this->setUpGpuBackedSurface(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 72 | void HelloWorldWindow::setUpGpuBackedSurface() { |
Jim Van Verth | c1720d4 | 2017-10-09 10:16:01 -0400 | [diff] [blame^] | 73 | fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface.get(), fContext.get()); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void 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 |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 84 | SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 85 | 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 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 95 | paint.setShader(SkGradientShader::MakeLinear( |
| 96 | linearPoints, linearColors, nullptr, 2, |
| 97 | SkShader::kMirror_TileMode)); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 98 | paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 99 | |
| 100 | canvas->drawCircle(200, 200, 64, paint); |
| 101 | |
| 102 | // Detach shader |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 103 | paint.setShader(nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Draw a message with a nice black paint. |
| 107 | paint.setFlags( |
| 108 | SkPaint::kAntiAlias_Flag | |
Jim Van Verth | c1720d4 | 2017-10-09 10:16:01 -0400 | [diff] [blame^] | 109 | SkPaint::kSubpixelText_Flag); // ... avoid waggly text when rotating. |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 110 | 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 | |
| 131 | void HelloWorldWindow::draw(SkCanvas* canvas) { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 132 | this->drawContents(canvas); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 133 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| 134 | this->inval(NULL); |
| 135 | |
| 136 | if (kRaster_DeviceType == fType) { |
Mike Reed | 5cf7b61 | 2016-09-29 14:12:11 -0400 | [diff] [blame] | 137 | fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 138 | } |
Mike Reed | 5cf7b61 | 2016-09-29 14:12:11 -0400 | [diff] [blame] | 139 | fGpuSurface->getCanvas()->flush(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 140 | INHERITED::present(); |
| 141 | } |
| 142 | |
| 143 | void HelloWorldWindow::onSizeChange() { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 144 | this->setUpGpuBackedSurface(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool 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 | |
| 158 | SkOSWindow* create_sk_window(void* hwnd, int , char** ) { |
| 159 | return new HelloWorldWindow(hwnd); |
| 160 | } |