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/GrGLInterface.h" |
| 13 #include "SkGpuDevice.h" |
| 14 |
| 15 BaseExample::BaseExample(void* hWnd, int argc, char** argv) |
| 16 : INHERITED(hWnd) |
| 17 { |
| 18 this->setConfig(SkBitmap::kARGB_8888_Config); |
| 19 this->setVisibleP(true); |
| 20 this->setClipToBounds(false); |
| 21 |
| 22 // Software rendering by default |
| 23 fType = kRaster_DeviceType; |
| 24 |
| 25 setupBackend(fType); |
| 26 } |
| 27 |
| 28 BaseExample::~BaseExample() { |
| 29 if (fType == kGPU_DeviceType) { |
| 30 SkSafeUnref(fContext); |
| 31 SkSafeUnref(fInterface); |
| 32 SkSafeUnref(fRenderTarget); |
| 33 } |
| 34 } |
| 35 |
| 36 void BaseExample::setupBackend(DeviceType type) { |
| 37 fType = type; |
| 38 if(type == kGPU_DeviceType) { |
| 39 AttachmentInfo attachmentInfo; |
| 40 bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo)
; |
| 41 |
| 42 if (false == result) { |
| 43 SkDebugf("Not possible to create GL backend. Defaulting to raster.")
; |
| 44 fType = kRaster_DeviceType; |
| 45 detach(); |
| 46 return; |
| 47 } |
| 48 |
| 49 const GrGLInterface* interface = GrGLCreateNativeInterface(); |
| 50 fInterface = const_cast<GrGLInterface*>(interface); |
| 51 SkASSERT(NULL != interface); |
| 52 |
| 53 fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interf
ace); |
| 54 SkASSERT(NULL != fContext); |
| 55 |
| 56 GrBackendRenderTargetDesc desc; |
| 57 desc.fWidth = SkScalarRound(width()); |
| 58 desc.fHeight = SkScalarRound(height()); |
| 59 desc.fConfig = kSkia8888_GrPixelConfig; |
| 60 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 61 desc.fSampleCnt = attachmentInfo.fSampleCount; |
| 62 desc.fStencilBits = attachmentInfo.fStencilBits; |
| 63 |
| 64 GrGLint buffer; |
| 65 interface->fGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); |
| 66 desc.fRenderTargetHandle = buffer; |
| 67 |
| 68 fRenderTarget = fContext->wrapBackendRenderTarget(desc); |
| 69 |
| 70 fContext->setRenderTarget(fRenderTarget); |
| 71 } |
| 72 } |
| 73 |
| 74 SkCanvas* BaseExample::createCanvas() { |
| 75 if (fType == kGPU_DeviceType && |
| 76 NULL != fContext && NULL != fRenderTarget) { |
| 77 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget)); |
| 78 return new SkCanvas(device); |
| 79 } else { |
| 80 return INHERITED::createCanvas(); |
| 81 } |
| 82 } |
| 83 |
| 84 void BaseExample::draw(SkCanvas* canvas) { |
| 85 if (getDeviceType() == kGPU_DeviceType) { |
| 86 present(); |
| 87 } |
| 88 } |
| 89 |
OLD | NEW |