diff options
Diffstat (limited to 'libs')
155 files changed, 15417 insertions, 4695 deletions
diff --git a/libs/binder/Android.mk b/libs/binder/Android.mk index 13dc500..f9d9f25 100644 --- a/libs/binder/Android.mk +++ b/libs/binder/Android.mk @@ -16,6 +16,7 @@ sources := \ Binder.cpp \ BpBinder.cpp \ + CursorWindow.cpp \ IInterface.cpp \ IMemory.cpp \ IPCThreadState.cpp \ diff --git a/libs/binder/CursorWindow.cpp b/libs/binder/CursorWindow.cpp new file mode 100644 index 0000000..20b27c9 --- /dev/null +++ b/libs/binder/CursorWindow.cpp @@ -0,0 +1,408 @@ +/* + * Copyright (C) 2006-2007 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. + */ + +#undef LOG_TAG +#define LOG_TAG "CursorWindow" + +#include <utils/Log.h> +#include <binder/CursorWindow.h> +#include <binder/MemoryHeapBase.h> +#include <binder/MemoryBase.h> + +#include <assert.h> +#include <string.h> +#include <stdlib.h> + +namespace android { + +CursorWindow::CursorWindow(size_t maxSize) : + mMaxSize(maxSize) +{ +} + +bool CursorWindow::setMemory(const sp<IMemory>& memory) +{ + mMemory = memory; + mData = (uint8_t *) memory->pointer(); + if (mData == NULL) { + return false; + } + mHeader = (window_header_t *) mData; + + // Make the window read-only + ssize_t size = memory->size(); + mSize = size; + mMaxSize = size; + mFreeOffset = size; +LOG_WINDOW("Created CursorWindow from existing IMemory: mFreeOffset = %d, numRows = %d, numColumns = %d, mSize = %d, mMaxSize = %d, mData = %p", mFreeOffset, mHeader->numRows, mHeader->numColumns, mSize, mMaxSize, mData); + return true; +} + +bool CursorWindow::initBuffer(bool localOnly) +{ + //TODO Use a non-memory dealer mmap region for localOnly + + sp<MemoryHeapBase> heap; + heap = new MemoryHeapBase(mMaxSize, 0, "CursorWindow"); + if (heap != NULL) { + mMemory = new MemoryBase(heap, 0, mMaxSize); + if (mMemory != NULL) { + mData = (uint8_t *) mMemory->pointer(); + if (mData) { + mHeader = (window_header_t *) mData; + mSize = mMaxSize; + + // Put the window into a clean state + clear(); + LOG_WINDOW("Created CursorWindow with new MemoryDealer: mFreeOffset = %d, mSize = %d, mMaxSize = %d, mData = %p", mFreeOffset, mSize, mMaxSize, mData); + return true; + } + } + LOGE("CursorWindow heap allocation failed"); + return false; + } else { + LOGE("failed to create the CursorWindow heap"); + return false; + } +} + +CursorWindow::~CursorWindow() +{ + // Everything that matters is a smart pointer +} + +void CursorWindow::clear() +{ + mHeader->numRows = 0; + mHeader->numColumns = 0; + mFreeOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE; + // Mark the first chunk's next 'pointer' as null + *((uint32_t *)(mData + mFreeOffset - sizeof(uint32_t))) = 0; +} + +int32_t CursorWindow::freeSpace() +{ + int32_t freeSpace = mSize - mFreeOffset; + if (freeSpace < 0) { + freeSpace = 0; + } + return freeSpace; +} + +field_slot_t * CursorWindow::allocRow() +{ + // Fill in the row slot + row_slot_t * rowSlot = allocRowSlot(); + if (rowSlot == NULL) { + return NULL; + } + + // Allocate the slots for the field directory + size_t fieldDirSize = mHeader->numColumns * sizeof(field_slot_t); + uint32_t fieldDirOffset = alloc(fieldDirSize); + if (!fieldDirOffset) { + mHeader->numRows--; + LOGE("The row failed, so back out the new row accounting from allocRowSlot %d", mHeader->numRows); + return NULL; + } + field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(fieldDirOffset); + memset(fieldDir, 0x0, fieldDirSize); + +LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at offset %u\n", (mHeader->numRows - 1), ((uint8_t *)rowSlot) - mData, fieldDirSize, fieldDirOffset); + rowSlot->offset = fieldDirOffset; + + return fieldDir; +} + +uint32_t CursorWindow::alloc(size_t requestedSize, bool aligned) +{ + int32_t size; + uint32_t padding; + if (aligned) { + // 4 byte alignment + padding = 4 - (mFreeOffset & 0x3); + } else { + padding = 0; + } + + size = requestedSize + padding; + + if (size > freeSpace()) { + LOGE("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d", mSize, size, freeSpace(), mHeader->numRows); + // Only grow the window if the first row doesn't fit + if (mHeader->numRows > 1) { +LOGE("not growing since there are already %d row(s), max size %d", mHeader->numRows, mMaxSize); + return 0; + } + + // Find a new size that will fit the allocation + int allocated = mSize - freeSpace(); + int newSize = mSize + WINDOW_ALLOCATION_SIZE; + while (size > (newSize - allocated)) { + newSize += WINDOW_ALLOCATION_SIZE; + if (newSize > mMaxSize) { + LOGE("Attempting to grow window beyond max size (%d)", mMaxSize); + return 0; + } + } +LOG_WINDOW("found size %d", newSize); + mSize = newSize; + } + + uint32_t offset = mFreeOffset + padding; + mFreeOffset += size; + return offset; +} + +row_slot_t * CursorWindow::getRowSlot(int row) +{ + LOG_WINDOW("enter getRowSlot current row num %d, this row %d", mHeader->numRows, row); + int chunkNum = row / ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPos = row % ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t); + uint8_t * rowChunk = mData + sizeof(window_header_t); + for (int i = 0; i < chunkNum; i++) { + rowChunk = offsetToPtr(*((uint32_t *)(mData + chunkPtrOffset))); + chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)); + } + return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t))); + LOG_WINDOW("exit getRowSlot current row num %d, this row %d", mHeader->numRows, row); +} + +row_slot_t * CursorWindow::allocRowSlot() +{ + int chunkNum = mHeader->numRows / ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPos = mHeader->numRows % ROW_SLOT_CHUNK_NUM_ROWS; + int chunkPtrOffset = sizeof(window_header_t) + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t); + uint8_t * rowChunk = mData + sizeof(window_header_t); +LOG_WINDOW("Allocating row slot, mHeader->numRows is %d, chunkNum is %d, chunkPos is %d", mHeader->numRows, chunkNum, chunkPos); + for (int i = 0; i < chunkNum; i++) { + uint32_t nextChunkOffset = *((uint32_t *)(mData + chunkPtrOffset)); +LOG_WINDOW("nextChunkOffset is %d", nextChunkOffset); + if (nextChunkOffset == 0) { + // Allocate a new row chunk + nextChunkOffset = alloc(ROW_SLOT_CHUNK_SIZE, true); + if (nextChunkOffset == 0) { + return NULL; + } + rowChunk = offsetToPtr(nextChunkOffset); +LOG_WINDOW("allocated new chunk at %d, rowChunk = %p", nextChunkOffset, rowChunk); + *((uint32_t *)(mData + chunkPtrOffset)) = rowChunk - mData; + // Mark the new chunk's next 'pointer' as null + *((uint32_t *)(rowChunk + ROW_SLOT_CHUNK_SIZE - sizeof(uint32_t))) = 0; + } else { +LOG_WINDOW("follwing 'pointer' to next chunk, offset of next pointer is %d", chunkPtrOffset); + rowChunk = offsetToPtr(nextChunkOffset); + chunkPtrOffset = rowChunk - mData + (ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)); + } + } + mHeader->numRows++; + + return (row_slot_t *)(rowChunk + (chunkPos * sizeof(row_slot_t))); +} + +field_slot_t * CursorWindow::getFieldSlotWithCheck(int row, int column) +{ + if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) { + LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns); + return NULL; + } + row_slot_t * rowSlot = getRowSlot(row); + if (!rowSlot) { + LOGE("Failed to find rowSlot for row %d", row); + return NULL; + } + if (rowSlot->offset == 0 || rowSlot->offset >= mSize) { + LOGE("Invalid rowSlot, offset = %d", rowSlot->offset); + return NULL; + } + int fieldDirOffset = rowSlot->offset; + return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; +} + +uint32_t CursorWindow::read_field_slot(int row, int column, field_slot_t * slotOut) +{ + if (row < 0 || row >= mHeader->numRows || column < 0 || column >= mHeader->numColumns) { + LOGE("Bad request for field slot %d,%d. numRows = %d, numColumns = %d", row, column, mHeader->numRows, mHeader->numColumns); + return -1; + } + row_slot_t * rowSlot = getRowSlot(row); + if (!rowSlot) { + LOGE("Failed to find rowSlot for row %d", row); + return -1; + } + if (rowSlot->offset == 0 || rowSlot->offset >= mSize) { + LOGE("Invalid rowSlot, offset = %d", rowSlot->offset); + return -1; + } +LOG_WINDOW("Found field directory for %d,%d at rowSlot %d, offset %d", row, column, (uint8_t *)rowSlot - mData, rowSlot->offset); + field_slot_t * fieldDir = (field_slot_t *)offsetToPtr(rowSlot->offset); +LOG_WINDOW("Read field_slot_t %d,%d: offset = %d, size = %d, type = %d", row, column, fieldDir[column].data.buffer.offset, fieldDir[column].data.buffer.size, fieldDir[column].type); + + // Copy the data to the out param + slotOut->data.buffer.offset = fieldDir[column].data.buffer.offset; + slotOut->data.buffer.size = fieldDir[column].data.buffer.size; + slotOut->type = fieldDir[column].type; + return 0; +} + +void CursorWindow::copyIn(uint32_t offset, uint8_t const * data, size_t size) +{ + assert(offset + size <= mSize); + memcpy(mData + offset, data, size); +} + +void CursorWindow::copyIn(uint32_t offset, int64_t data) +{ + assert(offset + sizeof(int64_t) <= mSize); + memcpy(mData + offset, (uint8_t *)&data, sizeof(int64_t)); +} + +void CursorWindow::copyIn(uint32_t offset, double data) +{ + assert(offset + sizeof(double) <= mSize); + memcpy(mData + offset, (uint8_t *)&data, sizeof(double)); +} + +void CursorWindow::copyOut(uint32_t offset, uint8_t * data, size_t size) +{ + assert(offset + size <= mSize); + memcpy(data, mData + offset, size); +} + +int64_t CursorWindow::copyOutLong(uint32_t offset) +{ + int64_t value; + assert(offset + sizeof(int64_t) <= mSize); + memcpy(&value, mData + offset, sizeof(int64_t)); + return value; +} + +double CursorWindow::copyOutDouble(uint32_t offset) +{ + double value; + assert(offset + sizeof(double) <= mSize); + memcpy(&value, mData + offset, sizeof(double)); + return value; +} + +bool CursorWindow::putLong(unsigned int row, unsigned int col, int64_t value) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot) { + return false; + } + +#if WINDOW_STORAGE_INLINE_NUMERICS + fieldSlot->data.l = value; +#else + int offset = alloc(sizeof(int64_t)); + if (!offset) { + return false; + } + + copyIn(offset, value); + + fieldSlot->data.buffer.offset = offset; + fieldSlot->data.buffer.size = sizeof(int64_t); +#endif + fieldSlot->type = FIELD_TYPE_INTEGER; + return true; +} + +bool CursorWindow::putDouble(unsigned int row, unsigned int col, double value) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot) { + return false; + } + +#if WINDOW_STORAGE_INLINE_NUMERICS + fieldSlot->data.d = value; +#else + int offset = alloc(sizeof(int64_t)); + if (!offset) { + return false; + } + + copyIn(offset, value); + + fieldSlot->data.buffer.offset = offset; + fieldSlot->data.buffer.size = sizeof(double); +#endif + fieldSlot->type = FIELD_TYPE_FLOAT; + return true; +} + +bool CursorWindow::putNull(unsigned int row, unsigned int col) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot) { + return false; + } + + fieldSlot->type = FIELD_TYPE_NULL; + fieldSlot->data.buffer.offset = 0; + fieldSlot->data.buffer.size = 0; + return true; +} + +bool CursorWindow::getLong(unsigned int row, unsigned int col, int64_t * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot || fieldSlot->type != FIELD_TYPE_INTEGER) { + return false; + } + +#if WINDOW_STORAGE_INLINE_NUMERICS + *valueOut = fieldSlot->data.l; +#else + *valueOut = copyOutLong(fieldSlot->data.buffer.offset); +#endif + return true; +} + +bool CursorWindow::getDouble(unsigned int row, unsigned int col, double * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot || fieldSlot->type != FIELD_TYPE_FLOAT) { + return false; + } + +#if WINDOW_STORAGE_INLINE_NUMERICS + *valueOut = fieldSlot->data.d; +#else + *valueOut = copyOutDouble(fieldSlot->data.buffer.offset); +#endif + return true; +} + +bool CursorWindow::getNull(unsigned int row, unsigned int col, bool * valueOut) +{ + field_slot_t * fieldSlot = getFieldSlotWithCheck(row, col); + if (!fieldSlot) { + return false; + } + + if (fieldSlot->type != FIELD_TYPE_NULL) { + *valueOut = false; + } else { + *valueOut = true; + } + return true; +} + +}; // namespace android diff --git a/libs/camera/CameraParameters.cpp b/libs/camera/CameraParameters.cpp index 1415493..abd418a 100644 --- a/libs/camera/CameraParameters.cpp +++ b/libs/camera/CameraParameters.cpp @@ -269,7 +269,7 @@ void CameraParameters::remove(const char *key) mMap.removeItem(String8(key)); } -static int parse_size(const char *str, int &width, int &height) +static int parse_size(const char *str, int &width, int &height, char **endptr = NULL) { // Find the width. char *end; @@ -279,14 +279,42 @@ static int parse_size(const char *str, int &width, int &height) return -1; // Find the height, immediately after the 'x'. - int h = (int)strtol(end+1, 0, 10); + int h = (int)strtol(end+1, &end, 10); width = w; height = h; + if (endptr) { + *endptr = end; + } + return 0; } +static void parseSizesList(const char *sizesStr, Vector<Size> &sizes) +{ + if (sizesStr == 0) { + return; + } + + char *sizeStartPtr = (char *)sizesStr; + + while (true) { + int width, height; + int success = parse_size(sizeStartPtr, width, height, &sizeStartPtr); + if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) { + LOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr); + return; + } + sizes.push(Size(width, height)); + + if (*sizeStartPtr == '\0') { + return; + } + sizeStartPtr++; + } +} + void CameraParameters::setPreviewSize(int width, int height) { char str[32]; @@ -311,6 +339,12 @@ void CameraParameters::getPreviewSize(int *width, int *height) const } } +void CameraParameters::getSupportedPreviewSizes(Vector<Size> &sizes) const +{ + const char *previewSizesStr = get(KEY_SUPPORTED_PREVIEW_SIZES); + parseSizesList(previewSizesStr, sizes); +} + void CameraParameters::setPreviewFrameRate(int fps) { set(KEY_PREVIEW_FRAME_RATE, fps); @@ -355,6 +389,12 @@ void CameraParameters::getPictureSize(int *width, int *height) const } } +void CameraParameters::getSupportedPictureSizes(Vector<Size> &sizes) const +{ + const char *pictureSizesStr = get(KEY_SUPPORTED_PICTURE_SIZES); + parseSizesList(pictureSizesStr, sizes); +} + void CameraParameters::setPictureFormat(const char *format) { set(KEY_PICTURE_FORMAT, format); diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk new file mode 100644 index 0000000..1efe6b5 --- /dev/null +++ b/libs/hwui/Android.mk @@ -0,0 +1,41 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +# Only build libhwui when USE_OPENGL_RENDERER is +# defined in the current device/board configuration +ifeq ($(USE_OPENGL_RENDERER),true) + LOCAL_SRC_FILES:= \ + FontRenderer.cpp \ + GradientCache.cpp \ + LayerCache.cpp \ + Matrix.cpp \ + OpenGLRenderer.cpp \ + Patch.cpp \ + PatchCache.cpp \ + PathCache.cpp \ + Program.cpp \ + ProgramCache.cpp \ + SkiaColorFilter.cpp \ + SkiaShader.cpp \ + TextureCache.cpp + + LOCAL_C_INCLUDES += \ + $(JNI_H_INCLUDE) \ + $(LOCAL_PATH)/../../include/utils \ + external/skia/include/core \ + external/skia/include/effects \ + external/skia/include/images \ + external/skia/src/ports \ + external/skia/include/utils + + LOCAL_CFLAGS += -DUSE_OPENGL_RENDERER + LOCAL_MODULE_CLASS := SHARED_LIBRARIES + LOCAL_SHARED_LIBRARIES := libcutils libutils libGLESv2 libskia + LOCAL_MODULE := libhwui + LOCAL_MODULE_TAGS := optional + LOCAL_PRELINK_MODULE := false + + include $(BUILD_SHARED_LIBRARY) + + include $(call all-makefiles-under,$(LOCAL_PATH)) +endif diff --git a/libs/hwui/Extensions.h b/libs/hwui/Extensions.h new file mode 100644 index 0000000..7778290 --- /dev/null +++ b/libs/hwui/Extensions.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_EXTENSIONS_H +#define ANDROID_UI_EXTENSIONS_H + +#include <utils/SortedVector.h> +#include <utils/String8.h> + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +namespace android { +namespace uirenderer { + +class Extensions { +public: + Extensions() { + const char* buffer = (const char*) glGetString(GL_EXTENSIONS); + const char* current = buffer; + const char* head = current; + do { + head = strchr(current, ' '); + String8 s(current, head ? head - current : strlen(current)); + if (s.length()) { + mExtensionList.add(s); + } + current = head + 1; + } while (head); + + mHasNPot = hasExtension("GL_OES_texture_npot"); + mHasDrawPath = hasExtension("GL_NV_draw_path"); + mHasCoverageSample = hasExtension("GL_NV_coverage_sample"); + + mExtensions = buffer; + } + + inline bool hasNPot() const { return mHasNPot; } + inline bool hasDrawPath() const { return mHasDrawPath; } + inline bool hasCoverageSample() const { return mHasCoverageSample; } + + bool hasExtension(const char* extension) const { + const String8 s(extension); + return mExtensionList.indexOf(s) >= 0; + } + + void dump() { + LOGD("Supported extensions:\n%s", mExtensions); + } + +private: + SortedVector<String8> mExtensionList; + + const char* mExtensions; + + bool mHasNPot; + bool mHasDrawPath; + bool mHasCoverageSample; +}; // class Extensions + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_EXTENSIONS_H diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp new file mode 100644 index 0000000..e807aba --- /dev/null +++ b/libs/hwui/FontRenderer.cpp @@ -0,0 +1,804 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <SkUtils.h> + +#include <cutils/properties.h> +#include <utils/Log.h> + +#include "FontRenderer.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +#define DEFAULT_TEXT_CACHE_WIDTH 1024 +#define DEFAULT_TEXT_CACHE_HEIGHT 256 + +/////////////////////////////////////////////////////////////////////////////// +// Font +/////////////////////////////////////////////////////////////////////////////// + +Font::Font(FontRenderer* state, uint32_t fontId, float fontSize) : + mState(state), mFontId(fontId), mFontSize(fontSize) { +} + + +Font::~Font() { + for (uint32_t ct = 0; ct < mState->mActiveFonts.size(); ct++) { + if (mState->mActiveFonts[ct] == this) { + mState->mActiveFonts.removeAt(ct); + break; + } + } + + for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { + CachedGlyphInfo* glyph = mCachedGlyphs.valueAt(i); + delete glyph; + } +} + +void Font::invalidateTextureCache() { + for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { + mCachedGlyphs.valueAt(i)->mIsValid = false; + } +} + +void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds) { + int nPenX = x + glyph->mBitmapLeft; + int nPenY = y + glyph->mBitmapTop; + + int width = (int) glyph->mBitmapWidth; + int height = (int) glyph->mBitmapHeight; + + if (bounds->bottom > nPenY) { + bounds->bottom = nPenY; + } + if (bounds->left > nPenX) { + bounds->left = nPenX; + } + if (bounds->right < nPenX + width) { + bounds->right = nPenX + width; + } + if (bounds->top < nPenY + height) { + bounds->top = nPenY + height; + } +} + +void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y) { + int nPenX = x + glyph->mBitmapLeft; + int nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight; + + float u1 = glyph->mBitmapMinU; + float u2 = glyph->mBitmapMaxU; + float v1 = glyph->mBitmapMinV; + float v2 = glyph->mBitmapMaxV; + + int width = (int) glyph->mBitmapWidth; + int height = (int) glyph->mBitmapHeight; + + mState->appendMeshQuad(nPenX, nPenY, 0, u1, v2, + nPenX + width, nPenY, 0, u2, v2, + nPenX + width, nPenY - height, 0, u2, v1, + nPenX, nPenY - height, 0, u1, v1); +} + +void Font::drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { + int nPenX = x + glyph->mBitmapLeft; + int nPenY = y + glyph->mBitmapTop; + + uint32_t endX = glyph->mStartX + glyph->mBitmapWidth; + uint32_t endY = glyph->mStartY + glyph->mBitmapHeight; + + uint32_t cacheWidth = mState->getCacheWidth(); + const uint8_t* cacheBuffer = mState->getTextTextureData(); + + uint32_t cacheX = 0, cacheY = 0; + int32_t bX = 0, bY = 0; + for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) { + for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) { + if (bX < 0 || bY < 0 || bX >= (int32_t)bitmapW || bY >= (int32_t)bitmapH) { + LOGE("Skipping invalid index"); + continue; + } + uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; + bitmap[bY * bitmapW + bX] = tempCol; + } + } + +} + +Font::CachedGlyphInfo* Font::getCachedUTFChar(SkPaint* paint, int32_t utfChar) { + CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(utfChar); + if (cachedGlyph == NULL) { + cachedGlyph = cacheGlyph(paint, utfChar); + } + + // Is the glyph still in texture cache? + if (!cachedGlyph->mIsValid) { + const SkGlyph& skiaGlyph = paint->getUnicharMetrics(utfChar); + updateGlyphCache(paint, skiaGlyph, cachedGlyph); + } + + return cachedGlyph; +} + +void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, + int numGlyphs, int x, int y, uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) { + if (bitmap != NULL && bitmapW > 0 && bitmapH > 0) { + renderUTF(paint, text, start, len, numGlyphs, x, y, BITMAP, bitmap, + bitmapW, bitmapH, NULL); + } else { + renderUTF(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL, 0, 0, NULL); + } + +} + +void Font::measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, + int numGlyphs, Rect *bounds) { + if (bounds == NULL) { + LOGE("No return rectangle provided to measure text"); + return; + } + bounds->set(1e6, -1e6, -1e6, 1e6); + renderUTF(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds); +} + +void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, + int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, + uint32_t bitmapW, uint32_t bitmapH,Rect *bounds) { + if (numGlyphs == 0 || text == NULL || len == 0) { + return; + } + + int penX = x, penY = y; + int glyphsLeft = 1; + if (numGlyphs > 0) { + glyphsLeft = numGlyphs; + } + + text += start; + + while (glyphsLeft > 0) { + int32_t utfChar = SkUTF16_NextUnichar((const uint16_t**) &text); + + // Reached the end of the string + if (utfChar < 0) { + break; + } + + CachedGlyphInfo* cachedGlyph = getCachedUTFChar(paint, utfChar); + + // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage + if (cachedGlyph->mIsValid) { + switch(mode) { + case FRAMEBUFFER: + drawCachedGlyph(cachedGlyph, penX, penY); + break; + case BITMAP: + drawCachedGlyph(cachedGlyph, penX, penY, bitmap, bitmapW, bitmapH); + break; + case MEASURE: + measureCachedGlyph(cachedGlyph, penX, penY, bounds); + break; + } + } + + penX += SkFixedFloor(cachedGlyph->mAdvanceX); + + // If we were given a specific number of glyphs, decrement + if (numGlyphs > 0) { + glyphsLeft--; + } + } +} + +void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph) { + glyph->mAdvanceX = skiaGlyph.fAdvanceX; + glyph->mAdvanceY = skiaGlyph.fAdvanceY; + glyph->mBitmapLeft = skiaGlyph.fLeft; + glyph->mBitmapTop = skiaGlyph.fTop; + + uint32_t startX = 0; + uint32_t startY = 0; + + // Get the bitmap for the glyph + paint->findImage(skiaGlyph); + glyph->mIsValid = mState->cacheBitmap(skiaGlyph, &startX, &startY); + + if (!glyph->mIsValid) { + return; + } + + uint32_t endX = startX + skiaGlyph.fWidth; + uint32_t endY = startY + skiaGlyph.fHeight; + + glyph->mStartX = startX; + glyph->mStartY = startY; + glyph->mBitmapWidth = skiaGlyph.fWidth; + glyph->mBitmapHeight = skiaGlyph.fHeight; + + uint32_t cacheWidth = mState->getCacheWidth(); + uint32_t cacheHeight = mState->getCacheHeight(); + + glyph->mBitmapMinU = (float) startX / (float) cacheWidth; + glyph->mBitmapMinV = (float) startY / (float) cacheHeight; + glyph->mBitmapMaxU = (float) endX / (float) cacheWidth; + glyph->mBitmapMaxV = (float) endY / (float) cacheHeight; + + mState->mUploadTexture = true; +} + +Font::CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, int32_t glyph) { + CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); + mCachedGlyphs.add(glyph, newGlyph); + + const SkGlyph& skiaGlyph = paint->getUnicharMetrics(glyph); + newGlyph->mGlyphIndex = skiaGlyph.fID; + newGlyph->mIsValid = false; + + updateGlyphCache(paint, skiaGlyph, newGlyph); + + return newGlyph; +} + +Font* Font::create(FontRenderer* state, uint32_t fontId, float fontSize) { + Vector<Font*> &activeFonts = state->mActiveFonts; + + for (uint32_t i = 0; i < activeFonts.size(); i++) { + Font* font = activeFonts[i]; + if (font->mFontId == fontId && font->mFontSize == fontSize) { + return font; + } + } + + Font* newFont = new Font(state, fontId, fontSize); + activeFonts.push(newFont); + return newFont; +} + +/////////////////////////////////////////////////////////////////////////////// +// FontRenderer +/////////////////////////////////////////////////////////////////////////////// + +FontRenderer::FontRenderer() { + LOGD("Creating FontRenderer"); + + mInitialized = false; + mMaxNumberOfQuads = 1024; + mCurrentQuadIndex = 0; + mTextureId = 0; + + mTextMeshPtr = NULL; + mTextTexture = NULL; + + mIndexBufferID = 0; + + mCacheWidth = DEFAULT_TEXT_CACHE_WIDTH; + mCacheHeight = DEFAULT_TEXT_CACHE_HEIGHT; + + char property[PROPERTY_VALUE_MAX]; + if (property_get(PROPERTY_TEXT_CACHE_WIDTH, property, NULL) > 0) { + LOGD(" Setting text cache width to %s pixels", property); + mCacheWidth = atoi(property); + } else { + LOGD(" Using default text cache width of %i pixels", mCacheWidth); + } + + if (property_get(PROPERTY_TEXT_CACHE_HEIGHT, property, NULL) > 0) { + LOGD(" Setting text cache width to %s pixels", property); + mCacheHeight = atoi(property); + } else { + LOGD(" Using default text cache height of %i pixels", mCacheHeight); + } +} + +FontRenderer::~FontRenderer() { + for (uint32_t i = 0; i < mCacheLines.size(); i++) { + delete mCacheLines[i]; + } + mCacheLines.clear(); + + if (mInitialized) { + delete[] mTextMeshPtr; + delete[] mTextTexture; + } + + if (mTextureId) { + glDeleteTextures(1, &mTextureId); + } + + Vector<Font*> fontsToDereference = mActiveFonts; + for (uint32_t i = 0; i < fontsToDereference.size(); i++) { + delete fontsToDereference[i]; + } +} + +void FontRenderer::flushAllAndInvalidate() { + if (mCurrentQuadIndex != 0) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } + for (uint32_t i = 0; i < mActiveFonts.size(); i++) { + mActiveFonts[i]->invalidateTextureCache(); + } + for (uint32_t i = 0; i < mCacheLines.size(); i++) { + mCacheLines[i]->mCurrentCol = 0; + } +} + +bool FontRenderer::cacheBitmap(const SkGlyph& glyph, uint32_t* retOriginX, uint32_t* retOriginY) { + // If the glyph is too tall, don't cache it + if (glyph.fWidth > mCacheLines[mCacheLines.size() - 1]->mMaxHeight) { + LOGE("Font size to large to fit in cache. width, height = %i, %i", + (int) glyph.fWidth, (int) glyph.fHeight); + return false; + } + + // Now copy the bitmap into the cache texture + uint32_t startX = 0; + uint32_t startY = 0; + + bool bitmapFit = false; + for (uint32_t i = 0; i < mCacheLines.size(); i++) { + bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY); + if (bitmapFit) { + break; + } + } + + // If the new glyph didn't fit, flush the state so far and invalidate everything + if (!bitmapFit) { + flushAllAndInvalidate(); + + // Try to fit it again + for (uint32_t i = 0; i < mCacheLines.size(); i++) { + bitmapFit = mCacheLines[i]->fitBitmap(glyph, &startX, &startY); + if (bitmapFit) { + break; + } + } + + // if we still don't fit, something is wrong and we shouldn't draw + if (!bitmapFit) { + LOGE("Bitmap doesn't fit in cache. width, height = %i, %i", + (int) glyph.fWidth, (int) glyph.fHeight); + return false; + } + } + + *retOriginX = startX; + *retOriginY = startY; + + uint32_t endX = startX + glyph.fWidth; + uint32_t endY = startY + glyph.fHeight; + + uint32_t cacheWidth = mCacheWidth; + + uint8_t* cacheBuffer = mTextTexture; + uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage; + unsigned int stride = glyph.rowBytes(); + + uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; + for (cacheX = startX, bX = 0; cacheX < endX; cacheX++, bX++) { + for (cacheY = startY, bY = 0; cacheY < endY; cacheY++, bY++) { + uint8_t tempCol = bitmapBuffer[bY * stride + bX]; + cacheBuffer[cacheY * cacheWidth + cacheX] = tempCol; + } + } + + return true; +} + +void FontRenderer::initTextTexture() { + mTextTexture = new uint8_t[mCacheWidth * mCacheHeight]; + mUploadTexture = false; + + glGenTextures(1, &mTextureId); + glBindTexture(GL_TEXTURE_2D, mTextureId); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + // Initialize texture dimentions + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, mCacheWidth, mCacheHeight, 0, + GL_ALPHA, GL_UNSIGNED_BYTE, 0); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + // Split up our cache texture into lines of certain widths + int nextLine = 0; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 16, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, 40, nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(mCacheWidth, mCacheHeight - nextLine, nextLine, 0)); +} + +// Avoid having to reallocate memory and render quad by quad +void FontRenderer::initVertexArrayBuffers() { + uint32_t numIndicies = mMaxNumberOfQuads * 6; + uint32_t indexBufferSizeBytes = numIndicies * sizeof(uint16_t); + uint16_t* indexBufferData = (uint16_t*) malloc(indexBufferSizeBytes); + + // Four verts, two triangles , six indices per quad + for (uint32_t i = 0; i < mMaxNumberOfQuads; i++) { + int i6 = i * 6; + int i4 = i * 4; + + indexBufferData[i6 + 0] = i4 + 0; + indexBufferData[i6 + 1] = i4 + 1; + indexBufferData[i6 + 2] = i4 + 2; + + indexBufferData[i6 + 3] = i4 + 0; + indexBufferData[i6 + 4] = i4 + 2; + indexBufferData[i6 + 5] = i4 + 3; + } + + glGenBuffers(1, &mIndexBufferID); + glBindBuffer(GL_ARRAY_BUFFER, mIndexBufferID); + glBufferData(GL_ARRAY_BUFFER, indexBufferSizeBytes, indexBufferData, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + free(indexBufferData); + + uint32_t coordSize = 3; + uint32_t uvSize = 2; + uint32_t vertsPerQuad = 4; + uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize; + mTextMeshPtr = new float[vertexBufferSize]; +} + +// We don't want to allocate anything unless we actually draw text +void FontRenderer::checkInit() { + if (mInitialized) { + return; + } + + initTextTexture(); + initVertexArrayBuffers(); + + // We store a string with letters in a rough frequency of occurrence + mLatinPrecache = String16("eisarntolcdugpmhbyfvkwzxjq "); + mLatinPrecache += String16("EISARNTOLCDUGPMHBYFVKWZXJQ"); + mLatinPrecache += String16(",.?!()-+@;:`'"); + mLatinPrecache += String16("0123456789"); + + mInitialized = true; +} + +void FontRenderer::checkTextureUpdate() { + if (!mUploadTexture) { + return; + } + + glBindTexture(GL_TEXTURE_2D, mTextureId); + + // Iterate over all the cache lines and see which ones need to be updated + for (uint32_t i = 0; i < mCacheLines.size(); i++) { + CacheTextureLine* cl = mCacheLines[i]; + if(cl->mDirty) { + uint32_t xOffset = 0; + uint32_t yOffset = cl->mCurrentRow; + uint32_t width = mCacheWidth; + uint32_t height = cl->mMaxHeight; + void* textureData = mTextTexture + yOffset*width; + + glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height, + GL_ALPHA, GL_UNSIGNED_BYTE, textureData); + + cl->mDirty = false; + } + } + + mUploadTexture = false; +} + +void FontRenderer::issueDrawCommand() { + checkTextureUpdate(); + + float* vtx = mTextMeshPtr; + float* tex = vtx + 3; + + // position is slot 0 + uint32_t slot = 0; + glVertexAttribPointer(slot, 3, GL_FLOAT, false, 20, vtx); + + // texture0 is slot 1 + slot = 1; + glVertexAttribPointer(slot, 2, GL_FLOAT, false, 20, tex); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferID); + glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, NULL); +} + +void FontRenderer::appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, + float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, + float x4, float y4, float z4, float u4, float v4) { + if (x1 > mClip->right || y1 < mClip->top || x2 < mClip->left || y4 > mClip->bottom) { + return; + } + + const uint32_t vertsPerQuad = 4; + const uint32_t floatsPerVert = 5; + float* currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; + + (*currentPos++) = x1; + (*currentPos++) = y1; + (*currentPos++) = z1; + (*currentPos++) = u1; + (*currentPos++) = v1; + + (*currentPos++) = x2; + (*currentPos++) = y2; + (*currentPos++) = z2; + (*currentPos++) = u2; + (*currentPos++) = v2; + + (*currentPos++) = x3; + (*currentPos++) = y3; + (*currentPos++) = z3; + (*currentPos++) = u3; + (*currentPos++) = v3; + + (*currentPos++) = x4; + (*currentPos++) = y4; + (*currentPos++) = z4; + (*currentPos++) = u4; + (*currentPos++) = v4; + + mCurrentQuadIndex++; + + if (mCurrentQuadIndex == mMaxNumberOfQuads) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } +} + +uint32_t FontRenderer::getRemainingCacheCapacity() { + uint32_t remainingCapacity = 0; + float totalPixels = 0; + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol); + totalPixels += mCacheLines[i]->mMaxWidth; + } + remainingCapacity = (remainingCapacity * 100) / totalPixels; + return remainingCapacity; +} + +void FontRenderer::precacheLatin(SkPaint* paint) { + // Remaining capacity is measured in % + uint32_t remainingCapacity = getRemainingCacheCapacity(); + uint32_t precacheIdx = 0; + while(remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) { + mCurrentFont->getCachedUTFChar(paint, (int32_t)mLatinPrecache[precacheIdx]); + remainingCapacity = getRemainingCacheCapacity(); + precacheIdx ++; + } +} + +void FontRenderer::setFont(SkPaint* paint, uint32_t fontId, float fontSize) { + uint32_t currentNumFonts = mActiveFonts.size(); + mCurrentFont = Font::create(this, fontId, fontSize); + + const float maxPrecacheFontSize = 40.0f; + bool isNewFont = currentNumFonts != mActiveFonts.size(); + + if(isNewFont && fontSize <= maxPrecacheFontSize ){ + precacheLatin(paint); + } +} +FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, + uint32_t startIndex, uint32_t len, int numGlyphs, uint32_t radius) { + + Rect bounds; + mCurrentFont->measureUTF(paint, text, startIndex, len, numGlyphs, &bounds); + uint32_t paddedWidth = (uint32_t)(bounds.right - bounds.left) + 2*radius; + uint32_t paddedHeight = (uint32_t)(bounds.top - bounds.bottom) + 2*radius; + uint8_t* dataBuffer = new uint8_t[paddedWidth * paddedHeight]; + for(uint32_t i = 0; i < paddedWidth * paddedHeight; i ++) { + dataBuffer[i] = 0; + } + int penX = radius - bounds.left; + int penY = radius - bounds.bottom; + + mCurrentFont->renderUTF(paint, text, startIndex, len, numGlyphs, penX, penY, + dataBuffer, paddedWidth, paddedHeight); + blurImage(dataBuffer, paddedWidth, paddedHeight, radius); + + DropShadow image; + image.width = paddedWidth; + image.height = paddedHeight; + image.image = dataBuffer; + image.penX = penX; + image.penY = penY; + return image; +} + +void FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text, + uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y) { + checkInit(); + + if (!mCurrentFont) { + LOGE("No font set"); + return; + } + + mClip = clip; + mCurrentFont->renderUTF(paint, text, startIndex, len, numGlyphs, x, y); + + if (mCurrentQuadIndex != 0) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } +} + +void FontRenderer::computeGaussianWeights(float* weights, int32_t radius) { + // Compute gaussian weights for the blur + // e is the euler's number + float e = 2.718281828459045f; + float pi = 3.1415926535897932f; + // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) + // x is of the form [-radius .. 0 .. radius] + // and sigma varies with radius. + // Based on some experimental radius values and sigma's + // we approximately fit sigma = f(radius) as + // sigma = radius * 0.3 + 0.6 + // The larger the radius gets, the more our gaussian blur + // will resemble a box blur since with large sigma + // the gaussian curve begins to lose its shape + float sigma = 0.3f * (float)radius + 0.6f; + + // Now compute the coefficints + // We will store some redundant values to save some math during + // the blur calculations + // precompute some values + float coeff1 = 1.0f / (sqrt( 2.0f * pi ) * sigma); + float coeff2 = - 1.0f / (2.0f * sigma * sigma); + + float normalizeFactor = 0.0f; + for(int32_t r = -radius; r <= radius; r ++) { + float floatR = (float)r; + weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2); + normalizeFactor += weights[r + radius]; + } + + //Now we need to normalize the weights because all our coefficients need to add up to one + normalizeFactor = 1.0f / normalizeFactor; + for(int32_t r = -radius; r <= radius; r ++) { + weights[r + radius] *= normalizeFactor; + } +} + +void FontRenderer::horizontalBlur(float* weights, int32_t radius, + const uint8_t* source, uint8_t* dest, + int32_t width, int32_t height) { + float blurredPixel = 0.0f; + float currentPixel = 0.0f; + + for(int32_t y = 0; y < height; y ++) { + + const uint8_t* input = source + y * width; + uint8_t* output = dest + y * width; + + for(int32_t x = 0; x < width; x ++) { + blurredPixel = 0.0f; + const float* gPtr = weights; + // Optimization for non-border pixels + if ((x > radius) && (x < (width - radius))) { + const uint8_t *i = input + (x - radius); + for(int r = -radius; r <= radius; r ++) { + currentPixel = (float)(*i); + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + i++; + } + } else { + for(int32_t r = -radius; r <= radius; r ++) { + // Stepping left and right away from the pixel + int validW = x + r; + if(validW < 0) { + validW = 0; + } + if(validW > width - 1) { + validW = width - 1; + } + + currentPixel = (float)(input[validW]); + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + } + } + *output = (uint8_t)blurredPixel; + output ++; + } + } +} + +void FontRenderer::verticalBlur(float* weights, int32_t radius, + const uint8_t* source, uint8_t* dest, + int32_t width, int32_t height) { + float blurredPixel = 0.0f; + float currentPixel = 0.0f; + + for(int32_t y = 0; y < height; y ++) { + + uint8_t* output = dest + y * width; + + for(int32_t x = 0; x < width; x ++) { + blurredPixel = 0.0f; + const float* gPtr = weights; + const uint8_t* input = source + x; + // Optimization for non-border pixels + if ((y > radius) && (y < (height - radius))) { + const uint8_t *i = input + ((y - radius) * width); + for(int32_t r = -radius; r <= radius; r ++) { + currentPixel = (float)(*i); + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + i += width; + } + } else { + for(int32_t r = -radius; r <= radius; r ++) { + int validH = y + r; + // Clamp to zero and width + if(validH < 0) { + validH = 0; + } + if(validH > height - 1) { + validH = height - 1; + } + + const uint8_t *i = input + validH * width; + currentPixel = (float)(*i); + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + } + } + *output = (uint8_t)blurredPixel; + output ++; + } + } +} + + +void FontRenderer::blurImage(uint8_t *image, int32_t width, int32_t height, int32_t radius) { + float *gaussian = new float[2 * radius + 1]; + computeGaussianWeights(gaussian, radius); + uint8_t* scratch = new uint8_t[width * height]; + horizontalBlur(gaussian, radius, image, scratch, width, height); + verticalBlur(gaussian, radius, scratch, image, width, height); + delete[] gaussian; + delete[] scratch; +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h new file mode 100644 index 0000000..6346ded --- /dev/null +++ b/libs/hwui/FontRenderer.h @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_FONT_RENDERER_H +#define ANDROID_UI_FONT_RENDERER_H + +#include <utils/String8.h> +#include <utils/String16.h> +#include <utils/Vector.h> +#include <utils/KeyedVector.h> + +#include <SkScalerContext.h> +#include <SkPaint.h> + +#include <GLES2/gl2.h> + +#include "Rect.h" +#include "Properties.h" + +namespace android { +namespace uirenderer { + +class FontRenderer; + +/** + * Represents a font, defined by a Skia font id and a font size. A font is used + * to generate glyphs and cache them in the FontState. + */ +class Font { +public: + ~Font(); + + /** + * Renders the specified string of text. + * If bitmap is specified, it will be used as the render target + */ + void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len, + int numGlyphs, int x, int y, + uint8_t *bitmap = NULL, uint32_t bitmapW = 0, uint32_t bitmapH = 0); + /** + * Creates a new font associated with the specified font state. + */ + static Font* create(FontRenderer* state, uint32_t fontId, float fontSize); + +protected: + friend class FontRenderer; + + enum RenderMode { + FRAMEBUFFER, + BITMAP, + MEASURE, + }; + + void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len, + int numGlyphs, int x, int y, RenderMode mode, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, + Rect *bounds); + + void measureUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len, + int numGlyphs, Rect *bounds); + + struct CachedGlyphInfo { + // Has the cache been invalidated? + bool mIsValid; + // Location of the cached glyph in the bitmap + // in case we need to resize the texture or + // render to bitmap + uint32_t mStartX; + uint32_t mStartY; + uint32_t mBitmapWidth; + uint32_t mBitmapHeight; + // Also cache texture coords for the quad + float mBitmapMinU; + float mBitmapMinV; + float mBitmapMaxU; + float mBitmapMaxV; + // Minimize how much we call freetype + uint32_t mGlyphIndex; + uint32_t mAdvanceX; + uint32_t mAdvanceY; + // Values below contain a glyph's origin in the bitmap + int32_t mBitmapLeft; + int32_t mBitmapTop; + }; + + Font(FontRenderer* state, uint32_t fontId, float fontSize); + + DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs; + + void invalidateTextureCache(); + + CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph); + void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph); + void measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, Rect *bounds); + void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); + void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y, + uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH); + + CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar); + + FontRenderer* mState; + uint32_t mFontId; + float mFontSize; +}; + +class FontRenderer { +public: + FontRenderer(); + ~FontRenderer(); + + void init(); + void deinit(); + + void setFont(SkPaint* paint, uint32_t fontId, float fontSize); + void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, + uint32_t len, int numGlyphs, int x, int y); + + struct DropShadow { + uint32_t width; + uint32_t height; + uint8_t* image; + int32_t penX; + int32_t penY; + }; + + // After renderDropShadow returns, the called owns the memory in DropShadow.image + // and is responsible for releasing it when it's done with it + DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, + uint32_t len, int numGlyphs, uint32_t radius); + + GLuint getTexture() { + checkInit(); + return mTextureId; + } + +protected: + friend class Font; + + struct CacheTextureLine { + uint16_t mMaxHeight; + uint16_t mMaxWidth; + uint32_t mCurrentRow; + uint32_t mCurrentCol; + bool mDirty; + + CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow, + uint32_t currentCol): + mMaxHeight(maxHeight), + mMaxWidth(maxWidth), + mCurrentRow(currentRow), + mCurrentCol(currentCol), + mDirty(false) { + } + + bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) { + if (glyph.fHeight > mMaxHeight) { + return false; + } + + if (mCurrentCol + glyph.fWidth < mMaxWidth) { + *retOriginX = mCurrentCol; + *retOriginY = mCurrentRow; + mCurrentCol += glyph.fWidth; + mDirty = true; + return true; + } + + return false; + } + }; + + uint32_t getCacheWidth() const { + return mCacheWidth; + } + + uint32_t getCacheHeight() const { + return mCacheHeight; + } + + void initTextTexture(); + bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY); + + void flushAllAndInvalidate(); + void initVertexArrayBuffers(); + + void checkInit(); + + String16 mLatinPrecache; + void precacheLatin(SkPaint* paint); + + void issueDrawCommand(); + void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2, + float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, + float x4, float y4, float z4, float u4, float v4); + + uint32_t mCacheWidth; + uint32_t mCacheHeight; + + Vector<CacheTextureLine*> mCacheLines; + uint32_t getRemainingCacheCapacity(); + + Font* mCurrentFont; + Vector<Font*> mActiveFonts; + + // Texture to cache glyph bitmaps + uint8_t* mTextTexture; + const uint8_t* getTextTextureData() const { + return mTextTexture; + } + GLuint mTextureId; + void checkTextureUpdate(); + bool mUploadTexture; + + // Pointer to vertex data to speed up frame to frame work + float *mTextMeshPtr; + uint32_t mCurrentQuadIndex; + uint32_t mMaxNumberOfQuads; + + uint32_t mIndexBufferID; + + const Rect* mClip; + + bool mInitialized; + + void computeGaussianWeights(float* weights, int32_t radius); + void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, + int32_t width, int32_t height); + void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, + int32_t width, int32_t height); + void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius); +}; + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_FONT_RENDERER_H diff --git a/libs/hwui/GenerationCache.h b/libs/hwui/GenerationCache.h new file mode 100644 index 0000000..45b3ffa --- /dev/null +++ b/libs/hwui/GenerationCache.h @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_GENERATION_CACHE_H +#define ANDROID_UI_GENERATION_CACHE_H + +#include <utils/KeyedVector.h> +#include <utils/RefBase.h> + +namespace android { +namespace uirenderer { + +template<typename EntryKey, typename EntryValue> +class OnEntryRemoved { +public: + virtual ~OnEntryRemoved() { }; + virtual void operator()(EntryKey& key, EntryValue& value) = 0; +}; // class OnEntryRemoved + +template<typename EntryKey, typename EntryValue> +struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > { + Entry() { } + Entry(const Entry<EntryKey, EntryValue>& e): + key(e.key), value(e.value), parent(e.parent), child(e.child) { } + Entry(sp<Entry<EntryKey, EntryValue> > e): + key(e->key), value(e->value), parent(e->parent), child(e->child) { } + + EntryKey key; + EntryValue value; + + sp<Entry<EntryKey, EntryValue> > parent; + sp<Entry<EntryKey, EntryValue> > child; +}; // struct Entry + +template<typename K, typename V> +class GenerationCache { +public: + GenerationCache(uint32_t maxCapacity); + virtual ~GenerationCache(); + + enum Capacity { + kUnlimitedCapacity, + }; + + void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener); + + void clear(); + + bool contains(K key) const; + V get(K key); + void put(K key, V value); + V remove(K key); + V removeOldest(); + + uint32_t size() const; + + void addToCache(sp<Entry<K, V> > entry, K key, V value); + void attachToCache(sp<Entry<K, V> > entry); + void detachFromCache(sp<Entry<K, V> > entry); + + V removeAt(ssize_t index); + + KeyedVector<K, sp<Entry<K, V> > > mCache; + uint32_t mMaxCapacity; + + OnEntryRemoved<K, V>* mListener; + + sp<Entry<K, V> > mOldest; + sp<Entry<K, V> > mYoungest; +}; // class GenerationCache + +template<typename K, typename V> +GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) { +}; + +template<typename K, typename V> +GenerationCache<K, V>::~GenerationCache() { + clear(); +}; + +template<typename K, typename V> +uint32_t GenerationCache<K, V>::size() const { + return mCache.size(); +} + +template<typename K, typename V> +void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) { + mListener = listener; +} + +template<typename K, typename V> +void GenerationCache<K, V>::clear() { + if (mListener) { + while (mCache.size() > 0) { + removeOldest(); + } + } else { + mCache.clear(); + } + mYoungest.clear(); + mOldest.clear(); +} + +template<typename K, typename V> +bool GenerationCache<K, V>::contains(K key) const { + return mCache.indexOfKey(key) >= 0; +} + +template<typename K, typename V> +V GenerationCache<K, V>::get(K key) { + ssize_t index = mCache.indexOfKey(key); + if (index >= 0) { + sp<Entry<K, V> > entry = mCache.valueAt(index); + if (entry.get()) { + detachFromCache(entry); + attachToCache(entry); + return entry->value; + } + } + + return NULL; +} + +template<typename K, typename V> +void GenerationCache<K, V>::put(K key, V value) { + if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) { + removeOldest(); + } + + ssize_t index = mCache.indexOfKey(key); + if (index >= 0) { + sp<Entry<K, V> > entry = mCache.valueAt(index); + detachFromCache(entry); + addToCache(entry, key, value); + } else { + sp<Entry<K, V> > entry = new Entry<K, V>; + addToCache(entry, key, value); + } +} + +template<typename K, typename V> +void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) { + entry->key = key; + entry->value = value; + mCache.add(key, entry); + attachToCache(entry); +} + +template<typename K, typename V> +V GenerationCache<K, V>::remove(K key) { + ssize_t index = mCache.indexOfKey(key); + if (index >= 0) { + return removeAt(index); + } + + return NULL; +} + +template<typename K, typename V> +V GenerationCache<K, V>::removeAt(ssize_t index) { + sp<Entry<K, V> > entry = mCache.valueAt(index); + if (mListener) { + (*mListener)(entry->key, entry->value); + } + mCache.removeItemsAt(index, 1); + detachFromCache(entry); + + return entry->value; +} + +template<typename K, typename V> +V GenerationCache<K, V>::removeOldest() { + if (mOldest.get()) { + ssize_t index = mCache.indexOfKey(mOldest->key); + if (index >= 0) { + return removeAt(index); + } + } + + return NULL; +} + +template<typename K, typename V> +void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) { + if (!mYoungest.get()) { + mYoungest = mOldest = entry; + } else { + entry->parent = mYoungest; + mYoungest->child = entry; + mYoungest = entry; + } +} + +template<typename K, typename V> +void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) { + if (entry->parent.get()) { + entry->parent->child = entry->child; + } + + if (entry->child.get()) { + entry->child->parent = entry->parent; + } + + if (mOldest == entry) { + mOldest = entry->child; + } + + if (mYoungest == entry) { + mYoungest = entry->parent; + } + + entry->parent.clear(); + entry->child.clear(); +} + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_GENERATION_CACHE_H diff --git a/libs/hwui/GradientCache.cpp b/libs/hwui/GradientCache.cpp new file mode 100644 index 0000000..59fa0a7 --- /dev/null +++ b/libs/hwui/GradientCache.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <GLES2/gl2.h> + +#include <SkCanvas.h> +#include <SkGradientShader.h> + +#include "GradientCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +GradientCache::GradientCache(uint32_t maxByteSize): + mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity), + mSize(0), mMaxSize(maxByteSize) { + mCache.setOnEntryRemovedListener(this); +} + +GradientCache::~GradientCache() { + mCache.clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Size management +/////////////////////////////////////////////////////////////////////////////// + +uint32_t GradientCache::getSize() { + return mSize; +} + +uint32_t GradientCache::getMaxSize() { + return mMaxSize; +} + +void GradientCache::setMaxSize(uint32_t maxSize) { + mMaxSize = maxSize; + while (mSize > mMaxSize) { + mCache.removeOldest(); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Callbacks +/////////////////////////////////////////////////////////////////////////////// + +void GradientCache::operator()(SkShader*& shader, Texture*& texture) { + if (shader) { + const uint32_t size = texture->width * texture->height * 4; + mSize -= size; + } + + if (texture) { + glDeleteTextures(1, &texture->id); + delete texture; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Caching +/////////////////////////////////////////////////////////////////////////////// + +Texture* GradientCache::get(SkShader* shader) { + return mCache.get(shader); +} + +void GradientCache::remove(SkShader* shader) { + mCache.remove(shader); +} + +void GradientCache::clear() { + mCache.clear(); +} + +Texture* GradientCache::addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors, + float* positions, int count, SkShader::TileMode tileMode) { + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1); + bitmap.allocPixels(); + bitmap.eraseColor(0); + + SkCanvas canvas(bitmap); + + SkPoint points[2]; + points[0].set(0.0f, 0.0f); + points[1].set(bitmap.width(), 0.0f); + + SkShader* localShader = SkGradientShader::CreateLinear(points, + reinterpret_cast<const SkColor*>(colors), positions, count, tileMode); + + SkPaint p; + p.setStyle(SkPaint::kStrokeAndFill_Style); + p.setShader(localShader)->unref(); + + canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p); + + // Asume the cache is always big enough + const uint32_t size = bitmap.rowBytes() * bitmap.height(); + while (mSize + size > mMaxSize) { + mCache.removeOldest(); + } + + Texture* texture = new Texture; + generateTexture(&bitmap, texture); + + mSize += size; + mCache.put(shader, texture); + + return texture; +} + +void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) { + SkAutoLockPixels autoLock(*bitmap); + if (!bitmap->readyToDraw()) { + LOGE("Cannot generate texture from shader"); + return; + } + + texture->generation = bitmap->getGenerationID(); + texture->width = bitmap->width(); + texture->height = bitmap->height(); + + glGenTextures(1, &texture->id); + + glBindTexture(GL_TEXTURE_2D, texture->id); + glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); + + texture->blend = !bitmap->isOpaque(); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/GradientCache.h b/libs/hwui/GradientCache.h new file mode 100644 index 0000000..12c8a23 --- /dev/null +++ b/libs/hwui/GradientCache.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_GRADIENT_CACHE_H +#define ANDROID_UI_GRADIENT_CACHE_H + +#include <SkShader.h> + +#include "Texture.h" +#include "GenerationCache.h" + +namespace android { +namespace uirenderer { + +/** + * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. + * Any texture added to the cache causing the cache to grow beyond the maximum + * allowed size will also cause the oldest texture to be kicked out. + */ +class GradientCache: public OnEntryRemoved<SkShader*, Texture*> { +public: + GradientCache(uint32_t maxByteSize); + ~GradientCache(); + + /** + * Used as a callback when an entry is removed from the cache. + * Do not invoke directly. + */ + void operator()(SkShader*& shader, Texture*& texture); + + /** + * Adds a new linear gradient to the cache. The generated texture is + * returned. + */ + Texture* addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors, + float* positions, int count, SkShader::TileMode tileMode); + /** + * Returns the texture associated with the specified shader. + */ + Texture* get(SkShader* shader); + /** + * Removes the texture associated with the specified shader. Returns NULL + * if the texture cannot be found. Upon remove the texture is freed. + */ + void remove(SkShader* shader); + /** + * Clears the cache. This causes all textures to be deleted. + */ + void clear(); + + /** + * Sets the maximum size of the cache in bytes. + */ + void setMaxSize(uint32_t maxSize); + /** + * Returns the maximum size of the cache in bytes. + */ + uint32_t getMaxSize(); + /** + * Returns the current size of the cache in bytes. + */ + uint32_t getSize(); + +private: + void generateTexture(SkBitmap* bitmap, Texture* texture); + + GenerationCache<SkShader*, Texture*> mCache; + + uint32_t mSize; + uint32_t mMaxSize; +}; // class GradientCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_GRADIENT_CACHE_H diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h new file mode 100644 index 0000000..d4db782 --- /dev/null +++ b/libs/hwui/Layer.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_LAYER_H +#define ANDROID_UI_LAYER_H + +#include <sys/types.h> + +#include <GLES2/gl2.h> + +#include <SkXfermode.h> + +#include "Rect.h" + +namespace android { +namespace uirenderer { + +/** + * Dimensions of a layer. + */ +struct LayerSize { + LayerSize(): width(0), height(0), id(0) { } + LayerSize(const uint32_t width, const uint32_t height): width(width), height(height), id(0) { } + LayerSize(const LayerSize& size): width(size.width), height(size.height), id(size.id) { } + + uint32_t width; + uint32_t height; + + // Incremental id used by the layer cache to store multiple + // LayerSize with the same dimensions + uint32_t id; + + bool operator<(const LayerSize& rhs) const { + if (id != 0 && rhs.id != 0) { + return id < rhs.id; + } + if (width == rhs.width) { + return height < rhs.height; + } + return width < rhs.width; + } + + bool operator==(const LayerSize& rhs) const { + return width == rhs.width && height == rhs.height; + } +}; // struct LayerSize + +/** + * A layer has dimensions and is backed by an OpenGL texture. + */ +struct Layer { + /** + * Coordinates of the layer corresponding to this snapshot. + * Only set when the flag kFlagIsLayer is set. + */ + Rect layer; + /** + * Name of the texture used to render the layer. + * Only set when the flag kFlagIsLayer is set. + */ + GLuint texture; + /** + * Name of the FBO used to render the layer. + * Only set when the flag kFlagIsLayer is set. + */ + GLuint fbo; + /** + * Opacity of the layer. + * Only set when the flag kFlagIsLayer is set. + */ + float alpha; + /** + * Blending mode of the layer. + * Only set when the flag kFlagIsLayer is set. + */ + SkXfermode::Mode mode; + /** + * Indicates whether this layer should be blended. + */ + bool blend; +}; // struct Layer + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_LAYER_H diff --git a/libs/hwui/LayerCache.cpp b/libs/hwui/LayerCache.cpp new file mode 100644 index 0000000..3d263a3 --- /dev/null +++ b/libs/hwui/LayerCache.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <GLES2/gl2.h> + +#include <utils/Log.h> + +#include "LayerCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +LayerCache::LayerCache(uint32_t maxByteSize): + mCache(GenerationCache<LayerSize, Layer*>::kUnlimitedCapacity), + mIdGenerator(1), mSize(0), mMaxSize(maxByteSize) { +} + +LayerCache::~LayerCache() { + clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Size management +/////////////////////////////////////////////////////////////////////////////// + +uint32_t LayerCache::getSize() { + return mSize; +} + +uint32_t LayerCache::getMaxSize() { + return mMaxSize; +} + +void LayerCache::setMaxSize(uint32_t maxSize) { + mMaxSize = maxSize; + while (mSize > mMaxSize) { + Layer* oldest = mCache.removeOldest(); + deleteLayer(oldest); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Callbacks +/////////////////////////////////////////////////////////////////////////////// + +void LayerCache::operator()(LayerSize& size, Layer*& layer) { + deleteLayer(layer); +} + +/////////////////////////////////////////////////////////////////////////////// +// Caching +/////////////////////////////////////////////////////////////////////////////// + +void LayerCache::deleteLayer(Layer* layer) { + if (layer) { + mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4; + + glDeleteFramebuffers(1, &layer->fbo); + glDeleteTextures(1, &layer->texture); + delete layer; + } +} + +void LayerCache::clear() { + mCache.setOnEntryRemovedListener(this); + mCache.clear(); + mCache.setOnEntryRemovedListener(NULL); +} + +Layer* LayerCache::get(LayerSize& size, GLuint previousFbo) { + Layer* layer = mCache.remove(size); + if (layer) { + LAYER_LOGD("Reusing layer"); + + mSize -= layer->layer.getWidth() * layer->layer.getHeight() * 4; + } else { + LAYER_LOGD("Creating new layer"); + + layer = new Layer; + layer->blend = true; + + // Generate the FBO and attach the texture + glGenFramebuffers(1, &layer->fbo); + glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); + + // Generate the texture in which the FBO will draw + glGenTextures(1, &layer->texture); + glBindTexture(GL_TEXTURE_2D, layer->texture); + + // The FBO will not be scaled, so we can use lower quality filtering + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + // Bind texture to FBO + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + layer->texture, 0); + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + LOGE("Framebuffer incomplete (GL error code 0x%x)", status); + + glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); + + glDeleteFramebuffers(1, &layer->fbo); + glDeleteTextures(1, &layer->texture); + delete layer; + + return NULL; + } + } + + return layer; +} + +bool LayerCache::put(LayerSize& layerSize, Layer* layer) { + const uint32_t size = layerSize.width * layerSize.height * 4; + // Don't even try to cache a layer that's bigger than the cache + if (size < mMaxSize) { + while (mSize + size > mMaxSize) { + Layer* oldest = mCache.removeOldest(); + deleteLayer(oldest); + } + + layerSize.id = mIdGenerator++; + mCache.put(layerSize, layer); + mSize += size; + + return true; + } + return false; +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/LayerCache.h b/libs/hwui/LayerCache.h new file mode 100644 index 0000000..2580551 --- /dev/null +++ b/libs/hwui/LayerCache.h @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_LAYER_CACHE_H +#define ANDROID_UI_LAYER_CACHE_H + +#include "Layer.h" +#include "GenerationCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +// Debug +#define DEBUG_LAYERS 0 + +// Debug +#if DEBUG_LAYERS + #define LAYER_LOGD(...) LOGD(__VA_ARGS__) +#else + #define LAYER_LOGD(...) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Cache +/////////////////////////////////////////////////////////////////////////////// + +class LayerCache: public OnEntryRemoved<LayerSize, Layer*> { +public: + LayerCache(uint32_t maxByteSize); + ~LayerCache(); + + /** + * Used as a callback when an entry is removed from the cache. + * Do not invoke directly. + */ + void operator()(LayerSize& size, Layer*& layer); + + /** + * Returns the layer of specified dimensions. If not suitable layer + * can be found, a new one is created and returned. If creating a new + * layer fails, NULL is returned. + * + * When a layer is obtained from the cache, it is removed and the total + * size of the cache goes down. + * + * @param size The dimensions of the desired layer + * @param previousFbo The name of the FBO to bind to if creating a new + * layer fails + */ + Layer* get(LayerSize& size, GLuint previousFbo); + /** + * Adds the layer to the cache. The layer will not be added if there is + * not enough space available. + * + * @param size The dimensions of the layer + * @param layer The layer to add to the cache + * + * @return True if the layer was added, false otherwise. + */ + bool put(LayerSize& size, Layer* layer); + /** + * Clears the cache. This causes all layers to be deleted. + */ + void clear(); + + /** + * Sets the maximum size of the cache in bytes. + */ + void setMaxSize(uint32_t maxSize); + /** + * Returns the maximum size of the cache in bytes. + */ + uint32_t getMaxSize(); + /** + * Returns the current size of the cache in bytes. + */ + uint32_t getSize(); + +private: + void deleteLayer(Layer* layer); + + GenerationCache<LayerSize, Layer*> mCache; + uint32_t mIdGenerator; + + uint32_t mSize; + uint32_t mMaxSize; +}; // class LayerCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_LAYER_CACHE_H diff --git a/libs/hwui/MODULE_LICENSE_APACHE2 b/libs/hwui/MODULE_LICENSE_APACHE2 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/libs/hwui/MODULE_LICENSE_APACHE2 diff --git a/libs/hwui/Matrix.cpp b/libs/hwui/Matrix.cpp new file mode 100644 index 0000000..0c31ba9 --- /dev/null +++ b/libs/hwui/Matrix.cpp @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#include <utils/Log.h> + +#include <SkMatrix.h> + +#include "Matrix.h" + +namespace android { +namespace uirenderer { + +void Matrix4::loadIdentity() { + data[0] = 1.0f; + data[1] = 0.0f; + data[2] = 0.0f; + data[3] = 0.0f; + + data[4] = 0.0f; + data[5] = 1.0f; + data[6] = 0.0f; + data[7] = 0.0f; + + data[8] = 0.0f; + data[9] = 0.0f; + data[10] = 1.0f; + data[11] = 0.0f; + + data[12] = 0.0f; + data[13] = 0.0f; + data[14] = 0.0f; + data[15] = 1.0f; +} + +void Matrix4::load(const float* v) { + memcpy(data, v, sizeof(data)); +} + +void Matrix4::load(const Matrix4& v) { + memcpy(data, v.data, sizeof(data)); +} + +void Matrix4::load(const SkMatrix& v) { + memset(data, 0, sizeof(data)); + + data[0] = v[SkMatrix::kMScaleX]; + data[4] = v[SkMatrix::kMSkewX]; + data[12] = v[SkMatrix::kMTransX]; + + data[1] = v[SkMatrix::kMSkewY]; + data[5] = v[SkMatrix::kMScaleY]; + data[13] = v[SkMatrix::kMTransY]; + + data[3] = v[SkMatrix::kMPersp0]; + data[7] = v[SkMatrix::kMPersp1]; + data[15] = v[SkMatrix::kMPersp2]; + + data[10] = 1.0f; +} + +void Matrix4::copyTo(SkMatrix& v) const { + v.reset(); + + v.set(SkMatrix::kMScaleX, data[0]); + v.set(SkMatrix::kMSkewX, data[4]); + v.set(SkMatrix::kMTransX, data[12]); + + v.set(SkMatrix::kMSkewY, data[1]); + v.set(SkMatrix::kMScaleY, data[5]); + v.set(SkMatrix::kMTransY, data[13]); + + v.set(SkMatrix::kMPersp0, data[3]); + v.set(SkMatrix::kMPersp1, data[7]); + v.set(SkMatrix::kMPersp2, data[15]); +} + +void Matrix4::loadInverse(const Matrix4& v) { + double scale = 1.0 / + (v.data[0] * ((double) v.data[5] * v.data[15] - (double) v.data[13] * v.data[7]) + + v.data[4] * ((double) v.data[13] * v.data[3] - (double) v.data[1] * v.data[15]) + + v.data[12] * ((double) v.data[1] * v.data[7] - (double) v.data[5] * v.data[3])); + + data[0] = (v.data[5] * v.data[15] - v.data[13] * v.data[7]) * scale; + data[4] = (v.data[12] * v.data[7] - v.data[4] * v.data[15]) * scale; + data[12] = (v.data[4] * v.data[13] - v.data[12] * v.data[5]) * scale; + + data[1] = (v.data[13] * v.data[3] - v.data[1] * v.data[15]) * scale; + data[5] = (v.data[0] * v.data[15] - v.data[12] * v.data[3]) * scale; + data[13] = (v.data[12] * v.data[1] - v.data[0] * v.data[13]) * scale; + + data[3] = (v.data[1] * v.data[7] - v.data[5] * v.data[3]) * scale; + data[7] = (v.data[4] * v.data[3] - v.data[0] * v.data[7]) * scale; + data[15] = (v.data[0] * v.data[5] - v.data[4] * v.data[1]) * scale; +} + +void Matrix4::copyTo(float* v) const { + memcpy(v, data, sizeof(data)); +} + +float Matrix4::getTranslateX() { + return data[12]; +} + +float Matrix4::getTranslateY() { + return data[13]; +} + +void Matrix4::loadTranslate(float x, float y, float z) { + loadIdentity(); + data[12] = x; + data[13] = y; + data[14] = z; +} + +void Matrix4::loadScale(float sx, float sy, float sz) { + loadIdentity(); + data[0] = sx; + data[5] = sy; + data[10] = sz; +} + +void Matrix4::loadRotate(float angle, float x, float y, float z) { + data[3] = 0.0f; + data[7] = 0.0f; + data[11] = 0.0f; + data[12] = 0.0f; + data[13] = 0.0f; + data[14] = 0.0f; + data[15] = 1.0f; + + angle *= float(M_PI / 180.0f); + float c = cosf(angle); + float s = sinf(angle); + + const float length = sqrtf(x * x + y * y + z * z); + float recipLen = 1.0f / length; + x *= recipLen; + y *= recipLen; + z *= recipLen; + + const float nc = 1.0f - c; + const float xy = x * y; + const float yz = y * z; + const float zx = z * x; + const float xs = x * s; + const float ys = y * s; + const float zs = z * s; + + data[0] = x * x * nc + c; + data[4] = xy * nc - zs; + data[8] = zx * nc + ys; + data[1] = xy * nc + zs; + data[5] = y * y * nc + c; + data[9] = yz * nc - xs; + data[2] = zx * nc - ys; + data[6] = yz * nc + xs; + data[10] = z * z * nc + c; +} + +void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { + for (int i = 0 ; i < 4 ; i++) { + float x = 0; + float y = 0; + float z = 0; + float w = 0; + + for (int j = 0 ; j < 4 ; j++) { + const float e = v.get(i, j); + x += u.get(j, 0) * e; + y += u.get(j, 1) * e; + z += u.get(j, 2) * e; + w += u.get(j, 3) * e; + } + + set(i, 0, x); + set(i, 1, y); + set(i, 2, z); + set(i, 3, w); + } +} + +void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { + loadIdentity(); + data[0] = 2.0f / (right - left); + data[5] = 2.0f / (top - bottom); + data[10] = -2.0f / (far - near); + data[12] = -(right + left) / (right - left); + data[13] = -(top + bottom) / (top - bottom); + data[14] = -(far + near) / (far - near); +} + +#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) + +void Matrix4::mapRect(Rect& r) const { + const float sx = data[0]; + const float sy = data[5]; + + const float tx = data[12]; + const float ty = data[13]; + + MUL_ADD_STORE(r.left, sx, tx); + MUL_ADD_STORE(r.right, sx, tx); + MUL_ADD_STORE(r.top, sy, ty); + MUL_ADD_STORE(r.bottom, sy, ty); +} + +void Matrix4::dump() const { + LOGD("Matrix4["); + LOGD(" %f %f %f %f", data[0], data[4], data[ 8], data[12]); + LOGD(" %f %f %f %f", data[1], data[5], data[ 9], data[13]); + LOGD(" %f %f %f %f", data[2], data[6], data[10], data[14]); + LOGD(" %f %f %f %f", data[3], data[7], data[11], data[15]); + LOGD("]"); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/Matrix.h b/libs/hwui/Matrix.h new file mode 100644 index 0000000..b8a4da7 --- /dev/null +++ b/libs/hwui/Matrix.h @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_MATRIX_H +#define ANDROID_UI_MATRIX_H + +#include <SkMatrix.h> + +#include "Rect.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Classes +/////////////////////////////////////////////////////////////////////////////// + +class Matrix4 { +public: + float data[16]; + + Matrix4() { + loadIdentity(); + } + + Matrix4(const float* v) { + load(v); + } + + Matrix4(const Matrix4& v) { + load(v); + } + + Matrix4(const SkMatrix& v) { + load(v); + } + + void loadIdentity(); + + void load(const float* v); + void load(const Matrix4& v); + void load(const SkMatrix& v); + + void loadInverse(const Matrix4& v); + + void loadTranslate(float x, float y, float z); + void loadScale(float sx, float sy, float sz); + void loadRotate(float angle, float x, float y, float z); + void loadMultiply(const Matrix4& u, const Matrix4& v); + + void loadOrtho(float left, float right, float bottom, float top, float near, float far); + + void multiply(const Matrix4& v) { + Matrix4 u; + u.loadMultiply(*this, v); + load(u); + } + + void translate(float x, float y, float z) { + Matrix4 u; + u.loadTranslate(x, y, z); + multiply(u); + } + + void scale(float sx, float sy, float sz) { + Matrix4 u; + u.loadScale(sx, sy, sz); + multiply(u); + } + + void rotate(float angle, float x, float y, float z) { + Matrix4 u; + u.loadRotate(angle, x, y, z); + multiply(u); + } + + void copyTo(float* v) const; + void copyTo(SkMatrix& v) const; + + /** + * Does not apply rotations! + */ + void mapRect(Rect& r) const; + + float getTranslateX(); + float getTranslateY(); + + void dump() const; + +private: + inline float get(int i, int j) const { + return data[i * 4 + j]; + } + + inline void set(int i, int j, float v) { + data[i * 4 + j] = v; + } +}; // class Matrix4 + +/////////////////////////////////////////////////////////////////////////////// +// Types +/////////////////////////////////////////////////////////////////////////////// + +typedef Matrix4 mat4; + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_MATRIX_H diff --git a/libs/hwui/NOTICE b/libs/hwui/NOTICE new file mode 100644 index 0000000..c5b1efa --- /dev/null +++ b/libs/hwui/NOTICE @@ -0,0 +1,190 @@ + + Copyright (c) 2005-2008, 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. + + 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. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp new file mode 100644 index 0000000..aaf7564 --- /dev/null +++ b/libs/hwui/OpenGLRenderer.cpp @@ -0,0 +1,959 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <stdlib.h> +#include <stdint.h> +#include <sys/types.h> + +#include <SkCanvas.h> +#include <SkTypeface.h> + +#include <cutils/properties.h> +#include <utils/Log.h> + +#include "OpenGLRenderer.h" +#include "Properties.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f +#define DEFAULT_LAYER_CACHE_SIZE 6.0f +#define DEFAULT_PATH_CACHE_SIZE 6.0f +#define DEFAULT_PATCH_CACHE_SIZE 100 +#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f + +#define REQUIRED_TEXTURE_UNITS_COUNT 3 + +// Converts a number of mega-bytes into bytes +#define MB(s) s * 1024 * 1024 + +// Generates simple and textured vertices +#define FV(x, y, u, v) { { x, y }, { u, v } } + +/////////////////////////////////////////////////////////////////////////////// +// Globals +/////////////////////////////////////////////////////////////////////////////// + +// This array is never used directly but used as a memcpy source in the +// OpenGLRenderer constructor +static const TextureVertex gMeshVertices[] = { + FV(0.0f, 0.0f, 0.0f, 0.0f), + FV(1.0f, 0.0f, 1.0f, 0.0f), + FV(0.0f, 1.0f, 0.0f, 1.0f), + FV(1.0f, 1.0f, 1.0f, 1.0f) +}; +static const GLsizei gMeshStride = sizeof(TextureVertex); +static const GLsizei gMeshCount = 4; + +/** + * Structure mapping Skia xfermodes to OpenGL blending factors. + */ +struct Blender { + SkXfermode::Mode mode; + GLenum src; + GLenum dst; +}; // struct Blender + +// In this array, the index of each Blender equals the value of the first +// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] +static const Blender gBlends[] = { + { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, + { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, + { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, + { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, + { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, + { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, + { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, + { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, + { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, + { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, + { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, + { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } +}; + +static const GLenum gTextureUnits[] = { + GL_TEXTURE0, + GL_TEXTURE1, + GL_TEXTURE2 +}; + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +OpenGLRenderer::OpenGLRenderer(): + mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), + mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), + mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), + mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)), + mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)), + mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { + LOGD("Create OpenGLRenderer"); + + char property[PROPERTY_VALUE_MAX]; + if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { + LOGD(" Setting texture cache size to %sMB", property); + mTextureCache.setMaxSize(MB(atof(property))); + } else { + LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); + } + + if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { + LOGD(" Setting layer cache size to %sMB", property); + mLayerCache.setMaxSize(MB(atof(property))); + } else { + LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); + } + + if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { + LOGD(" Setting gradient cache size to %sMB", property); + mGradientCache.setMaxSize(MB(atof(property))); + } else { + LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); + } + + if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) { + LOGD(" Setting path cache size to %sMB", property); + mPathCache.setMaxSize(MB(atof(property))); + } else { + LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE); + } + + mCurrentProgram = NULL; + mShader = NULL; + mColorFilter = NULL; + + memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); + + mFirstSnapshot = new Snapshot; + + GLint maxTextureUnits; + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); + if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { + LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); + } +} + +OpenGLRenderer::~OpenGLRenderer() { + LOGD("Destroy OpenGLRenderer"); + + mTextureCache.clear(); + mLayerCache.clear(); + mGradientCache.clear(); + mPatchCache.clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Setup +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::setViewport(int width, int height) { + glViewport(0, 0, width, height); + + mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); + + mWidth = width; + mHeight = height; + mFirstSnapshot->height = height; +} + +void OpenGLRenderer::prepare() { + mSnapshot = new Snapshot(mFirstSnapshot); + mSaveCount = 0; + + glDisable(GL_SCISSOR_TEST); + + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glEnable(GL_SCISSOR_TEST); + glScissor(0, 0, mWidth, mHeight); + + mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); +} + +/////////////////////////////////////////////////////////////////////////////// +// State management +/////////////////////////////////////////////////////////////////////////////// + +int OpenGLRenderer::getSaveCount() const { + return mSaveCount; +} + +int OpenGLRenderer::save(int flags) { + return saveSnapshot(); +} + +void OpenGLRenderer::restore() { + if (mSaveCount == 0) return; + + if (restoreSnapshot()) { + setScissorFromClip(); + } +} + +void OpenGLRenderer::restoreToCount(int saveCount) { + if (saveCount <= 0 || saveCount > mSaveCount) return; + + bool restoreClip = false; + + while (mSaveCount != saveCount - 1) { + restoreClip |= restoreSnapshot(); + } + + if (restoreClip) { + setScissorFromClip(); + } +} + +int OpenGLRenderer::saveSnapshot() { + mSnapshot = new Snapshot(mSnapshot); + return ++mSaveCount; +} + +bool OpenGLRenderer::restoreSnapshot() { + bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; + bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; + bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; + + sp<Snapshot> current = mSnapshot; + sp<Snapshot> previous = mSnapshot->previous; + + if (restoreOrtho) { + mOrthoMatrix.load(current->orthoMatrix); + } + + if (restoreLayer) { + composeLayer(current, previous); + } + + mSnapshot = previous; + mSaveCount--; + + return restoreClip; +} + +void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { + if (!current->layer) { + LOGE("Attempting to compose a layer that does not exist"); + return; + } + + // Unbind current FBO and restore previous one + // Most of the time, previous->fbo will be 0 to bind the default buffer + glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); + + // Restore the clip from the previous snapshot + const Rect& clip = previous->clipRect; + glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); + + Layer* layer = current->layer; + const Rect& rect = layer->layer; + + drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, + layer->texture, layer->alpha, layer->mode, layer->blend); + + LayerSize size(rect.getWidth(), rect.getHeight()); + // Failing to add the layer to the cache should happen only if the + // layer is too large + if (!mLayerCache.put(size, layer)) { + LAYER_LOGD("Deleting layer"); + + glDeleteFramebuffers(1, &layer->fbo); + glDeleteTextures(1, &layer->texture); + + delete layer; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Layers +/////////////////////////////////////////////////////////////////////////////// + +int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, + const SkPaint* p, int flags) { + int count = saveSnapshot(); + + int alpha = 255; + SkXfermode::Mode mode; + + if (p) { + alpha = p->getAlpha(); + const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); + if (!isMode) { + // Assume SRC_OVER + mode = SkXfermode::kSrcOver_Mode; + } + } else { + mode = SkXfermode::kSrcOver_Mode; + } + + createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); + + return count; +} + +int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, + int alpha, int flags) { + int count = saveSnapshot(); + createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); + return count; +} + +bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, + float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { + LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); + LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); + + GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; + LayerSize size(right - left, bottom - top); + + Layer* layer = mLayerCache.get(size, previousFbo); + if (!layer) { + return false; + } + + glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); + + // Clear the FBO + glDisable(GL_SCISSOR_TEST); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + glEnable(GL_SCISSOR_TEST); + + // Save the layer in the snapshot + snapshot->flags |= Snapshot::kFlagIsLayer; + layer->mode = mode; + layer->alpha = alpha / 255.0f; + layer->layer.set(left, top, right, bottom); + + snapshot->layer = layer; + snapshot->fbo = layer->fbo; + + // Creates a new snapshot to draw into the FBO + saveSnapshot(); + // TODO: This doesn't preserve other transformations (check Skia first) + mSnapshot->transform.loadTranslate(-left, -top, 0.0f); + mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top); + mSnapshot->height = bottom - top; + setScissorFromClip(); + + mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet; + mSnapshot->orthoMatrix.load(mOrthoMatrix); + + // Change the ortho projection + mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); + + return true; +} + +/////////////////////////////////////////////////////////////////////////////// +// Transforms +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::translate(float dx, float dy) { + mSnapshot->transform.translate(dx, dy, 0.0f); +} + +void OpenGLRenderer::rotate(float degrees) { + mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); +} + +void OpenGLRenderer::scale(float sx, float sy) { + mSnapshot->transform.scale(sx, sy, 1.0f); +} + +void OpenGLRenderer::setMatrix(SkMatrix* matrix) { + mSnapshot->transform.load(*matrix); +} + +void OpenGLRenderer::getMatrix(SkMatrix* matrix) { + mSnapshot->transform.copyTo(*matrix); +} + +void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { + mat4 m(*matrix); + mSnapshot->transform.multiply(m); +} + +/////////////////////////////////////////////////////////////////////////////// +// Clipping +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::setScissorFromClip() { + const Rect& clip = mSnapshot->clipRect; + glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); +} + +const Rect& OpenGLRenderer::getClipBounds() { + return mSnapshot->getLocalClip(); +} + +bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { + Rect r(left, top, right, bottom); + mSnapshot->transform.mapRect(r); + return !mSnapshot->clipRect.intersects(r); +} + +bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { + bool clipped = mSnapshot->clip(left, top, right, bottom, op); + if (clipped) { + setScissorFromClip(); + } + return !mSnapshot->clipRect.isEmpty(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Drawing +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { + const float right = left + bitmap->width(); + const float bottom = top + bitmap->height(); + + if (quickReject(left, top, right, bottom)) { + return; + } + + glActiveTexture(GL_TEXTURE0); + const Texture* texture = mTextureCache.get(bitmap); + if (!texture) return; + const AutoTexture autoCleanup(texture); + + drawTextureRect(left, top, right, bottom, texture, paint); +} + +void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { + Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); + const mat4 transform(*matrix); + transform.mapRect(r); + + if (quickReject(r.left, r.top, r.right, r.bottom)) { + return; + } + + glActiveTexture(GL_TEXTURE0); + const Texture* texture = mTextureCache.get(bitmap); + if (!texture) return; + const AutoTexture autoCleanup(texture); + + drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); +} + +void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, + float srcLeft, float srcTop, float srcRight, float srcBottom, + float dstLeft, float dstTop, float dstRight, float dstBottom, + const SkPaint* paint) { + if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { + return; + } + + glActiveTexture(GL_TEXTURE0); + const Texture* texture = mTextureCache.get(bitmap); + if (!texture) return; + const AutoTexture autoCleanup(texture); + + const float width = texture->width; + const float height = texture->height; + + const float u1 = srcLeft / width; + const float v1 = srcTop / height; + const float u2 = srcRight / width; + const float v2 = srcBottom / height; + + resetDrawTextureTexCoords(u1, v1, u2, v2); + + drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); + + resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); +} + +void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, + float left, float top, float right, float bottom, const SkPaint* paint) { + if (quickReject(left, top, right, bottom)) { + return; + } + + glActiveTexture(GL_TEXTURE0); + const Texture* texture = mTextureCache.get(bitmap); + if (!texture) return; + const AutoTexture autoCleanup(texture); + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + Patch* mesh = mPatchCache.get(patch); + mesh->updateVertices(bitmap, left, top, right, bottom, + &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); + + // Specify right and bottom as +1.0f from left/top to prevent scaling since the + // patch mesh already defines the final size + drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, + mode, texture->blend, &mesh->vertices[0].position[0], + &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); +} + +void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { + const Rect& clip = mSnapshot->clipRect; + drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); +} + +void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { + if (quickReject(left, top, right, bottom)) { + return; + } + + SkXfermode::Mode mode; + + const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); + if (!isMode) { + // Assume SRC_OVER + mode = SkXfermode::kSrcOver_Mode; + } + + // Skia draws using the color's alpha channel if < 255 + // Otherwise, it uses the paint's alpha + int color = p->getColor(); + if (((color >> 24) & 0xff) == 255) { + color |= p->getAlpha() << 24; + } + + drawColorRect(left, top, right, bottom, color, mode); +} + +#define kStdStrikeThru_Offset (-6.0f / 21.0f) +#define kStdUnderline_Offset (1.0f / 9.0f) +#define kStdUnderline_Thickness (1.0f / 18.0f) + +void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, + float x, float y, SkPaint* paint) { + if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { + return; + } + + float length = -1.0f; + switch (paint->getTextAlign()) { + case SkPaint::kCenter_Align: + length = paint->measureText(text, bytesCount); + x -= length / 2.0f; + break; + case SkPaint::kRight_Align: + length = paint->measureText(text, bytesCount); + x -= length; + break; + default: + break; + } + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + uint32_t color = paint->getColor(); + const GLfloat a = alpha / 255.0f; + const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; + const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; + const GLfloat b = a * ((color ) & 0xFF) / 255.0f; + + mModelView.loadIdentity(); + + GLuint textureUnit = 0; + // Needs to be set prior to calling FontRenderer::getTexture() + glActiveTexture(gTextureUnits[textureUnit]); + + ProgramDescription description; + description.hasTexture = true; + description.hasAlpha8Texture = true; + if (mShader) { + mShader->describe(description, mExtensions); + } + if (mColorFilter) { + mColorFilter->describe(description, mExtensions); + } + + useProgram(mProgramCache.get(description)); + mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); + + // Text is always blended, no need to check the shader + chooseBlending(true, mode); + bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); + glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); + + int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); + glEnableVertexAttribArray(texCoordsSlot); + + // Always premultiplied + glUniform4f(mCurrentProgram->color, r, g, b, a); + + textureUnit++; + // Setup attributes and uniforms required by the shaders + if (mShader) { + mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); + } + if (mColorFilter) { + mColorFilter->setupProgram(mCurrentProgram); + } + + const Rect& clip = mSnapshot->getLocalClip(); + mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize()); + mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDisableVertexAttribArray(texCoordsSlot); + + // Handle underline and strike-through + uint32_t flags = paint->getFlags(); + if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { + float underlineWidth = length; + // If length is > 0.0f, we already measured the text for the text alignment + if (length <= 0.0f) { + underlineWidth = paint->measureText(text, bytesCount); + } + + float offsetX = 0; + switch (paint->getTextAlign()) { + case SkPaint::kCenter_Align: + offsetX = underlineWidth * 0.5f; + break; + case SkPaint::kRight_Align: + offsetX = underlineWidth; + break; + default: + break; + } + + if (underlineWidth > 0.0f) { + float textSize = paint->getTextSize(); + float height = textSize * kStdUnderline_Thickness; + + float left = x - offsetX; + float top = 0.0f; + float right = left + underlineWidth; + float bottom = 0.0f; + + if (flags & SkPaint::kUnderlineText_Flag) { + top = y + textSize * kStdUnderline_Offset; + bottom = top + height; + drawRect(left, top, right, bottom, paint); + } + + if (flags & SkPaint::kStrikeThruText_Flag) { + top = y + textSize * kStdStrikeThru_Offset; + bottom = top + height; + drawRect(left, top, right, bottom, paint); + } + } + } +} + +void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { + GLuint textureUnit = 0; + glActiveTexture(gTextureUnits[textureUnit]); + + const PathTexture* texture = mPathCache.get(path, paint); + if (!texture) return; + const AutoTexture autoCleanup(texture); + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + uint32_t color = paint->getColor(); + const GLfloat a = alpha / 255.0f; + const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; + const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; + const GLfloat b = a * ((color ) & 0xFF) / 255.0f; + + // Describe the required shaders + ProgramDescription description; + description.hasTexture = true; + description.hasAlpha8Texture = true; + if (mShader) { + mShader->describe(description, mExtensions); + } + if (mColorFilter) { + mColorFilter->describe(description, mExtensions); + } + + // Build and use the appropriate shader + useProgram(mProgramCache.get(description)); + + // Setup the blending mode + chooseBlending(true, mode); + bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); + glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); + + int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); + glEnableVertexAttribArray(texCoordsSlot); + + // Setup attributes + glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, + gMeshStride, &mMeshVertices[0].position[0]); + glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, + gMeshStride, &mMeshVertices[0].texture[0]); + + // Setup uniforms + mModelView.loadTranslate(texture->left - texture->offset, + texture->top - texture->offset, 0.0f); + mModelView.scale(texture->width, texture->height, 1.0f); + mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); + + glUniform4f(mCurrentProgram->color, r, g, b, a); + + textureUnit++; + // Setup attributes and uniforms required by the shaders + if (mShader) { + mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); + } + if (mColorFilter) { + mColorFilter->setupProgram(mCurrentProgram); + } + + // Draw the mesh + glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); + + glDisableVertexAttribArray(texCoordsSlot); +} + +/////////////////////////////////////////////////////////////////////////////// +// Shaders +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::resetShader() { + mShader = NULL; +} + +void OpenGLRenderer::setupShader(SkiaShader* shader) { + mShader = shader; + if (mShader) { + mShader->set(&mTextureCache, &mGradientCache); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Color filters +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::resetColorFilter() { + mColorFilter = NULL; +} + +void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { + mColorFilter = filter; +} + +/////////////////////////////////////////////////////////////////////////////// +// Drawing implementation +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, + int color, SkXfermode::Mode mode, bool ignoreTransform) { + // If a shader is set, preserve only the alpha + if (mShader) { + color |= 0x00ffffff; + } + + // Render using pre-multiplied alpha + const int alpha = (color >> 24) & 0xFF; + const GLfloat a = alpha / 255.0f; + const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; + const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; + const GLfloat b = a * ((color ) & 0xFF) / 255.0f; + + GLuint textureUnit = 0; + + // Setup the blending mode + chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode); + + // Describe the required shaders + ProgramDescription description; + if (mShader) { + mShader->describe(description, mExtensions); + } + if (mColorFilter) { + mColorFilter->describe(description, mExtensions); + } + + // Build and use the appropriate shader + useProgram(mProgramCache.get(description)); + + // Setup attributes + glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, + gMeshStride, &mMeshVertices[0].position[0]); + + // Setup uniforms + mModelView.loadTranslate(left, top, 0.0f); + mModelView.scale(right - left, bottom - top, 1.0f); + if (!ignoreTransform) { + mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); + } else { + mat4 identity; + mCurrentProgram->set(mOrthoMatrix, mModelView, identity); + } + glUniform4f(mCurrentProgram->color, r, g, b, a); + + // Setup attributes and uniforms required by the shaders + if (mShader) { + mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); + } + if (mColorFilter) { + mColorFilter->setupProgram(mCurrentProgram); + } + + // Draw the mesh + glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); +} + +void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, + const Texture* texture, const SkPaint* paint) { + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, + &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); +} + +void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, + GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { + drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, + &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); +} + +void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, + GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, + GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { + ProgramDescription description; + description.hasTexture = true; + if (mColorFilter) { + mColorFilter->describe(description, mExtensions); + } + + mModelView.loadTranslate(left, top, 0.0f); + mModelView.scale(right - left, bottom - top, 1.0f); + + useProgram(mProgramCache.get(description)); + mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); + + chooseBlending(blend || alpha < 1.0f, mode); + + // Texture + bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); + glUniform1i(mCurrentProgram->getUniform("sampler"), 0); + + // Always premultiplied + glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha); + + // Mesh + int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); + glEnableVertexAttribArray(texCoordsSlot); + glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, + gMeshStride, vertices); + glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); + + // Color filter + if (mColorFilter) { + mColorFilter->setupProgram(mCurrentProgram); + } + + if (!indices) { + glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); + } else { + glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); + } + glDisableVertexAttribArray(texCoordsSlot); +} + +void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { + blend = blend || mode != SkXfermode::kSrcOver_Mode; + if (blend) { + if (!mBlend) { + glEnable(GL_BLEND); + } + + GLenum sourceMode = gBlends[mode].src; + GLenum destMode = gBlends[mode].dst; + if (!isPremultiplied && sourceMode == GL_ONE) { + sourceMode = GL_SRC_ALPHA; + } + + if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { + glBlendFunc(sourceMode, destMode); + mLastSrcMode = sourceMode; + mLastDstMode = destMode; + } + } else if (mBlend) { + glDisable(GL_BLEND); + } + mBlend = blend; +} + +bool OpenGLRenderer::useProgram(Program* program) { + if (!program->isInUse()) { + if (mCurrentProgram != NULL) mCurrentProgram->remove(); + program->use(); + mCurrentProgram = program; + return false; + } + return true; +} + +void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { + TextureVertex* v = &mMeshVertices[0]; + TextureVertex::setUV(v++, u1, v1); + TextureVertex::setUV(v++, u2, v1); + TextureVertex::setUV(v++, u1, v2); + TextureVertex::setUV(v++, u2, v2); +} + +void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { + if (paint) { + const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); + if (!isMode) { + // Assume SRC_OVER + *mode = SkXfermode::kSrcOver_Mode; + } + + // Skia draws using the color's alpha channel if < 255 + // Otherwise, it uses the paint's alpha + int color = paint->getColor(); + *alpha = (color >> 24) & 0xFF; + if (*alpha == 255) { + *alpha = paint->getAlpha(); + } + } else { + *mode = SkXfermode::kSrcOver_Mode; + *alpha = 255; + } +} + +void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { + glActiveTexture(gTextureUnits[textureUnit]); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h new file mode 100644 index 0000000..76783e9 --- /dev/null +++ b/libs/hwui/OpenGLRenderer.h @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_OPENGL_RENDERER_H +#define ANDROID_UI_OPENGL_RENDERER_H + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <SkBitmap.h> +#include <SkMatrix.h> +#include <SkPaint.h> +#include <SkRegion.h> +#include <SkShader.h> +#include <SkXfermode.h> + +#include <utils/RefBase.h> +#include <utils/ResourceTypes.h> + +#include "Extensions.h" +#include "Matrix.h" +#include "Program.h" +#include "Rect.h" +#include "Snapshot.h" +#include "TextureCache.h" +#include "LayerCache.h" +#include "GradientCache.h" +#include "PatchCache.h" +#include "Vertex.h" +#include "FontRenderer.h" +#include "ProgramCache.h" +#include "SkiaShader.h" +#include "SkiaColorFilter.h" +#include "PathCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Renderer +/////////////////////////////////////////////////////////////////////////////// + +/** + * OpenGL renderer used to draw accelerated 2D graphics. The API is a + * simplified version of Skia's Canvas API. + */ +class OpenGLRenderer { +public: + OpenGLRenderer(); + ~OpenGLRenderer(); + + void setViewport(int width, int height); + void prepare(); + + int getSaveCount() const; + int save(int flags); + void restore(); + void restoreToCount(int saveCount); + + int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags); + int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags); + + void translate(float dx, float dy); + void rotate(float degrees); + void scale(float sx, float sy); + + void setMatrix(SkMatrix* matrix); + void getMatrix(SkMatrix* matrix); + void concatMatrix(SkMatrix* matrix); + + const Rect& getClipBounds(); + bool quickReject(float left, float top, float right, float bottom); + bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); + + void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint); + void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint); + void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom, + float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint); + void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top, + float right, float bottom, const SkPaint* paint); + void drawColor(int color, SkXfermode::Mode mode); + void drawRect(float left, float top, float right, float bottom, const SkPaint* paint); + void drawPath(SkPath* path, SkPaint* paint); + + void resetShader(); + void setupShader(SkiaShader* shader); + + void resetColorFilter(); + void setupColorFilter(SkiaColorFilter* filter); + + void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint); + +private: + /** + * Saves the current state of the renderer as a new snapshot. + * The new snapshot is saved in mSnapshot and the previous snapshot + * is linked from mSnapshot->previous. + * + * @return The new save count. This value can be passed to #restoreToCount() + */ + int saveSnapshot(); + + /** + * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. + * + * @return True if the clip should be also reapplied by calling + * #setScissorFromClip(). + */ + bool restoreSnapshot(); + + /** + * Sets the clipping rectangle using glScissor. The clip is defined by + * the current snapshot's clipRect member. + */ + void setScissorFromClip(); + + /** + * Compose the layer defined in the current snapshot with the layer + * defined by the previous snapshot. + * + * The current snapshot *must* be a layer (flag kFlagIsLayer set.) + * + * @param curent The current snapshot containing the layer to compose + * @param previous The previous snapshot to compose the current layer with + */ + void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); + + /** + * Creates a new layer stored in the specified snapshot. + * + * @param snapshot The snapshot associated with the new layer + * @param left The left coordinate of the layer + * @param top The top coordinate of the layer + * @param right The right coordinate of the layer + * @param bottom The bottom coordinate of the layer + * @param alpha The translucency of the layer + * @param mode The blending mode of the layer + * @param flags The layer save flags + * + * @return True if the layer was successfully created, false otherwise + */ + bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom, + int alpha, SkXfermode::Mode mode, int flags); + + /** + * Draws a colored rectangle with the specified color. The specified coordinates + * are transformed by the current snapshot's transform matrix. + * + * @param left The left coordinate of the rectangle + * @param top The top coordinate of the rectangle + * @param right The right coordinate of the rectangle + * @param bottom The bottom coordinate of the rectangle + * @param color The rectangle's ARGB color, defined as a packed 32 bits word + * @param mode The Skia xfermode to use + * @param ignoreTransform True if the current transform should be ignored + */ + void drawColorRect(float left, float top, float right, float bottom, + int color, SkXfermode::Mode mode, bool ignoreTransform = false); + + /** + * Draws a textured rectangle with the specified texture. The specified coordinates + * are transformed by the current snapshot's transform matrix. + * + * @param left The left coordinate of the rectangle + * @param top The top coordinate of the rectangle + * @param right The right coordinate of the rectangle + * @param bottom The bottom coordinate of the rectangle + * @param texture The texture name to map onto the rectangle + * @param alpha An additional translucency parameter, between 0.0f and 1.0f + * @param mode The blending mode + * @param blend True if the texture contains an alpha channel + */ + void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, + float alpha, SkXfermode::Mode mode, bool blend); + + /** + * Draws a textured rectangle with the specified texture. The specified coordinates + * are transformed by the current snapshot's transform matrix. + * + * @param left The left coordinate of the rectangle + * @param top The top coordinate of the rectangle + * @param right The right coordinate of the rectangle + * @param bottom The bottom coordinate of the rectangle + * @param texture The texture to use + * @param paint The paint containing the alpha, blending mode, etc. + */ + void drawTextureRect(float left, float top, float right, float bottom, + const Texture* texture, const SkPaint* paint); + + /** + * Draws a textured mesh with the specified texture. If the indices are omitted, the + * mesh is drawn as a simple quad. + * + * @param left The left coordinate of the rectangle + * @param top The top coordinate of the rectangle + * @param right The right coordinate of the rectangle + * @param bottom The bottom coordinate of the rectangle + * @param texture The texture name to map onto the rectangle + * @param alpha An additional translucency parameter, between 0.0f and 1.0f + * @param mode The blending mode + * @param blend True if the texture contains an alpha channel + * @param vertices The vertices that define the mesh + * @param texCoords The texture coordinates of each vertex + * @param indices The indices of the vertices, can be NULL + * @param elementsCount The number of elements in the mesh, required by indices + */ + void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, + float alpha, SkXfermode::Mode mode, bool blend, + GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0); + + /** + * Resets the texture coordinates stored in mMeshVertices. Setting the values + * back to default is achieved by calling: + * + * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); + * + * @param u1 The left coordinate of the texture + * @param v1 The bottom coordinate of the texture + * @param u2 The right coordinate of the texture + * @param v2 The top coordinate of the texture + */ + void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); + + /** + * Gets the alpha and xfermode out of a paint object. If the paint is null + * alpha will be 255 and the xfermode will be SRC_OVER. + * + * @param paint The paint to extract values from + * @param alpha Where to store the resulting alpha + * @param mode Where to store the resulting xfermode + */ + inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode); + + /** + * Binds the specified texture with the specified wrap modes. + */ + inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit = 0); + + /** + * Enable or disable blending as necessary. This function sets the appropriate + * blend function based on the specified xfermode. + */ + inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true); + + /** + * Use the specified program with the current GL context. If the program is already + * in use, it will not be bound again. If it is not in use, the current program is + * marked unused and the specified program becomes used and becomes the new + * current program. + * + * @param program The program to use + * + * @return true If the specified program was already in use, false otherwise. + */ + inline bool useProgram(Program* program); + + // Dimensions of the drawing surface + int mWidth, mHeight; + + // Matrix used for ortho projection in shaders + mat4 mOrthoMatrix; + + // Model-view matrix used to position/size objects + mat4 mModelView; + + // Number of saved states + int mSaveCount; + // Base state + sp<Snapshot> mFirstSnapshot; + // Current state + sp<Snapshot> mSnapshot; + + // Shaders + Program* mCurrentProgram; + SkiaShader* mShader; + + // Color filters + SkiaColorFilter* mColorFilter; + + // Used to draw textured quads + TextureVertex mMeshVertices[4]; + + // Last known blend state + bool mBlend; + GLenum mLastSrcMode; + GLenum mLastDstMode; + + // GL extensions + Extensions mExtensions; + + // Font renderer + FontRenderer mFontRenderer; + + // Various caches + TextureCache mTextureCache; + LayerCache mLayerCache; + GradientCache mGradientCache; + ProgramCache mProgramCache; + PathCache mPathCache; + PatchCache mPatchCache; +}; // class OpenGLRenderer + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_OPENGL_RENDERER_H diff --git a/libs/hwui/Patch.cpp b/libs/hwui/Patch.cpp new file mode 100644 index 0000000..4b6bb37 --- /dev/null +++ b/libs/hwui/Patch.cpp @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <cstring> + +#include <utils/Log.h> + +#include "Patch.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +Patch::Patch(const uint32_t xCount, const uint32_t yCount): + xCount(xCount + 2), yCount(yCount + 2) { + verticesCount = (xCount + 2) * (yCount + 2); + vertices = new TextureVertex[verticesCount]; + + // 2 triangles per patch, 3 vertices per triangle + indicesCount = (xCount + 1) * (yCount + 1) * 2 * 3; + indices = new uint16_t[indicesCount]; + + const uint32_t xNum = xCount + 1; + const uint32_t yNum = yCount + 1; + + uint16_t* startIndices = indices; + uint32_t n = 0; + for (uint32_t y = 0; y < yNum; y++) { + for (uint32_t x = 0; x < xNum; x++) { + *startIndices++ = n; + *startIndices++ = n + 1; + *startIndices++ = n + xNum + 2; + + *startIndices++ = n; + *startIndices++ = n + xNum + 2; + *startIndices++ = n + xNum + 1; + + n += 1; + } + n += 1; + } +} + +Patch::~Patch() { + delete indices; + delete vertices; +} + +/////////////////////////////////////////////////////////////////////////////// +// Vertices management +/////////////////////////////////////////////////////////////////////////////// + +void Patch::updateVertices(const SkBitmap* bitmap, float left, float top, float right, + float bottom, const int32_t* xDivs, const int32_t* yDivs, const uint32_t width, + const uint32_t height) { + const uint32_t xStretchCount = (width + 1) >> 1; + const uint32_t yStretchCount = (height + 1) >> 1; + + float xStretch = 0; + float yStretch = 0; + float xStretchTex = 0; + float yStretchTex = 0; + + const float meshWidth = right - left; + + const float bitmapWidth = float(bitmap->width()); + const float bitmapHeight = float(bitmap->height()); + + if (xStretchCount > 0) { + uint32_t stretchSize = 0; + for (uint32_t i = 1; i < width; i += 2) { + stretchSize += xDivs[i] - xDivs[i - 1]; + } + xStretchTex = (stretchSize / bitmapWidth) / xStretchCount; + const float fixed = bitmapWidth - stretchSize; + xStretch = (right - left - fixed) / xStretchCount; + } + + if (yStretchCount > 0) { + uint32_t stretchSize = 0; + for (uint32_t i = 1; i < height; i += 2) { + stretchSize += yDivs[i] - yDivs[i - 1]; + } + yStretchTex = (stretchSize / bitmapHeight) / yStretchCount; + const float fixed = bitmapHeight - stretchSize; + yStretch = (bottom - top - fixed) / yStretchCount; + } + + float vy = 0.0f; + float ty = 0.0f; + TextureVertex* vertex = vertices; + + generateVertices(vertex, 0.0f, 0.0f, xDivs, width, xStretch, xStretchTex, + meshWidth, bitmapWidth); + vertex += width + 2; + + for (uint32_t y = 0; y < height; y++) { + if (y & 1) { + vy += yStretch; + ty += yStretchTex; + } else { + const float step = float(yDivs[y]); + vy += step; + ty += step / bitmapHeight; + } + generateVertices(vertex, vy, ty, xDivs, width, xStretch, xStretchTex, + meshWidth, bitmapWidth); + vertex += width + 2; + } + + generateVertices(vertex, bottom - top, 1.0f, xDivs, width, xStretch, xStretchTex, + meshWidth, bitmapWidth); +} + +inline void Patch::generateVertices(TextureVertex* vertex, float y, float v, + const int32_t xDivs[], uint32_t xCount, float xStretch, float xStretchTex, + float width, float widthTex) { + float vx = 0.0f; + float tx = 0.0f; + + TextureVertex::set(vertex, vx, y, tx, v); + vertex++; + + for (uint32_t x = 0; x < xCount; x++) { + if (x & 1) { + vx += xStretch; + tx += xStretchTex; + } else { + const float step = float(xDivs[x]); + vx += step; + tx += step / widthTex; + } + + TextureVertex::set(vertex, vx, y, tx, v); + vertex++; + } + + TextureVertex::set(vertex, width, y, 1.0f, v); + vertex++; +} + +/////////////////////////////////////////////////////////////////////////////// +// Debug tools +/////////////////////////////////////////////////////////////////////////////// + +void Patch::dump() { + LOGD("Vertices ["); + for (uint32_t y = 0; y < yCount; y++) { + char buffer[512]; + buffer[0] = '\0'; + uint32_t offset = 0; + for (uint32_t x = 0; x < xCount; x++) { + TextureVertex* v = &vertices[y * xCount + x]; + offset += sprintf(&buffer[offset], " (%.4f,%.4f)-(%.4f,%.4f)", + v->position[0], v->position[1], v->texture[0], v->texture[1]); + } + LOGD(" [%s ]", buffer); + } + LOGD("]\nIndices [ "); + char buffer[4096]; + buffer[0] = '\0'; + uint32_t offset = 0; + for (uint32_t i = 0; i < indicesCount; i++) { + offset += sprintf(&buffer[offset], "%d ", indices[i]); + } + LOGD(" %s\n]", buffer); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/Patch.h b/libs/hwui/Patch.h new file mode 100644 index 0000000..5d3ad03 --- /dev/null +++ b/libs/hwui/Patch.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PATCH_H +#define ANDROID_UI_PATCH_H + +#include <sys/types.h> + +#include <SkBitmap.h> + +#include "Vertex.h" + +namespace android { +namespace uirenderer { + +/** + * Description of a patch. + */ +struct PatchDescription { + PatchDescription(): xCount(0), yCount(0) { } + PatchDescription(const uint32_t xCount, const uint32_t yCount): + xCount(xCount), yCount(yCount) { } + PatchDescription(const PatchDescription& description): + xCount(description.xCount), yCount(description.yCount) { } + + uint32_t xCount; + uint32_t yCount; + + bool operator<(const PatchDescription& rhs) const { + if (xCount == rhs.xCount) { + return yCount < rhs.yCount; + } + return xCount < rhs.xCount; + } + + bool operator==(const PatchDescription& rhs) const { + return xCount == rhs.xCount && yCount == rhs.yCount; + } +}; // struct PatchDescription + +/** + * An OpenGL patch. This contains an array of vertices and an array of + * indices to render the vertices. + */ +struct Patch { + Patch(const uint32_t xCount, const uint32_t yCount); + ~Patch(); + + void updateVertices(const SkBitmap* bitmap, float left, float top, float right, float bottom, + const int32_t* xDivs, const int32_t* yDivs, + const uint32_t width, const uint32_t height); + void dump(); + + uint32_t xCount; + uint32_t yCount; + + uint16_t* indices; + uint32_t indicesCount; + + TextureVertex* vertices; + uint32_t verticesCount; + +private: + static inline void generateVertices(TextureVertex* vertex, float y, float v, + const int32_t xDivs[], uint32_t xCount, float xStretch, float xStretchTex, + float width, float widthTex); +}; // struct Patch + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PATCH_H diff --git a/libs/hwui/PatchCache.cpp b/libs/hwui/PatchCache.cpp new file mode 100644 index 0000000..694e3fd --- /dev/null +++ b/libs/hwui/PatchCache.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <utils/Log.h> +#include <utils/ResourceTypes.h> + +#include "PatchCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +PatchCache::PatchCache(uint32_t maxEntries): mCache(maxEntries) { +} + +PatchCache::~PatchCache() { + clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Callbacks +/////////////////////////////////////////////////////////////////////////////// + +void PatchCache::operator()(PatchDescription& description, Patch*& mesh) { + if (mesh) delete mesh; +} + +/////////////////////////////////////////////////////////////////////////////// +// Caching +/////////////////////////////////////////////////////////////////////////////// + +void PatchCache::clear() { + mCache.setOnEntryRemovedListener(this); + mCache.clear(); + mCache.setOnEntryRemovedListener(NULL); +} + +Patch* PatchCache::get(const Res_png_9patch* patch) { + const uint32_t width = patch->numXDivs; + const uint32_t height = patch->numYDivs; + const PatchDescription description(width, height); + + Patch* mesh = mCache.get(description); + if (!mesh) { + PATCH_LOGD("Creating new patch mesh, w=%d h=%d", width, height); + mesh = new Patch(width, height); + mCache.put(description, mesh); + } + + return mesh; +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/PatchCache.h b/libs/hwui/PatchCache.h new file mode 100644 index 0000000..de95087 --- /dev/null +++ b/libs/hwui/PatchCache.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PATCH_CACHE_H +#define ANDROID_UI_PATCH_CACHE_H + +#include "Patch.h" +#include "GenerationCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +// Debug +#define DEBUG_PATCHES 0 + +// Debug +#if DEBUG_PATCHES + #define PATCH_LOGD(...) LOGD(__VA_ARGS__) +#else + #define PATCH_LOGD(...) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Cache +/////////////////////////////////////////////////////////////////////////////// + +class PatchCache: public OnEntryRemoved<PatchDescription, Patch*> { +public: + PatchCache(uint32_t maxCapacity); + ~PatchCache(); + + /** + * Used as a callback when an entry is removed from the cache. + * Do not invoke directly. + */ + void operator()(PatchDescription& description, Patch*& mesh); + + Patch* get(const Res_png_9patch* patch); + void clear(); + +private: + GenerationCache<PatchDescription, Patch*> mCache; +}; // class PatchCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PATCH_CACHE_H diff --git a/libs/hwui/PathCache.cpp b/libs/hwui/PathCache.cpp new file mode 100644 index 0000000..9a22dc0 --- /dev/null +++ b/libs/hwui/PathCache.cpp @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <GLES2/gl2.h> + +#include <SkCanvas.h> +#include <SkRect.h> + +#include "PathCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +PathCache::PathCache(uint32_t maxByteSize): + mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity), + mSize(0), mMaxSize(maxByteSize) { + mCache.setOnEntryRemovedListener(this); + + GLint maxTextureSize; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); + mMaxTextureSize = maxTextureSize; +} + +PathCache::~PathCache() { + mCache.clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Size management +/////////////////////////////////////////////////////////////////////////////// + +uint32_t PathCache::getSize() { + return mSize; +} + +uint32_t PathCache::getMaxSize() { + return mMaxSize; +} + +void PathCache::setMaxSize(uint32_t maxSize) { + mMaxSize = maxSize; + while (mSize > mMaxSize) { + mCache.removeOldest(); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Callbacks +/////////////////////////////////////////////////////////////////////////////// + +void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) { + const uint32_t size = texture->width * texture->height; + mSize -= size; + + if (texture) { + glDeleteTextures(1, &texture->id); + delete texture; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Caching +/////////////////////////////////////////////////////////////////////////////// + +PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { + PathCacheEntry entry(path, paint); + PathTexture* texture = mCache.get(entry); + + if (!texture) { + texture = addTexture(entry, path, paint); + } else if (path->getGenerationID() != texture->generation) { + mCache.remove(entry); + texture = addTexture(entry, path, paint); + } + + return texture; +} + +PathTexture* PathCache::addTexture(const PathCacheEntry& entry, + const SkPath *path, const SkPaint* paint) { + const SkRect& bounds = path->getBounds(); + + const float pathWidth = bounds.width(); + const float pathHeight = bounds.height(); + + if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) { + LOGW("Path too large to be rendered into a texture"); + return NULL; + } + + const float offset = entry.strokeWidth * 1.5f; + const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5); + const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5); + + const uint32_t size = width * height; + // Don't even try to cache a bitmap that's bigger than the cache + if (size < mMaxSize) { + while (mSize + size > mMaxSize) { + mCache.removeOldest(); + } + } + + PathTexture* texture = new PathTexture; + texture->left = bounds.fLeft; + texture->top = bounds.fTop; + texture->offset = offset; + texture->width = width; + texture->height = height; + texture->generation = path->getGenerationID(); + + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kA8_Config, width, height); + bitmap.allocPixels(); + bitmap.eraseColor(0); + + SkCanvas canvas(bitmap); + canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset); + canvas.drawPath(*path, *paint); + + generateTexture(bitmap, texture); + + if (size < mMaxSize) { + mSize += size; + mCache.put(entry, texture); + } else { + texture->cleanup = true; + } + + return texture; +} + +void PathCache::clear() { + mCache.clear(); +} + +void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { + SkAutoLockPixels alp(bitmap); + if (!bitmap.readyToDraw()) { + LOGE("Cannot generate texture from bitmap"); + return; + } + + glGenTextures(1, &texture->id); + + glBindTexture(GL_TEXTURE_2D, texture->id); + // Textures are Alpha8 + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + texture->blend = true; + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap.rowBytesAsPixels(), texture->height, 0, + GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels()); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h new file mode 100644 index 0000000..d09789f --- /dev/null +++ b/libs/hwui/PathCache.h @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PATH_CACHE_H +#define ANDROID_UI_PATH_CACHE_H + +#include <SkBitmap.h> +#include <SkPaint.h> +#include <SkPath.h> + +#include "Texture.h" +#include "GenerationCache.h" + +namespace android { +namespace uirenderer { + +/** + * Describe a path in the path cache. + */ +struct PathCacheEntry { + PathCacheEntry() { + path = NULL; + join = SkPaint::kDefault_Join; + cap = SkPaint::kDefault_Cap; + style = SkPaint::kFill_Style; + miter = 4.0f; + strokeWidth = 1.0f; + } + + PathCacheEntry(const PathCacheEntry& entry): + path(entry.path), join(entry.join), cap(entry.cap), + style(entry.style), miter(entry.miter), + strokeWidth(entry.strokeWidth) { + } + + PathCacheEntry(SkPath* path, SkPaint* paint) { + this->path = path; + join = paint->getStrokeJoin(); + cap = paint->getStrokeCap(); + miter = paint->getStrokeMiter(); + strokeWidth = paint->getStrokeWidth(); + style = paint->getStyle(); + } + + SkPath* path; + SkPaint::Join join; + SkPaint::Cap cap; + SkPaint::Style style; + float miter; + float strokeWidth; + + bool operator<(const PathCacheEntry& rhs) const { + return memcmp(this, &rhs, sizeof(PathCacheEntry)) < 0; + } +}; // struct PathCacheEntry + +/** + * Alpha texture used to represent a path. + */ +struct PathTexture: public Texture { + PathTexture(): Texture() { + } + + /** + * Left coordinate of the path bounds. + */ + float left; + /** + * Top coordinate of the path bounds. + */ + float top; + /** + * Offset to draw the path at the correct origin. + */ + float offset; +}; // struct PathTexture + +/** + * A simple LRU path cache. The cache has a maximum size expressed in bytes. + * Any texture added to the cache causing the cache to grow beyond the maximum + * allowed size will also cause the oldest texture to be kicked out. + */ +class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> { +public: + PathCache(uint32_t maxByteSize); + ~PathCache(); + + /** + * Used as a callback when an entry is removed from the cache. + * Do not invoke directly. + */ + void operator()(PathCacheEntry& path, PathTexture*& texture); + + /** + * Returns the texture associated with the specified path. If the texture + * cannot be found in the cache, a new texture is generated. + */ + PathTexture* get(SkPath* path, SkPaint* paint); + /** + * Clears the cache. This causes all textures to be deleted. + */ + void clear(); + + /** + * Sets the maximum size of the cache in bytes. + */ + void setMaxSize(uint32_t maxSize); + /** + * Returns the maximum size of the cache in bytes. + */ + uint32_t getMaxSize(); + /** + * Returns the current size of the cache in bytes. + */ + uint32_t getSize(); + +private: + /** + * Generates the texture from a bitmap into the specified texture structure. + */ + void generateTexture(SkBitmap& bitmap, Texture* texture); + + PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint); + + GenerationCache<PathCacheEntry, PathTexture*> mCache; + + uint32_t mSize; + uint32_t mMaxSize; + GLuint mMaxTextureSize; +}; // class PathCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PATH_CACHE_H diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp new file mode 100644 index 0000000..dbae38e --- /dev/null +++ b/libs/hwui/Program.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include "Program.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Shaders +/////////////////////////////////////////////////////////////////////////////// + +#define SHADER_SOURCE(name, source) const char* name = #source + +/////////////////////////////////////////////////////////////////////////////// +// Base program +/////////////////////////////////////////////////////////////////////////////// + +Program::Program(const char* vertex, const char* fragment) { + vertexShader = buildShader(vertex, GL_VERTEX_SHADER); + fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); + + id = glCreateProgram(); + glAttachShader(id, vertexShader); + glAttachShader(id, fragmentShader); + glLinkProgram(id); + + GLint status; + glGetProgramiv(id, GL_LINK_STATUS, &status); + if (status != GL_TRUE) { + GLint infoLen = 0; + glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) { + char* log = (char*) malloc(sizeof(char) * infoLen); + glGetProgramInfoLog(id, infoLen, 0, log); + LOGE("Error while linking shaders: %s", log); + delete log; + } + glDeleteProgram(id); + } + + mUse = false; + + position = addAttrib("position"); + color = addUniform("color"); + transform = addUniform("transform"); +} + +Program::~Program() { + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + glDeleteProgram(id); +} + +int Program::addAttrib(const char* name) { + int slot = glGetAttribLocation(id, name); + attributes.add(name, slot); + return slot; +} + +int Program::getAttrib(const char* name) { + ssize_t index = attributes.indexOfKey(name); + if (index >= 0) { + return attributes.valueAt(index); + } + return addAttrib(name); +} + +int Program::addUniform(const char* name) { + int slot = glGetUniformLocation(id, name); + uniforms.add(name, slot); + return slot; +} + +int Program::getUniform(const char* name) { + ssize_t index = uniforms.indexOfKey(name); + if (index >= 0) { + return uniforms.valueAt(index); + } + return addUniform(name); +} + +GLuint Program::buildShader(const char* source, GLenum type) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &source, 0); + glCompileShader(shader); + + GLint status; + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if (status != GL_TRUE) { + // Some drivers return wrong values for GL_INFO_LOG_LENGTH + // use a fixed size instead + GLchar log[512]; + glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); + LOGE("Error while compiling shader: %s", log); + glDeleteShader(shader); + } + + return shader; +} + +void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, + const mat4& transformMatrix) { + mat4 t(projectionMatrix); + t.multiply(transformMatrix); + t.multiply(modelViewMatrix); + + glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); +} + +void Program::use() { + glUseProgram(id); + mUse = true; + + glEnableVertexAttribArray(position); +} + +void Program::remove() { + mUse = false; + + glDisableVertexAttribArray(position); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/Program.h b/libs/hwui/Program.h new file mode 100644 index 0000000..6531c74 --- /dev/null +++ b/libs/hwui/Program.h @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PROGRAM_H +#define ANDROID_UI_PROGRAM_H + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <utils/KeyedVector.h> + +#include "Matrix.h" + +namespace android { +namespace uirenderer { + +/** + * A program holds a vertex and a fragment shader. It offers several utility + * methods to query attributes and uniforms. + */ +class Program { +public: + /** + * Creates a new program with the specified vertex and fragment + * shaders sources. + */ + Program(const char* vertex, const char* fragment); + virtual ~Program(); + + /** + * Binds this program to the GL context. + */ + virtual void use(); + + /** + * Marks this program as unused. This will not unbind + * the program from the GL context. + */ + virtual void remove(); + + /** + * Returns the OpenGL name of the specified attribute. + */ + int getAttrib(const char* name); + + /** + * Returns the OpenGL name of the specified uniform. + */ + int getUniform(const char* name); + + /** + * Indicates whether this program is currently in use with + * the GL context. + */ + inline bool isInUse() const { + return mUse; + } + + /** + * Binds the program with the specified projection, modelView and + * transform matrices. + */ + void set(const mat4& projectionMatrix, const mat4& modelViewMatrix, + const mat4& transformMatrix); + + /** + * Name of the position attribute. + */ + int position; + + /** + * Name of the color uniform. + */ + int color; + + /** + * Name of the transform uniform. + */ + int transform; + +protected: + /** + * Adds an attribute with the specified name. + * + * @return The OpenGL name of the attribute. + */ + int addAttrib(const char* name); + + /** + * Adds a uniform with the specified name. + * + * @return The OpenGL name of the uniform. + */ + int addUniform(const char* name); + +private: + /** + * Compiles the specified shader of the specified type. + * + * @return The name of the compiled shader. + */ + GLuint buildShader(const char* source, GLenum type); + + // Name of the OpenGL program + GLuint id; + + // Name of the shaders + GLuint vertexShader; + GLuint fragmentShader; + + // Keeps track of attributes and uniforms slots + KeyedVector<const char*, int> attributes; + KeyedVector<const char*, int> uniforms; + + bool mUse; +}; // class Program + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PROGRAM_H diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp new file mode 100644 index 0000000..39fe85a --- /dev/null +++ b/libs/hwui/ProgramCache.cpp @@ -0,0 +1,437 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <utils/String8.h> + +#include "ProgramCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Vertex shaders snippets +/////////////////////////////////////////////////////////////////////////////// + +const char* gVS_Header_Attributes = + "attribute vec4 position;\n"; +const char* gVS_Header_Attributes_TexCoords = + "attribute vec2 texCoords;\n"; +const char* gVS_Header_Uniforms = + "uniform mat4 transform;\n"; +const char* gVS_Header_Uniforms_HasGradient = + "uniform float gradientLength;\n" + "uniform vec2 gradient;\n" + "uniform vec2 gradientStart;\n" + "uniform mat4 screenSpace;\n"; +const char* gVS_Header_Uniforms_HasBitmap = + "uniform mat4 textureTransform;\n" + "uniform vec2 textureDimension;\n"; +const char* gVS_Header_Varyings_HasTexture = + "varying vec2 outTexCoords;\n"; +const char* gVS_Header_Varyings_HasBitmap = + "varying vec2 outBitmapTexCoords;\n"; +const char* gVS_Header_Varyings_HasGradient = + "varying float index;\n"; +const char* gVS_Main = + "\nvoid main(void) {\n"; +const char* gVS_Main_OutTexCoords = + " outTexCoords = texCoords;\n"; +const char* gVS_Main_OutGradientIndex = + " vec4 location = screenSpace * position;\n" + " index = dot(location.xy - gradientStart, gradient) * gradientLength;\n"; +const char* gVS_Main_OutBitmapTexCoords = + " vec4 bitmapCoords = textureTransform * position;\n" + " outBitmapTexCoords = bitmapCoords.xy * textureDimension;\n"; +const char* gVS_Main_Position = + " gl_Position = transform * position;\n"; +const char* gVS_Footer = + "}\n\n"; + +/////////////////////////////////////////////////////////////////////////////// +// Fragment shaders snippets +/////////////////////////////////////////////////////////////////////////////// + +const char* gFS_Header = + "precision mediump float;\n\n"; +const char* gFS_Uniforms_Color = + "uniform vec4 color;\n"; +const char* gFS_Uniforms_TextureSampler = + "uniform sampler2D sampler;\n"; +const char* gFS_Uniforms_GradientSampler = + "uniform sampler2D gradientSampler;\n"; +const char* gFS_Uniforms_BitmapSampler = + "uniform sampler2D bitmapSampler;\n"; +const char* gFS_Uniforms_ColorOp[4] = { + // None + "", + // Matrix + "uniform mat4 colorMatrix;\n" + "uniform vec4 colorMatrixVector;\n", + // Lighting + "uniform vec4 lightingMul;\n" + "uniform vec4 lightingAdd;\n", + // PorterDuff + "uniform vec4 colorBlend;\n" +}; +const char* gFS_Main = + "\nvoid main(void) {\n" + " lowp vec4 fragColor;\n"; +const char* gFS_Main_FetchColor = + " fragColor = color;\n"; +const char* gFS_Main_FetchTexture = + " fragColor = color * texture2D(sampler, outTexCoords);\n"; +const char* gFS_Main_FetchA8Texture = + " fragColor = color * texture2D(sampler, outTexCoords).a;\n"; +const char* gFS_Main_FetchGradient = + " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n"; +const char* gFS_Main_FetchBitmap = + " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; +const char* gFS_Main_FetchBitmapNpot = + " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; +const char* gFS_Main_BlendShadersBG = + " fragColor = blendShaders(gradientColor, bitmapColor)"; +const char* gFS_Main_BlendShadersGB = + " fragColor = blendShaders(bitmapColor, gradientColor)"; +const char* gFS_Main_BlendShaders_Modulate = + " * fragColor.a;\n"; +const char* gFS_Main_GradientShader_Modulate = + " fragColor = gradientColor * fragColor.a;\n"; +const char* gFS_Main_BitmapShader_Modulate = + " fragColor = bitmapColor * fragColor.a;\n"; +const char* gFS_Main_FragColor = + " gl_FragColor = fragColor;\n"; +const char* gFS_Main_ApplyColorOp[4] = { + // None + "", + // Matrix + // TODO: Fix premultiplied alpha computations for color matrix + " fragColor *= colorMatrix;\n" + " fragColor += colorMatrixVector;\n" + " fragColor.rgb *= fragColor.a;\n", + // Lighting + " float lightingAlpha = fragColor.a;\n" + " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n" + " fragColor.a = lightingAlpha;\n", + // PorterDuff + " fragColor = blendColors(colorBlend, fragColor);\n" +}; +const char* gFS_Footer = + "}\n\n"; + +/////////////////////////////////////////////////////////////////////////////// +// PorterDuff snippets +/////////////////////////////////////////////////////////////////////////////// + +const char* gBlendOps[18] = { + // Clear + "return vec4(0.0, 0.0, 0.0, 0.0);\n", + // Src + "return src;\n", + // Dst + "return dst;\n", + // SrcOver + "return src + dst * (1.0 - src.a);\n", + // DstOver + "return dst + src * (1.0 - dst.a);\n", + // SrcIn + "return src * dst.a;\n", + // DstIn + "return dst * src.a;\n", + // SrcOut + "return src * (1.0 - dst.a);\n", + // DstOut + "return dst * (1.0 - src.a);\n", + // SrcAtop + "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", + // DstAtop + "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", + // Xor + "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " + "src.a + dst.a - 2.0 * src.a * dst.a);\n", + // Add + "return min(src + dst, 1.0);\n", + // Multiply + "return src * dst;\n", + // Screen + "return src + dst - src * dst;\n", + // Overlay + "return clamp(vec4(mix(" + "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " + "src.a * dst.a - 2.0 * (dst.a - dst.rgb) * (src.a - src.rgb) + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " + "step(dst.a, 2.0 * dst.rgb)), " + "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", + // Darken + "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " + "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", + // Lighten + "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " + "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", +}; + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructors +/////////////////////////////////////////////////////////////////////////////// + +ProgramCache::ProgramCache() { +} + +ProgramCache::~ProgramCache() { + clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Cache management +/////////////////////////////////////////////////////////////////////////////// + +void ProgramCache::clear() { + size_t count = mCache.size(); + for (size_t i = 0; i < count; i++) { + delete mCache.valueAt(i); + } + mCache.clear(); +} + +Program* ProgramCache::get(const ProgramDescription& description) { + programid key = description.key(); + ssize_t index = mCache.indexOfKey(key); + Program* program = NULL; + if (index < 0) { + PROGRAM_LOGD("Could not find program with key 0x%x", key); + program = generateProgram(description, key); + mCache.add(key, program); + } else { + program = mCache.valueAt(index); + } + return program; +} + +/////////////////////////////////////////////////////////////////////////////// +// Program generation +/////////////////////////////////////////////////////////////////////////////// + +Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { + String8 vertexShader = generateVertexShader(description); + String8 fragmentShader = generateFragmentShader(description); + + Program* program = new Program(vertexShader.string(), fragmentShader.string()); + return program; +} + +String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { + // Add attributes + String8 shader(gVS_Header_Attributes); + if (description.hasTexture) { + shader.append(gVS_Header_Attributes_TexCoords); + } + // Uniforms + shader.append(gVS_Header_Uniforms); + if (description.hasGradient) { + shader.append(gVS_Header_Uniforms_HasGradient); + } + if (description.hasBitmap) { + shader.append(gVS_Header_Uniforms_HasBitmap); + } + // Varyings + if (description.hasTexture) { + shader.append(gVS_Header_Varyings_HasTexture); + } + if (description.hasGradient) { + shader.append(gVS_Header_Varyings_HasGradient); + } + if (description.hasBitmap) { + shader.append(gVS_Header_Varyings_HasBitmap); + } + + // Begin the shader + shader.append(gVS_Main); { + if (description.hasTexture) { + shader.append(gVS_Main_OutTexCoords); + } + if (description.hasGradient) { + shader.append(gVS_Main_OutGradientIndex); + } + if (description.hasBitmap) { + shader.append(gVS_Main_OutBitmapTexCoords); + } + // Output transformed position + shader.append(gVS_Main_Position); + } + // End the shader + shader.append(gVS_Footer); + + PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); + + return shader; +} + +String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { + // Set the default precision + String8 shader(gFS_Header); + + // Varyings + if (description.hasTexture) { + shader.append(gVS_Header_Varyings_HasTexture); + } + if (description.hasGradient) { + shader.append(gVS_Header_Varyings_HasGradient); + } + if (description.hasBitmap) { + shader.append(gVS_Header_Varyings_HasBitmap); + } + + + // Uniforms + shader.append(gFS_Uniforms_Color); + if (description.hasTexture) { + shader.append(gFS_Uniforms_TextureSampler); + } + if (description.hasGradient) { + shader.append(gFS_Uniforms_GradientSampler); + } + if (description.hasBitmap) { + shader.append(gFS_Uniforms_BitmapSampler); + } + shader.append(gFS_Uniforms_ColorOp[description.colorOp]); + + // Generate required functions + if (description.hasGradient && description.hasBitmap) { + generateBlend(shader, "blendShaders", description.shadersMode); + } + if (description.colorOp == ProgramDescription::kColorBlend) { + generateBlend(shader, "blendColors", description.colorMode); + } + if (description.isBitmapNpot) { + generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); + } + + // Begin the shader + shader.append(gFS_Main); { + // Stores the result in fragColor directly + if (description.hasTexture) { + if (description.hasAlpha8Texture) { + shader.append(gFS_Main_FetchA8Texture); + } else { + shader.append(gFS_Main_FetchTexture); + } + } else { + shader.append(gFS_Main_FetchColor); + } + if (description.hasGradient) { + shader.append(gFS_Main_FetchGradient); + } + if (description.hasBitmap) { + if (!description.isBitmapNpot) { + shader.append(gFS_Main_FetchBitmap); + } else { + shader.append(gFS_Main_FetchBitmapNpot); + } + } + // Case when we have two shaders set + if (description.hasGradient && description.hasBitmap) { + if (description.isBitmapFirst) { + shader.append(gFS_Main_BlendShadersBG); + } else { + shader.append(gFS_Main_BlendShadersGB); + } + shader.append(gFS_Main_BlendShaders_Modulate); + } else { + if (description.hasGradient) { + shader.append(gFS_Main_GradientShader_Modulate); + } else if (description.hasBitmap) { + shader.append(gFS_Main_BitmapShader_Modulate); + } + } + // Apply the color op if needed + shader.append(gFS_Main_ApplyColorOp[description.colorOp]); + // Output the fragment + shader.append(gFS_Main_FragColor); + } + // End the shader + shader.append(gFS_Footer); + + if (DEBUG_PROGRAM_CACHE) { + PROGRAM_LOGD("*** Generated fragment shader:\n\n"); + printLongString(shader); + } + + return shader; +} + +void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) { + shader.append("\nvec4 "); + shader.append(name); + shader.append("(vec4 src, vec4 dst) {\n"); + shader.append(" "); + shader.append(gBlendOps[mode]); + shader.append("}\n"); +} + +void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { + shader.append("\nvec2 wrap(vec2 texCoords) {\n"); + if (wrapS == GL_MIRRORED_REPEAT) { + shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n"); + shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); + } + if (wrapT == GL_MIRRORED_REPEAT) { + shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n"); + shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); + } + shader.append(" return vec2("); + switch (wrapS) { + case GL_CLAMP_TO_EDGE: + shader.append("texCoords.x"); + break; + case GL_REPEAT: + shader.append("mod(texCoords.x, 1.0)"); + break; + case GL_MIRRORED_REPEAT: + shader.append("xMod2"); + break; + } + shader.append(", "); + switch (wrapT) { + case GL_CLAMP_TO_EDGE: + shader.append("texCoords.y"); + break; + case GL_REPEAT: + shader.append("mod(texCoords.y, 1.0)"); + break; + case GL_MIRRORED_REPEAT: + shader.append("yMod2"); + break; + } + shader.append(");\n"); + shader.append("}\n"); +} + +void ProgramCache::printLongString(const String8& shader) const { + ssize_t index = 0; + ssize_t lastIndex = 0; + const char* str = shader.string(); + while ((index = shader.find("\n", index)) > -1) { + String8 line(str, index - lastIndex); + if (line.length() == 0) line.append("\n"); + PROGRAM_LOGD("%s", line.string()); + index++; + str += (index - lastIndex); + lastIndex = index; + } +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h new file mode 100644 index 0000000..54850ee --- /dev/null +++ b/libs/hwui/ProgramCache.h @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PROGRAM_CACHE_H +#define ANDROID_UI_PROGRAM_CACHE_H + +#include <utils/KeyedVector.h> +#include <utils/Log.h> +#include <utils/String8.h> + +#include <GLES2/gl2.h> + +#include <SkXfermode.h> + +#include "Program.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +// Debug +#define DEBUG_PROGRAM_CACHE 1 + +// Debug +#if DEBUG_PROGRAM_CACHE + #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__) +#else + #define PROGRAM_LOGD(...) +#endif + +#define PROGRAM_KEY_TEXTURE 0x1 +#define PROGRAM_KEY_A8_TEXTURE 0x2 +#define PROGRAM_KEY_BITMAP 0x4 +#define PROGRAM_KEY_GRADIENT 0x8 +#define PROGRAM_KEY_BITMAP_FIRST 0x10 +#define PROGRAM_KEY_COLOR_MATRIX 0x20 +#define PROGRAM_KEY_COLOR_LIGHTING 0x40 +#define PROGRAM_KEY_COLOR_BLEND 0x80 +#define PROGRAM_KEY_BITMAP_NPOT 0x100 + +#define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600 +#define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800 + +// Encode the xfermodes on 6 bits +#define PROGRAM_MAX_XFERMODE 0x1f +#define PROGRAM_XFERMODE_SHADER_SHIFT 26 +#define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20 + +#define PROGRAM_BITMAP_WRAPS_SHIFT 9 +#define PROGRAM_BITMAP_WRAPT_SHIFT 11 + +/////////////////////////////////////////////////////////////////////////////// +// Types +/////////////////////////////////////////////////////////////////////////////// + +typedef uint32_t programid; + +/////////////////////////////////////////////////////////////////////////////// +// Cache +/////////////////////////////////////////////////////////////////////////////// + +/** + * Describe the features required for a given program. The features + * determine the generation of both the vertex and fragment shaders. + * A ProgramDescription must be used in conjunction with a ProgramCache. + */ +struct ProgramDescription { + enum ColorModifier { + kColorNone, + kColorMatrix, + kColorLighting, + kColorBlend + }; + + ProgramDescription(): + hasTexture(false), hasAlpha8Texture(false), + hasBitmap(false), isBitmapNpot(false), hasGradient(false), + shadersMode(SkXfermode::kClear_Mode), isBitmapFirst(false), + bitmapWrapS(GL_CLAMP_TO_EDGE), bitmapWrapT(GL_CLAMP_TO_EDGE), + colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode) { + } + + // Texturing + bool hasTexture; + bool hasAlpha8Texture; + + // Shaders + bool hasBitmap; + bool isBitmapNpot; + bool hasGradient; + SkXfermode::Mode shadersMode; + bool isBitmapFirst; + GLenum bitmapWrapS; + GLenum bitmapWrapT; + + // Color operations + int colorOp; + SkXfermode::Mode colorMode; + + inline uint32_t getEnumForWrap(GLenum wrap) const { + switch (wrap) { + case GL_CLAMP_TO_EDGE: + return 0; + case GL_REPEAT: + return 1; + case GL_MIRRORED_REPEAT: + return 2; + } + return 0; + } + + programid key() const { + programid key = 0; + if (hasTexture) key |= PROGRAM_KEY_TEXTURE; + if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE; + if (hasBitmap) { + key |= PROGRAM_KEY_BITMAP; + if (isBitmapNpot) { + key |= PROGRAM_KEY_BITMAP_NPOT; + key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT; + key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT; + } + } + if (hasGradient) key |= PROGRAM_KEY_GRADIENT; + if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST; + if (hasBitmap && hasGradient) { + key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT; + } + switch (colorOp) { + case kColorMatrix: + key |= PROGRAM_KEY_COLOR_MATRIX; + break; + case kColorLighting: + key |= PROGRAM_KEY_COLOR_LIGHTING; + break; + case kColorBlend: + key |= PROGRAM_KEY_COLOR_BLEND; + key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT; + break; + case kColorNone: + break; + } + return key; + } +}; // struct ProgramDescription + +/** + * Generates and caches program. Programs are generated based on + * ProgramDescriptions. + */ +class ProgramCache { +public: + ProgramCache(); + ~ProgramCache(); + + Program* get(const ProgramDescription& description); + + void clear(); + +private: + Program* generateProgram(const ProgramDescription& description, programid key); + String8 generateVertexShader(const ProgramDescription& description); + String8 generateFragmentShader(const ProgramDescription& description); + void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode); + void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT); + + void printLongString(const String8& shader) const; + + KeyedVector<programid, Program*> mCache; + +}; // class ProgramCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PROGRAM_CACHE_H diff --git a/libs/hwui/Properties.h b/libs/hwui/Properties.h new file mode 100644 index 0000000..915b0be --- /dev/null +++ b/libs/hwui/Properties.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_PROPERTIES_H +#define ANDROID_UI_PROPERTIES_H + +/** + * This file contains the list of system properties used to configure + * the OpenGLRenderer. + */ + +// These properties are defined in mega-bytes +#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size" +#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size" +#define PROPERTY_GRADIENT_CACHE_SIZE "ro.hwui.gradient_cache_size" +#define PROPERTY_PATH_CACHE_SIZE "ro.hwui.path_cache_size" + +// These properties are defined in pixels +#define PROPERTY_TEXT_CACHE_WIDTH "ro.hwui.text_cache_width" +#define PROPERTY_TEXT_CACHE_HEIGHT "ro.hwui.text_cache_height" + +#endif // ANDROID_UI_PROPERTIES_H diff --git a/libs/hwui/Rect.h b/libs/hwui/Rect.h new file mode 100644 index 0000000..7be0c34 --- /dev/null +++ b/libs/hwui/Rect.h @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_RECT_H +#define ANDROID_UI_RECT_H + +#include <utils/Log.h> + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Structs +/////////////////////////////////////////////////////////////////////////////// + +struct Rect { + float left; + float top; + float right; + float bottom; + + Rect(): + left(0), + top(0), + right(0), + bottom(0) { + } + + Rect(float left, float top, float right, float bottom): + left(left), + top(top), + right(right), + bottom(bottom) { + } + + Rect(const Rect& r) { + set(r); + } + + Rect(Rect& r) { + set(r); + } + + Rect& operator=(const Rect& r) { + set(r); + return *this; + } + + Rect& operator=(Rect& r) { + set(r); + return *this; + } + + friend int operator==(const Rect& a, const Rect& b) { + return !memcmp(&a, &b, sizeof(a)); + } + + friend int operator!=(const Rect& a, const Rect& b) { + return memcmp(&a, &b, sizeof(a)); + } + + bool isEmpty() const { + return left >= right || top >= bottom; + } + + void setEmpty() { + memset(this, 0, sizeof(*this)); + } + + void set(float left, float top, float right, float bottom) { + this->left = left; + this->right = right; + this->top = top; + this->bottom = bottom; + } + + void set(const Rect& r) { + set(r.left, r.top, r.right, r.bottom); + } + + float getWidth() const { + return right - left; + } + + float getHeight() const { + return bottom - top; + } + + bool intersects(float left, float top, float right, float bottom) const { + return left < right && top < bottom && + this->left < this->right && this->top < this->bottom && + this->left < right && left < this->right && + this->top < bottom && top < this->bottom; + } + + bool intersects(const Rect& r) const { + return intersects(r.left, r.top, r.right, r.bottom); + } + + bool intersect(float left, float top, float right, float bottom) { + if (left < right && top < bottom && !this->isEmpty() && + this->left < right && left < this->right && + this->top < bottom && top < this->bottom) { + + if (this->left < left) this->left = left; + if (this->top < top) this->top = top; + if (this->right > right) this->right = right; + if (this->bottom > bottom) this->bottom = bottom; + + return true; + } + return false; + } + + bool intersect(const Rect& r) { + return intersect(r.left, r.top, r.right, r.bottom); + } + + bool unionWith(const Rect& r) { + if (r.left < r.right && r.top < r.bottom) { + if (left < right && top < bottom) { + if (left > r.left) left = r.left; + if (top > r.top) top = r.top; + if (right < r.right) right = r.right; + if (bottom < r.bottom) bottom = r.bottom; + return true; + } else { + left = r.left; + top = r.top; + right = r.right; + bottom = r.bottom; + return true; + } + } + return false; + } + + void dump() const { + LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); + } + +}; // struct Rect + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_RECT_H diff --git a/libs/hwui/SkiaColorFilter.cpp b/libs/hwui/SkiaColorFilter.cpp new file mode 100644 index 0000000..fe57ae7 --- /dev/null +++ b/libs/hwui/SkiaColorFilter.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2010 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. + */ + +#include "SkiaColorFilter.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Base color filter +/////////////////////////////////////////////////////////////////////////////// + +SkiaColorFilter::SkiaColorFilter(Type type, bool blend): mType(type), mBlend(blend) { +} + +SkiaColorFilter::~SkiaColorFilter() { +} + +/////////////////////////////////////////////////////////////////////////////// +// Color matrix filter +/////////////////////////////////////////////////////////////////////////////// + +SkiaColorMatrixFilter::SkiaColorMatrixFilter(float* matrix, float* vector): + SkiaColorFilter(kColorMatrix, true), mMatrix(matrix), mVector(vector) { +} + +SkiaColorMatrixFilter::~SkiaColorMatrixFilter() { + delete[] mMatrix; + delete[] mVector; +} + +void SkiaColorMatrixFilter::describe(ProgramDescription& description, + const Extensions& extensions) { + description.colorOp = ProgramDescription::kColorMatrix; +} + +void SkiaColorMatrixFilter::setupProgram(Program* program) { + glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]); + glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector); +} + +/////////////////////////////////////////////////////////////////////////////// +// Lighting color filter +/////////////////////////////////////////////////////////////////////////////// + +SkiaLightingFilter::SkiaLightingFilter(int multiply, int add): + SkiaColorFilter(kLighting, true) { + mMulR = ((multiply >> 16) & 0xFF) / 255.0f; + mMulG = ((multiply >> 8) & 0xFF) / 255.0f; + mMulB = ((multiply ) & 0xFF) / 255.0f; + + mAddR = ((add >> 16) & 0xFF) / 255.0f; + mAddG = ((add >> 8) & 0xFF) / 255.0f; + mAddB = ((add ) & 0xFF) / 255.0f; +} + +void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) { + description.colorOp = ProgramDescription::kColorLighting; +} + +void SkiaLightingFilter::setupProgram(Program* program) { + glUniform4f(program->getUniform("lightingMul"), mMulR, mMulG, mMulB, 1.0f); + glUniform4f(program->getUniform("lightingAdd"), mAddR, mAddG, mAddB, 0.0f); +} + +/////////////////////////////////////////////////////////////////////////////// +// Blend color filter +/////////////////////////////////////////////////////////////////////////////// + +SkiaBlendFilter::SkiaBlendFilter(int color, SkXfermode::Mode mode): + SkiaColorFilter(kBlend, true), mMode(mode) { + const int alpha = (color >> 24) & 0xFF; + mA = alpha / 255.0f; + mR = mA * ((color >> 16) & 0xFF) / 255.0f; + mG = mA * ((color >> 8) & 0xFF) / 255.0f; + mB = mA * ((color ) & 0xFF) / 255.0f; +} + +void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) { + description.colorOp = ProgramDescription::kColorBlend; + description.colorMode = mMode; +} + +void SkiaBlendFilter::setupProgram(Program* program) { + glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/SkiaColorFilter.h b/libs/hwui/SkiaColorFilter.h new file mode 100644 index 0000000..865b6f0 --- /dev/null +++ b/libs/hwui/SkiaColorFilter.h @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_SKIA_COLOR_FILTER_H +#define ANDROID_UI_SKIA_COLOR_FILTER_H + +#include <GLES2/gl2.h> + +#include "ProgramCache.h" +#include "Extensions.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Base color filter +/////////////////////////////////////////////////////////////////////////////// + +/** + * Represents a Skia color filter. A color filter modifies a ProgramDescription + * and sets uniforms on the resulting shaders. + */ +struct SkiaColorFilter { + /** + * Type of Skia color filter in use. + */ + enum Type { + kNone, + kColorMatrix, + kLighting, + kBlend, + }; + + SkiaColorFilter(Type type, bool blend); + virtual ~SkiaColorFilter(); + + virtual void describe(ProgramDescription& description, const Extensions& extensions) = 0; + virtual void setupProgram(Program* program) = 0; + + inline bool blend() const { + return mBlend; + } + + Type type() const { + return mType; + } + +protected: + Type mType; + bool mBlend; +}; // struct SkiaColorFilter + +/////////////////////////////////////////////////////////////////////////////// +// Implementations +/////////////////////////////////////////////////////////////////////////////// + +/** + * A color filter that multiplies the source color with a matrix and adds a vector. + */ +struct SkiaColorMatrixFilter: public SkiaColorFilter { + SkiaColorMatrixFilter(float* matrix, float* vector); + ~SkiaColorMatrixFilter(); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program); + +private: + float* mMatrix; + float* mVector; +}; // struct SkiaColorMatrixFilter + +/** + * A color filters that multiplies the source color with a fixed value and adds + * another fixed value. Ignores the alpha channel of both arguments. + */ +struct SkiaLightingFilter: public SkiaColorFilter { + SkiaLightingFilter(int multiply, int add); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program); + +private: + GLfloat mMulR, mMulG, mMulB; + GLfloat mAddR, mAddG, mAddB; +}; // struct SkiaLightingFilter + +/** + * A color filters that blends the source color with a specified destination color + * and PorterDuff blending mode. + */ +struct SkiaBlendFilter: public SkiaColorFilter { + SkiaBlendFilter(int color, SkXfermode::Mode mode); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program); + +private: + SkXfermode::Mode mMode; + GLfloat mR, mG, mB, mA; +}; // struct SkiaBlendFilter + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_SKIA_COLOR_FILTER_H diff --git a/libs/hwui/SkiaShader.cpp b/libs/hwui/SkiaShader.cpp new file mode 100644 index 0000000..3569d6a --- /dev/null +++ b/libs/hwui/SkiaShader.cpp @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <utils/Log.h> + +#include <SkMatrix.h> + +#include "SkiaShader.h" +#include "Texture.h" +#include "Matrix.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Support +/////////////////////////////////////////////////////////////////////////////// + +static const GLenum gTextureUnitsMap[] = { + GL_TEXTURE0, + GL_TEXTURE1, + GL_TEXTURE2 +}; + +static const GLint gTileModes[] = { + GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode + GL_REPEAT, // == SkShader::kRepeat_Mode + GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode +}; + +/////////////////////////////////////////////////////////////////////////////// +// Base shader +/////////////////////////////////////////////////////////////////////////////// + +SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, + SkShader::TileMode tileY, SkMatrix* matrix, bool blend): + mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mMatrix(matrix), mBlend(blend) { +} + +SkiaShader::~SkiaShader() { +} + +void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) { +} + +void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, + GLuint* textureUnit) { +} + +void SkiaShader::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { + glActiveTexture(gTextureUnitsMap[textureUnit]); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); +} + +/////////////////////////////////////////////////////////////////////////////// +// Bitmap shader +/////////////////////////////////////////////////////////////////////////////// + +SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, + SkShader::TileMode tileY, SkMatrix* matrix, bool blend): + SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) { +} + +void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) { + const Texture* texture = mTextureCache->get(mBitmap); + if (!texture) return; + mTexture = texture; + + const float width = texture->width; + const float height = texture->height; + + description.hasBitmap = true; + // The driver does not support non-power of two mirrored/repeated + // textures, so do it ourselves + if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) && + (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) { + description.isBitmapNpot = true; + description.bitmapWrapS = gTileModes[mTileX]; + description.bitmapWrapT = gTileModes[mTileY]; + } +} + +void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView, + const Snapshot& snapshot, GLuint* textureUnit) { + GLuint textureSlot = (*textureUnit)++; + glActiveTexture(gTextureUnitsMap[textureSlot]); + + const Texture* texture = mTexture; + mTexture = NULL; + if (!texture) return; + const AutoTexture autoCleanup(texture); + + const float width = texture->width; + const float height = texture->height; + + mat4 textureTransform; + if (mMatrix) { + SkMatrix inverse; + mMatrix->invert(&inverse); + textureTransform.load(inverse); + textureTransform.multiply(modelView); + } else { + textureTransform.load(modelView); + } + + // Uniforms + bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); + glUniform1i(program->getUniform("bitmapSampler"), textureSlot); + glUniformMatrix4fv(program->getUniform("textureTransform"), 1, + GL_FALSE, &textureTransform.data[0]); + glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); +} + +/////////////////////////////////////////////////////////////////////////////// +// Linear gradient shader +/////////////////////////////////////////////////////////////////////////////// + +SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors, + float* positions, int count, SkShader* key, SkShader::TileMode tileMode, + SkMatrix* matrix, bool blend): + SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend), + mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) { +} + +SkiaLinearGradientShader::~SkiaLinearGradientShader() { + delete[] mBounds; + delete[] mColors; + delete[] mPositions; +} + +void SkiaLinearGradientShader::describe(ProgramDescription& description, + const Extensions& extensions) { + description.hasGradient = true; +} + +void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView, + const Snapshot& snapshot, GLuint* textureUnit) { + GLuint textureSlot = (*textureUnit)++; + glActiveTexture(gTextureUnitsMap[textureSlot]); + + Texture* texture = mGradientCache->get(mKey); + if (!texture) { + texture = mGradientCache->addLinearGradient(mKey, mBounds, mColors, mPositions, + mCount, mTileX); + } + + Rect start(mBounds[0], mBounds[1], mBounds[2], mBounds[3]); + if (mMatrix) { + mat4 shaderMatrix(*mMatrix); + shaderMatrix.mapRect(start); + } + snapshot.transform.mapRect(start); + + const float gradientX = start.right - start.left; + const float gradientY = start.bottom - start.top; + + mat4 screenSpace(snapshot.transform); + screenSpace.multiply(modelView); + + // Uniforms + bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); + glUniform1i(program->getUniform("gradientSampler"), textureSlot); + glUniform2f(program->getUniform("gradientStart"), start.left, start.top); + glUniform2f(program->getUniform("gradient"), gradientX, gradientY); + glUniform1f(program->getUniform("gradientLength"), + 1.0f / (gradientX * gradientX + gradientY * gradientY)); + glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); +} + +/////////////////////////////////////////////////////////////////////////////// +// Compose shader +/////////////////////////////////////////////////////////////////////////////// + +SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second, + SkXfermode::Mode mode, SkShader* key): + SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, + NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) { +} + +void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) { + SkiaShader::set(textureCache, gradientCache); + mFirst->set(textureCache, gradientCache); + mSecond->set(textureCache, gradientCache); +} + +void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) { + mFirst->describe(description, extensions); + mSecond->describe(description, extensions); + if (mFirst->type() == kBitmap) { + description.isBitmapFirst = true; + } + description.shadersMode = mMode; +} + +void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView, + const Snapshot& snapshot, GLuint* textureUnit) { + mFirst->setupProgram(program, modelView, snapshot, textureUnit); + mSecond->setupProgram(program, modelView, snapshot, textureUnit); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/SkiaShader.h b/libs/hwui/SkiaShader.h new file mode 100644 index 0000000..d95e3b0 --- /dev/null +++ b/libs/hwui/SkiaShader.h @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_SKIA_SHADER_H +#define ANDROID_UI_SKIA_SHADER_H + +#include <SkShader.h> +#include <SkXfermode.h> + +#include <GLES2/gl2.h> + +#include "Extensions.h" +#include "ProgramCache.h" +#include "TextureCache.h" +#include "GradientCache.h" +#include "Snapshot.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Base shader +/////////////////////////////////////////////////////////////////////////////// + +/** + * Represents a Skia shader. A shader will modify the GL context and active + * program to recreate the original effect. + */ +struct SkiaShader { + /** + * Type of Skia shader in use. + */ + enum Type { + kNone, + kBitmap, + kLinearGradient, + kCircularGradient, + kSweepGradient, + kCompose + }; + + SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, SkShader::TileMode tileY, + SkMatrix* matrix, bool blend); + virtual ~SkiaShader(); + + virtual void describe(ProgramDescription& description, const Extensions& extensions); + virtual void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, + GLuint* textureUnit); + + inline bool blend() const { + return mBlend; + } + + Type type() const { + return mType; + } + + virtual void set(TextureCache* textureCache, GradientCache* gradientCache) { + mTextureCache = textureCache; + mGradientCache = gradientCache; + } + + void setMatrix(SkMatrix* matrix) { + mMatrix = matrix; + } + +protected: + inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit); + + Type mType; + SkShader* mKey; + SkShader::TileMode mTileX; + SkShader::TileMode mTileY; + SkMatrix* mMatrix; + bool mBlend; + + TextureCache* mTextureCache; + GradientCache* mGradientCache; +}; // struct SkiaShader + + +/////////////////////////////////////////////////////////////////////////////// +// Implementations +/////////////////////////////////////////////////////////////////////////////// + +/** + * A shader that draws a bitmap. + */ +struct SkiaBitmapShader: public SkiaShader { + SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, + SkShader::TileMode tileY, SkMatrix* matrix, bool blend); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, + GLuint* textureUnit); + +private: + /** + * This method does not work for n == 0. + */ + inline bool isPowerOfTwo(unsigned int n) { + return !(n & (n - 1)); + } + + SkBitmap* mBitmap; + const Texture* mTexture; +}; // struct SkiaBitmapShader + +/** + * A shader that draws a linear gradient. + */ +struct SkiaLinearGradientShader: public SkiaShader { + SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, int count, + SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); + ~SkiaLinearGradientShader(); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, + GLuint* textureUnit); + +private: + float* mBounds; + uint32_t* mColors; + float* mPositions; + int mCount; +}; // struct SkiaLinearGradientShader + +/** + * A shader that draws two shaders, composited with an xfermode. + */ +struct SkiaComposeShader: public SkiaShader { + SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, SkShader* key); + + void set(TextureCache* textureCache, GradientCache* gradientCache); + + void describe(ProgramDescription& description, const Extensions& extensions); + void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, + GLuint* textureUnit); + +private: + SkiaShader* mFirst; + SkiaShader* mSecond; + SkXfermode::Mode mMode; +}; // struct SkiaComposeShader + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_SKIA_SHADER_H diff --git a/libs/hwui/Snapshot.h b/libs/hwui/Snapshot.h new file mode 100644 index 0000000..399ae68 --- /dev/null +++ b/libs/hwui/Snapshot.h @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_SNAPSHOT_H +#define ANDROID_UI_SNAPSHOT_H + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <utils/RefBase.h> + +#include <SkRegion.h> + +#include "Layer.h" +#include "Matrix.h" +#include "Rect.h" + +namespace android { +namespace uirenderer { + +/** + * A snapshot holds information about the current state of the rendering + * surface. A snapshot is usually created whenever the user calls save() + * and discarded when the user calls restore(). Once a snapshot is created, + * it can hold information for deferred rendering. + * + * Each snapshot has a link to a previous snapshot, indicating the previous + * state of the renderer. + */ +class Snapshot: public LightRefBase<Snapshot> { +public: + Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0) { } + + /** + * Copies the specified snapshot. Only the transform and clip rectangle + * are copied. The layer information is set to 0 and the transform is + * assumed to be dirty. The specified snapshot is stored as the previous + * snapshot. + */ + Snapshot(const sp<Snapshot>& s): + height(s->height), + transform(s->transform), + clipRect(s->clipRect), + flags(0), + previous(s), + layer(NULL), + fbo(s->fbo) { + if ((s->flags & Snapshot::kFlagClipSet) && + !(s->flags & Snapshot::kFlagDirtyLocalClip)) { + localClip.set(s->localClip); + } else { + flags |= Snapshot::kFlagDirtyLocalClip; + } + } + + /** + * Various flags set on #flags. + */ + enum Flags { + /** + * Indicates that the clip region was modified. When this + * snapshot is restored so must the clip. + */ + kFlagClipSet = 0x1, + /** + * Indicates that this snapshot was created when saving + * a new layer. + */ + kFlagIsLayer = 0x2, + /** + * Indicates that this snapshot has changed the ortho matrix. + */ + kFlagDirtyOrtho = 0x4, + /** + * Indicates that the local clip should be recomputed. + */ + kFlagDirtyLocalClip = 0x8, + }; + + /** + * Intersects the current clip with the new clip rectangle. + */ + bool clip(float left, float top, float right, float bottom, SkRegion::Op op) { + bool clipped = false; + + Rect r(left, top, right, bottom); + transform.mapRect(r); + + switch (op) { + case SkRegion::kDifference_Op: + break; + case SkRegion::kIntersect_Op: + clipped = clipRect.intersect(r); + break; + case SkRegion::kUnion_Op: + clipped = clipRect.unionWith(r); + break; + case SkRegion::kXOR_Op: + break; + case SkRegion::kReverseDifference_Op: + break; + case SkRegion::kReplace_Op: + clipRect.set(r); + clipped = true; + break; + } + + if (clipped) { + flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; + } + + return clipped; + } + + /** + * Sets the current clip. + */ + void setClip(float left, float top, float right, float bottom) { + clipRect.set(left, top, right, bottom); + flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; + } + + const Rect& getLocalClip() { + if (flags & Snapshot::kFlagDirtyLocalClip) { + mat4 inverse; + inverse.loadInverse(transform); + localClip.set(clipRect); + inverse.mapRect(localClip); + flags &= ~Snapshot::kFlagDirtyLocalClip; + } + return localClip; + } + + /** + * Height of the framebuffer the snapshot is rendering into. + */ + int height; + + /** + * Local transformation. Holds the current translation, scale and + * rotation values. + */ + mat4 transform; + + /** + * Current clip region. The clip is stored in canvas-space coordinates, + * (screen-space coordinates in the regular case.) + */ + Rect clipRect; + + /** + * Dirty flags. + */ + int flags; + + /** + * Previous snapshot. + */ + sp<Snapshot> previous; + + /** + * Only set when the flag kFlagIsLayer is set. + */ + Layer* layer; + GLuint fbo; + + /** + * Contains the previous ortho matrix. + */ + mat4 orthoMatrix; + +private: + Rect localClip; + +}; // class Snapshot + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_SNAPSHOT_H diff --git a/libs/hwui/Texture.h b/libs/hwui/Texture.h new file mode 100644 index 0000000..90f548b --- /dev/null +++ b/libs/hwui/Texture.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_TEXTURE_H +#define ANDROID_UI_TEXTURE_H + +#include <GLES2/gl2.h> + +namespace android { +namespace uirenderer { + +/** + * Represents an OpenGL texture. + */ +struct Texture { + Texture() { + cleanup = false; + } + + /** + * Name of the texture. + */ + GLuint id; + /** + * Generation of the backing bitmap, + */ + uint32_t generation; + /** + * Indicates whether the texture requires blending. + */ + bool blend; + /** + * Width of the backing bitmap. + */ + uint32_t width; + /** + * Height of the backing bitmap. + */ + uint32_t height; + /** + * Indicates whether this texture should be cleaned up after use. + */ + bool cleanup; +}; // struct Texture + +class AutoTexture { +public: + AutoTexture(const Texture* texture): mTexture(texture) { } + ~AutoTexture() { + if (mTexture && mTexture->cleanup) { + glDeleteTextures(1, &mTexture->id); + delete mTexture; + } + } + +private: + const Texture* mTexture; +}; // class AutoTexture + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_TEXTURE_H diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp new file mode 100644 index 0000000..3f9698d --- /dev/null +++ b/libs/hwui/TextureCache.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2010 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. + */ + +#define LOG_TAG "OpenGLRenderer" + +#include <GLES2/gl2.h> + +#include "TextureCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +TextureCache::TextureCache(uint32_t maxByteSize): + mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), + mSize(0), mMaxSize(maxByteSize) { + mCache.setOnEntryRemovedListener(this); + + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); + LOGD("Maximum texture dimension is %d pixels", mMaxTextureSize); +} + +TextureCache::~TextureCache() { + mCache.clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Size management +/////////////////////////////////////////////////////////////////////////////// + +uint32_t TextureCache::getSize() { + return mSize; +} + +uint32_t TextureCache::getMaxSize() { + return mMaxSize; +} + +void TextureCache::setMaxSize(uint32_t maxSize) { + mMaxSize = maxSize; + while (mSize > mMaxSize) { + mCache.removeOldest(); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Callbacks +/////////////////////////////////////////////////////////////////////////////// + +void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { + if (bitmap) { + const uint32_t size = bitmap->rowBytes() * bitmap->height(); + mSize -= size; + } + + if (texture) { + glDeleteTextures(1, &texture->id); + delete texture; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Caching +/////////////////////////////////////////////////////////////////////////////// + +Texture* TextureCache::get(SkBitmap* bitmap) { + Texture* texture = mCache.get(bitmap); + if (!texture) { + if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { + LOGW("Bitmap too large to be uploaded into a texture"); + return NULL; + } + + const uint32_t size = bitmap->rowBytes() * bitmap->height(); + // Don't even try to cache a bitmap that's bigger than the cache + if (size < mMaxSize) { + while (mSize + size > mMaxSize) { + mCache.removeOldest(); + } + } + + texture = new Texture; + generateTexture(bitmap, texture, false); + + if (size < mMaxSize) { + mSize += size; + mCache.put(bitmap, texture); + } else { + texture->cleanup = true; + } + } else if (bitmap->getGenerationID() != texture->generation) { + generateTexture(bitmap, texture, true); + } + + return texture; +} + +void TextureCache::remove(SkBitmap* bitmap) { + mCache.remove(bitmap); +} + +void TextureCache::clear() { + mCache.clear(); +} + +void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { + SkAutoLockPixels alp(*bitmap); + if (!bitmap->readyToDraw()) { + LOGE("Cannot generate texture from bitmap"); + return; + } + + if (!regenerate) { + texture->generation = bitmap->getGenerationID(); + texture->width = bitmap->width(); + texture->height = bitmap->height(); + + glGenTextures(1, &texture->id); + } + + glBindTexture(GL_TEXTURE_2D, texture->id); + glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); + + switch (bitmap->getConfig()) { + case SkBitmap::kA8_Config: + texture->blend = true; + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0, + GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels()); + break; + case SkBitmap::kRGB_565_Config: + texture->blend = false; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, + GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); + break; + case SkBitmap::kARGB_8888_Config: + texture->blend = !bitmap->isOpaque(); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); + break; + default: + break; + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h new file mode 100644 index 0000000..452716c --- /dev/null +++ b/libs/hwui/TextureCache.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_TEXTURE_CACHE_H +#define ANDROID_UI_TEXTURE_CACHE_H + +#include <SkBitmap.h> + +#include "Texture.h" +#include "GenerationCache.h" + +namespace android { +namespace uirenderer { + +/** + * A simple LRU texture cache. The cache has a maximum size expressed in bytes. + * Any texture added to the cache causing the cache to grow beyond the maximum + * allowed size will also cause the oldest texture to be kicked out. + */ +class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { +public: + TextureCache(uint32_t maxByteSize); + ~TextureCache(); + + /** + * Used as a callback when an entry is removed from the cache. + * Do not invoke directly. + */ + void operator()(SkBitmap*& bitmap, Texture*& texture); + + /** + * Returns the texture associated with the specified bitmap. If the texture + * cannot be found in the cache, a new texture is generated. + */ + Texture* get(SkBitmap* bitmap); + /** + * Removes the texture associated with the specified bitmap. Returns NULL + * if the texture cannot be found. Upon remove the texture is freed. + */ + void remove(SkBitmap* bitmap); + /** + * Clears the cache. This causes all textures to be deleted. + */ + void clear(); + + /** + * Sets the maximum size of the cache in bytes. + */ + void setMaxSize(uint32_t maxSize); + /** + * Returns the maximum size of the cache in bytes. + */ + uint32_t getMaxSize(); + /** + * Returns the current size of the cache in bytes. + */ + uint32_t getSize(); + +private: + /** + * Generates the texture from a bitmap into the specified texture structure. + * + * @param regenerate If true, the bitmap data is reuploaded into the texture, but + * no new texture is generated. + */ + void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); + + GenerationCache<SkBitmap*, Texture*> mCache; + + uint32_t mSize; + uint32_t mMaxSize; + GLint mMaxTextureSize; +}; // class TextureCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_TEXTURE_CACHE_H diff --git a/libs/hwui/Vertex.h b/libs/hwui/Vertex.h new file mode 100644 index 0000000..1f54086 --- /dev/null +++ b/libs/hwui/Vertex.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2010 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. + */ + +#ifndef ANDROID_UI_VERTEX_H +#define ANDROID_UI_VERTEX_H + +namespace android { +namespace uirenderer { + +/** + * Simple structure to describe a vertex with a position and a texture. + */ +struct TextureVertex { + float position[2]; + float texture[2]; + + static inline void set(TextureVertex* vertex, float x, float y, float u, float v) { + vertex[0].position[0] = x; + vertex[0].position[1] = y; + vertex[0].texture[0] = u; + vertex[0].texture[1] = v; + } + + static inline void setUV(TextureVertex* vertex, float u, float v) { + vertex[0].texture[0] = u; + vertex[0].texture[1] = v; + } +}; // struct TextureVertex + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_VERTEX_H diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index 98464a0..37c418b 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -76,34 +76,44 @@ ifneq ($(TARGET_SIMULATOR),true) LOCAL_SRC_FILES:= \ rsAdapter.cpp \ rsAllocation.cpp \ + rsAnimation.cpp \ rsComponent.cpp \ rsContext.cpp \ rsDevice.cpp \ rsElement.cpp \ - rsFileA3D.cpp \ + rsFileA3D.cpp \ + rsFont.cpp \ rsLight.cpp \ rsLocklessFifo.cpp \ rsObjectBase.cpp \ rsMatrix.cpp \ - rsMesh.cpp \ - rsNoise.cpp \ + rsMesh.cpp \ + rsMutex.cpp \ rsProgram.cpp \ rsProgramFragment.cpp \ - rsProgramFragmentStore.cpp \ + rsProgramStore.cpp \ rsProgramRaster.cpp \ rsProgramVertex.cpp \ rsSampler.cpp \ rsScript.cpp \ rsScriptC.cpp \ rsScriptC_Lib.cpp \ - rsShaderCache.cpp \ - rsSimpleMesh.cpp \ + rsScriptC_LibCL.cpp \ + rsScriptC_LibGL.cpp \ + rsShaderCache.cpp \ + rsSignal.cpp \ + rsStream.cpp \ rsThreadIO.cpp \ rsType.cpp \ rsVertexArray.cpp -LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libacc +LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc + +LOCAL_STATIC_LIBRARIES := libft2 + +LOCAL_C_INCLUDES += external/freetype/include + LOCAL_LDLIBS := -lpthread -ldl LOCAL_MODULE:= libRS LOCAL_MODULE_TAGS := optional diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h index d280f50..6636fef 100644 --- a/libs/rs/RenderScript.h +++ b/libs/rs/RenderScript.h @@ -30,20 +30,23 @@ extern "C" { typedef void * RsAdapter1D; typedef void * RsAdapter2D; typedef void * RsAllocation; +typedef void * RsAnimation; typedef void * RsContext; typedef void * RsDevice; typedef void * RsElement; typedef void * RsFile; +typedef void * RsFont; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsSimpleMesh; +typedef void * RsMesh; typedef void * RsType; typedef void * RsLight; +typedef void * RsObjectBase; typedef void * RsProgram; typedef void * RsProgramVertex; typedef void * RsProgramFragment; -typedef void * RsProgramFragmentStore; +typedef void * RsProgramStore; typedef void * RsProgramRaster; typedef void (* RsBitmapCallback_t)(void *); @@ -83,6 +86,8 @@ enum RsDataType { RS_TYPE_UNSIGNED_32, RS_TYPE_UNSIGNED_64, + RS_TYPE_BOOLEAN, + RS_TYPE_UNSIGNED_5_6_5, RS_TYPE_UNSIGNED_5_5_5_1, RS_TYPE_UNSIGNED_4_4_4_4, @@ -101,19 +106,12 @@ enum RsDataType { enum RsDataKind { RS_KIND_USER, - RS_KIND_COLOR, - RS_KIND_POSITION, - RS_KIND_TEXTURE, - RS_KIND_NORMAL, - RS_KIND_INDEX, - RS_KIND_POINT_SIZE, - - RS_KIND_PIXEL_L, + + RS_KIND_PIXEL_L = 7, RS_KIND_PIXEL_A, RS_KIND_PIXEL_LA, RS_KIND_PIXEL_RGB, RS_KIND_PIXEL_RGBA, - }; enum RsSamplerParam { @@ -205,9 +203,71 @@ enum RsPrimitive { enum RsError { RS_ERROR_NONE, RS_ERROR_BAD_SHADER, - RS_ERROR_BAD_SCRIPT + RS_ERROR_BAD_SCRIPT, + RS_ERROR_BAD_VALUE, + RS_ERROR_OUT_OF_MEMORY +}; + +enum RsAnimationInterpolation { + RS_ANIMATION_INTERPOLATION_STEP, + RS_ANIMATION_INTERPOLATION_LINEAR, + RS_ANIMATION_INTERPOLATION_BEZIER, + RS_ANIMATION_INTERPOLATION_CARDINAL, + RS_ANIMATION_INTERPOLATION_HERMITE, + RS_ANIMATION_INTERPOLATION_BSPLINE +}; + +enum RsAnimationEdge { + RS_ANIMATION_EDGE_UNDEFINED, + RS_ANIMATION_EDGE_CONSTANT, + RS_ANIMATION_EDGE_GRADIENT, + RS_ANIMATION_EDGE_CYCLE, + RS_ANIMATION_EDGE_OSCILLATE, + RS_ANIMATION_EDGE_CYLE_RELATIVE +}; + +enum RsA3DClassID { + RS_A3D_CLASS_ID_UNKNOWN, + RS_A3D_CLASS_ID_MESH, + RS_A3D_CLASS_ID_TYPE, + RS_A3D_CLASS_ID_ELEMENT, + RS_A3D_CLASS_ID_ALLOCATION, + RS_A3D_CLASS_ID_PROGRAM_VERTEX, + RS_A3D_CLASS_ID_PROGRAM_RASTER, + RS_A3D_CLASS_ID_PROGRAM_FRAGMENT, + RS_A3D_CLASS_ID_PROGRAM_STORE, + RS_A3D_CLASS_ID_SAMPLER, + RS_A3D_CLASS_ID_ANIMATION, + RS_A3D_CLASS_ID_LIGHT, + RS_A3D_CLASS_ID_ADAPTER_1D, + RS_A3D_CLASS_ID_ADAPTER_2D, + RS_A3D_CLASS_ID_SCRIPT_C }; +enum RsCullMode { + RS_CULL_BACK, + RS_CULL_FRONT, + RS_CULL_NONE +}; + +typedef struct { + RsA3DClassID classID; + const char* objectName; +} RsFileIndexEntry; + +// Script to Script +typedef struct { + uint32_t xStart; + uint32_t xEnd; + uint32_t yStart; + uint32_t yEnd; + uint32_t zStart; + uint32_t zEnd; + uint32_t arrayStart; + uint32_t arrayEnd; + +} RsScriptCall; + #ifndef NO_RS_FUNCS #include "rsgApiFuncDecl.h" #endif diff --git a/libs/rs/RenderScriptEnv.h b/libs/rs/RenderScriptEnv.h index 99b8c04..9225904 100644 --- a/libs/rs/RenderScriptEnv.h +++ b/libs/rs/RenderScriptEnv.h @@ -9,10 +9,10 @@ typedef void * RsDevice; typedef void * RsElement; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsSimpleMesh; +typedef void * RsMesh; typedef void * RsType; typedef void * RsProgramFragment; -typedef void * RsProgramFragmentStore; +typedef void * RsProgramStore; typedef void * RsLight; diff --git a/libs/rs/java/Film/res/drawable/p01.png b/libs/rs/java/Film/res/drawable/p01.png Binary files differdeleted file mode 100644 index a9b9bdb..0000000 --- a/libs/rs/java/Film/res/drawable/p01.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p02.png b/libs/rs/java/Film/res/drawable/p02.png Binary files differdeleted file mode 100644 index 8162c82..0000000 --- a/libs/rs/java/Film/res/drawable/p02.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p03.png b/libs/rs/java/Film/res/drawable/p03.png Binary files differdeleted file mode 100644 index e3e26c0..0000000 --- a/libs/rs/java/Film/res/drawable/p03.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p04.png b/libs/rs/java/Film/res/drawable/p04.png Binary files differdeleted file mode 100644 index daee603..0000000 --- a/libs/rs/java/Film/res/drawable/p04.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p05.png b/libs/rs/java/Film/res/drawable/p05.png Binary files differdeleted file mode 100644 index fac5248..0000000 --- a/libs/rs/java/Film/res/drawable/p05.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p06.png b/libs/rs/java/Film/res/drawable/p06.png Binary files differdeleted file mode 100644 index 3b51261..0000000 --- a/libs/rs/java/Film/res/drawable/p06.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p07.png b/libs/rs/java/Film/res/drawable/p07.png Binary files differdeleted file mode 100644 index d8bd938..0000000 --- a/libs/rs/java/Film/res/drawable/p07.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p08.png b/libs/rs/java/Film/res/drawable/p08.png Binary files differdeleted file mode 100644 index ef175e8..0000000 --- a/libs/rs/java/Film/res/drawable/p08.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p09.png b/libs/rs/java/Film/res/drawable/p09.png Binary files differdeleted file mode 100644 index 7bf3874..0000000 --- a/libs/rs/java/Film/res/drawable/p09.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p10.png b/libs/rs/java/Film/res/drawable/p10.png Binary files differdeleted file mode 100644 index 908827d..0000000 --- a/libs/rs/java/Film/res/drawable/p10.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p11.png b/libs/rs/java/Film/res/drawable/p11.png Binary files differdeleted file mode 100644 index 1289f71..0000000 --- a/libs/rs/java/Film/res/drawable/p11.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p12.png b/libs/rs/java/Film/res/drawable/p12.png Binary files differdeleted file mode 100644 index e1af16a..0000000 --- a/libs/rs/java/Film/res/drawable/p12.png +++ /dev/null diff --git a/libs/rs/java/Film/res/drawable/p13.png b/libs/rs/java/Film/res/drawable/p13.png Binary files differdeleted file mode 100644 index d08bcbe..0000000 --- a/libs/rs/java/Film/res/drawable/p13.png +++ /dev/null diff --git a/libs/rs/java/Film/res/raw/filmimage.c b/libs/rs/java/Film/res/raw/filmimage.c deleted file mode 100644 index d154c68..0000000 --- a/libs/rs/java/Film/res/raw/filmimage.c +++ /dev/null @@ -1,110 +0,0 @@ -// Fountain test script - -#pragma version(1) -#pragma stateVertex(orthoWindow) -#pragma stateRaster(flat) -#pragma stateFragment(PgmFragBackground) -#pragma stateStore(MyBlend) - - -int main(void* con, int ft, int launchID) { - int count, touch, x, y, rate, maxLife, lifeShift; - int life; - int ct, ct2; - int newPart; - int drawCount; - int dx, dy, idx; - int posx,posy; - int c; - int srcIdx; - int dstIdx; - - count = loadI32(con, 0, 1); - touch = loadI32(con, 0, 2); - x = loadI32(con, 0, 3); - y = loadI32(con, 0, 4); - - rate = 4; - maxLife = (count / rate) - 1; - lifeShift = 0; - { - life = maxLife; - while (life > 255) { - life = life >> 1; - lifeShift ++; - } - } - - drawRect(con, 0, 256, 0, 512); - contextBindProgramFragment(con, NAMED_PgmFragParts); - - if (touch) { - newPart = loadI32(con, 2, 0); - for (ct2=0; ct2<rate; ct2++) { - dx = scriptRand(con, 0x10000) - 0x8000; - dy = scriptRand(con, 0x10000) - 0x8000; - - idx = newPart * 5 + 1; - storeI32(con, 2, idx, dx); - storeI32(con, 2, idx + 1, dy); - storeI32(con, 2, idx + 2, maxLife); - storeI32(con, 2, idx + 3, x << 16); - storeI32(con, 2, idx + 4, y << 16); - - newPart++; - if (newPart >= count) { - newPart = 0; - } - } - storeI32(con, 2, 0, newPart); - } - - drawCount = 0; - for (ct=0; ct < count; ct++) { - srcIdx = ct * 5 + 1; - - dx = loadI32(con, 2, srcIdx); - dy = loadI32(con, 2, srcIdx + 1); - life = loadI32(con, 2, srcIdx + 2); - posx = loadI32(con, 2, srcIdx + 3); - posy = loadI32(con, 2, srcIdx + 4); - - if (life) { - if (posy < (480 << 16)) { - dstIdx = drawCount * 9; - c = 0xffafcf | ((life >> lifeShift) << 24); - - storeU32(con, 1, dstIdx, c); - storeI32(con, 1, dstIdx + 1, posx); - storeI32(con, 1, dstIdx + 2, posy); - - storeU32(con, 1, dstIdx + 3, c); - storeI32(con, 1, dstIdx + 4, posx + 0x10000); - storeI32(con, 1, dstIdx + 5, posy + dy * 4); - - storeU32(con, 1, dstIdx + 6, c); - storeI32(con, 1, dstIdx + 7, posx - 0x10000); - storeI32(con, 1, dstIdx + 8, posy + dy * 4); - drawCount ++; - } else { - if (dy > 0) { - dy = (-dy) >> 1; - } - } - - posx = posx + dx; - posy = posy + dy; - dy = dy + 0x400; - life --; - - //storeI32(con, 2, srcIdx, dx); - storeI32(con, 2, srcIdx + 1, dy); - storeI32(con, 2, srcIdx + 2, life); - storeI32(con, 2, srcIdx + 3, posx); - storeI32(con, 2, srcIdx + 4, posy); - } - } - - drawTriangleArray(con, NAMED_PartBuffer, drawCount); - return 1; -} diff --git a/libs/rs/java/Film/res/raw/filmstrip.c b/libs/rs/java/Film/res/raw/filmstrip.c deleted file mode 100644 index bf75675..0000000 --- a/libs/rs/java/Film/res/raw/filmstrip.c +++ /dev/null @@ -1,94 +0,0 @@ -// Fountain test script - -#pragma version(1) -#pragma stateVertex(PVBackground) -#pragma stateFragment(PFBackground) -#pragma stateStore(PSBackground) - -#define STATE_TRIANGLE_OFFSET_COUNT 0 -#define STATE_LAST_FOCUS 1 - - -// The script enviroment has 3 env allocations. -// bank0: (r) The enviroment structure -// bank1: (r) The position information -// bank2: (rw) The temporary texture state - -int lastFocus; - -int main(int index) -{ - float mat1[16]; - - float trans = Pos->translate; - float rot = Pos->rotate; - - matrixLoadScale(mat1, 2.f, 2.f, 2.f); - matrixTranslate(mat1, 0.f, 0.f, trans); - matrixRotate(mat1, 90.f, 0.f, 0.f, 1.f); - matrixRotate(mat1, rot, 1.f, 0.f, 0.f); - vpLoadModelMatrix(mat1); - - // Draw the lighting effect in the strip and fill the Z buffer. - drawSimpleMesh(NAMED_mesh); - - // Start of images. - bindProgramStore(NAMED_PSImages); - bindProgramFragment(NAMED_PFImages); - bindProgramVertex(NAMED_PVImages); - - float focusPos = Pos->focus; - int focusID = 0; - int lastFocusID = loadI32(2, STATE_LAST_FOCUS); - int imgCount = 13; - - if (trans > (-.3f)) { - focusID = -1.0f - focusPos; - if (focusID >= imgCount) { - focusID = -1; - } - } else { - focusID = -1; - } - - /* - if (focusID != lastFocusID) { - if (lastFocusID >= 0) { - uploadToTexture(con, env->tex[lastFocusID], 1); - } - if (focusID >= 0) { - uploadToTexture(con, env->tex[focusID], 0); - } - } - */ - lastFocus = focusID; - - int triangleOffsetsCount = Pos->triangleOffsetCount; - - int imgId = 0; - for (imgId=1; imgId <= imgCount; imgId++) { - float pos = focusPos + imgId + 0.4f; - int offset = (int)floorf(pos * 2.f); - pos = pos - 0.75f; - - offset = offset + triangleOffsetsCount / 2; - if (!((offset < 0) || (offset >= triangleOffsetsCount))) { - int start = offset -2; - int end = offset + 2; - - if (start < 0) { - start = 0; - } - if (end >= triangleOffsetsCount) { - end = triangleOffsetsCount-1; - } - - bindTexture(NAMED_PFImages, 0, loadI32(0, imgId - 1)); - matrixLoadTranslate(mat1, -pos - loadF(5, triangleOffsetsCount / 2), 0, 0); - vpLoadTextureMatrix(mat1); - drawSimpleMeshRange(NAMED_mesh, loadI32(4, start), (loadI32(4, end) - loadI32(4, start))); - } - } - return 0; -} - diff --git a/libs/rs/java/Film/src/com/android/film/FilmRS.java b/libs/rs/java/Film/src/com/android/film/FilmRS.java deleted file mode 100644 index 7d04502..0000000 --- a/libs/rs/java/Film/src/com/android/film/FilmRS.java +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package com.android.film; - -import java.io.Writer; - -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.util.Log; - -import android.renderscript.*; - -public class FilmRS { - class StripPosition { - public float translate; - public float rotate; - public float focus; - public int triangleOffsetCount; - } - StripPosition mPos = new StripPosition(); - - - private final int STATE_LAST_FOCUS = 1; - - public FilmRS() { - } - - public void init(RenderScriptGL rs, Resources res, int width, int height) { - mRS = rs; - mRes = res; - initRS(); - } - - public void setFilmStripPosition(int x, int y) - { - if (x < 50) { - x = 50; - } - if (x > 270) { - x = 270; - } - - float anim = ((float)x-50) / 270.f; - mPos.translate = 2f * anim + 0.5f; // translation - mPos.rotate = (anim * 40); // rotation - mPos.focus = ((float)y) / 16.f - 10.f; // focusPos - mPos.triangleOffsetCount = mFSM.mTriangleOffsetsCount; - mAllocPos.data(mPos); - } - - - private Resources mRes; - private RenderScriptGL mRS; - private Script mScriptStrip; - private Script mScriptImage; - private Sampler mSampler; - private ProgramStore mPSBackground; - private ProgramStore mPSImages; - private ProgramFragment mPFBackground; - private ProgramFragment mPFImages; - private ProgramVertex mPVBackground; - private ProgramVertex mPVImages; - private ProgramVertex.MatrixAllocation mPVA; - private Type mStripPositionType; - - private Allocation mImages[]; - private Allocation mAllocIDs; - private Allocation mAllocPos; - private Allocation mAllocState; - private Allocation mAllocPV; - private Allocation mAllocOffsetsTex; - private Allocation mAllocOffsets; - - private SimpleMesh mMesh; - private Light mLight; - - private FilmStripMesh mFSM; - - private int[] mBufferIDs; - private float[] mBufferPos = new float[3]; - private int[] mBufferState; - - private void initPFS() { - ProgramStore.Builder b = new ProgramStore.Builder(mRS, null, null); - - b.setDepthFunc(ProgramStore.DepthFunc.LESS); - b.setDitherEnable(true); - b.setDepthMask(true); - mPSBackground = b.create(); - mPSBackground.setName("PSBackground"); - - b.setDepthFunc(ProgramStore.DepthFunc.EQUAL); - b.setDitherEnable(false); - b.setDepthMask(false); - b.setBlendFunc(ProgramStore.BlendSrcFunc.ONE, - ProgramStore.BlendDstFunc.ONE); - mPSImages = b.create(); - mPSImages.setName("PSImages"); - } - - private void initPF() { - Sampler.Builder bs = new Sampler.Builder(mRS); - bs.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR); - bs.setMag(Sampler.Value.LINEAR); - bs.setWrapS(Sampler.Value.CLAMP); - bs.setWrapT(Sampler.Value.WRAP); - mSampler = bs.create(); - - ProgramFragment.Builder b = new ProgramFragment.Builder(mRS); - mPFBackground = b.create(); - mPFBackground.setName("PFBackground"); - - b = new ProgramFragment.Builder(mRS); - b.setTexture(ProgramFragment.Builder.EnvMode.REPLACE, - ProgramFragment.Builder.Format.RGBA, 0); - mPFImages = b.create(); - mPFImages.bindSampler(mSampler, 0); - mPFImages.setName("PFImages"); - } - - private void initPV() { - mLight = (new Light.Builder(mRS)).create(); - mLight.setPosition(0, -0.5f, -1.0f); - - ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); - //pvb.addLight(mLight); - mPVBackground = pvb.create(); - mPVBackground.setName("PVBackground"); - - pvb = new ProgramVertex.Builder(mRS, null, null); - pvb.setTextureMatrixEnable(true); - mPVImages = pvb.create(); - mPVImages.setName("PVImages"); - } - - private void loadImages() { - mBufferIDs = new int[13]; - mImages = new Allocation[13]; - mAllocIDs = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.FLOAT_32), - mBufferIDs.length); - - Element ie = Element.createPixel(mRS, Element.DataType.UNSIGNED_5_6_5, Element.DataKind.PIXEL_RGB); - mImages[0] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p01, ie, true); - mImages[1] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p02, ie, true); - mImages[2] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p03, ie, true); - mImages[3] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p04, ie, true); - mImages[4] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p05, ie, true); - mImages[5] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p06, ie, true); - mImages[6] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p07, ie, true); - mImages[7] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p08, ie, true); - mImages[8] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p09, ie, true); - mImages[9] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p10, ie, true); - mImages[10] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p11, ie, true); - mImages[11] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p12, ie, true); - mImages[12] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p13, ie, true); - - int black[] = new int[1024]; - for(int ct=0; ct < mImages.length; ct++) { - Allocation.Adapter2D a = mImages[ct].createAdapter2D(); - - int size = 512; - int mip = 0; - while(size >= 2) { - a.subData(0, 0, 2, size, black); - a.subData(size-2, 0, 2, size, black); - a.subData(0, 0, size, 2, black); - a.subData(0, size-2, size, 2, black); - size >>= 1; - mip++; - a.setConstraint(Dimension.LOD, mip); - } - - mImages[ct].uploadToTexture(1); - mBufferIDs[ct] = mImages[ct].getID(); - } - mAllocIDs.data(mBufferIDs); - } - - private void initState() - { - mBufferState = new int[10]; - mAllocState = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.FLOAT_32), - mBufferState.length); - mBufferState[STATE_LAST_FOCUS] = -1; - mAllocState.data(mBufferState); - } - - private void initRS() { - mFSM = new FilmStripMesh(); - mMesh = mFSM.init(mRS); - mMesh.setName("mesh"); - - initPFS(); - initPF(); - initPV(); - - Log.e("rs", "Done loading named"); - - mStripPositionType = Type.createFromClass(mRS, StripPosition.class, 1); - - ScriptC.Builder sb = new ScriptC.Builder(mRS); - sb.setScript(mRes, R.raw.filmstrip); - sb.setRoot(true); - sb.setType(mStripPositionType, "Pos", 1); - mScriptStrip = sb.create(); - mScriptStrip.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); - - mAllocPos = Allocation.createTyped(mRS, mStripPositionType); - - loadImages(); - initState(); - - mPVA = new ProgramVertex.MatrixAllocation(mRS); - mPVBackground.bindAllocation(mPVA); - mPVImages.bindAllocation(mPVA); - mPVA.setupProjectionNormalized(320, 480); - - - mScriptStrip.bindAllocation(mAllocIDs, 0); - mScriptStrip.bindAllocation(mAllocPos, 1); - mScriptStrip.bindAllocation(mAllocState, 2); - mScriptStrip.bindAllocation(mPVA.mAlloc, 3); - - - mAllocOffsets = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.SIGNED_32), mFSM.mTriangleOffsets.length); - mAllocOffsets.data(mFSM.mTriangleOffsets); - mScriptStrip.bindAllocation(mAllocOffsets, 4); - - mAllocOffsetsTex = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.FLOAT_32), mFSM.mTriangleOffsetsTex.length); - mAllocOffsetsTex.data(mFSM.mTriangleOffsetsTex); - mScriptStrip.bindAllocation(mAllocOffsetsTex, 5); - - setFilmStripPosition(0, 0); - mRS.contextBindRootScript(mScriptStrip); - } -} - - - diff --git a/libs/rs/java/Film/src/com/android/film/FilmStripMesh.java b/libs/rs/java/Film/src/com/android/film/FilmStripMesh.java deleted file mode 100644 index 448cce0..0000000 --- a/libs/rs/java/Film/src/com/android/film/FilmStripMesh.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright (C) 2009 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. - */ - - -package com.android.film; - -import java.io.Writer; -import java.lang.Math; -import android.util.Log; - -import android.renderscript.RenderScript; -import android.renderscript.SimpleMesh; - - -class FilmStripMesh { - - class Vertex { - float nx; - float ny; - float nz; - float s; - float t; - float x; - float y; - float z; - - Vertex() { - nx = 0; - ny = 0; - nz = 0; - s = 0; - t = 0; - x = 0; - y = 0; - z = 0; - } - - void xyz(float _x, float _y, float _z) { - x = _x; - y = _y; - z = _z; - } - - void nxyz(float _x, float _y, float _z) { - nx = _x; - ny = _y; - nz = _z; - } - - void st(float _s, float _t) { - s = _s; - t = _t; - } - - void computeNorm(Vertex v1, Vertex v2) { - float dx = v1.x - v2.x; - float dy = v1.y - v2.y; - float dz = v1.z - v2.z; - float len = (float)java.lang.Math.sqrt(dx*dx + dy*dy + dz*dz); - dx /= len; - dy /= len; - dz /= len; - - nx = dx * dz; - ny = dy * dz; - nz = (float)java.lang.Math.sqrt(dx*dx + dy*dy); - - len = (float)java.lang.Math.sqrt(nx*nx + ny*ny + nz*nz); - nx /= len; - ny /= len; - nz /= len; - } - } - - int[] mTriangleOffsets; - float[] mTriangleOffsetsTex; - int mTriangleOffsetsCount; - - SimpleMesh init(RenderScript rs) - { - float vtx[] = new float[] { - 60.431003f, 124.482050f, - 60.862074f, 120.872604f, - 61.705303f, 117.336662f, - 62.949505f, 113.921127f, - 64.578177f, 110.671304f, - 66.569716f, 107.630302f, - 68.897703f, 104.838457f, - 71.531259f, 102.332803f, - 74.435452f, 100.146577f, - 77.571757f, 98.308777f, - 80.898574f, 96.843781f, - 84.371773f, 95.771023f, - 87.945283f, 95.104731f, - 98.958994f, 95.267098f, - 109.489523f, 98.497596f, - 118.699582f, 104.539366f, - 125.856872f, 112.912022f, - 130.392311f, 122.949849f, - 131.945283f, 133.854731f, - 130.392311f, 144.759613f, - 125.856872f, 154.797439f, - 118.699582f, 163.170096f, - 109.489523f, 169.211866f, - 98.958994f, 172.442364f, - 87.945283f, 172.604731f, - 72.507313f, 172.672927f, - 57.678920f, 168.377071f, - 44.668135f, 160.067134f, - 34.534908f, 148.420104f, - 28.104767f, 134.384831f, - 25.901557f, 119.104731f, - 28.104767f, 103.824631f, - 34.534908f, 89.789358f, - 44.668135f, 78.142327f, - 57.678920f, 69.832390f, - 72.507313f, 65.536534f, - 87.945283f, 65.604731f, - 106.918117f, 65.688542f, - 125.141795f, 60.409056f, - 141.131686f, 50.196376f, - 153.585137f, 35.882502f, - 161.487600f, 18.633545f, - 164.195283f, -0.145269f, - 161.487600f, -18.924084f, - 153.585137f, -36.173040f, - 141.131686f, -50.486914f, - 125.141795f, -60.699594f, - 106.918117f, -65.979081f, - 87.945283f, -65.895269f, - 80f, -65.895269f, - 60f, -65.895269f, - 40f, -65.895269f, - 20f, -65.895269f, - 0f, -65.895269f, - -20f, -65.895269f, - -40f, -65.895269f, - -60f, -65.895269f, - -80f, -65.895269f, - -87.945283f, -65.895269f, - -106.918117f, -65.979081f, - -125.141795f, -60.699594f, - -141.131686f, -50.486914f, - -153.585137f, -36.173040f, - -161.487600f, -18.924084f, - -164.195283f, -0.145269f, - -161.487600f, 18.633545f, - -153.585137f, 35.882502f, - -141.131686f, 50.196376f, - -125.141795f, 60.409056f, - -106.918117f, 65.688542f, - -87.945283f, 65.604731f, - -72.507313f, 65.536534f, - -57.678920f, 69.832390f, - -44.668135f, 78.142327f, - -34.534908f, 89.789358f, - -28.104767f, 103.824631f, - -25.901557f, 119.104731f, - -28.104767f, 134.384831f, - -34.534908f, 148.420104f, - -44.668135f, 160.067134f, - -57.678920f, 168.377071f, - -72.507313f, 172.672927f, - -87.945283f, 172.604731f, - -98.958994f, 172.442364f, - -109.489523f, 169.211866f, - -118.699582f, 163.170096f, - -125.856872f, 154.797439f, - -130.392311f, 144.759613f, - -131.945283f, 133.854731f, - -130.392311f, 122.949849f, - -125.856872f, 112.912022f, - -118.699582f, 104.539366f, - -109.489523f, 98.497596f, - -98.958994f, 95.267098f, - -87.945283f, 95.104731f, - -84.371773f, 95.771023f, - -80.898574f, 96.843781f, - -77.571757f, 98.308777f, - -74.435452f, 100.146577f, - -71.531259f, 102.332803f, - -68.897703f, 104.838457f, - -66.569716f, 107.630302f, - -64.578177f, 110.671304f, - -62.949505f, 113.921127f, - -61.705303f, 117.336662f, - -60.862074f, 120.872604f, - -60.431003f, 124.482050f - }; - - - mTriangleOffsets = new int[64]; - mTriangleOffsetsTex = new float[64]; - - mTriangleOffsets[0] = 0; - mTriangleOffsetsCount = 1; - - Vertex t = new Vertex(); - t.nxyz(1, 0, 0); - int count = vtx.length / 2; - - SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder( - rs, 3, - SimpleMesh.TriangleMeshBuilder.NORMAL | SimpleMesh.TriangleMeshBuilder.TEXTURE_0); - - float runningS = 0; - for (int ct=0; ct < (count-1); ct++) { - t.x = -vtx[ct*2] / 100.f; - t.z = vtx[ct*2+1] / 100.f; - t.s = runningS; - t.nx = (vtx[ct*2+3] - vtx[ct*2 +1]); - t.ny = (vtx[ct*2+2] - vtx[ct*2 ]); - float len = (float)java.lang.Math.sqrt(t.nx * t.nx + t.ny * t.ny); - runningS += len / 100; - t.nx /= len; - t.ny /= len; - t.y = -0.5f; - t.t = 0; - tm.setNormal(t.nx, t.ny, t.nz); - tm.setTexture(t.s, t.t); - tm.addVertex(t.x, t.y, t.z); - //android.util.Log.e("rs", "vtx x="+t.x+" y="+t.y+" z="+t.z+" s="+t.s+" t="+t.t); - t.y = .5f; - t.t = 1; - tm.setTexture(t.s, t.t); - tm.addVertex(t.x, t.y, t.z); - //android.util.Log.e("rs", "vtx x="+t.x+" y="+t.y+" z="+t.z+" s="+t.s+" t="+t.t); - - if((runningS*2) > mTriangleOffsetsCount) { - mTriangleOffsets[mTriangleOffsetsCount] = ct*2 * 3; - mTriangleOffsetsTex[mTriangleOffsetsCount] = t.s; - mTriangleOffsetsCount ++; - } - } - - count = (count * 2 - 2); - for (int ct=0; ct < (count-2); ct+= 2) { - tm.addTriangle(ct, ct+1, ct+2); - tm.addTriangle(ct+1, ct+3, ct+2); - } - return tm.create(); - } - - -} - diff --git a/libs/rs/java/Fountain/Android.mk b/libs/rs/java/Fountain/Android.mk index f7e53a8..71944b2 100644 --- a/libs/rs/java/Fountain/Android.mk +++ b/libs/rs/java/Fountain/Android.mk @@ -14,14 +14,18 @@ # limitations under the License. # +ifneq ($(TARGET_SIMULATOR),true) + LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src) #LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript LOCAL_PACKAGE_NAME := Fountain include $(BUILD_PACKAGE) + +endif diff --git a/libs/rs/java/Fountain/res/raw/fountain.c b/libs/rs/java/Fountain/res/raw/fountain.c deleted file mode 100644 index 73b819b..0000000 --- a/libs/rs/java/Fountain/res/raw/fountain.c +++ /dev/null @@ -1,52 +0,0 @@ -// Fountain test script -#pragma version(1) - -int newPart = 0; - -int main(int launchID) { - int ct; - int count = Control->count; - int rate = Control->rate; - float height = getHeight(); - struct point_s * p = (struct point_s *)point; - - if (rate) { - float rMax = ((float)rate) * 0.005f; - int x = Control->x; - int y = Control->y; - int color = ((int)(Control->r * 255.f)) | - ((int)(Control->g * 255.f)) << 8 | - ((int)(Control->b * 255.f)) << 16 | - (0xf0 << 24); - struct point_s * np = &p[newPart]; - - while (rate--) { - vec2Rand((float *)&np->delta.x, rMax); - np->position.x = x; - np->position.y = y; - np->color = color; - newPart++; - np++; - if (newPart >= count) { - newPart = 0; - np = &p[newPart]; - } - } - } - - for (ct=0; ct < count; ct++) { - float dy = p->delta.y + 0.15f; - float posy = p->position.y + dy; - if ((posy > height) && (dy > 0)) { - dy *= -0.3f; - } - p->delta.y = dy; - p->position.x += p->delta.x; - p->position.y = posy; - p++; - } - - uploadToBufferObject(NAMED_PartBuffer); - drawSimpleMesh(NAMED_PartMesh); - return 1; -} diff --git a/libs/rs/java/Fountain/res/raw/fountain2.rs b/libs/rs/java/Fountain/res/raw/fountain2.rs deleted file mode 100644 index 3301140..0000000 --- a/libs/rs/java/Fountain/res/raw/fountain2.rs +++ /dev/null @@ -1,73 +0,0 @@ -// Fountain test script -#pragma version(1) - -#include "rs_types.rsh" -#include "rs_math.rsh" -#include "rs_graphics.rsh" - -static int newPart = 0; - -typedef struct Control_s { - int x, y; - int rate; - int count; - float r, g, b; - rs_allocation partBuffer; - rs_mesh partMesh; -} Control_t; -Control_t *Control; - -typedef struct Point_s{ - float2 delta; - float2 position; - unsigned int color; -} Point_t; -Point_t *point; - -int main(int launchID) { - int ct; - int count = Control->count; - int rate = Control->rate; - float height = getHeight(); - Point_t * p = point; - - if (rate) { - float rMax = ((float)rate) * 0.005f; - int x = Control->x; - int y = Control->y; - int color = ((int)(Control->r * 255.f)) | - ((int)(Control->g * 255.f)) << 8 | - ((int)(Control->b * 255.f)) << 16 | - (0xf0 << 24); - Point_t * np = &p[newPart]; - - while (rate--) { - np->delta = vec2Rand(rMax); - np->position.x = x; - np->position.y = y; - np->color = color; - newPart++; - np++; - if (newPart >= count) { - newPart = 0; - np = &p[newPart]; - } - } - } - - for (ct=0; ct < count; ct++) { - float dy = p->delta.y + 0.15f; - float posy = p->position.y + dy; - if ((posy > height) && (dy > 0)) { - dy *= -0.3f; - } - p->delta.y = dy; - p->position.x += p->delta.x; - p->position.y = posy; - p++; - } - - uploadToBufferObject(Control->partBuffer); - drawSimpleMesh(Control->partMesh); - return 1; -} diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java index 9356579..297ea07 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java @@ -22,94 +22,50 @@ import android.util.Log; public class FountainRS { - public static final int PART_COUNT = 20000; - - static class SomeData { - public int x; - public int y; - public int rate; - public int count; - public float r; - public float g; - public float b; - } + public static final int PART_COUNT = 50000; public FountainRS() { } + private Resources mRes; + private RenderScriptGL mRS; + private ScriptC_Fountain mScript; public void init(RenderScriptGL rs, Resources res, int width, int height) { mRS = rs; mRes = res; - initRS(); - } - - public void newTouchPosition(int x, int y, int rate) { - if (mSD.rate == 0) { - mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f; - mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f; - mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f; - if ((mSD.r + mSD.g + mSD.b) < 0.9f) { - mSD.r = 0.8f; - mSD.g = 0.5f; - mSD.b = 1.f; - } - } - mSD.rate = rate; - mSD.x = x; - mSD.y = y; - mIntAlloc.data(mSD); - } - - - ///////////////////////////////////////// - - private Resources mRes; - - private RenderScriptGL mRS; - private Allocation mIntAlloc; - private SimpleMesh mSM; - private SomeData mSD; - private Type mSDType; - private void initRS() { - mSD = new SomeData(); - mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData"); - mIntAlloc = Allocation.createTyped(mRS, mSDType); - mSD.count = PART_COUNT; - mIntAlloc.data(mSD); + ProgramFragment.Builder pfb = new ProgramFragment.Builder(rs); + pfb.setVaryingColor(true); + rs.contextBindProgramFragment(pfb.create()); - Element.Builder eb = new Element.Builder(mRS); - eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "delta"); - eb.add(Element.createAttrib(mRS, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "position"); - eb.add(Element.createAttrib(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color"); - Element primElement = eb.create(); + ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT); + Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS); + smb.addVertexAllocation(points.getAllocation()); + smb.addIndexType(Primitive.POINT); + Mesh sm = smb.create(); - SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS); - int vtxSlot = smb.addVertexType(primElement, PART_COUNT); - smb.setPrimitive(Primitive.POINT); - mSM = smb.create(); - mSM.setName("PartMesh"); - - Allocation partAlloc = mSM.createVertexAllocation(vtxSlot); - partAlloc.setName("PartBuffer"); - mSM.bindVertexAllocation(partAlloc, 0); + mScript = new ScriptC_Fountain(mRS, mRes, R.raw.fountain, true); + mScript.set_partMesh(sm); + mScript.bind_point(points); + mRS.contextBindRootScript(mScript); + } - // All setup of named objects should be done by this point - // because we are about to compile the script. - ScriptC.Builder sb = new ScriptC.Builder(mRS); - sb.setScript(mRes, R.raw.fountain); - sb.setRoot(true); - sb.setType(mSDType, "Control", 0); - sb.setType(mSM.getVertexType(0), "point", 1); - Script script = sb.create(); - script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); + boolean holdingColor[] = new boolean[10]; + public void newTouchPosition(float x, float y, float pressure, int id) { + if (id > holdingColor.length) { + return; + } + int rate = (int)(pressure * pressure * 500.f); + if(rate > 500) { + rate = 500; + } + if (rate > 0) { + mScript.invoke_addParticles(rate, x, y, id, !holdingColor[id]); + holdingColor[id] = true; + } else { + holdingColor[id] = false; + } - script.bindAllocation(mIntAlloc, 0); - script.bindAllocation(partAlloc, 1); - mRS.contextBindRootScript(script); } - } - - diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java index dfd6a49..c141165 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java @@ -71,17 +71,33 @@ public class FountainView extends RSSurfaceView { @Override public boolean onTouchEvent(MotionEvent ev) { - int act = ev.getAction(); + int act = ev.getActionMasked(); if (act == ev.ACTION_UP) { - mRender.newTouchPosition(0, 0, 0); + mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0)); return false; + } else if (act == MotionEvent.ACTION_POINTER_UP) { + // only one pointer going up, we can get the index like this + int pointerIndex = ev.getActionIndex(); + int pointerId = ev.getPointerId(pointerIndex); + mRender.newTouchPosition(0, 0, 0, pointerId); } - float rate = (ev.getPressure() * 50.f); - rate *= rate; - if(rate > 2000.f) { - rate = 2000.f; + int count = ev.getHistorySize(); + int pcount = ev.getPointerCount(); + + for (int p=0; p < pcount; p++) { + int id = ev.getPointerId(p); + mRender.newTouchPosition(ev.getX(p), + ev.getY(p), + ev.getPressure(p), + id); + + for (int i=0; i < count; i++) { + mRender.newTouchPosition(ev.getHistoricalX(p, i), + ev.getHistoricalY(p, i), + ev.getHistoricalPressure(p, i), + id); + } } - mRender.newTouchPosition((int)ev.getX(), (int)ev.getY(), (int)rate); return true; } } diff --git a/libs/rs/java/Fountain/src/com/android/fountain/fountain.rs b/libs/rs/java/Fountain/src/com/android/fountain/fountain.rs new file mode 100644 index 0000000..812cb7a --- /dev/null +++ b/libs/rs/java/Fountain/src/com/android/fountain/fountain.rs @@ -0,0 +1,72 @@ +// Fountain test script +#pragma version(1) + +#pragma rs java_package_name(com.android.fountain) + +#pragma stateFragment(parent) + +#include "rs_graphics.rsh" + +static int newPart = 0; +rs_mesh partMesh; + +typedef struct __attribute__((packed, aligned(4))) Point { + float2 delta; + float2 position; + uchar4 color; +} Point_t; +Point_t *point; + +#pragma rs export_var(point, partMesh) +#pragma rs export_func(addParticles) + +int root() { + float dt = min(rsGetDt(), 0.1f); + rsgClearColor(0.f, 0.f, 0.f, 1.f); + const float height = rsgGetHeight(); + const int size = rsAllocationGetDimX(rsGetAllocation(point)); + float dy2 = dt * (10.f); + Point_t * p = point; + for (int ct=0; ct < size; ct++) { + p->delta.y += dy2; + p->position += p->delta; + if ((p->position.y > height) && (p->delta.y > 0)) { + p->delta.y *= -0.3f; + } + p++; + } + + rsgDrawMesh(partMesh); + return 1; +} + +static float4 partColor[10]; +void addParticles(int rate, float x, float y, int index, bool newColor) +{ + if (newColor) { + partColor[index].x = rsRand(0.5f, 1.0f); + partColor[index].y = rsRand(1.0f); + partColor[index].z = rsRand(1.0f); + } + float rMax = ((float)rate) * 0.02f; + int size = rsAllocationGetDimX(rsGetAllocation(point)); + uchar4 c = rsPackColorTo8888(partColor[index]); + + Point_t * np = &point[newPart]; + float2 p = {x, y}; + while (rate--) { + float angle = rsRand(3.14f * 2.f); + float len = rsRand(rMax); + np->delta.x = len * sin(angle); + np->delta.y = len * cos(angle); + np->position = p; + np->color = c; + newPart++; + np++; + if (newPart >= size) { + newPart = 0; + np = &point[newPart]; + } + } +} + diff --git a/libs/rs/java/ImageProcessing/Android.mk b/libs/rs/java/ImageProcessing/Android.mk index 833427b..7fa30d0 100644 --- a/libs/rs/java/ImageProcessing/Android.mk +++ b/libs/rs/java/ImageProcessing/Android.mk @@ -14,14 +14,19 @@ # limitations under the License. # +ifneq ($(TARGET_SIMULATOR),true) + LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_SRC_FILES := $(call all-java-files-under, src) \ + $(call all-renderscript-files-under, src) #LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript LOCAL_PACKAGE_NAME := ImageProcessing include $(BUILD_PACKAGE) + +endif diff --git a/libs/rs/java/ImageProcessing/AndroidManifest.xml b/libs/rs/java/ImageProcessing/AndroidManifest.xml index b48d208..d6a2db4 100644 --- a/libs/rs/java/ImageProcessing/AndroidManifest.xml +++ b/libs/rs/java/ImageProcessing/AndroidManifest.xml @@ -6,7 +6,8 @@ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:label="Image Processing"> - <activity android:name="ImageProcessingActivity"> + <activity android:name="ImageProcessingActivity" + android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> diff --git a/libs/rs/java/ImageProcessing/res/drawable/data.jpg b/libs/rs/java/ImageProcessing/res/drawable/data.jpg Binary files differnew file mode 100644 index 0000000..81a87b1 --- /dev/null +++ b/libs/rs/java/ImageProcessing/res/drawable/data.jpg diff --git a/libs/rs/java/ImageProcessing/res/layout/main.xml b/libs/rs/java/ImageProcessing/res/layout/main.xml index 6770c18..c6ec729 100644 --- a/libs/rs/java/ImageProcessing/res/layout/main.xml +++ b/libs/rs/java/ImageProcessing/res/layout/main.xml @@ -25,9 +25,147 @@ android:id="@+id/display" android:layout_width="320dip" android:layout_height="266dip" /> - + + <Button + android:layout_marginBottom="170dip" + android:layout_width="wrap_content" + android:layout_height="40dip" + android:text="@string/benchmark" + android:onClick="benchmark" + android:layout_gravity="bottom"/> + + <TextView + android:id="@+id/benchmarkText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="100dip" + android:layout_marginBottom="175dip" + android:layout_gravity="bottom" + android:text="@string/saturation"/> + + <SeekBar + android:id="@+id/inSaturation" + android:layout_marginBottom="140dip" + android:layout_marginLeft="10dip" + android:layout_marginRight="10dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/inSaturationText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="50dip" + android:layout_marginBottom="142dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/saturation"/> + + <SeekBar + android:id="@+id/inGamma" + android:layout_marginBottom="110dip" + android:layout_marginLeft="10dip" + android:layout_marginRight="10dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/inGammaText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="50dip" + android:layout_marginBottom="112dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/gamma"/> + <SeekBar - android:id="@+id/threshold" + android:id="@+id/outWhite" + android:layout_marginBottom="80dip" + android:layout_marginLeft="170dip" + android:layout_marginRight="10dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/outWhiteText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="220dip" + android:layout_marginBottom="82dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/out_white"/> + + <SeekBar + android:id="@+id/inWhite" + android:layout_marginBottom="80dip" + android:layout_marginLeft="10dip" + android:layout_marginRight="170dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/inWhiteText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="50dip" + android:layout_marginBottom="82dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/in_white"/> + + <SeekBar + android:id="@+id/outBlack" + android:layout_marginBottom="50dip" + android:layout_marginLeft="170dip" + android:layout_marginRight="10dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/outBlackText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="220dip" + android:layout_marginBottom="52dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/out_black"/> + + <SeekBar + android:id="@+id/inBlack" + android:layout_marginBottom="50dip" + android:layout_marginLeft="10dip" + android:layout_marginRight="170dip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="bottom" /> + + <TextView + android:id="@+id/inBlackText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="50dip" + android:layout_marginBottom="52dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/in_black"/> + + <SeekBar + android:id="@+id/radius" android:layout_marginBottom="10dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" @@ -35,4 +173,15 @@ android:layout_height="wrap_content" android:layout_gravity="bottom" /> + <TextView + android:id="@+id/blurText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="50dip" + android:layout_marginBottom="12dip" + android:textColor="#000" + android:layout_gravity="bottom" + android:text="@string/blur_description"/> + </merge>
\ No newline at end of file diff --git a/libs/rs/java/ImageProcessing/res/raw/threshold.rs b/libs/rs/java/ImageProcessing/res/raw/threshold.rs deleted file mode 100644 index 888f0cd..0000000 --- a/libs/rs/java/ImageProcessing/res/raw/threshold.rs +++ /dev/null @@ -1,62 +0,0 @@ -/* -// block of defines matching what RS will insert at runtime. -struct Params_s{ - int inHeight; - int inWidth; - int outHeight; - int outWidth; - float threshold; -}; -struct Params_s * Params; -struct InPixel_s{ - char a; - char b; - char g; - char r; -}; -struct InPixel_s * InPixel; -struct OutPixel_s{ - char a; - char b; - char g; - char r; -}; -struct OutPixel_s * OutPixel; -*/ - -struct color_s { - char b; - char g; - char r; - char a; -}; - -void main() { - int t = uptimeMillis(); - - struct color_s *in = (struct color_s *) InPixel; - struct color_s *out = (struct color_s *) OutPixel; - - int count = Params->inWidth * Params->inHeight; - int i; - float threshold = (Params->threshold * 255.f); - - for (i = 0; i < count; i++) { - float luminance = 0.2125f * in->r + - 0.7154f * in->g + - 0.0721f * in->b; - if (luminance > threshold) { - *out = *in; - } else { - *((int *)out) = *((int *)in) & 0xff000000; - } - - in++; - out++; - } - - t= uptimeMillis() - t; - debugI32("Filter time", t); - - sendToClient(&count, 1, 4, 0); -} diff --git a/libs/rs/java/ImageProcessing/res/values/strings.xml b/libs/rs/java/ImageProcessing/res/values/strings.xml new file mode 100644 index 0000000..cc5cc4d --- /dev/null +++ b/libs/rs/java/ImageProcessing/res/values/strings.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +* Copyright (C) 2008 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. +*/ +--> + +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + <!-- General --> + <skip /> + <!--slider label --> + <string name="blur_description">Blur Radius</string> + <string name="in_white">In White</string> + <string name="out_white">Out White</string> + <string name="in_black">In Black</string> + <string name="out_black">Out Black</string> + <string name="gamma">Gamma</string> + <string name="saturation">Saturation</string> + <string name="benchmark">Benchmark</string> + +</resources> diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java index 9ce53d8..24ef547 100644 --- a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java @@ -31,53 +31,56 @@ import android.view.SurfaceView; import android.view.SurfaceHolder; import android.widget.ImageView; import android.widget.SeekBar; +import android.widget.TextView; +import android.view.View; import java.lang.Math; -public class ImageProcessingActivity extends Activity implements SurfaceHolder.Callback { - private Bitmap mBitmap; - private Params mParams; - private Script.Invokable mInvokable; - private int[] mInData; - private int[] mOutData; +public class ImageProcessingActivity extends Activity + implements SurfaceHolder.Callback, + SeekBar.OnSeekBarChangeListener { + private Bitmap mBitmapIn; + private Bitmap mBitmapOut; + private Bitmap mBitmapScratch; + private ScriptC_Threshold mScript; + private ScriptC_Vertical_blur mScriptVBlur; + private ScriptC_Horizontal_blur mScriptHBlur; + private ScriptC_Levels mScriptLevels; + private int mRadius = 0; + private SeekBar mRadiusSeekBar; + + private float mInBlack = 0.0f; + private SeekBar mInBlackSeekBar; + private float mOutBlack = 0.0f; + private SeekBar mOutBlackSeekBar; + private float mInWhite = 255.0f; + private SeekBar mInWhiteSeekBar; + private float mOutWhite = 255.0f; + private SeekBar mOutWhiteSeekBar; + private float mGamma = 1.0f; + private SeekBar mGammaSeekBar; + + private float mSaturation = 1.0f; + private SeekBar mSaturationSeekBar; + + private TextView mBenchmarkResult; @SuppressWarnings({"FieldCanBeLocal"}) private RenderScript mRS; @SuppressWarnings({"FieldCanBeLocal"}) - private Type mParamsType; - @SuppressWarnings({"FieldCanBeLocal"}) - private Allocation mParamsAllocation; - @SuppressWarnings({"FieldCanBeLocal"}) private Type mPixelType; @SuppressWarnings({"FieldCanBeLocal"}) private Allocation mInPixelsAllocation; @SuppressWarnings({"FieldCanBeLocal"}) private Allocation mOutPixelsAllocation; + @SuppressWarnings({"FieldCanBeLocal"}) + private Allocation mScratchPixelsAllocation; private SurfaceView mSurfaceView; private ImageView mDisplayView; - static class Params { - public int inWidth; - public int outWidth; - public int inHeight; - public int outHeight; - - public float threshold; - } - - static class Pixel { - public byte a; - public byte r; - public byte g; - public byte b; - } - class FilterCallback extends RenderScript.RSMessage { private Runnable mAction = new Runnable() { public void run() { - mOutPixelsAllocation.readData(mOutData); - mBitmap.setPixels(mOutData, 0, mParams.outWidth, 0, 0, - mParams.outWidth, mParams.outHeight); mDisplayView.invalidate(); } }; @@ -89,29 +92,218 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C } } - private void javaFilter() { + int in[]; + int interm[]; + int out[]; + int MAX_RADIUS = 25; + // Store our coefficients here + float gaussian[]; + + private long javaFilter() { + final int width = mBitmapIn.getWidth(); + final int height = mBitmapIn.getHeight(); + final int count = width * height; + + if (in == null) { + in = new int[count]; + interm = new int[count]; + out = new int[count]; + gaussian = new float[MAX_RADIUS * 2 + 1]; + mBitmapIn.getPixels(in, 0, width, 0, 0, width, height); + } + long t = java.lang.System.currentTimeMillis(); - int count = mParams.inWidth * mParams.inHeight; - float threshold = mParams.threshold * 255.f; - - for (int i = 0; i < count; i++) { - final float r = (float)((mInData[i] >> 0) & 0xff); - final float g = (float)((mInData[i] >> 8) & 0xff); - final float b = (float)((mInData[i] >> 16) & 0xff); - - final float luminance = 0.2125f * r + - 0.7154f * g + - 0.0721f * b; - if (luminance > threshold) { - mOutData[i] = mInData[i]; - } else { - mOutData[i] = mInData[i] & 0xff000000; + + int w, h, r; + + float fRadius = (float)mRadius; + int radius = (int)mRadius; + + // Compute gaussian weights for the blur + // e is the euler's number + float e = 2.718281828459045f; + float pi = 3.1415926535897932f; + // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) + // x is of the form [-radius .. 0 .. radius] + // and sigma varies with radius. + // Based on some experimental radius values and sigma's + // we approximately fit sigma = f(radius) as + // sigma = radius * 0.4 + 0.6 + // The larger the radius gets, the more our gaussian blur + // will resemble a box blur since with large sigma + // the gaussian curve begins to lose its shape + float sigma = 0.4f * fRadius + 0.6f; + // Now compute the coefficints + // We will store some redundant values to save some math during + // the blur calculations + // precompute some values + float coeff1 = 1.0f / (float)(Math.sqrt( 2.0f * pi ) * sigma); + float coeff2 = - 1.0f / (2.0f * sigma * sigma); + float normalizeFactor = 0.0f; + float floatR = 0.0f; + for(r = -radius; r <= radius; r ++) { + floatR = (float)r; + gaussian[r + radius] = coeff1 * (float)Math.pow(e, floatR * floatR * coeff2); + normalizeFactor += gaussian[r + radius]; + } + + //Now we need to normalize the weights because all our coefficients need to add up to one + normalizeFactor = 1.0f / normalizeFactor; + for(r = -radius; r <= radius; r ++) { + floatR = (float)r; + gaussian[r + radius] *= normalizeFactor; + } + + float blurredPixelR = 0.0f; + float blurredPixelG = 0.0f; + float blurredPixelB = 0.0f; + float blurredPixelA = 0.0f; + + for(h = 0; h < height; h ++) { + for(w = 0; w < width; w ++) { + + blurredPixelR = 0.0f; + blurredPixelG = 0.0f; + blurredPixelB = 0.0f; + blurredPixelA = 0.0f; + + for(r = -radius; r <= radius; r ++) { + // Stepping left and right away from the pixel + int validW = w + r; + // Clamp to zero and width max() isn't exposed for ints yet + if(validW < 0) { + validW = 0; + } + if(validW > width - 1) { + validW = width - 1; + } + + int input = in[h*width + validW]; + + int R = ((input >> 24) & 0xff); + int G = ((input >> 16) & 0xff); + int B = ((input >> 8) & 0xff); + int A = (input & 0xff); + + float weight = gaussian[r + radius]; + + blurredPixelR += (float)(R)*weight; + blurredPixelG += (float)(G)*weight; + blurredPixelB += (float)(B)*weight; + blurredPixelA += (float)(A)*weight; + } + + int R = (int)blurredPixelR; + int G = (int)blurredPixelG; + int B = (int)blurredPixelB; + int A = (int)blurredPixelA; + + interm[h*width + w] = (R << 24) | (G << 16) | (B << 8) | (A); + } + } + + for(h = 0; h < height; h ++) { + for(w = 0; w < width; w ++) { + + blurredPixelR = 0.0f; + blurredPixelG = 0.0f; + blurredPixelB = 0.0f; + blurredPixelA = 0.0f; + for(r = -radius; r <= radius; r ++) { + int validH = h + r; + // Clamp to zero and width + if(validH < 0) { + validH = 0; + } + if(validH > height - 1) { + validH = height - 1; + } + + int input = interm[validH*width + w]; + + int R = ((input >> 24) & 0xff); + int G = ((input >> 16) & 0xff); + int B = ((input >> 8) & 0xff); + int A = (input & 0xff); + + float weight = gaussian[r + radius]; + + blurredPixelR += (float)(R)*weight; + blurredPixelG += (float)(G)*weight; + blurredPixelB += (float)(B)*weight; + blurredPixelA += (float)(A)*weight; + } + + int R = (int)blurredPixelR; + int G = (int)blurredPixelG; + int B = (int)blurredPixelB; + int A = (int)blurredPixelA; + + out[h*width + w] = (R << 24) | (G << 16) | (B << 8) | (A); } } t = java.lang.System.currentTimeMillis() - t; + android.util.Log.v("Img", "Java frame time ms " + t); + mBitmapOut.setPixels(out, 0, width, 0, 0, width, height); + return t; + } + + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) { + + if(seekBar == mRadiusSeekBar) { + float fRadius = progress / 100.0f; + fRadius *= (float)(MAX_RADIUS); + mRadius = (int)fRadius; - android.util.Log.v("Img", "frame time ms " + t); + mScript.set_radius(mRadius); + } + else if(seekBar == mInBlackSeekBar) { + mInBlack = (float)progress; + mScriptLevels.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); + } + else if(seekBar == mOutBlackSeekBar) { + mOutBlack = (float)progress; + mScriptLevels.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); + } + else if(seekBar == mInWhiteSeekBar) { + mInWhite = (float)progress + 127.0f; + mScriptLevels.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); + } + else if(seekBar == mOutWhiteSeekBar) { + mOutWhite = (float)progress + 127.0f; + mScriptLevels.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); + } + else if(seekBar == mGammaSeekBar) { + mGamma = (float)progress/100.0f; + mGamma = Math.max(mGamma, 0.1f); + mGamma = 1.0f / mGamma; + mScriptLevels.invoke_setGamma(mGamma); + } + else if(seekBar == mSaturationSeekBar) { + mSaturation = (float)progress / 50.0f; + mScriptLevels.invoke_setSaturation(mSaturation); + } + + long t = java.lang.System.currentTimeMillis(); + if (true) { + mScript.invoke_filter(); + mRS.finish(); + } else { + javaFilter(); + mDisplayView.invalidate(); + } + + t = java.lang.System.currentTimeMillis() - t; + android.util.Log.v("Img", "Renderscript frame time core ms " + t); + } + } + + public void onStartTrackingTouch(SeekBar seekBar) { + } + + public void onStopTrackingTouch(SeekBar seekBar) { } @Override @@ -119,45 +311,54 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C super.onCreate(savedInstanceState); setContentView(R.layout.main); - mBitmap = loadBitmap(R.drawable.data); + mBitmapIn = loadBitmap(R.drawable.data); + mBitmapOut = loadBitmap(R.drawable.data); + mBitmapScratch = loadBitmap(R.drawable.data); mSurfaceView = (SurfaceView) findViewById(R.id.surface); mSurfaceView.getHolder().addCallback(this); mDisplayView = (ImageView) findViewById(R.id.display); - mDisplayView.setImageBitmap(mBitmap); - - ((SeekBar) findViewById(R.id.threshold)).setOnSeekBarChangeListener( - new SeekBar.OnSeekBarChangeListener() { - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser) { - mParams.threshold = progress / 100.0f; - mParamsAllocation.data(mParams); - - if (true) { - mInvokable.execute(); - } else { - javaFilter(); - mBitmap.setPixels(mOutData, 0, mParams.outWidth, 0, 0, - mParams.outWidth, mParams.outHeight); - mDisplayView.invalidate(); - } - } - } - - public void onStartTrackingTouch(SeekBar seekBar) { - } - - public void onStopTrackingTouch(SeekBar seekBar) { - } - }); + mDisplayView.setImageBitmap(mBitmapOut); + + mRadiusSeekBar = (SeekBar) findViewById(R.id.radius); + mRadiusSeekBar.setOnSeekBarChangeListener(this); + + mInBlackSeekBar = (SeekBar)findViewById(R.id.inBlack); + mInBlackSeekBar.setOnSeekBarChangeListener(this); + mInBlackSeekBar.setMax(128); + mInBlackSeekBar.setProgress(0); + mOutBlackSeekBar = (SeekBar)findViewById(R.id.outBlack); + mOutBlackSeekBar.setOnSeekBarChangeListener(this); + mOutBlackSeekBar.setMax(128); + mOutBlackSeekBar.setProgress(0); + + mInWhiteSeekBar = (SeekBar)findViewById(R.id.inWhite); + mInWhiteSeekBar.setOnSeekBarChangeListener(this); + mInWhiteSeekBar.setMax(128); + mInWhiteSeekBar.setProgress(128); + mOutWhiteSeekBar = (SeekBar)findViewById(R.id.outWhite); + mOutWhiteSeekBar.setOnSeekBarChangeListener(this); + mOutWhiteSeekBar.setMax(128); + mOutWhiteSeekBar.setProgress(128); + + mGammaSeekBar = (SeekBar)findViewById(R.id.inGamma); + mGammaSeekBar.setOnSeekBarChangeListener(this); + mGammaSeekBar.setMax(150); + mGammaSeekBar.setProgress(100); + + mSaturationSeekBar = (SeekBar)findViewById(R.id.inSaturation); + mSaturationSeekBar.setOnSeekBarChangeListener(this); + mSaturationSeekBar.setProgress(50); + + mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); + mBenchmarkResult.setText("Benchmark no yet run"); } public void surfaceCreated(SurfaceHolder holder) { - mParams = createParams(); - mInvokable = createScript(); - - mInvokable.execute(); + createScript(); + mScript.invoke_filter(); + mRS.finish(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { @@ -166,54 +367,34 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C public void surfaceDestroyed(SurfaceHolder holder) { } - private Script.Invokable createScript() { + private void createScript() { mRS = RenderScript.create(); mRS.mMessageCallback = new FilterCallback(); - mParamsType = Type.createFromClass(mRS, Params.class, 1, "Parameters"); - mParamsAllocation = Allocation.createTyped(mRS, mParamsType); - mParamsAllocation.data(mParams); - - final int pixelCount = mParams.inWidth * mParams.inHeight; - - mPixelType = Type.createFromClass(mRS, Pixel.class, 1, "Pixel"); - mInPixelsAllocation = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.SIGNED_32), - pixelCount); - mOutPixelsAllocation = Allocation.createSized(mRS, - Element.createUser(mRS, Element.DataType.SIGNED_32), - pixelCount); - - mInData = new int[pixelCount]; - mBitmap.getPixels(mInData, 0, mParams.inWidth, 0, 0, mParams.inWidth, mParams.inHeight); - mInPixelsAllocation.data(mInData); - - mOutData = new int[pixelCount]; - mOutPixelsAllocation.data(mOutData); - - ScriptC.Builder sb = new ScriptC.Builder(mRS); - sb.setType(mParamsType, "Params", 0); - sb.setType(mPixelType, "InPixel", 1); - sb.setType(mPixelType, "OutPixel", 2); - sb.setType(true, 2); - Script.Invokable invokable = sb.addInvokable("main"); - sb.setScript(getResources(), R.raw.threshold); - //sb.setRoot(true); - - ScriptC script = sb.create(); - script.bindAllocation(mParamsAllocation, 0); - script.bindAllocation(mInPixelsAllocation, 1); - script.bindAllocation(mOutPixelsAllocation, 2); - - return invokable; - } + mInPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapIn); + mOutPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapOut); + mScratchPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapScratch); + + mScriptVBlur = new ScriptC_Vertical_blur(mRS, getResources(), R.raw.vertical_blur, false); + mScriptHBlur = new ScriptC_Horizontal_blur(mRS, getResources(), R.raw.horizontal_blur, false); + mScriptLevels = new ScriptC_Levels(mRS, getResources(), R.raw.levels, false); + + mScript = new ScriptC_Threshold(mRS, getResources(), R.raw.threshold, false); + mScript.set_width(mBitmapIn.getWidth()); + mScript.set_height(mBitmapIn.getHeight()); + mScript.set_radius(mRadius); + + mScriptLevels.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); + mScriptLevels.invoke_setGamma(mGamma); + mScriptLevels.invoke_setSaturation(mSaturation); + + mScript.bind_InPixel(mInPixelsAllocation); + mScript.bind_OutPixel(mOutPixelsAllocation); + mScript.bind_ScratchPixel(mScratchPixelsAllocation); - private Params createParams() { - final Params params = new Params(); - params.inWidth = params.outWidth = mBitmap.getWidth(); - params.inHeight = params.outHeight = mBitmap.getHeight(); - params.threshold = 0.5f; - return params; + mScript.set_vBlurScript(mScriptVBlur); + mScript.set_hBlurScript(mScriptHBlur); + mScript.set_levelsScript(mScriptLevels); } private Bitmap loadBitmap(int resource) { @@ -229,4 +410,30 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C source.recycle(); return b; } + + // button hook + public void benchmark(View v) { + android.util.Log.v("Img", "Benchmarking"); + int oldRadius = mRadius; + mRadius = MAX_RADIUS; + mScript.set_radius(mRadius); + + long t = java.lang.System.currentTimeMillis(); + + mScript.invoke_filterBenchmark(); + mRS.finish(); + + t = java.lang.System.currentTimeMillis() - t; + android.util.Log.v("Img", "Renderscript frame time core ms " + t); + + long javaTime = javaFilter(); + mBenchmarkResult.setText("RS: " + t + " ms Java: " + javaTime + " ms"); + //mBenchmarkResult.setText("RS: " + t + " ms"); + + mRadius = oldRadius; + mScript.set_radius(mRadius); + + mScript.invoke_filter(); + mRS.finish(); + } } diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/horizontal_blur.rs b/libs/rs/java/ImageProcessing/src/com/android/rs/image/horizontal_blur.rs new file mode 100644 index 0000000..4ed5aba --- /dev/null +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/horizontal_blur.rs @@ -0,0 +1,50 @@ +#pragma version(1) + +#include "ip.rsh" + +void root(const void *v_in, void *v_out, const void *usrData, uint32_t x, uint32_t y) { + uchar4 *output = (uchar4 *)v_out; + const FilterStruct *fs = (const FilterStruct *)usrData; + const uchar4 *input = (const uchar4 *)rsGetElementAt(fs->ain, 0, y); + + float3 blurredPixel = 0; + float3 currentPixel = 0; + + const float *gPtr = fs->gaussian; + if ((x > fs->radius) && (x < (fs->width - fs->radius))) { + const uchar4 *i = input + (x - fs->radius); + for(int r = -fs->radius; r <= fs->radius; r ++) { + currentPixel.x = (float)(i->x); + currentPixel.y = (float)(i->y); + currentPixel.z = (float)(i->z); + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + i++; + } + } else { + for(int r = -fs->radius; r <= fs->radius; r ++) { + // Stepping left and right away from the pixel + int validW = x + r; + // Clamp to zero and width max() isn't exposed for ints yet + if(validW < 0) { + validW = 0; + } + if(validW > fs->width - 1) { + validW = fs->width - 1; + } + //int validW = rsClamp(w + r, 0, width - 1); + + currentPixel.x = (float)(input[validW].x); + currentPixel.y = (float)(input[validW].y); + currentPixel.z = (float)(input[validW].z); + + blurredPixel += currentPixel * gPtr[0]; + gPtr++; + } + } + + output->x = (uint8_t)blurredPixel.x; + output->y = (uint8_t)blurredPixel.y; + output->z = (uint8_t)blurredPixel.z; +} + diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ip.rsh b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ip.rsh new file mode 100644 index 0000000..34213f5 --- /dev/null +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ip.rsh @@ -0,0 +1,15 @@ +#pragma rs java_package_name(com.android.rs.image) + +#define MAX_RADIUS 25 + +typedef struct { + rs_allocation ain; + + float *gaussian; //[MAX_RADIUS * 2 + 1]; + int height; + int width; + int radius; + +} FilterStruct; + + diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/levels.rs b/libs/rs/java/ImageProcessing/src/com/android/rs/image/levels.rs new file mode 100644 index 0000000..bb8a6fc --- /dev/null +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/levels.rs @@ -0,0 +1,86 @@ +#pragma version(1) + +#include "ip.rsh" + + +static float inBlack; +static float outBlack; +static float inWhite; +static float outWhite; +static float3 gamma; +static float saturation; + +static float inWMinInB; +static float outWMinOutB; +static float overInWMinInB; +static rs_matrix3x3 colorMat; + +//#pragma rs export_var(height, width, radius, InPixel, OutPixel, ScratchPixel, inBlack, outBlack, inWhite, outWhite, gamma, saturation, InPixel, OutPixel, ScratchPixel, vBlurScript, hBlurScript) +#pragma rs export_func(setLevels, setSaturation, setGamma); + +void setLevels(float iBlk, float oBlk, float iWht, float oWht) { + inBlack = iBlk; + outBlack = oBlk; + inWhite = iWht; + outWhite = oWht; + + inWMinInB = inWhite - inBlack; + outWMinOutB = outWhite - outBlack; + overInWMinInB = 1.f / inWMinInB; +} + +void setSaturation(float sat) { + saturation = sat; + + // Saturation + // Linear weights + //float rWeight = 0.3086f; + //float gWeight = 0.6094f; + //float bWeight = 0.0820f; + + // Gamma 2.2 weights (we haven't converted our image to linear space yet for perf reasons) + float rWeight = 0.299f; + float gWeight = 0.587f; + float bWeight = 0.114f; + + float oneMinusS = 1.0f - saturation; + rsMatrixSet(&colorMat, 0, 0, oneMinusS * rWeight + saturation); + rsMatrixSet(&colorMat, 0, 1, oneMinusS * rWeight); + rsMatrixSet(&colorMat, 0, 2, oneMinusS * rWeight); + rsMatrixSet(&colorMat, 1, 0, oneMinusS * gWeight); + rsMatrixSet(&colorMat, 1, 1, oneMinusS * gWeight + saturation); + rsMatrixSet(&colorMat, 1, 2, oneMinusS * gWeight); + rsMatrixSet(&colorMat, 2, 0, oneMinusS * bWeight); + rsMatrixSet(&colorMat, 2, 1, oneMinusS * bWeight); + rsMatrixSet(&colorMat, 2, 2, oneMinusS * bWeight + saturation); +} + +void setGamma(float g) { + gamma = (float3)g; +} + + +void root(const void *v_in, void *v_out, const void *usrData, uint32_t x, uint32_t y) { + const uchar4 *input = v_in; + uchar4 *output = v_out; + + float3 currentPixel = 0; + + //currentPixel.xyz = convert_float3(input.xyz); + currentPixel.x = (float)(input->x); + currentPixel.y = (float)(input->y); + currentPixel.z = (float)(input->z); + + float3 temp = rsMatrixMultiply(&colorMat, currentPixel); + temp = (clamp(temp, 0.f, 255.f) - inBlack) * overInWMinInB; + if (gamma.x != 1.0f) + temp = pow(temp, (float3)gamma); + currentPixel = clamp(temp * outWMinOutB + outBlack, 0.f, 255.f); + + //output.xyz = convert_uchar3(currentPixel.xyz); + output->x = (uint8_t)currentPixel.x; + output->y = (uint8_t)currentPixel.y; + output->z = (uint8_t)currentPixel.z; + output->w = input->w; +} + diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/threshold.rs b/libs/rs/java/ImageProcessing/src/com/android/rs/image/threshold.rs new file mode 100644 index 0000000..8e198c7 --- /dev/null +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/threshold.rs @@ -0,0 +1,100 @@ +#pragma version(1) + +#include "ip.rsh" + +int height; +int width; +int radius; + +uchar4 * InPixel; +uchar4 * OutPixel; +uchar4 * ScratchPixel; + +#pragma rs export_var(height, width, radius, InPixel, OutPixel, ScratchPixel, vBlurScript, hBlurScript, levelsScript) +#pragma rs export_func(filter, filterBenchmark); + +rs_script vBlurScript; +rs_script hBlurScript; +rs_script levelsScript; + +const int CMD_FINISHED = 1; + +// Store our coefficients here +static float gaussian[MAX_RADIUS * 2 + 1]; + + +static void computeGaussianWeights() { + // Compute gaussian weights for the blur + // e is the euler's number + float e = 2.718281828459045f; + float pi = 3.1415926535897932f; + // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) + // x is of the form [-radius .. 0 .. radius] + // and sigma varies with radius. + // Based on some experimental radius values and sigma's + // we approximately fit sigma = f(radius) as + // sigma = radius * 0.4 + 0.6 + // The larger the radius gets, the more our gaussian blur + // will resemble a box blur since with large sigma + // the gaussian curve begins to lose its shape + float sigma = 0.4f * (float)radius + 0.6f; + + // Now compute the coefficints + // We will store some redundant values to save some math during + // the blur calculations + // precompute some values + float coeff1 = 1.0f / (sqrt( 2.0f * pi ) * sigma); + float coeff2 = - 1.0f / (2.0f * sigma * sigma); + + float normalizeFactor = 0.0f; + float floatR = 0.0f; + int r; + for(r = -radius; r <= radius; r ++) { + floatR = (float)r; + gaussian[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2); + normalizeFactor += gaussian[r + radius]; + } + + //Now we need to normalize the weights because all our coefficients need to add up to one + normalizeFactor = 1.0f / normalizeFactor; + for(r = -radius; r <= radius; r ++) { + floatR = (float)r; + gaussian[r + radius] *= normalizeFactor; + } +} + + +static void blur() { + computeGaussianWeights(); + + FilterStruct fs; + fs.gaussian = gaussian; + fs.width = width; + fs.height = height; + fs.radius = radius; + + fs.ain = rsGetAllocation(InPixel); + rsForEach(hBlurScript, fs.ain, rsGetAllocation(ScratchPixel), &fs); + + fs.ain = rsGetAllocation(ScratchPixel); + rsForEach(vBlurScript, fs.ain, rsGetAllocation(OutPixel), &fs); +} + +void filter() { + //RS_DEBUG(radius); + + if(radius > 0) { + blur(); + rsForEach(levelsScript, rsGetAllocation(OutPixel), rsGetAllocation(OutPixel), 0); + } else { + rsForEach(levelsScript, rsGetAllocation(InPixel), rsGetAllocation(OutPixel), 0); + } + + rsSendToClientBlocking(CMD_FINISHED); +} + +void filterBenchmark() { + blur(); + rsSendToClientBlocking(CMD_FINISHED); +} + diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/vertical_blur.rs b/libs/rs/java/ImageProcessing/src/com/android/rs/image/vertical_blur.rs new file mode 100644 index 0000000..3c69289 --- /dev/null +++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/vertical_blur.rs @@ -0,0 +1,29 @@ +#pragma version(1) + +#include "ip.rsh" + +void root(const void *v_in, void *v_out, const void *usrData, uint32_t x, uint32_t y) { + uchar4 *output = (uchar4 *)v_out; + const FilterStruct *fs = (const FilterStruct *)usrData; + const uchar4 *input = (const uchar4 *)rsGetElementAt(fs->ain, x, 0); + + float3 blurredPixel = 0; + const float *gPtr = fs->gaussian; + if ((y > fs->radius) && (y < (fs->height - fs->radius))) { + const uchar4 *i = input + ((y - fs->radius) * fs->width); + for(int r = -fs->radius; r <= fs->radius; r ++) { + blurredPixel += convert_float3(i->xyz) * gPtr[0]; + gPtr++; + i += fs->width; + } + } else { + for(int r = -fs->radius; r <= fs->radius; r ++) { + int validH = rsClamp(y + r, (uint)0, (uint)(fs->height - 1)); + const uchar4 *i = input + validH * fs->width; + blurredPixel += convert_float3(i->xyz) * gPtr[0]; + gPtr++; + } + } + output->xyz = convert_uchar3(blurredPixel); +} + diff --git a/libs/rs/java/Film/Android.mk b/libs/rs/java/ModelViewer/Android.mk index 9e6ed7e..efe77d7 100644 --- a/libs/rs/java/Film/Android.mk +++ b/libs/rs/java/ModelViewer/Android.mk @@ -14,14 +14,18 @@ # limitations under the License. # +ifneq ($(TARGET_SIMULATOR),true) + LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src) #LOCAL_STATIC_JAVA_LIBRARIES := android.renderscript -LOCAL_PACKAGE_NAME := Film +LOCAL_PACKAGE_NAME := ModelViewer include $(BUILD_PACKAGE) + +endif diff --git a/libs/rs/java/Film/AndroidManifest.xml b/libs/rs/java/ModelViewer/AndroidManifest.xml index a5ce8a1..ebbe743 100644 --- a/libs/rs/java/Film/AndroidManifest.xml +++ b/libs/rs/java/ModelViewer/AndroidManifest.xml @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="com.android.film"> - <application android:label="Film"> - <activity android:name="Film" + package="com.android.modelviewer"> + <application android:label="ModelViewer"> + <activity android:name="ModelViewer" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar"> <intent-filter> diff --git a/libs/rs/java/ModelViewer/res/drawable/robot.png b/libs/rs/java/ModelViewer/res/drawable/robot.png Binary files differnew file mode 100644 index 0000000..7c85e56 --- /dev/null +++ b/libs/rs/java/ModelViewer/res/drawable/robot.png diff --git a/libs/rs/java/ModelViewer/res/raw/robot.a3d b/libs/rs/java/ModelViewer/res/raw/robot.a3d Binary files differnew file mode 100644 index 0000000..2d7d32b --- /dev/null +++ b/libs/rs/java/ModelViewer/res/raw/robot.a3d diff --git a/libs/rs/java/Film/src/com/android/film/Film.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewer.java index 6e99816..7491744 100644 --- a/libs/rs/java/Film/src/com/android/film/Film.java +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.film; +package com.android.modelviewer; import android.renderscript.RSSurfaceView; import android.renderscript.RenderScript; @@ -37,18 +37,9 @@ import android.widget.ListView; import java.lang.Runtime; -public class Film extends Activity { - //EventListener mListener = new EventListener(); - - private static final String LOG_TAG = "libRS_jni"; - private static final boolean DEBUG = false; - private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; - - private FilmView mView; - - // get the current looper (from your Activity UI thread for instance - +public class ModelViewer extends Activity { + private ModelViewerView mView; @Override public void onCreate(Bundle icicle) { @@ -56,7 +47,7 @@ public class Film extends Activity { // Create our Preview view and set it as the content of our // Activity - mView = new FilmView(this); + mView = new ModelViewerView(this); setContentView(mView); } @@ -74,17 +65,7 @@ public class Film extends Activity { // to take appropriate action when the activity looses focus super.onPause(); mView.onPause(); - - Runtime.getRuntime().exit(0); } - - static void log(String message) { - if (LOG_ENABLED) { - Log.v(LOG_TAG, message); - } - } - - } diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java new file mode 100644 index 0000000..479aaf3 --- /dev/null +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2008 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. + */ + +package com.android.modelviewer; + +import java.io.Writer; + +import android.content.res.Resources; +import android.renderscript.*; +import android.renderscript.ProgramStore.DepthFunc; +import android.util.Log; + + +public class ModelViewerRS { + + private final int STATE_LAST_FOCUS = 1; + + int mWidth; + int mHeight; + int mRotation; + + public ModelViewerRS() { + } + + public void init(RenderScriptGL rs, Resources res, int width, int height) { + mRS = rs; + mRes = res; + mWidth = width; + mHeight = height; + mRotation = 0; + initRS(); + } + + private Resources mRes; + private RenderScriptGL mRS; + private Sampler mSampler; + private ProgramStore mPSBackground; + private ProgramFragment mPFBackground; + private ProgramVertex mPVBackground; + private ProgramVertex.MatrixAllocation mPVA; + + private Allocation mGridImage; + private Allocation mAllocPV; + + private Mesh mMesh; + + private Font mItalic; + private Allocation mTextAlloc; + + private ScriptC_Modelviewer mScript; + + int mLastX; + int mLastY; + + public void touchEvent(int x, int y) { + int dx = mLastX - x; + if(Math.abs(dx) > 50 || Math.abs(dx) < 3) { + dx = 0; + } + + mRotation -= dx; + if(mRotation > 360) { + mRotation -= 360; + } + if(mRotation < 0) { + mRotation += 360; + } + + mScript.set_gRotate(-(float)mRotation); + + mLastX = x; + mLastY = y; + } + + private void initPFS() { + ProgramStore.Builder b = new ProgramStore.Builder(mRS, null, null); + + b.setDepthFunc(ProgramStore.DepthFunc.LESS); + b.setDitherEnable(false); + b.setDepthMask(true); + mPSBackground = b.create(); + + mScript.set_gPFSBackground(mPSBackground); + } + + private void initPF() { + Sampler.Builder bs = new Sampler.Builder(mRS); + bs.setMin(Sampler.Value.LINEAR); + bs.setMag(Sampler.Value.LINEAR); + bs.setWrapS(Sampler.Value.CLAMP); + bs.setWrapT(Sampler.Value.WRAP); + mSampler = bs.create(); + + ProgramFragment.Builder b = new ProgramFragment.Builder(mRS); + b.setTexture(ProgramFragment.Builder.EnvMode.REPLACE, + ProgramFragment.Builder.Format.RGBA, 0); + mPFBackground = b.create(); + mPFBackground.bindSampler(mSampler, 0); + + mScript.set_gPFBackground(mPFBackground); + } + + private void initPV() { + ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); + mPVBackground = pvb.create(); + + mPVA = new ProgramVertex.MatrixAllocation(mRS); + mPVBackground.bindAllocation(mPVA); + mPVA.setupProjectionNormalized(mWidth, mHeight); + + mScript.set_gPVBackground(mPVBackground); + } + + private void loadImage() { + mGridImage = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.robot, Element.RGB_565(mRS), true); + mGridImage.uploadToTexture(0); + + mScript.set_gTGrid(mGridImage); + } + + private void initTextAllocation() { + String allocString = "Displaying file: R.raw.robot"; + mTextAlloc = Allocation.createFromString(mRS, allocString); + mScript.set_gTextAlloc(mTextAlloc); + } + + private void initRS() { + + mScript = new ScriptC_Modelviewer(mRS, mRes, R.raw.modelviewer, true); + + initPFS(); + initPF(); + initPV(); + + loadImage(); + + FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot); + FileA3D.IndexEntry entry = model.getIndexEntry(0); + if(entry == null || entry.getClassID() != FileA3D.ClassID.MESH) { + Log.e("rs", "could not load model"); + } + else { + mMesh = (Mesh)entry.getObject(); + mScript.set_gTestMesh(mMesh); + } + + mItalic = Font.create(mRS, mRes, "DroidSerif-Italic.ttf", 8); + mScript.set_gItalic(mItalic); + + initTextAllocation(); + + mRS.contextBindRootScript(mScript); + } +} + + + diff --git a/libs/rs/java/Film/src/com/android/film/FilmView.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerView.java index 5bc2811..061cf8e 100644 --- a/libs/rs/java/Film/src/com/android/film/FilmView.java +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerView.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.film; +package com.android.modelviewer; import java.io.Writer; import java.util.ArrayList; @@ -39,15 +39,15 @@ import android.view.SurfaceView; import android.view.KeyEvent; import android.view.MotionEvent; -public class FilmView extends RSSurfaceView { +public class ModelViewerView extends RSSurfaceView { - public FilmView(Context context) { + public ModelViewerView(Context context) { super(context); //setFocusable(true); } private RenderScriptGL mRS; - private FilmRS mRender; + private ModelViewerRS mRender; public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { @@ -55,7 +55,7 @@ public class FilmView extends RSSurfaceView { if (mRS == null) { mRS = createRenderScript(true); mRS.contextSetSurface(w, h, holder.getSurface()); - mRender = new FilmRS(); + mRender = new ModelViewerRS(); mRender.init(mRS, getResources(), w, h); } } @@ -85,7 +85,8 @@ public class FilmView extends RSSurfaceView { if (act == ev.ACTION_UP) { ret = false; } - mRender.setFilmStripPosition((int)ev.getX(), (int)ev.getY() / 5); + + mRender.touchEvent((int)ev.getX(), (int)ev.getY()); return ret; } } diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/modelviewer.rs b/libs/rs/java/ModelViewer/src/com/android/modelviewer/modelviewer.rs new file mode 100644 index 0000000..adb609c --- /dev/null +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/modelviewer.rs @@ -0,0 +1,72 @@ +// Copyright (C) 2009 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. + +#pragma version(1) + +#pragma rs java_package_name(com.android.modelviewer) + +#include "rs_graphics.rsh" + +rs_program_vertex gPVBackground; +rs_program_fragment gPFBackground; + +rs_allocation gTGrid; +rs_mesh gTestMesh; + +rs_program_store gPFSBackground; + +float gRotate; + +rs_font gItalic; +rs_allocation gTextAlloc; + +#pragma rs export_var(gPVBackground, gPFBackground, gTGrid, gTestMesh, gPFSBackground, gRotate, gItalic, gTextAlloc) + +float gDT; +int64_t gLastTime; + +void init() { + gRotate = 0.0f; +} + +int root(int launchID) { + + rsgClearColor(1.0f, 1.0f, 1.0f, 1.0f); + rsgClearDepth(1.0f); + + rsgBindProgramVertex(gPVBackground); + + rsgBindProgramFragment(gPFBackground); + rsgBindProgramStore(gPFSBackground); + rsgBindTexture(gPFBackground, 0, gTGrid); + + rs_matrix4x4 matrix; + rsMatrixLoadIdentity(&matrix); + // Position our model on the screen + rsMatrixTranslate(&matrix, 0.0f, -0.3f, 1.2f); + rsMatrixScale(&matrix, 0.2f, 0.2f, 0.2f); + rsMatrixRotate(&matrix, -25.0f, 1.0f, 0.0f, 0.0f); + rsMatrixRotate(&matrix, gRotate, 0.0f, 1.0f, 0.0f); + rsgProgramVertexLoadModelMatrix(&matrix); + + rsgDrawMesh(gTestMesh); + + rsgFontColor(0.3f, 0.3f, 0.3f, 1.0f); + rsgDrawText("Renderscript model test", 30, 695); + + rsgBindFont(gItalic); + rsgDrawText(gTextAlloc, 30, 730); + + return 10; +} diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index 5ae8d01..571b145 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -1,11 +1,14 @@ +ContextFinish { + handcodeApi + } ContextBindRootScript { param RsScript sampler } -ContextBindProgramFragmentStore { - param RsProgramFragmentStore pgm +ContextBindProgramStore { + param RsProgramStore pgm } ContextBindProgramFragment { @@ -20,6 +23,10 @@ ContextBindProgramRaster { param RsProgramRaster pgm } +ContextBindFont { + param RsFont pgm + } + ContextPause { } @@ -51,6 +58,11 @@ AssignName { param size_t len } +GetName { + param void *obj + param const char **name + } + ObjDestroy { param void *obj } @@ -71,6 +83,19 @@ ElementCreate2 { ret RsElement } +ElementGetNativeData { + param RsElement elem + param uint32_t *elemData + param uint32_t elemDataSize + } + +ElementGetSubElements { + param RsElement elem + param uint32_t *ids + param const char **names + param uint32_t dataSize + } + TypeBegin { param RsElement type } @@ -84,6 +109,12 @@ TypeCreate { ret RsType } +TypeGetNativeData { + param RsType type + param uint32_t * typeData + param uint32_t typeDataSize + } + AllocationCreateTyped { param RsType type ret RsAllocation @@ -224,6 +255,11 @@ Adapter2DSubData { param const void *data } +AllocationGetType { + param RsAllocation va + ret const void* + } + SamplerBegin { } @@ -248,13 +284,6 @@ ScriptBindAllocation { ScriptCBegin { } -ScriptSetClearColor { - param RsScript s - param float r - param float g - param float b - param float a - } ScriptSetTimeZone { param RsScript s @@ -262,39 +291,43 @@ ScriptSetTimeZone { param uint32_t length } -ScriptSetClearDepth { - param RsScript s - param float depth - } -ScriptSetClearStencil { +ScriptInvoke { param RsScript s - param uint32_t stencil + param uint32_t slot } -ScriptSetType { - param RsType type +ScriptInvokeV { + param RsScript s param uint32_t slot - param bool isWritable - param const char * name + param const void * data + param uint32_t dataLen + handcodeApi + togglePlay } -ScriptSetInvoke { - param const char * name +ScriptSetVarI { + param RsScript s param uint32_t slot + param int value } -ScriptInvoke { +ScriptSetVarF { param RsScript s param uint32_t slot + param float value } -ScriptSetRoot { - param bool isRoot +ScriptSetVarV { + param RsScript s + param uint32_t slot + param const void * data + param uint32_t dataLen + handcodeApi + togglePlay } - ScriptCSetScript { param void * codePtr } @@ -308,52 +341,41 @@ ScriptCCreate { ret RsScript } -ScriptCSetDefineF { - param const char* name - param float value - } - -ScriptCSetDefineI32 { - param const char* name - param int32_t value - } -ProgramFragmentStoreBegin { +ProgramStoreBegin { param RsElement in param RsElement out } -ProgramFragmentStoreColorMask { +ProgramStoreColorMask { param bool r param bool g param bool b param bool a } -ProgramFragmentStoreBlendFunc { +ProgramStoreBlendFunc { param RsBlendSrcFunc srcFunc param RsBlendDstFunc destFunc } -ProgramFragmentStoreDepthMask { +ProgramStoreDepthMask { param bool enable } -ProgramFragmentStoreDither { +ProgramStoreDither { param bool enable } -ProgramFragmentStoreDepthFunc { +ProgramStoreDepthFunc { param RsDepthFunc func } -ProgramFragmentStoreCreate { - ret RsProgramFragmentStore +ProgramStoreCreate { + ret RsProgramStore } ProgramRasterCreate { - param RsElement in - param RsElement out param bool pointSmooth param bool lineSmooth param bool pointSprite @@ -365,12 +387,11 @@ ProgramRasterSetLineWidth { param float lw } -ProgramRasterSetPointSize{ +ProgramRasterSetCullMode { param RsProgramRaster pr - param float ps + param RsCullMode mode } - ProgramBindConstants { param RsProgram vp param uint32_t slot @@ -447,36 +468,91 @@ LightSetColor { param float b } +FileA3DCreateFromAssetStream { + param const void * data + param size_t len + ret RsFile + } + FileOpen { ret RsFile param const char *name param size_t len } +FileA3DGetNumIndexEntries { + param int32_t * numEntries + param RsFile file + } -SimpleMeshCreate { - ret RsSimpleMesh - param RsAllocation prim - param RsAllocation index - param RsAllocation *vtx - param uint32_t vtxCount - param uint32_t primType +FileA3DGetIndexEntries { + param RsFileIndexEntry * fileEntries + param uint32_t numEntries + param RsFile fileA3D } +FileA3DGetEntryByIndex { + param uint32_t index + param RsFile file + ret RsObjectBase + } -SimpleMeshBindIndex { - param RsSimpleMesh mesh - param RsAllocation idx +FontCreateFromFile { + param const char *name + param uint32_t fontSize + param uint32_t dpi + ret RsFont } -SimpleMeshBindPrimitive { - param RsSimpleMesh mesh - param RsAllocation prim +MeshCreate { + ret RsMesh + param uint32_t vtxCount + param uint32_t idxCount } -SimpleMeshBindVertex { - param RsSimpleMesh mesh +MeshBindIndex { + param RsMesh mesh + param RsAllocation idx + param uint32_t primType + param uint32_t slot + } + +MeshBindVertex { + param RsMesh mesh param RsAllocation vtx param uint32_t slot } +MeshGetVertexBufferCount { + param RsMesh mesh + param int32_t *numVtx + } + +MeshGetIndexCount { + param RsMesh mesh + param int32_t *numIdx + } + +MeshGetVertices { + param RsMesh mv + param RsAllocation *vtxData + param uint32_t vtxDataCount + } + +MeshGetIndices { + param RsMesh mv + param RsAllocation *va + param uint32_t *primType + param uint32_t idxDataCount + } + +AnimationCreate { + param const float *inValues + param const float *outValues + param uint32_t valueCount + param RsAnimationInterpolation interp + param RsAnimationEdge pre + param RsAnimationEdge post + ret RsAnimation + } + diff --git a/libs/rs/rsAdapter.cpp b/libs/rs/rsAdapter.cpp index 0d31fac..b4ec250 100644 --- a/libs/rs/rsAdapter.cpp +++ b/libs/rs/rsAdapter.cpp @@ -15,7 +15,11 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif using namespace android; using namespace android::renderscript; @@ -70,6 +74,16 @@ void Adapter1D::data(const void *data) mAllocation.get()->getType()->getSizeBytes()); } +void Adapter1D::serialize(OStream *stream) const +{ + +} + +Adapter1D *Adapter1D::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + namespace android { namespace renderscript { @@ -185,6 +199,15 @@ void Adapter2D::data(const void *data) mAllocation.get()->getType()->getSizeBytes()); } +void Adapter2D::serialize(OStream *stream) const +{ + +} + +Adapter2D *Adapter2D::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} namespace android { diff --git a/libs/rs/rsAdapter.h b/libs/rs/rsAdapter.h index cb2872e..449e7ad 100644 --- a/libs/rs/rsAdapter.h +++ b/libs/rs/rsAdapter.h @@ -50,6 +50,10 @@ public: void subData(uint32_t xoff, uint32_t count, const void *data); void data(const void *data); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ADAPTER_1D; } + static Adapter1D *createFromStream(Context *rsc, IStream *stream); + protected: ObjectBaseRef<Allocation> mAllocation; uint32_t mY; @@ -82,6 +86,10 @@ public: void data(const void *data); void subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ADAPTER_2D; } + static Adapter2D *createFromStream(Context *rsc, IStream *stream); + protected: ObjectBaseRef<Allocation> mAllocation; uint32_t mZ; diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index 4e8278d..d62fa55 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -13,12 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" #include <GLES/gl.h> #include <GLES2/gl2.h> #include <GLES/glext.h> +#else +#include "rsContextHostStub.h" + +#include <OpenGL/gl.h> +#include <OpenGl/glext.h> +#endif using namespace android; using namespace android::renderscript; @@ -167,9 +173,12 @@ void Allocation::uploadToTexture(const Context *rsc) 0, format, type, ptr); } if (mTextureGenMipmap) { +#ifndef ANDROID_RS_BUILD_FOR_HOST glGenerateMipmap(GL_TEXTURE_2D); +#endif //ANDROID_RS_BUILD_FOR_HOST } + rsc->checkError("Allocation::uploadToTexture"); } void Allocation::deferedUploadToBufferObject(const Context *rsc) @@ -201,6 +210,7 @@ void Allocation::uploadToBufferObject(const Context *rsc) glBindBuffer(GL_ARRAY_BUFFER, mBufferID); glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); + rsc->checkError("Allocation::uploadToBufferObject"); } void Allocation::uploadCheck(const Context *rsc) @@ -224,6 +234,12 @@ void Allocation::data(const void *data, uint32_t sizeBytes) LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); return; } + + if (mType->getElement()->getHasReferences()) { + incRefs(data, sizeBytes / mType->getElement()->getSizeBytes()); + decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes()); + } + memcpy(mPtr, data, size); sendDirty(); mUploadDefered = true; @@ -246,6 +262,12 @@ void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32 mType->dumpLOGV("type info"); return; } + + if (mType->getElement()->getHasReferences()) { + incRefs(data, count); + decRefs(ptr, count); + } + memcpy(ptr, data, size); sendDirty(); mUploadDefered = true; @@ -269,6 +291,10 @@ void Allocation::subData(uint32_t xoff, uint32_t yoff, for (uint32_t line=yoff; line < (yoff+h); line++) { uint8_t * ptr = static_cast<uint8_t *>(mPtr); + if (mType->getElement()->getHasReferences()) { + incRefs(src, w); + decRefs(dst, w); + } memcpy(dst, src, lineSize); src += lineSize; dst += destW * eSize; @@ -284,7 +310,7 @@ void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, void Allocation::addProgramToDirty(const Program *p) { - mToDirtyList.add(p); + mToDirtyList.push(p); } void Allocation::removeProgramToDirty(const Program *p) @@ -316,6 +342,60 @@ void Allocation::dumpLOGV(const char *prefix) const } +void Allocation::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + // First thing we need to serialize is the type object since it will be needed + // to initialize the class + mType->serialize(stream); + + uint32_t dataSize = mType->getSizeBytes(); + // Write how much data we are storing + stream->addU32(dataSize); + // Now write the data + stream->addByteArray(mPtr, dataSize); +} + +Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) +{ + // First make sure we are reading the correct object + RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); + if(classID != RS_A3D_CLASS_ID_ALLOCATION) { + LOGE("allocation loading skipped due to invalid class id\n"); + return NULL; + } + + String8 name; + stream->loadString(&name); + + Type *type = Type::createFromStream(rsc, stream); + if(!type) { + return NULL; + } + type->compute(); + + // Number of bytes we wrote out for this allocation + uint32_t dataSize = stream->loadU32(); + if(dataSize != type->getSizeBytes()) { + LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); + delete type; + return NULL; + } + + Allocation *alloc = new Allocation(rsc, type); + alloc->setName(name.string(), name.size()); + + // Read in all of our allocation data + stream->loadByteArray(alloc->getPtr(), dataSize); + + return alloc; +} + void Allocation::sendDirty() const { for (size_t ct=0; ct < mToDirtyList.size(); ct++) { @@ -323,6 +403,32 @@ void Allocation::sendDirty() const } } +void Allocation::incRefs(const void *ptr, size_t ct) const +{ + const uint8_t *p = static_cast<const uint8_t *>(ptr); + const Element *e = mType->getElement(); + uint32_t stride = e->getSizeBytes(); + + while (ct > 0) { + e->incRefs(p); + ct --; + p += stride; + } +} + +void Allocation::decRefs(const void *ptr, size_t ct) const +{ + const uint8_t *p = static_cast<const uint8_t *>(ptr); + const Element *e = mType->getElement(); + uint32_t stride = e->getSizeBytes(); + + while (ct > 0) { + e->decRefs(p); + ct --; + p += stride; + } +} + ///////////////// // @@ -495,7 +601,7 @@ static ElementConverter_t pickConverter(const Element *dst, const Element *src) if (srcGLType == GL_UNSIGNED_BYTE && srcGLFmt == GL_RGB && dstGLType == GL_UNSIGNED_SHORT_5_6_5 && - dstGLType == GL_RGB) { + dstGLFmt == GL_RGB) { return elementConverter_888_to_565; } @@ -503,15 +609,21 @@ static ElementConverter_t pickConverter(const Element *dst, const Element *src) if (srcGLType == GL_UNSIGNED_BYTE && srcGLFmt == GL_RGBA && dstGLType == GL_UNSIGNED_SHORT_5_6_5 && - dstGLType == GL_RGB) { + dstGLFmt == GL_RGB) { return elementConverter_8888_to_565; } LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); + LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt); + LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt); + src->dumpLOGV("SRC "); + dst->dumpLOGV("DST "); return 0; } +#ifndef ANDROID_RS_BUILD_FOR_HOST + RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, void *bmp, void *callbackData, RsBitmapCallback_t callback) { @@ -613,6 +725,15 @@ void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) a->read(data); } +const void* rsi_AllocationGetType(Context *rsc, RsAllocation va) +{ + Allocation *a = static_cast<Allocation *>(va); + a->getType()->incUserRef(); + + return a->getType(); +} + +#endif //ANDROID_RS_BUILD_FOR_HOST } } diff --git a/libs/rs/rsAllocation.h b/libs/rs/rsAllocation.h index 516f8b7..177d5a4 100644 --- a/libs/rs/rsAllocation.h +++ b/libs/rs/rsAllocation.h @@ -72,9 +72,18 @@ public: void removeProgramToDirty(const Program *); virtual void dumpLOGV(const char *prefix) const; + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } + static Allocation *createFromStream(Context *rsc, IStream *stream); virtual void uploadCheck(const Context *rsc); + bool getIsTexture() const {return mIsTexture;} + bool getIsBufferObject() const {return mIsVertexBuffer;} + + void incRefs(const void *ptr, size_t ct) const; + void decRefs(const void *ptr, size_t ct) const; + protected: void sendDirty() const; diff --git a/libs/rs/rsAnimation.cpp b/libs/rs/rsAnimation.cpp new file mode 100644 index 0000000..6200715 --- /dev/null +++ b/libs/rs/rsAnimation.cpp @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_BUILD_FOR_HOST +#include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsAnimation.h" + + +using namespace android; +using namespace android::renderscript; + +void Animation::serialize(OStream *stream) const +{ + +} + +Animation *Animation::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + +/* +Animation::Animation(Context *rsc) : ObjectBase(rsc) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + + mValuesInput = NULL; + mValuesOutput = NULL; + mValueCount = 0; + mInterpolation = RS_ANIMATION_INTERPOLATION_STEP; + mEdgePre = RS_ANIMATION_EDGE_UNDEFINED; + mEdgePost = RS_ANIMATION_EDGE_UNDEFINED; + mInputMin = 0; + mInputMax = 0; +} + +Animation * Animation::create(Context *rsc, + const float *inValues, const float *outValues, + uint32_t valueCount, RsAnimationInterpolation interp, + RsAnimationEdge pre, RsAnimationEdge post) +{ + if (valueCount < 2) { + rsc->setError(RS_ERROR_BAD_VALUE, "Animations require more than 2 values."); + return NULL; + } + Animation *a = new Animation(rsc); + if (!a) { + rsc->setError(RS_ERROR_OUT_OF_MEMORY); + return NULL; + } + + float *vin = (float *)malloc(valueCount * sizeof(float)); + float *vout = (float *)malloc(valueCount * sizeof(float)); + a->mValuesInput = vin; + a->mValuesOutput = vout; + if (a->mValuesInput == NULL || a->mValuesOutput == NULL) { + delete a; + rsc->setError(RS_ERROR_OUT_OF_MEMORY); + return NULL; + } + + a->mEdgePre = pre; + a->mEdgePost = post; + a->mInterpolation = interp; + a->mValueCount = valueCount; + + memcpy(vin, inValues, valueCount * sizeof(float)); + memcpy(vout, outValues, valueCount * sizeof(float)); + a->mInputMin = inValues[0]; + a->mInputMax = inValues[0]; + + bool needSort = false; + for (uint32_t ct=1; ct < valueCount; ct++) { + if (a->mInputMin > vin[ct]) { + needSort = true; + a->mInputMin = vin[ct]; + } + if (a->mInputMax < vin[ct]) { + a->mInputMax = vin[ct]; + } else { + needSort = true; + } + } + + while (1) { + bool changed = false; + for (uint32_t ct=1; ct < valueCount; ct++) { + if (vin[ct-1] > vin[ct]) { + float t = vin[ct-1]; + vin[ct-1] = vin[ct]; + vin[ct] = t; + t = vout[ct-1]; + vout[ct-1] = vout[ct]; + vout[ct] = t; + changed = true; + } + } + if (!changed) break; + } + + return a; +} +*/ + + +///////////////////////////////////////// +// + +namespace android { +namespace renderscript { + +RsAnimation rsi_AnimationCreate(Context *rsc, + const float *inValues, + const float *outValues, + uint32_t valueCount, + RsAnimationInterpolation interp, + RsAnimationEdge pre, + RsAnimationEdge post) +{ + //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); + Animation *a = NULL;//Animation::create(rsc, inValues, outValues, valueCount, interp, pre, post); + if (a != NULL) { + a->incUserRef(); + } + return (RsAnimation)a; +} + + +} +} + diff --git a/libs/rs/rsAnimation.h b/libs/rs/rsAnimation.h new file mode 100644 index 0000000..340314e --- /dev/null +++ b/libs/rs/rsAnimation.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_ANIMATION_H +#define ANDROID_RS_ANIMATION_H + +#include "rsUtils.h" +#include "rsObjectBase.h" + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + + +class Animation : public ObjectBase +{ +public: + ~Animation(); + + static Animation * create(Context *rsc, + const float *inValues, const float *outValues, + uint32_t valueCount, RsAnimationInterpolation, + RsAnimationEdge pre, RsAnimationEdge post); + + float eval(float) const; + + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ANIMATION; } + static Animation *createFromStream(Context *rsc, IStream *stream); + +protected: + Animation(Context *rsc); + + + + float evalInRange(float) const; + + + + const float *mValuesInput; + const float *mValuesOutput; + uint32_t mValueCount; + RsAnimationInterpolation mInterpolation; + RsAnimationEdge mEdgePre; + RsAnimationEdge mEdgePost; + + // derived + float mInputMin; + float mInputMax; +}; + + + + +} +} +#endif //ANDROID_STRUCTURED_ELEMENT_H + diff --git a/libs/rs/rsComponent.cpp b/libs/rs/rsComponent.cpp index 15a56f7..fbaa75f 100644 --- a/libs/rs/rsComponent.cpp +++ b/libs/rs/rsComponent.cpp @@ -16,7 +16,11 @@ #include "rsComponent.h" +#ifndef ANDROID_RS_BUILD_FOR_HOST #include <GLES/gl.h> +#else +#include <OpenGL/gl.h> +#endif using namespace android; using namespace android::renderscript; @@ -148,11 +152,19 @@ void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize) case RS_TYPE_UNSIGNED_64: mTypeBits = 64; break; + + case RS_TYPE_BOOLEAN: + mTypeBits = 8; + break; } mBits = mTypeBits * mVectorSize; } +bool Component::isReference() const +{ + return (mType >= RS_TYPE_ELEMENT); +} @@ -188,82 +200,6 @@ uint32_t Component::getGLFormat() const return 0; } -static const char * gCTypeStrings[] = { - 0, - 0,//"F16", - "float", - "double", - "char", - "short", - "int", - 0,//"S64", - "char",//U8", - "short",//U16", - "int",//U32", - 0,//"U64", - 0,//"UP_565", - 0,//"UP_5551", - 0,//"UP_4444", - 0,//"ELEMENT", - 0,//"TYPE", - 0,//"ALLOCATION", - 0,//"SAMPLER", - 0,//"SCRIPT", - 0,//"MESH", - 0,//"PROGRAM_FRAGMENT", - 0,//"PROGRAM_VERTEX", - 0,//"PROGRAM_RASTER", - 0,//"PROGRAM_STORE", -}; - -static const char * gCVecTypeStrings[] = { - 0, - 0,//"F16", - "vecF32", - "vecF64", - "vecI8", - "vecI16", - "vecI32", - 0,//"S64", - "vecU8",//U8", - "vecU16",//U16", - "vecU32",//U32", - 0,//"U64", - 0,//"UP_565", - 0,//"UP_5551", - 0,//"UP_4444", - 0,//"ELEMENT", - 0,//"TYPE", - 0,//"ALLOCATION", - 0,//"SAMPLER", - 0,//"SCRIPT", - 0,//"MESH", - 0,//"PROGRAM_FRAGMENT", - 0,//"PROGRAM_VERTEX", - 0,//"PROGRAM_RASTER", - 0,//"PROGRAM_STORE", -}; - -String8 Component::getCType() const -{ - char buf[64]; - if (mVectorSize == 1) { - return String8(gCTypeStrings[mType]); - } - - // Yuck, acc WAR - // Appears to have problems packing chars - if (mVectorSize == 4 && mType == RS_TYPE_UNSIGNED_8) { - return String8("int"); - } - - - String8 s(gCVecTypeStrings[mType]); - sprintf(buf, "_%i_t", mVectorSize); - s.append(buf); - return s; -} - String8 Component::getGLSLType() const { if (mType == RS_TYPE_SIGNED_32) { @@ -298,6 +234,7 @@ static const char * gTypeStrings[] = { "U16", "U32", "U64", + "BOOLEAN", "UP_565", "UP_5551", "UP_4444", @@ -334,4 +271,25 @@ void Component::dumpLOGV(const char *prefix) const prefix, gTypeStrings[mType], gKindStrings[mKind], mVectorSize, mBits); } +void Component::serialize(OStream *stream) const +{ + stream->addU8((uint8_t)mType); + stream->addU8((uint8_t)mKind); + stream->addU8((uint8_t)(mNormalized ? 1 : 0)); + stream->addU32(mVectorSize); +} + +void Component::loadFromStream(IStream *stream) +{ + mType = (RsDataType)stream->loadU8(); + mKind = (RsDataKind)stream->loadU8(); + uint8_t temp = stream->loadU8(); + mNormalized = temp != 0; + mVectorSize = stream->loadU32(); + + set(mType, mKind, mNormalized, mVectorSize); +} + + + diff --git a/libs/rs/rsComponent.h b/libs/rs/rsComponent.h index 71de324..a775051 100644 --- a/libs/rs/rsComponent.h +++ b/libs/rs/rsComponent.h @@ -35,7 +35,6 @@ public: uint32_t getGLType() const; uint32_t getGLFormat() const; - String8 getCType() const; String8 getGLSLType() const; void dumpLOGV(const char *prefix) const; @@ -48,6 +47,12 @@ public: bool getIsSigned() const {return mIsSigned;} uint32_t getBits() const {return mBits;} + // Helpers for reading / writing this class out + void serialize(OStream *stream) const; + void loadFromStream(IStream *stream); + + bool isReference() const; + protected: RsDataType mType; RsDataKind mKind; diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index 596f533..2a94651 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -23,6 +23,7 @@ #include <sys/types.h> #include <sys/resource.h> +#include <sched.h> #include <cutils/properties.h> @@ -127,19 +128,21 @@ void Context::deinitEGL() } -uint32_t Context::runScript(Script *s, uint32_t launchID) +uint32_t Context::runScript(Script *s) { ObjectBaseRef<ProgramFragment> frag(mFragment); ObjectBaseRef<ProgramVertex> vtx(mVertex); - ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore); + ObjectBaseRef<ProgramStore> store(mFragmentStore); ObjectBaseRef<ProgramRaster> raster(mRaster); + ObjectBaseRef<Font> font(mFont); - uint32_t ret = s->run(this, launchID); + uint32_t ret = s->run(this); mFragment.set(frag); mVertex.set(vtx); mFragmentStore.set(store); mRaster.set(raster); + mFont.set(font); return ret; } @@ -153,29 +156,11 @@ void Context::checkError(const char *msg) const uint32_t Context::runRootScript() { - timerSet(RS_TIMER_CLEAR_SWAP); - rsAssert(mRootScript->mEnviroment.mIsRoot); - - eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); - eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); - glViewport(0, 0, mEGL.mWidth, mEGL.mHeight); - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - - glClearColor(mRootScript->mEnviroment.mClearColor[0], - mRootScript->mEnviroment.mClearColor[1], - mRootScript->mEnviroment.mClearColor[2], - mRootScript->mEnviroment.mClearColor[3]); - if (mUseDepth) { - glDepthMask(GL_TRUE); - glClearDepthf(mRootScript->mEnviroment.mClearDepth); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } else { - glClear(GL_COLOR_BUFFER_BIT); - } + glViewport(0, 0, mWidth, mHeight); timerSet(RS_TIMER_SCRIPT); mStateFragmentStore.mLast.clear(); - uint32_t ret = runScript(mRootScript.get(), 0); + uint32_t ret = runScript(mRootScript.get()); checkError("runRootScript"); if (mError != RS_ERROR_NONE) { @@ -274,6 +259,24 @@ static bool getProp(const char *str) return 0 != strcmp(buf, "0"); } +void Context::displayDebugStats() +{ + char buffer[128]; + sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript); + float oldR, oldG, oldB, oldA; + mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); + + float shadowCol = 0.2f; + mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); + mStateFont.renderText(buffer, 5, getHeight() - 5); + + float textCol = 0.9f; + mStateFont.setFontColor(textCol, textCol, textCol, 1.0f); + mStateFont.renderText(buffer, 4, getHeight() - 6); + + mStateFont.setFontColor(oldR, oldG, oldB, oldA); +} + void * Context::threadProc(void *vrsc) { Context *rsc = static_cast<Context *>(vrsc); @@ -286,6 +289,7 @@ void * Context::threadProc(void *vrsc) rsc->props.mLogScripts = getProp("debug.rs.script"); rsc->props.mLogObjects = getProp("debug.rs.object"); rsc->props.mLogShaders = getProp("debug.rs.shader"); + rsc->props.mLogVisual = getProp("debug.rs.visual"); ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; if (!tlsStruct) { @@ -300,14 +304,16 @@ void * Context::threadProc(void *vrsc) } if (rsc->mIsGraphicsContext) { - rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); + rsc->mStateRaster.init(rsc); rsc->setRaster(NULL); - rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); + rsc->mStateVertex.init(rsc); rsc->setVertex(NULL); - rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); + rsc->mStateFragment.init(rsc); rsc->setFragment(NULL); - rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); + rsc->mStateFragmentStore.init(rsc); rsc->setFragmentStore(NULL); + rsc->mStateFont.init(rsc); + rsc->setFont(NULL); rsc->mStateVertexArray.init(rsc); } @@ -321,6 +327,11 @@ void * Context::threadProc(void *vrsc) uint32_t targetTime = 0; if (mDraw && rsc->mIsGraphicsContext) { targetTime = rsc->runRootScript(); + + if(rsc->props.mLogVisual) { + rsc->displayDebugStats(); + } + mDraw = targetTime && !rsc->mPaused; rsc->timerSet(RS_TIMER_CLEAR_SWAP); eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); @@ -346,11 +357,13 @@ void * Context::threadProc(void *vrsc) rsc->mFragment.clear(); rsc->mVertex.clear(); rsc->mFragmentStore.clear(); + rsc->mFont.clear(); rsc->mRootScript.clear(); rsc->mStateRaster.deinit(rsc); rsc->mStateVertex.deinit(rsc); rsc->mStateFragment.deinit(rsc); rsc->mStateFragmentStore.deinit(rsc); + rsc->mStateFont.deinit(rsc); } ObjectBase::zeroAllUserRef(rsc); @@ -367,6 +380,49 @@ void * Context::threadProc(void *vrsc) return NULL; } +void * Context::helperThreadProc(void *vrsc) +{ + Context *rsc = static_cast<Context *>(vrsc); + uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount); + + LOGV("RS helperThread starting %p idx=%i", rsc, idx); + + rsc->mWorkers.mLaunchSignals[idx].init(); + rsc->mWorkers.mNativeThreadId[idx] = gettid(); + + //cpu_set_t cpset[16]; + //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset); + //LOGE("ret = %i", ret); + +//sched_setaffinity + + setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority); + while(rsc->mRunning) { + rsc->mWorkers.mLaunchSignals[idx].wait(); + if (rsc->mWorkers.mLaunchCallback) { + rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx); + } + android_atomic_dec(&rsc->mWorkers.mRunningCount); + rsc->mWorkers.mCompleteSignal.set(); + } + + LOGV("RS helperThread exiting %p idx=%i", rsc, idx); + return NULL; +} + +void Context::launchThreads(WorkerCallback_t cbk, void *data) +{ + mWorkers.mLaunchData = data; + mWorkers.mLaunchCallback = cbk; + mWorkers.mRunningCount = (int)mWorkers.mCount; + for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { + mWorkers.mLaunchSignals[ct].set(); + } + while(mWorkers.mRunningCount) { + mWorkers.mCompleteSignal.wait(); + } +} + void Context::setPriority(int32_t p) { // Note: If we put this in the proper "background" policy @@ -383,7 +439,10 @@ void Context::setPriority(int32_t p) // success; reset the priority as well } #else - setpriority(PRIO_PROCESS, mNativeThreadId, p); + setpriority(PRIO_PROCESS, mNativeThreadId, p); + for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { + setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p); + } #endif } @@ -433,16 +492,36 @@ Context::Context(Device *dev, bool isGraphics, bool useDepth) timerInit(); timerSet(RS_TIMER_INTERNAL); - LOGV("RS Launching thread"); + int cpu = sysconf(_SC_NPROCESSORS_ONLN); + LOGV("RS Launching thread(s), reported CPU count %i", cpu); + if (cpu < 2) cpu = 0; + + mWorkers.mCount = (uint32_t)cpu; + mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t)); + mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t)); + mWorkers.mLaunchSignals = new Signal[mWorkers.mCount]; + mWorkers.mLaunchCallback = NULL; status = pthread_create(&mThreadId, &threadAttr, threadProc, this); if (status) { LOGE("Failed to start rs context thread."); + return; } - while(!mRunning) { usleep(100); } + mWorkers.mRunningCount = 0; + mWorkers.mLaunchCount = 0; + for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { + status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this); + if (status) { + mWorkers.mCount = ct; + LOGE("Created fewer than expected number of RS threads."); + break; + } + } + + pthread_attr_destroy(&threadAttr); } @@ -486,14 +565,14 @@ void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) checkEglError("eglDestroySurface", ret); mEGL.mSurface = NULL; - mEGL.mWidth = 0; - mEGL.mHeight = 0; mWidth = 0; mHeight = 0; } mWndSurface = sur; if (mWndSurface != NULL) { + mWidth = w; + mHeight = h; bool first = false; if (!mEGL.mContext) { first = true; @@ -511,15 +590,7 @@ void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); checkEglError("eglMakeCurrent", ret); - eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); - eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); - mWidth = w; - mHeight = h; - mStateVertex.updateSize(this, w, h); - - if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) { - LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight); - } + mStateVertex.updateSize(this); if (first) { mGL.mVersion = glGetString(GL_VERSION); @@ -583,7 +654,7 @@ void Context::setRootScript(Script *s) mRootScript.set(s); } -void Context::setFragmentStore(ProgramFragmentStore *pfs) +void Context::setFragmentStore(ProgramStore *pfs) { rsAssert(mIsGraphicsContext); if (pfs == NULL) { @@ -623,6 +694,16 @@ void Context::setVertex(ProgramVertex *pv) } } +void Context::setFont(Font *f) +{ + rsAssert(mIsGraphicsContext); + if (f == NULL) { + mFont.set(mStateFont.mDefault); + } else { + mFont.set(f); + } +} + void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { rsAssert(!obj->getName()); @@ -640,32 +721,9 @@ void Context::removeName(ObjectBase *obj) } } -ObjectBase * Context::lookupName(const char *name) const -{ - for(size_t ct=0; ct < mNames.size(); ct++) { - if (!strcmp(name, mNames[ct]->getName())) { - return mNames[ct]; - } - } - return NULL; -} - -void Context::appendNameDefines(String8 *str) const -{ - char buf[256]; - for (size_t ct=0; ct < mNames.size(); ct++) { - str->append("#define NAMED_"); - str->append(mNames[ct]->getName()); - str->append(" "); - sprintf(buf, "%i\n", (int)mNames[ct]); - str->append(buf); - } -} - bool Context::objDestroyOOBInit() { - int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); - if (status) { + if (!mObjDestroy.mMutex.init()) { LOGE("Context::ObjDestroyOOBInit mutex init failure"); return false; } @@ -675,9 +733,8 @@ bool Context::objDestroyOOBInit() void Context::objDestroyOOBRun() { if (mObjDestroy.mNeedToEmpty) { - int status = pthread_mutex_lock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); + if (!mObjDestroy.mMutex.lock()) { + LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); return; } @@ -686,35 +743,25 @@ void Context::objDestroyOOBRun() } mObjDestroy.mDestroyList.clear(); mObjDestroy.mNeedToEmpty = false; - - status = pthread_mutex_unlock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); - } + mObjDestroy.mMutex.unlock(); } } void Context::objDestroyOOBDestroy() { rsAssert(!mObjDestroy.mNeedToEmpty); - pthread_mutex_destroy(&mObjDestroy.mMutex); } void Context::objDestroyAdd(ObjectBase *obj) { - int status = pthread_mutex_lock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); + if (!mObjDestroy.mMutex.lock()) { + LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); return; } mObjDestroy.mNeedToEmpty = true; mObjDestroy.mDestroyList.add(obj); - - status = pthread_mutex_unlock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); - } + mObjDestroy.mMutex.unlock(); } uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait) @@ -751,15 +798,19 @@ bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool w return false; } if (!waitForSpace) { - if (mIO.mToClient.getFreeSpace() < len) { + if (mIO.mToClient.getFreeSpace() <= (len + 8)) { // Not enough room, and not waiting. return false; } } //LOGE("sendMessageToClient 2"); - void *p = mIO.mToClient.reserve(len); - memcpy(p, data, len); - mIO.mToClient.commit(cmdID, len); + if (len > 0) { + void *p = mIO.mToClient.reserve(len); + memcpy(p, data, len); + mIO.mToClient.commit(cmdID, len); + } else { + mIO.mToClient.commit(cmdID, 0); + } //LOGE("sendMessageToClient 3"); return true; } @@ -799,8 +850,7 @@ void Context::dumpDebug() const LOGE("RS Context debug"); LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); - LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext, - mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay); + LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay); LOGE(" GL vendor: %s", mGL.mVendor); LOGE(" GL renderer: %s", mGL.mRenderer); LOGE(" GL Version: %s", mGL.mVersion); @@ -822,6 +872,9 @@ void Context::dumpDebug() const namespace android { namespace renderscript { +void rsi_ContextFinish(Context *rsc) +{ +} void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { @@ -841,9 +894,9 @@ void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) s->bindToContext(&rsc->mStateSampler, slot); } -void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) +void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { - ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); + ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); rsc->setFragmentStore(pfs); } @@ -865,12 +918,24 @@ void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) rsc->setVertex(pv); } +void rsi_ContextBindFont(Context *rsc, RsFont vfont) +{ + Font *font = static_cast<Font *>(vfont); + rsc->setFont(font); +} + void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) { ObjectBase *ob = static_cast<ObjectBase *>(obj); rsc->assignName(ob, name, len); } +void rsi_GetName(Context *rsc, void * obj, const char **name) +{ + ObjectBase *ob = static_cast<ObjectBase *>(obj); + (*name) = ob->getName(); +} + void rsi_ObjDestroy(Context *rsc, void *obj) { ObjectBase *ob = static_cast<ObjectBase *>(obj); diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 709730e..2da3ab5 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -18,12 +18,12 @@ #define ANDROID_RS_CONTEXT_H #include "rsUtils.h" +#include "rsMutex.h" #include "rsThreadIO.h" #include "rsType.h" #include "rsMatrix.h" #include "rsAllocation.h" -#include "rsSimpleMesh.h" #include "rsMesh.h" #include "rsDevice.h" #include "rsScriptC.h" @@ -31,8 +31,9 @@ #include "rsAdapter.h" #include "rsSampler.h" #include "rsLight.h" +#include "rsFont.h" #include "rsProgramFragment.h" -#include "rsProgramFragmentStore.h" +#include "rsProgramStore.h" #include "rsProgramRaster.h" #include "rsProgramVertex.h" #include "rsShaderCache.h" @@ -64,17 +65,19 @@ public: Script * mScript; }; + typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); //StructuredAllocationContext mStateAllocation; ElementState mStateElement; TypeState mStateType; SamplerState mStateSampler; ProgramFragmentState mStateFragment; - ProgramFragmentStoreState mStateFragmentStore; + ProgramStoreState mStateFragmentStore; ProgramRasterState mStateRaster; ProgramVertexState mStateVertex; LightState mStateLight; VertexArrayState mStateVertexArray; + FontState mStateFont; ScriptCState mScriptC; ShaderCache mShaderCache; @@ -84,14 +87,16 @@ public: void setRaster(ProgramRaster *); void setVertex(ProgramVertex *); void setFragment(ProgramFragment *); - void setFragmentStore(ProgramFragmentStore *); + void setFragmentStore(ProgramStore *); + void setFont(Font *); void updateSurface(void *sur); const ProgramFragment * getFragment() {return mFragment.get();} - const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();} + const ProgramStore * getFragmentStore() {return mFragmentStore.get();} const ProgramRaster * getRaster() {return mRaster.get();} const ProgramVertex * getVertex() {return mVertex.get();} + Font * getFont() {return mFont.get();} bool setupCheck(); bool checkDriver() const {return mEGL.mSurface != 0;} @@ -103,12 +108,10 @@ public: void assignName(ObjectBase *obj, const char *name, uint32_t len); void removeName(ObjectBase *obj); - ObjectBase * lookupName(const char *name) const; - void appendNameDefines(String8 *str) const; uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait); bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace); - uint32_t runScript(Script *s, uint32_t launchID); + uint32_t runScript(Script *s); void initToClient(); void deinitToClient(); @@ -119,15 +122,18 @@ public: ProgramVertex * getDefaultProgramVertex() const { return mStateVertex.mDefault.get(); } - ProgramFragmentStore * getDefaultProgramFragmentStore() const { + ProgramStore * getDefaultProgramStore() const { return mStateFragmentStore.mDefault.get(); } ProgramRaster * getDefaultProgramRaster() const { return mStateRaster.mDefault.get(); } + Font* getDefaultFont() const { + return mStateFont.mDefault.get(); + } - uint32_t getWidth() const {return mEGL.mWidth;} - uint32_t getHeight() const {return mEGL.mHeight;} + uint32_t getWidth() const {return mWidth;} + uint32_t getHeight() const {return mHeight;} ThreadIO mIO; @@ -156,17 +162,21 @@ public: bool mLogScripts; bool mLogObjects; bool mLogShaders; + bool mLogVisual; } props; void dumpDebug() const; void checkError(const char *) const; const char * getError(RsError *); - void setError(RsError e, const char *msg); + void setError(RsError e, const char *msg = NULL); mutable const ObjectBase * mObjHead; bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;} + void launchThreads(WorkerCallback_t cbk, void *data); + uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mRunningCount;} + protected: Device *mDev; @@ -177,8 +187,6 @@ protected: EGLConfig mConfig; EGLContext mContext; EGLSurface mSurface; - EGLint mWidth; - EGLint mHeight; EGLDisplay mDisplay; } mEGL; @@ -219,15 +227,29 @@ protected: pthread_t mThreadId; pid_t mNativeThreadId; + struct Workers { + volatile int mRunningCount; + volatile int mLaunchCount; + uint32_t mCount; + pthread_t *mThreadId; + pid_t *mNativeThreadId; + Signal mCompleteSignal; + + Signal *mLaunchSignals; + WorkerCallback_t mLaunchCallback; + void *mLaunchData; + }; + Workers mWorkers; + ObjectBaseRef<Script> mRootScript; ObjectBaseRef<ProgramFragment> mFragment; ObjectBaseRef<ProgramVertex> mVertex; - ObjectBaseRef<ProgramFragmentStore> mFragmentStore; + ObjectBaseRef<ProgramStore> mFragmentStore; ObjectBaseRef<ProgramRaster> mRaster; - + ObjectBaseRef<Font> mFont; struct ObjDestroyOOB { - pthread_mutex_t mMutex; + Mutex mMutex; Vector<ObjectBase *> mDestroyList; bool mNeedToEmpty; }; @@ -236,6 +258,8 @@ protected: void objDestroyOOBRun(); void objDestroyOOBDestroy(); + void displayDebugStats(); + private: Context(); @@ -245,6 +269,7 @@ private: uint32_t runRootScript(); static void * threadProc(void *); + static void * helperThreadProc(void *); ANativeWindow *mWndSurface; diff --git a/libs/rs/rsContextHostStub.h b/libs/rs/rsContextHostStub.h new file mode 100644 index 0000000..c437606 --- /dev/null +++ b/libs/rs/rsContextHostStub.h @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_CONTEXT_HOST_STUB_H +#define ANDROID_RS_CONTEXT_HOST_STUB_H + +#include "rsUtils.h" +//#include "rsMutex.h" + +//#include "rsThreadIO.h" +#include "rsType.h" +#include "rsMatrix.h" +#include "rsAllocation.h" +#include "rsMesh.h" +//#include "rsDevice.h" +#include "rsScriptC.h" +#include "rsAllocation.h" +#include "rsAdapter.h" +#include "rsSampler.h" +#include "rsLight.h" +#include "rsProgramFragment.h" +#include "rsProgramStore.h" +#include "rsProgramRaster.h" +#include "rsProgramVertex.h" +#include "rsShaderCache.h" +#include "rsVertexArray.h" + +//#include "rsgApiStructs.h" +//#include "rsLocklessFifo.h" + +//#include <ui/egl/android_natives.h> + +// --------------------------------------------------------------------------- +namespace android { + +namespace renderscript { + +class Device; + +class Context +{ +public: + Context(Device *, bool isGraphics, bool useDepth) { + mObjHead = NULL; + } + ~Context() { + } + + + //StructuredAllocationContext mStateAllocation; + ElementState mStateElement; + TypeState mStateType; + SamplerState mStateSampler; + //ProgramFragmentState mStateFragment; + ProgramStoreState mStateFragmentStore; + //ProgramRasterState mStateRaster; + //ProgramVertexState mStateVertex; + LightState mStateLight; + VertexArrayState mStateVertexArray; + + //ScriptCState mScriptC; + ShaderCache mShaderCache; + + + //bool setupCheck(); + bool checkDriver() const {return false;} + + ProgramFragment * getDefaultProgramFragment() const { + return NULL; + } + ProgramVertex * getDefaultProgramVertex() const { + return NULL; + } + ProgramStore * getDefaultProgramStore() const { + return NULL; + } + ProgramRaster * getDefaultProgramRaster() const { + return NULL; + } + + uint32_t getWidth() const {return 0;} + uint32_t getHeight() const {return 0;} + + // Timers + enum Timers { + RS_TIMER_IDLE, + RS_TIMER_INTERNAL, + RS_TIMER_SCRIPT, + RS_TIMER_CLEAR_SWAP, + _RS_TIMER_TOTAL + }; + + bool checkVersion1_1() const {return false; } + bool checkVersion2_0() const {return false; } + + struct { + bool mLogTimes; + bool mLogScripts; + bool mLogObjects; + bool mLogShaders; + } props; + + void dumpDebug() const { } + void checkError(const char *) const { }; + void setError(RsError e, const char *msg = NULL) { } + + mutable const ObjectBase * mObjHead; + + bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;} + +protected: + + struct { + const uint8_t * mVendor; + const uint8_t * mRenderer; + const uint8_t * mVersion; + const uint8_t * mExtensions; + + uint32_t mMajorVersion; + uint32_t mMinorVersion; + + int32_t mMaxVaryingVectors; + int32_t mMaxTextureImageUnits; + + int32_t mMaxFragmentTextureImageUnits; + int32_t mMaxFragmentUniformVectors; + + int32_t mMaxVertexAttribs; + int32_t mMaxVertexUniformVectors; + int32_t mMaxVertexTextureUnits; + + bool OES_texture_npot; + } mGL; + +}; + +} +} +#endif diff --git a/libs/rs/rsDevice.cpp b/libs/rs/rsDevice.cpp index b670ad4..a96b114 100644 --- a/libs/rs/rsDevice.cpp +++ b/libs/rs/rsDevice.cpp @@ -15,7 +15,11 @@ */ #include "rsDevice.h" +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif using namespace android; using namespace android::renderscript; @@ -33,7 +37,7 @@ Device::~Device() void Device::addContext(Context *rsc) { - mContexts.add(rsc); + mContexts.push(rsc); } void Device::removeContext(Context *rsc) diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp index 6288bc4..05902f9 100644 --- a/libs/rs/rsElement.cpp +++ b/libs/rs/rsElement.cpp @@ -14,9 +14,14 @@ * limitations under the License. */ -#include "rsContext.h" +#ifndef ANDROID_RS_BUILD_FOR_HOST +#include "rsContext.h" #include <GLES/gl.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#endif using namespace android; using namespace android::renderscript; @@ -29,6 +34,7 @@ Element::Element(Context *rsc) : ObjectBase(rsc) mAllocLine = __LINE__; mFields = NULL; mFieldCount = 0; + mHasReference = false; } @@ -48,6 +54,7 @@ void Element::clear() delete [] mFields; mFields = NULL; mFieldCount = 0; + mHasReference = false; } size_t Element::getSizeBits() const @@ -63,15 +70,6 @@ size_t Element::getSizeBits() const return total; } -size_t Element::getFieldOffsetBits(uint32_t componentNumber) const -{ - size_t offset = 0; - for (uint32_t ct = 0; ct < componentNumber; ct++) { - offset += mFields[ct].e->mBits; - } - return offset; -} - void Element::dumpLOGV(const char *prefix) const { ObjectBase::dumpLOGV(prefix); @@ -83,6 +81,93 @@ void Element::dumpLOGV(const char *prefix) const } } +void Element::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + mComponent.serialize(stream); + + // Now serialize all the fields + stream->addU32(mFieldCount); + for(uint32_t ct = 0; ct < mFieldCount; ct++) { + stream->addString(&mFields[ct].name); + mFields[ct].e->serialize(stream); + } +} + +Element *Element::createFromStream(Context *rsc, IStream *stream) +{ + // First make sure we are reading the correct object + RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); + if(classID != RS_A3D_CLASS_ID_ELEMENT) { + LOGE("element loading skipped due to invalid class id\n"); + return NULL; + } + + String8 name; + stream->loadString(&name); + + Element *elem = new Element(rsc); + elem->mComponent.loadFromStream(stream); + elem->mBits = elem->mComponent.getBits(); + + elem->mFieldCount = stream->loadU32(); + if(elem->mFieldCount) { + uint32_t offset = 0; + elem->mFields = new ElementField_t [elem->mFieldCount]; + for(uint32_t ct = 0; ct < elem->mFieldCount; ct ++) { + stream->loadString(&elem->mFields[ct].name); + Element *fieldElem = Element::createFromStream(rsc, stream); + elem->mFields[ct].e.set(fieldElem); + elem->mFields[ct].offsetBits = offset; + offset += fieldElem->getSizeBits(); + } + } + + // We need to check if this already exists + for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { + Element *ee = rsc->mStateElement.mElements[ct]; + + if (!ee->getFieldCount() ) { + + if((ee->getComponent().getType() == elem->getComponent().getType()) && + (ee->getComponent().getKind() == elem->getComponent().getKind()) && + (ee->getComponent().getIsNormalized() == elem->getComponent().getIsNormalized()) && + (ee->getComponent().getVectorSize() == elem->getComponent().getVectorSize())) { + // Match + delete elem; + ee->incUserRef(); + return ee; + } + + } else if (ee->getFieldCount() == elem->mFieldCount) { + + bool match = true; + for (uint32_t i=0; i < elem->mFieldCount; i++) { + if ((ee->mFields[i].e.get() != elem->mFields[i].e.get()) || + (ee->mFields[i].name.length() != elem->mFields[i].name.length()) || + (ee->mFields[i].name != elem->mFields[i].name)) { + match = false; + break; + } + } + if (match) { + delete elem; + ee->incUserRef(); + return ee; + } + + } + } + + rsc->mStateElement.mElements.push(elem); + return elem; +} + const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk, bool isNorm, uint32_t vecSize) @@ -104,6 +189,7 @@ const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk, Element *e = new Element(rsc); e->mComponent.set(dt, dk, isNorm, vecSize); e->mBits = e->mComponent.getBits(); + e->mHasReference = e->mComponent.isReference(); rsc->mStateElement.mElements.push(e); return e; } @@ -134,37 +220,23 @@ const Element * Element::create(Context *rsc, size_t count, const Element **ein, Element *e = new Element(rsc); e->mFields = new ElementField_t [count]; e->mFieldCount = count; + size_t bits = 0; for (size_t ct=0; ct < count; ct++) { e->mFields[ct].e.set(ein[ct]); e->mFields[ct].name.setTo(nin[ct], lengths[ct]); + e->mFields[ct].offsetBits = bits; + bits += ein[ct]->getSizeBits(); + + if (ein[ct]->mHasReference) { + e->mHasReference = true; + } } rsc->mStateElement.mElements.push(e); return e; } -String8 Element::getCStructBody(uint32_t indent) const -{ - String8 si; - for (uint32_t ct=0; ct < indent; ct++) { - si.append(" "); - } - - String8 s(si); - s.append("{\n"); - for (uint32_t ct = 0; ct < mFieldCount; ct++) { - s.append(si); - s.append(mFields[ct].e->getCType(indent+4)); - s.append(" "); - s.append(mFields[ct].name); - s.append(";\n"); - } - s.append(si); - s.append("}"); - return s; -} - -String8 Element::getCType(uint32_t indent) const +String8 Element::getGLSLType(uint32_t indent) const { String8 s; for (uint32_t ct=0; ct < indent; ct++) { @@ -173,36 +245,55 @@ String8 Element::getCType(uint32_t indent) const if (!mFieldCount) { // Basic component. - s.append(mComponent.getCType()); + s.append(mComponent.getGLSLType()); } else { - s.append("struct "); - s.append(getCStructBody(indent)); + rsAssert(0); + //s.append("struct "); + //s.append(getCStructBody(indent)); } return s; } -String8 Element::getGLSLType(uint32_t indent) const +void Element::incRefs(const void *ptr) const { - String8 s; - for (uint32_t ct=0; ct < indent; ct++) { - s.append(" "); + if (!mFieldCount) { + if (mComponent.isReference()) { + ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); + ObjectBase *ob = obp[0]; + ob->incSysRef(); + } + return; } + const uint8_t *p = static_cast<const uint8_t *>(ptr); + for (uint32_t i=0; i < mFieldCount; i++) { + if (mFields[i].e->mHasReference) { + mFields[i].e->incRefs(&p[mFields[i].offsetBits >> 3]); + } + } +} + +void Element::decRefs(const void *ptr) const +{ if (!mFieldCount) { - // Basic component. - s.append(mComponent.getGLSLType()); - } else { - rsAssert(0); - //s.append("struct "); - //s.append(getCStructBody(indent)); + if (mComponent.isReference()) { + ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); + ObjectBase *ob = obp[0]; + ob->decSysRef(); + } + return; } - return s; + const uint8_t *p = static_cast<const uint8_t *>(ptr); + for (uint32_t i=0; i < mFieldCount; i++) { + if (mFields[i].e->mHasReference) { + mFields[i].e->decRefs(&p[mFields[i].offsetBits >> 3]); + } + } } - ElementState::ElementState() { } @@ -243,6 +334,32 @@ RsElement rsi_ElementCreate2(Context *rsc, return (RsElement)e; } +void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize) +{ + rsAssert(elemDataSize == 5); + // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements + Element *e = static_cast<Element *>(elem); + + (*elemData++) = (uint32_t)e->getType(); + (*elemData++) = (uint32_t)e->getKind(); + (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0; + (*elemData++) = e->getComponent().getVectorSize(); + (*elemData++) = e->getFieldCount(); + +} + +void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize) +{ + Element *e = static_cast<Element *>(elem); + rsAssert(e->getFieldCount() == dataSize); + + for(uint32_t i = 0; i < dataSize; i ++) { + ids[i] = (uint32_t)e->getField(i); + names[i] = e->getFieldName(i); + } + +} + } } diff --git a/libs/rs/rsElement.h b/libs/rs/rsElement.h index 02a1ca2..b5dad7a 100644 --- a/libs/rs/rsElement.h +++ b/libs/rs/rsElement.h @@ -40,9 +40,11 @@ public: return (getSizeBits() + 7) >> 3; } - size_t getFieldOffsetBits(uint32_t componentNumber) const; + size_t getFieldOffsetBits(uint32_t componentNumber) const { + return mFields[componentNumber].offsetBits; + } size_t getFieldOffsetBytes(uint32_t componentNumber) const { - return (getFieldOffsetBits(componentNumber) + 7) >> 3; + return mFields[componentNumber].offsetBits >> 3; } uint32_t getFieldCount() const {return mFieldCount;} @@ -54,17 +56,22 @@ public: RsDataKind getKind() const {return mComponent.getKind();} uint32_t getBits() const {return mBits;} - String8 getCType(uint32_t indent=0) const; - String8 getCStructBody(uint32_t indent=0) const; String8 getGLSLType(uint32_t indent=0) const; void dumpLOGV(const char *prefix) const; + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; } + static Element *createFromStream(Context *rsc, IStream *stream); static const Element * create(Context *rsc, RsDataType dt, RsDataKind dk, bool isNorm, uint32_t vecSize); static const Element * create(Context *rsc, size_t count, const Element **, const char **, const size_t * lengths); + void incRefs(const void *) const; + void decRefs(const void *) const; + bool getHasReferences() const {return mHasReference;} + protected: // deallocate any components that are part of this element. void clear(); @@ -72,9 +79,11 @@ protected: typedef struct { String8 name; ObjectBaseRef<const Element> e; + uint32_t offsetBits; } ElementField_t; ElementField_t *mFields; size_t mFieldCount; + bool mHasReference; Element(Context *); @@ -90,7 +99,7 @@ public: ~ElementState(); // Cache of all existing elements. - Vector<const Element *> mElements; + Vector<Element *> mElements; }; diff --git a/libs/rs/rsFileA3D.cpp b/libs/rs/rsFileA3D.cpp index e3272c5..5709f2a 100644 --- a/libs/rs/rsFileA3D.cpp +++ b/libs/rs/rsFileA3D.cpp @@ -15,29 +15,156 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif - -#include <utils/String8.h> #include "rsFileA3D.h" #include "rsMesh.h" +#include "rsAnimation.h" + using namespace android; using namespace android::renderscript; +FileA3D::FileA3D(Context *rsc) : ObjectBase(rsc) +{ + mAlloc = NULL; + mData = NULL; + mWriteStream = NULL; + mReadStream = NULL; + + mMajorVersion = 0; + mMinorVersion = 1; + mDataSize = 0; +} +FileA3D::~FileA3D() +{ + for(size_t i = 0; i < mIndex.size(); i ++) { + delete mIndex[i]; + } + for(size_t i = 0; i < mWriteIndex.size(); i ++) { + delete mWriteIndex[i]; + } + if(mWriteStream) { + delete mWriteStream; + } + if(mReadStream) { + delete mWriteStream; + } + if(mAlloc) { + free(mAlloc); + } +} -FileA3D::FileA3D() +void FileA3D::parseHeader(IStream *headerStream) { - mRsc = NULL; + mMajorVersion = headerStream->loadU32(); + mMinorVersion = headerStream->loadU32(); + uint32_t flags = headerStream->loadU32(); + mUse64BitOffsets = (flags & 1) != 0; + + LOGE("file open 64bit = %i", mUse64BitOffsets); + + uint32_t numIndexEntries = headerStream->loadU32(); + for(uint32_t i = 0; i < numIndexEntries; i ++) { + A3DIndexEntry *entry = new A3DIndexEntry(); + headerStream->loadString(&entry->mObjectName); + LOGE("Header data, entry name = %s", entry->mObjectName.string()); + entry->mType = (RsA3DClassID)headerStream->loadU32(); + if(mUse64BitOffsets){ + entry->mOffset = headerStream->loadOffset(); + entry->mLength = headerStream->loadOffset(); + } + else { + entry->mOffset = headerStream->loadU32(); + entry->mLength = headerStream->loadU32(); + } + entry->mRsObj = NULL; + mIndex.push(entry); + } } -FileA3D::~FileA3D() +bool FileA3D::load(const void *data, size_t length) { + LOGE("Loading data. Size: %u", length); + const uint8_t *localData = (const uint8_t *)data; + + size_t lengthRemaining = length; + size_t magicStrLen = 12; + if ((length < magicStrLen) || + memcmp(data, "Android3D_ff", magicStrLen)) { + return false; + } + + localData += magicStrLen; + lengthRemaining -= magicStrLen; + + // Next we get our header size + uint64_t headerSize = 0; + if(lengthRemaining < sizeof(headerSize)) { + return false; + } + + memcpy(&headerSize, localData, sizeof(headerSize)); + localData += sizeof(headerSize); + lengthRemaining -= sizeof(headerSize); + + LOGE("Loading data, headerSize = %lli", headerSize); + + if(lengthRemaining < headerSize) { + return false; + } + + uint8_t *headerData = (uint8_t *)malloc(headerSize); + if(!headerData) { + return false; + } + + memcpy(headerData, localData, headerSize); + + // Now open the stream to parse the header + IStream headerStream(headerData, false); + parseHeader(&headerStream); + + free(headerData); + + localData += headerSize; + lengthRemaining -= headerSize; + + if(lengthRemaining < sizeof(mDataSize)) { + return false; + } + + // Read the size of the data + memcpy(&mDataSize, localData, sizeof(mDataSize)); + localData += sizeof(mDataSize); + lengthRemaining -= sizeof(mDataSize); + + LOGE("Loading data, mDataSize = %lli", mDataSize); + + if(lengthRemaining < mDataSize) { + return false; + } + + // We should know enough to read the file in at this point. + mAlloc = malloc(mDataSize); + if (!mAlloc) { + return false; + } + mData = (uint8_t *)mAlloc; + memcpy(mAlloc, localData, mDataSize); + + mReadStream = new IStream(mData, mUse64BitOffsets); + + return true; } -bool FileA3D::load(Context *rsc, FILE *f) +bool FileA3D::load(FILE *f) { char magicString[12]; size_t len; @@ -49,47 +176,39 @@ bool FileA3D::load(Context *rsc, FILE *f) return false; } - LOGE("file open 2"); - len = fread(&mMajorVersion, 1, sizeof(mMajorVersion), f); - if (len != sizeof(mMajorVersion)) { + // Next thing is the size of the header + uint64_t headerSize = 0; + len = fread(&headerSize, 1, sizeof(headerSize), f); + if (len != sizeof(headerSize) || headerSize == 0) { return false; } - LOGE("file open 3"); - len = fread(&mMinorVersion, 1, sizeof(mMinorVersion), f); - if (len != sizeof(mMinorVersion)) { + uint8_t *headerData = (uint8_t *)malloc(headerSize); + if(!headerData) { return false; } - LOGE("file open 4"); - uint32_t flags; - len = fread(&flags, 1, sizeof(flags), f); - if (len != sizeof(flags)) { + len = fread(headerData, 1, headerSize, f); + if (len != headerSize) { return false; } - mUse64BitOffsets = (flags & 1) != 0; - LOGE("file open 64bit = %i", mUse64BitOffsets); + // Now open the stream to parse the header + IStream headerStream(headerData, false); + parseHeader(&headerStream); - if (mUse64BitOffsets) { - len = fread(&mDataSize, 1, sizeof(mDataSize), f); - if (len != sizeof(mDataSize)) { - return false; - } - } else { - uint32_t tmp; - len = fread(&tmp, 1, sizeof(tmp), f); - if (len != sizeof(tmp)) { - return false; - } - mDataSize = tmp; + free(headerData); + + // Next thing is the size of the header + len = fread(&mDataSize, 1, sizeof(mDataSize), f); + if (len != sizeof(mDataSize) || mDataSize == 0) { + return false; } LOGE("file open size = %lli", mDataSize); // We should know enough to read the file in at this point. - fseek(f, SEEK_SET, 0); - mAlloc= malloc(mDataSize); + mAlloc = malloc(mDataSize); if (!mAlloc) { return false; } @@ -99,282 +218,255 @@ bool FileA3D::load(Context *rsc, FILE *f) return false; } - LOGE("file start processing"); - return process(rsc); + mReadStream = new IStream(mData, mUse64BitOffsets); + + LOGE("Header is read an stream initialized"); + return true; } -bool FileA3D::processIndex(Context *rsc, A3DIndexEntry *ie) -{ - bool ret = false; - IO io(mData + ie->mOffset, mUse64BitOffsets); - - LOGE("process index, type %i", ie->mType); - - switch(ie->mType) { - case CHUNK_ELEMENT: - processChunk_Element(rsc, &io, ie); - break; - case CHUNK_ELEMENT_SOURCE: - processChunk_ElementSource(rsc, &io, ie); - break; - case CHUNK_VERTICIES: - processChunk_Verticies(rsc, &io, ie); - break; - case CHUNK_MESH: - processChunk_Mesh(rsc, &io, ie); - break; - case CHUNK_PRIMITIVE: - processChunk_Primitive(rsc, &io, ie); - break; - default: - LOGE("FileA3D Unknown chunk type"); - break; - } - return (ie->mRsObj != NULL); +size_t FileA3D::getNumIndexEntries() const { + return mIndex.size(); } -bool FileA3D::process(Context *rsc) -{ - LOGE("process"); - IO io(mData + 12, mUse64BitOffsets); - bool ret = true; - - // Build the index first - LOGE("process 1"); - io.loadU32(); // major version, already loaded - io.loadU32(); // minor version, already loaded - LOGE("process 2"); - - io.loadU32(); // flags - io.loadOffset(); // filesize, already loaded. - LOGE("process 4"); - uint64_t mIndexOffset = io.loadOffset(); - uint64_t mStringOffset = io.loadOffset(); - - LOGE("process mIndexOffset= 0x%016llx", mIndexOffset); - LOGE("process mStringOffset= 0x%016llx", mStringOffset); - - IO index(mData + mIndexOffset, mUse64BitOffsets); - IO stringTable(mData + mStringOffset, mUse64BitOffsets); - - uint32_t stringEntryCount = stringTable.loadU32(); - LOGE("stringEntryCount %i", stringEntryCount); - mStrings.setCapacity(stringEntryCount); - mStringIndexValues.setCapacity(stringEntryCount); - if (stringEntryCount) { - uint32_t stringType = stringTable.loadU32(); - LOGE("stringType %i", stringType); - rsAssert(stringType==0); - for (uint32_t ct = 0; ct < stringEntryCount; ct++) { - uint64_t offset = stringTable.loadOffset(); - LOGE("string offset 0x%016llx", offset); - IO tmp(mData + offset, mUse64BitOffsets); - String8 s; - tmp.loadString(&s); - LOGE("string %s", s.string()); - mStrings.push(s); - } +const FileA3D::A3DIndexEntry *FileA3D::getIndexEntry(size_t index) const { + if(index < mIndex.size()) { + return mIndex[index]; } + return NULL; +} - LOGE("strings done"); - uint32_t indexEntryCount = index.loadU32(); - LOGE("index count %i", indexEntryCount); - mIndex.setCapacity(indexEntryCount); - for (uint32_t ct = 0; ct < indexEntryCount; ct++) { - A3DIndexEntry e; - uint32_t stringIndex = index.loadU32(); - LOGE("index %i", ct); - LOGE(" string index %i", stringIndex); - e.mType = (A3DChunkType)index.loadU32(); - LOGE(" type %i", e.mType); - e.mOffset = index.loadOffset(); - LOGE(" offset 0x%016llx", e.mOffset); - - if (stringIndex && (stringIndex < mStrings.size())) { - e.mID = mStrings[stringIndex]; - mStringIndexValues.editItemAt(stringIndex) = ct; - LOGE(" id %s", e.mID.string()); - } +ObjectBase *FileA3D::initializeFromEntry(size_t index) { + if(index >= mIndex.size()) { + return NULL; + } - mIndex.push(e); + FileA3D::A3DIndexEntry *entry = mIndex[index]; + if(!entry) { + return NULL; } - LOGE("index done"); - // At this point the index should be fully populated. - // We can now walk though it and load all the objects. - for (uint32_t ct = 0; ct < indexEntryCount; ct++) { - LOGE("processing index entry %i", ct); - processIndex(rsc, &mIndex.editItemAt(ct)); + if(entry->mRsObj) { + entry->mRsObj->incUserRef(); + return entry->mRsObj; } - return ret; + // Seek to the beginning of object + mReadStream->reset(entry->mOffset); + switch (entry->mType) { + case RS_A3D_CLASS_ID_UNKNOWN: + return NULL; + case RS_A3D_CLASS_ID_MESH: + entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_TYPE: + entry->mRsObj = Type::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_ELEMENT: + entry->mRsObj = Element::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_ALLOCATION: + entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_PROGRAM_VERTEX: + entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_PROGRAM_RASTER: + entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT: + entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_PROGRAM_STORE: + entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_SAMPLER: + entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_ANIMATION: + entry->mRsObj = Animation::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_LIGHT: + entry->mRsObj = Light::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_ADAPTER_1D: + entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_ADAPTER_2D: + entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream); + break; + case RS_A3D_CLASS_ID_SCRIPT_C: + return NULL; + } + if(entry->mRsObj) { + entry->mRsObj->incUserRef(); + } + return entry->mRsObj; } - -FileA3D::IO::IO(const uint8_t *buf, bool use64) +bool FileA3D::writeFile(const char *filename) { - mData = buf; - mPos = 0; - mUse64 = use64; -} + if(!mWriteStream) { + LOGE("No objects to write\n"); + return false; + } + if(mWriteStream->getPos() == 0) { + LOGE("No objects to write\n"); + return false; + } -uint64_t FileA3D::IO::loadOffset() -{ - uint64_t tmp; - if (mUse64) { - mPos = (mPos + 7) & (~7); - tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0]; - mPos += sizeof(uint64_t); - return tmp; - } - return loadU32(); -} + FILE *writeHandle = fopen(filename, "wb"); + if(!writeHandle) { + LOGE("Couldn't open the file for writing\n"); + return false; + } -void FileA3D::IO::loadString(String8 *s) -{ - LOGE("loadString"); - uint32_t len = loadU32(); - LOGE("loadString len %i", len); - s->setTo((const char *)&mData[mPos], len); - mPos += len; -} + // Open a new stream to make writing the header easier + OStream headerStream(5*1024, false); + headerStream.addU32(mMajorVersion); + headerStream.addU32(mMinorVersion); + uint32_t is64Bit = 0; + headerStream.addU32(is64Bit); + + uint32_t writeIndexSize = mWriteIndex.size(); + headerStream.addU32(writeIndexSize); + for(uint32_t i = 0; i < writeIndexSize; i ++) { + headerStream.addString(&mWriteIndex[i]->mObjectName); + headerStream.addU32((uint32_t)mWriteIndex[i]->mType); + if(mUse64BitOffsets){ + headerStream.addOffset(mWriteIndex[i]->mOffset); + headerStream.addOffset(mWriteIndex[i]->mLength); + } + else { + uint32_t offset = (uint32_t)mWriteIndex[i]->mOffset; + headerStream.addU32(offset); + offset = (uint32_t)mWriteIndex[i]->mLength; + headerStream.addU32(offset); + } + } + // Write our magic string so we know we are reading the right file + String8 magicString(A3D_MAGIC_KEY); + fwrite(magicString.string(), sizeof(char), magicString.size(), writeHandle); -void FileA3D::processChunk_Mesh(Context *rsc, IO *io, A3DIndexEntry *ie) -{ - Mesh * m = new Mesh(rsc); + // Store the size of the header to make it easier to parse when we read it + uint64_t headerSize = headerStream.getPos(); + fwrite(&headerSize, sizeof(headerSize), 1, writeHandle); - m->mPrimitivesCount = io->loadU32(); - m->mPrimitives = new Mesh::Primitive_t *[m->mPrimitivesCount]; + // Now write our header + fwrite(headerStream.getPtr(), sizeof(uint8_t), headerStream.getPos(), writeHandle); - for (uint32_t ct = 0; ct < m->mPrimitivesCount; ct++) { - uint32_t index = io->loadU32(); + // Now write the size of the data part of the file for easier parsing later + uint64_t fileDataSize = mWriteStream->getPos(); + fwrite(&fileDataSize, sizeof(fileDataSize), 1, writeHandle); - m->mPrimitives[ct] = (Mesh::Primitive_t *)mIndex[index].mRsObj; + fwrite(mWriteStream->getPtr(), sizeof(uint8_t), mWriteStream->getPos(), writeHandle); + + int status = fclose(writeHandle); + + if(status != 0) { + LOGE("Couldn't close file\n"); + return false; } - ie->mRsObj = m; + + return true; } -void FileA3D::processChunk_Primitive(Context *rsc, IO *io, A3DIndexEntry *ie) +void FileA3D::appendToFile(ObjectBase *obj) { + if(!obj) { + return; + } + if(!mWriteStream) { + const uint64_t initialStreamSize = 256*1024; + mWriteStream = new OStream(initialStreamSize, false); + } + A3DIndexEntry *indexEntry = new A3DIndexEntry(); + indexEntry->mObjectName.setTo(obj->getName()); + indexEntry->mType = obj->getClassId(); + indexEntry->mOffset = mWriteStream->getPos(); + indexEntry->mRsObj = obj; + mWriteIndex.push(indexEntry); + obj->serialize(mWriteStream); + indexEntry->mLength = mWriteStream->getPos() - indexEntry->mOffset; + mWriteStream->align(4); +} + +namespace android { +namespace renderscript { + +void rsi_FileA3DGetNumIndexEntries(Context *rsc, int32_t *numEntries, RsFile file) { - Mesh::Primitive_t * p = new Mesh::Primitive_t; + FileA3D *fa3d = static_cast<FileA3D *>(file); - p->mIndexCount = io->loadU32(); - uint32_t vertIdx = io->loadU32(); - p->mRestartCounts = io->loadU16(); - uint32_t bits = io->loadU8(); - p->mType = (RsPrimitive)io->loadU8(); + if(fa3d) { + *numEntries = fa3d->getNumIndexEntries(); + } + else { + *numEntries = 0; + } +} - LOGE("processChunk_Primitive count %i, bits %i", p->mIndexCount, bits); +void rsi_FileA3DGetIndexEntries(Context *rsc, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) +{ + FileA3D *fa3d = static_cast<FileA3D *>(file); - p->mVerticies = (Mesh::Verticies_t *)mIndex[vertIdx].mRsObj; + if(!fa3d) { + LOGE("Can't load index entries. No valid file"); + return; + } - p->mIndicies = new uint16_t[p->mIndexCount]; - for (uint32_t ct = 0; ct < p->mIndexCount; ct++) { - switch(bits) { - case 8: - p->mIndicies[ct] = io->loadU8(); - break; - case 16: - p->mIndicies[ct] = io->loadU16(); - break; - case 32: - p->mIndicies[ct] = io->loadU32(); - break; - } - LOGE(" idx %i", p->mIndicies[ct]); - } - - if (p->mRestartCounts) { - p->mRestarts = new uint16_t[p->mRestartCounts]; - for (uint32_t ct = 0; ct < p->mRestartCounts; ct++) { - switch(bits) { - case 8: - p->mRestarts[ct] = io->loadU8(); - break; - case 16: - p->mRestarts[ct] = io->loadU16(); - break; - case 32: - p->mRestarts[ct] = io->loadU32(); - break; - } - LOGE(" idx %i", p->mRestarts[ct]); - } - } else { - p->mRestarts = NULL; + uint32_t numFileEntries = fa3d->getNumIndexEntries(); + if(numFileEntries != numEntries || numEntries == 0 || fileEntries == NULL) { + LOGE("Can't load index entries. Invalid number requested"); + return; } - ie->mRsObj = p; -} + for(uint32_t i = 0; i < numFileEntries; i ++) { + const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i); + fileEntries[i].classID = entry->getType(); + fileEntries[i].objectName = entry->getObjectName().string(); + } -void FileA3D::processChunk_Verticies(Context *rsc, IO *io, A3DIndexEntry *ie) -{ - Mesh::Verticies_t *cv = new Mesh::Verticies_t; - cv->mAllocationCount = io->loadU32(); - cv->mAllocations = new Allocation *[cv->mAllocationCount]; - LOGE("processChunk_Verticies count %i", cv->mAllocationCount); - for (uint32_t ct = 0; ct < cv->mAllocationCount; ct++) { - uint32_t i = io->loadU32(); - cv->mAllocations[ct] = (Allocation *)mIndex[i].mRsObj; - LOGE(" idx %i", i); - } - ie->mRsObj = cv; } -void FileA3D::processChunk_Element(Context *rsc, IO *io, A3DIndexEntry *ie) +RsObjectBase rsi_FileA3DGetEntryByIndex(Context *rsc, uint32_t index, RsFile file) { - /* - rsi_ElementBegin(rsc); - - uint32_t count = io->loadU32(); - LOGE("processChunk_Element count %i", count); - while (count--) { - RsDataKind dk = (RsDataKind)io->loadU8(); - RsDataType dt = (RsDataType)io->loadU8(); - uint32_t bits = io->loadU8(); - bool isNorm = io->loadU8() != 0; - LOGE(" %i %i %i %i", dk, dt, bits, isNorm); - rsi_ElementAdd(rsc, dk, dt, isNorm, bits, 0); - } - LOGE("processChunk_Element create"); - ie->mRsObj = rsi_ElementCreate(rsc); - */ + FileA3D *fa3d = static_cast<FileA3D *>(file); + if(!fa3d) { + LOGE("Can't load entry. No valid file"); + return NULL; + } + + ObjectBase *obj = fa3d->initializeFromEntry(index); + LOGE("Returning object with name %s", obj->getName()); + + return obj; } -void FileA3D::processChunk_ElementSource(Context *rsc, IO *io, A3DIndexEntry *ie) +RsFile rsi_FileA3DCreateFromAssetStream(Context *rsc, const void *data, uint32_t len) { - uint32_t index = io->loadU32(); - uint32_t count = io->loadU32(); - - LOGE("processChunk_ElementSource count %i, index %i", count, index); + if (data == NULL) { + LOGE("File load failed. Asset stream is NULL"); + return NULL; + } - RsElement e = (RsElement)mIndex[index].mRsObj; + FileA3D *fa3d = new FileA3D(rsc); - RsAllocation a = rsi_AllocationCreateSized(rsc, e, count); - Allocation * alloc = static_cast<Allocation *>(a); + fa3d->load(data, len); + fa3d->incUserRef(); - float * data = (float *)alloc->getPtr(); - while(count--) { - *data = io->loadF(); - LOGE(" %f", *data); - data++; - } - ie->mRsObj = alloc; + return fa3d; } -namespace android { -namespace renderscript { - RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len) { - FileA3D *fa3d = new FileA3D; + FileA3D *fa3d = new FileA3D(rsc); FILE *f = fopen("/sdcard/test.a3d", "rb"); if (f) { - fa3d->load(rsc, f); + fa3d->load(f); fclose(f); + fa3d->incUserRef(); return fa3d; } delete fa3d; diff --git a/libs/rs/rsFileA3D.h b/libs/rs/rsFileA3D.h index 9ee08ec..b985907 100644 --- a/libs/rs/rsFileA3D.h +++ b/libs/rs/rsFileA3D.h @@ -18,20 +18,23 @@ #define ANDROID_RS_FILE_A3D_H #include "RenderScript.h" -#include "rsFileA3DDecls.h" #include "rsMesh.h" #include <utils/String8.h> +#include "rsStream.h" #include <stdio.h> +#define A3D_MAGIC_KEY "Android3D_ff" + // --------------------------------------------------------------------------- namespace android { + namespace renderscript { -class FileA3D +class FileA3D : public ObjectBase { public: - FileA3D(); + FileA3D(Context *rsc); ~FileA3D(); uint32_t mMajorVersion; @@ -40,78 +43,53 @@ public: uint64_t mStringTableOffset; bool mUse64BitOffsets; - struct A3DIndexEntry { - String8 mID; - A3DChunkType mType; + class A3DIndexEntry { + String8 mObjectName; + RsA3DClassID mType; uint64_t mOffset; - void * mRsObj; - }; - - bool load(Context *rsc, FILE *f); - -protected: - class IO - { + uint64_t mLength; + ObjectBase *mRsObj; public: - IO(const uint8_t *, bool use64); - - float loadF() { - mPos = (mPos + 3) & (~3); - float tmp = reinterpret_cast<const float *>(&mData[mPos])[0]; - mPos += sizeof(float); - return tmp; - } - int32_t loadI32() { - mPos = (mPos + 3) & (~3); - int32_t tmp = reinterpret_cast<const int32_t *>(&mData[mPos])[0]; - mPos += sizeof(int32_t); - return tmp; + friend class FileA3D; + const String8 &getObjectName() const { + return mObjectName; } - uint32_t loadU32() { - mPos = (mPos + 3) & (~3); - uint32_t tmp = reinterpret_cast<const uint32_t *>(&mData[mPos])[0]; - mPos += sizeof(uint32_t); - return tmp; + RsA3DClassID getType() const { + return mType; } - uint16_t loadU16() { - mPos = (mPos + 1) & (~1); - uint16_t tmp = reinterpret_cast<const uint16_t *>(&mData[mPos])[0]; - mPos += sizeof(uint16_t); - return tmp; - } - uint8_t loadU8() { - uint8_t tmp = reinterpret_cast<const uint8_t *>(&mData[mPos])[0]; - mPos += sizeof(uint8_t); - return tmp; - } - uint64_t loadOffset(); - void loadString(String8 *s); - uint64_t getPos() const {return mPos;} - const uint8_t * getPtr() const; - protected: - const uint8_t * mData; - uint64_t mPos; - bool mUse64; }; + bool load(FILE *f); + bool load(const void *data, size_t length); + + size_t getNumIndexEntries() const; + const A3DIndexEntry* getIndexEntry(size_t index) const; + ObjectBase *initializeFromEntry(size_t index); + + void appendToFile(ObjectBase *obj); + bool writeFile(const char *filename); + + // Currently files do not get serialized, + // but we need to inherit from ObjectBase for ref tracking + virtual void serialize(OStream *stream) const { + } + virtual RsA3DClassID getClassId() const { + return RS_A3D_CLASS_ID_UNKNOWN; + } + +protected: - bool process(Context *rsc); - bool processIndex(Context *rsc, A3DIndexEntry *); - void processChunk_Mesh(Context *rsc, IO *io, A3DIndexEntry *ie); - void processChunk_Primitive(Context *rsc, IO *io, A3DIndexEntry *ie); - void processChunk_Verticies(Context *rsc, IO *io, A3DIndexEntry *ie); - void processChunk_Element(Context *rsc, IO *io, A3DIndexEntry *ie); - void processChunk_ElementSource(Context *rsc, IO *io, A3DIndexEntry *ie); + void parseHeader(IStream *headerStream); const uint8_t * mData; void * mAlloc; uint64_t mDataSize; - Context * mRsc; - Vector<A3DIndexEntry> mIndex; - Vector<String8> mStrings; - Vector<uint32_t> mStringIndexValues; + OStream *mWriteStream; + Vector<A3DIndexEntry*> mWriteIndex; + IStream *mReadStream; + Vector<A3DIndexEntry*> mIndex; }; diff --git a/libs/rs/rsFont.cpp b/libs/rs/rsFont.cpp new file mode 100644 index 0000000..833bee0 --- /dev/null +++ b/libs/rs/rsFont.cpp @@ -0,0 +1,727 @@ + +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_BUILD_FOR_HOST +#include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif + +#include "rsFont.h" +#include "rsProgramFragment.h" +#include FT_BITMAP_H + +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +using namespace android; +using namespace android::renderscript; + +Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + mInitialized = false; + mHasKerning = false; + mFace = NULL; +} + +bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi) +{ + if(mInitialized) { + LOGE("Reinitialization of fonts not supported"); + return false; + } + + String8 fontsDir("/fonts/"); + String8 fullPath(getenv("ANDROID_ROOT")); + fullPath += fontsDir; + fullPath += name; + + FT_Error error = FT_New_Face(mRSC->mStateFont.getLib(), fullPath.string(), 0, &mFace); + if(error) { + LOGE("Unable to initialize font %s", fullPath.string()); + return false; + } + + mFontName = name; + mFontSize = fontSize; + mDpi = dpi; + + error = FT_Set_Char_Size(mFace, fontSize * 64, 0, dpi, 0); + if(error) { + LOGE("Unable to set font size on %s", fullPath.string()); + return false; + } + + mHasKerning = FT_HAS_KERNING(mFace); + + mInitialized = true; + return true; +} + +void Font::invalidateTextureCache() +{ + for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { + mCachedGlyphs.valueAt(i)->mIsValid = false; + } +} + +void Font::drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y) +{ + FontState *state = &mRSC->mStateFont; + + int nPenX = x + glyph->mBitmapLeft; + int nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight; + + state->appendMeshQuad(nPenX, nPenY, 0, + glyph->mBitmapMinU, glyph->mBitmapMaxV, + + nPenX + (int)glyph->mBitmapWidth, nPenY, 0, + glyph->mBitmapMaxU, glyph->mBitmapMaxV, + + nPenX + (int)glyph->mBitmapWidth, nPenY - (int)glyph->mBitmapHeight, 0, + glyph->mBitmapMaxU, glyph->mBitmapMinV, + + nPenX, nPenY - (int)glyph->mBitmapHeight, 0, + glyph->mBitmapMinU, glyph->mBitmapMinV); +} + +void Font::renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyphs, int x, int y) +{ + if(!mInitialized || numGlyphs == 0 || text == NULL || len == 0) { + return; + } + + int penX = x, penY = y; + int glyphsLeft = 1; + if(numGlyphs > 0) { + glyphsLeft = numGlyphs; + } + + size_t index = start; + size_t nextIndex = 0; + + while (glyphsLeft > 0) { + + int32_t utfChar = utf32_at(text, len, index, &nextIndex); + + // Reached the end of the string or encountered + if(utfChar < 0) { + break; + } + + // Move to the next character in the array + index = nextIndex; + + CachedGlyphInfo *cachedGlyph = mCachedGlyphs.valueFor((uint32_t)utfChar); + + if(cachedGlyph == NULL) { + cachedGlyph = cacheGlyph((uint32_t)utfChar); + } + // Is the glyph still in texture cache? + if(!cachedGlyph->mIsValid) { + updateGlyphCache(cachedGlyph); + } + + // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage + if(cachedGlyph->mIsValid) { + drawCachedGlyph(cachedGlyph, penX, penY); + } + + penX += (cachedGlyph->mAdvance.x >> 6); + + // If we were given a specific number of glyphs, decrement + if(numGlyphs > 0) { + glyphsLeft --; + } + } +} + +void Font::updateGlyphCache(CachedGlyphInfo *glyph) +{ + FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); + if(error) { + LOGE("Couldn't load glyph."); + return; + } + + glyph->mAdvance = mFace->glyph->advance; + glyph->mBitmapLeft = mFace->glyph->bitmap_left; + glyph->mBitmapTop = mFace->glyph->bitmap_top; + + FT_Bitmap *bitmap = &mFace->glyph->bitmap; + + // Now copy the bitmap into the cache texture + uint32_t startX = 0; + uint32_t startY = 0; + + // Let the font state figure out where to put the bitmap + FontState *state = &mRSC->mStateFont; + glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY); + + if(!glyph->mIsValid) { + return; + } + + uint32_t endX = startX + bitmap->width; + uint32_t endY = startY + bitmap->rows; + + glyph->mBitmapMinX = startX; + glyph->mBitmapMinY = startY; + glyph->mBitmapWidth = bitmap->width; + glyph->mBitmapHeight = bitmap->rows; + + uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); + uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); + + glyph->mBitmapMinU = (float)startX / (float)cacheWidth; + glyph->mBitmapMinV = (float)startY / (float)cacheHeight; + glyph->mBitmapMaxU = (float)endX / (float)cacheWidth; + glyph->mBitmapMaxV = (float)endY / (float)cacheHeight; +} + +Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph) +{ + CachedGlyphInfo *newGlyph = new CachedGlyphInfo(); + mCachedGlyphs.add(glyph, newGlyph); + + newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); + newGlyph->mIsValid = false; + + updateGlyphCache(newGlyph); + + return newGlyph; +} + +Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi) +{ + Vector<Font*> &activeFonts = rsc->mStateFont.mActiveFonts; + + for(uint32_t i = 0; i < activeFonts.size(); i ++) { + Font *ithFont = activeFonts[i]; + if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { + return ithFont; + } + } + + Font *newFont = new Font(rsc); + bool isInitialized = newFont->init(name, fontSize, dpi); + if(isInitialized) { + activeFonts.push(newFont); + return newFont; + } + + delete newFont; + return NULL; + +} + +Font::~Font() +{ + if(mFace) { + FT_Done_Face(mFace); + } + + for (uint32_t ct = 0; ct < mRSC->mStateFont.mActiveFonts.size(); ct++) { + if (mRSC->mStateFont.mActiveFonts[ct] == this) { + mRSC->mStateFont.mActiveFonts.removeAt(ct); + break; + } + } + + for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { + CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); + delete glyph; + } +} + +FontState::FontState() +{ + mInitialized = false; + mMaxNumberOfQuads = 1024; + mCurrentQuadIndex = 0; + mRSC = NULL; + mLibrary = NULL; + setFontColor(0.1f, 0.1f, 0.1f, 1.0f); +} + +FontState::~FontState() +{ + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + delete mCacheLines[i]; + } + + rsAssert(!mActiveFonts.size()); +} + +FT_Library FontState::getLib() +{ + if(!mLibrary) { + FT_Error error = FT_Init_FreeType(&mLibrary); + if(error) { + LOGE("Unable to initialize freetype"); + return NULL; + } + } + + return mLibrary; +} + +void FontState::init(Context *rsc) +{ + mRSC = rsc; +} + +void FontState::flushAllAndInvalidate() +{ + if(mCurrentQuadIndex != 0) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } + for(uint32_t i = 0; i < mActiveFonts.size(); i ++) { + mActiveFonts[i]->invalidateTextureCache(); + } + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + mCacheLines[i]->mCurrentCol = 0; + } +} + +bool FontState::cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) +{ + // If the glyph is too tall, don't cache it + if((uint32_t)bitmap->rows > mCacheLines[mCacheLines.size()-1]->mMaxHeight) { + LOGE("Font size to large to fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); + return false; + } + + // Now copy the bitmap into the cache texture + uint32_t startX = 0; + uint32_t startY = 0; + + bool bitmapFit = false; + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); + if(bitmapFit) { + break; + } + } + + // If the new glyph didn't fit, flush the state so far and invalidate everything + if(!bitmapFit) { + flushAllAndInvalidate(); + + // Try to fit it again + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY); + if(bitmapFit) { + break; + } + } + + // if we still don't fit, something is wrong and we shouldn't draw + if(!bitmapFit) { + LOGE("Bitmap doesn't fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows); + return false; + } + } + + *retOriginX = startX; + *retOriginY = startY; + + uint32_t endX = startX + bitmap->width; + uint32_t endY = startY + bitmap->rows; + + uint32_t cacheWidth = getCacheTextureType()->getDimX(); + + unsigned char *cacheBuffer = (unsigned char*)mTextTexture->getPtr(); + unsigned char *bitmapBuffer = bitmap->buffer; + + uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0; + for(cacheX = startX, bX = 0; cacheX < endX; cacheX ++, bX ++) { + for(cacheY = startY, bY = 0; cacheY < endY; cacheY ++, bY ++) { + unsigned char tempCol = bitmapBuffer[bY * bitmap->width + bX]; + cacheBuffer[cacheY*cacheWidth + cacheX] = tempCol; + } + } + + // This will dirty the texture and the shader so next time + // we draw it will upload the data + mTextTexture->deferedUploadToTexture(mRSC, false, 0); + mFontShaderF->bindTexture(0, mTextTexture.get()); + + // Some debug code + /*for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + LOGE("Cache Line: H: %u Empty Space: %f", + mCacheLines[i]->mMaxHeight, + (1.0f - (float)mCacheLines[i]->mCurrentCol/(float)mCacheLines[i]->mMaxWidth)*100.0f); + + }*/ + + return true; +} + +void FontState::initRenderState() +{ + uint32_t tmp[] = { + RS_TEX_ENV_MODE_REPLACE, 1, + RS_TEX_ENV_MODE_NONE, 0, + 0, 0 + }; + ProgramFragment *pf = new ProgramFragment(mRSC, tmp, 6); + mFontShaderF.set(pf); + mFontShaderF->init(mRSC); + + Sampler *sampler = new Sampler(mRSC, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, + RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP); + mFontSampler.set(sampler); + mFontShaderF->bindSampler(0, sampler); + + ProgramStore *fontStore = new ProgramStore(mRSC); + mFontProgramStore.set(fontStore); + mFontProgramStore->setDepthFunc(RS_DEPTH_FUNC_ALWAYS); + mFontProgramStore->setBlendFunc(RS_BLEND_SRC_SRC_ALPHA, RS_BLEND_DST_ONE_MINUS_SRC_ALPHA); + mFontProgramStore->setDitherEnable(false); + mFontProgramStore->setDepthMask(false); +} + +void FontState::initTextTexture() +{ + const Element *alphaElem = Element::create(mRSC, RS_TYPE_UNSIGNED_8, RS_KIND_PIXEL_A, true, 1); + + // We will allocate a texture to initially hold 32 character bitmaps + Type *texType = new Type(mRSC); + texType->setElement(alphaElem); + texType->setDimX(1024); + texType->setDimY(256); + texType->compute(); + + Allocation *cacheAlloc = new Allocation(mRSC, texType); + mTextTexture.set(cacheAlloc); + mTextTexture->deferedUploadToTexture(mRSC, false, 0); + + // Split up our cache texture into lines of certain widths + int nextLine = 0; + mCacheLines.push(new CacheTextureLine(16, texType->getDimX(), nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(40, texType->getDimX(), nextLine, 0)); + nextLine += mCacheLines.top()->mMaxHeight; + mCacheLines.push(new CacheTextureLine(texType->getDimY() - nextLine, texType->getDimX(), nextLine, 0)); +} + +// Avoid having to reallocate memory and render quad by quad +void FontState::initVertexArrayBuffers() +{ + // Now lets write index data + const Element *indexElem = Element::create(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1); + Type *indexType = new Type(mRSC); + uint32_t numIndicies = mMaxNumberOfQuads * 6; + indexType->setDimX(numIndicies); + indexType->setElement(indexElem); + indexType->compute(); + + Allocation *indexAlloc = new Allocation(mRSC, indexType); + uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr(); + + // Four verts, two triangles , six indices per quad + for(uint32_t i = 0; i < mMaxNumberOfQuads; i ++) { + int i6 = i * 6; + int i4 = i * 4; + + indexPtr[i6 + 0] = i4 + 0; + indexPtr[i6 + 1] = i4 + 1; + indexPtr[i6 + 2] = i4 + 2; + + indexPtr[i6 + 3] = i4 + 0; + indexPtr[i6 + 4] = i4 + 2; + indexPtr[i6 + 5] = i4 + 3; + } + + indexAlloc->deferedUploadToBufferObject(mRSC); + mIndexBuffer.set(indexAlloc); + + const Element *posElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3); + const Element *texElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2); + + const Element *elemArray[2]; + elemArray[0] = posElem; + elemArray[1] = texElem; + + String8 posName("position"); + String8 texName("texture0"); + + const char *nameArray[2]; + nameArray[0] = posName.string(); + nameArray[1] = texName.string(); + size_t lengths[2]; + lengths[0] = posName.size(); + lengths[1] = texName.size(); + + const Element *vertexDataElem = Element::create(mRSC, 2, elemArray, nameArray, lengths); + + Type *vertexDataType = new Type(mRSC); + vertexDataType->setDimX(mMaxNumberOfQuads * 4); + vertexDataType->setElement(vertexDataElem); + vertexDataType->compute(); + + Allocation *vertexAlloc = new Allocation(mRSC, vertexDataType); + mTextMeshPtr = (float*)vertexAlloc->getPtr(); + + mVertexArray.set(vertexAlloc); +} + +// We don't want to allocate anything unless we actually draw text +void FontState::checkInit() +{ + if(mInitialized) { + return; + } + + initTextTexture(); + initRenderState(); + + initVertexArrayBuffers(); + + mInitialized = true; +} + +void FontState::issueDrawCommand() { + + ObjectBaseRef<const ProgramVertex> tmpV(mRSC->getVertex()); + mRSC->setVertex(mRSC->getDefaultProgramVertex()); + + ObjectBaseRef<const ProgramRaster> tmpR(mRSC->getRaster()); + mRSC->setRaster(mRSC->getDefaultProgramRaster()); + + ObjectBaseRef<const ProgramFragment> tmpF(mRSC->getFragment()); + mRSC->setFragment(mFontShaderF.get()); + + ObjectBaseRef<const ProgramStore> tmpPS(mRSC->getFragmentStore()); + mRSC->setFragmentStore(mFontProgramStore.get()); + + if(mFontColorDirty) { + mFontShaderF->setConstantColor(mFontColor[0], mFontColor[1], mFontColor[2], mFontColor[3]); + mFontColorDirty = false; + } + + if (!mRSC->setupCheck()) { + mRSC->setVertex((ProgramVertex *)tmpV.get()); + mRSC->setRaster((ProgramRaster *)tmpR.get()); + mRSC->setFragment((ProgramFragment *)tmpF.get()); + mRSC->setFragmentStore((ProgramStore *)tmpPS.get()); + return; + } + + float *vtx = (float*)mVertexArray->getPtr(); + float *tex = vtx + 3; + + VertexArray va; + va.add(GL_FLOAT, 3, 20, false, (uint32_t)vtx, "position"); + va.add(GL_FLOAT, 2, 20, false, (uint32_t)tex, "texture0"); + va.setupGL2(mRSC, &mRSC->mStateVertexArray, &mRSC->mShaderCache); + + mIndexBuffer->uploadCheck(mRSC); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); + glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, (uint16_t *)(0)); + + // Reset the state + mRSC->setVertex((ProgramVertex *)tmpV.get()); + mRSC->setRaster((ProgramRaster *)tmpR.get()); + mRSC->setFragment((ProgramFragment *)tmpF.get()); + mRSC->setFragmentStore((ProgramStore *)tmpPS.get()); +} + +void FontState::appendMeshQuad(float x1, float y1, float z1, + float u1, float v1, + float x2, float y2, float z2, + float u2, float v2, + float x3, float y3, float z3, + float u3, float v3, + float x4, float y4, float z4, + float u4, float v4) +{ + const uint32_t vertsPerQuad = 4; + const uint32_t floatsPerVert = 5; + float *currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert; + + // Cull things that are off the screen + float width = (float)mRSC->getWidth(); + float height = (float)mRSC->getHeight(); + + if(x1 > width || y1 < 0.0f || x2 < 0 || y4 > height) { + return; + } + + /*LOGE("V0 x: %f y: %f z: %f", x1, y1, z1); + LOGE("V1 x: %f y: %f z: %f", x2, y2, z2); + LOGE("V2 x: %f y: %f z: %f", x3, y3, z3); + LOGE("V3 x: %f y: %f z: %f", x4, y4, z4);*/ + + (*currentPos++) = x1; + (*currentPos++) = y1; + (*currentPos++) = z1; + (*currentPos++) = u1; + (*currentPos++) = v1; + + (*currentPos++) = x2; + (*currentPos++) = y2; + (*currentPos++) = z2; + (*currentPos++) = u2; + (*currentPos++) = v2; + + (*currentPos++) = x3; + (*currentPos++) = y3; + (*currentPos++) = z3; + (*currentPos++) = u3; + (*currentPos++) = v3; + + (*currentPos++) = x4; + (*currentPos++) = y4; + (*currentPos++) = z4; + (*currentPos++) = u4; + (*currentPos++) = v4; + + mCurrentQuadIndex ++; + + if(mCurrentQuadIndex == mMaxNumberOfQuads) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } +} + +void FontState::renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y) +{ + checkInit(); + + //String8 text8(text); + + // Render code here + Font *currentFont = mRSC->getFont(); + if(!currentFont) { + if(!mDefault.get()) { + mDefault.set(Font::create(mRSC, "DroidSans.ttf", 16, 96)); + } + currentFont = mDefault.get(); + } + if(!currentFont) { + LOGE("Unable to initialize any fonts"); + return; + } + + currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y); + + if(mCurrentQuadIndex != 0) { + issueDrawCommand(); + mCurrentQuadIndex = 0; + } +} + +void FontState::renderText(const char *text, int x, int y) +{ + size_t textLen = strlen(text); + renderText(text, textLen, 0, -1, x, y); +} + +void FontState::renderText(Allocation *alloc, int x, int y) +{ + if(!alloc) { + return; + } + + const char *text = (const char *)alloc->getPtr(); + size_t allocSize = alloc->getType()->getSizeBytes(); + renderText(text, allocSize, 0, -1, x, y); +} + +void FontState::renderText(Allocation *alloc, uint32_t start, int len, int x, int y) +{ + if(!alloc) { + return; + } + + const char *text = (const char *)alloc->getPtr(); + size_t allocSize = alloc->getType()->getSizeBytes(); + renderText(text, allocSize, start, len, x, y); +} + +void FontState::setFontColor(float r, float g, float b, float a) { + mFontColor[0] = r; + mFontColor[1] = g; + mFontColor[2] = b; + mFontColor[3] = a; + mFontColorDirty = true; +} + +void FontState::getFontColor(float *r, float *g, float *b, float *a) const { + *r = mFontColor[0]; + *g = mFontColor[1]; + *b = mFontColor[2]; + *a = mFontColor[3]; +} + +void FontState::deinit(Context *rsc) +{ + mInitialized = false; + + mIndexBuffer.clear(); + mVertexArray.clear(); + + mFontShaderF.clear(); + mFontSampler.clear(); + mFontProgramStore.clear(); + + mTextTexture.clear(); + for(uint32_t i = 0; i < mCacheLines.size(); i ++) { + delete mCacheLines[i]; + } + mCacheLines.clear(); + + mDefault.clear(); + + Vector<Font*> fontsToDereference = mActiveFonts; + for(uint32_t i = 0; i < fontsToDereference.size(); i ++) { + fontsToDereference[i]->zeroUserRef(); + } + + if(mLibrary) { + FT_Done_FreeType( mLibrary ); + mLibrary = NULL; + } +} + +namespace android { +namespace renderscript { + +RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi) +{ + Font *newFont = Font::create(rsc, name, fontSize, dpi); + if(newFont) { + newFont->incUserRef(); + } + return newFont; +} + +} // renderscript +} // android diff --git a/libs/rs/rsFont.h b/libs/rs/rsFont.h new file mode 100644 index 0000000..ab229be --- /dev/null +++ b/libs/rs/rsFont.h @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_FONT_H +#define ANDROID_RS_FONT_H + +#include "RenderScript.h" +#include "rsStream.h" +#include <utils/String8.h> +#include <utils/Vector.h> +#include <utils/KeyedVector.h> + +#include <ft2build.h> +#include FT_FREETYPE_H + +// --------------------------------------------------------------------------- +namespace android { + +namespace renderscript { + +class FontState; + +class Font : public ObjectBase +{ +public: + ~Font(); + + // Pointer to the utf data, length of data, where to start, number of glyphs ot read + // (each glyph may be longer than a char because we are dealing with utf data) + // Last two variables are the initial pen position + void renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyphs, int x, int y); + + // Currently files do not get serialized, + // but we need to inherit from ObjectBase for ref tracking + virtual void serialize(OStream *stream) const { + } + virtual RsA3DClassID getClassId() const { + return RS_A3D_CLASS_ID_UNKNOWN; + } + + static Font * create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi); + +protected: + + friend class FontState; + + void invalidateTextureCache(); + struct CachedGlyphInfo + { + // Has the cache been invalidated? + bool mIsValid; + // Location of the cached glyph in the bitmap + // in case we need to resize the texture + uint32_t mBitmapMinX; + uint32_t mBitmapMinY; + uint32_t mBitmapWidth; + uint32_t mBitmapHeight; + // Also cache texture coords for the quad + float mBitmapMinU; + float mBitmapMinV; + float mBitmapMaxU; + float mBitmapMaxV; + // Minimize how much we call freetype + FT_UInt mGlyphIndex; + FT_Vector mAdvance; + // Values below contain a glyph's origin in the bitmap + FT_Int mBitmapLeft; + FT_Int mBitmapTop; + }; + + String8 mFontName; + uint32_t mFontSize; + uint32_t mDpi; + + Font(Context *rsc); + bool init(const char *name, uint32_t fontSize, uint32_t dpi); + + FT_Face mFace; + bool mInitialized; + bool mHasKerning; + + DefaultKeyedVector<uint32_t, CachedGlyphInfo* > mCachedGlyphs; + + CachedGlyphInfo *cacheGlyph(uint32_t glyph); + void updateGlyphCache(CachedGlyphInfo *glyph); + void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y); +}; + +class FontState +{ +public: + FontState(); + ~FontState(); + + void init(Context *rsc); + void deinit(Context *rsc); + + ObjectBaseRef<Font> mDefault; + ObjectBaseRef<Font> mLast; + + void renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y); + void renderText(const char *text, int x, int y); + void renderText(Allocation *alloc, int x, int y); + void renderText(Allocation *alloc, uint32_t start, int len, int x, int y); + + void setFontColor(float r, float g, float b, float a); + void getFontColor(float *r, float *g, float *b, float *a) const; + +protected: + + friend class Font; + + struct CacheTextureLine + { + uint32_t mMaxHeight; + uint32_t mMaxWidth; + uint32_t mCurrentRow; + uint32_t mCurrentCol; + + CacheTextureLine(uint32_t maxHeight, uint32_t maxWidth, uint32_t currentRow, uint32_t currentCol) : + mMaxHeight(maxHeight), mMaxWidth(maxWidth), mCurrentRow(currentRow), mCurrentCol(currentCol) { + } + + bool fitBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY) { + if((uint32_t)bitmap->rows > mMaxHeight) { + return false; + } + + if(mCurrentCol + (uint32_t)bitmap->width < mMaxWidth) { + *retOriginX = mCurrentCol; + *retOriginY = mCurrentRow; + mCurrentCol += bitmap->width; + return true; + } + + return false; + } + }; + + Vector<CacheTextureLine*> mCacheLines; + + Context *mRSC; + + float mFontColor[4]; + bool mFontColorDirty; + + // Free type library, we only need one copy + FT_Library mLibrary; + FT_Library getLib(); + Vector<Font*> mActiveFonts; + + // Render state for the font + ObjectBaseRef<ProgramFragment> mFontShaderF; + ObjectBaseRef<Sampler> mFontSampler; + ObjectBaseRef<ProgramStore> mFontProgramStore; + void initRenderState(); + + // Texture to cache glyph bitmaps + ObjectBaseRef<Allocation> mTextTexture; + void initTextTexture(); + + bool cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY); + const Type* getCacheTextureType() { + return mTextTexture->getType(); + } + + void flushAllAndInvalidate(); + + // Pointer to vertex data to speed up frame to frame work + float *mTextMeshPtr; + uint32_t mCurrentQuadIndex; + uint32_t mMaxNumberOfQuads; + + void initVertexArrayBuffers(); + ObjectBaseRef<Allocation> mIndexBuffer; + ObjectBaseRef<Allocation> mVertexArray; + + + bool mInitialized; + + void checkInit(); + + void issueDrawCommand(); + + void appendMeshQuad(float x1, float y1, float z1, + float u1, float v1, + float x2, float y2, float z2, + float u2, float v2, + float x3, float y3, float z3, + float u3, float v3, + float x4, float y4, float z4, + float u4, float v4); + +}; + + +} +} + +#endif diff --git a/libs/rs/rsHandcode.h b/libs/rs/rsHandcode.h index 800eddd..353b73a 100644 --- a/libs/rs/rsHandcode.h +++ b/libs/rs/rsHandcode.h @@ -1,6 +1,57 @@ #define DATA_SYNC_SIZE 1024 +static inline void rsHCAPI_ContextFinish (RsContext rsc) +{ + ThreadIO *io = &((Context *)rsc)->mIO; + uint32_t size = sizeof(RS_CMD_ContextFinish); + RS_CMD_ContextFinish *cmd = static_cast<RS_CMD_ContextFinish *>(io->mToCore.reserve(size)); + io->mToCore.commitSync(RS_CMD_ID_ContextFinish, size); +} + +static inline void rsHCAPI_ScriptInvokeV (RsContext rsc, RsScript va, uint32_t slot, const void * data, uint32_t sizeBytes) +{ + ThreadIO *io = &((Context *)rsc)->mIO; + uint32_t size = sizeof(RS_CMD_ScriptInvokeV); + if (sizeBytes < DATA_SYNC_SIZE) { + size += (sizeBytes + 3) & ~3; + } + RS_CMD_ScriptInvokeV *cmd = static_cast<RS_CMD_ScriptInvokeV *>(io->mToCore.reserve(size)); + cmd->s = va; + cmd->slot = slot; + cmd->dataLen = sizeBytes; + cmd->data = data; + if (sizeBytes < DATA_SYNC_SIZE) { + cmd->data = (void *)(cmd+1); + memcpy(cmd+1, data, sizeBytes); + io->mToCore.commit(RS_CMD_ID_ScriptInvokeV, size); + } else { + io->mToCore.commitSync(RS_CMD_ID_ScriptInvokeV, size); + } +} + + +static inline void rsHCAPI_ScriptSetVarV (RsContext rsc, RsScript va, uint32_t slot, const void * data, uint32_t sizeBytes) +{ + ThreadIO *io = &((Context *)rsc)->mIO; + uint32_t size = sizeof(RS_CMD_ScriptSetVarV); + if (sizeBytes < DATA_SYNC_SIZE) { + size += (sizeBytes + 3) & ~3; + } + RS_CMD_ScriptSetVarV *cmd = static_cast<RS_CMD_ScriptSetVarV *>(io->mToCore.reserve(size)); + cmd->s = va; + cmd->slot = slot; + cmd->dataLen = sizeBytes; + cmd->data = data; + if (sizeBytes < DATA_SYNC_SIZE) { + cmd->data = (void *)(cmd+1); + memcpy(cmd+1, data, sizeBytes); + io->mToCore.commit(RS_CMD_ID_ScriptSetVarV, size); + } else { + io->mToCore.commitSync(RS_CMD_ID_ScriptSetVarV, size); + } +} + static inline void rsHCAPI_AllocationData (RsContext rsc, RsAllocation va, const void * data, uint32_t sizeBytes) { ThreadIO *io = &((Context *)rsc)->mIO; diff --git a/libs/rs/rsLight.cpp b/libs/rs/rsLight.cpp index 6f2cf3e..eab9a07 100644 --- a/libs/rs/rsLight.cpp +++ b/libs/rs/rsLight.cpp @@ -14,9 +14,13 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" - #include <GLES/gl.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#endif //ANDROID_RS_BUILD_FOR_HOST using namespace android; using namespace android::renderscript; @@ -65,6 +69,16 @@ void Light::setupGL(uint32_t num) const glLightfv(GL_LIGHT0 + num, GL_POSITION, mPosition); } +void Light::serialize(OStream *stream) const +{ + +} + +Light *Light::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + //////////////////////////////////////////// LightState::LightState() diff --git a/libs/rs/rsLight.h b/libs/rs/rsLight.h index d8796e6..bd58979 100644 --- a/libs/rs/rsLight.h +++ b/libs/rs/rsLight.h @@ -37,6 +37,9 @@ public: void setColor(float r, float g, float b); void setupGL(uint32_t num) const; + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_LIGHT; } + static Light *createFromStream(Context *rsc, IStream *stream); protected: float mColor[4]; diff --git a/libs/rs/rsLocklessFifo.cpp b/libs/rs/rsLocklessFifo.cpp index c796520..76ca32e 100644 --- a/libs/rs/rsLocklessFifo.cpp +++ b/libs/rs/rsLocklessFifo.cpp @@ -17,7 +17,7 @@ #include "rsLocklessFifo.h" using namespace android; - +using namespace android::renderscript; LocklessCommandFifo::LocklessCommandFifo() { @@ -128,15 +128,19 @@ void LocklessCommandFifo::flush() //dumpState("flush 2"); } +void LocklessCommandFifo::wait() +{ + while(isEmpty() && !mInShutdown) { + mSignalToControl.set(); + mSignalToWorker.wait(); + } +} + const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) { while(1) { //dumpState("get"); - while(isEmpty() && !mInShutdown) { - mSignalToControl.set(); - mSignalToWorker.wait(); - } - + wait(); if (mInShutdown) { *command = 0; *bytesData = 0; @@ -192,79 +196,3 @@ void LocklessCommandFifo::dumpState(const char *s) const LOGV("%s put %p, get %p, buf %p, end %p", s, mPut, mGet, mBuffer, mEnd); } -LocklessCommandFifo::Signal::Signal() -{ - mSet = true; -} - -LocklessCommandFifo::Signal::~Signal() -{ - pthread_mutex_destroy(&mMutex); - pthread_cond_destroy(&mCondition); -} - -bool LocklessCommandFifo::Signal::init() -{ - int status = pthread_mutex_init(&mMutex, NULL); - if (status) { - LOGE("LocklessFifo mutex init failure"); - return false; - } - - status = pthread_cond_init(&mCondition, NULL); - if (status) { - LOGE("LocklessFifo condition init failure"); - pthread_mutex_destroy(&mMutex); - return false; - } - - return true; -} - -void LocklessCommandFifo::Signal::set() -{ - int status; - - status = pthread_mutex_lock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i locking for set condition.", status); - return; - } - - mSet = true; - - status = pthread_cond_signal(&mCondition); - if (status) { - LOGE("LocklessCommandFifo: error %i on set condition.", status); - } - - status = pthread_mutex_unlock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); - } -} - -void LocklessCommandFifo::Signal::wait() -{ - int status; - - status = pthread_mutex_lock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i locking for condition.", status); - return; - } - - if (!mSet) { - status = pthread_cond_wait(&mCondition, &mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i waiting on condition.", status); - } - } - mSet = false; - - status = pthread_mutex_unlock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i unlocking for condition.", status); - } -} - diff --git a/libs/rs/rsLocklessFifo.h b/libs/rs/rsLocklessFifo.h index d0a4356..ae906ca 100644 --- a/libs/rs/rsLocklessFifo.h +++ b/libs/rs/rsLocklessFifo.h @@ -19,8 +19,10 @@ #include "rsUtils.h" +#include "rsSignal.h" namespace android { +namespace renderscript { // A simple FIFO to be used as a producer / consumer between two @@ -37,24 +39,7 @@ public: LocklessCommandFifo(); ~LocklessCommandFifo(); - protected: - class Signal { - public: - Signal(); - ~Signal(); - - bool init(); - - void set(); - void wait(); - - protected: - bool mSet; - pthread_mutex_t mMutex; - pthread_cond_t mCondition; - }; - uint8_t * volatile mPut; uint8_t * volatile mGet; uint8_t * mBuffer; @@ -65,14 +50,14 @@ protected: Signal mSignalToWorker; Signal mSignalToControl; - - public: void * reserve(uint32_t bytes); void commit(uint32_t command, uint32_t bytes); void commitSync(uint32_t command, uint32_t bytes); void flush(); + void wait(); + const void * get(uint32_t *command, uint32_t *bytesData); void next(); @@ -88,4 +73,5 @@ private: } +} #endif diff --git a/libs/rs/rsMatrix.cpp b/libs/rs/rsMatrix.cpp index 2f21405..94eef13 100644 --- a/libs/rs/rsMatrix.cpp +++ b/libs/rs/rsMatrix.cpp @@ -73,7 +73,7 @@ void Matrix::loadRotate(float rot, float x, float y, float z) s = sinf(rot); const float len = sqrtf(x*x + y*y + z*z); - if (!(len != 1)) { + if (len != 1) { const float recipLen = 1.f / len; x *= recipLen; y *= recipLen; diff --git a/libs/rs/rsMesh.cpp b/libs/rs/rsMesh.cpp index d595b4e..9026578 100644 --- a/libs/rs/rsMesh.cpp +++ b/libs/rs/rsMesh.cpp @@ -14,28 +14,215 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -using namespace android; -using namespace android::renderscript; - #include <GLES/gl.h> +#include <GLES2/gl2.h> #include <GLES/glext.h> +#else +#include "rsContextHostStub.h" + +#include <OpenGL/gl.h> +#include <OpenGl/glext.h> +#endif + + +using namespace android; +using namespace android::renderscript; Mesh::Mesh(Context *rsc) : ObjectBase(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; - mVerticies = NULL; - mVerticiesCount = 0; mPrimitives = NULL; mPrimitivesCount = 0; + mVertexBuffers = NULL; + mVertexBufferCount = 0; } Mesh::~Mesh() { + if(mVertexBuffers) { + delete[] mVertexBuffers; + } + + if(mPrimitives) { + for(uint32_t i = 0; i < mPrimitivesCount; i ++) { + delete mPrimitives[i]; + } + delete[] mPrimitives; + } +} + +void Mesh::render(Context *rsc) const +{ + for(uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + renderPrimitive(rsc, ct); + } +} + +void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { + if (primIndex >= mPrimitivesCount) { + LOGE("Invalid primitive index"); + return; + } + + Primitive_t *prim = mPrimitives[primIndex]; + + if (prim->mIndexBuffer.get()) { + renderPrimitiveRange(rsc, primIndex, 0, prim->mIndexBuffer->getType()->getDimX()); + return; + } + + renderPrimitiveRange(rsc, primIndex, 0, mVertexBuffers[0]->getType()->getDimX()); } +void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const +{ + if (len < 1 || primIndex >= mPrimitivesCount) { + return; + } + + rsc->checkError("Mesh::renderPrimitiveRange 1"); + VertexArray va; + for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { + mVertexBuffers[ct]->uploadCheck(rsc); + if (mVertexBuffers[ct]->getIsBufferObject()) { + va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); + } else { + va.setActiveBuffer(mVertexBuffers[ct]->getPtr()); + } + mVertexBuffers[ct]->getType()->enableGLVertexBuffer(&va); + } + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + + rsc->checkError("Mesh::renderPrimitiveRange 2"); + Primitive_t *prim = mPrimitives[primIndex]; + if (prim->mIndexBuffer.get()) { + prim->mIndexBuffer->uploadCheck(rsc); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID()); + glDrawElements(prim->mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); + } else { + glDrawArrays(prim->mGLPrimitive, start, len); + } + + rsc->checkError("Mesh::renderPrimitiveRange"); +} + + +void Mesh::uploadAll(Context *rsc) +{ + for (uint32_t ct = 0; ct < mVertexBufferCount; ct ++) { + if (mVertexBuffers[ct].get()) { + mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); + } + } + + for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + if (mPrimitives[ct]->mIndexBuffer.get()) { + mPrimitives[ct]->mIndexBuffer->deferedUploadToBufferObject(rsc); + } + } + + rsc->checkError("Mesh::uploadAll"); +} + +void Mesh::updateGLPrimitives() +{ + for(uint32_t i = 0; i < mPrimitivesCount; i ++) { + switch(mPrimitives[i]->mPrimitive) { + case RS_PRIMITIVE_POINT: mPrimitives[i]->mGLPrimitive = GL_POINTS; break; + case RS_PRIMITIVE_LINE: mPrimitives[i]->mGLPrimitive = GL_LINES; break; + case RS_PRIMITIVE_LINE_STRIP: mPrimitives[i]->mGLPrimitive = GL_LINE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE: mPrimitives[i]->mGLPrimitive = GL_TRIANGLES; break; + case RS_PRIMITIVE_TRIANGLE_STRIP: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE_FAN: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_FAN; break; + } + } +} + +void Mesh::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + // Store number of vertex streams + stream->addU32(mVertexBufferCount); + for(uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) { + mVertexBuffers[vCount]->serialize(stream); + } + + stream->addU32(mPrimitivesCount); + // Store the primitives + for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) { + Primitive_t * prim = mPrimitives[pCount]; + + stream->addU8((uint8_t)prim->mPrimitive); + + if(prim->mIndexBuffer.get()) { + stream->addU32(1); + prim->mIndexBuffer->serialize(stream); + } + else { + stream->addU32(0); + } + } +} + +Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) +{ + // First make sure we are reading the correct object + RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); + if(classID != RS_A3D_CLASS_ID_MESH) { + LOGE("mesh loading skipped due to invalid class id"); + return NULL; + } + + Mesh * mesh = new Mesh(rsc); + + String8 name; + stream->loadString(&name); + mesh->setName(name.string(), name.size()); + + mesh->mVertexBufferCount = stream->loadU32(); + if(mesh->mVertexBufferCount) { + mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount]; + + for(uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) { + Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); + mesh->mVertexBuffers[vCount].set(vertexAlloc); + } + } + + mesh->mPrimitivesCount = stream->loadU32(); + if(mesh->mPrimitivesCount) { + mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount]; + + // load all primitives + for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) { + Primitive_t * prim = new Primitive_t; + mesh->mPrimitives[pCount] = prim; + + prim->mPrimitive = (RsPrimitive)stream->loadU8(); + + // Check to see if the index buffer was stored + uint32_t isIndexPresent = stream->loadU32(); + if(isIndexPresent) { + Allocation *indexAlloc = Allocation::createFromStream(rsc, stream); + prim->mIndexBuffer.set(indexAlloc); + } + } + } + + mesh->updateGLPrimitives(); + mesh->uploadAll(rsc); + + return mesh; +} MeshContext::MeshContext() @@ -46,3 +233,83 @@ MeshContext::~MeshContext() { } +namespace android { +namespace renderscript { + +RsMesh rsi_MeshCreate(Context *rsc, uint32_t vtxCount, uint32_t idxCount) +{ + Mesh *sm = new Mesh(rsc); + sm->incUserRef(); + + sm->mPrimitivesCount = idxCount; + sm->mPrimitives = new Mesh::Primitive_t *[sm->mPrimitivesCount]; + for(uint32_t ct = 0; ct < idxCount; ct ++) { + sm->mPrimitives[ct] = new Mesh::Primitive_t; + } + + sm->mVertexBufferCount = vtxCount; + sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; + + return sm; +} + +void rsi_MeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mVertexBufferCount); + + sm->mVertexBuffers[slot].set((Allocation *)va); +} + +void rsi_MeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primType, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mPrimitivesCount); + + sm->mPrimitives[slot]->mIndexBuffer.set((Allocation *)va); + sm->mPrimitives[slot]->mPrimitive = (RsPrimitive)primType; + sm->updateGLPrimitives(); +} + +void rsi_MeshGetVertexBufferCount(Context *rsc, RsMesh mv, int32_t *numVtx) +{ + Mesh *sm = static_cast<Mesh *>(mv); + *numVtx = sm->mVertexBufferCount; +} + +void rsi_MeshGetIndexCount(Context *rsc, RsMesh mv, int32_t *numIdx) +{ + Mesh *sm = static_cast<Mesh *>(mv); + *numIdx = sm->mPrimitivesCount; +} + +void rsi_MeshGetVertices(Context *rsc, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(vtxDataCount == sm->mVertexBufferCount); + + for(uint32_t ct = 0; ct < vtxDataCount; ct ++) { + vtxData[ct] = sm->mVertexBuffers[ct].get(); + sm->mVertexBuffers[ct]->incUserRef(); + } +} + +void rsi_MeshGetIndices(Context *rsc, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(idxDataCount == sm->mPrimitivesCount); + + for(uint32_t ct = 0; ct < idxDataCount; ct ++) { + va[ct] = sm->mPrimitives[ct]->mIndexBuffer.get(); + primType[ct] = sm->mPrimitives[ct]->mPrimitive; + if(sm->mPrimitives[ct]->mIndexBuffer.get()) { + sm->mPrimitives[ct]->mIndexBuffer->incUserRef(); + } + } + +} + + + + +}} diff --git a/libs/rs/rsMesh.h b/libs/rs/rsMesh.h index 5201abd..765a971 100644 --- a/libs/rs/rsMesh.h +++ b/libs/rs/rsMesh.h @@ -32,45 +32,35 @@ public: Mesh(Context *); ~Mesh(); - struct Verticies_t - { - Allocation ** mAllocations; - uint32_t mAllocationCount; - - size_t mVertexDataSize; - - size_t mOffsetCoord; - size_t mOffsetTex; - size_t mOffsetNorm; - - size_t mSizeCoord; - size_t mSizeTex; - size_t mSizeNorm; - - uint32_t mBufferObject; - }; - + // Contains vertex data + // Position, normal, texcoord, etc could either be strided in one allocation + // of provided separetely in multiple ones + ObjectBaseRef<Allocation> *mVertexBuffers; + uint32_t mVertexBufferCount; + + // Either mIndexBuffer, mPrimitiveBuffer or both could have a NULL reference + // If both are null, mPrimitive only would be used to render the mesh struct Primitive_t { - RsPrimitive mType; - Verticies_t *mVerticies; - - uint32_t mIndexCount; - uint16_t *mIndicies; + ObjectBaseRef<Allocation> mIndexBuffer; - uint32_t mRestartCounts; - uint16_t *mRestarts; + RsPrimitive mPrimitive; + uint32_t mGLPrimitive; }; - Verticies_t * mVerticies; - uint32_t mVerticiesCount; - Primitive_t ** mPrimitives; uint32_t mPrimitivesCount; + void render(Context *) const; + void renderPrimitive(Context *, uint32_t primIndex) const; + void renderPrimitiveRange(Context *, uint32_t primIndex, uint32_t start, uint32_t len) const; + void uploadAll(Context *); + void updateGLPrimitives(); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_MESH; } + static Mesh *createFromStream(Context *rsc, IStream *stream); - void analyzeElement(); protected: }; @@ -88,3 +78,4 @@ public: #endif //ANDROID_RS_TRIANGLE_MESH_H + diff --git a/libs/rs/rsMutex.cpp b/libs/rs/rsMutex.cpp new file mode 100644 index 0000000..37752f2 --- /dev/null +++ b/libs/rs/rsMutex.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsMutex.h" + +using namespace android; +using namespace android::renderscript; + + +Mutex::Mutex() +{ +} + +Mutex::~Mutex() +{ + pthread_mutex_destroy(&mMutex); +} + +bool Mutex::init() +{ + int status = pthread_mutex_init(&mMutex, NULL); + if (status) { + LOGE("Mutex::Mutex init failure"); + return false; + } + return true; +} + +bool Mutex::lock() +{ + int status; + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("Mutex: error %i locking.", status); + return false; + } + return true; +} + +bool Mutex::unlock() +{ + int status; + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("Mutex error %i unlocking.", status); + return false; + } + return true; +} + + diff --git a/libs/rs/rsNoise.h b/libs/rs/rsMutex.h index 9040751..47725d7 100644 --- a/libs/rs/rsNoise.h +++ b/libs/rs/rsMutex.h @@ -14,22 +14,30 @@ * limitations under the License. */ -#ifndef ANDROID_RS_NOISE_H -#define ANDROID_RS_NOISE_H +#ifndef ANDROID_RS_MUTEX_H +#define ANDROID_RS_MUTEX_H + + +#include "rsUtils.h" -// --------------------------------------------------------------------------- namespace android { namespace renderscript { -void SC_normalizef2(float v[]); -void SC_normalizef3(float v[]); -float SC_noisef(float x); -float SC_noisef2(float x, float y); -float SC_noisef3(float x, float y, float z); -float SC_turbulencef2(float x, float y, float octaves); -float SC_turbulencef3(float x, float y, float z, float octaves); +class Mutex { +public: + Mutex(); + ~Mutex(); + + bool init(); + bool lock(); + bool unlock(); + +protected: + pthread_mutex_t mMutex; +}; } } #endif + diff --git a/libs/rs/rsNoise.cpp b/libs/rs/rsNoise.cpp deleted file mode 100644 index 4b67586..0000000 --- a/libs/rs/rsNoise.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* - * This implementation of the noise functions was ported from the Java - * implementation by Jerry Huxtable (http://www.jhlabs.com) under - * Apache License 2.0 (see http://jhlabs.com/ip/filters/download.html) - * - * Original header: - * - * Copyright 2006 Jerry Huxtable - * - * 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. - */ - -#include "rsNoise.h" - -#include <math.h> -#include <stdlib.h> -#include <time.h> - -namespace android { -namespace renderscript { - -#define B 0x100 -#define BM 0xff -#define N 0x1000 - -static int p[B + B + 2]; -static float g3[B + B + 2][3]; -static float g2[B + B + 2][2]; -static float g1[B + B + 2]; -static bool noise_start = true; - -#define lerpf(start, stop, amount) start + (stop - start) * amount - -static inline float noise_sCurve(float t) -{ - return t * t * (3.0f - 2.0f * t); -} - -inline void SC_normalizef2(float v[]) -{ - float s = (float)sqrtf(v[0] * v[0] + v[1] * v[1]); - v[0] = v[0] / s; - v[1] = v[1] / s; -} - -inline void SC_normalizef3(float v[]) -{ - float s = (float)sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); - v[0] = v[0] / s; - v[1] = v[1] / s; - v[2] = v[2] / s; -} - -static void noise_init() -{ - int i, j, k; - - for (i = 0; i < B; i++) { - p[i] = i; - - g1[i] = (float)((rand() % (B + B)) - B) / B; - - for (j = 0; j < 2; j++) - g2[i][j] = (float)((rand() % (B + B)) - B) / B; - SC_normalizef2(g2[i]); - - for (j = 0; j < 3; j++) - g3[i][j] = (float)((rand() % (B + B)) - B) / B; - SC_normalizef3(g3[i]); - } - - for (i = B-1; i >= 0; i--) { - k = p[i]; - p[i] = p[j = rand() % B]; - p[j] = k; - } - - for (i = 0; i < B + 2; i++) { - p[B + i] = p[i]; - g1[B + i] = g1[i]; - for (j = 0; j < 2; j++) - g2[B + i][j] = g2[i][j]; - for (j = 0; j < 3; j++) - g3[B + i][j] = g3[i][j]; - } -} - -float SC_noisef(float x) -{ - srand(time(NULL)); - int bx0, bx1; - float rx0, rx1, sx, t, u, v; - - if (noise_start) { - noise_start = false; - noise_init(); - } - - t = x + N; - bx0 = ((int)t) & BM; - bx1 = (bx0+1) & BM; - rx0 = t - (int)t; - rx1 = rx0 - 1.0f; - - sx = noise_sCurve(rx0); - - u = rx0 * g1[p[bx0]]; - v = rx1 * g1[p[bx1]]; - return 2.3f * lerpf(u, v, sx); -} - -float SC_noisef2(float x, float y) -{ - srand(time(NULL)); - int bx0, bx1, by0, by1, b00, b10, b01, b11; - float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v; - float *q; - int i, j; - - if (noise_start) { - noise_start = false; - noise_init(); - } - - t = x + N; - bx0 = ((int)t) & BM; - bx1 = (bx0+1) & BM; - rx0 = t - (int)t; - rx1 = rx0 - 1.0f; - - t = y + N; - by0 = ((int)t) & BM; - by1 = (by0+1) & BM; - ry0 = t - (int)t; - ry1 = ry0 - 1.0f; - - i = p[bx0]; - j = p[bx1]; - - b00 = p[i + by0]; - b10 = p[j + by0]; - b01 = p[i + by1]; - b11 = p[j + by1]; - - sx = noise_sCurve(rx0); - sy = noise_sCurve(ry0); - - q = g2[b00]; u = rx0 * q[0] + ry0 * q[1]; - q = g2[b10]; v = rx1 * q[0] + ry0 * q[1]; - a = lerpf(u, v, sx); - - q = g2[b01]; u = rx0 * q[0] + ry1 * q[1]; - q = g2[b11]; v = rx1 * q[0] + ry1 * q[1]; - b = lerpf(u, v, sx); - - return 1.5f*lerpf(a, b, sy); -} - -float SC_noisef3(float x, float y, float z) -{ - srand(time(NULL)); - int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; - float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v; - float *q; - int i, j; - - if (noise_start) { - noise_start = false; - noise_init(); - } - - t = x + N; - bx0 = ((int)t) & BM; - bx1 = (bx0+1) & BM; - rx0 = t - (int)t; - rx1 = rx0 - 1.0f; - - t = y + N; - by0 = ((int)t) & BM; - by1 = (by0+1) & BM; - ry0 = t - (int)t; - ry1 = ry0 - 1.0f; - - t = z + N; - bz0 = ((int)t) & BM; - bz1 = (bz0+1) & BM; - rz0 = t - (int)t; - rz1 = rz0 - 1.0f; - - i = p[bx0]; - j = p[bx1]; - - b00 = p[i + by0]; - b10 = p[j + by0]; - b01 = p[i + by1]; - b11 = p[j + by1]; - - t = noise_sCurve(rx0); - sy = noise_sCurve(ry0); - sz = noise_sCurve(rz0); - - q = g3[b00 + bz0]; u = rx0 * q[0] + ry0 * q[1] + rz0 * q[2]; - q = g3[b10 + bz0]; v = rx1 * q[0] + ry0 * q[1] + rz0 * q[2]; - a = lerpf(u, v, t); - - q = g3[b01 + bz0]; u = rx0 * q[0] + ry1 * q[1] + rz0 * q[2]; - q = g3[b11 + bz0]; v = rx1 * q[0] + ry1 * q[1] + rz0 * q[2]; - b = lerpf(u, v, t); - - c = lerpf(a, b, sy); - - q = g3[b00 + bz1]; u = rx0 * q[0] + ry0 * q[1] + rz1 * q[2]; - q = g3[b10 + bz1]; v = rx1 * q[0] + ry0 * q[1] + rz1 * q[2]; - a = lerpf(u, v, t); - - q = g3[b01 + bz1]; u = rx0 * q[0] + ry1 * q[1] + rz1 * q[2]; - q = g3[b11 + bz1]; v = rx1 * q[0] + ry1 * q[1] + rz1 * q[2]; - b = lerpf(u, v, t); - - d = lerpf(a, b, sy); - - return 1.5f*lerpf(c, d, sz); -} - -float SC_turbulencef2(float x, float y, float octaves) -{ - srand(time(NULL)); - float t = 0.0f; - - for (float f = 1.0f; f <= octaves; f *= 2) - t += fabs(SC_noisef2(f * x, f * y)) / f; - return t; -} - -float SC_turbulencef3(float x, float y, float z, float octaves) -{ - srand(time(NULL)); - float t = 0.0f; - - for (float f = 1.0f; f <= octaves; f *= 2) - t += fabs(SC_noisef3(f * x, f * y, f * z)) / f; - return t; -} - -} -} diff --git a/libs/rs/rsObjectBase.cpp b/libs/rs/rsObjectBase.cpp index 677413e..48f1fee 100644 --- a/libs/rs/rsObjectBase.cpp +++ b/libs/rs/rsObjectBase.cpp @@ -15,7 +15,12 @@ */ #include "rsObjectBase.h" + +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif using namespace android; using namespace android::renderscript; @@ -24,7 +29,6 @@ ObjectBase::ObjectBase(Context *rsc) { mUserRefCount = 0; mSysRefCount = 0; - mName = NULL; mRSC = NULL; mNext = NULL; mPrev = NULL; @@ -39,14 +43,13 @@ ObjectBase::~ObjectBase() rsAssert(!mUserRefCount); rsAssert(!mSysRefCount); remove(); - delete[] mName; } void ObjectBase::dumpLOGV(const char *op) const { - if (mName) { + if (mName.size()) { LOGV("%s RSobj %p, name %s, refs %i,%i from %s,%i links %p,%p,%p", - op, this, mName, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC); + op, this, mName.string(), mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC); } else { LOGV("%s RSobj %p, no-name, refs %i,%i from %s,%i links %p,%p,%p", op, this, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC); @@ -113,18 +116,12 @@ bool ObjectBase::decSysRef() const void ObjectBase::setName(const char *name) { - setName(name, strlen(name)); + mName.setTo(name); } void ObjectBase::setName(const char *name, uint32_t len) { - delete mName; - mName = NULL; - if (name) { - mName = new char[len + 1]; - memcpy(mName, name, len); - mName[len] = 0; - } + mName.setTo(name, len); } void ObjectBase::add() const diff --git a/libs/rs/rsObjectBase.h b/libs/rs/rsObjectBase.h index bb03b87..ad95b81 100644 --- a/libs/rs/rsObjectBase.h +++ b/libs/rs/rsObjectBase.h @@ -24,6 +24,7 @@ namespace android { namespace renderscript { class Context; +class OStream; // An element is a group of Components that occupies one cell in a structure. class ObjectBase @@ -40,7 +41,7 @@ public: bool zeroUserRef() const; const char * getName() const { - return mName; + return mName.string(); } void setName(const char *); void setName(const char *, uint32_t len); @@ -52,6 +53,8 @@ public: static void dumpAll(Context *rsc); virtual void dumpLOGV(const char *prefix) const; + virtual void serialize(OStream *stream) const = 0; + virtual RsA3DClassID getClassId() const = 0; protected: const char *mAllocFile; @@ -64,7 +67,7 @@ private: bool checkDelete() const; - char * mName; + String8 mName; mutable int32_t mSysRefCount; mutable int32_t mUserRefCount; diff --git a/libs/rs/rsProgram.cpp b/libs/rs/rsProgram.cpp index 70e2868..c4f8b2e 100644 --- a/libs/rs/rsProgram.cpp +++ b/libs/rs/rsProgram.cpp @@ -14,11 +14,17 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -#include "rsProgram.h" - #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGL/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsProgram.h" using namespace android; using namespace android::renderscript; diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp index c17b94c..a045043 100644 --- a/libs/rs/rsProgramFragment.cpp +++ b/libs/rs/rsProgramFragment.cpp @@ -14,13 +14,19 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -#include "rsProgramFragment.h" - #include <GLES/gl.h> #include <GLES/glext.h> #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGL/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsProgramFragment.h" using namespace android; using namespace android::renderscript; @@ -32,13 +38,21 @@ ProgramFragment::ProgramFragment(Context *rsc, const uint32_t * params, { mAllocFile = __FILE__; mAllocLine = __LINE__; - rsAssert(paramLength = 5); + rsAssert(paramLength == 6); + + mConstantColor[0] = 1.f; + mConstantColor[1] = 1.f; + mConstantColor[2] = 1.f; + mConstantColor[3] = 1.f; mEnvModes[0] = (RsTexEnvMode)params[0]; mTextureFormats[0] = params[1]; mEnvModes[1] = (RsTexEnvMode)params[2]; mTextureFormats[1] = params[3]; mPointSpriteEnable = params[4] != 0; + mVaryingColor = false; + if (paramLength > 5) + mVaryingColor = params[5] != 0; mTextureEnableMask = 0; if (mEnvModes[0]) { @@ -47,7 +61,17 @@ ProgramFragment::ProgramFragment(Context *rsc, const uint32_t * params, if (mEnvModes[1]) { mTextureEnableMask |= 2; } - init(rsc); + + mUniformCount = 0; + mUniformNames[mUniformCount++].setTo("uni_Tex0"); + mUniformNames[mUniformCount++].setTo("uni_Tex1"); + + mConstantColorUniformIndex = -1; + //if (!mVaryingColor) { + mConstantColorUniformIndex = mUniformCount; + mUniformNames[mUniformCount++].setTo("uni_Color"); + //} + createShader(); } ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, @@ -58,7 +82,19 @@ ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, mAllocFile = __FILE__; mAllocLine = __LINE__; - init(rsc); + mConstantColor[0] = 1.f; + mConstantColor[1] = 1.f; + mConstantColor[2] = 1.f; + mConstantColor[3] = 1.f; + + LOGE("Custom FP"); + + mUniformCount = 2; + mUniformNames[0].setTo("uni_Tex0"); + mUniformNames[1].setTo("uni_Tex1"); + + createShader(); + mTextureEnableMask = (1 << mTextureCount) -1; } @@ -67,76 +103,17 @@ ProgramFragment::~ProgramFragment() { } -void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state) +void ProgramFragment::setConstantColor(float r, float g, float b, float a) { - if ((state->mLast.get() == this) && !mDirty) { - return; - } - state->mLast.set(this); - - for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) { - glActiveTexture(GL_TEXTURE0 + ct); - if (!(mTextureEnableMask & (1 << ct)) || !mTextures[ct].get()) { - glDisable(GL_TEXTURE_2D); - continue; - } - - glEnable(GL_TEXTURE_2D); - if (rsc->checkVersion1_1()) { - if (mPointSpriteEnable) { - glEnable(GL_POINT_SPRITE_OES); - } else { - glDisable(GL_POINT_SPRITE_OES); - } - glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, mPointSpriteEnable); - } - mTextures[ct]->uploadCheck(rsc); - glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID()); - - switch(mEnvModes[ct]) { - case RS_TEX_ENV_MODE_NONE: - rsAssert(0); - break; - case RS_TEX_ENV_MODE_REPLACE: - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - break; - case RS_TEX_ENV_MODE_MODULATE: - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - break; - case RS_TEX_ENV_MODE_DECAL: - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); - break; - } - - if (mSamplers[ct].get()) { - mSamplers[ct]->setupGL(rsc, mTextures[ct]->getType()->getIsNp2()); - } else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - } + mConstantColor[0] = r; + mConstantColor[1] = g; + mConstantColor[2] = b; + mConstantColor[3] = a; + mDirty = true; +} - // Gross hack. - if (ct == 2) { - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD); - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD); - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); - } - } - glActiveTexture(GL_TEXTURE0); - mDirty = false; - rsc->checkError("ProgramFragment::setupGL"); +void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state) +{ } void ProgramFragment::setupGL2(const Context *rsc, ProgramFragmentState *state, ShaderCache *sc) @@ -144,11 +121,19 @@ void ProgramFragment::setupGL2(const Context *rsc, ProgramFragmentState *state, //LOGE("sgl2 frag1 %x", glGetError()); if ((state->mLast.get() == this) && !mDirty) { - //return; + return; } state->mLast.set(this); rsc->checkError("ProgramFragment::setupGL2 start"); + + if (!mVaryingColor && + (sc->fragUniformSlot(mConstantColorUniformIndex) >= 0)) { + //LOGE("mConstantColorUniformIndex %i %i", mConstantColorUniformIndex, sc->fragUniformSlot(mConstantColorUniformIndex)); + glUniform4fv(sc->fragUniformSlot(mConstantColorUniformIndex), 1, mConstantColor); + rsc->checkError("ProgramFragment::color setup"); + } + for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) { glActiveTexture(GL_TEXTURE0 + ct); if (!(mTextureEnableMask & (1 << ct)) || !mTextures[ct].get()) { @@ -184,8 +169,9 @@ void ProgramFragment::loadShader(Context *rsc) { void ProgramFragment::createShader() { mShader.setTo("precision mediump float;\n"); - mShader.append("varying vec4 varColor;\n"); + mShader.append("varying lowp vec4 varColor;\n"); mShader.append("varying vec4 varTex0;\n"); + mShader.append("uniform vec4 uni_Color;\n"); if (mUserShader.length() > 1) { for (uint32_t ct=0; ct < mTextureCount; ct++) { @@ -212,7 +198,11 @@ void ProgramFragment::createShader() mShader.append("void main() {\n"); - mShader.append(" vec4 col = varColor;\n"); + if (mVaryingColor) { + mShader.append(" lowp vec4 col = varColor;\n"); + } else { + mShader.append(" lowp vec4 col = uni_Color;\n"); + } if (mTextureEnableMask) { if (mPointSpriteEnable) { @@ -282,11 +272,16 @@ void ProgramFragment::createShader() void ProgramFragment::init(Context *rsc) { - mUniformCount = 2; - mUniformNames[0].setTo("uni_Tex0"); - mUniformNames[1].setTo("uni_Tex1"); +} - createShader(); +void ProgramFragment::serialize(OStream *stream) const +{ + +} + +ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; } ProgramFragmentState::ProgramFragmentState() @@ -300,14 +295,14 @@ ProgramFragmentState::~ProgramFragmentState() } -void ProgramFragmentState::init(Context *rsc, int32_t w, int32_t h) +void ProgramFragmentState::init(Context *rsc) { - uint32_t tmp[5] = { + uint32_t tmp[] = { RS_TEX_ENV_MODE_NONE, 0, RS_TEX_ENV_MODE_NONE, 0, - 0 + 0, 0 }; - ProgramFragment *pf = new ProgramFragment(rsc, tmp, 5); + ProgramFragment *pf = new ProgramFragment(rsc, tmp, 6); mDefault.set(pf); pf->init(rsc); } @@ -328,6 +323,7 @@ RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, { ProgramFragment *pf = new ProgramFragment(rsc, params, paramLength); pf->incUserRef(); + //LOGE("rsi_ProgramFragmentCreate %p", pf); return pf; } @@ -337,6 +333,7 @@ RsProgramFragment rsi_ProgramFragmentCreate2(Context *rsc, const char * shaderTe { ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); pf->incUserRef(); + //LOGE("rsi_ProgramFragmentCreate2 %p", pf); return pf; } diff --git a/libs/rs/rsProgramFragment.h b/libs/rs/rsProgramFragment.h index 9fa565d..7c1598e 100644 --- a/libs/rs/rsProgramFragment.h +++ b/libs/rs/rsProgramFragment.h @@ -40,6 +40,11 @@ public: virtual void createShader(); virtual void loadShader(Context *rsc); virtual void init(Context *rsc); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_FRAGMENT; } + static ProgramFragment *createFromStream(Context *rsc, IStream *stream); + + void setConstantColor(float, float, float, float); protected: // Hacks to create a program for now @@ -48,6 +53,10 @@ protected: RsTexEnvMode mEnvModes[MAX_TEXTURE]; uint32_t mTextureEnableMask; bool mPointSpriteEnable; + bool mVaryingColor; + + float mConstantColor[4]; + int32_t mConstantColorUniformIndex; }; class ProgramFragmentState @@ -57,7 +66,7 @@ public: ~ProgramFragmentState(); ProgramFragment *mPF; - void init(Context *rsc, int32_t w, int32_t h); + void init(Context *rsc); void deinit(Context *rsc); ObjectBaseRef<Type> mTextureTypes[ProgramFragment::MAX_TEXTURE]; diff --git a/libs/rs/rsProgramRaster.cpp b/libs/rs/rsProgramRaster.cpp index 13887d1..5b69370 100644 --- a/libs/rs/rsProgramRaster.cpp +++ b/libs/rs/rsProgramRaster.cpp @@ -14,11 +14,17 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -#include "rsProgramRaster.h" - #include <GLES/gl.h> #include <GLES/glext.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGl/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsProgramRaster.h" using namespace android; using namespace android::renderscript; @@ -35,9 +41,8 @@ ProgramRaster::ProgramRaster(Context *rsc, mPointSmooth = pointSmooth; mLineSmooth = lineSmooth; mPointSprite = pointSprite; - - mPointSize = 1.0f; mLineWidth = 1.0f; + mCull = RS_CULL_BACK; } ProgramRaster::~ProgramRaster() @@ -47,21 +52,23 @@ ProgramRaster::~ProgramRaster() void ProgramRaster::setLineWidth(float s) { mLineWidth = s; + mDirty = true; } -void ProgramRaster::setPointSize(float s) +void ProgramRaster::setCullMode(RsCullMode mode) { - mPointSize = s; + mCull = mode; + mDirty = true; } void ProgramRaster::setupGL(const Context *rsc, ProgramRasterState *state) { - if (state->mLast.get() == this) { + if (state->mLast.get() == this && !mDirty) { return; } state->mLast.set(this); + mDirty = false; - glPointSize(mPointSize); if (mPointSmooth) { glEnable(GL_POINT_SMOOTH); } else { @@ -76,23 +83,62 @@ void ProgramRaster::setupGL(const Context *rsc, ProgramRasterState *state) } if (rsc->checkVersion1_1()) { +#ifndef ANDROID_RS_BUILD_FOR_HOST if (mPointSprite) { glEnable(GL_POINT_SPRITE_OES); } else { glDisable(GL_POINT_SPRITE_OES); } +#endif //ANDROID_RS_BUILD_FOR_HOST + } + + switch(mCull) { + case RS_CULL_BACK: + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + break; + case RS_CULL_FRONT: + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); + break; + case RS_CULL_NONE: + glDisable(GL_CULL_FACE); + break; } } void ProgramRaster::setupGL2(const Context *rsc, ProgramRasterState *state) { - if (state->mLast.get() == this) { + if (state->mLast.get() == this && !mDirty) { return; } state->mLast.set(this); + mDirty = false; + + switch(mCull) { + case RS_CULL_BACK: + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + break; + case RS_CULL_FRONT: + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); + break; + case RS_CULL_NONE: + glDisable(GL_CULL_FACE); + break; + } } +void ProgramRaster::serialize(OStream *stream) const +{ +} + +ProgramRaster *ProgramRaster::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} ProgramRasterState::ProgramRasterState() { @@ -102,7 +148,7 @@ ProgramRasterState::~ProgramRasterState() { } -void ProgramRasterState::init(Context *rsc, int32_t w, int32_t h) +void ProgramRasterState::init(Context *rsc) { ProgramRaster *pr = new ProgramRaster(rsc, false, false, false); mDefault.set(pr); @@ -118,7 +164,7 @@ void ProgramRasterState::deinit(Context *rsc) namespace android { namespace renderscript { -RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, RsElement in, RsElement out, +RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, bool pointSmooth, bool lineSmooth, bool pointSprite) @@ -131,16 +177,16 @@ RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, RsElement in, RsElement o return pr; } -void rsi_ProgramRasterSetPointSize(Context * rsc, RsProgramRaster vpr, float s) +void rsi_ProgramRasterSetLineWidth(Context * rsc, RsProgramRaster vpr, float s) { ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); - pr->setPointSize(s); + pr->setLineWidth(s); } -void rsi_ProgramRasterSetLineWidth(Context * rsc, RsProgramRaster vpr, float s) +void rsi_ProgramRasterSetCullMode(Context * rsc, RsProgramRaster vpr, RsCullMode mode) { ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); - pr->setLineWidth(s); + pr->setCullMode(mode); } diff --git a/libs/rs/rsProgramRaster.h b/libs/rs/rsProgramRaster.h index c3a9c90..801ab2a 100644 --- a/libs/rs/rsProgramRaster.h +++ b/libs/rs/rsProgramRaster.h @@ -36,19 +36,19 @@ public: virtual void setupGL(const Context *, ProgramRasterState *); virtual void setupGL2(const Context *, ProgramRasterState *); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_RASTER; } + static ProgramRaster *createFromStream(Context *rsc, IStream *stream); void setLineWidth(float w); - void setPointSize(float s); + void setCullMode(RsCullMode mode); protected: bool mPointSmooth; bool mLineSmooth; bool mPointSprite; - - float mPointSize; float mLineWidth; - - + RsCullMode mCull; }; class ProgramRasterState @@ -56,7 +56,7 @@ class ProgramRasterState public: ProgramRasterState(); ~ProgramRasterState(); - void init(Context *rsc, int32_t w, int32_t h); + void init(Context *rsc); void deinit(Context *rsc); ObjectBaseRef<ProgramRaster> mDefault; diff --git a/libs/rs/rsProgramFragmentStore.cpp b/libs/rs/rsProgramStore.cpp index 8a2157f..e741c0a 100644 --- a/libs/rs/rsProgramFragmentStore.cpp +++ b/libs/rs/rsProgramStore.cpp @@ -14,17 +14,23 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -#include "rsProgramFragmentStore.h" - #include <GLES/gl.h> #include <GLES/glext.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGl/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsProgramStore.h" using namespace android; using namespace android::renderscript; -ProgramFragmentStore::ProgramFragmentStore(Context *rsc) : +ProgramStore::ProgramStore(Context *rsc) : Program(rsc) { mAllocFile = __FILE__; @@ -46,11 +52,11 @@ ProgramFragmentStore::ProgramFragmentStore(Context *rsc) : } -ProgramFragmentStore::~ProgramFragmentStore() +ProgramStore::~ProgramStore() { } -void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState *state) +void ProgramStore::setupGL(const Context *rsc, ProgramStoreState *state) { if (state->mLast.get() == this) { return; @@ -85,7 +91,7 @@ void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState } } -void ProgramFragmentStore::setupGL2(const Context *rsc, ProgramFragmentStoreState *state) +void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state) { if (state->mLast.get() == this) { return; @@ -121,12 +127,23 @@ void ProgramFragmentStore::setupGL2(const Context *rsc, ProgramFragmentStoreStat } -void ProgramFragmentStore::setDitherEnable(bool enable) +void ProgramStore::setDitherEnable(bool enable) { mDitherEnable = enable; } -void ProgramFragmentStore::setDepthFunc(RsDepthFunc func) +void ProgramStore::serialize(OStream *stream) const +{ + +} + +ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + + +void ProgramStore::setDepthFunc(RsDepthFunc func) { mDepthTestEnable = true; @@ -156,12 +173,12 @@ void ProgramFragmentStore::setDepthFunc(RsDepthFunc func) } } -void ProgramFragmentStore::setDepthMask(bool mask) +void ProgramStore::setDepthMask(bool mask) { mDepthWriteEnable = mask; } -void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) +void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) { mBlendEnable = true; if ((src == RS_BLEND_SRC_ONE) && @@ -227,7 +244,7 @@ void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) } } -void ProgramFragmentStore::setColorMask(bool r, bool g, bool b, bool a) +void ProgramStore::setColorMask(bool r, bool g, bool b, bool a) { mColorRWriteEnable = r; mColorGWriteEnable = g; @@ -236,24 +253,24 @@ void ProgramFragmentStore::setColorMask(bool r, bool g, bool b, bool a) } -ProgramFragmentStoreState::ProgramFragmentStoreState() +ProgramStoreState::ProgramStoreState() { mPFS = NULL; } -ProgramFragmentStoreState::~ProgramFragmentStoreState() +ProgramStoreState::~ProgramStoreState() { delete mPFS; } -void ProgramFragmentStoreState::init(Context *rsc, int32_t w, int32_t h) +void ProgramStoreState::init(Context *rsc) { - ProgramFragmentStore *pfs = new ProgramFragmentStore(rsc); + ProgramStore *pfs = new ProgramStore(rsc); mDefault.set(pfs); } -void ProgramFragmentStoreState::deinit(Context *rsc) +void ProgramStoreState::deinit(Context *rsc) { mDefault.clear(); mLast.clear(); @@ -263,42 +280,42 @@ void ProgramFragmentStoreState::deinit(Context *rsc) namespace android { namespace renderscript { -void rsi_ProgramFragmentStoreBegin(Context * rsc, RsElement in, RsElement out) +void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out) { delete rsc->mStateFragmentStore.mPFS; - rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore(rsc); + rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc); } -void rsi_ProgramFragmentStoreDepthFunc(Context *rsc, RsDepthFunc func) +void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func) { rsc->mStateFragmentStore.mPFS->setDepthFunc(func); } -void rsi_ProgramFragmentStoreDepthMask(Context *rsc, bool mask) +void rsi_ProgramStoreDepthMask(Context *rsc, bool mask) { rsc->mStateFragmentStore.mPFS->setDepthMask(mask); } -void rsi_ProgramFragmentStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) +void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) { rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a); } -void rsi_ProgramFragmentStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) +void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) { rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst); } -RsProgramFragmentStore rsi_ProgramFragmentStoreCreate(Context *rsc) +RsProgramStore rsi_ProgramStoreCreate(Context *rsc) { - ProgramFragmentStore *pfs = rsc->mStateFragmentStore.mPFS; + ProgramStore *pfs = rsc->mStateFragmentStore.mPFS; pfs->incUserRef(); rsc->mStateFragmentStore.mPFS = 0; return pfs; } -void rsi_ProgramFragmentStoreDither(Context *rsc, bool enable) +void rsi_ProgramStoreDither(Context *rsc, bool enable) { rsc->mStateFragmentStore.mPFS->setDitherEnable(enable); } diff --git a/libs/rs/rsProgramFragmentStore.h b/libs/rs/rsProgramStore.h index 3412c99..fe8d78e 100644 --- a/libs/rs/rsProgramFragmentStore.h +++ b/libs/rs/rsProgramStore.h @@ -18,21 +18,22 @@ #define ANDROID_RS_PROGRAM_FRAGMENT_STORE_H #include "rsProgram.h" +#include "rsStream.h" // --------------------------------------------------------------------------- namespace android { namespace renderscript { -class ProgramFragmentStoreState; +class ProgramStoreState; -class ProgramFragmentStore : public Program +class ProgramStore : public Program { public: - ProgramFragmentStore(Context *); - virtual ~ProgramFragmentStore(); + ProgramStore(Context *); + virtual ~ProgramStore(); - virtual void setupGL(const Context *, ProgramFragmentStoreState *); - virtual void setupGL2(const Context *, ProgramFragmentStoreState *); + virtual void setupGL(const Context *, ProgramStoreState *); + virtual void setupGL2(const Context *, ProgramStoreState *); void setDepthFunc(RsDepthFunc); void setDepthMask(bool); @@ -42,6 +43,10 @@ public: void setDitherEnable(bool); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_STORE; } + static ProgramStore *createFromStream(Context *rsc, IStream *stream); + protected: bool mDitherEnable; @@ -60,19 +65,19 @@ protected: bool mStencilTestEnable; }; -class ProgramFragmentStoreState +class ProgramStoreState { public: - ProgramFragmentStoreState(); - ~ProgramFragmentStoreState(); - void init(Context *rsc, int32_t w, int32_t h); + ProgramStoreState(); + ~ProgramStoreState(); + void init(Context *rsc); void deinit(Context *rsc); - ObjectBaseRef<ProgramFragmentStore> mDefault; - ObjectBaseRef<ProgramFragmentStore> mLast; + ObjectBaseRef<ProgramStore> mDefault; + ObjectBaseRef<ProgramStore> mLast; - ProgramFragmentStore *mPFS; + ProgramStore *mPFS; }; diff --git a/libs/rs/rsProgramVertex.cpp b/libs/rs/rsProgramVertex.cpp index a2b2df4..60de04a 100644 --- a/libs/rs/rsProgramVertex.cpp +++ b/libs/rs/rsProgramVertex.cpp @@ -14,13 +14,19 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" -#include "rsProgramVertex.h" - #include <GLES/gl.h> #include <GLES/glext.h> #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGL/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + +#include "rsProgramVertex.h" using namespace android; using namespace android::renderscript; @@ -81,9 +87,12 @@ void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (mLightCount) { +#ifndef ANDROID_RS_BUILD_FOR_HOST // GLES Only int v = 0; glEnable(GL_LIGHTING); + glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v); + for (uint32_t ct = 0; ct < mLightCount; ct++) { const Light *l = mLights[ct].get(); glEnable(GL_LIGHT0 + ct); @@ -92,6 +101,7 @@ void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) { glDisable(GL_LIGHT0 + ct); } +#endif //ANDROID_RS_BUILD_FOR_HOST } else { glDisable(GL_LIGHTING); } @@ -128,6 +138,11 @@ void ProgramVertex::createShader() const Element *e = mConstantTypes[ct]->getElement(); for (uint32_t field=0; field < e->getFieldCount(); field++) { const Element *f = e->getField(field); + const char *fn = e->getFieldName(field); + + if (fn[0] == '#') { + continue; + } // Cannot be complex rsAssert(!f->getFieldCount()); @@ -140,7 +155,7 @@ void ProgramVertex::createShader() rsAssert(0); } - mShader.append(e->getFieldName(field)); + mShader.append(fn); mShader.append(";\n"); } } @@ -150,6 +165,11 @@ void ProgramVertex::createShader() const Element *e = mInputElements[ct].get(); for (uint32_t field=0; field < e->getFieldCount(); field++) { const Element *f = e->getField(field); + const char *fn = e->getFieldName(field); + + if (fn[0] == '#') { + continue; + } // Cannot be complex rsAssert(!f->getFieldCount()); @@ -162,17 +182,16 @@ void ProgramVertex::createShader() rsAssert(0); } - mShader.append(e->getFieldName(field)); + mShader.append(fn); mShader.append(";\n"); } } mShader.append(mUserShader); } else { - mShader.append("attribute vec4 ATTRIB_LegacyPosition;\n"); - mShader.append("attribute vec4 ATTRIB_LegacyColor;\n"); - mShader.append("attribute vec3 ATTRIB_LegacyNormal;\n"); - mShader.append("attribute float ATTRIB_LegacyPointSize;\n"); - mShader.append("attribute vec4 ATTRIB_LegacyTexture;\n"); + mShader.append("attribute vec4 ATTRIB_position;\n"); + mShader.append("attribute vec4 ATTRIB_color;\n"); + mShader.append("attribute vec3 ATTRIB_normal;\n"); + mShader.append("attribute vec4 ATTRIB_texture0;\n"); for (uint32_t ct=0; ct < mUniformCount; ct++) { mShader.append("uniform mat4 "); @@ -181,18 +200,15 @@ void ProgramVertex::createShader() } mShader.append("void main() {\n"); - mShader.append(" gl_Position = UNI_MVP * ATTRIB_LegacyPosition;\n"); - mShader.append(" gl_PointSize = ATTRIB_LegacyPointSize;\n"); + mShader.append(" gl_Position = UNI_MVP * ATTRIB_position;\n"); + mShader.append(" gl_PointSize = 1.0;\n"); - mShader.append(" varColor = ATTRIB_LegacyColor;\n"); + mShader.append(" varColor = ATTRIB_color;\n"); if (mTextureMatrixEnable) { - mShader.append(" varTex0 = UNI_TexMatrix * ATTRIB_LegacyTexture;\n"); + mShader.append(" varTex0 = UNI_TexMatrix * ATTRIB_texture0;\n"); } else { - mShader.append(" varTex0 = ATTRIB_LegacyTexture;\n"); + mShader.append(" varTex0 = ATTRIB_texture0;\n"); } - //mShader.append(" pos.x = pos.x / 480.0;\n"); - //mShader.append(" pos.y = pos.y / 800.0;\n"); - //mShader.append(" gl_Position = pos;\n"); mShader.append("}\n"); } } @@ -201,11 +217,10 @@ void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, Shad { //LOGE("sgl2 vtx1 %x", glGetError()); if ((state->mLast.get() == this) && !mDirty) { - //return; + return; } rsc->checkError("ProgramVertex::setupGL2 start"); - glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]); const float *f = static_cast<const float *>(mConstants[0]->getPtr()); @@ -351,6 +366,16 @@ void ProgramVertex::init(Context *rsc) createShader(); } +void ProgramVertex::serialize(OStream *stream) const +{ + +} + +ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + /////////////////////////////////////////////////////////////////////// @@ -362,8 +387,9 @@ ProgramVertexState::~ProgramVertexState() { } -void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) +void ProgramVertexState::init(Context *rsc) { +#ifndef ANDROID_RS_BUILD_FOR_HOST RsElement e = (RsElement) Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); rsi_TypeBegin(rsc, e); @@ -372,23 +398,21 @@ void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) ProgramVertex *pv = new ProgramVertex(rsc, false); Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); + mDefaultAlloc.set(alloc); mDefault.set(pv); pv->init(rsc); pv->bindAllocation(alloc, 0); - color[0] = 1.f; - color[1] = 1.f; - color[2] = 1.f; - color[3] = 1.f; + updateSize(rsc); +#endif //ANDROID_RS_BUILD_FOR_HOST - updateSize(rsc, w, h); } -void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h) +void ProgramVertexState::updateSize(Context *rsc) { Matrix m; - m.loadOrtho(0,w, h,0, -1,1); + m.loadOrtho(0,rsc->getWidth(), rsc->getHeight(),0, -1,1); mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4); m.loadIdentity(); diff --git a/libs/rs/rsProgramVertex.h b/libs/rs/rsProgramVertex.h index 28554cc..1c8b9c8 100644 --- a/libs/rs/rsProgramVertex.h +++ b/libs/rs/rsProgramVertex.h @@ -52,6 +52,9 @@ public: virtual void loadShader(Context *); virtual void init(Context *); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_VERTEX; } + static ProgramVertex *createFromStream(Context *rsc, IStream *stream); protected: uint32_t mLightCount; @@ -71,18 +74,15 @@ public: ProgramVertexState(); ~ProgramVertexState(); - void init(Context *rsc, int32_t w, int32_t h); + void init(Context *rsc); void deinit(Context *rsc); - void updateSize(Context *rsc, int32_t w, int32_t h); + void updateSize(Context *rsc); ObjectBaseRef<ProgramVertex> mDefault; ObjectBaseRef<ProgramVertex> mLast; ObjectBaseRef<Allocation> mDefaultAlloc; ObjectBaseRef<Type> mAllocType; - - - float color[4]; }; diff --git a/libs/rs/rsSampler.cpp b/libs/rs/rsSampler.cpp index 71f508f..f41f295 100644 --- a/libs/rs/rsSampler.cpp +++ b/libs/rs/rsSampler.cpp @@ -14,10 +14,16 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include <GLES/gl.h> #include <GLES/glext.h> - #include "rsContext.h" +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#include <OpenGL/glext.h> +#endif //ANDROID_RS_BUILD_FOR_HOST + #include "rsSampler.h" @@ -94,6 +100,17 @@ void Sampler::unbindFromContext(SamplerState *ss) mBoundSlot = -1; ss->mSamplers[slot].clear(); } + +void Sampler::serialize(OStream *stream) const +{ + +} + +Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) +{ + return NULL; +} + /* void SamplerState::setupGL() { diff --git a/libs/rs/rsSampler.h b/libs/rs/rsSampler.h index 0506081..3786439 100644 --- a/libs/rs/rsSampler.h +++ b/libs/rs/rsSampler.h @@ -46,6 +46,10 @@ public: void bindToContext(SamplerState *, uint32_t slot); void unbindFromContext(SamplerState *); + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_SAMPLER; } + static Sampler *createFromStream(Context *rsc, IStream *stream); + protected: RsSamplerValue mMagFilter; RsSamplerValue mMinFilter; diff --git a/libs/rs/rsScript.cpp b/libs/rs/rsScript.cpp index a33933b..fc22fc3 100644 --- a/libs/rs/rsScript.cpp +++ b/libs/rs/rsScript.cpp @@ -24,19 +24,25 @@ Script::Script(Context *rsc) : ObjectBase(rsc) mAllocFile = __FILE__; mAllocLine = __LINE__; memset(&mEnviroment, 0, sizeof(mEnviroment)); - mEnviroment.mClearColor[0] = 0; - mEnviroment.mClearColor[1] = 0; - mEnviroment.mClearColor[2] = 0; - mEnviroment.mClearColor[3] = 1; - mEnviroment.mClearDepth = 1; - mEnviroment.mClearStencil = 0; - mEnviroment.mIsRoot = false; } Script::~Script() { } +void Script::setVar(uint32_t slot, const void *val, uint32_t len) +{ + int32_t *destPtr = ((int32_t **)mEnviroment.mFieldAddress)[slot]; + if (destPtr) { + //LOGE("setVar f1 %f", ((const float *)destPtr)[0]); + //LOGE("setVar %p %i", destPtr, len); + memcpy(destPtr, val, len); + //LOGE("setVar f2 %f", ((const float *)destPtr)[0]); + } else { + LOGE("Calling setVar on slot = %i which is null", slot); + } +} + namespace android { namespace renderscript { @@ -44,71 +50,62 @@ namespace renderscript { void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { Script *s = static_cast<Script *>(vs); - s->mSlots[slot].set(static_cast<Allocation *>(va)); + Allocation *a = static_cast<Allocation *>(va); + s->mSlots[slot].set(a); + //LOGE("rsi_ScriptBindAllocation %i %p %p", slot, a, a->getPtr()); } -void rsi_ScriptSetClearColor(Context * rsc, RsScript vs, float r, float g, float b, float a) +void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length) { Script *s = static_cast<Script *>(vs); - s->mEnviroment.mClearColor[0] = r; - s->mEnviroment.mClearColor[1] = g; - s->mEnviroment.mClearColor[2] = b; - s->mEnviroment.mClearColor[3] = a; + s->mEnviroment.mTimeZone = timeZone; } -void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, uint32_t length) +void rsi_ScriptSetType(Context * rsc, RsType vt, uint32_t slot, bool writable, const char *name) { - Script *s = static_cast<Script *>(vs); - s->mEnviroment.mTimeZone = timeZone; + ScriptCState *ss = &rsc->mScriptC; + const Type *t = static_cast<const Type *>(vt); + ss->mConstantBufferTypes[slot].set(t); + ss->mSlotWritable[slot] = writable; + LOGE("rsi_ScriptSetType"); } -void rsi_ScriptSetClearDepth(Context * rsc, RsScript vs, float v) +void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) { Script *s = static_cast<Script *>(vs); - s->mEnviroment.mClearDepth = v; + s->Invoke(rsc, slot, NULL, 0); } -void rsi_ScriptSetClearStencil(Context * rsc, RsScript vs, uint32_t v) + +void rsi_ScriptInvokeData(Context *rsc, RsScript vs, uint32_t slot, void *data) { Script *s = static_cast<Script *>(vs); - s->mEnviroment.mClearStencil = v; + s->Invoke(rsc, slot, NULL, 0); } -void rsi_ScriptSetType(Context * rsc, RsType vt, uint32_t slot, bool writable, const char *name) +void rsi_ScriptInvokeV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) { - ScriptCState *ss = &rsc->mScriptC; - const Type *t = static_cast<const Type *>(vt); - ss->mConstantBufferTypes[slot].set(t); - ss->mSlotWritable[slot] = writable; - if (name) { - ss->mSlotNames[slot].setTo(name); - } else { - ss->mSlotNames[slot].setTo(""); - } + Script *s = static_cast<Script *>(vs); + s->Invoke(rsc, slot, data, len); } -void rsi_ScriptSetInvoke(Context *rsc, const char *name, uint32_t slot) +void rsi_ScriptSetVarI(Context *rsc, RsScript vs, uint32_t slot, int value) { - ScriptCState *ss = &rsc->mScriptC; - ss->mInvokableNames[slot] = name; + Script *s = static_cast<Script *>(vs); + s->setVar(slot, &value, sizeof(value)); } -void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot) +void rsi_ScriptSetVarF(Context *rsc, RsScript vs, uint32_t slot, float value) { Script *s = static_cast<Script *>(vs); - if (s->mEnviroment.mInvokables[slot] == NULL) { - rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); - return; - } - s->setupScript(); - s->mEnviroment.mInvokables[slot](); + s->setVar(slot, &value, sizeof(value)); } - -void rsi_ScriptSetRoot(Context * rsc, bool isRoot) +void rsi_ScriptSetVarV(Context *rsc, RsScript vs, uint32_t slot, const void *data, uint32_t len) { - ScriptCState *ss = &rsc->mScriptC; - ss->mScript->mEnviroment.mIsRoot = isRoot; + const float *fp = (const float *)data; + Script *s = static_cast<Script *>(vs); + s->setVar(slot, data, len); } diff --git a/libs/rs/rsScript.h b/libs/rs/rsScript.h index 5f4a536..455ece7 100644 --- a/libs/rs/rsScript.h +++ b/libs/rs/rsScript.h @@ -27,9 +27,9 @@ namespace renderscript { class ProgramVertex; class ProgramFragment; class ProgramRaster; -class ProgramFragmentStore; +class ProgramStore; -#define MAX_SCRIPT_BANKS 16 +#define MAX_SCRIPT_BANKS 32 class Script : public ObjectBase { @@ -39,38 +39,41 @@ public: Script(Context *); virtual ~Script(); - struct Enviroment_t { - bool mIsRoot; - float mClearColor[4]; - float mClearDepth; - uint32_t mClearStencil; - - uint32_t mStartTimeMillis; + int64_t mStartTimeMillis; + int64_t mLastDtTime; const char* mTimeZone; ObjectBaseRef<ProgramVertex> mVertex; ObjectBaseRef<ProgramFragment> mFragment; ObjectBaseRef<ProgramRaster> mRaster; - ObjectBaseRef<ProgramFragmentStore> mFragmentStore; - InvokeFunc_t mInvokables[MAX_SCRIPT_BANKS]; + ObjectBaseRef<ProgramStore> mFragmentStore; + + uint32_t mInvokeFunctionCount; + InvokeFunc_t *mInvokeFunctions; + uint32_t mFieldCount; + void ** mFieldAddress; + char * mScriptText; uint32_t mScriptTextLength; }; Enviroment_t mEnviroment; - uint32_t mCounstantBufferCount; - - ObjectBaseRef<Allocation> mSlots[MAX_SCRIPT_BANKS]; ObjectBaseRef<const Type> mTypes[MAX_SCRIPT_BANKS]; - String8 mSlotNames[MAX_SCRIPT_BANKS]; bool mSlotWritable[MAX_SCRIPT_BANKS]; + void setVar(uint32_t slot, const void *val, uint32_t len); + virtual void runForEach(Context *rsc, + const Allocation * ain, + Allocation * aout, + const void * usr, + const RsScriptCall *sc = NULL) = 0; - virtual void setupScript() = 0; - virtual uint32_t run(Context *, uint32_t launchID) = 0; + virtual void Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) = 0; + virtual void setupScript(Context *rsc) = 0; + virtual uint32_t run(Context *) = 0; }; diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp index f4d2451..7c7b037 100644 --- a/libs/rs/rsScriptC.cpp +++ b/libs/rs/rsScriptC.cpp @@ -17,8 +17,7 @@ #include "rsContext.h" #include "rsScriptC.h" #include "rsMatrix.h" - -#include "acc/acc.h" +#include "../../../external/llvm/libbcc/include/bcc/bcc.h" #include "utils/Timers.h" #include <GLES/gl.h> @@ -37,40 +36,74 @@ ScriptC::ScriptC(Context *rsc) : Script(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; - mAccScript = NULL; + mBccScript = NULL; memset(&mProgram, 0, sizeof(mProgram)); } ScriptC::~ScriptC() { - if (mAccScript) { - accDeleteScript(mAccScript); + if (mBccScript) { + bccDeleteScript(mBccScript); } free(mEnviroment.mScriptText); mEnviroment.mScriptText = NULL; } -void ScriptC::setupScript() +void ScriptC::setupScript(Context *rsc) { - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mProgram.mSlotPointers[ct]) { - *mProgram.mSlotPointers[ct] = mSlots[ct]->getPtr(); + setupGLState(rsc); + mEnviroment.mStartTimeMillis + = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); + + for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { + if (!mSlots[ct].get()) + continue; + void *ptr = mSlots[ct]->getPtr(); + void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; + //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX()); + + //const uint32_t *p32 = (const uint32_t *)ptr; + //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) { + //LOGE(" %i = 0x%08x ", ct2, p32[ct2]); + //} + + if (dest) { + *dest = ptr; + } else { + LOGE("ScriptC::setupScript, NULL var binding address."); } } } - -uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex) +const Allocation *ScriptC::ptrToAllocation(const void *ptr) const { - if (mProgram.mScript == NULL) { - rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); - return 0; + if (!ptr) { + return NULL; } + for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { + if (!mSlots[ct].get()) + continue; + if (mSlots[ct]->getPtr() == ptr) { + return mSlots[ct].get(); + } + } + LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); + return NULL; +} - Context::ScriptTLSStruct * tls = - (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); +Script * ScriptC::setTLS(Script *sc) +{ + Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) + pthread_getspecific(Context::gThreadTLSKey); rsAssert(tls); + Script *old = tls->mScript; + tls->mScript = sc; + return old; +} + +void ScriptC::setupGLState(Context *rsc) +{ if (mEnviroment.mFragmentStore.get()) { rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); } @@ -83,20 +116,214 @@ uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex) if (mEnviroment.mRaster.get()) { rsc->setRaster(mEnviroment.mRaster.get()); } +} - if (launchIndex == 0) { - mEnviroment.mStartTimeMillis - = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); +uint32_t ScriptC::run(Context *rsc) +{ + if (mProgram.mRoot == NULL) { + rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); + return 0; } - setupScript(); + + setupScript(rsc); uint32_t ret = 0; - tls->mScript = this; - ret = mProgram.mScript(launchIndex); - tls->mScript = NULL; + Script * oldTLS = setTLS(this); + //LOGE("ScriptC::run %p", mProgram.mRoot); + ret = mProgram.mRoot(); + setTLS(oldTLS); + //LOGE("ScriptC::run ret %i", ret); return ret; } + +typedef struct { + Context *rsc; + ScriptC *script; + const Allocation * ain; + Allocation * aout; + const void * usr; + + uint32_t mSliceSize; + volatile int mSliceNum; + + const uint8_t *ptrIn; + uint32_t eStrideIn; + uint8_t *ptrOut; + uint32_t eStrideOut; + + uint32_t xStart; + uint32_t xEnd; + uint32_t yStart; + uint32_t yEnd; + uint32_t zStart; + uint32_t zEnd; + uint32_t arrayStart; + uint32_t arrayEnd; + + uint32_t dimX; + uint32_t dimY; + uint32_t dimZ; + uint32_t dimArray; +} MTLaunchStruct; +typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); + +static void wc_xy(void *usr, uint32_t idx) +{ + MTLaunchStruct *mtls = (MTLaunchStruct *)usr; + + while (1) { + uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); + uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; + uint32_t yEnd = yStart + mtls->mSliceSize; + yEnd = rsMin(yEnd, mtls->yEnd); + if (yEnd <= yStart) { + return; + } + + //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); + + for (uint32_t y = yStart; y < yEnd; y++) { + uint32_t offset = mtls->dimX * y; + uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset); + const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset); + + for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) { + ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0); + xPtrIn += mtls->eStrideIn; + xPtrOut += mtls->eStrideOut; + } + } + } + +} + +void ScriptC::runForEach(Context *rsc, + const Allocation * ain, + Allocation * aout, + const void * usr, + const RsScriptCall *sc) +{ + MTLaunchStruct mtls; + memset(&mtls, 0, sizeof(mtls)); + + if (ain) { + mtls.dimX = ain->getType()->getDimX(); + mtls.dimY = ain->getType()->getDimY(); + mtls.dimZ = ain->getType()->getDimZ(); + //mtls.dimArray = ain->getType()->getDimArray(); + } else if (aout) { + mtls.dimX = aout->getType()->getDimX(); + mtls.dimY = aout->getType()->getDimY(); + mtls.dimZ = aout->getType()->getDimZ(); + //mtls.dimArray = aout->getType()->getDimArray(); + } else { + rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); + return; + } + + if (!sc || (sc->xEnd == 0)) { + mtls.xEnd = mtls.dimX; + } else { + rsAssert(sc->xStart < mtls.dimX); + rsAssert(sc->xEnd <= mtls.dimX); + rsAssert(sc->xStart < sc->xEnd); + mtls.xStart = rsMin(mtls.dimX, sc->xStart); + mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); + if (mtls.xStart >= mtls.xEnd) return; + } + + if (!sc || (sc->yEnd == 0)) { + mtls.yEnd = mtls.dimY; + } else { + rsAssert(sc->yStart < mtls.dimY); + rsAssert(sc->yEnd <= mtls.dimY); + rsAssert(sc->yStart < sc->yEnd); + mtls.yStart = rsMin(mtls.dimY, sc->yStart); + mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); + if (mtls.yStart >= mtls.yEnd) return; + } + + mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); + mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); + mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); + mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); + + rsAssert(ain->getType()->getDimZ() == 0); + + setupScript(rsc); + Script * oldTLS = setTLS(this); + + + mtls.rsc = rsc; + mtls.ain = ain; + mtls.aout = aout; + mtls.script = this; + mtls.usr = usr; + mtls.mSliceSize = 10; + mtls.mSliceNum = 0; + + mtls.ptrIn = NULL; + mtls.eStrideIn = 0; + if (ain) { + mtls.ptrIn = (const uint8_t *)ain->getPtr(); + mtls.eStrideIn = ain->getType()->getElementSizeBytes(); + } + + mtls.ptrOut = NULL; + mtls.eStrideOut = 0; + if (aout) { + mtls.ptrOut = (uint8_t *)aout->getPtr(); + mtls.eStrideOut = aout->getType()->getElementSizeBytes(); + } + + + if ((rsc->getWorkerPoolSize() > 1) && + ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) { + + //LOGE("launch 1"); + rsc->launchThreads(wc_xy, &mtls); + //LOGE("launch 2"); + } else { + for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) { + for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) { + for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) { + uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar + + mtls.dimX * mtls.dimY * z + + mtls.dimX * y; + uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset); + const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset); + + for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) { + ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar); + xPtrIn += mtls.eStrideIn; + xPtrOut += mtls.eStrideOut; + } + } + } + } + } + + setTLS(oldTLS); +} + +void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) +{ + //LOGE("rsi_ScriptInvoke %i", slot); + if ((slot >= mEnviroment.mInvokeFunctionCount) || + (mEnviroment.mInvokeFunctions[slot] == NULL)) { + rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); + return; + } + setupScript(rsc); + Script * oldTLS = setTLS(this); + + ((void (*)(const void *, uint32_t)) + mEnviroment.mInvokeFunctions[slot])(data, len); + + setTLS(oldTLS); +} + ScriptCState::ScriptCState() { mScript = NULL; @@ -113,21 +340,25 @@ void ScriptCState::clear() { for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { mConstantBufferTypes[ct].clear(); - mSlotNames[ct].setTo(""); - mInvokableNames[ct].setTo(""); mSlotWritable[ct] = false; } delete mScript; mScript = new ScriptC(NULL); - - mInt32Defines.clear(); - mFloatDefines.clear(); } -static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) +static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) { - const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name); + const ScriptCState::SymbolTable_t *sym; + sym = ScriptCState::lookupSymbol(name); + if (sym) { + return sym->mPtr; + } + sym = ScriptCState::lookupSymbolCL(name); + if (sym) { + return sym->mPtr; + } + sym = ScriptCState::lookupSymbolGL(name); if (sym) { return sym->mPtr; } @@ -137,65 +368,52 @@ static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) void ScriptCState::runCompiler(Context *rsc, ScriptC *s) { - s->mAccScript = accCreateScript(); - String8 tmp; - - rsc->appendNameDefines(&tmp); - appendDecls(&tmp); - appendVarDefines(rsc, &tmp); - appendTypes(rsc, &tmp); - tmp.append("#line 1\n"); - - const char* scriptSource[] = {tmp.string(), s->mEnviroment.mScriptText}; - int scriptLength[] = {tmp.length(), s->mEnviroment.mScriptTextLength} ; - accScriptSource(s->mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); - accRegisterSymbolCallback(s->mAccScript, symbolLookup, NULL); - accCompileScript(s->mAccScript); - accGetScriptLabel(s->mAccScript, "main", (ACCvoid**) &s->mProgram.mScript); - accGetScriptLabel(s->mAccScript, "init", (ACCvoid**) &s->mProgram.mInit); - rsAssert(s->mProgram.mScript); - - if (!s->mProgram.mScript) { - ACCchar buf[4096]; - ACCsizei len; - accGetScriptInfoLog(s->mAccScript, sizeof(buf), &len, buf); - LOGE("%s", buf); - rsc->setError(RS_ERROR_BAD_SCRIPT, "Error compiling user script."); - return; - } + LOGV("ScriptCState::runCompiler "); + + s->mBccScript = bccCreateScript(); + bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength); + bccRegisterSymbolCallback(s->mBccScript, symbolLookup, NULL); + bccCompileScript(s->mBccScript); + bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); + bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); + LOGV("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit); if (s->mProgram.mInit) { s->mProgram.mInit(); } - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mSlotNames[ct].length() > 0) { - accGetScriptLabel(s->mAccScript, - mSlotNames[ct].string(), - (ACCvoid**) &s->mProgram.mSlotPointers[ct]); - } + bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); + if(s->mEnviroment.mInvokeFunctionCount <= 0) + s->mEnviroment.mInvokeFunctions = NULL; + else { + s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); + bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); } - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mInvokableNames[ct].length() > 0) { - accGetScriptLabel(s->mAccScript, - mInvokableNames[ct].string(), - (ACCvoid**) &s->mEnviroment.mInvokables[ct]); - } + bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL); + if(s->mEnviroment.mFieldCount <= 0) + s->mEnviroment.mFieldAddress = NULL; + else { + s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *)); + bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress); } + //for (int ct2=0; ct2 < s->mEnviroment.mFieldCount; ct2++ ) { + //LOGE("Script field %i = %p", ct2, s->mEnviroment.mFieldAddress[ct2]); + //} s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); - s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); + s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); - if (s->mProgram.mScript) { + if (s->mProgram.mRoot) { const static int pragmaMax = 16; - ACCsizei pragmaCount; - ACCchar * str[pragmaMax]; - accGetPragmas(s->mAccScript, &pragmaCount, pragmaMax, &str[0]); + BCCsizei pragmaCount; + BCCchar * str[pragmaMax]; + bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); for (int ct=0; ct < pragmaCount; ct+=2) { + //LOGE("pragme %s %s", str[ct], str[ct+1]); if (!strcmp(str[ct], "version")) { continue; } @@ -208,11 +426,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mVertex.clear(); continue; } - ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); - if (pv != NULL) { - s->mEnviroment.mVertex.set(pv); - continue; - } LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); } @@ -224,11 +437,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mRaster.clear(); continue; } - ProgramRaster * pr = (ProgramRaster *)rsc->lookupName(str[ct+1]); - if (pr != NULL) { - s->mEnviroment.mRaster.set(pr); - continue; - } LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); } @@ -240,11 +448,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mFragment.clear(); continue; } - ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); - if (pf != NULL) { - s->mEnviroment.mFragment.set(pf); - continue; - } LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); } @@ -256,12 +459,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mFragmentStore.clear(); continue; } - ProgramFragmentStore * pfs = - (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); - if (pfs != NULL) { - s->mEnviroment.mFragmentStore.set(pfs); - continue; - } LOGE("Unreconized value %s passed to stateStore", str[ct+1]); } @@ -273,111 +470,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) } } -static void appendElementBody(String8 *s, const Element *e) -{ - s->append(" {\n"); - for (size_t ct2=0; ct2 < e->getFieldCount(); ct2++) { - const Element *c = e->getField(ct2); - s->append(" "); - s->append(c->getCType()); - s->append(" "); - s->append(e->getFieldName(ct2)); - s->append(";\n"); - } - s->append("}"); -} - -void ScriptCState::appendVarDefines(const Context *rsc, String8 *str) -{ - char buf[256]; - if (rsc->props.mLogScripts) { - LOGD("appendVarDefines mInt32Defines.size()=%d mFloatDefines.size()=%d\n", - mInt32Defines.size(), mFloatDefines.size()); - } - for (size_t ct=0; ct < mInt32Defines.size(); ct++) { - str->append("#define "); - str->append(mInt32Defines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); - str->append(buf); - } - for (size_t ct=0; ct < mFloatDefines.size(); ct++) { - str->append("#define "); - str->append(mFloatDefines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); - str->append(buf); - } -} - - - -void ScriptCState::appendTypes(const Context *rsc, String8 *str) -{ - char buf[256]; - String8 tmp; - - str->append("struct vecF32_2_s {float x; float y;};\n"); - str->append("struct vecF32_3_s {float x; float y; float z;};\n"); - str->append("struct vecF32_4_s {float x; float y; float z; float w;};\n"); - str->append("struct vecU8_4_s {char r; char g; char b; char a;};\n"); - str->append("#define vecF32_2_t struct vecF32_2_s\n"); - str->append("#define vecF32_3_t struct vecF32_3_s\n"); - str->append("#define vecF32_4_t struct vecF32_4_s\n"); - str->append("#define vecU8_4_t struct vecU8_4_s\n"); - str->append("#define vecI8_4_t struct vecU8_4_s\n"); - - for (size_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - const Type *t = mConstantBufferTypes[ct].get(); - if (!t) { - continue; - } - const Element *e = t->getElement(); - if (e->getName() && (e->getFieldCount() > 1)) { - String8 s("struct struct_"); - s.append(e->getName()); - s.append(e->getCStructBody()); - s.append(";\n"); - - s.append("#define "); - s.append(e->getName()); - s.append("_t struct struct_"); - s.append(e->getName()); - s.append("\n\n"); - if (rsc->props.mLogScripts) { - LOGV("%s", static_cast<const char*>(s)); - } - str->append(s); - } - - if (mSlotNames[ct].length() > 0) { - String8 s; - if (e->getName()) { - // Use the named struct - s.setTo(e->getName()); - } else { - // create an struct named from the slot. - s.setTo("struct "); - s.append(mSlotNames[ct]); - s.append("_s"); - s.append(e->getCStructBody()); - //appendElementBody(&s, e); - s.append(";\n"); - s.append("struct "); - s.append(mSlotNames[ct]); - s.append("_s"); - } - - s.append(" * "); - s.append(mSlotNames[ct]); - s.append(";\n"); - if (rsc->props.mLogScripts) { - LOGV("%s", static_cast<const char*>(s)); - } - str->append(s); - } - } -} namespace android { @@ -420,7 +512,6 @@ RsScript rsi_ScriptCCreate(Context * rsc) s->setContext(rsc); for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get()); - s->mSlotNames[ct] = ss->mSlotNames[ct]; s->mSlotWritable[ct] = ss->mSlotWritable[ct]; } @@ -428,18 +519,6 @@ RsScript rsi_ScriptCCreate(Context * rsc) return s; } -void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value) -{ - ScriptCState *ss = &rsc->mScriptC; - ss->mFloatDefines.add(String8(name), value); -} - -void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value) -{ - ScriptCState *ss = &rsc->mScriptC; - ss->mInt32Defines.add(String8(name), value); -} - } } diff --git a/libs/rs/rsScriptC.h b/libs/rs/rsScriptC.h index 35abadf..9d09b0b 100644 --- a/libs/rs/rsScriptC.h +++ b/libs/rs/rsScriptC.h @@ -21,9 +21,7 @@ #include "RenderScriptEnv.h" -#include <utils/KeyedVector.h> - -struct ACCscript; +struct BCCscript; // --------------------------------------------------------------------------- namespace android { @@ -34,7 +32,7 @@ namespace renderscript { class ScriptC : public Script { public: - typedef int (*RunScript_t)(uint32_t launchIndex); + typedef int (*RunScript_t)(); typedef void (*VoidFunc_t)(); ScriptC(Context *); @@ -44,18 +42,35 @@ public: int mVersionMajor; int mVersionMinor; - RunScript_t mScript; + RunScript_t mRoot; VoidFunc_t mInit; - - void ** mSlotPointers[MAX_SCRIPT_BANKS]; }; Program_t mProgram; - ACCscript* mAccScript; + BCCscript* mBccScript; + + const Allocation *ptrToAllocation(const void *) const; + + + virtual void Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len); - virtual void setupScript(); - virtual uint32_t run(Context *, uint32_t launchID); + virtual uint32_t run(Context *); + + virtual void runForEach(Context *rsc, + const Allocation * ain, + Allocation * aout, + const void * usr, + const RsScriptCall *sc = NULL); + + virtual void serialize(OStream *stream) const { } + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_SCRIPT_C; } + static Type *createFromStream(Context *rsc, IStream *stream) { return NULL; } + +protected: + void setupScript(Context *); + void setupGLState(Context *); + Script * setTLS(Script *); }; class ScriptCState @@ -67,27 +82,21 @@ public: ScriptC *mScript; ObjectBaseRef<const Type> mConstantBufferTypes[MAX_SCRIPT_BANKS]; - String8 mSlotNames[MAX_SCRIPT_BANKS]; + //String8 mSlotNames[MAX_SCRIPT_BANKS]; bool mSlotWritable[MAX_SCRIPT_BANKS]; - String8 mInvokableNames[MAX_SCRIPT_BANKS]; + //String8 mInvokableNames[MAX_SCRIPT_BANKS]; void clear(); void runCompiler(Context *rsc, ScriptC *s); - void appendVarDefines(const Context *rsc, String8 *str); - void appendTypes(const Context *rsc, String8 *str); struct SymbolTable_t { const char * mName; void * mPtr; - const char * mRet; - const char * mParam; }; - static SymbolTable_t gSyms[]; + //static SymbolTable_t gSyms[]; static const SymbolTable_t * lookupSymbol(const char *); - static void appendDecls(String8 *str); - - KeyedVector<String8,int> mInt32Defines; - KeyedVector<String8,float> mFloatDefines; + static const SymbolTable_t * lookupSymbolCL(const char *); + static const SymbolTable_t * lookupSymbolGL(const char *); }; diff --git a/libs/rs/rsScriptC_Lib.cpp b/libs/rs/rsScriptC_Lib.cpp index 202ca3d..ac32810 100644 --- a/libs/rs/rsScriptC_Lib.cpp +++ b/libs/rs/rsScriptC_Lib.cpp @@ -17,18 +17,10 @@ #include "rsContext.h" #include "rsScriptC.h" #include "rsMatrix.h" -#include "rsNoise.h" #include "acc/acc.h" #include "utils/Timers.h" -#define GL_GLEXT_PROTOTYPES - -#include <GLES/gl.h> -#include <GLES/glext.h> -#include <GLES2/gl2.h> -#include <GLES2/gl2ext.h> - #include <time.h> using namespace android; @@ -39,252 +31,11 @@ using namespace android::renderscript; Context * rsc = tls->mContext; \ ScriptC * sc = (ScriptC *) tls->mScript -typedef struct { - float x; - float y; - float z; -} vec3_t; - -typedef struct { - float x; - float y; - float z; - float w; -} vec4_t; - -typedef struct { - float x; - float y; -} vec2_t; - -////////////////////////////////////////////////////////////////////////////// -// IO routines -////////////////////////////////////////////////////////////////////////////// - -static float SC_loadF(uint32_t bank, uint32_t offset) -{ - GET_TLS(); - const void *vp = sc->mSlots[bank]->getPtr(); - const float *f = static_cast<const float *>(vp); - //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]); - return f[offset]; -} - -static int32_t SC_loadI32(uint32_t bank, uint32_t offset) -{ - GET_TLS(); - const void *vp = sc->mSlots[bank]->getPtr(); - const int32_t *i = static_cast<const int32_t *>(vp); - //LOGE("loadI32 %i %i = %i", bank, offset, t); - return i[offset]; -} - -static float* SC_loadArrayF(uint32_t bank, uint32_t offset) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - float *f = static_cast<float *>(vp); - return f + offset; -} - -static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - int32_t *i = static_cast<int32_t *>(vp); - return i + offset; -} - -static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx) -{ - SimpleMesh *tm = static_cast<SimpleMesh *>(mesh); - void *vp = tm->mVertexBuffers[idx]->getPtr();; - return static_cast<float *>(vp); -} - -static void SC_updateSimpleMesh(RsSimpleMesh mesh) -{ - GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(mesh); - sm->uploadAll(rsc); -} - -static uint32_t SC_loadU32(uint32_t bank, uint32_t offset) -{ - GET_TLS(); - const void *vp = sc->mSlots[bank]->getPtr(); - const uint32_t *i = static_cast<const uint32_t *>(vp); - return i[offset]; -} - -static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v) -{ - GET_TLS(); - const void *vp = sc->mSlots[bank]->getPtr(); - const float *f = static_cast<const float *>(vp); - memcpy(v, &f[offset], sizeof(rsc_Vector4)); -} - -static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m) -{ - GET_TLS(); - const void *vp = sc->mSlots[bank]->getPtr(); - const float *f = static_cast<const float *>(vp); - memcpy(m, &f[offset], sizeof(rsc_Matrix)); -} - - -static void SC_storeF(uint32_t bank, uint32_t offset, float v) -{ - //LOGE("storeF %i %i %f", bank, offset, v); - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - float *f = static_cast<float *>(vp); - f[offset] = v; -} - -static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - int32_t *f = static_cast<int32_t *>(vp); - static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v; -} - -static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - uint32_t *f = static_cast<uint32_t *>(vp); - static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v; -} - -static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - float *f = static_cast<float *>(vp); - memcpy(&f[offset], v, sizeof(rsc_Vector4)); -} - -static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m) -{ - GET_TLS(); - void *vp = sc->mSlots[bank]->getPtr(); - float *f = static_cast<float *>(vp); - memcpy(&f[offset], m, sizeof(rsc_Matrix)); -} - -////////////////////////////////////////////////////////////////////////////// -// Vec3 routines -////////////////////////////////////////////////////////////////////////////// - -static void SC_vec3Norm(vec3_t *v) -{ - float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); - len = 1 / len; - v->x *= len; - v->y *= len; - v->z *= len; -} - -static float SC_vec3Length(const vec3_t *v) -{ - return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); -} - -static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) -{ - dest->x = lhs->x + rhs->x; - dest->y = lhs->y + rhs->y; - dest->z = lhs->z + rhs->z; -} - -static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) -{ - dest->x = lhs->x - rhs->x; - dest->y = lhs->y - rhs->y; - dest->z = lhs->z - rhs->z; -} - -static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) -{ - float x = lhs->y * rhs->z - lhs->z * rhs->y; - float y = lhs->z * rhs->x - lhs->x * rhs->z; - float z = lhs->x * rhs->y - lhs->y * rhs->x; - dest->x = x; - dest->y = y; - dest->z = z; -} - -static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs) -{ - return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z; -} - -static void SC_vec3Scale(vec3_t *lhs, float scale) -{ - lhs->x *= scale; - lhs->y *= scale; - lhs->z *= scale; -} - -////////////////////////////////////////////////////////////////////////////// -// Vec4 routines -////////////////////////////////////////////////////////////////////////////// - -static void SC_vec4Norm(vec4_t *v) -{ - float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); - len = 1 / len; - v->x *= len; - v->y *= len; - v->z *= len; - v->w *= len; -} - -static float SC_vec4Length(const vec4_t *v) -{ - return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); -} - -static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) -{ - dest->x = lhs->x + rhs->x; - dest->y = lhs->y + rhs->y; - dest->z = lhs->z + rhs->z; - dest->w = lhs->w + rhs->w; -} - -static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) -{ - dest->x = lhs->x - rhs->x; - dest->y = lhs->y - rhs->y; - dest->z = lhs->z - rhs->z; - dest->w = lhs->w - rhs->w; -} - -static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs) -{ - return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w; -} - -static void SC_vec4Scale(vec4_t *lhs, float scale) -{ - lhs->x *= scale; - lhs->y *= scale; - lhs->z *= scale; - lhs->w *= scale; -} ////////////////////////////////////////////////////////////////////////////// // Math routines ////////////////////////////////////////////////////////////////////////////// -#define PI 3.1415926f -#define DEG_TO_RAD PI / 180.0f -#define RAD_TO_DEG 180.0f / PI - static float SC_sinf_fast(float x) { const float A = 1.0f / (2.0f * M_PI); @@ -323,6 +74,7 @@ static float SC_cosf_fast(float x) return 0.2215f * (y * fabsf(y) - y) + y; } + static float SC_randf(float max) { float r = (float)rand(); @@ -335,104 +87,20 @@ static float SC_randf2(float min, float max) return r / RAND_MAX * (max - min) + min; } -static int SC_sign(int value) -{ - return (value > 0) - (value < 0); -} - -static float SC_signf(float value) -{ - return (value > 0) - (value < 0); -} - -static float SC_clampf(float amount, float low, float high) +static int SC_randi(int max) { - return amount < low ? low : (amount > high ? high : amount); + return (int)SC_randf(max); } -static int SC_clamp(int amount, int low, int high) +static int SC_randi2(int min, int max) { - return amount < low ? low : (amount > high ? high : amount); + return (int)SC_randf2(min, max); } -static float SC_maxf(float a, float b) +static float SC_frac(float v) { - return a > b ? a : b; -} - -static float SC_minf(float a, float b) -{ - return a < b ? a : b; -} - -static float SC_sqrf(float v) -{ - return v * v; -} - -static int SC_sqr(int v) -{ - return v * v; -} - -static float SC_fracf(float v) -{ - return v - floorf(v); -} - -static float SC_roundf(float v) -{ - return floorf(v + 0.4999999999); -} - -static float SC_distf2(float x1, float y1, float x2, float y2) -{ - float x = x2 - x1; - float y = y2 - y1; - return sqrtf(x * x + y * y); -} - -static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2) -{ - float x = x2 - x1; - float y = y2 - y1; - float z = z2 - z1; - return sqrtf(x * x + y * y + z * z); -} - -static float SC_magf2(float a, float b) -{ - return sqrtf(a * a + b * b); -} - -static float SC_magf3(float a, float b, float c) -{ - return sqrtf(a * a + b * b + c * c); -} - -static float SC_radf(float degrees) -{ - return degrees * DEG_TO_RAD; -} - -static float SC_degf(float radians) -{ - return radians * RAD_TO_DEG; -} - -static float SC_lerpf(float start, float stop, float amount) -{ - return start + (stop - start) * amount; -} - -static float SC_normf(float start, float stop, float value) -{ - return (value - start) / (stop - start); -} - -static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value) -{ - return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart)); + int i = (int)floor(v); + return fmin(v - i, 0x1.fffffep-1f); } ////////////////////////////////////////////////////////////////////////////// @@ -511,880 +179,276 @@ static int32_t SC_year() return timeinfo->tm_year; } -static int32_t SC_uptimeMillis() +static int64_t SC_uptimeMillis() { return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); } -static int32_t SC_startTimeMillis() -{ - GET_TLS(); - return sc->mEnviroment.mStartTimeMillis; -} - -static int32_t SC_elapsedTimeMillis() -{ - GET_TLS(); - return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) - - sc->mEnviroment.mStartTimeMillis; -} - -////////////////////////////////////////////////////////////////////////////// -// Matrix routines -////////////////////////////////////////////////////////////////////////////// - - -static void SC_matrixLoadIdentity(rsc_Matrix *mat) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->loadIdentity(); -} - -static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->load(f); -} - -static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->load(reinterpret_cast<const Matrix *>(newmat)); -} - -static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->loadRotate(rot, x, y, z); -} - -static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->loadScale(x, y, z); -} - -static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->loadTranslate(x, y, z); -} - -static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), - reinterpret_cast<const Matrix *>(rhs)); -} - -static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->multiply(reinterpret_cast<const Matrix *>(rhs)); -} - -static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->rotate(rot, x, y, z); -} - -static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->scale(x, y, z); -} - -static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z) -{ - Matrix *m = reinterpret_cast<Matrix *>(mat); - m->translate(x, y, z); -} - - -static void SC_vec2Rand(float *vec, float maxLen) -{ - float angle = SC_randf(PI * 2); - float len = SC_randf(maxLen); - vec[0] = len * sinf(angle); - vec[1] = len * cosf(angle); -} - - - -////////////////////////////////////////////////////////////////////////////// -// Context -////////////////////////////////////////////////////////////////////////////// - -static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va) -{ - GET_TLS(); - rsi_ProgramBindTexture(rsc, - static_cast<ProgramFragment *>(vpf), - slot, - static_cast<Allocation *>(va)); - -} - -static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs) +static int64_t SC_uptimeNanos() { - GET_TLS(); - rsi_ProgramBindSampler(rsc, - static_cast<ProgramFragment *>(vpf), - slot, - static_cast<Sampler *>(vs)); - + return systemTime(SYSTEM_TIME_MONOTONIC); } -static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs) +static float SC_getDt() { GET_TLS(); - rsi_ContextBindProgramFragmentStore(rsc, pfs); - + int64_t l = sc->mEnviroment.mLastDtTime; + sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC); + return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9; } -static void SC_bindProgramFragment(RsProgramFragment pf) -{ - GET_TLS(); - rsi_ContextBindProgramFragment(rsc, pf); - -} - -static void SC_bindProgramVertex(RsProgramVertex pv) -{ - GET_TLS(); - rsi_ContextBindProgramVertex(rsc, pv); - -} ////////////////////////////////////////////////////////////////////////////// -// VP +// ////////////////////////////////////////////////////////////////////////////// -static void SC_vpLoadModelMatrix(const rsc_Matrix *m) +static uint32_t SC_allocGetDimX(RsAllocation va) { GET_TLS(); - rsc->getVertex()->setModelviewMatrix(m); + const Allocation *a = static_cast<const Allocation *>(va); + //LOGE("SC_allocGetDimX a=%p", a); + //LOGE(" type=%p", a->getType()); + return a->getType()->getDimX(); } -static void SC_vpLoadTextureMatrix(const rsc_Matrix *m) +static uint32_t SC_allocGetDimY(RsAllocation va) { GET_TLS(); - rsc->getVertex()->setTextureMatrix(m); + const Allocation *a = static_cast<const Allocation *>(va); + return a->getType()->getDimY(); } - - -////////////////////////////////////////////////////////////////////////////// -// Drawing -////////////////////////////////////////////////////////////////////////////// - -static void SC_drawLine(float x1, float y1, float z1, - float x2, float y2, float z2) +static uint32_t SC_allocGetDimZ(RsAllocation va) { GET_TLS(); - if (!rsc->setupCheck()) { - return; - } - - float vtx[] = { x1, y1, z1, x2, y2, z2 }; - VertexArray va; - va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); - if (rsc->checkVersion2_0()) { - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - } else { - va.setupGL(rsc, &rsc->mStateVertexArray); - } - - glDrawArrays(GL_LINES, 0, 2); + const Allocation *a = static_cast<const Allocation *>(va); + return a->getType()->getDimZ(); } -static void SC_drawPoint(float x, float y, float z) +static uint32_t SC_allocGetDimLOD(RsAllocation va) { GET_TLS(); - if (!rsc->setupCheck()) { - return; - } - - float vtx[] = { x, y, z }; - - VertexArray va; - va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); - if (rsc->checkVersion2_0()) { - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - } else { - va.setupGL(rsc, &rsc->mStateVertexArray); - } - - glDrawArrays(GL_POINTS, 0, 1); + const Allocation *a = static_cast<const Allocation *>(va); + return a->getType()->getDimLOD(); } -static void SC_drawQuadTexCoords(float x1, float y1, float z1, - float u1, float v1, - float x2, float y2, float z2, - float u2, float v2, - float x3, float y3, float z3, - float u3, float v3, - float x4, float y4, float z4, - float u4, float v4) +static uint32_t SC_allocGetDimFaces(RsAllocation va) { GET_TLS(); - if (!rsc->setupCheck()) { - return; - } - - //LOGE("Quad"); - //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); - //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2); - //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3); - //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4); - - float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4}; - const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4}; - - VertexArray va; - va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx); - va.addLegacy(GL_FLOAT, 2, 8, RS_KIND_TEXTURE, false, (uint32_t)tex); - if (rsc->checkVersion2_0()) { - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - } else { - va.setupGL(rsc, &rsc->mStateVertexArray); - } - - - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + const Allocation *a = static_cast<const Allocation *>(va); + return a->getType()->getDimFaces(); } -static void SC_drawQuad(float x1, float y1, float z1, - float x2, float y2, float z2, - float x3, float y3, float z3, - float x4, float y4, float z4) +const void * SC_getElementAtX(RsAllocation va, uint32_t x) { - SC_drawQuadTexCoords(x1, y1, z1, 0, 1, - x2, y2, z2, 1, 1, - x3, y3, z3, 1, 0, - x4, y4, z4, 0, 0); + const Allocation *a = static_cast<const Allocation *>(va); + const Type *t = a->getType(); + const uint8_t *p = (const uint8_t *)a->getPtr(); + return &p[t->getElementSizeBytes() * x]; } -static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h) +const void * SC_getElementAtXY(RsAllocation va, uint32_t x, uint32_t y) { - GET_TLS(); - ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex()); - rsc->setVertex(rsc->getDefaultProgramVertex()); - //rsc->setupCheck(); - - //GLint crop[4] = {0, h, w, -h}; - - float sh = rsc->getHeight(); - - SC_drawQuad(x, sh - y, z, - x+w, sh - y, z, - x+w, sh - (y+h), z, - x, sh - (y+h), z); - rsc->setVertex((ProgramVertex *)tmp.get()); + const Allocation *a = static_cast<const Allocation *>(va); + const Type *t = a->getType(); + const uint8_t *p = (const uint8_t *)a->getPtr(); + return &p[t->getElementSizeBytes() * (x + y*t->getDimX())]; } -static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h, - float cx0, float cy0, float cx1, float cy1) +const void * SC_getElementAtXYZ(RsAllocation va, uint32_t x, uint32_t y, uint32_t z) { - GET_TLS(); - if (!rsc->setupCheck()) { - return; - } - - GLint crop[4] = {cx0, cy0, cx1, cy1}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); - glDrawTexfOES(x, y, z, w, h); + const Allocation *a = static_cast<const Allocation *>(va); + const Type *t = a->getType(); + const uint8_t *p = (const uint8_t *)a->getPtr(); + return &p[t->getElementSizeBytes() * (x + y*t->getDimX())]; } -static void SC_drawSprite(float x, float y, float z, float w, float h) -{ - GET_TLS(); - float vin[3] = {x, y, z}; - float vout[4]; - - //LOGE("ds in %f %f %f", x, y, z); - rsc->getVertex()->transformToScreen(rsc, vout, vin); - //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]); - vout[0] /= vout[3]; - vout[1] /= vout[3]; - vout[2] /= vout[3]; - - vout[0] *= rsc->getWidth() / 2; - vout[1] *= rsc->getHeight() / 2; - vout[0] += rsc->getWidth() / 2; - vout[1] += rsc->getHeight() / 2; - - vout[0] -= w/2; - vout[1] -= h/2; - - //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]); - // U, V, W, H - SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w); - //rsc->setupCheck(); +static void SC_debugF(const char *s, float f) { + LOGE("%s %f, 0x%08x", s, f, *((int *) (&f))); } - - -static void SC_drawRect(float x1, float y1, - float x2, float y2, float z) -{ - SC_drawQuad(x1, y2, z, - x2, y2, z, - x2, y1, z, - x1, y1, z); -} - -static void SC_drawSimpleMesh(RsSimpleMesh vsm) -{ - GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); - if (!rsc->setupCheck()) { - return; - } - sm->render(rsc); +static void SC_debugFv2(const char *s, float f1, float f2) { + LOGE("%s {%f, %f}", s, f1, f2); } - -static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len) -{ - GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); - if (!rsc->setupCheck()) { - return; - } - sm->renderRange(rsc, start, len); +static void SC_debugFv3(const char *s, float f1, float f2, float f3) { + LOGE("%s {%f, %f, %f}", s, f1, f2, f3); } - - -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// - -static void SC_color(float r, float g, float b, float a) -{ - GET_TLS(); - rsc->mStateVertex.color[0] = r; - rsc->mStateVertex.color[1] = g; - rsc->mStateVertex.color[2] = b; - rsc->mStateVertex.color[3] = a; - if (!rsc->checkVersion2_0()) { - glColor4f(r, g, b, a); - } +static void SC_debugFv4(const char *s, float f1, float f2, float f3, float f4) { + LOGE("%s {%f, %f, %f, %f}", s, f1, f2, f3, f4); } - -static void SC_ambient(float r, float g, float b, float a) -{ - GLfloat params[] = { r, g, b, a }; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params); +static void SC_debugFM4v4(const char *s, const float *f) { + LOGE("%s {%f, %f, %f, %f", s, f[0], f[4], f[8], f[12]); + LOGE("%s %f, %f, %f, %f", s, f[1], f[5], f[9], f[13]); + LOGE("%s %f, %f, %f, %f", s, f[2], f[6], f[10], f[14]); + LOGE("%s %f, %f, %f, %f}", s, f[3], f[7], f[11], f[15]); } - -static void SC_diffuse(float r, float g, float b, float a) -{ - GLfloat params[] = { r, g, b, a }; - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params); +static void SC_debugFM3v3(const char *s, const float *f) { + LOGE("%s {%f, %f, %f", s, f[0], f[3], f[6]); + LOGE("%s %f, %f, %f", s, f[1], f[4], f[7]); + LOGE("%s %f, %f, %f}",s, f[2], f[5], f[8]); } - -static void SC_specular(float r, float g, float b, float a) -{ - GLfloat params[] = { r, g, b, a }; - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params); +static void SC_debugFM2v2(const char *s, const float *f) { + LOGE("%s {%f, %f", s, f[0], f[2]); + LOGE("%s %f, %f}",s, f[1], f[3]); } -static void SC_emission(float r, float g, float b, float a) -{ - GLfloat params[] = { r, g, b, a }; - glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params); +static void SC_debugI32(const char *s, int32_t i) { + LOGE("%s %i 0x%x", s, i, i); } - -static void SC_shininess(float s) -{ - glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s); +static void SC_debugU32(const char *s, uint32_t i) { + LOGE("%s %i 0x%x", s, i, i); } -static void SC_pointAttenuation(float a, float b, float c) -{ - GLfloat params[] = { a, b, c }; - glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params); +static void SC_debugP(const char *s, const void *p) { + LOGE("%s %p", s, p); } -static void SC_hsbToRgb(float h, float s, float b, float* rgb) -{ - float red = 0.0f; - float green = 0.0f; - float blue = 0.0f; - - float x = h; - float y = s; - float z = b; - - float hf = (x - (int) x) * 6.0f; - int ihf = (int) hf; - float f = hf - ihf; - float pv = z * (1.0f - y); - float qv = z * (1.0f - y * f); - float tv = z * (1.0f - y * (1.0f - f)); - - switch (ihf) { - case 0: // Red is the dominant color - red = z; - green = tv; - blue = pv; - break; - case 1: // Green is the dominant color - red = qv; - green = z; - blue = pv; - break; - case 2: - red = pv; - green = z; - blue = tv; - break; - case 3: // Blue is the dominant color - red = pv; - green = qv; - blue = z; - break; - case 4: - red = tv; - green = pv; - blue = z; - break; - case 5: // Red is the dominant color - red = z; - green = pv; - blue = qv; - break; - } - - rgb[0] = red; - rgb[1] = green; - rgb[2] = blue; -} - -static int SC_hsbToAbgr(float h, float s, float b, float a) -{ - float rgb[3]; - SC_hsbToRgb(h, s, b, rgb); - return int(a * 255.0f) << 24 | - int(rgb[2] * 255.0f) << 16 | - int(rgb[1] * 255.0f) << 8 | - int(rgb[0] * 255.0f); -} - -static void SC_hsb(float h, float s, float b, float a) +static uint32_t SC_toClient2(int cmdID, void *data, int len) { GET_TLS(); - float rgb[3]; - SC_hsbToRgb(h, s, b, rgb); - if (rsc->checkVersion2_0()) { - glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a); - } else { - glColor4f(rgb[0], rgb[1], rgb[2], a); - } + //LOGE("SC_toClient %i %i %i", cmdID, len); + return rsc->sendMessageToClient(data, cmdID, len, false); } -static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel) +static uint32_t SC_toClient(int cmdID) { GET_TLS(); - rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel); + //LOGE("SC_toClient %i", cmdID); + return rsc->sendMessageToClient(NULL, cmdID, 0, false); } -static void SC_uploadToBufferObject(RsAllocation va) +static uint32_t SC_toClientBlocking2(int cmdID, void *data, int len) { GET_TLS(); - rsi_AllocationUploadToBufferObject(rsc, va); + //LOGE("SC_toClientBlocking %i %i", cmdID, len); + return rsc->sendMessageToClient(data, cmdID, len, true); } -static void SC_syncToGL(RsAllocation va) +static uint32_t SC_toClientBlocking(int cmdID) { GET_TLS(); - Allocation *a = static_cast<Allocation *>(va); - + //LOGE("SC_toClientBlocking %i", cmdID); + return rsc->sendMessageToClient(NULL, cmdID, 0, true); } -static void SC_ClearColor(float r, float g, float b, float a) +int SC_divsi3(int a, int b) { - //LOGE("c %f %f %f %f", r, g, b, a); - GET_TLS(); - sc->mEnviroment.mClearColor[0] = r; - sc->mEnviroment.mClearColor[1] = g; - sc->mEnviroment.mClearColor[2] = b; - sc->mEnviroment.mClearColor[3] = a; + return a / b; } -static void SC_debugF(const char *s, float f) -{ - LOGE("%s %f", s, f); -} - -static void SC_debugHexF(const char *s, float f) -{ - LOGE("%s 0x%x", s, *((int *) (&f))); -} - -static void SC_debugI32(const char *s, int32_t i) -{ - LOGE("%s %i", s, i); -} - -static void SC_debugHexI32(const char *s, int32_t i) -{ - LOGE("%s 0x%x", s, i); -} - -static uint32_t SC_getWidth() -{ - GET_TLS(); - return rsc->getWidth(); -} - -static uint32_t SC_getHeight() +int SC_getAllocation(const void *ptr) { GET_TLS(); - return rsc->getHeight(); + const Allocation *alloc = sc->ptrToAllocation(ptr); + return (int)alloc; } -static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a) -{ - uint32_t c = 0; - c |= (uint32_t)(r * 255.f + 0.5f); - c |= ((uint32_t)(g * 255.f + 0.5f)) << 8; - c |= ((uint32_t)(b * 255.f + 0.5f)) << 16; - c |= ((uint32_t)(a * 255.f + 0.5f)) << 24; - return c; -} - -static uint32_t SC_colorFloatRGBAto565(float r, float g, float b) -{ - uint32_t ir = (uint32_t)(r * 255.f + 0.5f); - uint32_t ig = (uint32_t)(g * 255.f + 0.5f); - uint32_t ib = (uint32_t)(b * 255.f + 0.5f); - return rs888to565(ir, ig, ib); -} -static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace) +void SC_ForEach(RsScript vs, + RsAllocation vin, + RsAllocation vout, + const void *usr) { GET_TLS(); - return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0); + const Allocation *ain = static_cast<const Allocation *>(vin); + Allocation *aout = static_cast<Allocation *>(vout); + Script *s = static_cast<Script *>(vs); + s->runForEach(rsc, ain, aout, usr); } -static void SC_scriptCall(int scriptID) +void SC_ForEach2(RsScript vs, + RsAllocation vin, + RsAllocation vout, + const void *usr, + const RsScriptCall *call) { GET_TLS(); - rsc->runScript((Script *)scriptID, 0); + const Allocation *ain = static_cast<const Allocation *>(vin); + Allocation *aout = static_cast<Allocation *>(vout); + Script *s = static_cast<Script *>(vs); + s->runForEach(rsc, ain, aout, usr, call); } - ////////////////////////////////////////////////////////////////////////////// // Class implementation ////////////////////////////////////////////////////////////////////////////// -ScriptCState::SymbolTable_t ScriptCState::gSyms[] = { - // IO - { "loadI32", (void *)&SC_loadI32, - "int", "(int, int)" }, - //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" }, - { "loadF", (void *)&SC_loadF, - "float", "(int, int)" }, - { "loadArrayF", (void *)&SC_loadArrayF, - "float*", "(int, int)" }, - { "loadArrayI32", (void *)&SC_loadArrayI32, - "int*", "(int, int)" }, - { "loadVec4", (void *)&SC_loadVec4, - "void", "(int, int, float *)" }, - { "loadMatrix", (void *)&SC_loadMatrix, - "void", "(int, int, float *)" }, - { "storeI32", (void *)&SC_storeI32, - "void", "(int, int, int)" }, - //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" }, - { "storeF", (void *)&SC_storeF, - "void", "(int, int, float)" }, - { "storeVec4", (void *)&SC_storeVec4, - "void", "(int, int, float *)" }, - { "storeMatrix", (void *)&SC_storeMatrix, - "void", "(int, int, float *)" }, - { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF, - "float*", "(int, int)" }, - { "updateSimpleMesh", (void *)&SC_updateSimpleMesh, - "void", "(int)" }, - - // math - { "modf", (void *)&fmod, - "float", "(float, float)" }, - { "abs", (void *)&abs, - "int", "(int)" }, - { "absf", (void *)&fabsf, - "float", "(float)" }, - { "sinf_fast", (void *)&SC_sinf_fast, - "float", "(float)" }, - { "cosf_fast", (void *)&SC_cosf_fast, - "float", "(float)" }, - { "sinf", (void *)&sinf, - "float", "(float)" }, - { "cosf", (void *)&cosf, - "float", "(float)" }, - { "asinf", (void *)&asinf, - "float", "(float)" }, - { "acosf", (void *)&acosf, - "float", "(float)" }, - { "atanf", (void *)&atanf, - "float", "(float)" }, - { "atan2f", (void *)&atan2f, - "float", "(float, float)" }, - { "fabsf", (void *)&fabsf, - "float", "(float)" }, - { "randf", (void *)&SC_randf, - "float", "(float)" }, - { "randf2", (void *)&SC_randf2, - "float", "(float, float)" }, - { "floorf", (void *)&floorf, - "float", "(float)" }, - { "fracf", (void *)&SC_fracf, - "float", "(float)" }, - { "ceilf", (void *)&ceilf, - "float", "(float)" }, - { "roundf", (void *)&SC_roundf, - "float", "(float)" }, - { "expf", (void *)&expf, - "float", "(float)" }, - { "logf", (void *)&logf, - "float", "(float)" }, - { "powf", (void *)&powf, - "float", "(float, float)" }, - { "maxf", (void *)&SC_maxf, - "float", "(float, float)" }, - { "minf", (void *)&SC_minf, - "float", "(float, float)" }, - { "sqrt", (void *)&sqrt, - "int", "(int)" }, - { "sqrtf", (void *)&sqrtf, - "float", "(float)" }, - { "sqr", (void *)&SC_sqr, - "int", "(int)" }, - { "sqrf", (void *)&SC_sqrf, - "float", "(float)" }, - { "sign", (void *)&SC_sign, - "int", "(int)" }, - { "signf", (void *)&SC_signf, - "float", "(float)" }, - { "clamp", (void *)&SC_clamp, - "int", "(int, int, int)" }, - { "clampf", (void *)&SC_clampf, - "float", "(float, float, float)" }, - { "distf2", (void *)&SC_distf2, - "float", "(float, float, float, float)" }, - { "distf3", (void *)&SC_distf3, - "float", "(float, float, float, float, float, float)" }, - { "magf2", (void *)&SC_magf2, - "float", "(float, float)" }, - { "magf3", (void *)&SC_magf3, - "float", "(float, float, float)" }, - { "radf", (void *)&SC_radf, - "float", "(float)" }, - { "degf", (void *)&SC_degf, - "float", "(float)" }, - { "lerpf", (void *)&SC_lerpf, - "float", "(float, float, float)" }, - { "normf", (void *)&SC_normf, - "float", "(float, float, float)" }, - { "mapf", (void *)&SC_mapf, - "float", "(float, float, float, float, float)" }, - { "noisef", (void *)&SC_noisef, - "float", "(float)" }, - { "noisef2", (void *)&SC_noisef2, - "float", "(float, float)" }, - { "noisef3", (void *)&SC_noisef3, - "float", "(float, float, float)" }, - { "turbulencef2", (void *)&SC_turbulencef2, - "float", "(float, float, float)" }, - { "turbulencef3", (void *)&SC_turbulencef3, - "float", "(float, float, float, float)" }, +// llvm name mangling ref +// <builtin-type> ::= v # void +// ::= b # bool +// ::= c # char +// ::= a # signed char +// ::= h # unsigned char +// ::= s # short +// ::= t # unsigned short +// ::= i # int +// ::= j # unsigned int +// ::= l # long +// ::= m # unsigned long +// ::= x # long long, __int64 +// ::= y # unsigned long long, __int64 +// ::= f # float +// ::= d # double + +static ScriptCState::SymbolTable_t gSyms[] = { + { "__divsi3", (void *)&SC_divsi3 }, + + // allocation + { "_Z19rsAllocationGetDimX13rs_allocation", (void *)&SC_allocGetDimX }, + { "_Z19rsAllocationGetDimY13rs_allocation", (void *)&SC_allocGetDimY }, + { "_Z19rsAllocationGetDimZ13rs_allocation", (void *)&SC_allocGetDimZ }, + { "_Z21rsAllocationGetDimLOD13rs_allocation", (void *)&SC_allocGetDimLOD }, + { "_Z23rsAllocationGetDimFaces13rs_allocation", (void *)&SC_allocGetDimFaces }, + { "_Z15rsGetAllocationPKv", (void *)&SC_getAllocation }, + + { "_Z14rsGetElementAt13rs_allocationj", (void *)&SC_getElementAtX }, + { "_Z14rsGetElementAt13rs_allocationjj", (void *)&SC_getElementAtXY }, + { "_Z14rsGetElementAt13rs_allocationjjj", (void *)&SC_getElementAtXYZ }, + + // Debug + { "_Z7rsDebugPKcf", (void *)&SC_debugF }, + { "_Z7rsDebugPKcff", (void *)&SC_debugFv2 }, + { "_Z7rsDebugPKcfff", (void *)&SC_debugFv3 }, + { "_Z7rsDebugPKcffff", (void *)&SC_debugFv4 }, + { "_Z7rsDebugPKcPK12rs_matrix4x4", (void *)&SC_debugFM4v4 }, + { "_Z7rsDebugPKcPK12rs_matrix3x3", (void *)&SC_debugFM3v3 }, + { "_Z7rsDebugPKcPK12rs_matrix2x2", (void *)&SC_debugFM2v2 }, + { "_Z7rsDebugPKci", (void *)&SC_debugI32 }, + { "_Z7rsDebugPKcj", (void *)&SC_debugU32 }, + { "_Z7rsDebugPKcPKv", (void *)&SC_debugP }, + + // RS Math + { "_Z6rsRandi", (void *)&SC_randi }, + { "_Z6rsRandii", (void *)&SC_randi2 }, + { "_Z6rsRandf", (void *)&SC_randf }, + { "_Z6rsRandff", (void *)&SC_randf2 }, + { "_Z6rsFracf", (void *)&SC_frac }, // time - { "second", (void *)&SC_second, - "int", "()" }, - { "minute", (void *)&SC_minute, - "int", "()" }, - { "hour", (void *)&SC_hour, - "int", "()" }, - { "day", (void *)&SC_day, - "int", "()" }, - { "month", (void *)&SC_month, - "int", "()" }, - { "year", (void *)&SC_year, - "int", "()" }, - { "uptimeMillis", (void*)&SC_uptimeMillis, - "int", "()" }, // TODO: use long instead - { "startTimeMillis", (void*)&SC_startTimeMillis, - "int", "()" }, // TODO: use long instead - { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis, - "int", "()" }, // TODO: use long instead - - // matrix - { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity, - "void", "(float *mat)" }, - { "matrixLoadFloat", (void *)&SC_matrixLoadFloat, - "void", "(float *mat, float *f)" }, - { "matrixLoadMat", (void *)&SC_matrixLoadMat, - "void", "(float *mat, float *newmat)" }, - { "matrixLoadRotate", (void *)&SC_matrixLoadRotate, - "void", "(float *mat, float rot, float x, float y, float z)" }, - { "matrixLoadScale", (void *)&SC_matrixLoadScale, - "void", "(float *mat, float x, float y, float z)" }, - { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate, - "void", "(float *mat, float x, float y, float z)" }, - { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply, - "void", "(float *mat, float *lhs, float *rhs)" }, - { "matrixMultiply", (void *)&SC_matrixMultiply, - "void", "(float *mat, float *rhs)" }, - { "matrixRotate", (void *)&SC_matrixRotate, - "void", "(float *mat, float rot, float x, float y, float z)" }, - { "matrixScale", (void *)&SC_matrixScale, - "void", "(float *mat, float x, float y, float z)" }, - { "matrixTranslate", (void *)&SC_matrixTranslate, - "void", "(float *mat, float x, float y, float z)" }, - - // vector - { "vec2Rand", (void *)&SC_vec2Rand, - "void", "(float *vec, float maxLen)" }, - - // vec3 - { "vec3Norm", (void *)&SC_vec3Norm, - "void", "(struct vecF32_3_s *)" }, - { "vec3Length", (void *)&SC_vec3Length, - "float", "(struct vecF32_3_s *)" }, - { "vec3Add", (void *)&SC_vec3Add, - "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, - { "vec3Sub", (void *)&SC_vec3Sub, - "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, - { "vec3Cross", (void *)&SC_vec3Cross, - "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, - { "vec3Dot", (void *)&SC_vec3Dot, - "float", "(struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" }, - { "vec3Scale", (void *)&SC_vec3Scale, - "void", "(struct vecF32_3_s *lhs, float scale)" }, - - // vec4 - { "vec4Norm", (void *)&SC_vec4Norm, - "void", "(struct vecF32_4_s *)" }, - { "vec4Length", (void *)&SC_vec4Length, - "float", "(struct vecF32_4_s *)" }, - { "vec4Add", (void *)&SC_vec4Add, - "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, - { "vec4Sub", (void *)&SC_vec4Sub, - "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, - { "vec4Dot", (void *)&SC_vec4Dot, - "float", "(struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" }, - { "vec4Scale", (void *)&SC_vec4Scale, - "void", "(struct vecF32_4_s *lhs, float scale)" }, - - // context - { "bindProgramFragment", (void *)&SC_bindProgramFragment, - "void", "(int)" }, - { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore, - "void", "(int)" }, - { "bindProgramStore", (void *)&SC_bindProgramFragmentStore, - "void", "(int)" }, - { "bindProgramVertex", (void *)&SC_bindProgramVertex, - "void", "(int)" }, - { "bindSampler", (void *)&SC_bindSampler, - "void", "(int, int, int)" }, - { "bindTexture", (void *)&SC_bindTexture, - "void", "(int, int, int)" }, - - // vp - { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix, - "void", "(void *)" }, - { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix, - "void", "(void *)" }, - - - - // drawing - { "drawRect", (void *)&SC_drawRect, - "void", "(float x1, float y1, float x2, float y2, float z)" }, - { "drawQuad", (void *)&SC_drawQuad, - "void", "(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)" }, - { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords, - "void", "(float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, float x4, float y4, float z4, float u4, float v4)" }, - { "drawSprite", (void *)&SC_drawSprite, - "void", "(float x, float y, float z, float w, float h)" }, - { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace, - "void", "(float x, float y, float z, float w, float h)" }, - { "drawSpriteScreenspaceCropped", (void *)&SC_drawSpriteScreenspaceCropped, - "void", "(float x, float y, float z, float w, float h, float cx0, float cy0, float cx1, float cy1)" }, - { "drawLine", (void *)&SC_drawLine, - "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" }, - { "drawPoint", (void *)&SC_drawPoint, - "void", "(float x1, float y1, float z1)" }, - { "drawSimpleMesh", (void *)&SC_drawSimpleMesh, - "void", "(int ism)" }, - { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange, - "void", "(int ism, int start, int len)" }, - - - // misc - { "pfClearColor", (void *)&SC_ClearColor, - "void", "(float, float, float, float)" }, - { "color", (void *)&SC_color, - "void", "(float, float, float, float)" }, - { "hsb", (void *)&SC_hsb, - "void", "(float, float, float, float)" }, - { "hsbToRgb", (void *)&SC_hsbToRgb, - "void", "(float, float, float, float*)" }, - { "hsbToAbgr", (void *)&SC_hsbToAbgr, - "int", "(float, float, float, float)" }, - { "ambient", (void *)&SC_ambient, - "void", "(float, float, float, float)" }, - { "diffuse", (void *)&SC_diffuse, - "void", "(float, float, float, float)" }, - { "specular", (void *)&SC_specular, - "void", "(float, float, float, float)" }, - { "emission", (void *)&SC_emission, - "void", "(float, float, float, float)" }, - { "shininess", (void *)&SC_shininess, - "void", "(float)" }, - { "pointAttenuation", (void *)&SC_pointAttenuation, - "void", "(float, float, float)" }, - - { "uploadToTexture", (void *)&SC_uploadToTexture, - "void", "(int, int)" }, - { "uploadToBufferObject", (void *)&SC_uploadToBufferObject, - "void", "(int)" }, - - { "syncToGL", (void *)&SC_syncToGL, - "void", "(int)" }, - - { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8, - "int", "(float, float, float, float)" }, - { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565, - "int", "(float, float, float)" }, - - - { "getWidth", (void *)&SC_getWidth, - "int", "()" }, - { "getHeight", (void *)&SC_getHeight, - "int", "()" }, - - { "sendToClient", (void *)&SC_toClient, - "int", "(void *data, int cmdID, int len, int waitForSpace)" }, - - - { "debugF", (void *)&SC_debugF, - "void", "(void *, float)" }, - { "debugI32", (void *)&SC_debugI32, - "void", "(void *, int)" }, - { "debugHexF", (void *)&SC_debugHexF, - "void", "(void *, float)" }, - { "debugHexI32", (void *)&SC_debugHexI32, - "void", "(void *, int)" }, - - { "scriptCall", (void *)&SC_scriptCall, - "void", "(int)" }, - - - { NULL, NULL, NULL, NULL } + { "_Z8rsSecondv", (void *)&SC_second }, + { "_Z8rsMinutev", (void *)&SC_minute }, + { "_Z6rsHourv", (void *)&SC_hour }, + { "_Z5rsDayv", (void *)&SC_day }, + { "_Z7rsMonthv", (void *)&SC_month }, + { "_Z6rsYearv", (void *)&SC_year }, + { "_Z14rsUptimeMillisv", (void*)&SC_uptimeMillis }, + { "_Z13rsUptimeNanosv", (void*)&SC_uptimeNanos }, + { "_Z7rsGetDtv", (void*)&SC_getDt }, + + { "_Z14rsSendToClienti", (void *)&SC_toClient }, + { "_Z14rsSendToClientiPKvj", (void *)&SC_toClient2 }, + { "_Z22rsSendToClientBlockingi", (void *)&SC_toClientBlocking }, + { "_Z22rsSendToClientBlockingiPKvj", (void *)&SC_toClientBlocking2 }, + + { "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach }, + //{ "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach2 }, + +//////////////////////////////////////////////////////////////////// + + //{ "sinf_fast", (void *)&SC_sinf_fast }, + //{ "cosf_fast", (void *)&SC_cosf_fast }, + + { NULL, NULL } }; const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) @@ -1400,17 +464,3 @@ const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) return NULL; } -void ScriptCState::appendDecls(String8 *str) -{ - ScriptCState::SymbolTable_t *syms = gSyms; - while (syms->mPtr) { - str->append(syms->mRet); - str->append(" "); - str->append(syms->mName); - str->append(syms->mParam); - str->append(";\n"); - syms++; - } -} - - diff --git a/libs/rs/rsScriptC_LibCL.cpp b/libs/rs/rsScriptC_LibCL.cpp new file mode 100644 index 0000000..ce8e7b2 --- /dev/null +++ b/libs/rs/rsScriptC_LibCL.cpp @@ -0,0 +1,314 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsContext.h" +#include "rsScriptC.h" + +// Implements rs_cl.rsh + + +using namespace android; +using namespace android::renderscript; + + +static float SC_acospi(float v) { + return acosf(v)/ M_PI; +} + +static float SC_asinpi(float v) { + return asinf(v) / M_PI; +} + +static float SC_atanpi(float v) { + return atanf(v) / M_PI; +} + +static float SC_atan2pi(float y, float x) { + return atan2f(y, x) / M_PI; +} + +static float SC_cospi(float v) { + return cosf(v * M_PI); +} + +static float SC_exp10(float v) { + return pow(10.f, v); + +} + +static float SC_fract(float v, int *iptr) { + int i = (int)floor(v); + iptr[0] = i; + return fmin(v - i, 0x1.fffffep-1f); +} + +static float SC_log2(float v) { + return log10(v) / log10(2.f); +} + +static float SC_pown(float v, int p) { + return powf(v, (float)p); +} + +static float SC_powr(float v, float p) { + return powf(v, p); +} + +float SC_rootn(float v, int r) { + return pow(v, 1.f / r); +} + +float SC_rsqrt(float v) { + return 1.f / sqrtf(v); +} + +float SC_sincos(float v, float *cosptr) { + *cosptr = cosf(v); + return sinf(v); +} + +static float SC_sinpi(float v) { + return sinf(v * M_PI); +} + +static float SC_tanpi(float v) { + return tanf(v * M_PI); +} + + //{ "logb", (void *)& }, + //{ "mad", (void *)& }, + //{ "nan", (void *)& }, + //{ "tgamma", (void *)& }, + +////////////////////////////////////////////////////////////////////////////// +// Integer +////////////////////////////////////////////////////////////////////////////// + + +static uint32_t SC_abs_i32(int32_t v) {return abs(v);} +static uint16_t SC_abs_i16(int16_t v) {return (uint16_t)abs(v);} +static uint8_t SC_abs_i8(int8_t v) {return (uint8_t)abs(v);} + +static uint32_t SC_clz_u32(uint32_t v) {return __builtin_clz(v);} +static uint16_t SC_clz_u16(uint16_t v) {return (uint16_t)__builtin_clz(v);} +static uint8_t SC_clz_u8(uint8_t v) {return (uint8_t)__builtin_clz(v);} +static int32_t SC_clz_i32(int32_t v) {return (int32_t)__builtin_clz((uint32_t)v);} +static int16_t SC_clz_i16(int16_t v) {return (int16_t)__builtin_clz(v);} +static int8_t SC_clz_i8(int8_t v) {return (int8_t)__builtin_clz(v);} + +static uint32_t SC_max_u32(uint32_t v, uint32_t v2) {return rsMax(v, v2);} +static uint16_t SC_max_u16(uint16_t v, uint16_t v2) {return rsMax(v, v2);} +static uint8_t SC_max_u8(uint8_t v, uint8_t v2) {return rsMax(v, v2);} +static int32_t SC_max_i32(int32_t v, int32_t v2) {return rsMax(v, v2);} +static int16_t SC_max_i16(int16_t v, int16_t v2) {return rsMax(v, v2);} +static int8_t SC_max_i8(int8_t v, int8_t v2) {return rsMax(v, v2);} + +static uint32_t SC_min_u32(uint32_t v, uint32_t v2) {return rsMin(v, v2);} +static uint16_t SC_min_u16(uint16_t v, uint16_t v2) {return rsMin(v, v2);} +static uint8_t SC_min_u8(uint8_t v, uint8_t v2) {return rsMin(v, v2);} +static int32_t SC_min_i32(int32_t v, int32_t v2) {return rsMin(v, v2);} +static int16_t SC_min_i16(int16_t v, int16_t v2) {return rsMin(v, v2);} +static int8_t SC_min_i8(int8_t v, int8_t v2) {return rsMin(v, v2);} + +////////////////////////////////////////////////////////////////////////////// +// Float util +////////////////////////////////////////////////////////////////////////////// + +static float SC_clamp_f32(float amount, float low, float high) +{ + return amount < low ? low : (amount > high ? high : amount); +} + +static float SC_degrees(float radians) +{ + return radians * (180.f / M_PI); +} + +static float SC_max_f32(float v, float v2) +{ + return rsMax(v, v2); +} + +static float SC_min_f32(float v, float v2) +{ + return rsMin(v, v2); +} + +static float SC_mix_f32(float start, float stop, float amount) +{ + //LOGE("lerpf %f %f %f", start, stop, amount); + return start + (stop - start) * amount; +} + +static float SC_radians(float degrees) +{ + return degrees * (M_PI / 180.f); +} + +static float SC_step_f32(float edge, float v) +{ + if (v < edge) return 0.f; + return 1.f; +} + +static float SC_sign_f32(float value) +{ + if (value > 0) return 1.f; + if (value < 0) return -1.f; + return value; +} + + + + + +////////////////////////////////////////////////////////////////////////////// +// Class implementation +////////////////////////////////////////////////////////////////////////////// + +// llvm name mangling ref +// <builtin-type> ::= v # void +// ::= b # bool +// ::= c # char +// ::= a # signed char +// ::= h # unsigned char +// ::= s # short +// ::= t # unsigned short +// ::= i # int +// ::= j # unsigned int +// ::= l # long +// ::= m # unsigned long +// ::= x # long long, __int64 +// ::= y # unsigned long long, __int64 +// ::= f # float +// ::= d # double + +static ScriptCState::SymbolTable_t gSyms[] = { + // OpenCL math + { "_Z4acosf", (void *)&acosf }, + { "_Z5acoshf", (void *)&acoshf }, + { "_Z6acospif", (void *)&SC_acospi }, + { "_Z4asinf", (void *)&asinf }, + { "_Z5asinhf", (void *)&asinhf }, + { "_Z6asinpif", (void *)&SC_asinpi }, + { "_Z4atanf", (void *)&atanf }, + { "_Z5atan2f", (void *)&atan2f }, + { "_Z6atanpif", (void *)&SC_atanpi }, + { "_Z7atan2pif", (void *)&SC_atan2pi }, + { "_Z4cbrtf", (void *)&cbrtf }, + { "_Z4ceilf", (void *)&ceilf }, + { "_Z8copysignff", (void *)©signf }, + { "_Z3cosf", (void *)&cosf }, + { "_Z4coshf", (void *)&coshf }, + { "_Z5cospif", (void *)&SC_cospi }, + { "_Z4erfcf", (void *)&erfcf }, + { "_Z3erff", (void *)&erff }, + { "_Z3expf", (void *)&expf }, + { "_Z4exp2f", (void *)&exp2f }, + { "_Z5exp10f", (void *)&SC_exp10 }, + { "_Z5expm1f", (void *)&expm1f }, + { "_Z4fabsf", (void *)&fabsf }, + { "_Z4fdimff", (void *)&fdimf }, + { "_Z5floorf", (void *)&floorf }, + { "_Z3fmafff", (void *)&fmaf }, + { "_Z4fmaxff", (void *)&fmaxf }, + { "_Z4fminff", (void *)&fminf }, // float fmin(float, float) + { "_Z4fmodff", (void *)&fmodf }, + { "_Z5fractfPf", (void *)&SC_fract }, + { "_Z5frexpfPi", (void *)&frexpf }, + { "_Z5hypotff", (void *)&hypotf }, + { "_Z5ilogbf", (void *)&ilogbf }, + { "_Z5ldexpfi", (void *)&ldexpf }, + { "_Z6lgammaf", (void *)&lgammaf }, + { "_Z3logf", (void *)&logf }, + { "_Z4log2f", (void *)&SC_log2 }, + { "_Z5log10f", (void *)&log10f }, + { "_Z5log1pf", (void *)&log1pf }, + //{ "logb", (void *)& }, + //{ "mad", (void *)& }, + { "modf", (void *)&modff }, + //{ "nan", (void *)& }, + { "_Z9nextafterff", (void *)&nextafterf }, + { "_Z3powff", (void *)&powf }, + { "_Z4pownfi", (void *)&SC_pown }, + { "_Z4powrff", (void *)&SC_powr }, + { "_Z9remainderff", (void *)&remainderf }, + { "remquo", (void *)&remquof }, + { "_Z4rintf", (void *)&rintf }, + { "_Z5rootnfi", (void *)&SC_rootn }, + { "_Z5roundf", (void *)&roundf }, + { "_Z5rsqrtf", (void *)&SC_rsqrt }, + { "_Z3sinf", (void *)&sinf }, + { "sincos", (void *)&SC_sincos }, + { "_Z4sinhf", (void *)&sinhf }, + { "_Z5sinpif", (void *)&SC_sinpi }, + { "_Z4sqrtf", (void *)&sqrtf }, + { "_Z3tanf", (void *)&tanf }, + { "_Z4tanhf", (void *)&tanhf }, + { "_Z5tanpif", (void *)&SC_tanpi }, + //{ "tgamma", (void *)& }, + { "_Z5truncf", (void *)&truncf }, + + // OpenCL Int + { "_Z3absi", (void *)&SC_abs_i32 }, + { "_Z3abss", (void *)&SC_abs_i16 }, + { "_Z3absc", (void *)&SC_abs_i8 }, + { "_Z3clzj", (void *)&SC_clz_u32 }, + { "_Z3clzt", (void *)&SC_clz_u16 }, + { "_Z3clzh", (void *)&SC_clz_u8 }, + { "_Z3clzi", (void *)&SC_clz_i32 }, + { "_Z3clzs", (void *)&SC_clz_i16 }, + { "_Z3clzc", (void *)&SC_clz_i8 }, + { "_Z3maxjj", (void *)&SC_max_u32 }, + { "_Z3maxtt", (void *)&SC_max_u16 }, + { "_Z3maxhh", (void *)&SC_max_u8 }, + { "_Z3maxii", (void *)&SC_max_i32 }, + { "_Z3maxss", (void *)&SC_max_i16 }, + { "_Z3maxcc", (void *)&SC_max_i8 }, + { "_Z3minjj", (void *)&SC_min_u32 }, + { "_Z3mintt", (void *)&SC_min_u16 }, + { "_Z3minhh", (void *)&SC_min_u8 }, + { "_Z3minii", (void *)&SC_min_i32 }, + { "_Z3minss", (void *)&SC_min_i16 }, + { "_Z3mincc", (void *)&SC_min_i8 }, + + // OpenCL 6.11.4 + { "_Z5clampfff", (void *)&SC_clamp_f32 }, + { "_Z7degreesf", (void *)&SC_degrees }, + { "_Z3maxff", (void *)&SC_max_f32 }, + { "_Z3minff", (void *)&SC_min_f32 }, + { "_Z3mixfff", (void *)&SC_mix_f32 }, + { "_Z7radiansf", (void *)&SC_radians }, + { "_Z4stepff", (void *)&SC_step_f32 }, + //{ "smoothstep", (void *)& }, + { "_Z4signf", (void *)&SC_sign_f32 }, + + { NULL, NULL } +}; + +const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolCL(const char *sym) +{ + ScriptCState::SymbolTable_t *syms = gSyms; + + while (syms->mPtr) { + if (!strcmp(syms->mName, sym)) { + return syms; + } + syms++; + } + return NULL; +} + diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp new file mode 100644 index 0000000..f5e59534 --- /dev/null +++ b/libs/rs/rsScriptC_LibGL.cpp @@ -0,0 +1,421 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsContext.h" +#include "rsScriptC.h" +#include "rsMatrix.h" + +#include "acc/acc.h" +#include "utils/Timers.h" + +#define GL_GLEXT_PROTOTYPES + +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <time.h> + +using namespace android; +using namespace android::renderscript; + +#define GET_TLS() Context::ScriptTLSStruct * tls = \ + (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ + Context * rsc = tls->mContext; \ + ScriptC * sc = (ScriptC *) tls->mScript + + +////////////////////////////////////////////////////////////////////////////// +// Context +////////////////////////////////////////////////////////////////////////////// + +static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va) +{ + GET_TLS(); + rsi_ProgramBindTexture(rsc, + static_cast<ProgramFragment *>(vpf), + slot, + static_cast<Allocation *>(va)); + +} + +static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs) +{ + GET_TLS(); + rsi_ProgramBindSampler(rsc, + static_cast<ProgramFragment *>(vpf), + slot, + static_cast<Sampler *>(vs)); + +} + +static void SC_bindProgramStore(RsProgramStore pfs) +{ + GET_TLS(); + rsi_ContextBindProgramStore(rsc, pfs); +} + +static void SC_bindProgramFragment(RsProgramFragment pf) +{ + GET_TLS(); + rsi_ContextBindProgramFragment(rsc, pf); +} + +static void SC_bindProgramVertex(RsProgramVertex pv) +{ + GET_TLS(); + rsi_ContextBindProgramVertex(rsc, pv); +} + +static void SC_bindProgramRaster(RsProgramRaster pv) +{ + GET_TLS(); + rsi_ContextBindProgramRaster(rsc, pv); +} + +////////////////////////////////////////////////////////////////////////////// +// VP +////////////////////////////////////////////////////////////////////////////// + +static void SC_vpLoadProjectionMatrix(const rsc_Matrix *m) +{ + GET_TLS(); + rsc->getVertex()->setProjectionMatrix(m); +} + +static void SC_vpLoadModelMatrix(const rsc_Matrix *m) +{ + GET_TLS(); + rsc->getVertex()->setModelviewMatrix(m); +} + +static void SC_vpLoadTextureMatrix(const rsc_Matrix *m) +{ + GET_TLS(); + rsc->getVertex()->setTextureMatrix(m); +} + + +static void SC_pfConstantColor(RsProgramFragment vpf, float r, float g, float b, float a) +{ + //GET_TLS(); + ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); + pf->setConstantColor(r, g, b, a); +} + + +////////////////////////////////////////////////////////////////////////////// +// Drawing +////////////////////////////////////////////////////////////////////////////// + +static void SC_drawQuadTexCoords(float x1, float y1, float z1, + float u1, float v1, + float x2, float y2, float z2, + float u2, float v2, + float x3, float y3, float z3, + float u3, float v3, + float x4, float y4, float z4, + float u4, float v4) +{ + GET_TLS(); + if (!rsc->setupCheck()) { + return; + } + + //LOGE("Quad"); + //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); + //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2); + //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3); + //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4); + + float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4}; + const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4}; + + VertexArray va; + va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "position"); + va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "texture0"); + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + +static void SC_drawQuad(float x1, float y1, float z1, + float x2, float y2, float z2, + float x3, float y3, float z3, + float x4, float y4, float z4) +{ + SC_drawQuadTexCoords(x1, y1, z1, 0, 1, + x2, y2, z2, 1, 1, + x3, y3, z3, 1, 0, + x4, y4, z4, 0, 0); +} + +static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h) +{ + GET_TLS(); + ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex()); + rsc->setVertex(rsc->getDefaultProgramVertex()); + //rsc->setupCheck(); + + //GLint crop[4] = {0, h, w, -h}; + + float sh = rsc->getHeight(); + + SC_drawQuad(x, sh - y, z, + x+w, sh - y, z, + x+w, sh - (y+h), z, + x, sh - (y+h), z); + rsc->setVertex((ProgramVertex *)tmp.get()); +} +/* +static void SC_drawSprite(float x, float y, float z, float w, float h) +{ + GET_TLS(); + float vin[3] = {x, y, z}; + float vout[4]; + + //LOGE("ds in %f %f %f", x, y, z); + rsc->getVertex()->transformToScreen(rsc, vout, vin); + //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]); + vout[0] /= vout[3]; + vout[1] /= vout[3]; + vout[2] /= vout[3]; + + vout[0] *= rsc->getWidth() / 2; + vout[1] *= rsc->getHeight() / 2; + vout[0] += rsc->getWidth() / 2; + vout[1] += rsc->getHeight() / 2; + + vout[0] -= w/2; + vout[1] -= h/2; + + //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]); + + // U, V, W, H + SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w); + //rsc->setupCheck(); +} +*/ + +static void SC_drawRect(float x1, float y1, + float x2, float y2, float z) +{ + //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z); + SC_drawQuad(x1, y2, z, + x2, y2, z, + x2, y1, z, + x1, y1, z); +} + +static void SC_drawMesh(RsMesh vsm) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->render(rsc); +} + +static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitive(rsc, primIndex); +} + +static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitiveRange(rsc, primIndex, start, len); +} + + +////////////////////////////////////////////////////////////////////////////// +// +////////////////////////////////////////////////////////////////////////////// + + +static void SC_color(float r, float g, float b, float a) +{ + GET_TLS(); + ProgramFragment *pf = (ProgramFragment *)rsc->getFragment(); + pf->setConstantColor(r, g, b, a); +} + +static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel) +{ + GET_TLS(); + rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel); +} +static void SC_uploadToTexture(RsAllocation va) +{ + GET_TLS(); + rsi_AllocationUploadToTexture(rsc, va, false, 0); +} + +static void SC_uploadToBufferObject(RsAllocation va) +{ + GET_TLS(); + rsi_AllocationUploadToBufferObject(rsc, va); +} + +static void SC_ClearColor(float r, float g, float b, float a) +{ + GET_TLS(); + if (!rsc->setupCheck()) { + return; + } + + glClearColor(r, g, b, a); + glClear(GL_COLOR_BUFFER_BIT); +} + +static void SC_ClearDepth(float v) +{ + GET_TLS(); + if (!rsc->setupCheck()) { + return; + } + + glClearDepthf(v); + glClear(GL_DEPTH_BUFFER_BIT); +} + +static uint32_t SC_getWidth() +{ + GET_TLS(); + return rsc->getWidth(); +} + +static uint32_t SC_getHeight() +{ + GET_TLS(); + return rsc->getHeight(); +} + +static void SC_DrawTextAlloc(RsAllocation va, int x, int y) +{ + GET_TLS(); + Allocation *alloc = static_cast<Allocation *>(va); + rsc->mStateFont.renderText(alloc, x, y); +} + +static void SC_DrawText(const char *text, int x, int y) +{ + GET_TLS(); + rsc->mStateFont.renderText(text, x, y); +} + +static void SC_BindFont(RsFont font) +{ + GET_TLS(); + rsi_ContextBindFont(rsc, font); +} + +static void SC_FontColor(float r, float g, float b, float a) +{ + GET_TLS(); + rsc->mStateFont.setFontColor(r, g, b, a); +} + +////////////////////////////////////////////////////////////////////////////// +// Class implementation +////////////////////////////////////////////////////////////////////////////// + +// llvm name mangling ref +// <builtin-type> ::= v # void +// ::= b # bool +// ::= c # char +// ::= a # signed char +// ::= h # unsigned char +// ::= s # short +// ::= t # unsigned short +// ::= i # int +// ::= j # unsigned int +// ::= l # long +// ::= m # unsigned long +// ::= x # long long, __int64 +// ::= y # unsigned long long, __int64 +// ::= f # float +// ::= d # double + +static ScriptCState::SymbolTable_t gSyms[] = { + { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment }, + { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore }, + { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex }, + { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster }, + { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler }, + { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture }, + + { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix }, + { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix }, + { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix }, + + { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor }, + + { "_Z11rsgGetWidthv", (void *)&SC_getWidth }, + { "_Z12rsgGetHeightv", (void *)&SC_getHeight }, + + { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 }, + { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture }, + { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject }, + + { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect }, + { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad }, + { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords }, + { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace }, + + { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh }, + { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive }, + { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange }, + + { "_Z13rsgClearColorffff", (void *)&SC_ClearColor }, + { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth }, + + { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText }, + { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc }, + + { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont }, + { "_Z12rsgFontColorffff", (void *)&SC_FontColor }, + + // misc + { "_Z5colorffff", (void *)&SC_color }, + + { NULL, NULL } +}; + +const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym) +{ + ScriptCState::SymbolTable_t *syms = gSyms; + + while (syms->mPtr) { + if (!strcmp(syms->mName, sym)) { + return syms; + } + syms++; + } + return NULL; +} + diff --git a/libs/rs/rsShaderCache.cpp b/libs/rs/rsShaderCache.cpp index 4711d1b..5c073b3 100644 --- a/libs/rs/rsShaderCache.cpp +++ b/libs/rs/rsShaderCache.cpp @@ -14,10 +14,14 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" - #include <GLES/gl.h> #include <GLES2/gl2.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#endif //ANDROID_RS_BUILD_FOR_HOST using namespace android; using namespace android::renderscript; @@ -94,16 +98,10 @@ bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag glAttachShader(pgm, frag->getShaderID()); if (!vtx->isUserProgram()) { - glBindAttribLocation(pgm, 0, "ATTRIB_LegacyPosition"); - glBindAttribLocation(pgm, 1, "ATTRIB_LegacyColor"); - glBindAttribLocation(pgm, 2, "ATTRIB_LegacyNormal"); - glBindAttribLocation(pgm, 3, "ATTRIB_LegacyPointSize"); - glBindAttribLocation(pgm, 4, "ATTRIB_LegacyTexture"); - e->mVtxAttribSlots[RS_KIND_POSITION] = 0; - e->mVtxAttribSlots[RS_KIND_COLOR] = 1; - e->mVtxAttribSlots[RS_KIND_NORMAL] = 2; - e->mVtxAttribSlots[RS_KIND_POINT_SIZE] = 3; - e->mVtxAttribSlots[RS_KIND_TEXTURE] = 4; + glBindAttribLocation(pgm, 0, "ATTRIB_position"); + glBindAttribLocation(pgm, 1, "ATTRIB_color"); + glBindAttribLocation(pgm, 2, "ATTRIB_normal"); + glBindAttribLocation(pgm, 3, "ATTRIB_texture0"); } //LOGE("e2 %x", glGetError()); diff --git a/libs/rs/rsSignal.cpp b/libs/rs/rsSignal.cpp new file mode 100644 index 0000000..9239bfd --- /dev/null +++ b/libs/rs/rsSignal.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsSignal.h" + +using namespace android; +using namespace android::renderscript; + + +Signal::Signal() +{ + mSet = true; +} + +Signal::~Signal() +{ + pthread_mutex_destroy(&mMutex); + pthread_cond_destroy(&mCondition); +} + +bool Signal::init() +{ + int status = pthread_mutex_init(&mMutex, NULL); + if (status) { + LOGE("LocklessFifo mutex init failure"); + return false; + } + + status = pthread_cond_init(&mCondition, NULL); + if (status) { + LOGE("LocklessFifo condition init failure"); + pthread_mutex_destroy(&mMutex); + return false; + } + + return true; +} + +void Signal::set() +{ + int status; + + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i locking for set condition.", status); + return; + } + + mSet = true; + + status = pthread_cond_signal(&mCondition); + if (status) { + LOGE("LocklessCommandFifo: error %i on set condition.", status); + } + + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); + } +} + +void Signal::wait() +{ + int status; + + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i locking for condition.", status); + return; + } + + if (!mSet) { + status = pthread_cond_wait(&mCondition, &mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i waiting on condition.", status); + } + } + mSet = false; + + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i unlocking for condition.", status); + } +} + diff --git a/libs/rs/rsFileA3DDecls.h b/libs/rs/rsSignal.h index 2a08bd3..2e760f1 100644 --- a/libs/rs/rsFileA3DDecls.h +++ b/libs/rs/rsSignal.h @@ -14,31 +14,33 @@ * limitations under the License. */ -#ifndef ANDROID_RS_FILE_A3D_DECLS_H -#define ANDROID_RS_FILE_A3D_DECLS_H +#ifndef ANDROID_RS_SIGNAL_H +#define ANDROID_RS_SIGNAL_H -#define A3D_MAGIC_KEY "Android3D_ff" +#include "rsUtils.h" namespace android { namespace renderscript { - enum A3DChunkType { - CHUNK_EMPTY, +class Signal { +public: + Signal(); + ~Signal(); - CHUNK_ELEMENT, - CHUNK_ELEMENT_SOURCE, - CHUNK_VERTICIES, - CHUNK_MESH, - CHUNK_PRIMITIVE, + bool init(); - CHUNK_LAST - }; + void set(); + void wait(); +protected: + bool mSet; + pthread_mutex_t mMutex; + pthread_cond_t mCondition; +}; } } -#endif //ANDROID_RS_FILE_A3D_H - +#endif diff --git a/libs/rs/rsSimpleMesh.cpp b/libs/rs/rsSimpleMesh.cpp deleted file mode 100644 index 53ce5cd..0000000 --- a/libs/rs/rsSimpleMesh.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2009 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. - */ - -#include "rsContext.h" - -using namespace android; -using namespace android::renderscript; - -#include <GLES/gl.h> -#include <GLES/glext.h> - -SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc) -{ - mAllocFile = __FILE__; - mAllocLine = __LINE__; -} - -SimpleMesh::~SimpleMesh() -{ - delete[] mVertexTypes; - delete[] mVertexBuffers; -} - -void SimpleMesh::render(Context *rsc) const -{ - if (mPrimitiveType.get()) { - renderRange(rsc, 0, mPrimitiveType->getDimX()); - return; - } - - if (mIndexType.get()) { - renderRange(rsc, 0, mIndexType->getDimX()); - return; - } - - renderRange(rsc, 0, mVertexTypes[0]->getDimX()); -} - -void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const -{ - if (len < 1) { - return; - } - - rsc->checkError("SimpleMesh::renderRange 1"); - VertexArray va; - if (rsc->checkVersion2_0()) { - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - mVertexBuffers[ct]->uploadCheck(rsc); - va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); - mVertexTypes[ct]->enableGLVertexBuffer2(&va); - } - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - } else { - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - mVertexBuffers[ct]->uploadCheck(rsc); - va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); - mVertexTypes[ct]->enableGLVertexBuffer(&va); - } - va.setupGL(rsc, 0); - } - - rsc->checkError("SimpleMesh::renderRange 2"); - if (mIndexType.get()) { - mIndexBuffer->uploadCheck(rsc); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); - glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); - } else { - glDrawArrays(mGLPrimitive, start, len); - } - - rsc->checkError("SimpleMesh::renderRange"); -} - -void SimpleMesh::uploadAll(Context *rsc) -{ - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - if (mVertexBuffers[ct].get()) { - mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); - } - } - if (mIndexBuffer.get()) { - mIndexBuffer->deferedUploadToBufferObject(rsc); - } - if (mPrimitiveBuffer.get()) { - mPrimitiveBuffer->deferedUploadToBufferObject(rsc); - } - rsc->checkError("SimpleMesh::uploadAll"); -} - - -SimpleMeshContext::SimpleMeshContext() -{ -} - -SimpleMeshContext::~SimpleMeshContext() -{ -} - - -namespace android { -namespace renderscript { - - -RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType) -{ - SimpleMesh *sm = new SimpleMesh(rsc); - sm->incUserRef(); - - sm->mIndexType.set((const Type *)idx); - sm->mPrimitiveType.set((const Type *)prim); - - sm->mVertexTypeCount = vtxCount; - sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount]; - sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; - for (uint32_t ct=0; ct < vtxCount; ct++) { - sm->mVertexTypes[ct].set((const Type *)vtx[ct]); - } - - sm->mPrimitive = (RsPrimitive)primType; - switch(sm->mPrimitive) { - case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break; - case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break; - case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break; - case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break; - } - return sm; -} - -void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - rsAssert(slot < sm->mVertexTypeCount); - - sm->mVertexBuffers[slot].set((Allocation *)va); -} - -void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - sm->mIndexBuffer.set((Allocation *)va); -} - -void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - sm->mPrimitiveBuffer.set((Allocation *)va); -} - - - - -}} - diff --git a/libs/rs/rsSimpleMesh.h b/libs/rs/rsSimpleMesh.h deleted file mode 100644 index 6defbda..0000000 --- a/libs/rs/rsSimpleMesh.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2009 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. - */ - -#ifndef ANDROID_RS_SIMPLE_MESH_H -#define ANDROID_RS_SIMPLE_MESH_H - - -#include "RenderScript.h" - -// --------------------------------------------------------------------------- -namespace android { -namespace renderscript { - - -// An element is a group of Components that occupies one cell in a structure. -class SimpleMesh : public ObjectBase -{ -public: - SimpleMesh(Context *); - ~SimpleMesh(); - - ObjectBaseRef<const Type> mIndexType; - ObjectBaseRef<const Type> mPrimitiveType; - ObjectBaseRef<const Type> *mVertexTypes; - uint32_t mVertexTypeCount; - - ObjectBaseRef<Allocation> mIndexBuffer; - ObjectBaseRef<Allocation> mPrimitiveBuffer; - ObjectBaseRef<Allocation> *mVertexBuffers; - - RsPrimitive mPrimitive; - uint32_t mGLPrimitive; - - - void render(Context *) const; - void renderRange(Context *, uint32_t start, uint32_t len) const; - void uploadAll(Context *); - - -protected: -}; - -class SimpleMeshContext -{ -public: - SimpleMeshContext(); - ~SimpleMeshContext(); - - -}; - - -} -} -#endif //ANDROID_RS_SIMPLE_MESH_H - diff --git a/libs/rs/rsStream.cpp b/libs/rs/rsStream.cpp new file mode 100644 index 0000000..68241fa --- /dev/null +++ b/libs/rs/rsStream.cpp @@ -0,0 +1,131 @@ + +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_BUILD_FOR_HOST +#include "rsContext.h" +#else +#include "rsContextHostStub.h" +#endif + +#include "rsStream.h" + +using namespace android; +using namespace android::renderscript; + +IStream::IStream(const uint8_t *buf, bool use64) +{ + mData = buf; + mPos = 0; + mUse64 = use64; +} + +void IStream::loadByteArray(void *dest, size_t numBytes) +{ + memcpy(dest, mData + mPos, numBytes); + mPos += numBytes; +} + +uint64_t IStream::loadOffset() +{ + uint64_t tmp; + if (mUse64) { + mPos = (mPos + 7) & (~7); + tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0]; + mPos += sizeof(uint64_t); + return tmp; + } + return loadU32(); +} + +void IStream::loadString(String8 *s) +{ + uint32_t len = loadU32(); + s->setTo((const char *)&mData[mPos], len); + mPos += len; +} + + +// Output stream implementation + +OStream::OStream(uint64_t len, bool use64) +{ + mData = (uint8_t*)malloc(len); + mLength = len; + mPos = 0; + mUse64 = use64; +} + +OStream::~OStream() +{ + free(mData); +} + +void OStream::addByteArray(const void *src, size_t numBytes) +{ + // We need to potentially grow more than once if the number of byes we write is substantial + while(mPos + numBytes >= mLength) { + growSize(); + } + memcpy(mData + mPos, src, numBytes); + mPos += numBytes; +} + +void OStream::addOffset(uint64_t v) +{ + if (mUse64) { + mPos = (mPos + 7) & (~7); + if(mPos + sizeof(v) >= mLength) { + growSize(); + } + mData[mPos++] = (uint8_t)(v & 0xff); + mData[mPos++] = (uint8_t)((v >> 8) & 0xff); + mData[mPos++] = (uint8_t)((v >> 16) & 0xff); + mData[mPos++] = (uint8_t)((v >> 24) & 0xff); + mData[mPos++] = (uint8_t)((v >> 32) & 0xff); + mData[mPos++] = (uint8_t)((v >> 40) & 0xff); + mData[mPos++] = (uint8_t)((v >> 48) & 0xff); + mData[mPos++] = (uint8_t)((v >> 56) & 0xff); + } + else { + addU32(v); + } +} + +void OStream::addString(String8 *s) +{ + uint32_t len = s->size(); + addU32(len); + if(mPos + len*sizeof(char) >= mLength) { + growSize(); + } + char *stringData = reinterpret_cast<char *>(&mData[mPos]); + for(uint32_t i = 0; i < len; i ++) { + stringData[i] = s->string()[i]; + } + mPos += len*sizeof(char); +} + +void OStream::growSize() +{ + uint8_t *newData = (uint8_t*)malloc(mLength*2); + memcpy(newData, mData, mLength*sizeof(uint8_t)); + mLength = mLength * 2; + free(mData); + mData = newData; +} + + diff --git a/libs/rs/rsStream.h b/libs/rs/rsStream.h new file mode 100644 index 0000000..d401cd1 --- /dev/null +++ b/libs/rs/rsStream.h @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_STREAM_H +#define ANDROID_RS_STREAM_H + +#include <utils/String8.h> +#include <stdio.h> + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + +class IStream +{ +public: + IStream(const uint8_t *, bool use64); + + float loadF() { + mPos = (mPos + 3) & (~3); + float tmp = reinterpret_cast<const float *>(&mData[mPos])[0]; + mPos += sizeof(float); + return tmp; + } + int32_t loadI32() { + mPos = (mPos + 3) & (~3); + int32_t tmp = reinterpret_cast<const int32_t *>(&mData[mPos])[0]; + mPos += sizeof(int32_t); + return tmp; + } + uint32_t loadU32() { + mPos = (mPos + 3) & (~3); + uint32_t tmp = reinterpret_cast<const uint32_t *>(&mData[mPos])[0]; + mPos += sizeof(uint32_t); + return tmp; + } + uint16_t loadU16() { + mPos = (mPos + 1) & (~1); + uint16_t tmp = reinterpret_cast<const uint16_t *>(&mData[mPos])[0]; + mPos += sizeof(uint16_t); + return tmp; + } + inline uint8_t loadU8() { + uint8_t tmp = reinterpret_cast<const uint8_t *>(&mData[mPos])[0]; + mPos += sizeof(uint8_t); + return tmp; + } + void loadByteArray(void *dest, size_t numBytes); + uint64_t loadOffset(); + void loadString(String8 *s); + uint64_t getPos() const { + return mPos; + } + void reset(uint64_t pos) { + mPos = pos; + } + void reset() { + mPos = 0; + } + + const uint8_t * getPtr() const { + return mData; + } +protected: + const uint8_t * mData; + uint64_t mPos; + bool mUse64; +}; + +class OStream +{ +public: + OStream(uint64_t length, bool use64); + ~OStream(); + + void align(uint32_t bytes) { + mPos = (mPos + (bytes - 1)) & (~(bytes - 1)); + if(mPos >= mLength) { + growSize(); + } + } + + void addF(float v) { + uint32_t uintV = *reinterpret_cast<uint32_t*> (&v); + addU32(uintV); + } + void addI32(int32_t v) { + mPos = (mPos + 3) & (~3); + if(mPos + sizeof(v) >= mLength) { + growSize(); + } + mData[mPos++] = (uint8_t)(v & 0xff); + mData[mPos++] = (uint8_t)((v >> 8) & 0xff); + mData[mPos++] = (uint8_t)((v >> 16) & 0xff); + mData[mPos++] = (uint8_t)((v >> 24) & 0xff); + } + void addU32(uint32_t v) { + mPos = (mPos + 3) & (~3); + if(mPos + sizeof(v) >= mLength) { + growSize(); + } + mData[mPos++] = (uint8_t)(v & 0xff); + mData[mPos++] = (uint8_t)((v >> 8) & 0xff); + mData[mPos++] = (uint8_t)((v >> 16) & 0xff); + mData[mPos++] = (uint8_t)((v >> 24) & 0xff); + } + void addU16(uint16_t v) { + mPos = (mPos + 1) & (~1); + if(mPos + sizeof(v) >= mLength) { + growSize(); + } + mData[mPos++] = (uint8_t)(v & 0xff); + mData[mPos++] = (uint8_t)(v >> 8); + } + inline void addU8(uint8_t v) { + if(mPos + 1 >= mLength) { + growSize(); + } + reinterpret_cast<uint8_t *>(&mData[mPos])[0] = v; + mPos ++; + } + void addByteArray(const void *src, size_t numBytes); + void addOffset(uint64_t v); + void addString(String8 *s); + uint64_t getPos() const { + return mPos; + } + void reset(uint64_t pos) { + mPos = pos; + } + void reset() { + mPos = 0; + } + const uint8_t * getPtr() const { + return mData; + } +protected: + void growSize(); + uint8_t * mData; + uint64_t mLength; + uint64_t mPos; + bool mUse64; +}; + + +} // renderscript +} // android +#endif //ANDROID_RS_STREAM_H + + diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp index c09e979..79cfd41 100644 --- a/libs/rs/rsType.cpp +++ b/libs/rs/rsType.cpp @@ -14,8 +14,13 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" #include <GLES/gl.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#endif using namespace android; using namespace android::renderscript; @@ -84,7 +89,9 @@ void Type::compute() mLODCount = 1; } if (mLODCount != oldLODCount) { - delete [] mLODs; + if(mLODs){ + delete [] mLODs; + } mLODs = new LOD[mLODCount]; } @@ -138,136 +145,31 @@ uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) co void Type::makeGLComponents() { + if(getElement()->getFieldCount() >= RS_MAX_ATTRIBS) { + return; + } + uint32_t userNum = 0; for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) { const Component &c = getElement()->getField(ct)->getComponent(); - switch(c.getKind()) { - case RS_KIND_USER: - mGL.mUser[userNum].size = c.getVectorSize(); - mGL.mUser[userNum].offset = mElement->getFieldOffsetBytes(ct); - mGL.mUser[userNum].type = c.getGLType(); - mGL.mUser[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); - mGL.mUser[userNum].name.setTo(getElement()->getFieldName(ct)); - userNum ++; - break; - - case RS_KIND_POSITION: - rsAssert(mGL.mVtx.size == 0); - mGL.mVtx.size = c.getVectorSize(); - mGL.mVtx.offset = mElement->getFieldOffsetBytes(ct); - mGL.mVtx.type = c.getGLType(); - mGL.mVtx.normalized = false; - mGL.mVtx.name.setTo("Position"); - break; - - case RS_KIND_COLOR: - rsAssert(mGL.mColor.size == 0); - mGL.mColor.size = c.getVectorSize(); - mGL.mColor.offset = mElement->getFieldOffsetBytes(ct); - mGL.mColor.type = c.getGLType(); - mGL.mColor.normalized = c.getType() != RS_TYPE_FLOAT_32; - mGL.mColor.name.setTo("Color"); - break; - - case RS_KIND_NORMAL: - rsAssert(mGL.mNorm.size == 0); - mGL.mNorm.size = c.getVectorSize(); - mGL.mNorm.offset = mElement->getFieldOffsetBytes(ct); - mGL.mNorm.type = c.getGLType(); - mGL.mNorm.normalized = false; - mGL.mNorm.name.setTo("Normal"); - break; - - case RS_KIND_TEXTURE: - rsAssert(mGL.mTex.size == 0); - mGL.mTex.size = c.getVectorSize(); - mGL.mTex.offset = mElement->getFieldOffsetBytes(ct); - mGL.mTex.type = c.getGLType(); - mGL.mTex.normalized = false; - mGL.mTex.name.setTo("Texture"); - break; - - case RS_KIND_POINT_SIZE: - rsAssert(!mGL.mPointSize.size); - mGL.mPointSize.size = c.getVectorSize(); - mGL.mPointSize.offset = mElement->getFieldOffsetBytes(ct); - mGL.mPointSize.type = c.getGLType(); - mGL.mPointSize.normalized = false; - mGL.mPointSize.name.setTo("PointSize"); - break; - - default: - break; - } + mAttribs[userNum].size = c.getVectorSize(); + mAttribs[userNum].offset = mElement->getFieldOffsetBytes(ct); + mAttribs[userNum].type = c.getGLType(); + mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); + mAttribs[userNum].name.setTo(getElement()->getFieldName(ct)); + userNum ++; } } -void Type::enableGLVertexBuffer(VertexArray *va) const -{ - // Note: We are only going to enable buffers and never disable them - // here. The reason is more than one Allocation may be used as a vertex - // source. So we cannot disable arrays that may have been in use by - // another allocation. - - uint32_t stride = mElement->getSizeBytes(); - if (mGL.mVtx.size) { - va->addLegacy(mGL.mVtx.type, - mGL.mVtx.size, - stride, - RS_KIND_POSITION, - false, - mGL.mVtx.offset); - } - - if (mGL.mNorm.size) { - va->addLegacy(mGL.mNorm.type, - 3, - stride, - RS_KIND_NORMAL, - false, - mGL.mNorm.offset); - } - - if (mGL.mColor.size) { - va->addLegacy(mGL.mColor.type, - mGL.mColor.size, - stride, - RS_KIND_COLOR, - true, - mGL.mColor.offset); - } - - if (mGL.mTex.size) { - va->addLegacy(mGL.mTex.type, - mGL.mTex.size, - stride, - RS_KIND_TEXTURE, - false, - mGL.mTex.offset); - } - if (mGL.mPointSize.size) { - va->addLegacy(mGL.mPointSize.type, - 1, - stride, - RS_KIND_POINT_SIZE, - false, - mGL.mPointSize.offset); - } - -} - -void Type::enableGLVertexBuffer2(VertexArray *va) const +void Type::enableGLVertexBuffer(VertexArray *va) const { - // Do legacy buffers - enableGLVertexBuffer(va); - uint32_t stride = mElement->getSizeBytes(); for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { - if (mGL.mUser[ct].size) { - va->addUser(mGL.mUser[ct], stride); + if (mAttribs[ct].size) { + va->add(mAttribs[ct], stride); } } } @@ -283,6 +185,57 @@ void Type::dumpLOGV(const char *prefix) const mElement->dumpLOGV(buf); } +void Type::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + mElement->serialize(stream); + + stream->addU32(mDimX); + stream->addU32(mDimY); + stream->addU32(mDimZ); + + stream->addU8((uint8_t)(mDimLOD ? 1 : 0)); + stream->addU8((uint8_t)(mFaces ? 1 : 0)); +} + +Type *Type::createFromStream(Context *rsc, IStream *stream) +{ + // First make sure we are reading the correct object + RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); + if(classID != RS_A3D_CLASS_ID_TYPE) { + LOGE("type loading skipped due to invalid class id\n"); + return NULL; + } + + String8 name; + stream->loadString(&name); + + Element *elem = Element::createFromStream(rsc, stream); + if(!elem) { + return NULL; + } + + Type *type = new Type(rsc); + type->mDimX = stream->loadU32(); + type->mDimY = stream->loadU32(); + type->mDimZ = stream->loadU32(); + + uint8_t temp = stream->loadU8(); + type->mDimLOD = temp != 0; + + temp = stream->loadU8(); + type->mFaces = temp != 0; + + type->setElement(elem); + + return type; +} + bool Type::getIsNp2() const { uint32_t x = getDimX(); @@ -391,6 +344,22 @@ RsType rsi_TypeCreate(Context *rsc) return st; } +void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize) +{ + rsAssert(typeDataSize == 6); + // Pack the data in the follofing way mDimX; mDimY; mDimZ; + // mDimLOD; mDimFaces; mElement; into typeData + Type *t = static_cast<Type *>(type); + + (*typeData++) = t->getDimX(); + (*typeData++) = t->getDimY(); + (*typeData++) = t->getDimZ(); + (*typeData++) = t->getDimLOD(); + (*typeData++) = t->getDimFaces() ? 1 : 0; + (*typeData++) = (uint32_t)t->getElement(); + +} + } } diff --git a/libs/rs/rsType.h b/libs/rs/rsType.h index c25577c..5b51e20 100644 --- a/libs/rs/rsType.h +++ b/libs/rs/rsType.h @@ -71,9 +71,11 @@ public: void compute(); void enableGLVertexBuffer(class VertexArray *) const; - void enableGLVertexBuffer2(class VertexArray *) const; void dumpLOGV(const char *prefix) const; + virtual void serialize(OStream *stream) const; + virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_TYPE; } + static Type *createFromStream(Context *rsc, IStream *stream); protected: struct LOD { @@ -112,15 +114,7 @@ protected: LOD *mLODs; uint32_t mLODCount; - struct GLState_t { - VertexArray::Attrib mUser[RS_MAX_ATTRIBS]; - VertexArray::Attrib mVtx; - VertexArray::Attrib mNorm; - VertexArray::Attrib mColor; - VertexArray::Attrib mTex; - VertexArray::Attrib mPointSize; - }; - GLState_t mGL; + VertexArray::Attrib mAttribs[RS_MAX_ATTRIBS]; void makeGLComponents(); private: diff --git a/libs/rs/rsUtils.h b/libs/rs/rsUtils.h index 07f8933..17feb22 100644 --- a/libs/rs/rsUtils.h +++ b/libs/rs/rsUtils.h @@ -19,15 +19,23 @@ #define LOG_NDEBUG 0 #define LOG_TAG "RenderScript" + #include <utils/Log.h> -#include <utils/Vector.h> -#include <utils/KeyedVector.h> + +#include "rsStream.h" + #include <utils/String8.h> +#include <utils/Vector.h> + #include <stdlib.h> #include <pthread.h> #include <time.h> +#include <cutils/atomic.h> +#ifndef ANDROID_RS_BUILD_FOR_HOST #include <EGL/egl.h> +#endif + #include <math.h> #include "RenderScript.h" @@ -41,6 +49,26 @@ namespace renderscript { #define rsAssert(v) while(0) #endif +typedef float rsvF_2 __attribute__ ((vector_size (8))); +typedef float rsvF_4 __attribute__ ((vector_size (16))); +typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4))); + +union float2 { + rsvF_2 v; + float f[2]; +}; + +union float4 { + rsvF_4 v; + float f[4]; +}; + +union uchar4 { + rsvU8_4 v; + uint8_t f[4]; + uint32_t packed; +}; + template<typename T> T rsMin(T in1, T in2) { diff --git a/libs/rs/rsVertexArray.cpp b/libs/rs/rsVertexArray.cpp index 6c2002d..001927c 100644 --- a/libs/rs/rsVertexArray.cpp +++ b/libs/rs/rsVertexArray.cpp @@ -14,10 +14,15 @@ * limitations under the License. */ +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" - #include <GLES/gl.h> #include <GLES2/gl2.h> +#else +#include "rsContextHostStub.h" +#include <OpenGL/gl.h> +#endif + using namespace android; using namespace android::renderscript; @@ -39,6 +44,7 @@ void VertexArray::clearAll() mAttribs[ct].clear(); } mActiveBuffer = 0; + mActivePointer = NULL; mCount = 0; } @@ -50,12 +56,12 @@ VertexArray::Attrib::Attrib() void VertexArray::Attrib::set(const Attrib &a) { buffer = a.buffer; + ptr = a.ptr; offset = a.offset; type = a.type; size = a.size; stride = a.stride; normalized = a.normalized; - kind = RS_KIND_USER; name.setTo(a.name); } @@ -66,6 +72,7 @@ void VertexArray::Attrib::clear() type = 0; size = 0; stride = 0; + ptr = NULL; normalized = false; name.setTo(""); } @@ -75,138 +82,84 @@ void VertexArray::clear(uint32_t n) mAttribs[n].clear(); } -void VertexArray::addUser(const Attrib &a, uint32_t stride) +void VertexArray::add(const Attrib &a, uint32_t stride) { - assert(mCount < RS_MAX_ATTRIBS); + rsAssert(mCount < RS_MAX_ATTRIBS); mAttribs[mCount].set(a); mAttribs[mCount].buffer = mActiveBuffer; + mAttribs[mCount].ptr = mActivePointer; mAttribs[mCount].stride = stride; - mAttribs[mCount].kind = RS_KIND_USER; mCount ++; } -void VertexArray::addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset) +void VertexArray::add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name) { - assert(mCount < RS_MAX_ATTRIBS); + rsAssert(mCount < RS_MAX_ATTRIBS); mAttribs[mCount].clear(); mAttribs[mCount].type = type; mAttribs[mCount].size = size; mAttribs[mCount].offset = offset; mAttribs[mCount].normalized = normalized; - mAttribs[mCount].buffer = mActiveBuffer; mAttribs[mCount].stride = stride; - mAttribs[mCount].kind = kind; + mAttribs[mCount].name.setTo(name); + + mAttribs[mCount].buffer = mActiveBuffer; + mAttribs[mCount].ptr = mActivePointer; mCount ++; } void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { - LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x kind=%i stride=0x%x norm=%i offset=0x%x", idx, slot, + LOGE("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot, mAttribs[idx].name.string(), mAttribs[idx].buffer, + mAttribs[idx].ptr, mAttribs[idx].size, mAttribs[idx].type, - mAttribs[idx].kind, mAttribs[idx].stride, mAttribs[idx].normalized, mAttribs[idx].offset); } -void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const -{ - glClientActiveTexture(GL_TEXTURE0); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_POINT_SIZE_ARRAY_OES); - - for (uint32_t ct=0; ct < mCount; ct++) { - switch(mAttribs[ct].kind) { - case RS_KIND_POSITION: - //logAttrib(POSITION); - glEnableClientState(GL_VERTEX_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glVertexPointer(mAttribs[ct].size, - mAttribs[ct].type, - mAttribs[ct].stride, - (void *)mAttribs[ct].offset); - break; - - case RS_KIND_NORMAL: - //logAttrib(NORMAL); - glEnableClientState(GL_NORMAL_ARRAY); - rsAssert(mAttribs[ct].size == 3); - glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glNormalPointer(mAttribs[ct].type, - mAttribs[ct].stride, - (void *)mAttribs[ct].offset); - break; - - case RS_KIND_COLOR: - //logAttrib(COLOR); - glEnableClientState(GL_COLOR_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glColorPointer(mAttribs[ct].size, - mAttribs[ct].type, - mAttribs[ct].stride, - (void *)mAttribs[ct].offset); - break; - - case RS_KIND_TEXTURE: - //logAttrib(TEXTURE); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glTexCoordPointer(mAttribs[ct].size, - mAttribs[ct].type, - mAttribs[ct].stride, - (void *)mAttribs[ct].offset); - break; - - case RS_KIND_POINT_SIZE: - //logAttrib(POINT_SIZE); - glEnableClientState(GL_POINT_SIZE_ARRAY_OES); - glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glPointSizePointerOES(mAttribs[ct].type, - mAttribs[ct].stride, - (void *)mAttribs[ct].offset); - break; - - default: - rsAssert(0); - } - } - - rsc->checkError("VertexArray::setupGL"); -} - void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const { rsc->checkError("VertexArray::setupGL2 start"); - for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) { + for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) { glDisableVertexAttribArray(ct); } rsc->checkError("VertexArray::setupGL2 disabled"); for (uint32_t ct=0; ct < mCount; ct++) { uint32_t slot = 0; + + if (mAttribs[ct].name[0] == '#') { + continue; + } + if (sc->isUserVertexProgram()) { slot = sc->vtxAttribSlot(ct); } else { - if (mAttribs[ct].kind == RS_KIND_USER) { + if (mAttribs[ct].name == "position") { + slot = 0; + } else if (mAttribs[ct].name == "color") { + slot = 1; + } else if (mAttribs[ct].name == "normal") { + slot = 2; + } else if (mAttribs[ct].name == "texture0") { + slot = 3; + } else { continue; } - slot = sc->vtxAttribSlot(mAttribs[ct].kind); } //logAttrib(ct, slot); glEnableVertexAttribArray(slot); glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); - glVertexAttribPointer(slot, mAttribs[ct].size, mAttribs[ct].type, mAttribs[ct].normalized, mAttribs[ct].stride, - (void *)mAttribs[ct].offset); + mAttribs[ct].ptr + mAttribs[ct].offset); } state->mLastEnableCount = mCount; rsc->checkError("VertexArray::setupGL2 done"); diff --git a/libs/rs/rsVertexArray.h b/libs/rs/rsVertexArray.h index 3904cb6..7c609b2 100644 --- a/libs/rs/rsVertexArray.h +++ b/libs/rs/rsVertexArray.h @@ -37,13 +37,13 @@ public: class Attrib { public: uint32_t buffer; + const uint8_t * ptr; uint32_t offset; uint32_t type; uint32_t size; uint32_t stride; bool normalized; String8 name; - RsDataKind kind; Attrib(); void set(const Attrib &); @@ -52,9 +52,18 @@ public: void clearAll(); - void setActiveBuffer(uint32_t id) {mActiveBuffer = id;} - void addUser(const Attrib &, uint32_t stride); - void addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset); + void setActiveBuffer(uint32_t id) { + mActiveBuffer = id; + mActivePointer = NULL; + } + void setActiveBuffer(const void *ptr) { + mActiveBuffer = 0; + mActivePointer = (const uint8_t *)ptr; + } + + void add(const Attrib &, uint32_t stride); + //void addLegacy(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset); + void add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name); void setupGL(const Context *rsc, class VertexArrayState *) const; void setupGL2(const Context *rsc, class VertexArrayState *, ShaderCache *) const; @@ -63,6 +72,7 @@ public: protected: void clear(uint32_t index); uint32_t mActiveBuffer; + const uint8_t * mActivePointer; uint32_t mCount; Attrib mAttribs[RS_MAX_ATTRIBS]; diff --git a/libs/rs/rsg_ScriptJavaClass.cpp b/libs/rs/rsg_ScriptJavaClass.cpp index cee9f52..0169b98 100644 --- a/libs/rs/rsg_ScriptJavaClass.cpp +++ b/libs/rs/rsg_ScriptJavaClass.cpp @@ -7,8 +7,12 @@ struct Element; struct ElementField { + // An Element Field is a combination of an Element with a name assigned. + const char *name; Element *e; + + ElementField(const char *n, Element *_e) { name = n; e = _e; @@ -20,12 +24,21 @@ struct ElementField { }; struct Element { + // An Element can take one of two forms. + // 1: Basic. It contains a single basic type and vector size. + // 2: Complex. It contains a list of fields with names. Each field + // will in turn be another element. + ElementField *fields; - size_t fieldCount; + size_t fieldCount; // If field count is 0, the element is a Basic type. const char *name; bool generated; + // The basic data type from RenderScript.h RsDataType compType; + + // The vector size of the data type for float2, float3, .... + // Allowed sizes are 2,3,4,8,16 uint32_t compVectorSize; Element() { diff --git a/libs/rs/scriptc/rs_cl.rsh b/libs/rs/scriptc/rs_cl.rsh new file mode 100644 index 0000000..64844a4 --- /dev/null +++ b/libs/rs/scriptc/rs_cl.rsh @@ -0,0 +1,785 @@ +#ifndef __RS_CL_RSH__ +#define __RS_CL_RSH__ + +#define M_PI 3.14159265358979323846264338327950288f /* pi */ + + +// Conversions +#define CVT_FUNC_2(typeout, typein) \ +static typeout##2 __attribute__((overloadable)) convert_##typeout##2(typein##2 v) { \ + typeout##2 r = {(typeout)v.x, (typeout)v.y}; \ + return r; \ +} \ +static typeout##3 __attribute__((overloadable)) convert_##typeout##3(typein##3 v) { \ + typeout##3 r = {(typeout)v.x, (typeout)v.y, (typeout)v.z}; \ + return r; \ +} \ +static typeout##4 __attribute__((overloadable)) convert_##typeout##4(typein##4 v) { \ + typeout##4 r = {(typeout)v.x, (typeout)v.y, (typeout)v.z, (typeout)v.w}; \ + return r; \ +} + +#define CVT_FUNC(type) CVT_FUNC_2(type, uchar) \ + CVT_FUNC_2(type, char) \ + CVT_FUNC_2(type, ushort) \ + CVT_FUNC_2(type, short) \ + CVT_FUNC_2(type, int) \ + CVT_FUNC_2(type, uint) \ + CVT_FUNC_2(type, float) + +CVT_FUNC(char) +CVT_FUNC(uchar) +CVT_FUNC(short) +CVT_FUNC(ushort) +CVT_FUNC(int) +CVT_FUNC(uint) +CVT_FUNC(float) + + + +// Float ops, 6.11.2 + +#define DEF_FUNC_1(fnc) \ +static float2 __attribute__((overloadable)) fnc(float2 v) { \ + float2 r; \ + r.x = fnc(v.x); \ + r.y = fnc(v.y); \ + return r; \ +} \ +static float3 __attribute__((overloadable)) fnc(float3 v) { \ + float3 r; \ + r.x = fnc(v.x); \ + r.y = fnc(v.y); \ + r.z = fnc(v.z); \ + return r; \ +} \ +static float4 __attribute__((overloadable)) fnc(float4 v) { \ + float4 r; \ + r.x = fnc(v.x); \ + r.y = fnc(v.y); \ + r.z = fnc(v.z); \ + r.w = fnc(v.w); \ + return r; \ +} + +#define DEF_FUNC_2(fnc) \ +static float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \ + float2 r; \ + r.x = fnc(v1.x, v2.x); \ + r.y = fnc(v1.y, v2.y); \ + return r; \ +} \ +static float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \ + float3 r; \ + r.x = fnc(v1.x, v2.x); \ + r.y = fnc(v1.y, v2.y); \ + r.z = fnc(v1.z, v2.z); \ + return r; \ +} \ +static float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \ + float4 r; \ + r.x = fnc(v1.x, v2.x); \ + r.y = fnc(v1.y, v2.y); \ + r.z = fnc(v1.z, v2.z); \ + r.w = fnc(v1.w, v2.z); \ + return r; \ +} + +#define DEF_FUNC_2F(fnc) \ +static float2 __attribute__((overloadable)) fnc(float2 v1, float v2) { \ + float2 r; \ + r.x = fnc(v1.x, v2); \ + r.y = fnc(v1.y, v2); \ + return r; \ +} \ +static float3 __attribute__((overloadable)) fnc(float3 v1, float v2) { \ + float3 r; \ + r.x = fnc(v1.x, v2); \ + r.y = fnc(v1.y, v2); \ + r.z = fnc(v1.z, v2); \ + return r; \ +} \ +static float4 __attribute__((overloadable)) fnc(float4 v1, float v2) { \ + float4 r; \ + r.x = fnc(v1.x, v2); \ + r.y = fnc(v1.y, v2); \ + r.z = fnc(v1.z, v2); \ + r.w = fnc(v1.w, v2); \ + return r; \ +} + + +extern float __attribute__((overloadable)) acos(float); +DEF_FUNC_1(acos) + +extern float __attribute__((overloadable)) acosh(float); +DEF_FUNC_1(acosh) + +static float __attribute__((overloadable)) acospi(float v) { + return acos(v) / M_PI; +} +DEF_FUNC_1(acospi) + +extern float __attribute__((overloadable)) asin(float); +DEF_FUNC_1(asin) + +extern float __attribute__((overloadable)) asinh(float); +DEF_FUNC_1(asinh) + +static float __attribute__((overloadable)) asinpi(float v) { + return asin(v) / M_PI; +} +DEF_FUNC_1(asinpi) + +extern float __attribute__((overloadable)) atan(float); +DEF_FUNC_1(atan) + +extern float __attribute__((overloadable)) atan2(float, float); +DEF_FUNC_2(atan2) + +extern float __attribute__((overloadable)) atanh(float); +DEF_FUNC_1(atanh) + +static float __attribute__((overloadable)) atanpi(float v) { + return atan(v) / M_PI; +} +DEF_FUNC_1(atanpi) + +static float __attribute__((overloadable)) atan2pi(float y, float x) { + return atan2(y, x) / M_PI; +} +DEF_FUNC_2(atan2pi) + +extern float __attribute__((overloadable)) cbrt(float); +DEF_FUNC_1(cbrt) + +extern float __attribute__((overloadable)) ceil(float); +DEF_FUNC_1(ceil) + +extern float __attribute__((overloadable)) copysign(float, float); +DEF_FUNC_2(copysign) + +extern float __attribute__((overloadable)) cos(float); +DEF_FUNC_1(cos) + +extern float __attribute__((overloadable)) cosh(float); +DEF_FUNC_1(cosh) + +static float __attribute__((overloadable)) cospi(float v) { + return cos(v * M_PI); +} +DEF_FUNC_1(cospi) + +extern float __attribute__((overloadable)) erfc(float); +DEF_FUNC_1(erfc) + +extern float __attribute__((overloadable)) erf(float); +DEF_FUNC_1(erf) + +extern float __attribute__((overloadable)) exp(float); +DEF_FUNC_1(exp) + +extern float __attribute__((overloadable)) exp2(float); +DEF_FUNC_1(exp2) + +extern float __attribute__((overloadable)) pow(float, float); +static float __attribute__((overloadable)) exp10(float v) { + return pow(10.f, v); +} +DEF_FUNC_1(exp10) + +extern float __attribute__((overloadable)) expm1(float); +DEF_FUNC_1(expm1) + +extern float __attribute__((overloadable)) fabs(float); +DEF_FUNC_1(fabs) + +extern float __attribute__((overloadable)) fdim(float, float); +DEF_FUNC_2(fdim) + +extern float __attribute__((overloadable)) floor(float); +DEF_FUNC_1(floor) + +extern float __attribute__((overloadable)) fma(float, float, float); +extern float2 __attribute__((overloadable)) fma(float2, float2, float2); +extern float3 __attribute__((overloadable)) fma(float3, float3, float3); +extern float4 __attribute__((overloadable)) fma(float4, float4, float4); + +extern float __attribute__((overloadable)) fmax(float, float); +DEF_FUNC_2(fmax); +DEF_FUNC_2F(fmax); + +extern float __attribute__((overloadable)) fmin(float, float); +DEF_FUNC_2(fmin); +DEF_FUNC_2F(fmin); + +extern float __attribute__((overloadable)) fmod(float, float); +DEF_FUNC_2(fmod) + +static float __attribute__((overloadable)) fract(float v, float *iptr) { + int i = (int)floor(v); + iptr[0] = i; + return fmin(v - i, 0x1.fffffep-1f); +} +static float2 __attribute__((overloadable)) fract(float2 v, float2 *iptr) { + float t[2]; + float2 r; + r.x = fract(v.x, &t[0]); + r.y = fract(v.y, &t[1]); + iptr[0] = t[0]; + iptr[1] = t[1]; + return r; +} +static float3 __attribute__((overloadable)) fract(float3 v, float3 *iptr) { + float t[3]; + float3 r; + r.x = fract(v.x, &t[0]); + r.y = fract(v.y, &t[1]); + r.z = fract(v.z, &t[2]); + iptr[0] = t[0]; + iptr[1] = t[1]; + iptr[2] = t[2]; + return r; +} +static float4 __attribute__((overloadable)) fract(float4 v, float4 *iptr) { + float t[4]; + float4 r; + r.x = fract(v.x, &t[0]); + r.y = fract(v.y, &t[1]); + r.z = fract(v.z, &t[2]); + r.w = fract(v.w, &t[3]); + iptr[0] = t[0]; + iptr[1] = t[1]; + iptr[2] = t[2]; + iptr[3] = t[3]; + return r; +} + +extern float __attribute__((overloadable)) frexp(float, float *); +extern float2 __attribute__((overloadable)) frexp(float2, float2 *); +extern float3 __attribute__((overloadable)) frexp(float3, float3 *); +extern float4 __attribute__((overloadable)) frexp(float4, float4 *); + +extern float __attribute__((overloadable)) hypot(float, float); +DEF_FUNC_2(hypot) + +extern int __attribute__((overloadable)) ilogb(float); +DEF_FUNC_1(ilogb) + +extern float __attribute__((overloadable)) ldexp(float, int); +extern float2 __attribute__((overloadable)) ldexp(float2, int2); +extern float3 __attribute__((overloadable)) ldexp(float3, int3); +extern float4 __attribute__((overloadable)) ldexp(float4, int4); +extern float2 __attribute__((overloadable)) ldexp(float2, int); +extern float3 __attribute__((overloadable)) ldexp(float3, int); +extern float4 __attribute__((overloadable)) ldexp(float4, int); + +extern float __attribute__((overloadable)) lgamma(float); +DEF_FUNC_1(lgamma) +extern float __attribute__((overloadable)) lgamma(float, float *); +extern float2 __attribute__((overloadable)) lgamma(float2, float2 *); +extern float3 __attribute__((overloadable)) lgamma(float3, float3 *); +extern float4 __attribute__((overloadable)) lgamma(float4, float4 *); + +extern float __attribute__((overloadable)) log(float); +DEF_FUNC_1(log) + + +extern float __attribute__((overloadable)) log10(float); +DEF_FUNC_1(log10) + +static float __attribute__((overloadable)) log2(float v) { + return log10(v) / log10(2.f); +} +DEF_FUNC_1(log2) + +extern float __attribute__((overloadable)) log1p(float); +DEF_FUNC_1(log1p) + +extern float __attribute__((overloadable)) logb(float); +DEF_FUNC_1(logb) + +extern float __attribute__((overloadable)) mad(float, float, float); +extern float2 __attribute__((overloadable)) mad(float2, float2, float2); +extern float3 __attribute__((overloadable)) mad(float3, float3, float3); +extern float4 __attribute__((overloadable)) mad(float4, float4, float4); + +extern float __attribute__((overloadable)) modf(float, float *); +extern float2 __attribute__((overloadable)) modf(float2, float2 *); +extern float3 __attribute__((overloadable)) modf(float3, float3 *); +extern float4 __attribute__((overloadable)) modf(float4, float4 *); + +//extern float __attribute__((overloadable)) nan(uint); + +extern float __attribute__((overloadable)) nextafter(float, float); +DEF_FUNC_2(nextafter) + +DEF_FUNC_2(pow) + +static float __attribute__((overloadable)) pown(float v, int p) { + return pow(v, (float)p); +} +static float2 __attribute__((overloadable)) pown(float2 v, int2 p) { + return pow(v, (float2)p); +} +static float3 __attribute__((overloadable)) pown(float3 v, int3 p) { + return pow(v, (float3)p); +} +static float4 __attribute__((overloadable)) pown(float4 v, int4 p) { + return pow(v, (float4)p); +} + +static float __attribute__((overloadable)) powr(float v, float p) { + return pow(v, p); +} +static float2 __attribute__((overloadable)) powr(float2 v, float2 p) { + return pow(v, p); +} +static float3 __attribute__((overloadable)) powr(float3 v, float3 p) { + return pow(v, p); +} +static float4 __attribute__((overloadable)) powr(float4 v, float4 p) { + return pow(v, p); +} + +extern float __attribute__((overloadable)) remainder(float, float); +DEF_FUNC_2(remainder) + +extern float __attribute__((overloadable)) remquo(float, float, float *); +extern float2 __attribute__((overloadable)) remquo(float2, float2, float2 *); +extern float3 __attribute__((overloadable)) remquo(float3, float3, float3 *); +extern float4 __attribute__((overloadable)) remquo(float4, float4, float4 *); + +extern float __attribute__((overloadable)) rint(float); +DEF_FUNC_1(rint) + +static float __attribute__((overloadable)) rootn(float v, int r) { + return pow(v, 1.f / r); +} +static float2 __attribute__((overloadable)) rootn(float2 v, int2 r) { + float2 t = {1.f / r.x, 1.f / r.y}; + return pow(v, t); +} +static float3 __attribute__((overloadable)) rootn(float3 v, int3 r) { + float3 t = {1.f / r.x, 1.f / r.y, 1.f / r.z}; + return pow(v, t); +} +static float4 __attribute__((overloadable)) rootn(float4 v, int4 r) { + float4 t = {1.f / r.x, 1.f / r.y, 1.f / r.z, 1.f / r.w}; + return pow(v, t); +} + +extern float __attribute__((overloadable)) round(float); +DEF_FUNC_1(round) + +extern float __attribute__((overloadable)) sqrt(float); +/*static float __attribute__((overloadable)) rsqrt(float v) { + return 1.f / sqrt(v); +} +DEF_FUNC_1(rsqrt)*/ + +extern float __attribute__((overloadable)) sin(float); +DEF_FUNC_1(sin) + +static float __attribute__((overloadable)) sincos(float v, float *cosptr) { + *cosptr = cos(v); + return sin(v); +} +static float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) { + *cosptr = cos(v); + return sin(v); +} +static float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) { + *cosptr = cos(v); + return sin(v); +} +static float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) { + *cosptr = cos(v); + return sin(v); +} + +extern float __attribute__((overloadable)) sinh(float); +DEF_FUNC_1(sinh) + +static float __attribute__((overloadable)) sinpi(float v) { + return sin(v * M_PI); +} +DEF_FUNC_1(sinpi) + +DEF_FUNC_1(sqrt) + +extern float __attribute__((overloadable)) tan(float); +DEF_FUNC_1(tan) + +extern float __attribute__((overloadable)) tanh(float); +DEF_FUNC_1(tanh) + +static float __attribute__((overloadable)) tanpi(float v) { + return tan(v * M_PI); +} +DEF_FUNC_1(tanpi) + +extern float __attribute__((overloadable)) tgamma(float); +DEF_FUNC_1(tgamma) + +extern float __attribute__((overloadable)) trunc(float); +DEF_FUNC_1(trunc) + +// Int ops (partial), 6.11.3 +extern uint __attribute__((overloadable)) abs(int); +extern ushort __attribute__((overloadable)) abs(short); +extern uchar __attribute__((overloadable)) abs(char); + +extern uint __attribute__((overloadable)) clz(uint); +extern int __attribute__((overloadable)) clz(int); +extern ushort __attribute__((overloadable)) clz(ushort); +extern short __attribute__((overloadable)) clz(short); +extern uchar __attribute__((overloadable)) clz(uchar); +extern char __attribute__((overloadable)) clz(char); + +static uint __attribute__((overloadable)) min(uint v1, uint v2) { + return v1 < v2 ? v1 : v2; +} +static int __attribute__((overloadable)) min(int v1, int v2) { + return v1 < v2 ? v1 : v2; +} +static ushort __attribute__((overloadable)) min(ushort v1, ushort v2) { + return v1 < v2 ? v1 : v2; +} +static short __attribute__((overloadable)) min(short v1, short v2) { + return v1 < v2 ? v1 : v2; +} +static uchar __attribute__((overloadable)) min(uchar v1, uchar v2) { + return v1 < v2 ? v1 : v2; +} +static char __attribute__((overloadable)) min(char v1, char v2) { + return v1 < v2 ? v1 : v2; +} + +static uint __attribute__((overloadable)) max(uint v1, uint v2) { + return v1 > v2 ? v1 : v2; +} +static int __attribute__((overloadable)) max(int v1, int v2) { + return v1 > v2 ? v1 : v2; +} +static ushort __attribute__((overloadable)) max(ushort v1, ushort v2) { + return v1 > v2 ? v1 : v2; +} +static short __attribute__((overloadable)) max(short v1, short v2) { + return v1 > v2 ? v1 : v2; +} +static uchar __attribute__((overloadable)) max(uchar v1, uchar v2) { + return v1 > v2 ? v1 : v2; +} +static char __attribute__((overloadable)) max(char v1, char v2) { + return v1 > v2 ? v1 : v2; +} + + + + +// 6.11.4 + +static float __attribute__((overloadable)) clamp(float amount, float low, float high) { + return amount < low ? low : (amount > high ? high : amount); +} +static float2 __attribute__((overloadable)) clamp(float2 amount, float2 low, float2 high) { + float2 r; + r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x); + r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y); + return r; +} +static float3 __attribute__((overloadable)) clamp(float3 amount, float3 low, float3 high) { + float3 r; + r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x); + r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y); + r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z); + return r; +} +static float4 __attribute__((overloadable)) clamp(float4 amount, float4 low, float4 high) { + float4 r; + r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x); + r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y); + r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z); + r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w); + return r; +} +static float2 __attribute__((overloadable)) clamp(float2 amount, float low, float high) { + float2 r; + r.x = amount.x < low ? low : (amount.x > high ? high : amount.x); + r.y = amount.y < low ? low : (amount.y > high ? high : amount.y); + return r; +} +static float3 __attribute__((overloadable)) clamp(float3 amount, float low, float high) { + float3 r; + r.x = amount.x < low ? low : (amount.x > high ? high : amount.x); + r.y = amount.y < low ? low : (amount.y > high ? high : amount.y); + r.z = amount.z < low ? low : (amount.z > high ? high : amount.z); + return r; +} +static float4 __attribute__((overloadable)) clamp(float4 amount, float low, float high) { + float4 r; + r.x = amount.x < low ? low : (amount.x > high ? high : amount.x); + r.y = amount.y < low ? low : (amount.y > high ? high : amount.y); + r.z = amount.z < low ? low : (amount.z > high ? high : amount.z); + r.w = amount.w < low ? low : (amount.w > high ? high : amount.w); + return r; +} + +static float __attribute__((overloadable)) degrees(float radians) { + return radians * (180.f / M_PI); +} +DEF_FUNC_1(degrees) + +static float __attribute__((overloadable)) max(float v1, float v2) { + return v1 > v2 ? v1 : v2; +} +static float2 __attribute__((overloadable)) max(float2 v1, float2 v2) { + float2 r; + r.x = v1.x > v2.x ? v1.x : v2.x; + r.y = v1.y > v2.y ? v1.y : v2.y; + return r; +} +static float3 __attribute__((overloadable)) max(float3 v1, float3 v2) { + float3 r; + r.x = v1.x > v2.x ? v1.x : v2.x; + r.y = v1.y > v2.y ? v1.y : v2.y; + r.z = v1.z > v2.z ? v1.z : v2.z; + return r; +} +static float4 __attribute__((overloadable)) max(float4 v1, float4 v2) { + float4 r; + r.x = v1.x > v2.x ? v1.x : v2.x; + r.y = v1.y > v2.y ? v1.y : v2.y; + r.z = v1.z > v2.z ? v1.z : v2.z; + r.w = v1.w > v2.w ? v1.w : v2.w; + return r; +} +static float2 __attribute__((overloadable)) max(float2 v1, float v2) { + float2 r; + r.x = v1.x > v2 ? v1.x : v2; + r.y = v1.y > v2 ? v1.y : v2; + return r; +} +static float3 __attribute__((overloadable)) max(float3 v1, float v2) { + float3 r; + r.x = v1.x > v2 ? v1.x : v2; + r.y = v1.y > v2 ? v1.y : v2; + r.z = v1.z > v2 ? v1.z : v2; + return r; +} +static float4 __attribute__((overloadable)) max(float4 v1, float v2) { + float4 r; + r.x = v1.x > v2 ? v1.x : v2; + r.y = v1.y > v2 ? v1.y : v2; + r.z = v1.z > v2 ? v1.z : v2; + r.w = v1.w > v2 ? v1.w : v2; + return r; +} + +static float __attribute__((overloadable)) min(float v1, float v2) { + return v1 < v2 ? v1 : v2; +} +static float2 __attribute__((overloadable)) min(float2 v1, float2 v2) { + float2 r; + r.x = v1.x < v2.x ? v1.x : v2.x; + r.y = v1.y < v2.y ? v1.y : v2.y; + return r; +} +static float3 __attribute__((overloadable)) min(float3 v1, float3 v2) { + float3 r; + r.x = v1.x < v2.x ? v1.x : v2.x; + r.y = v1.y < v2.y ? v1.y : v2.y; + r.z = v1.z < v2.z ? v1.z : v2.z; + return r; +} +static float4 __attribute__((overloadable)) min(float4 v1, float4 v2) { + float4 r; + r.x = v1.x < v2.x ? v1.x : v2.x; + r.y = v1.y < v2.y ? v1.y : v2.y; + r.z = v1.z < v2.z ? v1.z : v2.z; + r.w = v1.w < v2.w ? v1.w : v2.w; + return r; +} +static float2 __attribute__((overloadable)) min(float2 v1, float v2) { + float2 r; + r.x = v1.x < v2 ? v1.x : v2; + r.y = v1.y < v2 ? v1.y : v2; + return r; +} +static float3 __attribute__((overloadable)) min(float3 v1, float v2) { + float3 r; + r.x = v1.x < v2 ? v1.x : v2; + r.y = v1.y < v2 ? v1.y : v2; + r.z = v1.z < v2 ? v1.z : v2; + return r; +} +static float4 __attribute__((overloadable)) min(float4 v1, float v2) { + float4 r; + r.x = v1.x < v2 ? v1.x : v2; + r.y = v1.y < v2 ? v1.y : v2; + r.z = v1.z < v2 ? v1.z : v2; + r.w = v1.w < v2 ? v1.w : v2; + return r; +} + +static float __attribute__((overloadable)) mix(float start, float stop, float amount) { + return start + (stop - start) * amount; +} +static float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) { + return start + (stop - start) * amount; +} +static float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) { + return start + (stop - start) * amount; +} +static float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) { + return start + (stop - start) * amount; +} +static float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) { + return start + (stop - start) * amount; +} +static float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) { + return start + (stop - start) * amount; +} +static float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) { + return start + (stop - start) * amount; +} + +static float __attribute__((overloadable)) radians(float degrees) { + return degrees * (M_PI / 180.f); +} +DEF_FUNC_1(radians) + +static float __attribute__((overloadable)) step(float edge, float v) { + return (v < edge) ? 0.f : 1.f; +} +static float2 __attribute__((overloadable)) step(float2 edge, float2 v) { + float2 r; + r.x = (v.x < edge.x) ? 0.f : 1.f; + r.y = (v.y < edge.y) ? 0.f : 1.f; + return r; +} +static float3 __attribute__((overloadable)) step(float3 edge, float3 v) { + float3 r; + r.x = (v.x < edge.x) ? 0.f : 1.f; + r.y = (v.y < edge.y) ? 0.f : 1.f; + r.z = (v.z < edge.z) ? 0.f : 1.f; + return r; +} +static float4 __attribute__((overloadable)) step(float4 edge, float4 v) { + float4 r; + r.x = (v.x < edge.x) ? 0.f : 1.f; + r.y = (v.y < edge.y) ? 0.f : 1.f; + r.z = (v.z < edge.z) ? 0.f : 1.f; + r.w = (v.w < edge.w) ? 0.f : 1.f; + return r; +} +static float2 __attribute__((overloadable)) step(float2 edge, float v) { + float2 r; + r.x = (v < edge.x) ? 0.f : 1.f; + r.y = (v < edge.y) ? 0.f : 1.f; + return r; +} +static float3 __attribute__((overloadable)) step(float3 edge, float v) { + float3 r; + r.x = (v < edge.x) ? 0.f : 1.f; + r.y = (v < edge.y) ? 0.f : 1.f; + r.z = (v < edge.z) ? 0.f : 1.f; + return r; +} +static float4 __attribute__((overloadable)) step(float4 edge, float v) { + float4 r; + r.x = (v < edge.x) ? 0.f : 1.f; + r.y = (v < edge.y) ? 0.f : 1.f; + r.z = (v < edge.z) ? 0.f : 1.f; + r.w = (v < edge.w) ? 0.f : 1.f; + return r; +} + +extern float __attribute__((overloadable)) smoothstep(float, float, float); +extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2); +extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3); +extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4); +extern float2 __attribute__((overloadable)) smoothstep(float, float, float2); +extern float3 __attribute__((overloadable)) smoothstep(float, float, float3); +extern float4 __attribute__((overloadable)) smoothstep(float, float, float4); + +static float __attribute__((overloadable)) sign(float v) { + if (v > 0) return 1.f; + if (v < 0) return -1.f; + return v; +} +DEF_FUNC_1(sign) + +// 6.11.5 +static float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) { + float3 r; + r.x = lhs.y * rhs.z - lhs.z * rhs.y; + r.y = lhs.z * rhs.x - lhs.x * rhs.z; + r.z = lhs.x * rhs.y - lhs.y * rhs.x; + return r; +} + +static float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) { + float4 r; + r.x = lhs.y * rhs.z - lhs.z * rhs.y; + r.y = lhs.z * rhs.x - lhs.x * rhs.z; + r.z = lhs.x * rhs.y - lhs.y * rhs.x; + r.w = 0.f; + return r; +} + +static float __attribute__((overloadable)) dot(float lhs, float rhs) { + return lhs * rhs; +} +static float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) { + return lhs.x*rhs.x + lhs.y*rhs.y; +} +static float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) { + return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z; +} +static float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) { + return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w; +} + +static float __attribute__((overloadable)) length(float v) { + return v; +} +static float __attribute__((overloadable)) length(float2 v) { + return sqrt(v.x*v.x + v.y*v.y); +} +static float __attribute__((overloadable)) length(float3 v) { + return sqrt(v.x*v.x + v.y*v.y + v.z*v.z); +} +static float __attribute__((overloadable)) length(float4 v) { + return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); +} + +static float __attribute__((overloadable)) distance(float lhs, float rhs) { + return length(lhs - rhs); +} +static float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) { + return length(lhs - rhs); +} +static float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) { + return length(lhs - rhs); +} +static float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) { + return length(lhs - rhs); +} + +static float __attribute__((overloadable)) normalize(float v) { + return 1.f; +} +static float2 __attribute__((overloadable)) normalize(float2 v) { + return v / length(v); +} +static float3 __attribute__((overloadable)) normalize(float3 v) { + return v / length(v); +} +static float4 __attribute__((overloadable)) normalize(float4 v) { + return v / length(v); +} + + +#endif diff --git a/libs/rs/scriptc/rs_core.rsh b/libs/rs/scriptc/rs_core.rsh new file mode 100644 index 0000000..85f3b25 --- /dev/null +++ b/libs/rs/scriptc/rs_core.rsh @@ -0,0 +1,632 @@ +#ifndef __RS_CORE_RSH__ +#define __RS_CORE_RSH__ + +static void __attribute__((overloadable)) rsDebug(const char *s, float2 v) { + rsDebug(s, v.x, v.y); +} +static void __attribute__((overloadable)) rsDebug(const char *s, float3 v) { + rsDebug(s, v.x, v.y, v.z); +} +static void __attribute__((overloadable)) rsDebug(const char *s, float4 v) { + rsDebug(s, v.x, v.y, v.z, v.w); +} + +static uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b) +{ + uchar4 c; + c.x = (uchar)(r * 255.f); + c.y = (uchar)(g * 255.f); + c.z = (uchar)(b * 255.f); + c.w = 255; + return c; +} + +static uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a) +{ + uchar4 c; + c.x = (uchar)(r * 255.f); + c.y = (uchar)(g * 255.f); + c.z = (uchar)(b * 255.f); + c.w = (uchar)(a * 255.f); + return c; +} + +static uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color) +{ + color *= 255.f; + uchar4 c = {color.x, color.y, color.z, 255}; + return c; +} + +static uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color) +{ + color *= 255.f; + uchar4 c = {color.x, color.y, color.z, color.w}; + return c; +} + +static float4 rsUnpackColor8888(uchar4 c) +{ + float4 ret = (float4)0.0039156862745f; + ret *= convert_float4(c); + return ret; +} + +//extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float r, float g, float b); +//extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float3); +//extern float4 rsUnpackColor565(uchar4); + + +///////////////////////////////////////////////////// +// Matrix ops +///////////////////////////////////////////////////// + +static void __attribute__((overloadable)) +rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) { + m->m[row * 4 + col] = v; +} + +static float __attribute__((overloadable)) +rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) { + return m->m[row * 4 + col]; +} + +static void __attribute__((overloadable)) +rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) { + m->m[row * 3 + col] = v; +} + +static float __attribute__((overloadable)) +rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) { + return m->m[row * 3 + col]; +} + +static void __attribute__((overloadable)) +rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) { + m->m[row * 2 + col] = v; +} + +static float __attribute__((overloadable)) +rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) { + return m->m[row * 2 + col]; +} + +static void __attribute__((overloadable)) +rsMatrixLoadIdentity(rs_matrix4x4 *m) { + m->m[0] = 1.f; + m->m[1] = 0.f; + m->m[2] = 0.f; + m->m[3] = 0.f; + m->m[4] = 0.f; + m->m[5] = 1.f; + m->m[6] = 0.f; + m->m[7] = 0.f; + m->m[8] = 0.f; + m->m[9] = 0.f; + m->m[10] = 1.f; + m->m[11] = 0.f; + m->m[12] = 0.f; + m->m[13] = 0.f; + m->m[14] = 0.f; + m->m[15] = 1.f; +} + +static void __attribute__((overloadable)) +rsMatrixLoadIdentity(rs_matrix3x3 *m) { + m->m[0] = 1.f; + m->m[1] = 0.f; + m->m[2] = 0.f; + m->m[3] = 0.f; + m->m[4] = 1.f; + m->m[5] = 0.f; + m->m[6] = 0.f; + m->m[7] = 0.f; + m->m[8] = 1.f; +} + +static void __attribute__((overloadable)) +rsMatrixLoadIdentity(rs_matrix2x2 *m) { + m->m[0] = 1.f; + m->m[1] = 0.f; + m->m[2] = 0.f; + m->m[3] = 1.f; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix4x4 *m, const float *v) { + m->m[0] = v[0]; + m->m[1] = v[1]; + m->m[2] = v[2]; + m->m[3] = v[3]; + m->m[4] = v[4]; + m->m[5] = v[5]; + m->m[6] = v[6]; + m->m[7] = v[7]; + m->m[8] = v[8]; + m->m[9] = v[9]; + m->m[10] = v[10]; + m->m[11] = v[11]; + m->m[12] = v[12]; + m->m[13] = v[13]; + m->m[14] = v[14]; + m->m[15] = v[15]; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix3x3 *m, const float *v) { + m->m[0] = v[0]; + m->m[1] = v[1]; + m->m[2] = v[2]; + m->m[3] = v[3]; + m->m[4] = v[4]; + m->m[5] = v[5]; + m->m[6] = v[6]; + m->m[7] = v[7]; + m->m[8] = v[8]; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix2x2 *m, const float *v) { + m->m[0] = v[0]; + m->m[1] = v[1]; + m->m[2] = v[2]; + m->m[3] = v[3]; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *v) { + m->m[0] = v->m[0]; + m->m[1] = v->m[1]; + m->m[2] = v->m[2]; + m->m[3] = v->m[3]; + m->m[4] = v->m[4]; + m->m[5] = v->m[5]; + m->m[6] = v->m[6]; + m->m[7] = v->m[7]; + m->m[8] = v->m[8]; + m->m[9] = v->m[9]; + m->m[10] = v->m[10]; + m->m[11] = v->m[11]; + m->m[12] = v->m[12]; + m->m[13] = v->m[13]; + m->m[14] = v->m[14]; + m->m[15] = v->m[15]; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v) { + m->m[0] = v->m[0]; + m->m[1] = v->m[1]; + m->m[2] = v->m[2]; + m->m[3] = 0.f; + m->m[4] = v->m[3]; + m->m[5] = v->m[4]; + m->m[6] = v->m[5]; + m->m[7] = 0.f; + m->m[8] = v->m[6]; + m->m[9] = v->m[7]; + m->m[10] = v->m[8]; + m->m[11] = 0.f; + m->m[12] = 0.f; + m->m[13] = 0.f; + m->m[14] = 0.f; + m->m[15] = 1.f; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v) { + m->m[0] = v->m[0]; + m->m[1] = v->m[1]; + m->m[2] = 0.f; + m->m[3] = 0.f; + m->m[4] = v->m[3]; + m->m[5] = v->m[4]; + m->m[6] = 0.f; + m->m[7] = 0.f; + m->m[8] = v->m[6]; + m->m[9] = v->m[7]; + m->m[10] = 1.f; + m->m[11] = 0.f; + m->m[12] = 0.f; + m->m[13] = 0.f; + m->m[14] = 0.f; + m->m[15] = 1.f; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *v) { + m->m[0] = v->m[0]; + m->m[1] = v->m[1]; + m->m[2] = v->m[2]; + m->m[3] = v->m[3]; + m->m[4] = v->m[4]; + m->m[5] = v->m[5]; + m->m[6] = v->m[6]; + m->m[7] = v->m[7]; + m->m[8] = v->m[8]; +} + +static void __attribute__((overloadable)) +rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *v) { + m->m[0] = v->m[0]; + m->m[1] = v->m[1]; + m->m[2] = v->m[2]; + m->m[3] = v->m[3]; +} + +static void __attribute__((overloadable)) +rsMatrixLoadRotate(rs_matrix4x4 *m, float rot, float x, float y, float z) { + float c, s; + m->m[3] = 0; + m->m[7] = 0; + m->m[11]= 0; + m->m[12]= 0; + m->m[13]= 0; + m->m[14]= 0; + m->m[15]= 1; + rot *= (float)(M_PI / 180.0f); + c = cos(rot); + s = sin(rot); + + const float len = x*x + y*y + z*z; + if (len != 1) { + const float recipLen = 1.f / sqrt(len); + x *= recipLen; + y *= recipLen; + z *= recipLen; + } + const float nc = 1.0f - c; + const float xy = x * y; + const float yz = y * z; + const float zx = z * x; + const float xs = x * s; + const float ys = y * s; + const float zs = z * s; + m->m[ 0] = x*x*nc + c; + m->m[ 4] = xy*nc - zs; + m->m[ 8] = zx*nc + ys; + m->m[ 1] = xy*nc + zs; + m->m[ 5] = y*y*nc + c; + m->m[ 9] = yz*nc - xs; + m->m[ 2] = zx*nc - ys; + m->m[ 6] = yz*nc + xs; + m->m[10] = z*z*nc + c; +} + +static void __attribute__((overloadable)) +rsMatrixLoadScale(rs_matrix4x4 *m, float x, float y, float z) { + rsMatrixLoadIdentity(m); + m->m[0] = x; + m->m[5] = y; + m->m[10] = z; +} + +static void __attribute__((overloadable)) +rsMatrixLoadTranslate(rs_matrix4x4 *m, float x, float y, float z) { + rsMatrixLoadIdentity(m); + m->m[12] = x; + m->m[13] = y; + m->m[14] = z; +} + +static void __attribute__((overloadable)) +rsMatrixLoadMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) { + for (int i=0 ; i<4 ; i++) { + float ri0 = 0; + float ri1 = 0; + float ri2 = 0; + float ri3 = 0; + for (int j=0 ; j<4 ; j++) { + const float rhs_ij = rsMatrixGet(rhs, i,j); + ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; + ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; + ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij; + ri3 += rsMatrixGet(lhs, j, 3) * rhs_ij; + } + rsMatrixSet(m, i, 0, ri0); + rsMatrixSet(m, i, 1, ri1); + rsMatrixSet(m, i, 2, ri2); + rsMatrixSet(m, i, 3, ri3); + } +} + +static void __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *rhs) { + rs_matrix4x4 mt; + rsMatrixLoadMultiply(&mt, m, rhs); + rsMatrixLoad(m, &mt); +} + +static void __attribute__((overloadable)) +rsMatrixLoadMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs) { + for (int i=0 ; i<3 ; i++) { + float ri0 = 0; + float ri1 = 0; + float ri2 = 0; + for (int j=0 ; j<3 ; j++) { + const float rhs_ij = rsMatrixGet(rhs, i,j); + ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; + ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; + ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij; + } + rsMatrixSet(m, i, 0, ri0); + rsMatrixSet(m, i, 1, ri1); + rsMatrixSet(m, i, 2, ri2); + } +} + +static void __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *rhs) { + rs_matrix3x3 mt; + rsMatrixLoadMultiply(&mt, m, rhs); + rsMatrixLoad(m, &mt); +} + +static void __attribute__((overloadable)) +rsMatrixLoadMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs) { + for (int i=0 ; i<2 ; i++) { + float ri0 = 0; + float ri1 = 0; + for (int j=0 ; j<2 ; j++) { + const float rhs_ij = rsMatrixGet(rhs, i,j); + ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; + ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; + } + rsMatrixSet(m, i, 0, ri0); + rsMatrixSet(m, i, 1, ri1); + } +} + +static void __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *rhs) { + rs_matrix2x2 mt; + rsMatrixLoadMultiply(&mt, m, rhs); + rsMatrixLoad(m, &mt); +} + +static void __attribute__((overloadable)) +rsMatrixRotate(rs_matrix4x4 *m, float rot, float x, float y, float z) { + rs_matrix4x4 m1; + rsMatrixLoadRotate(&m1, rot, x, y, z); + rsMatrixMultiply(m, &m1); +} + +static void __attribute__((overloadable)) +rsMatrixScale(rs_matrix4x4 *m, float x, float y, float z) { + rs_matrix4x4 m1; + rsMatrixLoadScale(&m1, x, y, z); + rsMatrixMultiply(m, &m1); +} + +static void __attribute__((overloadable)) +rsMatrixTranslate(rs_matrix4x4 *m, float x, float y, float z) { + rs_matrix4x4 m1; + rsMatrixLoadTranslate(&m1, x, y, z); + rsMatrixMultiply(m, &m1); +} + +static void __attribute__((overloadable)) +rsMatrixLoadOrtho(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far) { + rsMatrixLoadIdentity(m); + m->m[0] = 2.f / (right - left); + m->m[5] = 2.f / (top - bottom); + m->m[10]= -2.f / (far - near); + m->m[12]= -(right + left) / (right - left); + m->m[13]= -(top + bottom) / (top - bottom); + m->m[14]= -(far + near) / (far - near); +} + +static void __attribute__((overloadable)) +rsMatrixLoadFrustum(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far) { + rsMatrixLoadIdentity(m); + m->m[0] = 2.f * near / (right - left); + m->m[5] = 2.f * near / (top - bottom); + m->m[8] = (right + left) / (right - left); + m->m[9] = (top + bottom) / (top - bottom); + m->m[10]= -(far + near) / (far - near); + m->m[11]= -1.f; + m->m[14]= -2.f * far * near / (far - near); + m->m[15]= 0.f; +} + +static float4 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix4x4 *m, float4 in) { + float4 ret; + ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + (m->m[12] * in.w); + ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + (m->m[13] * in.w); + ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + (m->m[14] * in.w); + ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + (m->m[15] * in.w); + return ret; +} + +static float4 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix4x4 *m, float3 in) { + float4 ret; + ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + m->m[12]; + ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + m->m[13]; + ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + m->m[14]; + ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + m->m[15]; + return ret; +} + +static float4 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix4x4 *m, float2 in) { + float4 ret; + ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + m->m[12]; + ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + m->m[13]; + ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + m->m[14]; + ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + m->m[15]; + return ret; +} + +static float3 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix3x3 *m, float3 in) { + float3 ret; + ret.x = (m->m[0] * in.x) + (m->m[3] * in.y) + (m->m[6] * in.z); + ret.y = (m->m[1] * in.x) + (m->m[4] * in.y) + (m->m[7] * in.z); + ret.z = (m->m[2] * in.x) + (m->m[5] * in.y) + (m->m[8] * in.z); + return ret; +} + +static float3 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix3x3 *m, float2 in) { + float3 ret; + ret.x = (m->m[0] * in.x) + (m->m[3] * in.y); + ret.y = (m->m[1] * in.x) + (m->m[4] * in.y); + ret.z = (m->m[2] * in.x) + (m->m[5] * in.y); + return ret; +} + +static float2 __attribute__((overloadable)) +rsMatrixMultiply(rs_matrix2x2 *m, float2 in) { + float2 ret; + ret.x = (m->m[0] * in.x) + (m->m[2] * in.y); + ret.y = (m->m[1] * in.x) + (m->m[3] * in.y); + return ret; +} + +// Returns true if the matrix was successfully inversed +static bool __attribute__((overloadable)) +rsMatrixInverse(rs_matrix4x4 *m) { + rs_matrix4x4 result; + + int i, j; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + // computeCofactor for int i, int j + int c0 = (i+1) % 4; + int c1 = (i+2) % 4; + int c2 = (i+3) % 4; + int r0 = (j+1) % 4; + int r1 = (j+2) % 4; + int r2 = (j+3) % 4; + + float minor = (m->m[c0 + 4*r0] * (m->m[c1 + 4*r1] * m->m[c2 + 4*r2] - m->m[c1 + 4*r2] * m->m[c2 + 4*r1])) + - (m->m[c0 + 4*r1] * (m->m[c1 + 4*r0] * m->m[c2 + 4*r2] - m->m[c1 + 4*r2] * m->m[c2 + 4*r0])) + + (m->m[c0 + 4*r2] * (m->m[c1 + 4*r0] * m->m[c2 + 4*r1] - m->m[c1 + 4*r1] * m->m[c2 + 4*r0])); + + float cofactor = (i+j) & 1 ? -minor : minor; + + result.m[4*i + j] = cofactor; + } + } + + // Dot product of 0th column of source and 0th row of result + float det = m->m[0]*result.m[0] + m->m[4]*result.m[1] + + m->m[8]*result.m[2] + m->m[12]*result.m[3]; + + if (fabs(det) < 1e-6) { + return false; + } + + det = 1.0f / det; + for (i = 0; i < 16; ++i) { + m->m[i] = result.m[i] * det; + } + + return true; +} + +// Returns true if the matrix was successfully inversed +static bool __attribute__((overloadable)) +rsMatrixInverseTranspose(rs_matrix4x4 *m) { + rs_matrix4x4 result; + + int i, j; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + // computeCofactor for int i, int j + int c0 = (i+1) % 4; + int c1 = (i+2) % 4; + int c2 = (i+3) % 4; + int r0 = (j+1) % 4; + int r1 = (j+2) % 4; + int r2 = (j+3) % 4; + + float minor = (m->m[c0 + 4*r0] * (m->m[c1 + 4*r1] * m->m[c2 + 4*r2] - m->m[c1 + 4*r2] * m->m[c2 + 4*r1])) + - (m->m[c0 + 4*r1] * (m->m[c1 + 4*r0] * m->m[c2 + 4*r2] - m->m[c1 + 4*r2] * m->m[c2 + 4*r0])) + + (m->m[c0 + 4*r2] * (m->m[c1 + 4*r0] * m->m[c2 + 4*r1] - m->m[c1 + 4*r1] * m->m[c2 + 4*r0])); + + float cofactor = (i+j) & 1 ? -minor : minor; + + result.m[4*j + i] = cofactor; + } + } + + // Dot product of 0th column of source and 0th column of result + float det = m->m[0]*result.m[0] + m->m[4]*result.m[4] + + m->m[8]*result.m[8] + m->m[12]*result.m[12]; + + if (fabs(det) < 1e-6) { + return false; + } + + det = 1.0f / det; + for (i = 0; i < 16; ++i) { + m->m[i] = result.m[i] * det; + } + + return true; +} + +static void __attribute__((overloadable)) +rsMatrixTranspose(rs_matrix4x4 *m) { + int i, j; + float temp; + for (i = 0; i < 3; ++i) { + for (j = i + 1; j < 4; ++j) { + temp = m->m[i*4 + j]; + m->m[i*4 + j] = m->m[j*4 + i]; + m->m[j*4 + i] = temp; + } + } +} + +static void __attribute__((overloadable)) +rsMatrixTranspose(rs_matrix3x3 *m) { + int i, j; + float temp; + for (i = 0; i < 2; ++i) { + for (j = i + 1; j < 3; ++j) { + temp = m->m[i*3 + j]; + m->m[i*3 + j] = m->m[j*4 + i]; + m->m[j*3 + i] = temp; + } + } +} + +static void __attribute__((overloadable)) +rsMatrixTranspose(rs_matrix2x2 *m) { + float temp = m->m[1]; + m->m[1] = m->m[2]; + m->m[2] = temp; +} + + +///////////////////////////////////////////////////// +// int ops +///////////////////////////////////////////////////// + +__inline__ static uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high) { + return amount < low ? low : (amount > high ? high : amount); +} +__inline__ static int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high) { + return amount < low ? low : (amount > high ? high : amount); +} +__inline__ static ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high) { + return amount < low ? low : (amount > high ? high : amount); +} +__inline__ static short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high) { + return amount < low ? low : (amount > high ? high : amount); +} +__inline__ static uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high) { + return amount < low ? low : (amount > high ? high : amount); +} +__inline__ static char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high) { + return amount < low ? low : (amount > high ? high : amount); +} + + + +#endif + diff --git a/libs/rs/scriptc/rs_graphics.rsh b/libs/rs/scriptc/rs_graphics.rsh index 70cd562..fd0491c 100644 --- a/libs/rs/scriptc/rs_graphics.rsh +++ b/libs/rs/scriptc/rs_graphics.rsh @@ -1,65 +1,88 @@ - - -extern float2 vec2Rand(float len); - -extern float3 float3Norm(float3); -extern float float3Length(float3); -extern float3 float3Add(float3 lhs, float3 rhs); -extern float3 float3Sub(float3 lhs, float3 rhs); -extern float3 float3Cross(float3 lhs, float3 rhs); -extern float float3Dot(float3 lhs, float3 rhs); -extern float3 float3Scale(float3 v, float scale); - -extern float4 float4Add(float4 lhs, float4 rhs); -extern float4 float4Sub(float4 lhs, float4 rhs); -extern float4 float4Cross(float4 lhs, float4 rhs); -extern float float4Dot(float4 lhs, float4 rhs); -extern float4 float4Scale(float4 v, float scale); - - // context -extern void bindProgramFragment(rs_program_fragment); -extern void bindProgramStore(rs_program_store); -extern void bindProgramVertex(rs_program_vertex); - -extern void bindSampler(rs_program_fragment, int slot, rs_sampler); -extern void bindSampler(rs_program_fragment, int slot, rs_allocation); - -extern void vpLoadModelMatrix(const float *); -extern void vpLoadTextureMatrix(const float *); - - -// drawing -extern void drawRect(float x1, float y1, float x2, float y2, float z); -extern void drawQuad(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4); -extern void drawQuadTexCoords(float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, float x4, float y4, float z4, float u4, float v4); -extern void drawSprite(float x, float y, float z, float w, float h); -extern void drawSpriteScreenspace(float x, float y, float z, float w, float h); -extern void drawLine(float x1, float y1, float z1, float x2, float y2, float z2); -extern void drawPoint(float x1, float y1, float z1); -extern void drawSimpleMesh(int ism); -extern void drawSimpleMeshRange(int ism, int start, int len); - +#ifndef __RS_GRAPHICS_RSH__ +#define __RS_GRAPHICS_RSH__ + +#include "rs_math.rsh" + + +// Bind a ProgramFragment to the RS context. +extern void __attribute__((overloadable)) + rsgBindProgramFragment(rs_program_fragment); +extern void __attribute__((overloadable)) + rsgBindProgramStore(rs_program_store); +extern void __attribute__((overloadable)) + rsgBindProgramVertex(rs_program_vertex); +extern void __attribute__((overloadable)) + rsgBindProgramRaster(rs_program_raster); + +extern void __attribute__((overloadable)) + rsgBindSampler(rs_program_fragment, uint slot, rs_sampler); +extern void __attribute__((overloadable)) + rsgBindTexture(rs_program_fragment, uint slot, rs_allocation); + +extern void __attribute__((overloadable)) + rsgProgramVertexLoadProjectionMatrix(const rs_matrix4x4 *); +extern void __attribute__((overloadable)) + rsgProgramVertexLoadModelMatrix(const rs_matrix4x4 *); +extern void __attribute__((overloadable)) + rsgProgramVertexLoadTextureMatrix(const rs_matrix4x4 *); + +extern void __attribute__((overloadable)) + rsgProgramFragmentConstantColor(rs_program_fragment, float, float, float, float); + +extern uint __attribute__((overloadable)) + rsgGetWidth(void); +extern uint __attribute__((overloadable)) + rsgGetHeight(void); + +extern void __attribute__((overloadable)) + rsgUploadToTexture(rs_allocation); +extern void __attribute__((overloadable)) + rsgUploadToTexture(rs_allocation, uint mipLevel); +extern void __attribute__((overloadable)) + rsgUploadToBufferObject(rs_allocation); + +extern void __attribute__((overloadable)) + rsgDrawRect(float x1, float y1, float x2, float y2, float z); +extern void __attribute__((overloadable)) + rsgDrawQuad(float x1, float y1, float z1, + float x2, float y2, float z2, + float x3, float y3, float z3, + float x4, float y4, float z4); +extern void __attribute__((overloadable)) + rsgDrawQuadTexCoords(float x1, float y1, float z1, float u1, float v1, + float x2, float y2, float z2, float u2, float v2, + float x3, float y3, float z3, float u3, float v3, + float x4, float y4, float z4, float u4, float v4); +extern void __attribute__((overloadable)) + rsgDrawSpriteScreenspace(float x, float y, float z, float w, float h); + +extern void __attribute__((overloadable)) + rsgDrawMesh(rs_mesh ism); +extern void __attribute__((overloadable)) + rsgDrawMesh(rs_mesh ism, uint primitiveIndex); +extern void __attribute__((overloadable)) + rsgDrawMesh(rs_mesh ism, uint primitiveIndex, uint start, uint len); + +extern void __attribute__((overloadable)) + rsgClearColor(float, float, float, float); +extern void __attribute__((overloadable)) + rsgClearDepth(float); + +extern void __attribute__((overloadable)) + rsgDrawText(const char *, int x, int y); +extern void __attribute__((overloadable)) + rsgDrawText(rs_allocation, int x, int y); +extern void __attribute__((overloadable)) + rsgBindFont(rs_font); +extern void __attribute__((overloadable)) + rsgFontColor(float, float, float, float); + +/////////////////////////////////////////////////////// // misc -extern void pfClearColor(float, float, float, float); -extern void color(float, float, float, float); -extern void hsb(float, float, float, float); -extern void hsbToRgb(float, float, float, float*); -extern int hsbToAbgr(float, float, float, float); - -extern void uploadToTexture(int, int); -extern void uploadToBufferObject(int); - -extern int colorFloatRGBAtoUNorm8(float, float, float, float); -extern int colorFloatRGBto565(float, float, float); - -extern int getWidth(); -extern int getHeight(); - -extern int sendToClient(void *data, int cmdID, int len, int waitForSpace); - -extern void debugF(const char *, float); -extern void debugI32(const char *, int); -extern void debugHexI32(const char *, int); +// Depricated +extern void __attribute__((overloadable)) + color(float, float, float, float); +#endif diff --git a/libs/rs/scriptc/rs_math.rsh b/libs/rs/scriptc/rs_math.rsh index 613c7ca..45f6bf4 100644 --- a/libs/rs/scriptc/rs_math.rsh +++ b/libs/rs/scriptc/rs_math.rsh @@ -1,287 +1,137 @@ -// Float ops - -extern float __attribute__((overloadable)) abs(float); -extern float2 __attribute__((overloadable)) abs(float2); -extern float3 __attribute__((overloadable)) abs(float3); -extern float4 __attribute__((overloadable)) abs(float4); -extern float8 __attribute__((overloadable)) abs(float8); -extern float16 __attribute__((overloadable)) abs(float16); - -extern float __attribute__((overloadable)) acos(float); -extern float2 __attribute__((overloadable)) acos(float2); -extern float3 __attribute__((overloadable)) acos(float3); -extern float4 __attribute__((overloadable)) acos(float4); -extern float8 __attribute__((overloadable)) acos(float8); -extern float16 __attribute__((overloadable)) acos(float16); - -extern float __attribute__((overloadable)) asin(float); -extern float2 __attribute__((overloadable)) asin(float2); -extern float3 __attribute__((overloadable)) asin(float3); -extern float4 __attribute__((overloadable)) asin(float4); -extern float8 __attribute__((overloadable)) asin(float8); -extern float16 __attribute__((overloadable)) asin(float16); - -extern float __attribute__((overloadable)) atan(float); -extern float2 __attribute__((overloadable)) atan(float2); -extern float3 __attribute__((overloadable)) atan(float3); -extern float4 __attribute__((overloadable)) atan(float4); -extern float8 __attribute__((overloadable)) atan(float8); -extern float16 __attribute__((overloadable)) atan(float16); - -extern float __attribute__((overloadable)) atan2(float, float); -extern float2 __attribute__((overloadable)) atan2(float2, float2); -extern float3 __attribute__((overloadable)) atan2(float3, float3); -extern float4 __attribute__((overloadable)) atan2(float4, float4); -extern float8 __attribute__((overloadable)) atan2(float8, float8); -extern float16 __attribute__((overloadable)) atan2(float16, float16); - -extern float __attribute__((overloadable)) ceil(float); -extern float2 __attribute__((overloadable)) ceil(float2); -extern float3 __attribute__((overloadable)) ceil(float3); -extern float4 __attribute__((overloadable)) ceil(float4); -extern float8 __attribute__((overloadable)) ceil(float8); -extern float16 __attribute__((overloadable)) ceil(float16); - -extern float __attribute__((overloadable)) clamp(float, float, float); -extern float2 __attribute__((overloadable)) clamp(float2, float2, float2); -extern float3 __attribute__((overloadable)) clamp(float3, float3, float3); -extern float4 __attribute__((overloadable)) clamp(float4, float4, float4); -extern float8 __attribute__((overloadable)) clamp(float8, float8, float8); -extern float16 __attribute__((overloadable)) clamp(float16, float16, float16); -extern float __attribute__((overloadable)) clamp(float, float, float); -extern float2 __attribute__((overloadable)) clamp(float2, float, float); -extern float3 __attribute__((overloadable)) clamp(float3, float, float); -extern float4 __attribute__((overloadable)) clamp(float4, float, float); -extern float8 __attribute__((overloadable)) clamp(float8, float, float); -extern float16 __attribute__((overloadable)) clamp(float16, float, float); - -extern float __attribute__((overloadable)) copysign(float, float); -extern float2 __attribute__((overloadable)) copysign(float2, float2); -extern float3 __attribute__((overloadable)) copysign(float3, float3); -extern float4 __attribute__((overloadable)) copysign(float4, float4); -extern float8 __attribute__((overloadable)) copysign(float8, float8); -extern float16 __attribute__((overloadable)) copysign(float16, float16); - -extern float __attribute__((overloadable)) cos(float); -extern float2 __attribute__((overloadable)) cos(float2); -extern float3 __attribute__((overloadable)) cos(float3); -extern float4 __attribute__((overloadable)) cos(float4); -extern float8 __attribute__((overloadable)) cos(float8); -extern float16 __attribute__((overloadable)) cos(float16); - -extern float __attribute__((overloadable)) degrees(float); -extern float2 __attribute__((overloadable)) degrees(float2); -extern float3 __attribute__((overloadable)) degrees(float3); -extern float4 __attribute__((overloadable)) degrees(float4); -extern float8 __attribute__((overloadable)) degrees(float8); -extern float16 __attribute__((overloadable)) degrees(float16); - -extern float __attribute__((overloadable)) exp(float); -extern float2 __attribute__((overloadable)) exp(float2); -extern float3 __attribute__((overloadable)) exp(float3); -extern float4 __attribute__((overloadable)) exp(float4); -extern float8 __attribute__((overloadable)) exp(float8); -extern float16 __attribute__((overloadable)) exp(float16); - -extern float __attribute__((overloadable)) exp2(float); -extern float2 __attribute__((overloadable)) exp2(float2); -extern float3 __attribute__((overloadable)) exp2(float3); -extern float4 __attribute__((overloadable)) exp2(float4); -extern float8 __attribute__((overloadable)) exp2(float8); -extern float16 __attribute__((overloadable)) exp2(float16); - -extern float __attribute__((overloadable)) exp10(float); -extern float2 __attribute__((overloadable)) exp10(float2); -extern float3 __attribute__((overloadable)) exp10(float3); -extern float4 __attribute__((overloadable)) exp10(float4); -extern float8 __attribute__((overloadable)) exp10(float8); -extern float16 __attribute__((overloadable)) exp10(float16); - -extern float __attribute__((overloadable)) fabs(float); -extern float2 __attribute__((overloadable)) fabs(float2); -extern float3 __attribute__((overloadable)) fabs(float3); -extern float4 __attribute__((overloadable)) fabs(float4); -extern float8 __attribute__((overloadable)) fabs(float8); -extern float16 __attribute__((overloadable)) fabs(float16); - -extern float __attribute__((overloadable)) floor(float); -extern float2 __attribute__((overloadable)) floor(float2); -extern float3 __attribute__((overloadable)) floor(float3); -extern float4 __attribute__((overloadable)) floor(float4); -extern float8 __attribute__((overloadable)) floor(float8); -extern float16 __attribute__((overloadable)) floor(float16); - -extern float __attribute__((overloadable)) fmax(float, float); -extern float2 __attribute__((overloadable)) fmax(float2, float2); -extern float3 __attribute__((overloadable)) fmax(float3, float3); -extern float4 __attribute__((overloadable)) fmax(float4, float4); -extern float8 __attribute__((overloadable)) fmax(float8, float8); -extern float16 __attribute__((overloadable)) fmax(float16, float16); -extern float2 __attribute__((overloadable)) fmax(float2, float); -extern float3 __attribute__((overloadable)) fmax(float3, float); -extern float4 __attribute__((overloadable)) fmax(float4, float); -extern float8 __attribute__((overloadable)) fmax(float8, float); -extern float16 __attribute__((overloadable)) fmax(float16, float); - -extern float __attribute__((overloadable)) fmin(float, float); -extern float2 __attribute__((overloadable)) fmin(float2, float2); -extern float3 __attribute__((overloadable)) fmin(float3, float3); -extern float4 __attribute__((overloadable)) fmin(float4, float4); -extern float8 __attribute__((overloadable)) fmin(float8, float8); -extern float16 __attribute__((overloadable)) fmin(float16, float16); -extern float2 __attribute__((overloadable)) fmin(float2, float); -extern float3 __attribute__((overloadable)) fmin(float3, float); -extern float4 __attribute__((overloadable)) fmin(float4, float); -extern float8 __attribute__((overloadable)) fmin(float8, float); -extern float16 __attribute__((overloadable)) fmin(float16, float); - -extern float __attribute__((overloadable)) fmod(float, float); -extern float2 __attribute__((overloadable)) fmod(float2, float2); -extern float3 __attribute__((overloadable)) fmod(float3, float3); -extern float4 __attribute__((overloadable)) fmod(float4, float4); -extern float8 __attribute__((overloadable)) fmod(float8, float8); -extern float16 __attribute__((overloadable)) fmod(float16, float16); - -extern float __attribute__((overloadable)) log(float); -extern float2 __attribute__((overloadable)) log(float2); -extern float3 __attribute__((overloadable)) log(float3); -extern float4 __attribute__((overloadable)) log(float4); -extern float8 __attribute__((overloadable)) log(float8); -extern float16 __attribute__((overloadable)) log(float16); - -extern float __attribute__((overloadable)) log2(float); -extern float2 __attribute__((overloadable)) log2(float2); -extern float3 __attribute__((overloadable)) log2(float3); -extern float4 __attribute__((overloadable)) log2(float4); -extern float8 __attribute__((overloadable)) log2(float8); -extern float16 __attribute__((overloadable)) log2(float16); - -extern float __attribute__((overloadable)) log10(float); -extern float2 __attribute__((overloadable)) log10(float2); -extern float3 __attribute__((overloadable)) log10(float3); -extern float4 __attribute__((overloadable)) log10(float4); -extern float8 __attribute__((overloadable)) log10(float8); -extern float16 __attribute__((overloadable)) log10(float16); - -extern float __attribute__((overloadable)) max(float, float); -extern float2 __attribute__((overloadable)) max(float2, float2); -extern float3 __attribute__((overloadable)) max(float3, float3); -extern float4 __attribute__((overloadable)) max(float4, float4); -extern float8 __attribute__((overloadable)) max(float8, float8); -extern float16 __attribute__((overloadable)) max(float16, float16); - -extern float __attribute__((overloadable)) min(float, float); -extern float2 __attribute__((overloadable)) min(float2, float2); -extern float3 __attribute__((overloadable)) min(float3, float3); -extern float4 __attribute__((overloadable)) min(float4, float4); -extern float8 __attribute__((overloadable)) min(float8, float8); -extern float16 __attribute__((overloadable)) min(float16, float16); - -extern float __attribute__((overloadable)) mix(float, float, float); -extern float2 __attribute__((overloadable)) mix(float2, float2, float2); -extern float3 __attribute__((overloadable)) mix(float3, float3, float3); -extern float4 __attribute__((overloadable)) mix(float4, float4, float4); -extern float8 __attribute__((overloadable)) mix(float8, float8, float8); -extern float16 __attribute__((overloadable)) mix(float16, float16, float16); -extern float __attribute__((overloadable)) mix(float, float, float); -extern float2 __attribute__((overloadable)) mix(float2, float2, float); -extern float3 __attribute__((overloadable)) mix(float3, float3, float); -extern float4 __attribute__((overloadable)) mix(float4, float4, float); -extern float8 __attribute__((overloadable)) mix(float8, float8, float); -extern float16 __attribute__((overloadable)) mix(float16, float16, float); - -extern float __attribute__((overloadable)) pow(float, float); -extern float2 __attribute__((overloadable)) pow(float2, float2); -extern float3 __attribute__((overloadable)) pow(float3, float3); -extern float4 __attribute__((overloadable)) pow(float4, float4); -extern float8 __attribute__((overloadable)) pow(float8, float8); -extern float16 __attribute__((overloadable)) pow(float16, float16); - -extern float __attribute__((overloadable)) radians(float); -extern float2 __attribute__((overloadable)) radians(float2); -extern float3 __attribute__((overloadable)) radians(float3); -extern float4 __attribute__((overloadable)) radians(float4); -extern float8 __attribute__((overloadable)) radians(float8); -extern float16 __attribute__((overloadable)) radians(float16); - -extern float __attribute__((overloadable)) rint(float); -extern float2 __attribute__((overloadable)) rint(float2); -extern float3 __attribute__((overloadable)) rint(float3); -extern float4 __attribute__((overloadable)) rint(float4); -extern float8 __attribute__((overloadable)) rint(float8); -extern float16 __attribute__((overloadable)) rint(float16); - -extern float __attribute__((overloadable)) round(float); -extern float2 __attribute__((overloadable)) round(float2); -extern float3 __attribute__((overloadable)) round(float3); -extern float4 __attribute__((overloadable)) round(float4); -extern float8 __attribute__((overloadable)) round(float8); -extern float16 __attribute__((overloadable)) round(float16); - -extern float __attribute__((overloadable)) rsqrt(float); -extern float2 __attribute__((overloadable)) rsqrt(float2); -extern float3 __attribute__((overloadable)) rsqrt(float3); -extern float4 __attribute__((overloadable)) rsqrt(float4); -extern float8 __attribute__((overloadable)) rsqrt(float8); -extern float16 __attribute__((overloadable)) rsqrt(float16); - -extern float __attribute__((overloadable)) sign(float); -extern float2 __attribute__((overloadable)) sign(float2); -extern float3 __attribute__((overloadable)) sign(float3); -extern float4 __attribute__((overloadable)) sign(float4); -extern float8 __attribute__((overloadable)) sign(float8); -extern float16 __attribute__((overloadable)) sign(float16); - -extern float __attribute__((overloadable)) sin(float); -extern float2 __attribute__((overloadable)) sin(float2); -extern float3 __attribute__((overloadable)) sin(float3); -extern float4 __attribute__((overloadable)) sin(float4); -extern float8 __attribute__((overloadable)) sin(float8); -extern float16 __attribute__((overloadable)) sin(float16); - -extern float __attribute__((overloadable)) sqrt(float); -extern float2 __attribute__((overloadable)) sqrt(float2); -extern float3 __attribute__((overloadable)) sqrt(float3); -extern float4 __attribute__((overloadable)) sqrt(float4); -extern float8 __attribute__((overloadable)) sqrt(float8); -extern float16 __attribute__((overloadable)) sqrt(float16); - -extern float __attribute__((overloadable)) tan(float); -extern float2 __attribute__((overloadable)) tan(float2); -extern float3 __attribute__((overloadable)) tan(float3); -extern float4 __attribute__((overloadable)) tan(float4); -extern float8 __attribute__((overloadable)) tan(float8); -extern float16 __attribute__((overloadable)) tan(float16); - -extern float __attribute__((overloadable)) trunc(float); -extern float2 __attribute__((overloadable)) trunc(float2); -extern float3 __attribute__((overloadable)) trunc(float3); -extern float4 __attribute__((overloadable)) trunc(float4); -extern float8 __attribute__((overloadable)) trunc(float8); -extern float16 __attribute__((overloadable)) trunc(float16); - - - - - - -// Int ops - -extern int __attribute__((overloadable)) abs(int); -extern int2 __attribute__((overloadable)) abs(int2); -extern int3 __attribute__((overloadable)) abs(int3); -extern int4 __attribute__((overloadable)) abs(int4); -extern int8 __attribute__((overloadable)) abs(int8); -extern int16 __attribute__((overloadable)) abs(int16); - - - -/* -extern float modf(float, float); -extern float randf(float); -extern float randf2(float, float); -extern float fracf(float); -extern float lerpf(float, float, float); -extern float mapf(float, float, float, float, float); -*/ - +#ifndef __RS_MATH_RSH__ +#define __RS_MATH_RSH__ + +// Debugging, print to the LOG a description string and a value. +extern void __attribute__((overloadable)) + rsDebug(const char *, float); +extern void __attribute__((overloadable)) + rsDebug(const char *, float, float); +extern void __attribute__((overloadable)) + rsDebug(const char *, float, float, float); +extern void __attribute__((overloadable)) + rsDebug(const char *, float, float, float, float); +extern void __attribute__((overloadable)) + rsDebug(const char *, const rs_matrix4x4 *); +extern void __attribute__((overloadable)) + rsDebug(const char *, const rs_matrix3x3 *); +extern void __attribute__((overloadable)) + rsDebug(const char *, const rs_matrix2x2 *); +extern void __attribute__((overloadable)) + rsDebug(const char *, int); +extern void __attribute__((overloadable)) + rsDebug(const char *, uint); +extern void __attribute__((overloadable)) + rsDebug(const char *, const void *); +#define RS_DEBUG(a) rsDebug(#a, a) +#define RS_DEBUG_MARKER rsDebug(__FILE__, __LINE__) + + +#include "rs_cl.rsh" +#include "rs_core.rsh" + +// Allocations + +// Return the rs_allocation associated with a bound data +// pointer. +extern rs_allocation __attribute__((overloadable)) + rsGetAllocation(const void *); + +// Return the dimensions associated with an allocation. +extern uint32_t __attribute__((overloadable)) + rsAllocationGetDimX(rs_allocation); +extern uint32_t __attribute__((overloadable)) + rsAllocationGetDimY(rs_allocation); +extern uint32_t __attribute__((overloadable)) + rsAllocationGetDimZ(rs_allocation); +extern uint32_t __attribute__((overloadable)) + rsAllocationGetDimLOD(rs_allocation); +extern uint32_t __attribute__((overloadable)) + rsAllocationGetDimFaces(rs_allocation); + +// Extract a single element from an allocation. +extern const void * __attribute__((overloadable)) + rsGetElementAt(rs_allocation, uint32_t x); +extern const void * __attribute__((overloadable)) + rsGetElementAt(rs_allocation, uint32_t x, uint32_t y); +extern const void * __attribute__((overloadable)) + rsGetElementAt(rs_allocation, uint32_t x, uint32_t y, uint32_t z); + +// Return a random value between 0 (or min_value) and max_malue. +extern int __attribute__((overloadable)) + rsRand(int max_value); +extern int __attribute__((overloadable)) + rsRand(int min_value, int max_value); +extern float __attribute__((overloadable)) + rsRand(float max_value); +extern float __attribute__((overloadable)) + rsRand(float min_value, float max_value); + +// return the fractional part of a float +// min(v - ((int)floor(v)), 0x1.fffffep-1f); +extern float __attribute__((overloadable)) + rsFrac(float); + +// time +extern int32_t __attribute__((overloadable)) + rsSecond(void); +extern int32_t __attribute__((overloadable)) + rsMinute(void); +extern int32_t __attribute__((overloadable)) + rsHour(void); +extern int32_t __attribute__((overloadable)) + rsDay(void); +extern int32_t __attribute__((overloadable)) + rsMonth(void); +extern int32_t __attribute__((overloadable)) + rsYear(void); + +// Return the current system clock in milliseconds +extern int64_t __attribute__((overloadable)) + rsUptimeMillis(void); + +// Return the current system clock in nanoseconds +extern int64_t __attribute__((overloadable)) + rsUptimeNanos(void); + +// Return the time in seconds since function was last called in this script. +extern float __attribute__((overloadable)) + rsGetDt(void); + +// Send a message back to the client. Will not block and returns true +// if the message was sendable and false if the fifo was full. +// A message ID is required. Data payload is optional. +extern bool __attribute__((overloadable)) + rsSendToClient(int cmdID); +extern bool __attribute__((overloadable)) + rsSendToClient(int cmdID, const void *data, uint len); + +// Send a message back to the client, blocking until the message is queued. +// A message ID is required. Data payload is optional. +extern void __attribute__((overloadable)) + rsSendToClientBlocking(int cmdID); +extern void __attribute__((overloadable)) + rsSendToClientBlocking(int cmdID, const void *data, uint len); + +// Script to Script +typedef struct rs_script_call { + uint32_t xStart; + uint32_t xEnd; + uint32_t yStart; + uint32_t yEnd; + uint32_t zStart; + uint32_t zEnd; + uint32_t arrayStart; + uint32_t arrayEnd; + +} rs_script_call_t; + +extern void __attribute__((overloadable)) + rsForEach(rs_script script, rs_allocation input, + rs_allocation output, const void * usrData); + +extern void __attribute__((overloadable)) + rsForEach(rs_script script, rs_allocation input, + rs_allocation output, const void * usrData, + const rs_script_call_t *); + +#endif diff --git a/libs/rs/scriptc/rs_types.rsh b/libs/rs/scriptc/rs_types.rsh index 4198a74..69e1aed 100644 --- a/libs/rs/scriptc/rs_types.rsh +++ b/libs/rs/scriptc/rs_types.rsh @@ -2,70 +2,71 @@ typedef char int8_t; typedef short int16_t; typedef int int32_t; -//typedef long int64_t; +typedef long long int64_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; -//typedef long uint64_t; +typedef unsigned long long uint64_t; typedef uint8_t uchar; typedef uint16_t ushort; typedef uint32_t uint; -//typedef uint64_t ulong; - -typedef int rs_element; -typedef int rs_type; -typedef int rs_allocation; -typedef int rs_sampler; -typedef int rs_script; -typedef int rs_mesh; -typedef int rs_program_fragment; -typedef int rs_program_vertex; -typedef int rs_program_raster; -typedef int rs_program_store; +typedef uint64_t ulong; + +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_element; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_type; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_allocation; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_sampler; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_script; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_mesh; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_fragment; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_vertex; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_raster; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_store; +typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_font; typedef float float2 __attribute__((ext_vector_type(2))); typedef float float3 __attribute__((ext_vector_type(3))); typedef float float4 __attribute__((ext_vector_type(4))); -typedef float float8 __attribute__((ext_vector_type(8))); -typedef float float16 __attribute__((ext_vector_type(16))); typedef uchar uchar2 __attribute__((ext_vector_type(2))); typedef uchar uchar3 __attribute__((ext_vector_type(3))); typedef uchar uchar4 __attribute__((ext_vector_type(4))); -typedef uchar uchar8 __attribute__((ext_vector_type(8))); -typedef uchar uchar16 __attribute__((ext_vector_type(16))); typedef ushort ushort2 __attribute__((ext_vector_type(2))); typedef ushort ushort3 __attribute__((ext_vector_type(3))); typedef ushort ushort4 __attribute__((ext_vector_type(4))); -typedef ushort ushort8 __attribute__((ext_vector_type(8))); -typedef ushort ushort16 __attribute__((ext_vector_type(16))); typedef uint uint2 __attribute__((ext_vector_type(2))); typedef uint uint3 __attribute__((ext_vector_type(3))); typedef uint uint4 __attribute__((ext_vector_type(4))); -typedef uint uint8 __attribute__((ext_vector_type(8))); -typedef uint uint16 __attribute__((ext_vector_type(16))); typedef char char2 __attribute__((ext_vector_type(2))); typedef char char3 __attribute__((ext_vector_type(3))); typedef char char4 __attribute__((ext_vector_type(4))); -typedef char char8 __attribute__((ext_vector_type(8))); -typedef char char16 __attribute__((ext_vector_type(16))); typedef short short2 __attribute__((ext_vector_type(2))); typedef short short3 __attribute__((ext_vector_type(3))); typedef short short4 __attribute__((ext_vector_type(4))); -typedef short short8 __attribute__((ext_vector_type(8))); -typedef short short16 __attribute__((ext_vector_type(16))); typedef int int2 __attribute__((ext_vector_type(2))); typedef int int3 __attribute__((ext_vector_type(3))); typedef int int4 __attribute__((ext_vector_type(4))); -typedef int int8 __attribute__((ext_vector_type(8))); -typedef int int16 __attribute__((ext_vector_type(16))); +typedef struct { + float m[16]; +} rs_matrix4x4; + +typedef struct { + float m[9]; +} rs_matrix3x3; + +typedef struct { + float m[4]; +} rs_matrix2x2; + + +#define RS_PACKED __attribute__((packed, aligned(4))) diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp index a1401ad..4362d14 100644 --- a/libs/utils/ResourceTypes.cpp +++ b/libs/utils/ResourceTypes.cpp @@ -317,6 +317,12 @@ status_t ResStringPool::setTo(const void* data, size_t size, bool copyData) mStringPoolSize = (mHeader->header.size-mHeader->stringsStart)/charSize; } else { + // check invariant: styles starts before end of data + if (mHeader->stylesStart >= (mHeader->header.size-sizeof(uint16_t))) { + LOGW("Bad style block: style block starts at %d past data size of %d\n", + (int)mHeader->stylesStart, (int)mHeader->header.size); + return (mError=BAD_TYPE); + } // check invariant: styles follow the strings if (mHeader->stylesStart <= mHeader->stringsStart) { LOGW("Bad style block: style block starts at %d, before strings at %d\n", @@ -1878,6 +1884,12 @@ bool ResTable::getResourceName(uint32_t resID, resource_name* outName) const outName->type = grp->basePackage->typeStrings.stringAt(t, &outName->typeLen); outName->name = grp->basePackage->keyStrings.stringAt( dtohl(entry->key.index), &outName->nameLen); + + // If we have a bad index for some reason, we should abort. + if (outName->type == NULL || outName->name == NULL) { + return false; + } + return true; } @@ -2609,6 +2621,24 @@ bool ResTable::expandResourceRef(const uint16_t* refStr, size_t refLen, *outType = *defType; } *outName = String16(p, end-p); + if(**outPackage == 0) { + if(outErrorMsg) { + *outErrorMsg = "Resource package cannot be an empty string"; + } + return false; + } + if(**outType == 0) { + if(outErrorMsg) { + *outErrorMsg = "Resource type cannot be an empty string"; + } + return false; + } + if(**outName == 0) { + if(outErrorMsg) { + *outErrorMsg = "Resource id cannot be an empty string"; + } + return false; + } return true; } @@ -4127,13 +4157,16 @@ void ResTable::print(bool inclValues) const | (0x00ff0000 & ((typeIndex+1)<<16)) | (0x0000ffff & (entryIndex)); resource_name resName; - this->getResourceName(resID, &resName); - printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n", - resID, - CHAR16_TO_CSTR(resName.package, resName.packageLen), - CHAR16_TO_CSTR(resName.type, resName.typeLen), - CHAR16_TO_CSTR(resName.name, resName.nameLen), - dtohl(typeConfigs->typeSpecFlags[entryIndex])); + if (this->getResourceName(resID, &resName)) { + printf(" spec resource 0x%08x %s:%s/%s: flags=0x%08x\n", + resID, + CHAR16_TO_CSTR(resName.package, resName.packageLen), + CHAR16_TO_CSTR(resName.type, resName.typeLen), + CHAR16_TO_CSTR(resName.name, resName.nameLen), + dtohl(typeConfigs->typeSpecFlags[entryIndex])); + } else { + printf(" INVALID TYPE CONFIG FOR RESOURCE 0x%08x\n", resID); + } } } for (size_t configIndex=0; configIndex<NTC; configIndex++) { @@ -4340,11 +4373,14 @@ void ResTable::print(bool inclValues) const | (0x00ff0000 & ((typeIndex+1)<<16)) | (0x0000ffff & (entryIndex)); resource_name resName; - this->getResourceName(resID, &resName); - printf(" resource 0x%08x %s:%s/%s: ", resID, - CHAR16_TO_CSTR(resName.package, resName.packageLen), - CHAR16_TO_CSTR(resName.type, resName.typeLen), - CHAR16_TO_CSTR(resName.name, resName.nameLen)); + if (this->getResourceName(resID, &resName)) { + printf(" resource 0x%08x %s:%s/%s: ", resID, + CHAR16_TO_CSTR(resName.package, resName.packageLen), + CHAR16_TO_CSTR(resName.type, resName.typeLen), + CHAR16_TO_CSTR(resName.name, resName.nameLen)); + } else { + printf(" INVALID RESOURCE 0x%08x: ", resID); + } if ((thisOffset&0x3) != 0) { printf("NON-INTEGER OFFSET: %p\n", (void*)thisOffset); continue; @@ -4402,18 +4438,19 @@ void ResTable::print(bool inclValues) const print_value(pkg, value); } else if (bagPtr != NULL) { const int N = dtohl(bagPtr->count); - const ResTable_map* mapPtr = (const ResTable_map*) - (((const uint8_t*)ent) + esize); + const uint8_t* baseMapPtr = (const uint8_t*)ent; + size_t mapOffset = esize; + const ResTable_map* mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); printf(" Parent=0x%08x, Count=%d\n", dtohl(bagPtr->parent.ident), N); - for (int i=0; i<N; i++) { + for (int i=0; i<N && mapOffset < (typeSize-sizeof(ResTable_map)); i++) { printf(" #%i (Key=0x%08x): ", i, dtohl(mapPtr->name.ident)); value.copyFrom_dtoh(mapPtr->value); print_value(pkg, value); const size_t size = dtohs(mapPtr->value.size); - mapPtr = (ResTable_map*)(((const uint8_t*)mapPtr) - + size + sizeof(*mapPtr)-sizeof(mapPtr->value)); + mapOffset += size + sizeof(*mapPtr)-sizeof(mapPtr->value); + mapPtr = (ResTable_map*)(baseMapPtr+mapOffset); } } } |