| Index: experimental/SkiaExamples/BaseExample.cpp
|
| diff --git a/experimental/SkiaExamples/BaseExample.cpp b/experimental/SkiaExamples/BaseExample.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b0885f880f35aab512117f1e0d71fdeb4bc5a696
|
| --- /dev/null
|
| +++ b/experimental/SkiaExamples/BaseExample.cpp
|
| @@ -0,0 +1,125 @@
|
| +/*
|
| + * 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/GrGLUtil.h"
|
| +#include "gl/GrGLDefines.h"
|
| +#include "gl/GrGLInterface.h"
|
| +#include "SkApplication.h"
|
| +#include "SkGpuDevice.h"
|
| +#include "SkGraphics.h"
|
| +
|
| +void application_init() {
|
| + SkGraphics::Init();
|
| + SkEvent::Init();
|
| +}
|
| +
|
| +void application_term() {
|
| + SkEvent::Term();
|
| + SkGraphics::Term();
|
| +}
|
| +
|
| +BaseExample::BaseExample(void* hWnd, int argc, char** argv)
|
| + : INHERITED(hWnd)
|
| +{
|
| + this->setConfig(SkBitmap::kARGB_8888_Config);
|
| + this->setVisibleP(true);
|
| + this->setClipToBounds(false);
|
| +}
|
| +
|
| +void BaseExample::tearDownBackend() {
|
| + if (kGPU_DeviceType == fType) {
|
| + SkSafeUnref(fContext);
|
| + fContext = NULL;
|
| +
|
| + SkSafeUnref(fInterface);
|
| + fInterface = NULL;
|
| +
|
| + SkSafeUnref(fRenderTarget);
|
| + fRenderTarget = NULL;
|
| +
|
| + detach();
|
| + }
|
| +}
|
| +
|
| +bool BaseExample::setupBackend(DeviceType type) {
|
| + fType = type;
|
| +
|
| + if(fType != kGPU_DeviceType) {
|
| + return true;
|
| + }
|
| +
|
| + AttachmentInfo attachmentInfo;
|
| +
|
| + bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo);
|
| +
|
| + fInterface = GrGLCreateNativeInterface();
|
| +
|
| + SkASSERT(NULL != fInterface);
|
| +
|
| + fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface);
|
| + SkASSERT(NULL != fContext);
|
| +
|
| + if (false == result) {
|
| + SkDebugf("Not possible to create GL backend. Defaulting to raster.");
|
| + fType = kRaster_DeviceType;
|
| + detach();
|
| + return false;
|
| + }
|
| +
|
| + 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;
|
| + GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
|
| + desc.fRenderTargetHandle = buffer;
|
| +
|
| + fRenderTarget = fContext->wrapBackendRenderTarget(desc);
|
| +
|
| + fContext->setRenderTarget(fRenderTarget);
|
| +
|
| + return true;
|
| +}
|
| +
|
| +SkCanvas* BaseExample::createCanvas() {
|
| + if (fType == kGPU_DeviceType &&
|
| + NULL != fContext && NULL != fRenderTarget) {
|
| + SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget));
|
| + return new SkCanvas(device);
|
| + } else {
|
| + if (fType == kGPU_DeviceType) {
|
| + tearDownBackend();
|
| + setupBackend(kRaster_DeviceType);
|
| + }
|
| + return INHERITED::createCanvas();
|
| + }
|
| +}
|
| +
|
| +void BaseExample::draw(SkCanvas* canvas) {
|
| + if (getDeviceType() == kGPU_DeviceType) {
|
| + SkASSERT(NULL != fContext);
|
| + fContext->flush();
|
| + }
|
| + if (kRaster_DeviceType == getDeviceType()) {
|
| + // need to send the raster bits to the (gpu) window
|
| + fContext->setRenderTarget(fRenderTarget);
|
| + const SkBitmap& bm = getBitmap();
|
| + fRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
|
| + kSkia8888_GrPixelConfig,
|
| + bm.getPixels(),
|
| + bm.rowBytes());
|
| + }
|
| + present();
|
| +}
|
|
|