Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLES Mid-Execution Capture #2016

Merged
merged 7 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions gapic/src/main/com/google/gapid/views/TracerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ private abstract static class SharedTraceInput extends Composite {
private static final String DEFAULT_TRACE_FILE = "trace";
private static final String TRACE_EXTENSION = ".gfxtrace";
private static final DateFormat TRACE_DATE_FORMAT = new SimpleDateFormat("_yyyyMMdd_HHmm");
protected static final String MEC_LABEL = "Trace From Beginning";

private final String date = TRACE_DATE_FORMAT.format(new Date());
protected final ComboViewer api;
Expand Down Expand Up @@ -302,7 +303,7 @@ protected void configureDialog(DirectoryDialog dialog) {

createLabel(this, "");
fromBeginning = withLayoutData(
createCheckbox(this, "Trace From Beginning", !models.settings.traceMidExecution),
createCheckbox(this, MEC_LABEL, !models.settings.traceMidExecution),
new GridData(SWT.FILL, SWT.FILL, true, false));

createLabel(this, "");
Expand Down Expand Up @@ -377,6 +378,8 @@ private File getOutputFile() {
}

private static class AndroidInput extends SharedTraceInput {
private static final String MEC_WARNING = "(mid-execution capture for GLES is experimental)";

private final Runnable refreshDevices;
private ComboViewer device;
private LoadingIndicator.Widget deviceLoader;
Expand Down Expand Up @@ -440,16 +443,16 @@ public AndroidInput(
traceTarget.addBoxListener(SWT.Modify, targetListener);
targetListener.handleEvent(null);

Listener apiListener = e -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we confident enough marking this as "works" for GLES, or do we want to mark it as experimental?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because as soon as the button works, people will start sending bugs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (getSelectedApi() == Tracer.Api.Vulkan) {
fromBeginning.setEnabled(true);
Listener mecListener = e -> {
if (getSelectedApi() == Tracer.Api.Vulkan || fromBeginning.getSelection()) {
fromBeginning.setText(MEC_LABEL);
} else {
fromBeginning.setEnabled(false);
fromBeginning.setSelection(true);
fromBeginning.setText(MEC_LABEL + " " + MEC_WARNING);
}
};
api.getCombo().addListener(SWT.Selection, apiListener);
apiListener.handleEvent(null);
api.getCombo().addListener(SWT.Selection, mecListener);
fromBeginning.addListener(SWT.Selection, mecListener);
mecListener.handleEvent(null);

disablePcs.addListener(
SWT.Selection, e -> pcsWarning.setVisible(!disablePcs.getSelection()));
Expand Down
192 changes: 0 additions & 192 deletions gapii/cc/gles_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,198 +687,6 @@ bool GlesSpy::getFramebufferAttachmentSize(CallObserver* observer,
return false;
}

static bool ReadExternalPixels(GlesImports& imports, EGLImageKHR img,
GLsizei width, GLsizei height,
std::vector<uint8_t>* data) {
using namespace GLenum;

const char* vsSource =
"precision highp float;\n"
"attribute vec2 position;\n"
"varying vec2 texcoord;\n"
"void main() {\n"
" gl_Position = vec4(position, 0.5, 1.0);\n"
" texcoord = position * vec2(0.5) + vec2(0.5);\n"
"}\n";

const char* fsSource =
"#extension GL_OES_EGL_image_external : require\n"
"precision highp float;\n"
"uniform samplerExternalOES tex;\n"
"varying vec2 texcoord;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, texcoord);\n"
"}\n";

GLint err;
auto prog = imports.glCreateProgram();

auto vs = imports.glCreateShader(GL_VERTEX_SHADER);
imports.glShaderSource(vs, 1, &vsSource, nullptr);
imports.glCompileShader(vs);
imports.glAttachShader(prog, vs);

auto fs = imports.glCreateShader(GL_FRAGMENT_SHADER);
imports.glShaderSource(fs, 1, &fsSource, nullptr);
imports.glCompileShader(fs);
imports.glAttachShader(prog, fs);

imports.glBindAttribLocation(prog, 0, "position");
imports.glLinkProgram(prog);

if ((err = imports.glGetError()) != 0) {
GAPID_ERROR("ReadExternalPixels: Failed to create program: 0x%X", err);
return false;
}

GLint linkStatus = 0;
imports.glGetProgramiv(prog, GL_LINK_STATUS, &linkStatus);
if (linkStatus == 0) {
char log[1024];
imports.glGetProgramInfoLog(prog, sizeof(log), nullptr, log);
GAPID_ERROR("ReadExternalPixels: Failed to compile program:\n%s", log);
return false;
}

GLuint srcTex = 0;
imports.glGenTextures(1, &srcTex);
imports.glBindTexture(GL_TEXTURE_EXTERNAL_OES, srcTex);
imports.glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, img);
imports.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
imports.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);

GLuint dstTex = 0;
imports.glGenTextures(1, &dstTex);
imports.glBindTexture(GL_TEXTURE_2D, dstTex);
imports.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);

if ((err = imports.glGetError()) != 0) {
GAPID_ERROR("ReadExternalPixels: Failed to create texture: 0x%X", err);
return false;
}

GLuint fb = 0;
imports.glGenFramebuffers(1, &fb);
imports.glBindFramebuffer(GL_FRAMEBUFFER, fb);
imports.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, dstTex, 0);

if ((err = imports.glGetError()) != 0) {
GAPID_ERROR("ReadExternalPixels: Failed to create framebuffer: 0x%X", err);
return false;
}
if ((err = imports.glCheckFramebufferStatus(GL_FRAMEBUFFER)) !=
GL_FRAMEBUFFER_COMPLETE) {
GAPID_ERROR("ReadExternalPixels: Framebuffer incomplete: 0x%X", err);
return false;
}

imports.glDisable(GL_CULL_FACE);
imports.glDisable(GL_DEPTH_TEST);
imports.glViewport(0, 0, width, height);
imports.glClearColor(0.0, 0.0, 0.0, 0.0);
imports.glClear(GLbitfield::GL_COLOR_BUFFER_BIT);
imports.glUseProgram(prog);
GLfloat vb[] = {
-1.0f, +1.0f, // 2--4
-1.0f, -1.0f, // |\ |
+1.0f, +1.0f, // | \|
+1.0f, -1.0f, // 1--3
};
imports.glEnableVertexAttribArray(0);
imports.glVertexAttribPointer(0, 2, GL_FLOAT, 0, 0, vb);
imports.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
if ((err = imports.glGetError()) != 0) {
GAPID_ERROR("ReadExternalPixels: Failed to draw quad: 0x%X", err);
return false;
}

data->resize(width * height * 4);
imports.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
data->data());
if ((err = imports.glGetError()) != 0) {
GAPID_ERROR("ReadExternalPixels: Failed to read pixels: 0x%X", err);
return false;
}

return true;
}

void GlesSpy::GetEGLImageData(CallObserver* observer, EGLImageKHR img,
GLsizei width, GLsizei height) {
using namespace EGLenum;

if (!should_trace(kApiIndex)) {
return;
}

GAPID_DEBUG("Get EGLImage data: 0x%p x%xx%x", img, width, height);

// Save old state.
auto display = mImports.eglGetCurrentDisplay();
auto draw = mImports.eglGetCurrentSurface(EGL_DRAW);
auto read = mImports.eglGetCurrentSurface(EGL_READ);
auto oldCtx = mImports.eglGetCurrentContext();

// Find an EGL config.
EGLConfig cfg;
EGLint cfgAttribs[] = {EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE};
int one = 1;
if (mImports.eglChooseConfig(display, cfgAttribs, &cfg, 1, &one) ==
EGL_FALSE ||
one != 1) {
GAPID_ERROR("Failed to choose EGL config");
return;
}

// Create an EGL context.
EGLContext ctx;
EGLint ctxAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
if ((ctx = mImports.eglCreateContext(display, cfg, nullptr, ctxAttribs)) ==
nullptr) {
GAPID_ERROR("Failed to create EGL context");
return;
}

// Create an EGL surface.
EGLSurface surface;
EGLint surfaceAttribs[] = {EGL_WIDTH, 16, EGL_HEIGHT, 16, EGL_NONE};
if ((surface = mImports.eglCreatePbufferSurface(display, cfg,
surfaceAttribs)) == nullptr) {
GAPID_ERROR("Failed to create EGL surface");
return;
}

// Bind the EGL context.
if (mImports.eglMakeCurrent(display, surface, surface, ctx) == EGL_FALSE) {
GAPID_ERROR("Failed to bind new EGL context");
return;
}

std::vector<uint8_t> data;
if (ReadExternalPixels(mImports, img, width, height, &data)) {
auto resIndex = sendResource(kApiIndex, data.data(), data.size());
auto extra = new gles_pb::EGLImageData();
extra->set_res_index(resIndex);
extra->set_size(data.size());
extra->set_width(width);
extra->set_height(height);
extra->set_format(GLenum::GL_RGBA);
extra->set_type(GLenum::GL_UNSIGNED_BYTE);
observer->encodeAndDelete(extra);
}

if (mImports.eglMakeCurrent(display, draw, read, oldCtx) == EGL_FALSE) {
GAPID_FATAL("Failed to restore old EGL context");
}

mImports.eglDestroySurface(display, surface);
mImports.eglDestroyContext(display, ctx);
}

bool GlesSpy::observeFramebuffer(CallObserver* observer, uint32_t* w,
uint32_t* h, std::vector<uint8_t>* data) {
if (!getFramebufferAttachmentSize(observer, w, h)) {
Expand Down
Loading