2015-07-01 19:39:07 +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 "GLBench.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2016-05-20 18:14:33 +00:00
|
|
|
#include "GrGpu.h"
|
2015-07-01 19:39:07 +00:00
|
|
|
#include "GrTest.h"
|
2016-05-20 18:14:33 +00:00
|
|
|
#include "gl/GrGLContext.h"
|
2016-10-12 13:39:56 +00:00
|
|
|
#include "gl/builders/GrGLShaderStringBuilder.h"
|
|
|
|
#include "SkSLCompiler.h"
|
2015-07-01 19:39:07 +00:00
|
|
|
#include <stdio.h>
|
2016-10-12 13:39:56 +00:00
|
|
|
#include <string>
|
2015-07-01 19:39:07 +00:00
|
|
|
|
|
|
|
const GrGLContext* GLBench::getGLContext(SkCanvas* canvas) {
|
|
|
|
// This bench exclusively tests GL calls directly
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == canvas->getGrContext()) {
|
|
|
|
return nullptr;
|
2015-07-01 19:39:07 +00:00
|
|
|
}
|
|
|
|
GrContext* context = canvas->getGrContext();
|
2015-07-13 14:19:57 +00:00
|
|
|
GrGpu* gpu = context->getGpu();
|
|
|
|
if (!gpu) {
|
|
|
|
SkDebugf("Couldn't get Gr gpu.");
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-07-01 19:39:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 14:19:57 +00:00
|
|
|
const GrGLContext* ctx = gpu->glContextForTesting();
|
2015-07-01 19:39:07 +00:00
|
|
|
if (!ctx) {
|
|
|
|
SkDebugf("Couldn't get an interface\n");
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-07-01 19:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this->onGetGLContext(ctx);
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void GLBench::onPreDraw(SkCanvas* canvas) {
|
2015-07-01 19:39:07 +00:00
|
|
|
// This bench exclusively tests GL calls directly
|
|
|
|
const GrGLContext* ctx = this->getGLContext(canvas);
|
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->setup(ctx);
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void GLBench::onPostDraw(SkCanvas* canvas) {
|
2015-07-01 19:39:07 +00:00
|
|
|
// This bench exclusively tests GL calls directly
|
|
|
|
const GrGLContext* ctx = this->getGLContext(canvas);
|
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->teardown(ctx->interface());
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void GLBench::onDraw(int loops, SkCanvas* canvas) {
|
2015-07-01 19:39:07 +00:00
|
|
|
const GrGLContext* ctx = this->getGLContext(canvas);
|
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->glDraw(loops, ctx);
|
2016-05-11 15:49:59 +00:00
|
|
|
canvas->getGrContext()->resetContext();
|
2015-07-01 19:39:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, GrGLenum type) {
|
|
|
|
const GrGLInterface* gl = context->interface();
|
|
|
|
std::string glsl;
|
|
|
|
bool result = context->compiler()->toGLSL(type == GR_GL_VERTEX_SHADER
|
|
|
|
? SkSL::Program::kVertex_Kind
|
|
|
|
: SkSL::Program::kFragment_Kind,
|
|
|
|
std::string(sksl),
|
2016-11-11 20:16:46 +00:00
|
|
|
*context->caps()->glslCaps(),
|
2016-10-12 13:39:56 +00:00
|
|
|
&glsl);
|
|
|
|
if (!result) {
|
|
|
|
SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl,
|
|
|
|
context->compiler()->errorText().c_str());
|
|
|
|
}
|
2015-07-01 19:39:07 +00:00
|
|
|
GrGLuint shader;
|
|
|
|
// Create the shader object
|
|
|
|
GR_GL_CALL_RET(gl, shader, CreateShader(type));
|
|
|
|
|
|
|
|
// Load the shader source
|
2016-10-12 13:39:56 +00:00
|
|
|
const char* glslPtr = glsl.c_str();
|
|
|
|
GR_GL_CALL(gl, ShaderSource(shader, 1, (const char**) &glslPtr, nullptr));
|
2015-07-01 19:39:07 +00:00
|
|
|
|
|
|
|
// Compile the shader
|
|
|
|
GR_GL_CALL(gl, CompileShader(shader));
|
|
|
|
|
|
|
|
// Check for compile time errors
|
2015-09-08 22:18:56 +00:00
|
|
|
GrGLint success = GR_GL_INIT_ZERO;
|
2015-07-01 19:39:07 +00:00
|
|
|
GrGLchar infoLog[512];
|
|
|
|
GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &success));
|
|
|
|
if (!success) {
|
2015-08-27 14:41:13 +00:00
|
|
|
GR_GL_CALL(gl, GetShaderInfoLog(shader, 512, nullptr, infoLog));
|
2015-07-01 19:39:07 +00:00
|
|
|
SkDebugf("ERROR::SHADER::COMPLIATION_FAILED: %s\n", infoLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
GrGLuint GLBench::CreateProgram(const GrGLContext* context, const char* vshader,
|
|
|
|
const char* fshader) {
|
|
|
|
const GrGLInterface* gl = context->interface();
|
|
|
|
GrGLuint vertexShader = CompileShader(context, vshader, GR_GL_VERTEX_SHADER);
|
|
|
|
GrGLuint fragmentShader = CompileShader(context, fshader, GR_GL_FRAGMENT_SHADER);
|
2015-07-01 19:39:07 +00:00
|
|
|
|
|
|
|
GrGLuint shaderProgram;
|
|
|
|
GR_GL_CALL_RET(gl, shaderProgram, CreateProgram());
|
|
|
|
GR_GL_CALL(gl, AttachShader(shaderProgram, vertexShader));
|
|
|
|
GR_GL_CALL(gl, AttachShader(shaderProgram, fragmentShader));
|
|
|
|
GR_GL_CALL(gl, LinkProgram(shaderProgram));
|
|
|
|
|
|
|
|
// Check for linking errors
|
2015-09-08 22:18:56 +00:00
|
|
|
GrGLint success = GR_GL_INIT_ZERO;
|
2015-07-01 19:39:07 +00:00
|
|
|
GrGLchar infoLog[512];
|
|
|
|
GR_GL_CALL(gl, GetProgramiv(shaderProgram, GR_GL_LINK_STATUS, &success));
|
|
|
|
if (!success) {
|
2015-08-27 14:41:13 +00:00
|
|
|
GR_GL_CALL(gl, GetProgramInfoLog(shaderProgram, 512, nullptr, infoLog));
|
2015-07-01 19:39:07 +00:00
|
|
|
SkDebugf("Linker Error: %s\n", infoLog);
|
|
|
|
}
|
|
|
|
GR_GL_CALL(gl, DeleteShader(vertexShader));
|
|
|
|
GR_GL_CALL(gl, DeleteShader(fragmentShader));
|
|
|
|
|
|
|
|
return shaderProgram;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrGLuint GLBench::SetupFramebuffer(const GrGLInterface* gl, int screenWidth, int screenHeight) {
|
|
|
|
//Setup framebuffer
|
|
|
|
GrGLuint texture;
|
|
|
|
GR_GL_CALL(gl, GenTextures(1, &texture));
|
2015-10-02 13:49:23 +00:00
|
|
|
GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE7));
|
2015-07-01 19:39:07 +00:00
|
|
|
GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texture));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
|
|
|
|
GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
|
|
|
|
GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D,
|
|
|
|
0, //level
|
|
|
|
GR_GL_RGBA, //internal format
|
|
|
|
screenWidth, // width
|
|
|
|
screenHeight, // height
|
|
|
|
0, //border
|
|
|
|
GR_GL_RGBA, //format
|
|
|
|
GR_GL_UNSIGNED_BYTE, // type
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr));
|
2015-07-01 19:39:07 +00:00
|
|
|
|
|
|
|
// bind framebuffer
|
|
|
|
GrGLuint framebuffer;
|
|
|
|
GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
|
|
|
|
GR_GL_CALL(gl, GenFramebuffers(1, &framebuffer));
|
|
|
|
GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, framebuffer));
|
|
|
|
GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
|
|
|
|
GR_GL_COLOR_ATTACHMENT0,
|
|
|
|
GR_GL_TEXTURE_2D,
|
|
|
|
texture, 0));
|
|
|
|
GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
|
|
|
|
GR_GL_CALL(gl, Viewport(0, 0, screenWidth, screenHeight));
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GLBench::DumpImage(const GrGLInterface* gl, uint32_t screenWidth, uint32_t screenHeight,
|
|
|
|
const char* filename) {
|
|
|
|
// read back pixels
|
|
|
|
SkAutoTArray<uint32_t> readback(screenWidth * screenHeight);
|
|
|
|
GR_GL_CALL(gl, ReadPixels(0, // x
|
|
|
|
0, // y
|
|
|
|
screenWidth, // width
|
|
|
|
screenHeight, // height
|
|
|
|
GR_GL_RGBA, //format
|
|
|
|
GR_GL_UNSIGNED_BYTE, //type
|
|
|
|
readback.get()));
|
|
|
|
|
|
|
|
// dump png
|
|
|
|
SkBitmap bm;
|
|
|
|
if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(screenWidth, screenHeight))) {
|
|
|
|
SkDebugf("couldn't allocate bitmap\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bm.setPixels(readback.get());
|
|
|
|
|
|
|
|
if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
|
|
|
|
SkDebugf("------ failed to encode %s\n", filename);
|
|
|
|
remove(filename); // remove any partial file
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|