ColorBuffer.cpp 10.5 KB
Newer Older
S
Simon Fels 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

17 18 19 20 21
#include "anbox/graphics/emugl/ColorBuffer.h"
#include "anbox/graphics/emugl/DispatchTables.h"
#include "anbox/graphics/emugl/RenderThreadInfo.h"
#include "anbox/graphics/emugl/TextureDraw.h"
#include "anbox/graphics/emugl/TextureResize.h"
22
#include "anbox/logger.h"
S
Simon Fels 已提交
23

24 25
#include "external/android-emugl/host/include/OpenGLESDispatch/EGLDispatch.h"

S
Simon Fels 已提交
26 27 28 29 30 31 32 33
#include <stdio.h>

namespace {

// <EGL/egl.h> defines many types as 'void*' while they're really
// implemented as unsigned integers. These convenience template functions
// help casting between them safely without generating compiler warnings.
inline void* SafePointerFromUInt(unsigned int handle) {
T
Thomas Voß 已提交
34
  return reinterpret_cast<void*>(static_cast<uintptr_t>(handle));
S
Simon Fels 已提交
35 36 37 38 39 40 41 42
}

// Lazily create and bind a framebuffer object to the current host context.
// |fbo| is the address of the framebuffer object name.
// |tex| is the name of a texture that is attached to the framebuffer object
// on creation only. I.e. all rendering operations will target it.
// returns true in case of success, false on failure.
bool bindFbo(GLuint* fbo, GLuint tex) {
43 44
  if (*fbo) {
    // fbo already exist - just bind
S
Simon Fels 已提交
45 46
    s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
    return true;
47 48 49 50 51 52 53 54
  }

  s_gles2.glGenFramebuffers(1, fbo);
  s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
  s_gles2.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_OES,
                                 GL_TEXTURE_2D, tex, 0);
  GLenum status = s_gles2.glCheckFramebufferStatus(GL_FRAMEBUFFER);
  if (status != GL_FRAMEBUFFER_COMPLETE_OES) {
55
    ERROR("FBO not complete: %#x", status);
S
Simon Fels 已提交
56
    s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, 0);
57 58 59 60 61
    s_gles2.glDeleteFramebuffers(1, fbo);
    *fbo = 0;
    return false;
  }
  return true;
S
Simon Fels 已提交
62 63
}

64 65
void unbindFbo() { s_gles2.glBindFramebuffer(GL_FRAMEBUFFER, 0); }

S
Simon Fels 已提交
66 67 68 69 70 71 72 73 74 75 76 77
// Helper class to use a ColorBuffer::Helper context.
// Usage is pretty simple:
//
//     {
//        ScopedHelperContext context(m_helper);
//        if (!context.isOk()) {
//            return false;   // something bad happened.
//        }
//        .... do something ....
//     }   // automatically calls m_helper->teardownContext();
//
class ScopedHelperContext {
78 79 80 81
 public:
  ScopedHelperContext(ColorBuffer::Helper* helper) : mHelper(helper) {
    if (!helper->setupContext()) {
      mHelper = NULL;
S
Simon Fels 已提交
82
    }
83
  }
S
Simon Fels 已提交
84

85
  bool isOk() const { return mHelper != NULL; }
S
Simon Fels 已提交
86

87
  ~ScopedHelperContext() { release(); }
S
Simon Fels 已提交
88

89 90 91 92
  void release() {
    if (mHelper) {
      mHelper->teardownContext();
      mHelper = NULL;
S
Simon Fels 已提交
93
    }
94 95 96 97
  }

 private:
  ColorBuffer::Helper* mHelper;
S
Simon Fels 已提交
98 99 100 101 102
};

}  // namespace

// static
103 104
ColorBuffer* ColorBuffer::create(EGLDisplay p_display, int p_width,
                                 int p_height, GLenum p_internalFormat,
105
                                 bool has_eglimage_texture_2d, Helper* helper, HandleType hndl) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  GLenum texInternalFormat = 0;

  switch (p_internalFormat) {
    case GL_RGB:
    case GL_RGB565_OES:
      texInternalFormat = GL_RGB;
      break;

    case GL_RGBA:
    case GL_RGB5_A1_OES:
    case GL_RGBA4_OES:
      texInternalFormat = GL_RGBA;
      break;

    default:
      return NULL;
      break;
  }

  ScopedHelperContext context(helper);
  if (!context.isOk()) {
    return NULL;
  }

130
  ColorBuffer* cb = new ColorBuffer(p_display, helper, hndl);
W
wankanzhen 已提交
131 132 133
  if (!cb) {
    return NULL;
  }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  s_gles2.glGenTextures(1, &cb->m_tex);
  s_gles2.glBindTexture(GL_TEXTURE_2D, cb->m_tex);

  int nComp = (texInternalFormat == GL_RGB ? 3 : 4);

  char* zBuff = static_cast<char*>(::calloc(nComp * p_width * p_height, 1));
  s_gles2.glTexImage2D(GL_TEXTURE_2D, 0, texInternalFormat, p_width, p_height,
                       0, texInternalFormat, GL_UNSIGNED_BYTE, zBuff);
  ::free(zBuff);

  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  //
  // create another texture for that colorbuffer for blit
  //
  s_gles2.glGenTextures(1, &cb->m_blitTex);
  s_gles2.glBindTexture(GL_TEXTURE_2D, cb->m_blitTex);
  s_gles2.glTexImage2D(GL_TEXTURE_2D, 0, texInternalFormat, p_width, p_height,
                       0, texInternalFormat, GL_UNSIGNED_BYTE, NULL);

  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  s_gles2.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  cb->m_width = p_width;
  cb->m_height = p_height;
  cb->m_internalFormat = texInternalFormat;

  if (has_eglimage_texture_2d) {
    cb->m_eglImage = s_egl.eglCreateImageKHR(
        p_display, s_egl.eglGetCurrentContext(), EGL_GL_TEXTURE_2D_KHR,
T
Thomas Voß 已提交
170
        reinterpret_cast<EGLClientBuffer>(SafePointerFromUInt(cb->m_tex)), NULL);
171 172 173

    cb->m_blitEGLImage = s_egl.eglCreateImageKHR(
        p_display, s_egl.eglGetCurrentContext(), EGL_GL_TEXTURE_2D_KHR,
T
Thomas Voß 已提交
174
        reinterpret_cast<EGLClientBuffer>(SafePointerFromUInt(cb->m_blitTex)), NULL);
175 176 177
  }

  cb->m_resizer = new TextureResize(p_width, p_height);
W
wankanzhen 已提交
178 179 180
  if (!(cb->m_resizer)) {
    return NULL;
  }
181 182

  return cb;
S
Simon Fels 已提交
183 184
}

185
ColorBuffer::ColorBuffer(EGLDisplay display, Helper* helper, HandleType hndl)
186 187 188 189 190 191 192
    : m_tex(0),
      m_blitTex(0),
      m_eglImage(NULL),
      m_blitEGLImage(NULL),
      m_fbo(0),
      m_internalFormat(0),
      m_display(display),
193 194
      m_helper(helper),
      mHndl(hndl) {}
S
Simon Fels 已提交
195 196

ColorBuffer::~ColorBuffer() {
197
  ScopedHelperContext context(m_helper);
S
Simon Fels 已提交
198

199 200 201 202 203 204
  if (m_blitEGLImage) {
    s_egl.eglDestroyImageKHR(m_display, m_blitEGLImage);
  }
  if (m_eglImage) {
    s_egl.eglDestroyImageKHR(m_display, m_eglImage);
  }
S
Simon Fels 已提交
205

206 207 208
  if (m_fbo) {
    s_gles2.glDeleteFramebuffers(1, &m_fbo);
  }
S
Simon Fels 已提交
209

210 211
  GLuint tex[2] = {m_tex, m_blitTex};
  s_gles2.glDeleteTextures(2, tex);
S
Simon Fels 已提交
212

213
  delete m_resizer;
S
Simon Fels 已提交
214 215
}

216 217 218 219
HandleType ColorBuffer::getHndl() const {
    return mHndl;
}

220 221 222 223 224 225
void ColorBuffer::readPixels(int x, int y, int width, int height,
                             GLenum p_format, GLenum p_type, void* pixels) {
  ScopedHelperContext context(m_helper);
  if (!context.isOk()) {
    return;
  }
S
Simon Fels 已提交
226

227 228 229 230
  if (bindFbo(&m_fbo, m_tex)) {
    s_gles2.glReadPixels(x, y, width, height, p_format, p_type, pixels);
    unbindFbo();
  }
S
Simon Fels 已提交
231 232
}

233 234 235 236 237 238 239 240 241 242 243
void ColorBuffer::subUpdate(int x, int y, int width, int height,
                            GLenum p_format, GLenum p_type, void* pixels) {
  ScopedHelperContext context(m_helper);
  if (!context.isOk()) {
    return;
  }

  s_gles2.glBindTexture(GL_TEXTURE_2D, m_tex);
  s_gles2.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  s_gles2.glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, p_format,
                          p_type, pixels);
S
Simon Fels 已提交
244 245
}

246 247
bool ColorBuffer::blitFromCurrentReadBuffer() {
  RenderThreadInfo* tInfo = RenderThreadInfo::get();
248 249 250
  if (!tInfo) {
    return false;
  }
251
  if (!tInfo->currContext) {
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    // no Current context
    return false;
  }

  // Copy the content of the current read surface into m_blitEGLImage.
  // This is done by creating a temporary texture, bind it to the EGLImage
  // then call glCopyTexSubImage2D().
  GLuint tmpTex;
  GLint currTexBind;
  if (tInfo->currContext->isGL2()) {
    s_gles2.glGetIntegerv(GL_TEXTURE_BINDING_2D, &currTexBind);
    s_gles2.glGenTextures(1, &tmpTex);
    s_gles2.glBindTexture(GL_TEXTURE_2D, tmpTex);
    s_gles2.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_blitEGLImage);
    s_gles2.glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width,
                                m_height);
    s_gles2.glDeleteTextures(1, &tmpTex);
    s_gles2.glBindTexture(GL_TEXTURE_2D, currTexBind);
  } else {
    s_gles1.glGetIntegerv(GL_TEXTURE_BINDING_2D, &currTexBind);
    s_gles1.glGenTextures(1, &tmpTex);
    s_gles1.glBindTexture(GL_TEXTURE_2D, tmpTex);
    s_gles1.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_blitEGLImage);
    s_gles1.glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width,
                                m_height);
    s_gles1.glDeleteTextures(1, &tmpTex);
    s_gles1.glBindTexture(GL_TEXTURE_2D, currTexBind);
  }

  ScopedHelperContext context(m_helper);
  if (!context.isOk()) {
    return false;
  }

  if (!bindFbo(&m_fbo, m_tex)) {
    return false;
  }

  // Save current viewport and match it to the current colorbuffer size.
  GLint vport[4] = {
      0,
  };
  s_gles2.glGetIntegerv(GL_VIEWPORT, vport);
  s_gles2.glViewport(0, 0, m_width, m_height);

  // render m_blitTex
  m_helper->getTextureDraw()->draw(m_blitTex);

  // Restore previous viewport.
  s_gles2.glViewport(vport[0], vport[1], vport[2], vport[3]);
  unbindFbo();

  return true;
S
Simon Fels 已提交
305 306 307
}

bool ColorBuffer::bindToTexture() {
308 309 310 311
  if (!m_eglImage) {
    return false;
  }
  RenderThreadInfo* tInfo = RenderThreadInfo::get();
312 313 314
  if (!tInfo) {
    return false;
  }
315
  if (!tInfo->currContext) {
316 317 318 319 320 321 322 323
    return false;
  }
  if (tInfo->currContext->isGL2()) {
    s_gles2.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_eglImage);
  } else {
    s_gles1.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_eglImage);
  }
  return true;
S
Simon Fels 已提交
324 325 326
}

bool ColorBuffer::bindToRenderbuffer() {
327 328 329 330
  if (!m_eglImage) {
    return false;
  }
  RenderThreadInfo* tInfo = RenderThreadInfo::get();
W
wankanzhen 已提交
331 332 333
  if (!tInfo) {
    return false;
  }
334
  if (!tInfo->currContext) {
335 336 337 338 339 340 341 342 343 344
    return false;
  }
  if (tInfo->currContext->isGL2()) {
    s_gles2.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER_OES,
                                                   m_eglImage);
  } else {
    s_gles1.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER_OES,
                                                   m_eglImage);
  }
  return true;
S
Simon Fels 已提交
345 346 347
}

void ColorBuffer::readback(unsigned char* img) {
348 349 350 351 352 353 354 355 356
  ScopedHelperContext context(m_helper);
  if (!context.isOk()) {
    return;
  }
  if (bindFbo(&m_fbo, m_tex)) {
    s_gles2.glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE,
                         img);
    unbindFbo();
  }
S
Simon Fels 已提交
357
}
358 359

void ColorBuffer::bind() {
360 361
  const auto id = m_resizer->update(m_tex);
  s_gles2.glBindTexture(GL_TEXTURE_2D, id);
362
}