diff options
Diffstat (limited to 'libs/hwui')
-rw-r--r-- | libs/hwui/Android.mk | 25 | ||||
-rw-r--r-- | libs/hwui/GenerationCache.h | 230 | ||||
-rw-r--r-- | libs/hwui/Layer.h | 97 | ||||
-rw-r--r-- | libs/hwui/LayerCache.cpp | 160 | ||||
-rw-r--r-- | libs/hwui/LayerCache.h | 109 | ||||
-rw-r--r-- | libs/hwui/MODULE_LICENSE_APACHE2 | 0 | ||||
-rw-r--r-- | libs/hwui/Matrix.cpp | 212 | ||||
-rw-r--r-- | libs/hwui/Matrix.h | 117 | ||||
-rw-r--r-- | libs/hwui/NOTICE | 190 | ||||
-rw-r--r-- | libs/hwui/OpenGLRenderer.cpp | 618 | ||||
-rw-r--r-- | libs/hwui/OpenGLRenderer.h | 254 | ||||
-rw-r--r-- | libs/hwui/Program.cpp | 154 | ||||
-rw-r--r-- | libs/hwui/Program.h | 156 | ||||
-rw-r--r-- | libs/hwui/Rect.h | 141 | ||||
-rw-r--r-- | libs/hwui/Snapshot.h | 143 | ||||
-rw-r--r-- | libs/hwui/Texture.h | 54 | ||||
-rw-r--r-- | libs/hwui/TextureCache.cpp | 154 | ||||
-rw-r--r-- | libs/hwui/TextureCache.h | 90 | ||||
-rw-r--r-- | libs/hwui/shaders/drawColor.frag | 9 | ||||
-rw-r--r-- | libs/hwui/shaders/drawColor.vert | 17 | ||||
-rw-r--r-- | libs/hwui/shaders/drawTexture.frag | 12 | ||||
-rw-r--r-- | libs/hwui/shaders/drawTexture.vert | 20 |
22 files changed, 2962 insertions, 0 deletions
diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk new file mode 100644 index 0000000..9146ba6 --- /dev/null +++ b/libs/hwui/Android.mk @@ -0,0 +1,25 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + LayerCache.cpp \ + Matrix.cpp \ + OpenGLRenderer.cpp \ + Program.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_MODULE_CLASS := SHARED_LIBRARIES +LOCAL_SHARED_LIBRARIES := libcutils libutils libGLESv2 libskia +LOCAL_MODULE := libhwui +LOCAL_PRELINK_MODULE := false + +include $(BUILD_SHARED_LIBRARY) diff --git a/libs/hwui/GenerationCache.h b/libs/hwui/GenerationCache.h new file mode 100644 index 0000000..5c1b5e1 --- /dev/null +++ b/libs/hwui/GenerationCache.h @@ -0,0 +1,230 @@ +/* + * 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), index(e.index), parent(e.parent), child(e.child) { } + Entry(sp<Entry<EntryKey, EntryValue> > e): + key(e->key), value(e->value), index(e->index), parent(e->parent), child(e->child) { } + + EntryKey key; + EntryValue value; + ssize_t index; + + 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; + entry->index = 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()) { + return removeAt(mOldest->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/Layer.h b/libs/hwui/Layer.h new file mode 100644 index 0000000..586a05e --- /dev/null +++ b/libs/hwui/Layer.h @@ -0,0 +1,97 @@ +/* + * 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 <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; + } +}; + +/** + * 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..bcda45e --- /dev/null +++ b/libs/hwui/LayerCache.cpp @@ -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. + */ + +#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); + glBindTexture(GL_TEXTURE_2D, 0); + + // 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..adc6713 --- /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& bitmap, Layer*& texture); + + /** + * 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..877d3bb --- /dev/null +++ b/libs/hwui/Matrix.cpp @@ -0,0 +1,212 @@ +/* + * 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::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); + 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..40c80fa --- /dev/null +++ b/libs/hwui/Matrix.h @@ -0,0 +1,117 @@ +/* + * 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 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; + + 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..027ed79 --- /dev/null +++ b/libs/hwui/OpenGLRenderer.cpp @@ -0,0 +1,618 @@ +/* + * 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 <cutils/properties.h> +#include <utils/Log.h> + +#include "OpenGLRenderer.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Defines +/////////////////////////////////////////////////////////////////////////////// + +// 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 DEFAULT_TEXTURE_CACHE_SIZE 20 +#define DEFAULT_LAYER_CACHE_SIZE 10 + +// Converts a number of mega-bytes into bytes +#define MB(s) s * 1024 * 1024 + +// Generates simple and textured vertices +#define SV(x, y) { { x, y } } +#define FV(x, y, u, v) { { x, y }, { u, v } } + +/////////////////////////////////////////////////////////////////////////////// +// Globals +/////////////////////////////////////////////////////////////////////////////// + +static const SimpleVertex gDrawColorVertices[] = { + SV(0.0f, 0.0f), + SV(1.0f, 0.0f), + SV(0.0f, 1.0f), + SV(1.0f, 1.0f) +}; +static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex); +static const GLsizei gDrawColorVertexCount = 4; + +// This array is never used directly but used as a memcpy source in the +// OpenGLRenderer constructor +static const TextureVertex gDrawTextureVertices[] = { + 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 gDrawTextureVertexStride = sizeof(TextureVertex); +static const GLsizei gDrawTextureVertexCount = 4; + +// 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 } +}; + +/////////////////////////////////////////////////////////////////////////////// +// Constructors/destructor +/////////////////////////////////////////////////////////////////////////////// + +OpenGLRenderer::OpenGLRenderer(): + mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), + mLayerCache(MB(DEFAULT_LAYER_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(atoi(property))); + } else { + LOGD(" Using default texture cache size of %dMB", 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(atoi(property))); + } else { + LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE); + } + + mDrawColorShader = new DrawColorProgram; + mDrawTextureShader = new DrawTextureProgram; + + memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); +} + +OpenGLRenderer::~OpenGLRenderer() { + LOGD("Destroy OpenGLRenderer"); + + mTextureCache.clear(); + mLayerCache.clear(); +} + +/////////////////////////////////////////////////////////////////////////////// +// Setup +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::setViewport(int width, int height) { + glViewport(0, 0, width, height); + + mat4 ortho; + ortho.loadOrtho(0, width, height, 0, -1, 1); + ortho.copyTo(mOrthoMatrix); + + mWidth = width; + mHeight = height; + mFirstSnapshot.height = height; +} + +void OpenGLRenderer::prepare() { + mSnapshot = &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->clipRect.set(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) { + memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix)); + } + + 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->getMappedClip(); + glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); + + Layer* layer = current->layer; + + // Compute the correct texture coordinates for the FBO texture + // The texture is currently as big as the window but drawn with + // a quad of the appropriate size + const Rect& rect = layer->layer; + + drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, + layer->texture, layer->alpha, layer->mode, layer->blend, true); + + 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 %dx%d", size.width, size.height); + LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); + + GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; + LayerSize size(right - left, bottom - top); + + // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time + 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->clipRect.set(left, top, right, bottom); + mSnapshot->height = bottom - top; + setScissorFromClip(); + + mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho | + Snapshot::kFlagClipSet; + memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix)); + + // Change the ortho projection + mat4 ortho; + ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); + ortho.copyTo(mOrthoMatrix); + + return true; +} + +/////////////////////////////////////////////////////////////////////////////// +// Transforms +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::translate(float dx, float dy) { + mSnapshot->transform.translate(dx, dy, 0.0f); + mSnapshot->flags |= Snapshot::kFlagDirtyTransform; +} + +void OpenGLRenderer::rotate(float degrees) { + mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); + mSnapshot->flags |= Snapshot::kFlagDirtyTransform; +} + +void OpenGLRenderer::scale(float sx, float sy) { + mSnapshot->transform.scale(sx, sy, 1.0f); + mSnapshot->flags |= Snapshot::kFlagDirtyTransform; +} + +void OpenGLRenderer::setMatrix(SkMatrix* matrix) { + mSnapshot->transform.load(*matrix); + mSnapshot->flags |= Snapshot::kFlagDirtyTransform; +} + +void OpenGLRenderer::getMatrix(SkMatrix* matrix) { + mSnapshot->transform.copyTo(*matrix); +} + +void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { + mat4 m(*matrix); + mSnapshot->transform.multiply(m); + mSnapshot->flags |= Snapshot::kFlagDirtyTransform; +} + +/////////////////////////////////////////////////////////////////////////////// +// Clipping +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::setScissorFromClip() { + const Rect& clip = mSnapshot->getMappedClip(); + glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); +} + +const Rect& OpenGLRenderer::getClipBounds() { + return mSnapshot->clipRect; +} + +bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { + /* + * The documentation of quickReject() indicates that the specified rect + * is transformed before being compared to the clip rect. However, the + * clip rect is not stored transformed in the snapshot and can thus be + * compared directly + * + * The following code can be used instead to performed a mapped comparison: + * + * mSnapshot->transform.mapRect(r); + * const Rect& clip = mSnapshot->getMappedClip(); + * return !clip.intersects(r); + */ + Rect r(left, top, right, bottom); + return !mSnapshot->clipRect.intersects(r); +} + +bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) { + bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom); + if (clipped) { + mSnapshot->flags |= Snapshot::kFlagClipSet; + setScissorFromClip(); + } + return clipped; +} + +/////////////////////////////////////////////////////////////////////////////// +// Drawing +/////////////////////////////////////////////////////////////////////////////// + +void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { + const Texture* texture = mTextureCache.get(bitmap); + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id, + alpha / 255.0f, mode, texture->blend, true); +} + +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); + + const Texture* texture = mTextureCache.get(bitmap); + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id, + alpha / 255.0f, mode, texture->blend, true); +} + +void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, + float srcLeft, float srcTop, float srcRight, float srcBottom, + float dstLeft, float dstTop, float dstRight, float dstBottom, + const SkPaint* paint) { + const Texture* texture = mTextureCache.get(bitmap); + + int alpha; + SkXfermode::Mode mode; + getAlphaAndMode(paint, &alpha, &mode); + + 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->id, + alpha / 255.0f, mode, texture->blend, true); + + 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) { + // TODO: Implement + LOGD("Draw 9patch, paddingLeft=%d", patch->paddingLeft); +} + +void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { + const Rect& clip = mSnapshot->clipRect; + drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode); +} + +void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { + 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); +} + +void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, + int color, SkXfermode::Mode mode) { + const int alpha = (color >> 24) & 0xFF; + const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode; + + const GLfloat a = alpha / 255.0f; + const GLfloat r = ((color >> 16) & 0xFF) / 255.0f; + const GLfloat g = ((color >> 8) & 0xFF) / 255.0f; + const GLfloat b = ((color ) & 0xFF) / 255.0f; + + if (blend) { + glEnable(GL_BLEND); + glBlendFunc(gBlends[mode].src, gBlends[mode].dst); + } + + mModelView.loadTranslate(left, top, 0.0f); + mModelView.scale(right - left, bottom - top, 1.0f); + + mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); + + const GLvoid* p = &gDrawColorVertices[0].position[0]; + + glEnableVertexAttribArray(mDrawColorShader->position); + glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE, + gDrawColorVertexStride, p); + glVertexAttrib4f(mDrawColorShader->color, r, g, b, a); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); + + glDisableVertexAttribArray(mDrawColorShader->position); + + if (blend) { + glDisable(GL_BLEND); + } +} + +void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, + GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) { + mModelView.loadTranslate(left, top, 0.0f); + mModelView.scale(right - left, bottom - top, 1.0f); + + mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); + + if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) { + GLenum sourceMode = gBlends[mode].src; + if (!isPremultiplied && sourceMode == GL_ONE) { + sourceMode = GL_SRC_ALPHA; + } + + glEnable(GL_BLEND); + glBlendFunc(sourceMode, gBlends[mode].dst); + } + + glBindTexture(GL_TEXTURE_2D, texture); + + // TODO handle tiling and filtering here + + glActiveTexture(GL_TEXTURE0); + glUniform1i(mDrawTextureShader->sampler, 0); + + const GLvoid* p = &mDrawTextureVertices[0].position[0]; + const GLvoid* t = &mDrawTextureVertices[0].texture[0]; + + glEnableVertexAttribArray(mDrawTextureShader->position); + glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE, + gDrawTextureVertexStride, p); + + glEnableVertexAttribArray(mDrawTextureShader->texCoords); + glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE, + gDrawTextureVertexStride, t); + + glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); + + glDisableVertexAttribArray(mDrawTextureShader->position); + glDisableVertexAttribArray(mDrawTextureShader->texCoords); + + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_BLEND); +} + +void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { + mDrawTextureVertices[0].texture[0] = u1; + mDrawTextureVertices[0].texture[1] = v1; + mDrawTextureVertices[1].texture[0] = u2; + mDrawTextureVertices[1].texture[1] = v1; + mDrawTextureVertices[2].texture[0] = u1; + mDrawTextureVertices[2].texture[1] = v2; + mDrawTextureVertices[3].texture[0] = u2; + mDrawTextureVertices[3].texture[1] = 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; + } +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h new file mode 100644 index 0000000..9da4b8e --- /dev/null +++ b/libs/hwui/OpenGLRenderer.h @@ -0,0 +1,254 @@ +/* + * 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 <SkXfermode.h> + +#include <utils/RefBase.h> +#include <utils/ResourceTypes.h> + +#include "Matrix.h" +#include "Program.h" +#include "Rect.h" +#include "Snapshot.h" +#include "Texture.h" +#include "Layer.h" +#include "TextureCache.h" +#include "LayerCache.h" + +namespace android { +namespace uirenderer { + +/////////////////////////////////////////////////////////////////////////////// +// Support +/////////////////////////////////////////////////////////////////////////////// + +/** + * Simple structure to describe a vertex with a position. + * This is used to draw filled rectangles without a texture. + */ +struct SimpleVertex { + float position[2]; +}; // struct SimpleVertex + +/** + * Simple structure to describe a vertex with a position and a texture. + */ +struct TextureVertex { + float position[2]; + float texture[2]; +}; // struct TextureVertex + +/** + * Structure mapping Skia xfermodes to OpenGL blending factors. + */ +struct Blender { + SkXfermode::Mode mode; + GLenum src; + GLenum dst; +}; // struct Blender + +/////////////////////////////////////////////////////////////////////////////// +// 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); + + 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); + +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 + */ + void drawColorRect(float left, float top, float right, float bottom, + int color, SkXfermode::Mode mode); + + /** + * 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 + * @param isPremultiplied Indicates whether the texture has premultiplied alpha + */ + void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, + float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied = false); + + /** + * Resets the texture coordinates stored in mDrawTextureVertices. 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); + + // Dimensions of the drawing surface + int mWidth, mHeight; + + // Matrix used for ortho projection in shaders + float mOrthoMatrix[16]; + + // Model-view matrix used to position/size objects + mat4 mModelView; + + // Number of saved states + int mSaveCount; + // Base state + Snapshot mFirstSnapshot; + // Current state + sp<Snapshot> mSnapshot; + + // Shaders + sp<DrawColorProgram> mDrawColorShader; + sp<DrawTextureProgram> mDrawTextureShader; + + // Used to draw textured quads + TextureVertex mDrawTextureVertices[4]; + + // Used to cache all drawBitmap textures + TextureCache mTextureCache; + LayerCache mLayerCache; +}; // class OpenGLRenderer + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_OPENGL_RENDERER_H diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp new file mode 100644 index 0000000..2acddfc --- /dev/null +++ b/libs/hwui/Program.cpp @@ -0,0 +1,154 @@ +/* + * 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 + +#include "shaders/drawColor.vert" +#include "shaders/drawColor.frag" + +#include "shaders/drawTexture.vert" +#include "shaders/drawTexture.frag" + +/////////////////////////////////////////////////////////////////////////////// +// 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); + } +} + +Program::~Program() { + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + glDeleteProgram(id); +} + +void Program::use() { + glUseProgram(id); +} + +int Program::addAttrib(const char* name) { + int slot = glGetAttribLocation(id, name); + attributes.add(name, slot); + return slot; +} + +int Program::getAttrib(const char* name) { + return attributes.valueFor(name); +} + +int Program::addUniform(const char* name) { + int slot = glGetUniformLocation(id, name); + uniforms.add(name, slot); + return slot; +} + +int Program::getUniform(const char* name) { + return uniforms.valueFor(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; +} + +/////////////////////////////////////////////////////////////////////////////// +// Draw color +/////////////////////////////////////////////////////////////////////////////// + +DrawColorProgram::DrawColorProgram(): + Program(gDrawColorVertexShader, gDrawColorFragmentShader) { + getAttribsAndUniforms(); +} + +DrawColorProgram::DrawColorProgram(const char* vertex, const char* fragment): + Program(vertex, fragment) { + getAttribsAndUniforms(); +} + +void DrawColorProgram::getAttribsAndUniforms() { + position = addAttrib("position"); + color = addAttrib("color"); + projection = addUniform("projection"); + modelView = addUniform("modelView"); + transform = addUniform("transform"); +} + +void DrawColorProgram::use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix, + const GLfloat* transformMatrix) { + Program::use(); + glUniformMatrix4fv(projection, 1, GL_FALSE, projectionMatrix); + glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewMatrix); + glUniformMatrix4fv(transform, 1, GL_FALSE, transformMatrix); +} + +/////////////////////////////////////////////////////////////////////////////// +// Draw texture +/////////////////////////////////////////////////////////////////////////////// + +DrawTextureProgram::DrawTextureProgram(): + DrawColorProgram(gDrawTextureVertexShader, gDrawTextureFragmentShader) { + texCoords = addAttrib("texCoords"); + sampler = addUniform("sampler"); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/Program.h b/libs/hwui/Program.h new file mode 100644 index 0000000..ee16a92 --- /dev/null +++ b/libs/hwui/Program.h @@ -0,0 +1,156 @@ +/* + * 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 <utils/RefBase.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 LightRefBase<Program> { +public: + /** + * Creates a new program with the specified vertex and fragment + * shaders sources. + */ + Program(const char* vertex, const char* fragment); + ~Program(); + + /** + * Binds this program to the GL context. + */ + void use(); + +protected: + /** + * Adds an attribute with the specified name. + * + * @return The OpenGL name of the attribute. + */ + int addAttrib(const char* name); + /** + * Returns the OpenGL name of the specified attribute. + */ + int getAttrib(const char* name); + + /** + * Adds a uniform with the specified name. + * + * @return The OpenGL name of the uniform. + */ + int addUniform(const char* name); + /** + * Returns the OpenGL name of the specified uniform. + */ + int getUniform(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; +}; // class Program + +/** + * Program used to draw vertices with a simple color. The shaders must + * specify the following attributes: + * vec4 position, position of the vertex + * vec4 color, RGBA color of the vertex + * + * And the following uniforms: + * mat4 projection, the projection matrix + * mat4 modelView, the modelView matrix + * mat4 transform, an extra transformation matrix + */ +class DrawColorProgram: public Program { +public: + DrawColorProgram(); + DrawColorProgram(const char* vertex, const char* fragment); + + /** + * Binds the program with the specified projection, modelView and + * transform matrices. + */ + void use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix, + const GLfloat* transformMatrix); + + /** + * Name of the position attribute. + */ + int position; + /** + * Name of the color attribute. + */ + int color; + + /** + * Name of the projection uniform. + */ + int projection; + /** + * Name of the modelView uniform. + */ + int modelView; + /** + * Name of the transform uniform. + */ + int transform; + +protected: + void getAttribsAndUniforms(); +}; + +/** + * Program used to draw textured vertices. In addition to everything that the + * DrawColorProgram supports, the following two attributes must be specified: + * sampler2D sampler, the texture sampler + * vec2 texCoords, the texture coordinates of the vertex + */ +class DrawTextureProgram: public DrawColorProgram { +public: + DrawTextureProgram(); + + int sampler; + int texCoords; +}; + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_PROGRAM_H diff --git a/libs/hwui/Rect.h b/libs/hwui/Rect.h new file mode 100644 index 0000000..ad57550 --- /dev/null +++ b/libs/hwui/Rect.h @@ -0,0 +1,141 @@ +/* + * 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); + } + + 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/Snapshot.h b/libs/hwui/Snapshot.h new file mode 100644 index 0000000..17ca440 --- /dev/null +++ b/libs/hwui/Snapshot.h @@ -0,0 +1,143 @@ +/* + * 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 "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(): 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(kFlagDirtyTransform), + previous(s), + layer(NULL), + fbo(s->fbo) { + } + + /** + * 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 the snapshot holds new transform + * information. + */ + kFlagDirtyTransform = 0x2, + /** + * Indicates that this snapshot was created when saving + * a new layer. + */ + kFlagIsLayer = 0x4, + /** + * Indicates that this snapshot has changed the ortho matrix. + */ + kFlagDirtyOrtho = 0x8, + }; + + /** + * Returns the current clip region mapped by the current transform. + */ + const Rect& getMappedClip() { + if (flags & kFlagDirtyTransform) { + flags &= ~kFlagDirtyTransform; + mappedClip.set(clipRect); + transform.mapRect(mappedClip); + } + return mappedClip; + } + + /** + * 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. + */ + 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. + */ + float orthoMatrix[16]; + +private: + // Clipping rectangle mapped with the transform + Rect mappedClip; +}; // 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..d37013d --- /dev/null +++ b/libs/hwui/Texture.h @@ -0,0 +1,54 @@ +/* + * 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 { + /** + * 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; +}; // struct Texture + +}; // 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..612f04e --- /dev/null +++ b/libs/hwui/TextureCache.cpp @@ -0,0 +1,154 @@ +/* + * 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); +} + +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) { + 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 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::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 = true; + 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); + + glBindTexture(GL_TEXTURE_2D, 0); +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h new file mode 100644 index 0000000..bed1191 --- /dev/null +++ b/libs/hwui/TextureCache.h @@ -0,0 +1,90 @@ +/* + * 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; +}; // class TextureCache + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_UI_TEXTURE_CACHE_H diff --git a/libs/hwui/shaders/drawColor.frag b/libs/hwui/shaders/drawColor.frag new file mode 100644 index 0000000..e84c47b --- /dev/null +++ b/libs/hwui/shaders/drawColor.frag @@ -0,0 +1,9 @@ +SHADER_SOURCE(gDrawColorFragmentShader, + +varying lowp vec4 outColor; + +void main(void) { + gl_FragColor = outColor; +} + +); diff --git a/libs/hwui/shaders/drawColor.vert b/libs/hwui/shaders/drawColor.vert new file mode 100644 index 0000000..cef6e49 --- /dev/null +++ b/libs/hwui/shaders/drawColor.vert @@ -0,0 +1,17 @@ +SHADER_SOURCE(gDrawColorVertexShader, + +attribute vec4 position; +attribute vec4 color; + +uniform mat4 projection; +uniform mat4 modelView; +uniform mat4 transform; + +varying vec4 outColor; + +void main(void) { + outColor = color; + gl_Position = projection * transform * modelView * position; +} + +); diff --git a/libs/hwui/shaders/drawTexture.frag b/libs/hwui/shaders/drawTexture.frag new file mode 100644 index 0000000..5bd420e --- /dev/null +++ b/libs/hwui/shaders/drawTexture.frag @@ -0,0 +1,12 @@ +SHADER_SOURCE(gDrawTextureFragmentShader, + +varying lowp vec4 outColor; +varying mediump vec2 outTexCoords; + +uniform sampler2D sampler; + +void main(void) { + gl_FragColor = texture2D(sampler, outTexCoords) * outColor; +} + +); diff --git a/libs/hwui/shaders/drawTexture.vert b/libs/hwui/shaders/drawTexture.vert new file mode 100644 index 0000000..310a812 --- /dev/null +++ b/libs/hwui/shaders/drawTexture.vert @@ -0,0 +1,20 @@ +SHADER_SOURCE(gDrawTextureVertexShader, + +attribute vec4 position; +attribute vec2 texCoords; +attribute vec4 color; + +uniform mat4 projection; +uniform mat4 modelView; +uniform mat4 transform; + +varying vec4 outColor; +varying vec2 outTexCoords; + +void main(void) { + outColor = color; + outTexCoords = texCoords; + gl_Position = projection * transform * modelView * position; +} + +); |