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

Side by Side Diff: experimental/SkiaExamples/SkExample.cpp

Issue 18574002: SkiaExamples improvements. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Make diffs usable Created 7 years, 5 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
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "BaseExample.h" 10 #include "SkExample.h"
11
12 #include "gl/GrGLUtil.h"
13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLInterface.h"
15 #include "SkApplication.h"
16 #include "SkGpuDevice.h"
17 #include "SkGraphics.h"
18
19 void application_init() {
20 SkGraphics::Init();
21 SkEvent::Init();
22 }
23
24 void application_term() {
25 SkEvent::Term();
26 SkGraphics::Term();
27 }
28
29 SkExample::SkExample(void* hWnd, int argc, char** argv)
30 : INHERITED(hWnd) {}
31
32 void SkExample::tearDownBackend() {
33 if (kGPU_DeviceType == fType) {
34 SkSafeUnref(fContext);
35 fContext = NULL;
36
37 SkSafeUnref(fInterface);
38 fInterface = NULL;
39
40 SkSafeUnref(fRenderTarget);
41 fRenderTarget = NULL;
42
43 detach();
44 }
45 }
46
47 bool SkExample::setupBackend(DeviceType type) {
48 fType = type;
49
50 this->setConfig(SkBitmap::kARGB_8888_Config);
51 this->setVisibleP(true);
52 this->setClipToBounds(false);
53
54 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo);
55 if (false == result) {
56 SkDebugf("Not possible to create backend.\n");
57 detach();
58 return false;
59 }
60
61 fInterface = GrGLCreateNativeInterface();
62
63 SkASSERT(NULL != fInterface);
64
65 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface );
66 SkASSERT(NULL != fContext);
67
68 setupRenderTarget();
69
70 return true;
71 }
72
73 void SkExample::setupRenderTarget() {
74 GrBackendRenderTargetDesc desc;
75 desc.fWidth = SkScalarRound(width());
76 desc.fHeight = SkScalarRound(height());
77 desc.fConfig = kSkia8888_GrPixelConfig;
78 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
79 desc.fSampleCnt = fAttachmentInfo.fSampleCount;
80 desc.fStencilBits = fAttachmentInfo.fStencilBits;
81
82 GrGLint buffer;
83 GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
84 desc.fRenderTargetHandle = buffer;
85
86 fRenderTarget = fContext->wrapBackendRenderTarget(desc);
87
88 fContext->setRenderTarget(fRenderTarget);
89 }
90
91 SkCanvas* SkExample::createCanvas() {
92 if (fType == kGPU_DeviceType) {
93 if (NULL != fContext && NULL != fRenderTarget) {
94 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarge t));
95 return new SkCanvas(device);
96 }
97 tearDownBackend();
98 setupBackend(kRaster_DeviceType);
99 }
100 return INHERITED::createCanvas();
101 }
102
103 void SkExample::draw(SkCanvas* canvas) {
104 if (fType == kGPU_DeviceType) {
105
106 SkASSERT(NULL != fContext);
107 fContext->flush();
108 }
109 if (fType == kRaster_DeviceType) {
110 // need to send the raster bits to the (gpu) window
111 fContext->setRenderTarget(fRenderTarget);
112 const SkBitmap& bm = getBitmap();
113 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
114 kSkia8888_GrPixelConfig,
115 bm.getPixels(),
116 bm.rowBytes());
117 }
118 INHERITED::present();
119 }
120
121 void SkExample::onSizeChange() {
122 setupRenderTarget();
123 }
124
125 #ifdef SK_BUILD_FOR_WIN
126 void SkExample::onHandleInval(const SkIRect& rect) {
127 RECT winRect;
128 winRect.top = rect.top();
129 winRect.bottom = rect.bottom();
130 winRect.right = rect.right();
131 winRect.left = rect.left();
132 InvalidateRect((HWND)this->getHWND(), &winRect, false);
133 }
134 #endif
135
136 static SkOSWindow* dummy_factory(void* hwnd, int argc, char** argv) {
137 return NULL;
138 }
139 SkOSWindow* (*SkExample::create_user_example) (void* hwnd, int argc, char** argv ) = &dummy_factory;
140 bool SkExample::userExampleExists = false;
sglez 2013/07/03 15:53:25 Lines 136 to 140 have changes from BaseExample.cp
141
142 ////////////////////////////////////////////////////////////////////////////////
143 // Define a default "Hello World" Skia Example:
11 144
sglez 2013/07/03 15:53:25 Removed HelloWorldSkia.cpp and inserted its conten
12 #include "SkApplication.h" 145 #include "SkApplication.h"
13 #include "SkDraw.h" 146 #include "SkDraw.h"
14 #include "SkGradientShader.h" 147 #include "SkGradientShader.h"
15 #include "SkGraphics.h" 148 #include "SkGraphics.h"
16 #include "SkUnitMappers.h" 149 #include "SkUnitMappers.h"
17 150
18 class HelloSkia : public BaseExample { 151 class HelloSkia : public SkExample {
19 public: 152 public:
20 HelloSkia(void* hWnd, int argc, char** argv) 153 HelloSkia(void* hWnd, int argc, char** argv)
21 : INHERITED(hWnd, argc, argv) 154 : INHERITED(hWnd, argc, argv)
22 { 155 {
23 fBGColor = SK_ColorWHITE; 156 fBGColor = SK_ColorWHITE;
24 fRotationAngle = SkIntToScalar(0); 157 fRotationAngle = SkIntToScalar(0);
25 158
26 setupBackend(kGPU_DeviceType); 159 setupBackend(kGPU_DeviceType);
27 // Another option is software rendering: 160 // Another option is software rendering:
28 // setupBackend(kRaster_DeviceType); 161 // setupBackend(kRaster_DeviceType);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 paint.setShader(shader); 195 paint.setShader(shader);
63 paint.setFlags(SkPaint::kAntiAlias_Flag); 196 paint.setFlags(SkPaint::kAntiAlias_Flag);
64 197
65 canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200), 198 canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200),
66 SkIntToScalar(64), paint); 199 SkIntToScalar(64), paint);
67 200
68 // Detach shader 201 // Detach shader
69 paint.setShader(NULL); 202 paint.setShader(NULL);
70 } 203 }
71 204
72
73 // Draw a message with a nice black paint. 205 // Draw a message with a nice black paint.
74 paint.setFlags( 206 paint.setFlags(
75 SkPaint::kAntiAlias_Flag | 207 SkPaint::kAntiAlias_Flag |
76 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating. 208 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating.
77 SkPaint::kUnderlineText_Flag); 209 SkPaint::kUnderlineText_Flag);
78 paint.setColor(SK_ColorBLACK); 210 paint.setColor(SK_ColorBLACK);
79 paint.setTextSize(SkIntToScalar(20)); 211 paint.setTextSize(SkIntToScalar(20));
80 212
81 canvas->save(); 213 canvas->save();
82 214
(...skipping 14 matching lines...) Expand all
97 229
98 // Invalidate the window to force a redraw. Poor man's animation mec hanism. 230 // Invalidate the window to force a redraw. Poor man's animation mec hanism.
99 this->inval(NULL); 231 this->inval(NULL);
100 232
101 INHERITED::draw(canvas); 233 INHERITED::draw(canvas);
102 } 234 }
103 235
104 private: 236 private:
105 SkScalar fRotationAngle; 237 SkScalar fRotationAngle;
106 SkColor fBGColor; 238 SkColor fBGColor;
107 typedef BaseExample INHERITED; 239 typedef SkExample INHERITED;
108 }; 240 };
109 241
242 ////////////////////////////////////////////////////////////////////////////////
243 //
110 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { 244 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
245 SkOSWindow* userExample = SkExample::create_user_example(hwnd, argc, argv);
caryclark 2013/07/08 12:42:35 (*SkExample::create_user_example)(hwnd, ... (and
246 if (NULL == userExample) {
247 SkDebugf("No user example registered. Using default example.\n");
248 } else {
249 return userExample;
250 }
111 return new HelloSkia(hwnd, argc, argv); 251 return new HelloSkia(hwnd, argc, argv);
112 } 252 }
253
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698