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

Unified Diff: experimental/HelloSkia/HelloSkia.cpp

Issue 16337012: Smallest possible desktop application that uses Skia to render stuff. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: GPU support 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/HelloSkia/HelloSkia.cpp
diff --git a/experimental/HelloSkia/HelloSkia.cpp b/experimental/HelloSkia/HelloSkia.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..01d5126118bacfba1a6a15276878925b486fa0f7
--- /dev/null
+++ b/experimental/HelloSkia/HelloSkia.cpp
@@ -0,0 +1,174 @@
+/*
+ * 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 "HelloSkia.h"
+
+#include "SkDraw.h"
+#include "SkGradientShader.h"
+#include "SkGraphics.h"
+#include "SkUnitMappers.h"
+
+#ifdef SK_HelloSkia_ENABLE_GPU
+#include "gl/GrGLInterface.h"
+#include "GrContext.h"
+#include "SkDevice.h"
+#include "SkGpuDevice.h"
+#include "GrTypes.h"
+#endif
+
+SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
+ return new HelloWindow(hwnd, argc, argv);
+}
+
+void application_init() {
+ SkGraphics::Init();
+ SkEvent::Init();
+}
+
+void application_term() {
+ SkEvent::Term();
+ SkGraphics::Term();
+}
+
+HelloWindow::HelloWindow(void* hWnd, int argc, char** argv)
+ : INHERITED(hWnd)
+{
+ this->setConfig(SkBitmap::kARGB_8888_Config);
+ this->setVisibleP(true);
+ this->setClipToBounds(false);
+
+ fBGColor = SK_ColorWHITE;
+ fRotationAngle = SkIntToScalar(0);
+
+ setupBackend();
+}
+
+HelloWindow::~HelloWindow() {
+#ifdef SK_HelloSkia_ENABLE_GPU
+ SkSafeUnref(fContext);
+ SkSafeUnref(fInterface);
+ SkSafeUnref(fRenderTarget);
+#endif
+}
+
+void HelloWindow::setupBackend() {
+#ifdef SK_HelloSkia_ENABLE_GPU
+ AttachmentInfo attachmentInfo;
+ bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, &attachmentInfo);
+ if (false == result) {
+ SkDebugf("Not possible to create GL backend. Defaulting to raster.");
caryclark 2013/06/05 11:55:33 this should change the state of the runtime switch
sglez 2013/06/05 16:15:31 Yes, this was incomplete. Thanks.
+ }
+
+ 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);
+#endif
+}
+
+SkCanvas* HelloWindow::createCanvas() {
+#ifdef SK_HelloSkia_ENABLE_GPU
caryclark 2013/06/05 11:55:33 It looks like the only runtime decision is here (e
+ if (fContext && fRenderTarget) {
+ SkASSERT(NULL != fContext && NULL != fRenderTarget);
+ SkAutoTUnref<SkDevice> device(new SkGpuDevice(fContext, fRenderTarget));
+ return new SkCanvas(device);
+ } else {
+ SkDebugf("Not using GPU canvas.\n");
+ return INHERITED::createCanvas();
+ }
+#else
+ return INHERITED::createCanvas();
+#endif
+}
+
+void HelloWindow::draw(SkCanvas* canvas) {
+ // Clear background
+ canvas->drawColor(fBGColor);
+
+ SkPaint paint;
+ paint.setColor(SK_ColorRED); // SkColor is ARGB
+
+ // Draw a rectangle with blue paint
+ SkRect rect = {10, 10, 128, 128};
+ canvas->drawRect(rect, paint);
+
+ // Set up a linear gradient and draw a circle
+ {
+ SkPoint linearPoints[] = {{0, 0}, {SkIntToScalar(300), SkIntToScalar(300)}};
+ SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
+
+ SkUnitMapper* linearMapper = new SkDiscreteMapper(100);
+ SkAutoUnref lm_deleter(linearMapper);
+
+ SkShader* shader = SkGradientShader::CreateLinear(
+ linearPoints, linearColors, NULL, 2, SkShader::kMirror_TileMode, linearMapper);
+ SkAutoUnref shader_deleter(shader);
+
+ paint.setShader(shader);
+ paint.setFlags(SkPaint::kAntiAlias_Flag);
+
+ canvas->drawCircle(200, 200, 64, paint);
+
+ // Detach shader
+ paint.setShader(NULL);
+ }
+
+
+ // Draw a message with a nice black paint.
+ paint.setFlags(
+ SkPaint::kAntiAlias_Flag |
+ SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating.
+ SkPaint::kUnderlineText_Flag);
+ paint.setColor(SK_ColorBLACK);
+ paint.setTextSize(SkIntToScalar(20));
+
+ canvas->save();
+
+ static const char message[] = "Hello Skia!!!";
+
+ // Translate and rotate
+ canvas->translate(300, 300);
+ fRotationAngle += SkFloatToScalar(0.2);
+ if (fRotationAngle > SkFloatToScalar(360.0)) {
+ fRotationAngle -= SkFloatToScalar(360.0);
+ }
+ canvas->rotate(fRotationAngle);
+
+ // Draw the text:
+ canvas->drawText(message, strlen(message), 0, 0, paint);
+
+ canvas->restore();
+
+ // Invalidate the window to force a redraw. Poor man's animation mechanism.
+ this->inval(NULL);
+ canvas->flush();
+#ifdef SK_HelloSkia_ENABLE_GPU
+ // Swap buffers if we are using GL.
+ present();
+#endif
+}
+

Powered by Google App Engine
This is Rietveld 408576698