2015-11-13 19:22:19 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-05-01 13:50:58 +00:00
|
|
|
#include "GrBackendSurface.h"
|
2015-11-13 19:22:19 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
#include "gl/GrGLInterface.h"
|
|
|
|
#include "gl/GrGLUtil.h"
|
|
|
|
|
|
|
|
#if defined(SK_BUILD_FOR_ANDROID)
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
#elif defined(SK_BUILD_FOR_UNIX)
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#elif defined(SK_BUILD_FOR_MAC)
|
2016-08-04 19:30:31 +00:00
|
|
|
#include <OpenGL/gl.h>
|
2017-08-30 18:02:50 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_IOS)
|
|
|
|
#include <OpenGLES/ES2/gl.h>
|
2015-11-13 19:22:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This application is a simple example of how to combine SDL and Skia it demonstrates:
|
|
|
|
* how to setup gpu rendering to the main window
|
|
|
|
* how to perform cpu-side rendering and draw the result to the gpu-backed screen
|
|
|
|
* draw simple primitives (rectangles)
|
|
|
|
* draw more complex primitives (star)
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ApplicationState {
|
|
|
|
ApplicationState() : fQuit(false) {}
|
|
|
|
// Storage for the user created rectangles. The last one may still be being edited.
|
|
|
|
SkTArray<SkRect> fRects;
|
|
|
|
bool fQuit;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_error() {
|
|
|
|
const char* error = SDL_GetError();
|
|
|
|
SkDebugf("SDL Error: %s\n", error);
|
|
|
|
SDL_ClearError();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_events(ApplicationState* state, SkCanvas* canvas) {
|
|
|
|
SDL_Event event;
|
|
|
|
while(SDL_PollEvent(&event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
if (event.motion.state == SDL_PRESSED) {
|
|
|
|
SkRect& rect = state->fRects.back();
|
|
|
|
rect.fRight = event.motion.x;
|
|
|
|
rect.fBottom = event.motion.y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
if (event.button.state == SDL_PRESSED) {
|
|
|
|
state->fRects.push_back() = SkRect::MakeLTRB(SkIntToScalar(event.button.x),
|
|
|
|
SkIntToScalar(event.button.y),
|
|
|
|
SkIntToScalar(event.button.x),
|
|
|
|
SkIntToScalar(event.button.y));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_KEYDOWN: {
|
|
|
|
SDL_Keycode key = event.key.keysym.sym;
|
|
|
|
if (key == SDLK_ESCAPE) {
|
|
|
|
state->fQuit = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_QUIT:
|
|
|
|
state->fQuit = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a star type shape using a SkPath
|
|
|
|
static SkPath create_star() {
|
|
|
|
static const int kNumPoints = 5;
|
|
|
|
SkPath concavePath;
|
|
|
|
SkPoint points[kNumPoints] = {{0, SkIntToScalar(-50)} };
|
|
|
|
SkMatrix rot;
|
|
|
|
rot.setRotate(SkIntToScalar(360) / kNumPoints);
|
|
|
|
for (int i = 1; i < kNumPoints; ++i) {
|
|
|
|
rot.mapPoints(points + i, points + i - 1, 1);
|
|
|
|
}
|
|
|
|
concavePath.moveTo(points[0]);
|
|
|
|
for (int i = 0; i < kNumPoints; ++i) {
|
|
|
|
concavePath.lineTo(points[(2 * i) % kNumPoints]);
|
|
|
|
}
|
|
|
|
concavePath.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
SkASSERT(!concavePath.isConvex());
|
|
|
|
concavePath.close();
|
|
|
|
return concavePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SK_BUILD_FOR_ANDROID)
|
|
|
|
int SDL_main(int argc, char** argv) {
|
|
|
|
#else
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
#endif
|
|
|
|
uint32_t windowFlags = 0;
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
|
|
|
|
|
|
SDL_GLContext glContext = nullptr;
|
2017-09-01 18:49:09 +00:00
|
|
|
#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
|
|
|
|
// For Android/iOS we need to set up for OpenGL ES and we make the window hi res & full screen
|
2015-11-13 19:22:19 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
|
|
windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |
|
|
|
|
SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP |
|
|
|
|
SDL_WINDOW_ALLOW_HIGHDPI;
|
|
|
|
#else
|
|
|
|
// For all other clients we use the core profile and operate in a window
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
|
|
|
|
|
windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
|
|
|
|
#endif
|
|
|
|
static const int kStencilBits = 8; // Skia needs 8 stencil bits
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, kStencilBits);
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
|
|
|
|
|
|
|
|
// If you want multisampling, uncomment the below lines and set a sample count
|
|
|
|
static const int kMsaaSampleCount = 0; //4;
|
|
|
|
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
|
|
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, kMsaaSampleCount);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In a real application you might want to initialize more subsystems
|
|
|
|
*/
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
|
|
|
handle_error();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup window
|
|
|
|
// This code will create a window with the same resolution as the user's desktop.
|
|
|
|
SDL_DisplayMode dm;
|
|
|
|
if (SDL_GetDesktopDisplayMode(0, &dm) != 0) {
|
|
|
|
handle_error();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED,
|
|
|
|
SDL_WINDOWPOS_CENTERED, dm.w, dm.h, windowFlags);
|
|
|
|
|
|
|
|
if (!window) {
|
|
|
|
handle_error();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To go fullscreen
|
|
|
|
// SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
|
|
|
|
|
|
|
|
// try and setup a GL context
|
|
|
|
glContext = SDL_GL_CreateContext(window);
|
|
|
|
if (!glContext) {
|
|
|
|
handle_error();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int success = SDL_GL_MakeCurrent(window, glContext);
|
|
|
|
if (success != 0) {
|
|
|
|
handle_error();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:15:02 +00:00
|
|
|
uint32_t windowFormat = SDL_GetWindowPixelFormat(window);
|
|
|
|
int contextType;
|
|
|
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &contextType);
|
|
|
|
|
|
|
|
|
2017-09-01 18:49:09 +00:00
|
|
|
int dw, dh;
|
|
|
|
SDL_GL_GetDrawableSize(window, &dw, &dh);
|
|
|
|
|
|
|
|
glViewport(0, 0, dw, dh);
|
2015-11-13 19:22:19 +00:00
|
|
|
glClearColor(1, 1, 1, 1);
|
|
|
|
glClearStencil(0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
|
|
|
// setup GrContext
|
2017-12-11 15:06:31 +00:00
|
|
|
auto interface = GrGLMakeNativeInterface();
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
// setup contexts
|
2017-12-07 17:33:05 +00:00
|
|
|
sk_sp<GrContext> grContext(GrContext::MakeGL(interface));
|
2015-11-13 19:22:19 +00:00
|
|
|
SkASSERT(grContext);
|
|
|
|
|
|
|
|
// Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
|
|
|
|
// render to it
|
|
|
|
GrGLint buffer;
|
2017-07-11 16:03:01 +00:00
|
|
|
GR_GL_GetIntegerv(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
|
|
|
|
GrGLFramebufferInfo info;
|
|
|
|
info.fFBOID = (GrGLuint) buffer;
|
2017-12-19 18:15:02 +00:00
|
|
|
SkColorType colorType;
|
|
|
|
|
|
|
|
if (SDL_PIXELFORMAT_RGBA8888 == windowFormat) {
|
|
|
|
info.fFormat = GR_GL_RGBA8;
|
|
|
|
colorType = kRGBA_8888_SkColorType;
|
|
|
|
} else {
|
|
|
|
SkASSERT(SDL_PIXELFORMAT_BGRA8888);
|
|
|
|
colorType = kBGRA_8888_SkColorType;
|
|
|
|
if (SDL_GL_CONTEXT_PROFILE_ES == contextType) {
|
|
|
|
info.fFormat = GR_GL_BGRA8;
|
|
|
|
} else {
|
|
|
|
// We assume the internal format is RGBA8 on desktop GL
|
|
|
|
info.fFormat = GR_GL_RGBA8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GrBackendRenderTarget target(dw, dh, kMsaaSampleCount, kStencilBits, info);
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
// setup SkSurface
|
|
|
|
// To use distance field text, use commented out SkSurfaceProps instead
|
|
|
|
// SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
|
|
|
|
// SkSurfaceProps::kLegacyFontHost_InitType);
|
|
|
|
SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
|
2016-08-01 12:53:23 +00:00
|
|
|
|
2017-07-11 16:03:01 +00:00
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target,
|
2017-05-01 13:50:58 +00:00
|
|
|
kBottomLeft_GrSurfaceOrigin,
|
2017-12-19 18:15:02 +00:00
|
|
|
colorType, nullptr, &props));
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2017-09-01 18:49:09 +00:00
|
|
|
canvas->scale((float)dw/dm.w, (float)dh/dm.h);
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
ApplicationState state;
|
|
|
|
|
|
|
|
const char* helpMessage = "Click and drag to create rects. Press esc to quit.";
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// create a surface for CPU rasterization
|
2016-08-04 19:30:31 +00:00
|
|
|
sk_sp<SkSurface> cpuSurface(SkSurface::MakeRaster(canvas->imageInfo()));
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
SkCanvas* offscreen = cpuSurface->getCanvas();
|
|
|
|
offscreen->save();
|
|
|
|
offscreen->translate(50.0f, 50.0f);
|
|
|
|
offscreen->drawPath(create_star(), paint);
|
|
|
|
offscreen->restore();
|
|
|
|
|
2016-08-04 19:30:31 +00:00
|
|
|
sk_sp<SkImage> image = cpuSurface->makeImageSnapshot();
|
2015-11-13 19:22:19 +00:00
|
|
|
|
|
|
|
int rotation = 0;
|
|
|
|
while (!state.fQuit) { // Our application loop
|
|
|
|
SkRandom rand;
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
handle_events(&state, canvas);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawText(helpMessage, strlen(helpMessage), SkIntToScalar(100),
|
|
|
|
SkIntToScalar(100), paint);
|
|
|
|
for (int i = 0; i < state.fRects.count(); i++) {
|
|
|
|
paint.setColor(rand.nextU() | 0x44808080);
|
|
|
|
canvas->drawRect(state.fRects[i], paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw offscreen canvas
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(dm.w / 2.0, dm.h / 2.0);
|
|
|
|
canvas->rotate(rotation++);
|
|
|
|
canvas->drawImage(image, -50.0f, -50.0f);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->flush();
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glContext) {
|
|
|
|
SDL_GL_DeleteContext(glContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Destroy window
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
|
|
|
|
//Quit SDL subsystems
|
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
|
|
}
|