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

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

Issue 18574002: SkiaExamples improvements. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Support for multiple SkExamples 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
« no previous file with comments | « experimental/SkiaExamples/SkExample.h ('k') | gyp/SkiaExamples.gyp » ('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 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 11
12 #include "gl/GrGLUtil.h" 12 #include "gl/GrGLUtil.h"
13 #include "gl/GrGLDefines.h" 13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLInterface.h" 14 #include "gl/GrGLInterface.h"
15 #include "SkApplication.h" 15 #include "SkApplication.h"
16 #include "SkCommandLineFlags.h"
16 #include "SkGpuDevice.h" 17 #include "SkGpuDevice.h"
17 #include "SkGraphics.h" 18 #include "SkGraphics.h"
18 19
20 DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \
21 "Multiple matches may be separated by spaces.\n" \
22 "~ causes a matching test to always be skipped\n" \
23 "^ requires the start of the test to match\n" \
24 "$ requires the end of the test to match\n" \
25 "^ and $ requires an exact match\n" \
26 "If a test does not match any list entry,\n" \
27 "it is skipped unless some list entry starts with ~");
28
19 void application_init() { 29 void application_init() {
20 SkGraphics::Init(); 30 SkGraphics::Init();
21 SkEvent::Init(); 31 SkEvent::Init();
22 } 32 }
23 33
24 void application_term() { 34 void application_term() {
25 SkEvent::Term(); 35 SkEvent::Term();
26 SkGraphics::Term(); 36 SkGraphics::Term();
27 } 37 }
28 38
29 BaseExample::BaseExample(void* hWnd, int argc, char** argv) 39 SkExampleWindow::SkExampleWindow(void* hwnd)
30 : INHERITED(hWnd) {} 40 : INHERITED(hwnd) {
41 fRegistry = SkExample::Registry::Head();
42 fCurrExample = fRegistry->factory()(this);
31 43
32 void BaseExample::tearDownBackend() { 44 if (FLAGS_match.count()) {
45 for(int i = 0; i < FLAGS_match.count(); ++i) {
46 fMatchStrs.push(FLAGS_match[i]);
47 }
48 // Start with the a matching sample if possible.
49 bool found = this->findNextMatch();
50 if (!found) {
51 SkDebugf("No matching SkExample found.\n");
52 }
53 }
54 }
55
56 void SkExampleWindow::tearDownBackend() {
33 if (kGPU_DeviceType == fType) { 57 if (kGPU_DeviceType == fType) {
34 SkSafeUnref(fContext); 58 SkSafeUnref(fContext);
35 fContext = NULL; 59 fContext = NULL;
36 60
37 SkSafeUnref(fInterface); 61 SkSafeUnref(fInterface);
38 fInterface = NULL; 62 fInterface = NULL;
39 63
40 SkSafeUnref(fRenderTarget); 64 SkSafeUnref(fRenderTarget);
41 fRenderTarget = NULL; 65 fRenderTarget = NULL;
42 66
43 detach(); 67 detach();
44 } 68 }
45 } 69 }
46 70
47 bool BaseExample::setupBackend(DeviceType type) { 71 bool SkExampleWindow::setupBackend(DeviceType type) {
48 fType = type; 72 fType = type;
49 73
50 this->setConfig(SkBitmap::kARGB_8888_Config); 74 this->setConfig(SkBitmap::kARGB_8888_Config);
51 this->setVisibleP(true); 75 this->setVisibleP(true);
52 this->setClipToBounds(false); 76 this->setClipToBounds(false);
53 77
54 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo); 78 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo);
55 if (false == result) { 79 if (false == result) {
56 SkDebugf("Not possible to create backend.\n"); 80 SkDebugf("Not possible to create backend.\n");
57 detach(); 81 detach();
58 return false; 82 return false;
59 } 83 }
60 84
61 fInterface = GrGLCreateNativeInterface(); 85 fInterface = GrGLCreateNativeInterface();
62 86
63 SkASSERT(NULL != fInterface); 87 SkASSERT(NULL != fInterface);
64 88
65 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface ); 89 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface );
66 SkASSERT(NULL != fContext); 90 SkASSERT(NULL != fContext);
67 91
68 setupRenderTarget(); 92 setupRenderTarget();
69 93
70 return true; 94 return true;
71 } 95 }
72 96
73 void BaseExample::setupRenderTarget() { 97 void SkExampleWindow::setupRenderTarget() {
74 GrBackendRenderTargetDesc desc; 98 GrBackendRenderTargetDesc desc;
75 desc.fWidth = SkScalarRound(width()); 99 desc.fWidth = SkScalarRound(width());
76 desc.fHeight = SkScalarRound(height()); 100 desc.fHeight = SkScalarRound(height());
77 desc.fConfig = kSkia8888_GrPixelConfig; 101 desc.fConfig = kSkia8888_GrPixelConfig;
78 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; 102 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
79 desc.fSampleCnt = fAttachmentInfo.fSampleCount; 103 desc.fSampleCnt = fAttachmentInfo.fSampleCount;
80 desc.fStencilBits = fAttachmentInfo.fStencilBits; 104 desc.fStencilBits = fAttachmentInfo.fStencilBits;
81 105
82 GrGLint buffer; 106 GrGLint buffer;
83 GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer); 107 GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
84 desc.fRenderTargetHandle = buffer; 108 desc.fRenderTargetHandle = buffer;
85 109
86 fRenderTarget = fContext->wrapBackendRenderTarget(desc); 110 fRenderTarget = fContext->wrapBackendRenderTarget(desc);
87 111
88 fContext->setRenderTarget(fRenderTarget); 112 fContext->setRenderTarget(fRenderTarget);
89 } 113 }
90 114
91 SkCanvas* BaseExample::createCanvas() { 115 SkCanvas* SkExampleWindow::createCanvas() {
92 if (fType == kGPU_DeviceType) { 116 if (fType == kGPU_DeviceType) {
93 if (NULL != fContext && NULL != fRenderTarget) { 117 if (NULL != fContext && NULL != fRenderTarget) {
94 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarge t)); 118 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarge t));
95 return new SkCanvas(device); 119 return new SkCanvas(device);
96 } 120 }
97 tearDownBackend(); 121 tearDownBackend();
98 setupBackend(kRaster_DeviceType); 122 setupBackend(kRaster_DeviceType);
99 } 123 }
100 return INHERITED::createCanvas(); 124 return INHERITED::createCanvas();
101 } 125 }
102 126
103 void BaseExample::draw(SkCanvas* canvas) { 127 void SkExampleWindow::draw(SkCanvas* canvas) {
128 if (NULL != fCurrExample) {
129 fCurrExample->draw(canvas);
130 }
104 if (fType == kGPU_DeviceType) { 131 if (fType == kGPU_DeviceType) {
105 132
106 SkASSERT(NULL != fContext); 133 SkASSERT(NULL != fContext);
107 fContext->flush(); 134 fContext->flush();
108 } 135 }
109 if (fType == kRaster_DeviceType) { 136 if (fType == kRaster_DeviceType) {
110 // need to send the raster bits to the (gpu) window 137 // need to send the raster bits to the (gpu) window
111 fContext->setRenderTarget(fRenderTarget); 138 fContext->setRenderTarget(fRenderTarget);
112 const SkBitmap& bm = getBitmap(); 139 const SkBitmap& bm = getBitmap();
113 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(), 140 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
114 kSkia8888_GrPixelConfig, 141 kSkia8888_GrPixelConfig,
115 bm.getPixels(), 142 bm.getPixels(),
116 bm.rowBytes()); 143 bm.rowBytes());
117 } 144 }
118 INHERITED::present(); 145 INHERITED::present();
119 } 146 }
120 147
121 void BaseExample::onSizeChange() { 148 void SkExampleWindow::onSizeChange() {
122 setupRenderTarget(); 149 setupRenderTarget();
123 } 150 }
124 151
125 #ifdef SK_BUILD_FOR_WIN 152 #ifdef SK_BUILD_FOR_WIN
126 void BaseExample::onHandleInval(const SkIRect& rect) { 153 void SkExampleWindow::onHandleInval(const SkIRect& rect) {
127 RECT winRect; 154 RECT winRect;
128 winRect.top = rect.top(); 155 winRect.top = rect.top();
129 winRect.bottom = rect.bottom(); 156 winRect.bottom = rect.bottom();
130 winRect.right = rect.right(); 157 winRect.right = rect.right();
131 winRect.left = rect.left(); 158 winRect.left = rect.left();
132 InvalidateRect((HWND)this->getHWND(), &winRect, false); 159 InvalidateRect((HWND)this->getHWND(), &winRect, false);
133 } 160 }
134 #endif 161 #endif
162
163 bool SkExampleWindow::findNextMatch() {
164 bool found = false;
165 // Avoid infinite loop by knowing where we started.
166 const SkExample::Registry* begin = fRegistry;
167 while (!found) {
168 fRegistry = fRegistry->next();
169 if (NULL == fRegistry) { // Reached the end of the registered samples. GOTO head.
170 fRegistry = SkExample::Registry::Head();
171 }
172 SkExample* next = fRegistry->factory()(this);
173 if (!SkCommandLineFlags::ShouldSkip(fMatchStrs, next->getName().c_str()) ) {
174 fCurrExample = next;
175 found = true;
176 }
177 if (begin == fRegistry) { // We looped through every sample without fin ding anything.
178 break;
179 }
180 }
181 return found;
182 }
183
184 bool SkExampleWindow::onHandleChar(SkUnichar unichar) {
185 if ('n' == unichar) {
186 bool found = findNextMatch();
187 if (!found) {
188 SkDebugf("No SkExample that matches your query\n");
189 }
190 }
191 return true;
192 }
193
194 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
195 SkCommandLineFlags::Parse(argc, argv);
196 return new SkExampleWindow(hwnd);
197 }
OLDNEW
« no previous file with comments | « experimental/SkiaExamples/SkExample.h ('k') | gyp/SkiaExamples.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698