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
This commit is contained in:
caryclark 2015-01-30 12:37:02 -08:00 committed by Commit bot
parent 8d17a13a71
commit c8fcafb3f0
15 changed files with 186 additions and 277 deletions

View File

@ -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);

View File

@ -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);
fType = SkExampleWindow::kGPU_DeviceType;
fRenderTarget = NULL;
fRotationAngle = 0;
this->setTitle();
this->setUpBackend();
}
if (FLAGS_match.count()) {
// Start with the a matching sample if possible.
bool found = this->findNextMatch();
if (!found) {
SkDebugf("No matching SkExample found.\n");
}
}
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;
GrGLint buffer;
GR_GL_GetIntegerv(fInterface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;
fRenderTarget = fContext->wrapBackendRenderTarget(desc);
fContext->setRenderTarget(fRenderTarget);
void SkExampleWindow::setUpRenderTarget() {
SkSafeUnref(fRenderTarget);
fRenderTarget = this->renderTarget(fAttachmentInfo, fInterface, fContext);
}
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);
void SkExampleWindow::drawContents(SkCanvas* canvas) {
// Clear background
canvas->drawColor(SK_ColorWHITE);
SkPaint paint;
paint.setColor(SK_ColorRED);
// Draw a rectangle with red paint
SkRect rect = {
10, 10,
128, 128
};
canvas->drawRect(rect, paint);
// 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);
}

View File

@ -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();
// draw contents
SkScalar fRotationAngle;
// support framework
DeviceType fType;
SkExample* fCurrExample;
const SkExample::Registry* fRegistry;
SkSurface* fSurface;
GrContext* fContext;
GrRenderTarget* fRenderTarget;
AttachmentInfo fAttachmentInfo;

View File

@ -26,7 +26,6 @@
'sources': [
'../experimental/SkiaExamples/SkExample.h',
'../experimental/SkiaExamples/SkExample.cpp',
'../experimental/SkiaExamples/HelloSkiaExample.cpp',
],
'dependencies': [
'flags.gyp:flags',
@ -36,7 +35,7 @@
'conditions' : [
[ 'skia_gpu == 1', {
'include_dirs' : [
'../src/gpu',
'../include/gpu',
],
}],
[ 'skia_os == "win"', {

View File

@ -77,6 +77,11 @@
'../src/views/SDL/SkOSWindow_SDL.cpp',
],
'conditions': [
[ 'skia_gpu == 1', {
'include_dirs' : [
'../src/gpu',
],
}],
[ 'skia_os == "mac"', {
'link_settings': {
'libraries': [

View File

@ -20,11 +20,6 @@ public:
kNativeGL_BackEndType,
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
bool attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo* info);
void detach() {}
void present() {}

View File

@ -26,11 +26,6 @@ public:
#endif
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
void detach();
bool attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo*);
void present();

View File

@ -20,11 +20,6 @@ public:
kNativeGL_BackEndType,
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
bool attach(SkBackEndTypes /* attachType */, int /* msaaSampleCount */, AttachmentInfo* info) {
info->fSampleCount = 0;
info->fStencilBits = 0;

View File

@ -38,11 +38,6 @@ public:
kNativeGL_BackEndType,
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
bool attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo*);
void detach();
void present();

View File

@ -37,11 +37,6 @@ public:
#endif // SK_SUPPORT_GPU
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
bool attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo*);
void detach();
void present();

View File

@ -21,11 +21,6 @@ public:
kNativeGL_BackEndType,
};
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
void detach();
bool attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo*);
void present();

View File

@ -25,11 +25,22 @@
class SkSurface;
class SkOSMenu;
#if SK_SUPPORT_GPU
struct GrGLInterface;
class GrContext;
class GrRenderTarget;
#endif
class SkWindow : public SkView {
public:
SkWindow();
virtual ~SkWindow();
struct AttachmentInfo {
int fSampleCount;
int fStencilBits;
};
SkSurfaceProps getSurfaceProps() const { return fSurfaceProps; }
void setSurfaceProps(const SkSurfaceProps& props) {
fSurfaceProps = props;
@ -85,6 +96,11 @@ protected:
virtual bool onGetFocusView(SkView** focus) const;
virtual bool onSetFocusView(SkView* focus);
#if SK_SUPPORT_GPU
GrRenderTarget* renderTarget(const AttachmentInfo& attachmentInfo,
const GrGLInterface* , GrContext* grContext);
#endif
private:
SkSurfaceProps fSurfaceProps;
SkColorType fColorType;

View File

@ -315,20 +315,8 @@ public:
if (fCurContext) {
AttachmentInfo attachmentInfo;
win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
GrBackendRenderTargetDesc desc;
desc.fWidth = SkScalarRoundToInt(win->width());
desc.fHeight = SkScalarRoundToInt(win->height());
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = attachmentInfo.fSampleCount;
desc.fStencilBits = attachmentInfo.fStencilBits;
GrGLint buffer;
GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;
SkSafeUnref(fCurRenderTarget);
fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
fCurRenderTarget = win->renderTarget(attachmentInfo, fCurIntf, fCurContext);
}
#endif
}

View File

@ -341,3 +341,26 @@ bool SkWindow::onDispatchClick(int x, int y, Click::State state,
}
return handled;
}
#if SK_SUPPORT_GPU
#include "gl/GrGLInterface.h"
#include "gl/GrGLUtil.h"
#include "SkGr.h"
GrRenderTarget* SkWindow::renderTarget(const AttachmentInfo& attachmentInfo,
const GrGLInterface* interface, GrContext* grContext) {
GrBackendRenderTargetDesc desc;
desc.fWidth = SkScalarRoundToInt(this->width());
desc.fHeight = SkScalarRoundToInt(this->height());
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = attachmentInfo.fSampleCount;
desc.fStencilBits = attachmentInfo.fStencilBits;
GrGLint buffer;
GR_GL_GetIntegerv(interface, GR_GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;
return grContext->wrapBackendRenderTarget(desc);
}
#endif

View File

@ -303,6 +303,11 @@ void SkCommandLineFlags::Parse(int argc, char** argv) {
flag = flag->next();
}
if (!flagMatched) {
#if SK_BUILD_FOR_MAC
if (SkStrStartsWith(argv[i], "NSDocumentRevisions")) {
i++; // skip YES
} else
#endif
if (!FLAGS_undefok) {
SkDebugf("Got unknown flag \"%s\". Exiting.\n", argv[i]);
exit(-1);