| 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 "SkCanvas.h" |
| 17 #include "SkGpuDevice.h" | 15 #include "SkGradientShader.h" |
| 18 #include "SkGraphics.h" | 16 #include "SkGraphics.h" |
| 19 | 17 #include "SkGr.h" |
| 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 | 18 |
| 29 void application_init() { | 19 void application_init() { |
| 30 SkGraphics::Init(); | 20 SkGraphics::Init(); |
| 31 SkEvent::Init(); | 21 SkEvent::Init(); |
| 32 } | 22 } |
| 33 | 23 |
| 34 void application_term() { | 24 void application_term() { |
| 35 SkEvent::Term(); | 25 SkEvent::Term(); |
| 36 SkGraphics::Term(); | 26 SkGraphics::Term(); |
| 37 } | 27 } |
| 38 | 28 |
| 39 SkExampleWindow::SkExampleWindow(void* hwnd) | 29 SkExampleWindow::SkExampleWindow(void* hwnd) |
| 40 : INHERITED(hwnd) { | 30 : INHERITED(hwnd) { |
| 41 fRegistry = SkExample::Registry::Head(); | 31 fType = SkExampleWindow::kGPU_DeviceType; |
| 42 fCurrExample = fRegistry->factory()(this); | 32 fRenderTarget = NULL; |
| 33 fRotationAngle = 0; |
| 34 this->setTitle(); |
| 35 this->setUpBackend(); |
| 36 } |
| 43 | 37 |
| 44 if (FLAGS_match.count()) { | 38 SkExampleWindow::~SkExampleWindow() { |
| 45 // Start with the a matching sample if possible. | 39 tearDownBackend(); |
| 46 bool found = this->findNextMatch(); | |
| 47 if (!found) { | |
| 48 SkDebugf("No matching SkExample found.\n"); | |
| 49 } | |
| 50 } | |
| 51 } | 40 } |
| 52 | 41 |
| 53 void SkExampleWindow::tearDownBackend() { | 42 void SkExampleWindow::tearDownBackend() { |
| 54 if (kGPU_DeviceType == fType) { | 43 SkSafeUnref(fContext); |
| 55 SkSafeUnref(fContext); | 44 fContext = NULL; |
| 56 fContext = NULL; | |
| 57 | 45 |
| 58 SkSafeUnref(fInterface); | 46 SkSafeUnref(fInterface); |
| 59 fInterface = NULL; | 47 fInterface = NULL; |
| 60 | 48 |
| 61 SkSafeUnref(fRenderTarget); | 49 SkSafeUnref(fRenderTarget); |
| 62 fRenderTarget = NULL; | 50 fRenderTarget = NULL; |
| 63 | 51 |
| 64 detach(); | 52 INHERITED::detach(); |
| 65 } | |
| 66 } | 53 } |
| 67 | 54 |
| 68 bool SkExampleWindow::setupBackend(DeviceType type) { | 55 void SkExampleWindow::setTitle() { |
| 69 fType = type; | 56 SkString title("SkiaExample "); |
| 57 title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); |
| 58 INHERITED::setTitle(title.c_str()); |
| 59 } |
| 70 | 60 |
| 61 bool SkExampleWindow::setUpBackend() { |
| 71 this->setColorType(kRGBA_8888_SkColorType); | 62 this->setColorType(kRGBA_8888_SkColorType); |
| 72 this->setVisibleP(true); | 63 this->setVisibleP(true); |
| 73 this->setClipToBounds(false); | 64 this->setClipToBounds(false); |
| 74 | 65 |
| 75 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo); | 66 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo); |
| 76 if (false == result) { | 67 if (false == result) { |
| 77 SkDebugf("Not possible to create backend.\n"); | 68 SkDebugf("Not possible to create backend.\n"); |
| 78 detach(); | 69 detach(); |
| 79 return false; | 70 return false; |
| 80 } | 71 } |
| 81 | 72 |
| 82 fInterface = GrGLCreateNativeInterface(); | 73 fInterface = GrGLCreateNativeInterface(); |
| 83 | 74 |
| 84 SkASSERT(NULL != fInterface); | 75 SkASSERT(NULL != fInterface); |
| 85 | 76 |
| 86 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface
); | 77 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface
); |
| 87 SkASSERT(NULL != fContext); | 78 SkASSERT(NULL != fContext); |
| 88 | 79 |
| 89 setupRenderTarget(); | 80 this->setUpRenderTarget(); |
| 90 | |
| 91 return true; | 81 return true; |
| 92 } | 82 } |
| 93 | 83 |
| 94 void SkExampleWindow::setupRenderTarget() { | 84 void SkExampleWindow::setUpRenderTarget() { |
| 95 GrBackendRenderTargetDesc desc; | 85 SkSafeUnref(fRenderTarget); |
| 96 desc.fWidth = SkScalarRoundToInt(width()); | 86 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 } | 87 } |
| 111 | 88 |
| 112 SkCanvas* SkExampleWindow::createCanvas() { | 89 void SkExampleWindow::drawContents(SkCanvas* canvas) { |
| 113 if (fType == kGPU_DeviceType) { | 90 // Clear background |
| 114 if (NULL != fContext && NULL != fRenderTarget) { | 91 canvas->drawColor(SK_ColorWHITE); |
| 115 SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fContext, fRenderT
arget)); | 92 |
| 116 return new SkCanvas(device); | 93 SkPaint paint; |
| 117 } | 94 paint.setColor(SK_ColorRED); |
| 118 tearDownBackend(); | 95 |
| 119 setupBackend(kRaster_DeviceType); | 96 // Draw a rectangle with red paint |
| 97 SkRect rect = { |
| 98 10, 10, |
| 99 128, 128 |
| 100 }; |
| 101 canvas->drawRect(rect, paint); |
| 102 |
| 103 // Set up a linear gradient and draw a circle |
| 104 { |
| 105 SkPoint linearPoints[] = { |
| 106 {0, 0}, |
| 107 {300, 300} |
| 108 }; |
| 109 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; |
| 110 |
| 111 SkShader* shader = SkGradientShader::CreateLinear( |
| 112 linearPoints, linearColors, NULL, 2, |
| 113 SkShader::kMirror_TileMode); |
| 114 SkAutoUnref shader_deleter(shader); |
| 115 |
| 116 paint.setShader(shader); |
| 117 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 118 |
| 119 canvas->drawCircle(200, 200, 64, paint); |
| 120 |
| 121 // Detach shader |
| 122 paint.setShader(NULL); |
| 120 } | 123 } |
| 121 return INHERITED::createCanvas(); | 124 |
| 125 // Draw a message with a nice black paint. |
| 126 paint.setFlags( |
| 127 SkPaint::kAntiAlias_Flag | |
| 128 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotatin
g. |
| 129 SkPaint::kUnderlineText_Flag); |
| 130 paint.setColor(SK_ColorBLACK); |
| 131 paint.setTextSize(20); |
| 132 |
| 133 canvas->save(); |
| 134 |
| 135 static const char message[] = "Hello Skia!!!"; |
| 136 |
| 137 // Translate and rotate |
| 138 canvas->translate(300, 300); |
| 139 fRotationAngle += 0.2f; |
| 140 if (fRotationAngle > 360) { |
| 141 fRotationAngle -= 360; |
| 142 } |
| 143 canvas->rotate(fRotationAngle); |
| 144 |
| 145 // Draw the text: |
| 146 canvas->drawText(message, strlen(message), 0, 0, paint); |
| 147 |
| 148 canvas->restore(); |
| 122 } | 149 } |
| 123 | 150 |
| 124 void SkExampleWindow::draw(SkCanvas* canvas) { | 151 void SkExampleWindow::draw(SkCanvas* canvas) { |
| 125 if (NULL != fCurrExample) { | 152 drawContents(canvas); |
| 126 fCurrExample->draw(canvas); | 153 // in case we have queued drawing calls |
| 127 } | 154 fContext->flush(); |
| 128 if (fType == kGPU_DeviceType) { | 155 // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| 156 this->inval(NULL); |
| 129 | 157 |
| 130 SkASSERT(NULL != fContext); | 158 if (kRaster_DeviceType == fType) { |
| 131 fContext->flush(); | |
| 132 } | |
| 133 if (fType == kRaster_DeviceType) { | |
| 134 // need to send the raster bits to the (gpu) window | 159 // need to send the raster bits to the (gpu) window |
| 135 fContext->setRenderTarget(fRenderTarget); | 160 SkImage* snap = fSurface->newImageSnapshot(); |
| 136 const SkBitmap& bm = getBitmap(); | 161 size_t rowBytes; |
| 137 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(), | 162 SkImageInfo info; |
| 138 kSkia8888_GrPixelConfig, | 163 const void* pixels = snap->peekPixels(&info, &rowBytes); |
| 139 bm.getPixels(), | 164 fRenderTarget->writePixels(0, 0, snap->width(), snap->height(), |
| 140 bm.rowBytes()); | 165 SkImageInfo2GrPixelConfig(info.colorType
(), |
| 166 info.alphaType()
, |
| 167 info.profileType
()), |
| 168 pixels, |
| 169 rowBytes, |
| 170 GrContext::kFlushWrites_PixelOp); |
| 171 SkSafeUnref(snap); |
| 141 } | 172 } |
| 142 INHERITED::present(); | 173 INHERITED::present(); |
| 143 } | 174 } |
| 144 | 175 |
| 145 void SkExampleWindow::onSizeChange() { | 176 void SkExampleWindow::onSizeChange() { |
| 146 setupRenderTarget(); | 177 setUpRenderTarget(); |
| 147 } | |
| 148 | |
| 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() { | |
| 161 bool found = false; | |
| 162 // Avoid infinite loop by knowing where we started. | |
| 163 const SkExample::Registry* begin = fRegistry; | |
| 164 while (!found) { | |
| 165 fRegistry = fRegistry->next(); | |
| 166 if (NULL == fRegistry) { // Reached the end of the registered samples.
GOTO head. | |
| 167 fRegistry = SkExample::Registry::Head(); | |
| 168 } | |
| 169 SkExample* next = fRegistry->factory()(this); | |
| 170 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, next->getName().c_str()
)) { | |
| 171 fCurrExample = next; | |
| 172 found = true; | |
| 173 } | |
| 174 if (begin == fRegistry) { // We looped through every sample without fin
ding anything. | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 return found; | |
| 179 } | 178 } |
| 180 | 179 |
| 181 bool SkExampleWindow::onHandleChar(SkUnichar unichar) { | 180 bool SkExampleWindow::onHandleChar(SkUnichar unichar) { |
| 182 if ('n' == unichar) { | 181 if (' ' == unichar) { |
| 183 bool found = findNextMatch(); | 182 fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceTyp
e; |
| 184 if (!found) { | 183 tearDownBackend(); |
| 185 SkDebugf("No SkExample that matches your query\n"); | 184 setUpBackend(); |
| 186 } | 185 this->setTitle(); |
| 186 this->inval(NULL); |
| 187 } | 187 } |
| 188 return true; | 188 return true; |
| 189 } | 189 } |
| 190 | 190 |
| 191 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { | 191 SkOSWindow* create_sk_window(void* hwnd, int , char** ) { |
| 192 SkCommandLineFlags::Parse(argc, argv); | |
| 193 return new SkExampleWindow(hwnd); | 192 return new SkExampleWindow(hwnd); |
| 194 } | 193 } |
| OLD | NEW |