diff options
author | caryclark <caryclark@google.com> | 2015-01-30 12:37:02 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-30 12:37:02 -0800 |
commit | c8fcafb3f0d152fb92465451bdb2e4bd3ef37222 (patch) | |
tree | ed205c0bdd413c88bec4a59373f64dcc04760cfe /experimental | |
parent | 8d17a13a71edb0d8412e4354c428582b74587b79 (diff) |
First cut at cleaning up Sergio's example code and moving some common code to SkWindow.
Eventually, this will be moved to be a peer of SampleApp so it is compiled by the bots to avoid future bit rot.
Also ignore XCode auto-generated flag in CommandLineFlags, and remove the unused multiple-example part.
Review URL: https://codereview.chromium.org/890873003
Diffstat (limited to 'experimental')
-rw-r--r-- | experimental/SkiaExamples/HelloSkiaExample.cpp | 106 | ||||
-rw-r--r-- | experimental/SkiaExamples/SkExample.cpp | 225 | ||||
-rw-r--r-- | experimental/SkiaExamples/SkExample.h | 34 |
3 files changed, 134 insertions, 231 deletions
diff --git a/experimental/SkiaExamples/HelloSkiaExample.cpp b/experimental/SkiaExamples/HelloSkiaExample.cpp deleted file mode 100644 index 6fd2624c18..0000000000 --- a/experimental/SkiaExamples/HelloSkiaExample.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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 "SkExample.h" - -#include "SkApplication.h" -#include "SkDraw.h" -#include "SkGradientShader.h" -#include "SkGraphics.h" - -class HelloSkia : public SkExample { -public: - HelloSkia(SkExampleWindow* window) : SkExample(window) { - fName = "HelloSkia"; - fBGColor = SK_ColorWHITE; - fRotationAngle = SkIntToScalar(0); - - fWindow->setupBackend(SkExampleWindow::kGPU_DeviceType); - // Another option is software rendering: - // fWindow->setupBackend(SkExampleWindow::kRaster_DeviceType); - } - -protected: - void draw(SkCanvas* canvas) { - // Clear background - canvas->drawColor(fBGColor); - - SkPaint paint; - paint.setColor(SK_ColorRED); - - // Draw a rectangle with blue paint - SkRect rect = { - SkIntToScalar(10), SkIntToScalar(10), - SkIntToScalar(128), SkIntToScalar(128) - }; - canvas->drawRect(rect, paint); - - // Set up a linear gradient and draw a circle - { - SkPoint linearPoints[] = { - {SkIntToScalar(0), SkIntToScalar(0)}, - {SkIntToScalar(300), SkIntToScalar(300)} - }; - SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; - - SkShader* shader = SkGradientShader::CreateLinear( - linearPoints, linearColors, NULL, 2, - SkShader::kMirror_TileMode); - SkAutoUnref shader_deleter(shader); - - paint.setShader(shader); - paint.setFlags(SkPaint::kAntiAlias_Flag); - - canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200), - SkIntToScalar(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(SkIntToScalar(300), SkIntToScalar(300)); - fRotationAngle += SkDoubleToScalar(0.2); - if (fRotationAngle > SkDoubleToScalar(360.0)) { - fRotationAngle -= SkDoubleToScalar(360.0); - } - canvas->rotate(fRotationAngle); - - // Draw the text: - canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntToScalar(0), paint); - - canvas->restore(); - - // Invalidate the window to force a redraw. Poor man's animation mechanism. - this->fWindow->inval(NULL); - } - -private: - SkScalar fRotationAngle; - SkColor fBGColor; -}; - -static SkExample* MyFactory(SkExampleWindow* window) { - return new HelloSkia(window); -} - -// Register this class as a Skia Example. -SkExample::Registry registry(MyFactory); diff --git a/experimental/SkiaExamples/SkExample.cpp b/experimental/SkiaExamples/SkExample.cpp index 637ec9df53..849c14b3ec 100644 --- a/experimental/SkiaExamples/SkExample.cpp +++ b/experimental/SkiaExamples/SkExample.cpp @@ -9,22 +9,12 @@ #include "SkExample.h" -#include "gl/GrGLUtil.h" -#include "gl/GrGLDefines.h" #include "gl/GrGLInterface.h" #include "SkApplication.h" -#include "SkCommandLineFlags.h" -#include "SkGpuDevice.h" +#include "SkCanvas.h" +#include "SkGradientShader.h" #include "SkGraphics.h" - -DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \ - "Multiple matches may be separated by spaces.\n" \ - "~ causes a matching test to always be skipped\n" \ - "^ requires the start of the test to match\n" \ - "$ requires the end of the test to match\n" \ - "^ and $ requires an exact match\n" \ - "If a test does not match any list entry,\n" \ - "it is skipped unless some list entry starts with ~"); +#include "SkGr.h" void application_init() { SkGraphics::Init(); @@ -38,36 +28,37 @@ void application_term() { SkExampleWindow::SkExampleWindow(void* hwnd) : INHERITED(hwnd) { - fRegistry = SkExample::Registry::Head(); - fCurrExample = fRegistry->factory()(this); - - if (FLAGS_match.count()) { - // Start with the a matching sample if possible. - bool found = this->findNextMatch(); - if (!found) { - SkDebugf("No matching SkExample found.\n"); - } - } + fType = SkExampleWindow::kGPU_DeviceType; + fRenderTarget = NULL; + fRotationAngle = 0; + this->setTitle(); + this->setUpBackend(); +} + +SkExampleWindow::~SkExampleWindow() { + tearDownBackend(); } void SkExampleWindow::tearDownBackend() { - if (kGPU_DeviceType == fType) { - SkSafeUnref(fContext); - fContext = NULL; + SkSafeUnref(fContext); + fContext = NULL; - SkSafeUnref(fInterface); - fInterface = NULL; + SkSafeUnref(fInterface); + fInterface = NULL; - SkSafeUnref(fRenderTarget); - fRenderTarget = NULL; + SkSafeUnref(fRenderTarget); + fRenderTarget = NULL; - detach(); - } + INHERITED::detach(); } -bool SkExampleWindow::setupBackend(DeviceType type) { - fType = type; +void SkExampleWindow::setTitle() { + SkString title("SkiaExample "); + title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl"); + INHERITED::setTitle(title.c_str()); +} +bool SkExampleWindow::setUpBackend() { this->setColorType(kRGBA_8888_SkColorType); this->setVisibleP(true); this->setClipToBounds(false); @@ -86,109 +77,117 @@ bool SkExampleWindow::setupBackend(DeviceType type) { fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface); SkASSERT(NULL != fContext); - setupRenderTarget(); - + this->setUpRenderTarget(); return true; } -void SkExampleWindow::setupRenderTarget() { - GrBackendRenderTargetDesc desc; - desc.fWidth = SkScalarRoundToInt(width()); - desc.fHeight = SkScalarRoundToInt(height()); - desc.fConfig = kSkia8888_GrPixelConfig; - desc.fOrigin = kBottomLeft_GrSurfaceOrigin; - desc.fSampleCnt = fAttachmentInfo.fSampleCount; - desc.fStencilBits = fAttachmentInfo.fStencilBits; +void SkExampleWindow::setUpRenderTarget() { + SkSafeUnref(fRenderTarget); + fRenderTarget = this->renderTarget(fAttachmentInfo, fInterface, fContext); +} - GrGLint buffer; - GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer); - desc.fRenderTargetHandle = buffer; +void SkExampleWindow::drawContents(SkCanvas* canvas) { + // Clear background + canvas->drawColor(SK_ColorWHITE); - fRenderTarget = fContext->wrapBackendRenderTarget(desc); + SkPaint paint; + paint.setColor(SK_ColorRED); - fContext->setRenderTarget(fRenderTarget); -} + // Draw a rectangle with red paint + SkRect rect = { + 10, 10, + 128, 128 + }; + canvas->drawRect(rect, paint); -SkCanvas* SkExampleWindow::createCanvas() { - if (fType == kGPU_DeviceType) { - if (NULL != fContext && NULL != fRenderTarget) { - SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fContext, fRenderTarget)); - return new SkCanvas(device); - } - tearDownBackend(); - setupBackend(kRaster_DeviceType); + // Set up a linear gradient and draw a circle + { + SkPoint linearPoints[] = { + {0, 0}, + {300, 300} + }; + SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; + + SkShader* shader = SkGradientShader::CreateLinear( + linearPoints, linearColors, NULL, 2, + SkShader::kMirror_TileMode); + SkAutoUnref shader_deleter(shader); + + paint.setShader(shader); + paint.setFlags(SkPaint::kAntiAlias_Flag); + + canvas->drawCircle(200, 200, 64, paint); + + // Detach shader + paint.setShader(NULL); } - return INHERITED::createCanvas(); + + // 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(20); + + canvas->save(); + + static const char message[] = "Hello Skia!!!"; + + // Translate and rotate + canvas->translate(300, 300); + fRotationAngle += 0.2f; + if (fRotationAngle > 360) { + fRotationAngle -= 360; + } + canvas->rotate(fRotationAngle); + + // Draw the text: + canvas->drawText(message, strlen(message), 0, 0, paint); + + canvas->restore(); } void SkExampleWindow::draw(SkCanvas* canvas) { - if (NULL != fCurrExample) { - fCurrExample->draw(canvas); - } - if (fType == kGPU_DeviceType) { + drawContents(canvas); + // in case we have queued drawing calls + fContext->flush(); + // Invalidate the window to force a redraw. Poor man's animation mechanism. + this->inval(NULL); - SkASSERT(NULL != fContext); - fContext->flush(); - } - if (fType == kRaster_DeviceType) { + if (kRaster_DeviceType == fType) { // 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()); + SkImage* snap = fSurface->newImageSnapshot(); + size_t rowBytes; + SkImageInfo info; + const void* pixels = snap->peekPixels(&info, &rowBytes); + fRenderTarget->writePixels(0, 0, snap->width(), snap->height(), + SkImageInfo2GrPixelConfig(info.colorType(), + info.alphaType(), + info.profileType()), + pixels, + rowBytes, + GrContext::kFlushWrites_PixelOp); + SkSafeUnref(snap); } INHERITED::present(); } void SkExampleWindow::onSizeChange() { - setupRenderTarget(); -} - -#ifdef SK_BUILD_FOR_WIN -void SkExampleWindow::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 - -bool SkExampleWindow::findNextMatch() { - bool found = false; - // Avoid infinite loop by knowing where we started. - const SkExample::Registry* begin = fRegistry; - while (!found) { - fRegistry = fRegistry->next(); - if (NULL == fRegistry) { // Reached the end of the registered samples. GOTO head. - fRegistry = SkExample::Registry::Head(); - } - SkExample* next = fRegistry->factory()(this); - if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, next->getName().c_str())) { - fCurrExample = next; - found = true; - } - if (begin == fRegistry) { // We looped through every sample without finding anything. - break; - } - } - return found; + setUpRenderTarget(); } bool SkExampleWindow::onHandleChar(SkUnichar unichar) { - if ('n' == unichar) { - bool found = findNextMatch(); - if (!found) { - SkDebugf("No SkExample that matches your query\n"); - } + if (' ' == unichar) { + fType = fType == kRaster_DeviceType ? kGPU_DeviceType: kRaster_DeviceType; + tearDownBackend(); + setUpBackend(); + this->setTitle(); + this->inval(NULL); } return true; } -SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { - SkCommandLineFlags::Parse(argc, argv); +SkOSWindow* create_sk_window(void* hwnd, int , char** ) { return new SkExampleWindow(hwnd); } diff --git a/experimental/SkiaExamples/SkExample.h b/experimental/SkiaExamples/SkExample.h index ee9e36e0d0..4f4b10be55 100644 --- a/experimental/SkiaExamples/SkExample.h +++ b/experimental/SkiaExamples/SkExample.h @@ -10,6 +10,7 @@ #ifndef SkExample_DEFINED #define SkExample_DEFINED +#include "SkSurface.h" #include "SkWindow.h" #include "SkTRegistry.h" @@ -44,33 +45,42 @@ public: kGPU_DeviceType, }; SkExampleWindow(void* hwnd); + virtual ~SkExampleWindow() SK_OVERRIDE; // Changes the device type of the object. - bool setupBackend(DeviceType type); - void tearDownBackend(); + bool setUpBackend(); DeviceType getDeviceType() const { return fType; } protected: + SkSurface* createSurface() SK_OVERRIDE { + if (kGPU_DeviceType == fType) { + SkSurfaceProps props(INHERITED::getSurfaceProps()); + return SkSurface::NewRenderTargetDirect(fRenderTarget, &props); + } + static const SkImageInfo info = SkImageInfo::MakeN32Premul( + SkScalarRoundToInt(this->width()), SkScalarRoundToInt(this->height())); + return fSurface = SkSurface::NewRaster(info); + } + void draw(SkCanvas* canvas) SK_OVERRIDE; + void drawContents(SkCanvas* canvas); void onSizeChange() SK_OVERRIDE; -#ifdef SK_BUILD_FOR_WIN - void onHandleInval(const SkIRect&) SK_OVERRIDE; -#endif - - SkCanvas* createCanvas() SK_OVERRIDE; - private: bool findNextMatch(); // Set example to the first one that matches FLAGS_match. - void setupRenderTarget(); + void setTitle(); + void setUpRenderTarget(); bool onHandleChar(SkUnichar unichar) SK_OVERRIDE; + void tearDownBackend(); - DeviceType fType; + // draw contents + SkScalar fRotationAngle; - SkExample* fCurrExample; - const SkExample::Registry* fRegistry; + // support framework + DeviceType fType; + SkSurface* fSurface; GrContext* fContext; GrRenderTarget* fRenderTarget; AttachmentInfo fAttachmentInfo; |