| Index: experimental/SkiaExamples/BaseExample.cpp
|
| diff --git a/experimental/SkiaExamples/BaseExample.cpp b/experimental/SkiaExamples/BaseExample.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0bc8e800d5c2ee0506da8044f82edbf363e7bfbf
|
| --- /dev/null
|
| +++ b/experimental/SkiaExamples/BaseExample.cpp
|
| @@ -0,0 +1,89 @@
|
| +/*
|
| + * Copyright 2013 Google Inc.
|
| + *
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + *
|
| + */
|
| +
|
| +#include "BaseExample.h"
|
| +
|
| +#include "gl/GrGLInterface.h"
|
| +#include "SkGpuDevice.h"
|
| +
|
| +BaseExample::BaseExample(void* hWnd, int argc, char** argv)
|
| + : INHERITED(hWnd)
|
| +{
|
| + this->setConfig(SkBitmap::kARGB_8888_Config);
|
| + this->setVisibleP(true);
|
| + this->setClipToBounds(false);
|
| +
|
| + // Software rendering by default
|
| + fType = kRaster_DeviceType;
|
| +
|
| + setupBackend(fType);
|
| +}
|
| +
|
| +BaseExample::~BaseExample() {
|
| + if (fType == kGPU_DeviceType) {
|
| + SkSafeUnref(fContext);
|
| + SkSafeUnref(fInterface);
|
| + SkSafeUnref(fRenderTarget);
|
| + }
|
| +}
|
| +
|
| +void BaseExample::setupBackend(DeviceType type) {
|
| + fType = type;
|
| + if(type == kGPU_DeviceType) {
|
| + AttachmentInfo attachmentInfo;
|
| + bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo);
|
| +
|
| + if (false == result) {
|
| + SkDebugf("Not possible to create GL backend. Defaulting to raster.");
|
| + fType = kRaster_DeviceType;
|
| + detach();
|
| + return;
|
| + }
|
| +
|
| + const GrGLInterface* interface = GrGLCreateNativeInterface();
|
| + fInterface = const_cast<GrGLInterface*>(interface);
|
| + SkASSERT(NULL != interface);
|
| +
|
| + fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface);
|
| + SkASSERT(NULL != fContext);
|
| +
|
| + GrBackendRenderTargetDesc desc;
|
| + desc.fWidth = SkScalarRound(width());
|
| + desc.fHeight = SkScalarRound(height());
|
| + desc.fConfig = kSkia8888_GrPixelConfig;
|
| + desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
| + desc.fSampleCnt = attachmentInfo.fSampleCount;
|
| + desc.fStencilBits = attachmentInfo.fStencilBits;
|
| +
|
| + GrGLint buffer;
|
| + interface->fGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
|
| + desc.fRenderTargetHandle = buffer;
|
| +
|
| + fRenderTarget = fContext->wrapBackendRenderTarget(desc);
|
| +
|
| + fContext->setRenderTarget(fRenderTarget);
|
| + }
|
| +}
|
| +
|
| +SkCanvas* BaseExample::createCanvas() {
|
| + if (fType == kGPU_DeviceType &&
|
| + NULL != fContext && NULL != fRenderTarget) {
|
| + SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget));
|
| + return new SkCanvas(device);
|
| + } else {
|
| + return INHERITED::createCanvas();
|
| + }
|
| +}
|
| +
|
| +void BaseExample::draw(SkCanvas* canvas) {
|
| + if (getDeviceType() == kGPU_DeviceType) {
|
| + present();
|
| + }
|
| +}
|
| +
|
|
|