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() { |
| 42 | SkSafeUnref(fContext); |
| 43 | fContext = NULL; |
| 44 | |
| 45 | SkSafeUnref(fInterface); |
| 46 | fInterface = NULL; |
| 47 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 48 | fGpuSurface = nullptr; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 49 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 50 | INHERITED::release(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void HelloWorldWindow::setTitle() { |
| 54 | SkString title("Hello World "); |
| 55 | title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); |
| 56 | INHERITED::setTitle(title.c_str()); |
| 57 | } |
| 58 | |
| 59 | bool HelloWorldWindow::setUpBackend() { |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 60 | this->setVisibleP(true); |
| 61 | this->setClipToBounds(false); |
| 62 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 63 | bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 64 | if (false == result) { |
| 65 | SkDebugf("Not possible to create backend.\n"); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 66 | release(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | fInterface = GrGLCreateNativeInterface(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 71 | SkASSERT(NULL != fInterface); |
| 72 | |
| 73 | fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface); |
| 74 | SkASSERT(NULL != fContext); |
| 75 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 76 | this->setUpGpuBackedSurface(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 80 | void HelloWorldWindow::setUpGpuBackedSurface() { |
| 81 | fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface, fContext); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void HelloWorldWindow::drawContents(SkCanvas* canvas) { |
| 85 | // Clear background |
| 86 | canvas->drawColor(SK_ColorWHITE); |
| 87 | |
| 88 | SkPaint paint; |
| 89 | paint.setColor(SK_ColorRED); |
| 90 | |
| 91 | // Draw a rectangle with red paint |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 92 | SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 93 | canvas->drawRect(rect, paint); |
| 94 | |
| 95 | // Set up a linear gradient and draw a circle |
| 96 | { |
| 97 | SkPoint linearPoints[] = { |
| 98 | {0, 0}, |
| 99 | {300, 300} |
| 100 | }; |
| 101 | SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; |
| 102 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 103 | paint.setShader(SkGradientShader::MakeLinear( |
| 104 | linearPoints, linearColors, nullptr, 2, |
| 105 | SkShader::kMirror_TileMode)); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 106 | paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 107 | |
| 108 | canvas->drawCircle(200, 200, 64, paint); |
| 109 | |
| 110 | // Detach shader |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 111 | paint.setShader(nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Draw a message with a nice black paint. |
| 115 | paint.setFlags( |
| 116 | SkPaint::kAntiAlias_Flag | |
| 117 | SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating. |
| 118 | SkPaint::kUnderlineText_Flag); |
| 119 | paint.setColor(SK_ColorBLACK); |
| 120 | paint.setTextSize(20); |
| 121 | |
| 122 | canvas->save(); |
| 123 | |
| 124 | static const char message[] = "Hello World"; |
| 125 | |
| 126 | // Translate and rotate |
| 127 | canvas->translate(300, 300); |
| 128 | fRotationAngle += 0.2f; |
| 129 | if (fRotationAngle > 360) { |
| 130 | fRotationAngle -= 360; |
| 131 | } |
| 132 | canvas->rotate(fRotationAngle); |
| 133 | |
| 134 | // Draw the text: |
| 135 | canvas->drawText(message, strlen(message), 0, 0, paint); |
| 136 | |
| 137 | canvas->restore(); |
| 138 | } |
| 139 | |
| 140 | void HelloWorldWindow::draw(SkCanvas* canvas) { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 141 | this->drawContents(canvas); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 142 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| 143 | this->inval(NULL); |
| 144 | |
| 145 | if (kRaster_DeviceType == fType) { |
Mike Reed | 5cf7b61 | 2016-09-29 14:12:11 -0400 | [diff] [blame] | 146 | fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 147 | } |
Mike Reed | 5cf7b61 | 2016-09-29 14:12:11 -0400 | [diff] [blame] | 148 | fGpuSurface->getCanvas()->flush(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 149 | INHERITED::present(); |
| 150 | } |
| 151 | |
| 152 | void HelloWorldWindow::onSizeChange() { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 153 | this->setUpGpuBackedSurface(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool HelloWorldWindow::onHandleChar(SkUnichar unichar) { |
| 157 | if (' ' == unichar) { |
| 158 | fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceType; |
| 159 | tearDownBackend(); |
| 160 | setUpBackend(); |
| 161 | this->setTitle(); |
| 162 | this->inval(NULL); |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | SkOSWindow* create_sk_window(void* hwnd, int , char** ) { |
| 168 | return new HelloWorldWindow(hwnd); |
| 169 | } |