joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GLBench.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 11 | #include "GrGpu.h" |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 12 | #include "GrTest.h" |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 13 | #include "gl/GrGLContext.h" |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | |
| 16 | const GrGLContext* GLBench::getGLContext(SkCanvas* canvas) { |
| 17 | // This bench exclusively tests GL calls directly |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 18 | if (nullptr == canvas->getGrContext()) { |
| 19 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 20 | } |
| 21 | GrContext* context = canvas->getGrContext(); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 22 | GrGpu* gpu = context->getGpu(); |
| 23 | if (!gpu) { |
| 24 | SkDebugf("Couldn't get Gr gpu."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 26 | } |
| 27 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 28 | const GrGLContext* ctx = gpu->glContextForTesting(); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 29 | if (!ctx) { |
| 30 | SkDebugf("Couldn't get an interface\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | return this->onGetGLContext(ctx); |
| 35 | } |
| 36 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 37 | void GLBench::onPreDraw(SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 38 | // This bench exclusively tests GL calls directly |
| 39 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 40 | if (!ctx) { |
| 41 | return; |
| 42 | } |
| 43 | this->setup(ctx); |
| 44 | } |
| 45 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 46 | void GLBench::onPostDraw(SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 47 | // This bench exclusively tests GL calls directly |
| 48 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 49 | if (!ctx) { |
| 50 | return; |
| 51 | } |
| 52 | this->teardown(ctx->interface()); |
| 53 | } |
| 54 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 55 | void GLBench::onDraw(int loops, SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 56 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 57 | if (!ctx) { |
| 58 | return; |
| 59 | } |
| 60 | this->glDraw(loops, ctx); |
brianosman | 393c2ff | 2016-05-11 08:49:59 -0700 | [diff] [blame] | 61 | canvas->getGrContext()->resetContext(); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
jvanverth | 9df16b5 | 2016-10-11 10:03:56 -0700 | [diff] [blame^] | 64 | GrGLuint GLBench::CompileShader(const GrGLInterface* gl, const char* shaderSrc, GrGLenum type) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 65 | GrGLuint shader; |
| 66 | // Create the shader object |
| 67 | GR_GL_CALL_RET(gl, shader, CreateShader(type)); |
| 68 | |
| 69 | // Load the shader source |
jvanverth | 9df16b5 | 2016-10-11 10:03:56 -0700 | [diff] [blame^] | 70 | GR_GL_CALL(gl, ShaderSource(shader, 1, &shaderSrc, nullptr)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 71 | |
| 72 | // Compile the shader |
| 73 | GR_GL_CALL(gl, CompileShader(shader)); |
| 74 | |
| 75 | // Check for compile time errors |
hendrikw | 894a2e4 | 2015-09-08 15:18:56 -0700 | [diff] [blame] | 76 | GrGLint success = GR_GL_INIT_ZERO; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 77 | GrGLchar infoLog[512]; |
| 78 | GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &success)); |
| 79 | if (!success) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | GR_GL_CALL(gl, GetShaderInfoLog(shader, 512, nullptr, infoLog)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 81 | SkDebugf("ERROR::SHADER::COMPLIATION_FAILED: %s\n", infoLog); |
| 82 | } |
| 83 | |
| 84 | return shader; |
| 85 | } |
| 86 | |
jvanverth | 9df16b5 | 2016-10-11 10:03:56 -0700 | [diff] [blame^] | 87 | GrGLuint GLBench::CreateProgram(const GrGLInterface* gl, const char* vshader, const char* fshader) { |
| 88 | |
| 89 | GrGLuint vertexShader = CompileShader(gl, vshader, GR_GL_VERTEX_SHADER); |
| 90 | GrGLuint fragmentShader = CompileShader(gl, fshader, GR_GL_FRAGMENT_SHADER); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 91 | |
| 92 | GrGLuint shaderProgram; |
| 93 | GR_GL_CALL_RET(gl, shaderProgram, CreateProgram()); |
| 94 | GR_GL_CALL(gl, AttachShader(shaderProgram, vertexShader)); |
| 95 | GR_GL_CALL(gl, AttachShader(shaderProgram, fragmentShader)); |
| 96 | GR_GL_CALL(gl, LinkProgram(shaderProgram)); |
| 97 | |
| 98 | // Check for linking errors |
hendrikw | 894a2e4 | 2015-09-08 15:18:56 -0700 | [diff] [blame] | 99 | GrGLint success = GR_GL_INIT_ZERO; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 100 | GrGLchar infoLog[512]; |
| 101 | GR_GL_CALL(gl, GetProgramiv(shaderProgram, GR_GL_LINK_STATUS, &success)); |
| 102 | if (!success) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | GR_GL_CALL(gl, GetProgramInfoLog(shaderProgram, 512, nullptr, infoLog)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 104 | SkDebugf("Linker Error: %s\n", infoLog); |
| 105 | } |
| 106 | GR_GL_CALL(gl, DeleteShader(vertexShader)); |
| 107 | GR_GL_CALL(gl, DeleteShader(fragmentShader)); |
| 108 | |
| 109 | return shaderProgram; |
| 110 | } |
| 111 | |
| 112 | GrGLuint GLBench::SetupFramebuffer(const GrGLInterface* gl, int screenWidth, int screenHeight) { |
| 113 | //Setup framebuffer |
| 114 | GrGLuint texture; |
| 115 | GR_GL_CALL(gl, GenTextures(1, &texture)); |
joshualitt | b235168 | 2015-10-02 06:49:23 -0700 | [diff] [blame] | 116 | GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE7)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 117 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texture)); |
| 118 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); |
| 119 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 120 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); |
| 121 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE)); |
| 122 | GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, |
| 123 | 0, //level |
| 124 | GR_GL_RGBA, //internal format |
| 125 | screenWidth, // width |
| 126 | screenHeight, // height |
| 127 | 0, //border |
| 128 | GR_GL_RGBA, //format |
| 129 | GR_GL_UNSIGNED_BYTE, // type |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | nullptr)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 131 | |
| 132 | // bind framebuffer |
| 133 | GrGLuint framebuffer; |
| 134 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 135 | GR_GL_CALL(gl, GenFramebuffers(1, &framebuffer)); |
| 136 | GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, framebuffer)); |
| 137 | GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 138 | GR_GL_COLOR_ATTACHMENT0, |
| 139 | GR_GL_TEXTURE_2D, |
| 140 | texture, 0)); |
| 141 | GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 142 | GR_GL_CALL(gl, Viewport(0, 0, screenWidth, screenHeight)); |
| 143 | return texture; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void GLBench::DumpImage(const GrGLInterface* gl, uint32_t screenWidth, uint32_t screenHeight, |
| 148 | const char* filename) { |
| 149 | // read back pixels |
| 150 | SkAutoTArray<uint32_t> readback(screenWidth * screenHeight); |
| 151 | GR_GL_CALL(gl, ReadPixels(0, // x |
| 152 | 0, // y |
| 153 | screenWidth, // width |
| 154 | screenHeight, // height |
| 155 | GR_GL_RGBA, //format |
| 156 | GR_GL_UNSIGNED_BYTE, //type |
| 157 | readback.get())); |
| 158 | |
| 159 | // dump png |
| 160 | SkBitmap bm; |
| 161 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(screenWidth, screenHeight))) { |
| 162 | SkDebugf("couldn't allocate bitmap\n"); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | bm.setPixels(readback.get()); |
| 167 | |
| 168 | if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 169 | SkDebugf("------ failed to encode %s\n", filename); |
| 170 | remove(filename); // remove any partial file |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #endif |