2011-01-11 14:29:25 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-02-02 20:28:09 -08:00
|
|
|
#include "LayerCache.h"
|
2011-01-11 14:29:25 -08:00
|
|
|
#include "LayerRenderer.h"
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
#include "Matrix.h"
|
2011-01-12 14:30:59 -08:00
|
|
|
#include "Properties.h"
|
2011-02-01 22:59:58 -08:00
|
|
|
#include "Rect.h"
|
2015-01-26 18:06:29 -08:00
|
|
|
#include "renderstate/RenderState.h"
|
2015-06-23 10:33:46 -07:00
|
|
|
#include "utils/GLUtils.h"
|
2014-11-18 10:49:23 -08:00
|
|
|
#include "utils/TraceUtils.h"
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2015-01-26 18:06:29 -08:00
|
|
|
#include <ui/Rect.h>
|
|
|
|
|
|
|
|
#include <private/hwui/DrawGlInfo.h>
|
|
|
|
|
|
|
|
|
2011-01-11 14:29:25 -08:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Rendering
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
LayerRenderer::LayerRenderer(RenderState& renderState, Layer* layer)
|
|
|
|
: OpenGLRenderer(renderState)
|
|
|
|
, mLayer(layer) {
|
2011-10-12 13:48:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LayerRenderer::~LayerRenderer() {
|
|
|
|
}
|
|
|
|
|
2014-09-08 11:26:26 -04:00
|
|
|
void LayerRenderer::prepareDirty(float left, float top, float right, float bottom,
|
2012-10-18 15:05:02 -07:00
|
|
|
bool opaque) {
|
2011-07-07 20:50:11 -07:00
|
|
|
LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo());
|
2011-01-12 14:30:59 -08:00
|
|
|
|
2015-01-26 18:06:29 -08:00
|
|
|
mRenderState.bindFramebuffer(mLayer->getFbo());
|
2011-02-02 15:44:19 -08:00
|
|
|
|
2011-02-02 20:28:09 -08:00
|
|
|
const float width = mLayer->layer.getWidth();
|
|
|
|
const float height = mLayer->layer.getHeight();
|
|
|
|
|
2011-02-01 22:59:58 -08:00
|
|
|
Rect dirty(left, top, right, bottom);
|
|
|
|
if (dirty.isEmpty() || (dirty.left <= 0 && dirty.top <= 0 &&
|
2011-02-02 20:28:09 -08:00
|
|
|
dirty.right >= width && dirty.bottom >= height)) {
|
2011-02-01 22:59:58 -08:00
|
|
|
mLayer->region.clear();
|
2011-02-02 20:28:09 -08:00
|
|
|
dirty.set(0.0f, 0.0f, width, height);
|
2011-02-01 22:59:58 -08:00
|
|
|
} else {
|
2011-02-02 20:28:09 -08:00
|
|
|
dirty.intersect(0.0f, 0.0f, width, height);
|
2011-02-01 22:59:58 -08:00
|
|
|
android::Rect r(dirty.left, dirty.top, dirty.right, dirty.bottom);
|
|
|
|
mLayer->region.subtractSelf(r);
|
|
|
|
}
|
2013-01-29 17:26:25 -08:00
|
|
|
mLayer->clipRect.set(dirty);
|
2011-01-22 00:32:12 -08:00
|
|
|
|
2014-09-08 11:26:26 -04:00
|
|
|
OpenGLRenderer::prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, opaque);
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
2014-09-08 11:26:26 -04:00
|
|
|
void LayerRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
|
2012-10-18 15:05:02 -07:00
|
|
|
if (mLayer->isDirty()) {
|
2015-01-26 18:06:29 -08:00
|
|
|
mRenderState.scissor().setEnabled(false);
|
2012-10-18 15:05:02 -07:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2015-01-26 18:06:29 -08:00
|
|
|
mRenderState.scissor().reset();
|
2012-10-18 15:05:02 -07:00
|
|
|
mLayer->setDirty(false);
|
2014-09-08 11:26:26 -04:00
|
|
|
} else {
|
|
|
|
OpenGLRenderer::clear(left, top, right, bottom, opaque);
|
2012-10-18 15:05:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 11:26:26 -04:00
|
|
|
bool LayerRenderer::finish() {
|
|
|
|
bool retval = OpenGLRenderer::finish();
|
2011-01-12 14:30:59 -08:00
|
|
|
|
2011-01-16 12:54:25 -08:00
|
|
|
generateMesh();
|
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo());
|
2011-01-19 13:42:26 -08:00
|
|
|
|
|
|
|
// No need to unbind our FBO, this will be taken care of by the caller
|
|
|
|
// who will invoke OpenGLRenderer::resume()
|
2014-09-08 11:26:26 -04:00
|
|
|
return retval;
|
2011-01-19 13:42:26 -08:00
|
|
|
}
|
|
|
|
|
2015-02-27 10:55:28 -08:00
|
|
|
GLuint LayerRenderer::getTargetFbo() const {
|
2011-07-07 20:50:11 -07:00
|
|
|
return mLayer->getFbo();
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
2013-03-05 16:43:31 -08:00
|
|
|
bool LayerRenderer::suppressErrorChecks() const {
|
2012-09-21 00:39:43 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-11 14:29:25 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-01-15 18:51:42 -08:00
|
|
|
// Layer support
|
2011-01-16 12:54:25 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-05 16:43:31 -08:00
|
|
|
bool LayerRenderer::hasLayer() const {
|
2011-01-16 12:54:25 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-15 18:51:42 -08:00
|
|
|
void LayerRenderer::ensureStencilBuffer() {
|
|
|
|
attachStencilBufferToLayer(mLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Dirty region tracking
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-05 16:43:31 -08:00
|
|
|
Region* LayerRenderer::getRegion() const {
|
2014-10-10 13:38:16 -04:00
|
|
|
if (mState.currentFlags() & Snapshot::kFlagFboTarget) {
|
2011-01-16 12:54:25 -08:00
|
|
|
return OpenGLRenderer::getRegion();
|
|
|
|
}
|
|
|
|
return &mLayer->region;
|
|
|
|
}
|
|
|
|
|
2013-02-27 14:03:19 -08:00
|
|
|
// TODO: This implementation uses a very simple approach to fixing T-junctions which keeps the
|
|
|
|
// results as rectangles, and is thus not necessarily efficient in the geometry
|
|
|
|
// produced. Eventually, it may be better to develop triangle-based mechanism.
|
2011-01-16 12:54:25 -08:00
|
|
|
void LayerRenderer::generateMesh() {
|
|
|
|
if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
|
|
|
|
if (mLayer->mesh) {
|
2013-03-26 15:05:58 -07:00
|
|
|
delete[] mLayer->mesh;
|
2015-01-05 15:51:13 -08:00
|
|
|
mLayer->mesh = nullptr;
|
2011-01-16 12:54:25 -08:00
|
|
|
mLayer->meshElementCount = 0;
|
|
|
|
}
|
2011-03-18 14:34:03 -07:00
|
|
|
|
2011-04-27 14:21:41 -07:00
|
|
|
mLayer->setRegionAsRect();
|
2011-01-16 12:54:25 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-27 14:03:19 -08:00
|
|
|
// avoid T-junctions as they cause artifacts in between the resultant
|
|
|
|
// geometry when complex transforms occur.
|
|
|
|
// TODO: generate the safeRegion only if necessary based on drawing transform (see
|
|
|
|
// OpenGLRenderer::composeLayerRegion())
|
|
|
|
Region safeRegion = Region::createTJunctionFreeRegion(mLayer->region);
|
|
|
|
|
2011-01-16 12:54:25 -08:00
|
|
|
size_t count;
|
2013-02-27 14:03:19 -08:00
|
|
|
const android::Rect* rects = safeRegion.getArray(&count);
|
2011-01-16 12:54:25 -08:00
|
|
|
|
|
|
|
GLsizei elementCount = count * 6;
|
|
|
|
|
|
|
|
if (mLayer->mesh && mLayer->meshElementCount < elementCount) {
|
2013-03-26 15:05:58 -07:00
|
|
|
delete[] mLayer->mesh;
|
2015-01-05 15:51:13 -08:00
|
|
|
mLayer->mesh = nullptr;
|
2011-01-16 12:54:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLayer->mesh) {
|
|
|
|
mLayer->mesh = new TextureVertex[count * 4];
|
|
|
|
}
|
2011-01-26 22:41:43 -08:00
|
|
|
mLayer->meshElementCount = elementCount;
|
2011-01-16 12:54:25 -08:00
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
const float texX = 1.0f / float(mLayer->getWidth());
|
|
|
|
const float texY = 1.0f / float(mLayer->getHeight());
|
2011-01-16 12:54:25 -08:00
|
|
|
const float height = mLayer->layer.getHeight();
|
|
|
|
|
|
|
|
TextureVertex* mesh = mLayer->mesh;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
const android::Rect* r = &rects[i];
|
|
|
|
|
|
|
|
const float u1 = r->left * texX;
|
|
|
|
const float v1 = (height - r->top) * texY;
|
|
|
|
const float u2 = r->right * texX;
|
|
|
|
const float v2 = (height - r->bottom) * texY;
|
|
|
|
|
|
|
|
TextureVertex::set(mesh++, r->left, r->top, u1, v1);
|
|
|
|
TextureVertex::set(mesh++, r->right, r->top, u2, v1);
|
|
|
|
TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
|
|
|
|
TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Layers management
|
2011-01-11 14:29:25 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
Layer* LayerRenderer::createRenderLayer(RenderState& renderState, uint32_t width, uint32_t height) {
|
2014-11-18 10:49:23 -08:00
|
|
|
ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
|
2011-07-26 20:35:55 -07:00
|
|
|
LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height);
|
2011-01-12 14:30:59 -08:00
|
|
|
|
2011-12-13 14:55:06 -08:00
|
|
|
Caches& caches = Caches::getInstance();
|
|
|
|
GLuint fbo = caches.fboCache.get();
|
2011-02-02 20:28:09 -08:00
|
|
|
if (!fbo) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Could not obtain an FBO");
|
2015-01-05 15:51:13 -08:00
|
|
|
return nullptr;
|
2011-01-12 14:30:59 -08:00
|
|
|
}
|
|
|
|
|
2015-01-29 09:45:09 -08:00
|
|
|
caches.textureState().activateTexture(0);
|
2014-06-23 13:13:08 -07:00
|
|
|
Layer* layer = caches.layerCache.get(renderState, width, height);
|
2011-02-02 20:28:09 -08:00
|
|
|
if (!layer) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Could not obtain a layer");
|
2015-01-05 15:51:13 -08:00
|
|
|
return nullptr;
|
2011-02-02 20:28:09 -08:00
|
|
|
}
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2013-03-28 11:32:33 -07:00
|
|
|
// We first obtain a layer before comparing against the max texture size
|
|
|
|
// because layers are not allocated at the exact desired size. They are
|
|
|
|
// always created slighly larger to improve recycling
|
|
|
|
const uint32_t maxTextureSize = caches.maxTextureSize;
|
|
|
|
if (layer->getWidth() > maxTextureSize || layer->getHeight() > maxTextureSize) {
|
|
|
|
ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
|
|
|
|
width, height, maxTextureSize, maxTextureSize);
|
|
|
|
|
|
|
|
// Creating a new layer always increment its refcount by 1, this allows
|
|
|
|
// us to destroy the layer object if one was created for us
|
2015-01-05 15:51:13 -08:00
|
|
|
layer->decStrong(nullptr);
|
2013-03-28 11:32:33 -07:00
|
|
|
|
2015-01-05 15:51:13 -08:00
|
|
|
return nullptr;
|
2013-03-28 11:32:33 -07:00
|
|
|
}
|
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setFbo(fbo);
|
2011-02-02 20:28:09 -08:00
|
|
|
layer->layer.set(0.0f, 0.0f, width, height);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->texCoords.set(0.0f, height / float(layer->getHeight()),
|
|
|
|
width / float(layer->getWidth()), 0.0f);
|
|
|
|
layer->setAlpha(255, SkXfermode::kSrcOver_Mode);
|
2015-01-05 15:51:13 -08:00
|
|
|
layer->setColorFilter(nullptr);
|
2012-10-18 15:05:02 -07:00
|
|
|
layer->setDirty(true);
|
2011-02-02 20:28:09 -08:00
|
|
|
layer->region.clear();
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
GLuint previousFbo = renderState.getFramebuffer();
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
renderState.bindFramebuffer(layer->getFbo());
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->bindTexture();
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2011-02-02 20:28:09 -08:00
|
|
|
// Initialize the texture if needed
|
2011-07-07 20:50:11 -07:00
|
|
|
if (layer->isEmpty()) {
|
|
|
|
layer->setEmpty(false);
|
2013-04-04 12:27:54 -07:00
|
|
|
layer->allocateTexture();
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2013-03-28 11:32:33 -07:00
|
|
|
// This should only happen if we run out of memory
|
2015-06-23 10:33:46 -07:00
|
|
|
if (CC_UNLIKELY(GLUtils::dumpGLErrors())) {
|
|
|
|
LOG_ALWAYS_FATAL("Could not allocate texture for layer (fbo=%d %dx%d)",
|
|
|
|
fbo, width, height);
|
2014-06-23 13:13:08 -07:00
|
|
|
renderState.bindFramebuffer(previousFbo);
|
2015-01-05 15:51:13 -08:00
|
|
|
layer->decStrong(nullptr);
|
|
|
|
return nullptr;
|
2011-02-02 20:28:09 -08:00
|
|
|
}
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
2015-02-19 09:51:53 -08:00
|
|
|
layer->getTextureId(), 0);
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
renderState.bindFramebuffer(previousFbo);
|
2011-01-11 14:29:25 -08:00
|
|
|
|
2011-01-13 12:13:20 -08:00
|
|
|
return layer;
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
2011-01-13 12:13:20 -08:00
|
|
|
bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) {
|
|
|
|
if (layer) {
|
2011-07-07 20:50:11 -07:00
|
|
|
LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height);
|
2011-01-12 14:30:59 -08:00
|
|
|
|
2013-01-18 16:42:51 -08:00
|
|
|
if (layer->resize(width, height)) {
|
2011-02-02 20:28:09 -08:00
|
|
|
layer->layer.set(0.0f, 0.0f, width, height);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->texCoords.set(0.0f, height / float(layer->getHeight()),
|
|
|
|
width / float(layer->getWidth()), 0.0f);
|
2011-02-02 20:28:09 -08:00
|
|
|
} else {
|
2011-01-13 12:13:20 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 20:28:09 -08:00
|
|
|
|
2011-01-13 12:13:20 -08:00
|
|
|
return true;
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
Layer* LayerRenderer::createTextureLayer(RenderState& renderState) {
|
2011-06-24 17:53:53 -07:00
|
|
|
LAYER_RENDERER_LOGD("Creating new texture layer");
|
|
|
|
|
2014-09-08 16:40:21 -07:00
|
|
|
Layer* layer = new Layer(Layer::kType_Texture, renderState, 0, 0);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setCacheable(false);
|
2011-06-24 17:53:53 -07:00
|
|
|
layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f);
|
2011-10-11 14:06:21 -07:00
|
|
|
layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f);
|
2011-06-24 17:53:53 -07:00
|
|
|
layer->region.clear();
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer()
|
2011-06-24 17:53:53 -07:00
|
|
|
|
2015-01-29 09:45:09 -08:00
|
|
|
Caches::getInstance().textureState().activateTexture(0);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->generateTexture();
|
2011-06-24 17:53:53 -07:00
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
|
2014-02-25 18:50:17 -08:00
|
|
|
bool isOpaque, bool forceFilter, GLenum renderTarget, float* textureTransform) {
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
if (layer) {
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setBlend(!isOpaque);
|
2014-02-25 18:50:17 -08:00
|
|
|
layer->setForceFilter(forceFilter);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setSize(width, height);
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
layer->layer.set(0.0f, 0.0f, width, height);
|
|
|
|
layer->region.set(width, height);
|
|
|
|
layer->regionRect.set(0.0f, 0.0f, width, height);
|
2014-02-25 18:50:17 -08:00
|
|
|
layer->getTexTransform().load(textureTransform);
|
2011-05-02 17:24:22 -07:00
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
if (renderTarget != layer->getRenderTarget()) {
|
|
|
|
layer->setRenderTarget(renderTarget);
|
|
|
|
layer->bindTexture();
|
2011-11-30 20:21:23 -08:00
|
|
|
layer->setFilter(GL_NEAREST, false, true);
|
|
|
|
layer->setWrap(GL_CLAMP_TO_EDGE, false, true);
|
2011-06-24 17:53:53 -07:00
|
|
|
}
|
New widget: TextureView
Bug #4343984
TextureView can be used to render media content (video, OpenGL,
RenderScript) inside a View.
The key difference with SurfaceView is that TextureView does
not create a new Surface. This gives the ability to seamlessly
transform, animate, fade, etc. a TextureView, which was hard
if not impossible to do with a SurfaceView.
A TextureView also interacts perfectly with ScrollView,
ListView, etc. It allows application to embed media content
in a much more flexible way than before.
For instance, to render the camera preview at 50% opacity,
all you need to do is the following:
mTextureView.setAlpha(0.5f);
Camera c = Camera.open();
c.setPreviewTexture(mTextureView.getSurfaceTexture());
c.startPreview();
TextureView uses a SurfaceTexture to get the job done. More
APIs are required to make it easy to create OpenGL contexts
for a TextureView. It can currently be done with a bit of
JNI code.
Change-Id: Iaa7953097ab5beb8437bcbbfa03b2df5b7f80cd7
2011-04-28 18:40:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 12:13:20 -08:00
|
|
|
void LayerRenderer::destroyLayer(Layer* layer) {
|
|
|
|
if (layer) {
|
2014-11-18 10:49:23 -08:00
|
|
|
ATRACE_FORMAT("Destroy %ux%u HW Layer", layer->getWidth(), layer->getHeight());
|
2011-07-26 20:35:55 -07:00
|
|
|
LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d",
|
|
|
|
layer->getWidth(), layer->getHeight(), layer->getFbo());
|
2011-01-13 12:13:20 -08:00
|
|
|
|
2011-02-02 20:28:09 -08:00
|
|
|
if (!Caches::getInstance().layerCache.put(layer)) {
|
2011-07-26 20:35:55 -07:00
|
|
|
LAYER_RENDERER_LOGD(" Destroyed!");
|
2015-01-05 15:51:13 -08:00
|
|
|
layer->decStrong(nullptr);
|
2011-02-02 20:28:09 -08:00
|
|
|
} else {
|
2011-07-26 20:35:55 -07:00
|
|
|
LAYER_RENDERER_LOGD(" Cached!");
|
2011-07-27 18:51:50 -07:00
|
|
|
#if DEBUG_LAYER_RENDERER
|
2011-07-26 20:35:55 -07:00
|
|
|
Caches::getInstance().layerCache.dump();
|
|
|
|
#endif
|
2012-09-26 10:27:40 -07:00
|
|
|
layer->removeFbo();
|
2011-02-02 20:28:09 -08:00
|
|
|
layer->region.clear();
|
|
|
|
}
|
2011-01-13 12:13:20 -08:00
|
|
|
}
|
2011-01-11 14:29:25 -08:00
|
|
|
}
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
void LayerRenderer::flushLayer(RenderState& renderState, Layer* layer) {
|
2011-11-10 19:23:58 -08:00
|
|
|
#ifdef GL_EXT_discard_framebuffer
|
2014-06-12 10:42:54 +05:30
|
|
|
if (!layer) return;
|
|
|
|
|
2011-11-10 19:23:58 -08:00
|
|
|
GLuint fbo = layer->getFbo();
|
2014-06-12 10:42:54 +05:30
|
|
|
if (fbo) {
|
2011-11-10 19:23:58 -08:00
|
|
|
// If possible, discard any enqueud operations on deferred
|
|
|
|
// rendering architectures
|
2015-02-05 10:12:38 -08:00
|
|
|
if (Caches::getInstance().extensions().hasDiscardFramebuffer()) {
|
2014-06-23 13:13:08 -07:00
|
|
|
GLuint previousFbo = renderState.getFramebuffer();
|
|
|
|
if (fbo != previousFbo) {
|
|
|
|
renderState.bindFramebuffer(fbo);
|
|
|
|
}
|
2012-09-11 17:17:07 -07:00
|
|
|
|
|
|
|
const GLenum attachments[] = { GL_COLOR_ATTACHMENT0 };
|
|
|
|
glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
|
2011-11-10 19:23:58 -08:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
if (fbo != previousFbo) {
|
|
|
|
renderState.bindFramebuffer(previousFbo);
|
|
|
|
}
|
2011-11-10 19:23:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
bool LayerRenderer::copyLayer(RenderState& renderState, Layer* layer, SkBitmap* bitmap) {
|
2011-06-14 16:45:55 -07:00
|
|
|
Caches& caches = Caches::getInstance();
|
2015-02-26 16:28:17 -08:00
|
|
|
if (layer
|
|
|
|
&& bitmap->width() <= caches.maxTextureSize
|
|
|
|
&& bitmap->height() <= caches.maxTextureSize) {
|
2011-06-14 16:45:55 -07:00
|
|
|
|
|
|
|
GLuint fbo = caches.fboCache.get();
|
|
|
|
if (!fbo) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Could not obtain an FBO");
|
2011-06-14 16:45:55 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
SkAutoLockPixels alp(*bitmap);
|
|
|
|
|
2011-06-14 16:45:55 -07:00
|
|
|
GLuint texture;
|
|
|
|
GLuint previousFbo;
|
2014-06-23 13:13:08 -07:00
|
|
|
GLsizei previousViewportWidth;
|
|
|
|
GLsizei previousViewportHeight;
|
2011-06-14 16:45:55 -07:00
|
|
|
|
|
|
|
GLenum format;
|
|
|
|
GLenum type;
|
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
GLenum error = GL_NO_ERROR;
|
|
|
|
bool status = false;
|
|
|
|
|
2014-07-08 12:36:44 -04:00
|
|
|
switch (bitmap->colorType()) {
|
|
|
|
case kAlpha_8_SkColorType:
|
2011-06-14 16:45:55 -07:00
|
|
|
format = GL_ALPHA;
|
|
|
|
type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2014-07-08 12:36:44 -04:00
|
|
|
case kRGB_565_SkColorType:
|
2011-06-14 16:45:55 -07:00
|
|
|
format = GL_RGB;
|
|
|
|
type = GL_UNSIGNED_SHORT_5_6_5;
|
|
|
|
break;
|
2014-07-08 12:36:44 -04:00
|
|
|
case kARGB_4444_SkColorType:
|
2011-06-14 16:45:55 -07:00
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_4_4_4_4;
|
|
|
|
break;
|
2014-07-08 12:36:44 -04:00
|
|
|
case kN32_SkColorType:
|
2011-06-14 16:45:55 -07:00
|
|
|
default:
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
float alpha = layer->getAlpha();
|
|
|
|
SkXfermode::Mode mode = layer->getMode();
|
2012-09-17 17:25:49 -07:00
|
|
|
GLuint previousLayerFbo = layer->getFbo();
|
2011-06-17 17:45:59 -07:00
|
|
|
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setAlpha(255, SkXfermode::kSrc_Mode);
|
|
|
|
layer->setFbo(fbo);
|
2011-06-17 17:45:59 -07:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
previousFbo = renderState.getFramebuffer();
|
|
|
|
renderState.getViewport(&previousViewportWidth, &previousViewportHeight);
|
|
|
|
renderState.bindFramebuffer(fbo);
|
2011-06-14 16:45:55 -07:00
|
|
|
|
|
|
|
glGenTextures(1, &texture);
|
2011-06-17 17:45:59 -07:00
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) goto error;
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2015-01-29 09:45:09 -08:00
|
|
|
caches.textureState().activateTexture(0);
|
|
|
|
caches.textureState().bindTexture(texture);
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2012-09-07 18:42:38 -07:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
|
|
|
|
|
2011-07-07 21:05:04 -07:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2011-06-14 16:45:55 -07:00
|
|
|
|
|
|
|
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, format, bitmap->width(), bitmap->height(),
|
2015-01-05 15:51:13 -08:00
|
|
|
0, format, type, nullptr);
|
2011-06-17 17:45:59 -07:00
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) goto error;
|
|
|
|
|
2011-06-14 16:45:55 -07:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_TEXTURE_2D, texture, 0);
|
2011-06-17 17:45:59 -07:00
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) goto error;
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
{
|
2014-06-23 13:13:08 -07:00
|
|
|
LayerRenderer renderer(renderState, layer);
|
2011-06-17 17:45:59 -07:00
|
|
|
renderer.setViewport(bitmap->width(), bitmap->height());
|
|
|
|
renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f,
|
2011-07-07 20:50:11 -07:00
|
|
|
bitmap->width(), bitmap->height(), !layer->isBlend());
|
2011-10-11 14:06:21 -07:00
|
|
|
|
2015-01-26 18:06:29 -08:00
|
|
|
renderState.scissor().setEnabled(false);
|
2011-10-11 14:06:21 -07:00
|
|
|
renderer.translate(0.0f, bitmap->height());
|
|
|
|
renderer.scale(1.0f, -1.0f);
|
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) goto error;
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
{
|
|
|
|
Rect bounds;
|
|
|
|
bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height());
|
|
|
|
renderer.drawTextureLayer(layer, bounds);
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
|
|
|
|
type, bitmap->getPixels());
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) goto error;
|
|
|
|
}
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
status = true;
|
|
|
|
}
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
error:
|
|
|
|
#if DEBUG_OPENGL
|
|
|
|
if (error != GL_NO_ERROR) {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("GL error while copying layer into bitmap = 0x%x", error);
|
2011-06-17 17:45:59 -07:00
|
|
|
}
|
|
|
|
#endif
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2014-06-23 13:13:08 -07:00
|
|
|
renderState.bindFramebuffer(previousFbo);
|
2011-07-07 20:50:11 -07:00
|
|
|
layer->setAlpha(alpha, mode);
|
2012-09-17 17:25:49 -07:00
|
|
|
layer->setFbo(previousLayerFbo);
|
2015-01-29 09:45:09 -08:00
|
|
|
caches.textureState().deleteTexture(texture);
|
2011-06-14 16:45:55 -07:00
|
|
|
caches.fboCache.put(fbo);
|
2014-06-23 13:13:08 -07:00
|
|
|
renderState.setViewport(previousViewportWidth, previousViewportHeight);
|
2011-06-14 16:45:55 -07:00
|
|
|
|
2011-06-17 17:45:59 -07:00
|
|
|
return status;
|
2011-06-14 16:45:55 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-11 14:29:25 -08:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|