Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Unified Diff: experimental/SkiaExamples/BaseExample.cpp

Issue 16337012: Smallest possible desktop application that uses Skia to render stuff. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix speed hiccups in Windows Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: experimental/SkiaExamples/BaseExample.cpp
diff --git a/experimental/SkiaExamples/BaseExample.cpp b/experimental/SkiaExamples/BaseExample.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e54b039e9a071aea64d508ee81978f0becd0c7c0
--- /dev/null
+++ b/experimental/SkiaExamples/BaseExample.cpp
@@ -0,0 +1,146 @@
+/*
+ * 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), fDelayMs(16) {}
+
+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;
+
+ 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.");
+ detach();
+ return false;
+ }
+
+ fInterface = GrGLCreateNativeInterface();
+
+ SkASSERT(NULL != fInterface);
+
+ fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface);
+ SkASSERT(NULL != fContext);
+
+ setupRenderTarget();
+
+ return true;
+}
+
+void BaseExample::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* BaseExample::createCanvas() {
+ if (fType == kGPU_DeviceType &&
caryclark 2013/06/17 15:08:39 note if (fType ... ) here ... but below ...
+ 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();
+ }
caryclark 2013/06/17 15:08:39 I would rewrite this as: if (fType == kGPU_Device
sglez 2013/06/17 18:04:35 The else is still needed to handle a nil context o
+}
+
+void BaseExample::draw(SkCanvas* canvas) {
+ if (getDeviceType() == kGPU_DeviceType) {
caryclark 2013/06/17 15:08:39 if (getDeviceType() ... here. Choose one or the ot
+ SkASSERT(NULL != fContext);
+ fContext->flush();
+ }
+ if (kRaster_DeviceType == getDeviceType()) {
caryclark 2013/06/17 15:08:39 reverse compare order
+ // 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();
+}
+
+void BaseExample::onSizeChange() {
+ setupRenderTarget();
+}
+
+void BaseExample::setInvalDelay(SkMSec ms) {
+ fDelayMs = ms;
caryclark 2013/06/17 15:08:39 One line implementations might as well live in the
sglez 2013/06/17 18:04:35 Mike's changes make my delay fix obsolete so I'm t
+}
+
+#ifdef SK_BUILD_FOR_UNIX
+void BaseExample::onHandleInval(const SkIRect&) {
+ (new SkEvent("inval-imageview", this->getSinkID()))->postDelay(fDelayMs);
+}
+#endif
+
+#ifdef SK_BUILD_FOR_WIN
+void BaseExample::onHandleInval(const SkIRect& rect) {
+ Sleep(fDelayMs);
caryclark 2013/06/17 15:08:39 This suggests that one thread should be doing comp
sglez 2013/06/17 18:04:35 The intent was to mimic the Linux behavior by maki
+ 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

Powered by Google App Engine
This is Rietveld 408576698