Chromium Code Reviews| Index: experimental/SkiaExamples/SkExample.cpp |
| diff --git a/experimental/SkiaExamples/HelloSkiaExample.cpp b/experimental/SkiaExamples/SkExample.cpp |
| similarity index 46% |
| rename from experimental/SkiaExamples/HelloSkiaExample.cpp |
| rename to experimental/SkiaExamples/SkExample.cpp |
| index f25cd76bfabcad6dcbb74584fb20e3f2fa6e5877..9110371e6aa3188fbd8bd7198e52121090080cd4 100644 |
| --- a/experimental/SkiaExamples/HelloSkiaExample.cpp |
| +++ b/experimental/SkiaExamples/SkExample.cpp |
| @@ -7,7 +7,140 @@ |
| * |
| */ |
| -#include "BaseExample.h" |
| +#include "SkExample.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(); |
| +} |
| + |
| +SkExample::SkExample(void* hWnd, int argc, char** argv) |
| + : INHERITED(hWnd) {} |
| + |
| +void SkExample::tearDownBackend() { |
| + if (kGPU_DeviceType == fType) { |
| + SkSafeUnref(fContext); |
| + fContext = NULL; |
| + |
| + SkSafeUnref(fInterface); |
| + fInterface = NULL; |
| + |
| + SkSafeUnref(fRenderTarget); |
| + fRenderTarget = NULL; |
| + |
| + detach(); |
| + } |
| +} |
| + |
| +bool SkExample::setupBackend(DeviceType type) { |
| + fType = type; |
| + |
| + this->setConfig(SkBitmap::kARGB_8888_Config); |
| + this->setVisibleP(true); |
| + this->setClipToBounds(false); |
| + |
| + bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo); |
| + if (false == result) { |
| + SkDebugf("Not possible to create backend.\n"); |
| + detach(); |
| + return false; |
| + } |
| + |
| + fInterface = GrGLCreateNativeInterface(); |
| + |
| + SkASSERT(NULL != fInterface); |
| + |
| + fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface); |
| + SkASSERT(NULL != fContext); |
| + |
| + setupRenderTarget(); |
| + |
| + return true; |
| +} |
| + |
| +void SkExample::setupRenderTarget() { |
| + GrBackendRenderTargetDesc desc; |
| + desc.fWidth = SkScalarRound(width()); |
| + desc.fHeight = SkScalarRound(height()); |
| + desc.fConfig = kSkia8888_GrPixelConfig; |
| + desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| + desc.fSampleCnt = fAttachmentInfo.fSampleCount; |
| + desc.fStencilBits = fAttachmentInfo.fStencilBits; |
| + |
| + GrGLint buffer; |
| + GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| + desc.fRenderTargetHandle = buffer; |
| + |
| + fRenderTarget = fContext->wrapBackendRenderTarget(desc); |
| + |
| + fContext->setRenderTarget(fRenderTarget); |
| +} |
| + |
| +SkCanvas* SkExample::createCanvas() { |
| + if (fType == kGPU_DeviceType) { |
| + if (NULL != fContext && NULL != fRenderTarget) { |
| + SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget)); |
| + return new SkCanvas(device); |
| + } |
| + tearDownBackend(); |
| + setupBackend(kRaster_DeviceType); |
| + } |
| + return INHERITED::createCanvas(); |
| +} |
| + |
| +void SkExample::draw(SkCanvas* canvas) { |
| + if (fType == kGPU_DeviceType) { |
| + |
| + SkASSERT(NULL != fContext); |
| + fContext->flush(); |
| + } |
| + if (fType == kRaster_DeviceType) { |
| + // 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()); |
| + } |
| + INHERITED::present(); |
| +} |
| + |
| +void SkExample::onSizeChange() { |
| + setupRenderTarget(); |
| +} |
| + |
| +#ifdef SK_BUILD_FOR_WIN |
| +void SkExample::onHandleInval(const SkIRect& rect) { |
| + RECT winRect; |
| + winRect.top = rect.top(); |
| + winRect.bottom = rect.bottom(); |
| + winRect.right = rect.right(); |
| + winRect.left = rect.left(); |
| + InvalidateRect((HWND)this->getHWND(), &winRect, false); |
| +} |
| +#endif |
| + |
| +static SkOSWindow* dummy_factory(void* hwnd, int argc, char** argv) { |
| + return NULL; |
| +} |
| +SkOSWindow* (*SkExample::create_user_example) (void* hwnd, int argc, char** argv) = &dummy_factory; |
| +bool SkExample::userExampleExists = false; |
|
sglez
2013/07/03 15:53:25
Lines 136 to 140 have changes from BaseExample.cp
|
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Define a default "Hello World" Skia Example: |
|
sglez
2013/07/03 15:53:25
Removed HelloWorldSkia.cpp and inserted its conten
|
| #include "SkApplication.h" |
| #include "SkDraw.h" |
| @@ -15,7 +148,7 @@ |
| #include "SkGraphics.h" |
| #include "SkUnitMappers.h" |
| -class HelloSkia : public BaseExample { |
| +class HelloSkia : public SkExample { |
| public: |
| HelloSkia(void* hWnd, int argc, char** argv) |
| : INHERITED(hWnd, argc, argv) |
| @@ -69,7 +202,6 @@ class HelloSkia : public BaseExample { |
| paint.setShader(NULL); |
| } |
| - |
| // Draw a message with a nice black paint. |
| paint.setFlags( |
| SkPaint::kAntiAlias_Flag | |
| @@ -104,9 +236,18 @@ class HelloSkia : public BaseExample { |
| private: |
| SkScalar fRotationAngle; |
| SkColor fBGColor; |
| - typedef BaseExample INHERITED; |
| + typedef SkExample INHERITED; |
| }; |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// |
| SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
| + SkOSWindow* userExample = SkExample::create_user_example(hwnd, argc, argv); |
|
caryclark
2013/07/08 12:42:35
(*SkExample::create_user_example)(hwnd, ...
(and
|
| + if (NULL == userExample) { |
| + SkDebugf("No user example registered. Using default example.\n"); |
| + } else { |
| + return userExample; |
| + } |
| return new HelloSkia(hwnd, argc, argv); |
| } |
| + |