Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1422)

Side by Side Diff: example/HelloWorld.cpp

Issue 886413004: move HelloWorld to be a peer of SampleApp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: exclude android as well Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « example/HelloWorld.h ('k') | example/mac/HelloWorld.xib » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 * 7 *
8 */ 8 */
9 9
10 #include "SkExample.h" 10 #include "HelloWorld.h"
11 11
12 #include "gl/GrGLInterface.h" 12 #include "gl/GrGLInterface.h"
13 #include "SkApplication.h" 13 #include "SkApplication.h"
14 #include "SkCanvas.h" 14 #include "SkCanvas.h"
15 #include "SkGradientShader.h" 15 #include "SkGradientShader.h"
16 #include "SkGraphics.h" 16 #include "SkGraphics.h"
17 #include "SkGr.h" 17 #include "SkGr.h"
18 18
19 void application_init() { 19 void application_init() {
20 SkGraphics::Init(); 20 SkGraphics::Init();
21 SkEvent::Init(); 21 SkEvent::Init();
22 } 22 }
23 23
24 void application_term() { 24 void application_term() {
25 SkEvent::Term(); 25 SkEvent::Term();
26 SkGraphics::Term(); 26 SkGraphics::Term();
27 } 27 }
28 28
29 SkExampleWindow::SkExampleWindow(void* hwnd) 29 HelloWorldWindow::HelloWorldWindow(void* hwnd)
30 : INHERITED(hwnd) { 30 : INHERITED(hwnd) {
31 fType = SkExampleWindow::kGPU_DeviceType; 31 fType = kGPU_DeviceType;
32 fRenderTarget = NULL; 32 fRenderTarget = NULL;
33 fRotationAngle = 0; 33 fRotationAngle = 0;
34 this->setTitle(); 34 this->setTitle();
35 this->setUpBackend(); 35 this->setUpBackend();
36 } 36 }
37 37
38 SkExampleWindow::~SkExampleWindow() { 38 HelloWorldWindow::~HelloWorldWindow() {
39 tearDownBackend(); 39 tearDownBackend();
40 } 40 }
41 41
42 void SkExampleWindow::tearDownBackend() { 42 void HelloWorldWindow::tearDownBackend() {
43 SkSafeUnref(fContext); 43 SkSafeUnref(fContext);
44 fContext = NULL; 44 fContext = NULL;
45 45
46 SkSafeUnref(fInterface); 46 SkSafeUnref(fInterface);
47 fInterface = NULL; 47 fInterface = NULL;
48 48
49 SkSafeUnref(fRenderTarget); 49 SkSafeUnref(fRenderTarget);
50 fRenderTarget = NULL; 50 fRenderTarget = NULL;
51 51
52 INHERITED::detach(); 52 INHERITED::detach();
53 } 53 }
54 54
55 void SkExampleWindow::setTitle() { 55 void HelloWorldWindow::setTitle() {
56 SkString title("SkiaExample "); 56 SkString title("Hello World ");
57 title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); 57 title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl");
58 INHERITED::setTitle(title.c_str()); 58 INHERITED::setTitle(title.c_str());
59 } 59 }
60 60
61 bool SkExampleWindow::setUpBackend() { 61 bool HelloWorldWindow::setUpBackend() {
62 this->setColorType(kRGBA_8888_SkColorType); 62 this->setColorType(kRGBA_8888_SkColorType);
63 this->setVisibleP(true); 63 this->setVisibleP(true);
64 this->setClipToBounds(false); 64 this->setClipToBounds(false);
65 65
66 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo); 66 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo);
67 if (false == result) { 67 if (false == result) {
68 SkDebugf("Not possible to create backend.\n"); 68 SkDebugf("Not possible to create backend.\n");
69 detach(); 69 detach();
70 return false; 70 return false;
71 } 71 }
72 72
73 fInterface = GrGLCreateNativeInterface(); 73 fInterface = GrGLCreateNativeInterface();
74 74
75 SkASSERT(NULL != fInterface); 75 SkASSERT(NULL != fInterface);
76 76
77 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface ); 77 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface );
78 SkASSERT(NULL != fContext); 78 SkASSERT(NULL != fContext);
79 79
80 this->setUpRenderTarget(); 80 this->setUpRenderTarget();
81 return true; 81 return true;
82 } 82 }
83 83
84 void SkExampleWindow::setUpRenderTarget() { 84 void HelloWorldWindow::setUpRenderTarget() {
85 SkSafeUnref(fRenderTarget); 85 SkSafeUnref(fRenderTarget);
86 fRenderTarget = this->renderTarget(fAttachmentInfo, fInterface, fContext); 86 fRenderTarget = this->renderTarget(fAttachmentInfo, fInterface, fContext);
87 } 87 }
88 88
89 void SkExampleWindow::drawContents(SkCanvas* canvas) { 89 void HelloWorldWindow::drawContents(SkCanvas* canvas) {
90 // Clear background 90 // Clear background
91 canvas->drawColor(SK_ColorWHITE); 91 canvas->drawColor(SK_ColorWHITE);
92 92
93 SkPaint paint; 93 SkPaint paint;
94 paint.setColor(SK_ColorRED); 94 paint.setColor(SK_ColorRED);
95 95
96 // Draw a rectangle with red paint 96 // Draw a rectangle with red paint
97 SkRect rect = { 97 SkRect rect = {
98 10, 10, 98 10, 10,
99 128, 128 99 128, 128
(...skipping 25 matching lines...) Expand all
125 // Draw a message with a nice black paint. 125 // Draw a message with a nice black paint.
126 paint.setFlags( 126 paint.setFlags(
127 SkPaint::kAntiAlias_Flag | 127 SkPaint::kAntiAlias_Flag |
128 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotatin g. 128 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotatin g.
129 SkPaint::kUnderlineText_Flag); 129 SkPaint::kUnderlineText_Flag);
130 paint.setColor(SK_ColorBLACK); 130 paint.setColor(SK_ColorBLACK);
131 paint.setTextSize(20); 131 paint.setTextSize(20);
132 132
133 canvas->save(); 133 canvas->save();
134 134
135 static const char message[] = "Hello Skia!!!"; 135 static const char message[] = "Hello World";
136 136
137 // Translate and rotate 137 // Translate and rotate
138 canvas->translate(300, 300); 138 canvas->translate(300, 300);
139 fRotationAngle += 0.2f; 139 fRotationAngle += 0.2f;
140 if (fRotationAngle > 360) { 140 if (fRotationAngle > 360) {
141 fRotationAngle -= 360; 141 fRotationAngle -= 360;
142 } 142 }
143 canvas->rotate(fRotationAngle); 143 canvas->rotate(fRotationAngle);
144 144
145 // Draw the text: 145 // Draw the text:
146 canvas->drawText(message, strlen(message), 0, 0, paint); 146 canvas->drawText(message, strlen(message), 0, 0, paint);
147 147
148 canvas->restore(); 148 canvas->restore();
149 } 149 }
150 150
151 void SkExampleWindow::draw(SkCanvas* canvas) { 151 void HelloWorldWindow::draw(SkCanvas* canvas) {
152 drawContents(canvas); 152 drawContents(canvas);
153 // in case we have queued drawing calls 153 // in case we have queued drawing calls
154 fContext->flush(); 154 fContext->flush();
155 // Invalidate the window to force a redraw. Poor man's animation mechanism. 155 // Invalidate the window to force a redraw. Poor man's animation mechanism.
156 this->inval(NULL); 156 this->inval(NULL);
157 157
158 if (kRaster_DeviceType == fType) { 158 if (kRaster_DeviceType == fType) {
159 // need to send the raster bits to the (gpu) window 159 // need to send the raster bits to the (gpu) window
160 SkImage* snap = fSurface->newImageSnapshot(); 160 SkImage* snap = fSurface->newImageSnapshot();
161 size_t rowBytes; 161 size_t rowBytes;
162 SkImageInfo info; 162 SkImageInfo info;
163 const void* pixels = snap->peekPixels(&info, &rowBytes); 163 const void* pixels = snap->peekPixels(&info, &rowBytes);
164 fRenderTarget->writePixels(0, 0, snap->width(), snap->height(), 164 fRenderTarget->writePixels(0, 0, snap->width(), snap->height(),
165 SkImageInfo2GrPixelConfig(info.colorType (), 165 SkImageInfo2GrPixelConfig(info.colorType (),
166 info.alphaType() , 166 info.alphaType() ,
167 info.profileType ()), 167 info.profileType ()),
168 pixels, 168 pixels,
169 rowBytes, 169 rowBytes,
170 GrContext::kFlushWrites_PixelOp); 170 GrContext::kFlushWrites_PixelOp);
171 SkSafeUnref(snap); 171 SkSafeUnref(snap);
172 } 172 }
173 INHERITED::present(); 173 INHERITED::present();
174 } 174 }
175 175
176 void SkExampleWindow::onSizeChange() { 176 void HelloWorldWindow::onSizeChange() {
177 setUpRenderTarget(); 177 setUpRenderTarget();
178 } 178 }
179 179
180 bool SkExampleWindow::onHandleChar(SkUnichar unichar) { 180 bool HelloWorldWindow::onHandleChar(SkUnichar unichar) {
181 if (' ' == unichar) { 181 if (' ' == unichar) {
182 fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceTyp e; 182 fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceTyp e;
183 tearDownBackend(); 183 tearDownBackend();
184 setUpBackend(); 184 setUpBackend();
185 this->setTitle(); 185 this->setTitle();
186 this->inval(NULL); 186 this->inval(NULL);
187 } 187 }
188 return true; 188 return true;
189 } 189 }
190 190
191 SkOSWindow* create_sk_window(void* hwnd, int , char** ) { 191 SkOSWindow* create_sk_window(void* hwnd, int , char** ) {
192 return new SkExampleWindow(hwnd); 192 return new HelloWorldWindow(hwnd);
193 } 193 }
OLDNEW
« no previous file with comments | « example/HelloWorld.h ('k') | example/mac/HelloWorld.xib » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698