Derek Sollenberger | 349bc37 | 2011-01-04 16:14:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | #include "RenderingThread.h" |
| 26 | |
| 27 | #include "ANPOpenGL_npapi.h" |
| 28 | |
| 29 | extern ANPLogInterfaceV0 gLogI; |
| 30 | extern ANPOpenGLInterfaceV0 gOpenGLI; |
| 31 | |
| 32 | RenderingThread::RenderingThread(NPP npp) : android::Thread() { |
| 33 | m_npp = npp; |
| 34 | m_width = -1; |
| 35 | m_height = -1; |
| 36 | gLogI.log(kError_ANPLogType, "Created Rendering Thread"); |
| 37 | } |
| 38 | |
| 39 | android::status_t RenderingThread::readyToRun() { |
| 40 | |
| 41 | gLogI.log(kError_ANPLogType, "in ready to run"); |
| 42 | |
| 43 | EGLContext context = gOpenGLI.acquireContext(m_npp); |
| 44 | |
| 45 | gLogI.log(kError_ANPLogType, "context: %p", context); |
| 46 | |
| 47 | if (context == EGL_NO_CONTEXT) { |
| 48 | gLogI.log(kError_ANPLogType, "Unable to create EGLContext for a TextureProducer thread"); |
| 49 | return android::UNKNOWN_ERROR; |
| 50 | } |
| 51 | return android::NO_ERROR; |
| 52 | } |
| 53 | |
| 54 | void RenderingThread::setDimensions(int width, int height) { |
| 55 | android::Mutex::Autolock lock(m_sync); |
| 56 | m_width = width; |
| 57 | m_height = height; |
| 58 | } |
| 59 | |
| 60 | void RenderingThread::getDimensions(int& width, int& height) { |
| 61 | android::Mutex::Autolock lock(m_sync); |
| 62 | width = m_width; |
| 63 | height = m_height; |
| 64 | } |
| 65 | |
| 66 | void RenderingThread::printGLString(const char *name, GLenum s) { |
| 67 | const char *v = (const char *) glGetString(s); |
| 68 | gLogI.log(kError_ANPLogType, "GL %s = %s\n", name, v); |
| 69 | } |
| 70 | |
| 71 | void RenderingThread::checkGlError(const char* op) { |
| 72 | for (GLint error = glGetError(); error; error |
| 73 | = glGetError()) { |
| 74 | gLogI.log(kError_ANPLogType, "after %s() glError (0x%x)\n", op, error); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | GLenum RenderingThread::getInternalFormat(SkBitmap::Config config) |
| 79 | { |
| 80 | switch(config) { |
| 81 | case SkBitmap::kA8_Config: |
| 82 | return GL_ALPHA; |
| 83 | case SkBitmap::kARGB_4444_Config: |
| 84 | return GL_RGBA; |
| 85 | case SkBitmap::kARGB_8888_Config: |
| 86 | return GL_RGBA; |
| 87 | case SkBitmap::kRGB_565_Config: |
| 88 | return GL_RGB; |
| 89 | default: |
| 90 | return -1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | GLenum RenderingThread::getType(SkBitmap::Config config) |
| 95 | { |
| 96 | switch(config) { |
| 97 | case SkBitmap::kA8_Config: |
| 98 | return GL_UNSIGNED_BYTE; |
| 99 | case SkBitmap::kARGB_4444_Config: |
| 100 | return GL_UNSIGNED_SHORT_4_4_4_4; |
| 101 | case SkBitmap::kARGB_8888_Config: |
| 102 | return GL_UNSIGNED_BYTE; |
| 103 | case SkBitmap::kIndex8_Config: |
| 104 | return -1; // No type for compressed data. |
| 105 | case SkBitmap::kRGB_565_Config: |
| 106 | return GL_UNSIGNED_SHORT_5_6_5; |
| 107 | default: |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void RenderingThread::createTextureWithBitmap(GLuint texture, SkBitmap& bitmap) { |
| 113 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 114 | glBindTexture(GL_TEXTURE_2D, texture); |
| 115 | checkGlError("glBindTexture"); |
| 116 | SkBitmap::Config config = bitmap.getConfig(); |
| 117 | int internalformat = getInternalFormat(config); |
| 118 | int type = getType(config); |
| 119 | bitmap.lockPixels(); |
| 120 | glTexImage2D(GL_TEXTURE_2D, 0, internalformat, bitmap.width(), bitmap.height(), |
| 121 | 0, internalformat, type, bitmap.getPixels()); |
| 122 | bitmap.unlockPixels(); |
| 123 | checkGlError("glTexImage2D"); |
| 124 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 125 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 126 | } |
| 127 | |
| 128 | void RenderingThread::updateTextureWithBitmap(GLuint texture, SkBitmap& bitmap) { |
| 129 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 130 | glBindTexture(GL_TEXTURE_2D, texture); |
| 131 | checkGlError("glBindTexture"); |
| 132 | SkBitmap::Config config = bitmap.getConfig(); |
| 133 | int internalformat = getInternalFormat(config); |
| 134 | int type = getType(config); |
| 135 | bitmap.lockPixels(); |
| 136 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.width(), bitmap.height(), |
| 137 | internalformat, type, bitmap.getPixels()); |
| 138 | bitmap.unlockPixels(); |
| 139 | checkGlError("glTexSubImage2D"); |
| 140 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 141 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 142 | } |