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