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

Side by Side Diff: experimental/HelloSkia/HelloSkia.cpp

Issue 16337012: Smallest possible desktop application that uses Skia to render stuff. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: GPU support Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 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 "HelloSkia.h"
11
12 #include "SkDraw.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkUnitMappers.h"
16
17 #ifdef SK_HelloSkia_ENABLE_GPU
18 #include "gl/GrGLInterface.h"
19 #include "GrContext.h"
20 #include "SkDevice.h"
21 #include "SkGpuDevice.h"
22 #include "GrTypes.h"
23 #endif
24
25 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
26 return new HelloWindow(hwnd, argc, argv);
27 }
28
29 void application_init() {
30 SkGraphics::Init();
31 SkEvent::Init();
32 }
33
34 void application_term() {
35 SkEvent::Term();
36 SkGraphics::Term();
37 }
38
39 HelloWindow::HelloWindow(void* hWnd, int argc, char** argv)
40 : INHERITED(hWnd)
41 {
42 this->setConfig(SkBitmap::kARGB_8888_Config);
43 this->setVisibleP(true);
44 this->setClipToBounds(false);
45
46 fBGColor = SK_ColorWHITE;
47 fRotationAngle = SkIntToScalar(0);
48
49 setupBackend();
50 }
51
52 HelloWindow::~HelloWindow() {
53 #ifdef SK_HelloSkia_ENABLE_GPU
54 SkSafeUnref(fContext);
55 SkSafeUnref(fInterface);
56 SkSafeUnref(fRenderTarget);
57 #endif
58 }
59
60 void HelloWindow::setupBackend() {
61 #ifdef SK_HelloSkia_ENABLE_GPU
62 AttachmentInfo attachmentInfo;
63 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo);
64 if (false == result) {
65 SkDebugf("Not possible to create GL backend. Defaulting to raster.");
caryclark 2013/06/05 11:55:33 this should change the state of the runtime switch
sglez 2013/06/05 16:15:31 Yes, this was incomplete. Thanks.
66 }
67
68 const GrGLInterface* interface = GrGLCreateNativeInterface();
69 fInterface = const_cast<GrGLInterface*>(interface);
70 SkASSERT(NULL != interface);
71
72 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface) ;
73 SkASSERT(NULL != fContext);
74
75 GrBackendRenderTargetDesc desc;
76 desc.fWidth = SkScalarRound(width());
77 desc.fHeight = SkScalarRound(height());
78 desc.fConfig = kSkia8888_GrPixelConfig;
79 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
80 desc.fSampleCnt = attachmentInfo.fSampleCount;
81 desc.fStencilBits = attachmentInfo.fStencilBits;
82
83 GrGLint buffer;
84 interface->fGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
85 desc.fRenderTargetHandle = buffer;
86
87 fRenderTarget = fContext->wrapBackendRenderTarget(desc);
88
89 fContext->setRenderTarget(fRenderTarget);
90 #endif
91 }
92
93 SkCanvas* HelloWindow::createCanvas() {
94 #ifdef SK_HelloSkia_ENABLE_GPU
caryclark 2013/06/05 11:55:33 It looks like the only runtime decision is here (e
95 if (fContext && fRenderTarget) {
96 SkASSERT(NULL != fContext && NULL != fRenderTarget);
97 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget));
98 return new SkCanvas(device);
99 } else {
100 SkDebugf("Not using GPU canvas.\n");
101 return INHERITED::createCanvas();
102 }
103 #else
104 return INHERITED::createCanvas();
105 #endif
106 }
107
108 void HelloWindow::draw(SkCanvas* canvas) {
109 // Clear background
110 canvas->drawColor(fBGColor);
111
112 SkPaint paint;
113 paint.setColor(SK_ColorRED); // SkColor is ARGB
114
115 // Draw a rectangle with blue paint
116 SkRect rect = {10, 10, 128, 128};
117 canvas->drawRect(rect, paint);
118
119 // Set up a linear gradient and draw a circle
120 {
121 SkPoint linearPoints[] = {{0, 0}, {SkIntToScalar(300), SkIntToScalar(300 )}};
122 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
123
124 SkUnitMapper* linearMapper = new SkDiscreteMapper(100);
125 SkAutoUnref lm_deleter(linearMapper);
126
127 SkShader* shader = SkGradientShader::CreateLinear(
128 linearPoints, linearColors, NULL, 2, SkShader::kMirror_TileMode, linearMapper);
129 SkAutoUnref shader_deleter(shader);
130
131 paint.setShader(shader);
132 paint.setFlags(SkPaint::kAntiAlias_Flag);
133
134 canvas->drawCircle(200, 200, 64, paint);
135
136 // Detach shader
137 paint.setShader(NULL);
138 }
139
140
141 // Draw a message with a nice black paint.
142 paint.setFlags(
143 SkPaint::kAntiAlias_Flag |
144 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotatin g.
145 SkPaint::kUnderlineText_Flag);
146 paint.setColor(SK_ColorBLACK);
147 paint.setTextSize(SkIntToScalar(20));
148
149 canvas->save();
150
151 static const char message[] = "Hello Skia!!!";
152
153 // Translate and rotate
154 canvas->translate(300, 300);
155 fRotationAngle += SkFloatToScalar(0.2);
156 if (fRotationAngle > SkFloatToScalar(360.0)) {
157 fRotationAngle -= SkFloatToScalar(360.0);
158 }
159 canvas->rotate(fRotationAngle);
160
161 // Draw the text:
162 canvas->drawText(message, strlen(message), 0, 0, paint);
163
164 canvas->restore();
165
166 // Invalidate the window to force a redraw. Poor man's animation mechanism.
167 this->inval(NULL);
168 canvas->flush();
169 #ifdef SK_HelloSkia_ENABLE_GPU
170 // Swap buffers if we are using GL.
171 present();
172 #endif
173 }
174
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698