Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "BaseExample.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 BaseExample::BaseExample(void* hWnd, int argc, char** argv) | |
| 30 : INHERITED(hWnd), fDelayMs(16) {} | |
| 31 | |
| 32 void BaseExample::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 BaseExample::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."); | |
| 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 BaseExample::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* BaseExample::createCanvas() { | |
| 92 if (fType == kGPU_DeviceType && | |
|
caryclark
2013/06/17 15:08:39
note if (fType ... ) here ... but below ...
| |
| 93 NULL != fContext && NULL != fRenderTarget) { | |
| 94 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget)); | |
| 95 return new SkCanvas(device); | |
| 96 } else { | |
| 97 if (fType == kGPU_DeviceType) { | |
| 98 tearDownBackend(); | |
| 99 setupBackend(kRaster_DeviceType); | |
| 100 } | |
| 101 return INHERITED::createCanvas(); | |
| 102 } | |
|
caryclark
2013/06/17 15:08:39
I would rewrite this as:
if (fType == kGPU_Device
sglez
2013/06/17 18:04:35
The else is still needed to handle a nil context o
| |
| 103 } | |
| 104 | |
| 105 void BaseExample::draw(SkCanvas* canvas) { | |
| 106 if (getDeviceType() == kGPU_DeviceType) { | |
|
caryclark
2013/06/17 15:08:39
if (getDeviceType() ... here.
Choose one or the ot
| |
| 107 SkASSERT(NULL != fContext); | |
| 108 fContext->flush(); | |
| 109 } | |
| 110 if (kRaster_DeviceType == getDeviceType()) { | |
|
caryclark
2013/06/17 15:08:39
reverse compare order
| |
| 111 // need to send the raster bits to the (gpu) window | |
| 112 fContext->setRenderTarget(fRenderTarget); | |
| 113 const SkBitmap& bm = getBitmap(); | |
| 114 fRenderTarget->writePixels(0, 0, bm.width(), bm.height(), | |
| 115 kSkia8888_GrPixelConfig, | |
| 116 bm.getPixels(), | |
| 117 bm.rowBytes()); | |
| 118 } | |
| 119 present(); | |
| 120 } | |
| 121 | |
| 122 void BaseExample::onSizeChange() { | |
| 123 setupRenderTarget(); | |
| 124 } | |
| 125 | |
| 126 void BaseExample::setInvalDelay(SkMSec ms) { | |
| 127 fDelayMs = ms; | |
|
caryclark
2013/06/17 15:08:39
One line implementations might as well live in the
sglez
2013/06/17 18:04:35
Mike's changes make my delay fix obsolete so I'm t
| |
| 128 } | |
| 129 | |
| 130 #ifdef SK_BUILD_FOR_UNIX | |
| 131 void BaseExample::onHandleInval(const SkIRect&) { | |
| 132 (new SkEvent("inval-imageview", this->getSinkID()))->postDelay(fDelayMs); | |
| 133 } | |
| 134 #endif | |
| 135 | |
| 136 #ifdef SK_BUILD_FOR_WIN | |
| 137 void BaseExample::onHandleInval(const SkIRect& rect) { | |
| 138 Sleep(fDelayMs); | |
|
caryclark
2013/06/17 15:08:39
This suggests that one thread should be doing comp
sglez
2013/06/17 18:04:35
The intent was to mimic the Linux behavior by maki
| |
| 139 RECT winRect; | |
| 140 winRect.top = rect.top(); | |
| 141 winRect.bottom = rect.bottom(); | |
| 142 winRect.right = rect.right(); | |
| 143 winRect.left = rect.left(); | |
| 144 InvalidateRect((HWND)this->getHWND(), &winRect, false); | |
| 145 } | |
| 146 #endif | |
| OLD | NEW |