2015-02-02 20:55:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "HelloWorld.h"
|
|
|
|
|
|
|
|
#include "gl/GrGLInterface.h"
|
2015-10-09 20:36:42 +00:00
|
|
|
#include "GrContext.h"
|
2015-02-02 20:55:14 +00:00
|
|
|
#include "SkApplication.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
|
|
|
|
void application_init() {
|
|
|
|
SkGraphics::Init();
|
|
|
|
SkEvent::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void application_term() {
|
|
|
|
SkEvent::Term();
|
|
|
|
}
|
|
|
|
|
|
|
|
HelloWorldWindow::HelloWorldWindow(void* hwnd)
|
|
|
|
: INHERITED(hwnd) {
|
|
|
|
fType = kGPU_DeviceType;
|
|
|
|
fRotationAngle = 0;
|
|
|
|
this->setTitle();
|
|
|
|
this->setUpBackend();
|
|
|
|
}
|
|
|
|
|
|
|
|
HelloWorldWindow::~HelloWorldWindow() {
|
|
|
|
tearDownBackend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWorldWindow::tearDownBackend() {
|
|
|
|
SkSafeUnref(fContext);
|
|
|
|
fContext = NULL;
|
|
|
|
|
|
|
|
SkSafeUnref(fInterface);
|
|
|
|
fInterface = NULL;
|
|
|
|
|
2016-07-28 22:17:34 +00:00
|
|
|
fGpuSurface = nullptr;
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2016-03-16 20:53:35 +00:00
|
|
|
INHERITED::release();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWorldWindow::setTitle() {
|
|
|
|
SkString title("Hello World ");
|
|
|
|
title.appendf(fType == kRaster_DeviceType ? "raster" : "opengl");
|
|
|
|
INHERITED::setTitle(title.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HelloWorldWindow::setUpBackend() {
|
|
|
|
this->setVisibleP(true);
|
|
|
|
this->setClipToBounds(false);
|
|
|
|
|
2016-05-05 19:24:31 +00:00
|
|
|
bool result = attach(kNativeGL_BackEndType, 0 /*msaa*/, false, &fAttachmentInfo);
|
2015-02-02 20:55:14 +00:00
|
|
|
if (false == result) {
|
|
|
|
SkDebugf("Not possible to create backend.\n");
|
2016-03-16 20:53:35 +00:00
|
|
|
release();
|
2015-02-02 20:55:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fInterface = GrGLCreateNativeInterface();
|
|
|
|
SkASSERT(NULL != fInterface);
|
|
|
|
|
|
|
|
fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface);
|
|
|
|
SkASSERT(NULL != fContext);
|
|
|
|
|
2016-07-28 22:17:34 +00:00
|
|
|
this->setUpGpuBackedSurface();
|
2015-02-02 20:55:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:17:34 +00:00
|
|
|
void HelloWorldWindow::setUpGpuBackedSurface() {
|
|
|
|
fGpuSurface = this->makeGpuBackedSurface(fAttachmentInfo, fInterface, fContext);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWorldWindow::drawContents(SkCanvas* canvas) {
|
|
|
|
// Clear background
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// Draw a rectangle with red paint
|
2016-03-14 19:22:10 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
|
2015-02-02 20:55:14 +00:00
|
|
|
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};
|
|
|
|
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(
|
|
|
|
linearPoints, linearColors, nullptr, 2,
|
|
|
|
SkShader::kMirror_TileMode));
|
2015-02-02 20:55:14 +00:00
|
|
|
paint.setFlags(SkPaint::kAntiAlias_Flag);
|
|
|
|
|
|
|
|
canvas->drawCircle(200, 200, 64, paint);
|
|
|
|
|
|
|
|
// Detach shader
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(nullptr);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 World";
|
|
|
|
|
|
|
|
// 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 HelloWorldWindow::draw(SkCanvas* canvas) {
|
2016-07-28 22:17:34 +00:00
|
|
|
this->drawContents(canvas);
|
2015-02-02 20:55:14 +00:00
|
|
|
// Invalidate the window to force a redraw. Poor man's animation mechanism.
|
|
|
|
this->inval(NULL);
|
|
|
|
|
|
|
|
if (kRaster_DeviceType == fType) {
|
2016-09-29 18:12:11 +00:00
|
|
|
fRasterSurface->draw(fGpuSurface->getCanvas(), 0, 0, nullptr);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
2016-09-29 18:12:11 +00:00
|
|
|
fGpuSurface->getCanvas()->flush();
|
2015-02-02 20:55:14 +00:00
|
|
|
INHERITED::present();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWorldWindow::onSizeChange() {
|
2016-07-28 22:17:34 +00:00
|
|
|
this->setUpGpuBackedSurface();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HelloWorldWindow::onHandleChar(SkUnichar unichar) {
|
|
|
|
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 , char** ) {
|
|
|
|
return new HelloWorldWindow(hwnd);
|
|
|
|
}
|