| commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame^] | 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) {} |
| 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.\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 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) { |
| 93 | if (NULL != fContext && NULL != fRenderTarget) { |
| 94 | SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget)); |
| 95 | return new SkCanvas(device); |
| 96 | } |
| 97 | tearDownBackend(); |
| 98 | setupBackend(kRaster_DeviceType); |
| 99 | } |
| 100 | return INHERITED::createCanvas(); |
| 101 | } |
| 102 | |
| 103 | void BaseExample::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 BaseExample::onSizeChange() { |
| 122 | setupRenderTarget(); |
| 123 | } |
| 124 | |
| 125 | #ifdef SK_BUILD_FOR_WIN |
| 126 | void BaseExample::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 | |