Decouple SurfaceTexture from HWUI
Remove all Skia and HWUI types from SurfaceTexture implementation. Move SurfaceTexture to libgui (ag/9578265). Define private C++ API for SurfaceTexture, which is consumed by DeferredLayerUpdater. Move AutoBackendTextureRelease/Skia code from SurfaceTexture to HWUI. Test: pass CtsUiRenderingTestCases and CtsViewTestCases Bug: 136263580 Change-Id: I3f971bb490f64a3ac0b2a66a89ba935bf7f08213
This commit is contained in:
@@ -179,6 +179,7 @@ cc_library_shared {
|
||||
"android_hardware_UsbRequest.cpp",
|
||||
"android_hardware_location_ActivityRecognitionHardware.cpp",
|
||||
"android_util_FileObserver.cpp",
|
||||
"android/graphics/SurfaceTexture.cpp",
|
||||
"android/opengl/poly_clip.cpp", // TODO: .arm
|
||||
"android/opengl/util.cpp",
|
||||
"android_server_NetworkManagementSocketTagger.cpp",
|
||||
@@ -431,7 +432,6 @@ cc_library_static {
|
||||
"android/graphics/GIFMovie.cpp",
|
||||
"android/graphics/Movie.cpp",
|
||||
"android/graphics/MovieImpl.cpp",
|
||||
"android/graphics/SurfaceTexture.cpp",
|
||||
"android/graphics/pdf/PdfDocument.cpp",
|
||||
"android/graphics/pdf/PdfEditor.cpp",
|
||||
"android/graphics/pdf/PdfRenderer.cpp",
|
||||
|
||||
@@ -24,7 +24,9 @@
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
#include <gui/Surface.h>
|
||||
#include <gui/surfacetexture/SurfaceTexture.h>
|
||||
#include <gui/BufferQueue.h>
|
||||
#include <gui/surfacetexture/surface_texture_platform.h>
|
||||
|
||||
#include "core_jni_helpers.h"
|
||||
|
||||
@@ -35,7 +37,6 @@
|
||||
#include "jni.h"
|
||||
#include <nativehelper/JNIHelp.h>
|
||||
#include <nativehelper/ScopedLocalRef.h>
|
||||
#include "surfacetexture/SurfaceTexture.h"
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -402,3 +403,6 @@ int register_android_graphics_SurfaceTexture(JNIEnv* env)
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
||||
//TODO: Move this file to frameworks/base/core/jni/android_graphics_SurfaceTexture.cpp. See
|
||||
//TODO: android_view_Surface.cpp for example.
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
#include "core_jni_helpers.h"
|
||||
#include "android_runtime/android_view_Surface.h"
|
||||
#include "android_runtime/android_graphics_SurfaceTexture.h"
|
||||
#include "surfacetexture/SurfaceTexture.h"
|
||||
|
||||
#include <gui/Surface.h>
|
||||
#include <gui/surfacetexture/SurfaceTexture.h>
|
||||
#include <gui/IGraphicBufferProducer.h>
|
||||
#include <gui/IProducerListener.h>
|
||||
#include <ui/GraphicBuffer.h>
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
#include "core_jni_helpers.h"
|
||||
#include <android_runtime/android_graphics_SurfaceTexture.h>
|
||||
|
||||
#include <gui/GLConsumer.h>
|
||||
#include <gui/IGraphicBufferProducer.h>
|
||||
#include <gui/surfacetexture/surface_texture_platform.h>
|
||||
#include <gui/surfacetexture/SurfaceTexture.h>
|
||||
#include <hwui/Paint.h>
|
||||
|
||||
#include <SkMatrix.h>
|
||||
@@ -64,7 +66,10 @@ static void TextureLayer_setTransform(JNIEnv* env, jobject clazz,
|
||||
static void TextureLayer_setSurfaceTexture(JNIEnv* env, jobject clazz,
|
||||
jlong layerUpdaterPtr, jobject surface) {
|
||||
DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
|
||||
layer->setSurfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface));
|
||||
auto consumer = SurfaceTexture_getSurfaceTexture(env, surface);
|
||||
auto producer = SurfaceTexture_getProducer(env, surface);
|
||||
layer->setSurfaceTexture(AutoTextureRelease(
|
||||
ASurfaceTexture_create(consumer, producer)));
|
||||
}
|
||||
|
||||
static void TextureLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz,
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#ifndef _ANDROID_GRAPHICS_SURFACETEXTURE_H
|
||||
#define _ANDROID_GRAPHICS_SURFACETEXTURE_H
|
||||
|
||||
#include <utils/StrongPointer.h>
|
||||
|
||||
#include "jni.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
@@ -235,12 +235,10 @@ cc_defaults {
|
||||
"renderthread/RenderProxy.cpp",
|
||||
"renderthread/RenderThread.cpp",
|
||||
"service/GraphicsStatsService.cpp",
|
||||
"surfacetexture/EGLConsumer.cpp",
|
||||
"surfacetexture/ImageConsumer.cpp",
|
||||
"surfacetexture/SurfaceTexture.cpp",
|
||||
"thread/CommonPool.cpp",
|
||||
"utils/GLUtils.cpp",
|
||||
"utils/StringUtils.cpp",
|
||||
"AutoBackendTextureRelease.cpp",
|
||||
"DeferredLayerUpdater.cpp",
|
||||
"DeviceInfo.cpp",
|
||||
"FrameInfo.cpp",
|
||||
|
||||
91
libs/hwui/AutoBackendTextureRelease.cpp
Normal file
91
libs/hwui/AutoBackendTextureRelease.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "AutoBackendTextureRelease.h"
|
||||
|
||||
#include "renderthread/RenderThread.h"
|
||||
#include "utils/Color.h"
|
||||
#include "utils/PaintUtils.h"
|
||||
|
||||
using namespace android::uirenderer::renderthread;
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
|
||||
AutoBackendTextureRelease::AutoBackendTextureRelease(GrContext* context, AHardwareBuffer* buffer) {
|
||||
AHardwareBuffer_Desc desc;
|
||||
AHardwareBuffer_describe(buffer, &desc);
|
||||
bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
|
||||
GrBackendFormat backendFormat =
|
||||
GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false);
|
||||
mBackendTexture = GrAHardwareBufferUtils::MakeBackendTexture(
|
||||
context, buffer, desc.width, desc.height, &mDeleteProc, &mUpdateProc, &mImageCtx,
|
||||
createProtectedImage, backendFormat, false);
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::unref(bool releaseImage) {
|
||||
if (!RenderThread::isCurrent()) {
|
||||
// EGLImage needs to be destroyed on RenderThread to prevent memory leak.
|
||||
// ~SkImage dtor for both pipelines needs to be invoked on RenderThread, because it is not
|
||||
// thread safe.
|
||||
RenderThread::getInstance().queue().post([this, releaseImage]() { unref(releaseImage); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (releaseImage) {
|
||||
mImage.reset();
|
||||
}
|
||||
|
||||
mUsageCount--;
|
||||
if (mUsageCount <= 0) {
|
||||
if (mBackendTexture.isValid()) {
|
||||
mDeleteProc(mImageCtx);
|
||||
mBackendTexture = {};
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
// releaseProc is invoked by SkImage, when texture is no longer in use.
|
||||
// "releaseContext" contains an "AutoBackendTextureRelease*".
|
||||
static void releaseProc(SkImage::ReleaseContext releaseContext) {
|
||||
AutoBackendTextureRelease* textureRelease =
|
||||
reinterpret_cast<AutoBackendTextureRelease*>(releaseContext);
|
||||
textureRelease->unref(false);
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::makeImage(AHardwareBuffer* buffer, android_dataspace dataspace,
|
||||
GrContext* context) {
|
||||
AHardwareBuffer_Desc desc;
|
||||
AHardwareBuffer_describe(buffer, &desc);
|
||||
SkColorType colorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
|
||||
mImage = SkImage::MakeFromTexture(
|
||||
context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType, kPremul_SkAlphaType,
|
||||
uirenderer::DataSpaceToColorSpace(dataspace), releaseProc, this);
|
||||
if (mImage.get()) {
|
||||
// The following ref will be counteracted by releaseProc, when SkImage is discarded.
|
||||
ref();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::newBufferContent(GrContext* context) {
|
||||
if (mBackendTexture.isValid()) {
|
||||
mUpdateProc(mImageCtx, context);
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace uirenderer */
|
||||
} /* namespace android */
|
||||
67
libs/hwui/AutoBackendTextureRelease.h
Normal file
67
libs/hwui/AutoBackendTextureRelease.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <GrAHardwareBufferUtils.h>
|
||||
#include <GrBackendSurface.h>
|
||||
#include <SkImage.h>
|
||||
#include <android/hardware_buffer.h>
|
||||
#include <system/graphics.h>
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
|
||||
/**
|
||||
* AutoBackendTextureRelease manages EglImage/VkImage lifetime. It is a ref-counted object
|
||||
* that keeps GPU resources alive until the last SkImage object using them is destroyed.
|
||||
*/
|
||||
class AutoBackendTextureRelease final {
|
||||
public:
|
||||
AutoBackendTextureRelease(GrContext* context, AHardwareBuffer* buffer);
|
||||
|
||||
const GrBackendTexture& getTexture() const { return mBackendTexture; }
|
||||
|
||||
// Only called on the RenderThread, so it need not be thread-safe.
|
||||
void ref() { mUsageCount++; }
|
||||
|
||||
void unref(bool releaseImage);
|
||||
|
||||
inline sk_sp<SkImage> getImage() const { return mImage; }
|
||||
|
||||
void makeImage(AHardwareBuffer* buffer, android_dataspace dataspace, GrContext* context);
|
||||
|
||||
void newBufferContent(GrContext* context);
|
||||
|
||||
private:
|
||||
// The only way to invoke dtor is with unref, when mUsageCount is 0.
|
||||
~AutoBackendTextureRelease() {}
|
||||
|
||||
GrBackendTexture mBackendTexture;
|
||||
GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
|
||||
GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
|
||||
GrAHardwareBufferUtils::TexImageCtx mImageCtx;
|
||||
|
||||
// Starting with refcount 1, because the first ref is held by SurfaceTexture. Additional refs
|
||||
// are held by SkImages.
|
||||
int mUsageCount = 1;
|
||||
|
||||
// mImage is the SkImage created from mBackendTexture.
|
||||
sk_sp<SkImage> mImage;
|
||||
};
|
||||
|
||||
} /* namespace uirenderer */
|
||||
} /* namespace android */
|
||||
@@ -18,8 +18,15 @@
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
#include "AutoBackendTextureRelease.h"
|
||||
#include "Matrix.h"
|
||||
#include "Properties.h"
|
||||
#include "renderstate/RenderState.h"
|
||||
#include "utils/PaintUtils.h"
|
||||
#include "renderthread/EglManager.h"
|
||||
#include "renderthread/RenderThread.h"
|
||||
#include "renderthread/VulkanManager.h"
|
||||
|
||||
using namespace android::uirenderer::renderthread;
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
@@ -27,7 +34,6 @@ namespace uirenderer {
|
||||
DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState)
|
||||
: mRenderState(renderState)
|
||||
, mBlend(false)
|
||||
, mSurfaceTexture(nullptr)
|
||||
, mTransform(nullptr)
|
||||
, mGLContextAttached(false)
|
||||
, mUpdateTexImage(false)
|
||||
@@ -41,14 +47,12 @@ DeferredLayerUpdater::~DeferredLayerUpdater() {
|
||||
destroyLayer();
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::setSurfaceTexture(const sp<SurfaceTexture>& consumer) {
|
||||
if (consumer.get() != mSurfaceTexture.get()) {
|
||||
mSurfaceTexture = consumer;
|
||||
void DeferredLayerUpdater::setSurfaceTexture(AutoTextureRelease&& consumer) {
|
||||
mSurfaceTexture = std::move(consumer);
|
||||
|
||||
GLenum target = consumer->getCurrentTextureTarget();
|
||||
LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
|
||||
"set unsupported SurfaceTexture with target %x", target);
|
||||
}
|
||||
GLenum target = ASurfaceTexture_getCurrentTextureTarget(mSurfaceTexture.get());
|
||||
LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
|
||||
"set unsupported SurfaceTexture with target %x", target);
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::onContextDestroyed() {
|
||||
@@ -61,13 +65,15 @@ void DeferredLayerUpdater::destroyLayer() {
|
||||
}
|
||||
|
||||
if (mSurfaceTexture.get() && mGLContextAttached) {
|
||||
mSurfaceTexture->detachFromView();
|
||||
ASurfaceTexture_releaseConsumerOwnership(mSurfaceTexture.get());
|
||||
mGLContextAttached = false;
|
||||
}
|
||||
|
||||
mLayer->postDecStrong();
|
||||
|
||||
mLayer = nullptr;
|
||||
|
||||
mImageSlots.clear();
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
|
||||
@@ -80,6 +86,35 @@ void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
|
||||
}
|
||||
}
|
||||
|
||||
static status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, EGLDisplay* display,
|
||||
int* releaseFence, void* handle) {
|
||||
*display = EGL_NO_DISPLAY;
|
||||
RenderState* renderState = (RenderState*)handle;
|
||||
status_t err;
|
||||
if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
|
||||
EglManager& eglManager = renderState->getRenderThread().eglManager();
|
||||
*display = eglManager.eglDisplay();
|
||||
err = eglManager.createReleaseFence(useFenceSync, eglFence, releaseFence);
|
||||
} else {
|
||||
err = renderState->getRenderThread().vulkanManager().createReleaseFence(
|
||||
releaseFence, renderState->getRenderThread().getGrContext());
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static status_t fenceWait(int fence, void* handle) {
|
||||
// Wait on the producer fence for the buffer to be ready.
|
||||
status_t err;
|
||||
RenderState* renderState = (RenderState*)handle;
|
||||
if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
|
||||
err = renderState->getRenderThread().eglManager().fenceWait(fence);
|
||||
} else {
|
||||
err = renderState->getRenderThread().vulkanManager().fenceWait(
|
||||
fence, renderState->getRenderThread().getGrContext());
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::apply() {
|
||||
if (!mLayer) {
|
||||
mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode);
|
||||
@@ -92,24 +127,33 @@ void DeferredLayerUpdater::apply() {
|
||||
if (!mGLContextAttached) {
|
||||
mGLContextAttached = true;
|
||||
mUpdateTexImage = true;
|
||||
mSurfaceTexture->attachToView();
|
||||
ASurfaceTexture_takeConsumerOwnership(mSurfaceTexture.get());
|
||||
}
|
||||
if (mUpdateTexImage) {
|
||||
mUpdateTexImage = false;
|
||||
sk_sp<SkImage> layerImage;
|
||||
SkMatrix textureTransform;
|
||||
bool queueEmpty = true;
|
||||
// If the SurfaceTexture queue is in synchronous mode, need to discard all
|
||||
// but latest frame. Since we can't tell which mode it is in,
|
||||
// do this unconditionally.
|
||||
do {
|
||||
layerImage = mSurfaceTexture->dequeueImage(textureTransform, &queueEmpty,
|
||||
mRenderState);
|
||||
} while (layerImage.get() && (!queueEmpty));
|
||||
if (layerImage.get()) {
|
||||
// force filtration if buffer size != layer size
|
||||
bool forceFilter = mWidth != layerImage->width() || mHeight != layerImage->height();
|
||||
updateLayer(forceFilter, textureTransform, layerImage);
|
||||
float transformMatrix[16];
|
||||
android_dataspace dataspace;
|
||||
int slot;
|
||||
bool newContent = false;
|
||||
// Note: ASurfaceTexture_dequeueBuffer discards all but the last frame. This
|
||||
// is necessary if the SurfaceTexture queue is in synchronous mode, and we
|
||||
// cannot tell which mode it is in.
|
||||
AHardwareBuffer* hardwareBuffer = ASurfaceTexture_dequeueBuffer(
|
||||
mSurfaceTexture.get(), &slot, &dataspace, transformMatrix, &newContent,
|
||||
createReleaseFence, fenceWait, &mRenderState);
|
||||
|
||||
if (hardwareBuffer) {
|
||||
sk_sp<SkImage> layerImage = mImageSlots[slot].createIfNeeded(
|
||||
hardwareBuffer, dataspace, newContent,
|
||||
mRenderState.getRenderThread().getGrContext());
|
||||
if (layerImage.get()) {
|
||||
SkMatrix textureTransform;
|
||||
mat4(transformMatrix).copyTo(textureTransform);
|
||||
// force filtration if buffer size != layer size
|
||||
bool forceFilter =
|
||||
mWidth != layerImage->width() || mHeight != layerImage->height();
|
||||
updateLayer(forceFilter, textureTransform, layerImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +165,7 @@ void DeferredLayerUpdater::apply() {
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform,
|
||||
const sk_sp<SkImage>& layerImage) {
|
||||
const sk_sp<SkImage>& layerImage) {
|
||||
mLayer->setBlend(mBlend);
|
||||
mLayer->setForceFilter(forceFilter);
|
||||
mLayer->setSize(mWidth, mHeight);
|
||||
@@ -136,5 +180,42 @@ void DeferredLayerUpdater::detachSurfaceTexture() {
|
||||
}
|
||||
}
|
||||
|
||||
sk_sp<SkImage> DeferredLayerUpdater::ImageSlot::createIfNeeded(AHardwareBuffer* buffer,
|
||||
android_dataspace dataspace,
|
||||
bool forceCreate,
|
||||
GrContext* context) {
|
||||
if (!mTextureRelease || !mTextureRelease->getImage().get() || dataspace != mDataspace ||
|
||||
forceCreate || mBuffer != buffer) {
|
||||
if (buffer != mBuffer) {
|
||||
clear();
|
||||
}
|
||||
|
||||
if (!buffer) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!mTextureRelease) {
|
||||
mTextureRelease = new AutoBackendTextureRelease(context, buffer);
|
||||
} else {
|
||||
mTextureRelease->newBufferContent(context);
|
||||
}
|
||||
|
||||
mDataspace = dataspace;
|
||||
mBuffer = buffer;
|
||||
mTextureRelease->makeImage(buffer, dataspace, context);
|
||||
}
|
||||
return mTextureRelease ? mTextureRelease->getImage() : nullptr;
|
||||
}
|
||||
|
||||
void DeferredLayerUpdater::ImageSlot::clear() {
|
||||
if (mTextureRelease) {
|
||||
// The following unref counteracts the initial mUsageCount of 1, set by default initializer.
|
||||
mTextureRelease->unref(true);
|
||||
mTextureRelease = nullptr;
|
||||
}
|
||||
|
||||
mBuffer = nullptr;
|
||||
}
|
||||
|
||||
} /* namespace uirenderer */
|
||||
} /* namespace android */
|
||||
|
||||
@@ -19,21 +19,26 @@
|
||||
#include <SkColorFilter.h>
|
||||
#include <SkImage.h>
|
||||
#include <SkMatrix.h>
|
||||
#include <android/hardware_buffer.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <map>
|
||||
#include <system/graphics.h>
|
||||
#include <utils/StrongPointer.h>
|
||||
// TODO: Use public SurfaceTexture APIs once available and include public NDK header file instead.
|
||||
#include <gui/surfacetexture/surface_texture_platform.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include "renderstate/RenderState.h"
|
||||
#include "surfacetexture/SurfaceTexture.h"
|
||||
#include "Layer.h"
|
||||
#include "Rect.h"
|
||||
#include "renderstate/RenderState.h"
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
|
||||
class AutoBackendTextureRelease;
|
||||
class RenderState;
|
||||
|
||||
typedef std::unique_ptr<ASurfaceTexture> AutoTextureRelease;
|
||||
|
||||
// Container to hold the properties a layer should be set to at the start
|
||||
// of a render pass
|
||||
class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
|
||||
@@ -64,7 +69,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
ANDROID_API void setSurfaceTexture(const sp<SurfaceTexture>& consumer);
|
||||
ANDROID_API void setSurfaceTexture(AutoTextureRelease&& consumer);
|
||||
|
||||
ANDROID_API void updateTexImage() { mUpdateTexImage = true; }
|
||||
|
||||
@@ -92,6 +97,39 @@ protected:
|
||||
void onContextDestroyed() override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* ImageSlot contains the information and object references that
|
||||
* DeferredLayerUpdater maintains about a slot. Slot id comes from
|
||||
* ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
|
||||
*/
|
||||
class ImageSlot {
|
||||
public:
|
||||
~ImageSlot() { clear(); }
|
||||
|
||||
sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
|
||||
bool forceCreate, GrContext* context);
|
||||
|
||||
private:
|
||||
void clear();
|
||||
|
||||
// the dataspace associated with the current image
|
||||
android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
|
||||
|
||||
AHardwareBuffer* mBuffer = nullptr;
|
||||
|
||||
/**
|
||||
* mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
|
||||
* DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
|
||||
*/
|
||||
AutoBackendTextureRelease* mTextureRelease = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
|
||||
* for each buffer slot.
|
||||
*/
|
||||
std::map<int, ImageSlot> mImageSlots;
|
||||
|
||||
RenderState& mRenderState;
|
||||
|
||||
// Generic properties
|
||||
@@ -101,7 +139,7 @@ private:
|
||||
sk_sp<SkColorFilter> mColorFilter;
|
||||
int mAlpha = 255;
|
||||
SkBlendMode mMode = SkBlendMode::kSrcOver;
|
||||
sp<SurfaceTexture> mSurfaceTexture;
|
||||
AutoTextureRelease mSurfaceTexture;
|
||||
SkMatrix* mTransform;
|
||||
bool mGLContextAttached;
|
||||
bool mUpdateTexImage;
|
||||
|
||||
@@ -16,22 +16,22 @@
|
||||
|
||||
#include "EglManager.h"
|
||||
|
||||
#include <EGL/eglext.h>
|
||||
#include <GLES/gl.h>
|
||||
#include <cutils/properties.h>
|
||||
#include <log/log.h>
|
||||
#include <private/gui/SyncFeatures.h>
|
||||
#include <sync/sync.h>
|
||||
#include <system/window.h>
|
||||
#include <utils/Trace.h>
|
||||
#include "utils/Color.h"
|
||||
#include "utils/StringUtils.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Frame.h"
|
||||
#include "Properties.h"
|
||||
|
||||
#include <EGL/eglext.h>
|
||||
#include <GLES/gl.h>
|
||||
|
||||
#include <system/window.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "utils/Color.h"
|
||||
#include "utils/StringUtils.h"
|
||||
|
||||
#define GLES_VERSION 2
|
||||
|
||||
@@ -508,7 +508,21 @@ bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
|
||||
return preserved;
|
||||
}
|
||||
|
||||
status_t EglManager::fenceWait(sp<Fence>& fence) {
|
||||
static status_t waitForeverOnFence(int fence, const char* logname) {
|
||||
ATRACE_CALL();
|
||||
if (fence == -1) {
|
||||
return NO_ERROR;
|
||||
}
|
||||
constexpr int warningTimeout = 3000;
|
||||
int err = sync_wait(fence, warningTimeout);
|
||||
if (err < 0 && errno == ETIME) {
|
||||
ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
|
||||
err = sync_wait(fence, -1);
|
||||
}
|
||||
return err < 0 ? -errno : status_t(NO_ERROR);
|
||||
}
|
||||
|
||||
status_t EglManager::fenceWait(int fence) {
|
||||
if (!hasEglContext()) {
|
||||
ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
|
||||
return INVALID_OPERATION;
|
||||
@@ -518,7 +532,7 @@ status_t EglManager::fenceWait(sp<Fence>& fence) {
|
||||
SyncFeatures::getInstance().useNativeFenceSync()) {
|
||||
// Block GPU on the fence.
|
||||
// Create an EGLSyncKHR from the current fence.
|
||||
int fenceFd = fence->dup();
|
||||
int fenceFd = ::dup(fence);
|
||||
if (fenceFd == -1) {
|
||||
ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
|
||||
return -errno;
|
||||
@@ -543,7 +557,7 @@ status_t EglManager::fenceWait(sp<Fence>& fence) {
|
||||
}
|
||||
} else {
|
||||
// Block CPU on the fence.
|
||||
status_t err = fence->waitForever("EglManager::fenceWait");
|
||||
status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
|
||||
if (err != NO_ERROR) {
|
||||
ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
|
||||
return err;
|
||||
@@ -552,8 +566,8 @@ status_t EglManager::fenceWait(sp<Fence>& fence) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
|
||||
sp<Fence>& nativeFence) {
|
||||
status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
|
||||
*nativeFence = -1;
|
||||
if (!hasEglContext()) {
|
||||
ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
|
||||
return INVALID_OPERATION;
|
||||
@@ -574,7 +588,7 @@ status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
|
||||
eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
nativeFence = new Fence(fenceFd);
|
||||
*nativeFence = fenceFd;
|
||||
*eglFence = EGL_NO_SYNC_KHR;
|
||||
} else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
|
||||
if (*eglFence != EGL_NO_SYNC_KHR) {
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
#include <SkImageInfo.h>
|
||||
#include <SkRect.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <ui/Fence.h>
|
||||
#include <ui/GraphicBuffer.h>
|
||||
#include <utils/StrongPointer.h>
|
||||
|
||||
#include "IRenderPipeline.h"
|
||||
#include "utils/Result.h"
|
||||
|
||||
@@ -74,11 +74,11 @@ public:
|
||||
|
||||
// Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
|
||||
// support is missing, block the CPU on the fence.
|
||||
status_t fenceWait(sp<Fence>& fence);
|
||||
status_t fenceWait(int fence);
|
||||
|
||||
// Creates a fence that is signaled, when all the pending GL commands are flushed.
|
||||
// Depending on installed extensions, the result is either Android native fence or EGL fence.
|
||||
status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, sp<Fence>& nativeFence);
|
||||
status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence);
|
||||
|
||||
private:
|
||||
enum class SwapBehavior {
|
||||
|
||||
@@ -17,33 +17,32 @@
|
||||
#ifndef RENDERTHREAD_H_
|
||||
#define RENDERTHREAD_H_
|
||||
|
||||
#include "RenderTask.h"
|
||||
#include <GrContext.h>
|
||||
#include <SkBitmap.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <thread/ThreadBase.h>
|
||||
#include <utils/Looper.h>
|
||||
#include <utils/Thread.h>
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
|
||||
#include "CacheManager.h"
|
||||
#include "ProfileDataContainer.h"
|
||||
#include "RenderTask.h"
|
||||
#include "TimeLord.h"
|
||||
#include "WebViewFunctorManager.h"
|
||||
#include "thread/ThreadBase.h"
|
||||
#include "utils/TimeUtils.h"
|
||||
|
||||
#include <GrContext.h>
|
||||
#include <SkBitmap.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <utils/Looper.h>
|
||||
#include <utils/Thread.h>
|
||||
|
||||
#include <thread/ThreadBase.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
|
||||
namespace android {
|
||||
|
||||
class Bitmap;
|
||||
class AutoBackendTextureRelease;
|
||||
|
||||
namespace uirenderer {
|
||||
|
||||
class AutoBackendTextureRelease;
|
||||
class Readback;
|
||||
class RenderState;
|
||||
class TestUtils;
|
||||
@@ -137,7 +136,7 @@ private:
|
||||
friend class DispatchFrameCallbacks;
|
||||
friend class RenderProxy;
|
||||
friend class DummyVsyncSource;
|
||||
friend class android::AutoBackendTextureRelease;
|
||||
friend class android::uirenderer::AutoBackendTextureRelease;
|
||||
friend class android::uirenderer::TestUtils;
|
||||
friend class android::uirenderer::WebViewFunctor;
|
||||
friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
|
||||
|
||||
@@ -16,9 +16,15 @@
|
||||
|
||||
#include "VulkanManager.h"
|
||||
|
||||
#include <android/sync.h>
|
||||
#include <EGL/egl.h>
|
||||
#include <EGL/eglext.h>
|
||||
#include <GrBackendSemaphore.h>
|
||||
#include <GrBackendSurface.h>
|
||||
#include <GrContext.h>
|
||||
#include <GrTypes.h>
|
||||
#include <android/sync.h>
|
||||
#include <vk/GrVkExtensions.h>
|
||||
#include <vk/GrVkTypes.h>
|
||||
|
||||
#include "Properties.h"
|
||||
#include "RenderThread.h"
|
||||
@@ -26,13 +32,6 @@
|
||||
#include "utils/FatVector.h"
|
||||
#include "utils/TraceUtils.h"
|
||||
|
||||
#include <GrBackendSemaphore.h>
|
||||
#include <GrBackendSurface.h>
|
||||
#include <GrContext.h>
|
||||
#include <GrTypes.h>
|
||||
#include <vk/GrVkExtensions.h>
|
||||
#include <vk/GrVkTypes.h>
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
namespace renderthread {
|
||||
@@ -482,7 +481,7 @@ struct DestroySemaphoreInfo {
|
||||
int mRefs = 2;
|
||||
|
||||
DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
|
||||
VkSemaphore semaphore)
|
||||
VkSemaphore semaphore)
|
||||
: mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
|
||||
};
|
||||
|
||||
@@ -524,12 +523,11 @@ void VulkanManager::swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect)
|
||||
backendSemaphore.initVulkan(semaphore);
|
||||
|
||||
int fenceFd = -1;
|
||||
DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
|
||||
semaphore);
|
||||
GrSemaphoresSubmitted submitted =
|
||||
bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
|
||||
kNone_GrFlushFlags, 1, &backendSemaphore,
|
||||
destroy_semaphore, destroyInfo);
|
||||
DestroySemaphoreInfo* destroyInfo =
|
||||
new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
|
||||
GrSemaphoresSubmitted submitted = bufferInfo->skSurface->flush(
|
||||
SkSurface::BackendSurfaceAccess::kPresent, kNone_GrFlushFlags, 1, &backendSemaphore,
|
||||
destroy_semaphore, destroyInfo);
|
||||
if (submitted == GrSemaphoresSubmitted::kYes) {
|
||||
VkSemaphoreGetFdInfoKHR getFdInfo;
|
||||
getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
|
||||
@@ -571,14 +569,14 @@ VulkanSurface* VulkanManager::createSurface(ANativeWindow* window, ColorMode col
|
||||
*this, extraBuffers);
|
||||
}
|
||||
|
||||
status_t VulkanManager::fenceWait(sp<Fence>& fence, GrContext* grContext) {
|
||||
status_t VulkanManager::fenceWait(int fence, GrContext* grContext) {
|
||||
if (!hasVkContext()) {
|
||||
ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
// Block GPU on the fence.
|
||||
int fenceFd = fence->dup();
|
||||
int fenceFd = ::dup(fence);
|
||||
if (fenceFd == -1) {
|
||||
ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
|
||||
return -errno;
|
||||
@@ -619,7 +617,8 @@ status_t VulkanManager::fenceWait(sp<Fence>& fence, GrContext* grContext) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext) {
|
||||
status_t VulkanManager::createReleaseFence(int* nativeFence, GrContext* grContext) {
|
||||
*nativeFence = -1;
|
||||
if (!hasVkContext()) {
|
||||
ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
|
||||
return INVALID_OPERATION;
|
||||
@@ -644,14 +643,13 @@ status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* gr
|
||||
GrBackendSemaphore backendSemaphore;
|
||||
backendSemaphore.initVulkan(semaphore);
|
||||
|
||||
DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
|
||||
semaphore);
|
||||
DestroySemaphoreInfo* destroyInfo =
|
||||
new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
|
||||
// Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
|
||||
// which will remove its ref to the semaphore. The VulkanManager must still release its ref,
|
||||
// when it is done with the semaphore.
|
||||
GrSemaphoresSubmitted submitted =
|
||||
grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore,
|
||||
destroy_semaphore, destroyInfo);
|
||||
GrSemaphoresSubmitted submitted = grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore,
|
||||
destroy_semaphore, destroyInfo);
|
||||
|
||||
if (submitted == GrSemaphoresSubmitted::kNo) {
|
||||
ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
|
||||
@@ -673,7 +671,7 @@ status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* gr
|
||||
ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
nativeFence = new Fence(fenceFd);
|
||||
*nativeFence = fenceFd;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -20,14 +20,13 @@
|
||||
#if !defined(VK_USE_PLATFORM_ANDROID_KHR)
|
||||
#define VK_USE_PLATFORM_ANDROID_KHR
|
||||
#endif
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include <GrContextOptions.h>
|
||||
#include <SkSurface.h>
|
||||
#include <ui/Fence.h>
|
||||
#include <utils/StrongPointer.h>
|
||||
#include <vk/GrVkBackendContext.h>
|
||||
#include <vk/GrVkExtensions.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include "Frame.h"
|
||||
#include "IRenderPipeline.h"
|
||||
#include "VulkanSurface.h"
|
||||
@@ -71,11 +70,11 @@ public:
|
||||
void destroy();
|
||||
|
||||
// Inserts a wait on fence command into the Vulkan command buffer.
|
||||
status_t fenceWait(sp<Fence>& fence, GrContext* grContext);
|
||||
status_t fenceWait(int fence, GrContext* grContext);
|
||||
|
||||
// Creates a fence that is signaled when all the pending Vulkan commands are finished on the
|
||||
// GPU.
|
||||
status_t createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext);
|
||||
status_t createReleaseFence(int* nativeFence, GrContext* grContext);
|
||||
|
||||
// Returned pointers are owned by VulkanManager.
|
||||
// An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to
|
||||
|
||||
@@ -1,675 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <EGL/eglext.h>
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <gui/BufferItem.h>
|
||||
#include <gui/BufferQueue.h>
|
||||
#include <private/gui/SyncFeatures.h>
|
||||
#include "EGLConsumer.h"
|
||||
#include "SurfaceTexture.h"
|
||||
|
||||
#include <utils/Log.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/Trace.h>
|
||||
|
||||
#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
|
||||
#define EGL_PROTECTED_CONTENT_EXT 0x32C0
|
||||
|
||||
namespace android {
|
||||
|
||||
// Macros for including the SurfaceTexture name in log messages
|
||||
#define EGC_LOGV(x, ...) ALOGV("[%s] " x, st.mName.string(), ##__VA_ARGS__)
|
||||
#define EGC_LOGD(x, ...) ALOGD("[%s] " x, st.mName.string(), ##__VA_ARGS__)
|
||||
#define EGC_LOGW(x, ...) ALOGW("[%s] " x, st.mName.string(), ##__VA_ARGS__)
|
||||
#define EGC_LOGE(x, ...) ALOGE("[%s] " x, st.mName.string(), ##__VA_ARGS__)
|
||||
|
||||
static const struct {
|
||||
uint32_t width, height;
|
||||
char const* bits;
|
||||
} kDebugData = {15, 12,
|
||||
"_______________"
|
||||
"_______________"
|
||||
"_____XX_XX_____"
|
||||
"__X_X_____X_X__"
|
||||
"__X_XXXXXXX_X__"
|
||||
"__XXXXXXXXXXX__"
|
||||
"___XX_XXX_XX___"
|
||||
"____XXXXXXX____"
|
||||
"_____X___X_____"
|
||||
"____X_____X____"
|
||||
"_______________"
|
||||
"_______________"};
|
||||
|
||||
Mutex EGLConsumer::sStaticInitLock;
|
||||
sp<GraphicBuffer> EGLConsumer::sReleasedTexImageBuffer;
|
||||
|
||||
static bool hasEglProtectedContentImpl() {
|
||||
EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
|
||||
size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
|
||||
size_t extsLen = strlen(exts);
|
||||
bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
|
||||
bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen + 1);
|
||||
bool atEnd = (cropExtLen + 1) < extsLen &&
|
||||
!strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen + 1));
|
||||
bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
|
||||
return equal || atStart || atEnd || inMiddle;
|
||||
}
|
||||
|
||||
static bool hasEglProtectedContent() {
|
||||
// Only compute whether the extension is present once the first time this
|
||||
// function is called.
|
||||
static bool hasIt = hasEglProtectedContentImpl();
|
||||
return hasIt;
|
||||
}
|
||||
|
||||
EGLConsumer::EGLConsumer() : mEglDisplay(EGL_NO_DISPLAY), mEglContext(EGL_NO_CONTEXT) {}
|
||||
|
||||
status_t EGLConsumer::updateTexImage(SurfaceTexture& st) {
|
||||
// Make sure the EGL state is the same as in previous calls.
|
||||
status_t err = checkAndUpdateEglStateLocked(st);
|
||||
if (err != NO_ERROR) {
|
||||
return err;
|
||||
}
|
||||
|
||||
BufferItem item;
|
||||
|
||||
// Acquire the next buffer.
|
||||
// In asynchronous mode the list is guaranteed to be one buffer
|
||||
// deep, while in synchronous mode we use the oldest buffer.
|
||||
err = st.acquireBufferLocked(&item, 0);
|
||||
if (err != NO_ERROR) {
|
||||
if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
|
||||
// We always bind the texture even if we don't update its contents.
|
||||
EGC_LOGV("updateTexImage: no buffers were available");
|
||||
glBindTexture(st.mTexTarget, st.mTexName);
|
||||
err = NO_ERROR;
|
||||
} else {
|
||||
EGC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
// Release the previous buffer.
|
||||
err = updateAndReleaseLocked(item, nullptr, st);
|
||||
if (err != NO_ERROR) {
|
||||
// We always bind the texture.
|
||||
glBindTexture(st.mTexTarget, st.mTexName);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Bind the new buffer to the GL texture, and wait until it's ready.
|
||||
return bindTextureImageLocked(st);
|
||||
}
|
||||
|
||||
status_t EGLConsumer::releaseTexImage(SurfaceTexture& st) {
|
||||
// Make sure the EGL state is the same as in previous calls.
|
||||
status_t err = NO_ERROR;
|
||||
|
||||
// if we're detached, no need to validate EGL's state -- we won't use it.
|
||||
if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
|
||||
err = checkAndUpdateEglStateLocked(st, true);
|
||||
if (err != NO_ERROR) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the EGLConsumer state.
|
||||
int buf = st.mCurrentTexture;
|
||||
if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
EGC_LOGV("releaseTexImage: (slot=%d, mOpMode=%d)", buf, (int)st.mOpMode);
|
||||
|
||||
// if we're detached, we just use the fence that was created in detachFromContext()
|
||||
// so... basically, nothing more to do here.
|
||||
if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
|
||||
// Do whatever sync ops we need to do before releasing the slot.
|
||||
err = syncForReleaseLocked(mEglDisplay, st);
|
||||
if (err != NO_ERROR) {
|
||||
EGC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
err = st.releaseBufferLocked(buf, st.mSlots[buf].mGraphicBuffer, mEglDisplay,
|
||||
EGL_NO_SYNC_KHR);
|
||||
if (err < NO_ERROR) {
|
||||
EGC_LOGE("releaseTexImage: failed to release buffer: %s (%d)", strerror(-err), err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (mReleasedTexImage == nullptr) {
|
||||
mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
|
||||
}
|
||||
|
||||
st.mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
|
||||
mCurrentTextureImage = mReleasedTexImage;
|
||||
st.mCurrentCrop.makeInvalid();
|
||||
st.mCurrentTransform = 0;
|
||||
st.mCurrentTimestamp = 0;
|
||||
st.mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
|
||||
st.mCurrentFence = Fence::NO_FENCE;
|
||||
st.mCurrentFenceTime = FenceTime::NO_FENCE;
|
||||
|
||||
// detached, don't touch the texture (and we may not even have an
|
||||
// EGLDisplay here.
|
||||
if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
|
||||
// This binds a dummy buffer (mReleasedTexImage).
|
||||
status_t result = bindTextureImageLocked(st);
|
||||
if (result != NO_ERROR) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
sp<GraphicBuffer> EGLConsumer::getDebugTexImageBuffer() {
|
||||
Mutex::Autolock _l(sStaticInitLock);
|
||||
if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) {
|
||||
// The first time, create the debug texture in case the application
|
||||
// continues to use it.
|
||||
sp<GraphicBuffer> buffer = new GraphicBuffer(
|
||||
kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
|
||||
GraphicBuffer::USAGE_SW_WRITE_RARELY, "[EGLConsumer debug texture]");
|
||||
uint32_t* bits;
|
||||
buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
|
||||
uint32_t stride = buffer->getStride();
|
||||
uint32_t height = buffer->getHeight();
|
||||
memset(bits, 0, stride * height * 4);
|
||||
for (uint32_t y = 0; y < kDebugData.height; y++) {
|
||||
for (uint32_t x = 0; x < kDebugData.width; x++) {
|
||||
bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ? 0xFF000000
|
||||
: 0xFFFFFFFF;
|
||||
}
|
||||
bits += stride;
|
||||
}
|
||||
buffer->unlock();
|
||||
sReleasedTexImageBuffer = buffer;
|
||||
}
|
||||
return sReleasedTexImageBuffer;
|
||||
}
|
||||
|
||||
void EGLConsumer::onAcquireBufferLocked(BufferItem* item, SurfaceTexture& st) {
|
||||
// If item->mGraphicBuffer is not null, this buffer has not been acquired
|
||||
// before, so any prior EglImage created is using a stale buffer. This
|
||||
// replaces any old EglImage with a new one (using the new buffer).
|
||||
int slot = item->mSlot;
|
||||
if (item->mGraphicBuffer != nullptr || mEglSlots[slot].mEglImage.get() == nullptr) {
|
||||
mEglSlots[slot].mEglImage = new EglImage(st.mSlots[slot].mGraphicBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
void EGLConsumer::onReleaseBufferLocked(int buf) {
|
||||
mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::updateAndReleaseLocked(const BufferItem& item, PendingRelease* pendingRelease,
|
||||
SurfaceTexture& st) {
|
||||
status_t err = NO_ERROR;
|
||||
|
||||
int slot = item.mSlot;
|
||||
|
||||
if (st.mOpMode != SurfaceTexture::OpMode::attachedToGL) {
|
||||
EGC_LOGE(
|
||||
"updateAndRelease: EGLConsumer is not attached to an OpenGL "
|
||||
"ES context");
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
// Confirm state.
|
||||
err = checkAndUpdateEglStateLocked(st);
|
||||
if (err != NO_ERROR) {
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Ensure we have a valid EglImageKHR for the slot, creating an EglImage
|
||||
// if nessessary, for the gralloc buffer currently in the slot in
|
||||
// ConsumerBase.
|
||||
// We may have to do this even when item.mGraphicBuffer == NULL (which
|
||||
// means the buffer was previously acquired).
|
||||
err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay);
|
||||
if (err != NO_ERROR) {
|
||||
EGC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay,
|
||||
slot);
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
// Do whatever sync ops we need to do before releasing the old slot.
|
||||
if (slot != st.mCurrentTexture) {
|
||||
err = syncForReleaseLocked(mEglDisplay, st);
|
||||
if (err != NO_ERROR) {
|
||||
// Release the buffer we just acquired. It's not safe to
|
||||
// release the old buffer, so instead we just drop the new frame.
|
||||
// As we are still under lock since acquireBuffer, it is safe to
|
||||
// release by slot.
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay,
|
||||
EGL_NO_SYNC_KHR);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
EGC_LOGV(
|
||||
"updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", st.mCurrentTexture,
|
||||
mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : nullptr,
|
||||
slot, st.mSlots[slot].mGraphicBuffer->handle);
|
||||
|
||||
// Hang onto the pointer so that it isn't freed in the call to
|
||||
// releaseBufferLocked() if we're in shared buffer mode and both buffers are
|
||||
// the same.
|
||||
sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
|
||||
|
||||
// release old buffer
|
||||
if (st.mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
if (pendingRelease == nullptr) {
|
||||
status_t status = st.releaseBufferLocked(
|
||||
st.mCurrentTexture, mCurrentTextureImage->graphicBuffer(), mEglDisplay,
|
||||
mEglSlots[st.mCurrentTexture].mEglFence);
|
||||
if (status < NO_ERROR) {
|
||||
EGC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
|
||||
status);
|
||||
err = status;
|
||||
// keep going, with error raised [?]
|
||||
}
|
||||
} else {
|
||||
pendingRelease->currentTexture = st.mCurrentTexture;
|
||||
pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
|
||||
pendingRelease->display = mEglDisplay;
|
||||
pendingRelease->fence = mEglSlots[st.mCurrentTexture].mEglFence;
|
||||
pendingRelease->isPending = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the EGLConsumer state.
|
||||
st.mCurrentTexture = slot;
|
||||
mCurrentTextureImage = nextTextureImage;
|
||||
st.mCurrentCrop = item.mCrop;
|
||||
st.mCurrentTransform = item.mTransform;
|
||||
st.mCurrentScalingMode = item.mScalingMode;
|
||||
st.mCurrentTimestamp = item.mTimestamp;
|
||||
st.mCurrentDataSpace = item.mDataSpace;
|
||||
st.mCurrentFence = item.mFence;
|
||||
st.mCurrentFenceTime = item.mFenceTime;
|
||||
st.mCurrentFrameNumber = item.mFrameNumber;
|
||||
|
||||
st.computeCurrentTransformMatrixLocked();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::bindTextureImageLocked(SurfaceTexture& st) {
|
||||
if (mEglDisplay == EGL_NO_DISPLAY) {
|
||||
ALOGE("bindTextureImage: invalid display");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
GLenum error;
|
||||
while ((error = glGetError()) != GL_NO_ERROR) {
|
||||
EGC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
|
||||
}
|
||||
|
||||
glBindTexture(st.mTexTarget, st.mTexName);
|
||||
if (st.mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) {
|
||||
EGC_LOGE("bindTextureImage: no currently-bound texture");
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay);
|
||||
if (err != NO_ERROR) {
|
||||
EGC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
|
||||
st.mCurrentTexture);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
mCurrentTextureImage->bindToTextureTarget(st.mTexTarget);
|
||||
|
||||
// In the rare case that the display is terminated and then initialized
|
||||
// again, we can't detect that the display changed (it didn't), but the
|
||||
// image is invalid. In this case, repeat the exact same steps while
|
||||
// forcing the creation of a new image.
|
||||
if ((error = glGetError()) != GL_NO_ERROR) {
|
||||
glBindTexture(st.mTexTarget, st.mTexName);
|
||||
status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true);
|
||||
if (result != NO_ERROR) {
|
||||
EGC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
|
||||
st.mCurrentTexture);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
mCurrentTextureImage->bindToTextureTarget(st.mTexTarget);
|
||||
if ((error = glGetError()) != GL_NO_ERROR) {
|
||||
EGC_LOGE("bindTextureImage: error binding external image: %#04x", error);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the new buffer to be ready.
|
||||
return doGLFenceWaitLocked(st);
|
||||
}
|
||||
|
||||
status_t EGLConsumer::checkAndUpdateEglStateLocked(SurfaceTexture& st, bool contextCheck) {
|
||||
EGLDisplay dpy = eglGetCurrentDisplay();
|
||||
EGLContext ctx = eglGetCurrentContext();
|
||||
|
||||
if (!contextCheck) {
|
||||
// if this is the first time we're called, mEglDisplay/mEglContext have
|
||||
// never been set, so don't error out (below).
|
||||
if (mEglDisplay == EGL_NO_DISPLAY) {
|
||||
mEglDisplay = dpy;
|
||||
}
|
||||
if (mEglContext == EGL_NO_CONTEXT) {
|
||||
mEglContext = ctx;
|
||||
}
|
||||
}
|
||||
|
||||
if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
|
||||
EGC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
|
||||
EGC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
mEglDisplay = dpy;
|
||||
mEglContext = ctx;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::detachFromContext(SurfaceTexture& st) {
|
||||
EGLDisplay dpy = eglGetCurrentDisplay();
|
||||
EGLContext ctx = eglGetCurrentContext();
|
||||
|
||||
if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
|
||||
EGC_LOGE("detachFromContext: invalid current EGLDisplay");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
|
||||
EGC_LOGE("detachFromContext: invalid current EGLContext");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
|
||||
status_t err = syncForReleaseLocked(dpy, st);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
glDeleteTextures(1, &st.mTexName);
|
||||
}
|
||||
|
||||
mEglDisplay = EGL_NO_DISPLAY;
|
||||
mEglContext = EGL_NO_CONTEXT;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::attachToContext(uint32_t tex, SurfaceTexture& st) {
|
||||
// Initialize mCurrentTextureImage if there is a current buffer from past attached state.
|
||||
int slot = st.mCurrentTexture;
|
||||
if (slot != BufferItem::INVALID_BUFFER_SLOT) {
|
||||
if (!mEglSlots[slot].mEglImage.get()) {
|
||||
mEglSlots[slot].mEglImage = new EglImage(st.mSlots[slot].mGraphicBuffer);
|
||||
}
|
||||
mCurrentTextureImage = mEglSlots[slot].mEglImage;
|
||||
}
|
||||
|
||||
EGLDisplay dpy = eglGetCurrentDisplay();
|
||||
EGLContext ctx = eglGetCurrentContext();
|
||||
|
||||
if (dpy == EGL_NO_DISPLAY) {
|
||||
EGC_LOGE("attachToContext: invalid current EGLDisplay");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (ctx == EGL_NO_CONTEXT) {
|
||||
EGC_LOGE("attachToContext: invalid current EGLContext");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
// We need to bind the texture regardless of whether there's a current
|
||||
// buffer.
|
||||
glBindTexture(st.mTexTarget, GLuint(tex));
|
||||
|
||||
mEglDisplay = dpy;
|
||||
mEglContext = ctx;
|
||||
st.mTexName = tex;
|
||||
st.mOpMode = SurfaceTexture::OpMode::attachedToGL;
|
||||
|
||||
if (mCurrentTextureImage != nullptr) {
|
||||
// This may wait for a buffer a second time. This is likely required if
|
||||
// this is a different context, since otherwise the wait could be skipped
|
||||
// by bouncing through another context. For the same context the extra
|
||||
// wait is redundant.
|
||||
status_t err = bindTextureImageLocked(st);
|
||||
if (err != NO_ERROR) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::syncForReleaseLocked(EGLDisplay dpy, SurfaceTexture& st) {
|
||||
EGC_LOGV("syncForReleaseLocked");
|
||||
|
||||
if (st.mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
if (SyncFeatures::getInstance().useNativeFenceSync()) {
|
||||
EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
|
||||
if (sync == EGL_NO_SYNC_KHR) {
|
||||
EGC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
glFlush();
|
||||
int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
|
||||
eglDestroySyncKHR(dpy, sync);
|
||||
if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
|
||||
EGC_LOGE(
|
||||
"syncForReleaseLocked: error dup'ing native fence "
|
||||
"fd: %#x",
|
||||
eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
sp<Fence> fence(new Fence(fenceFd));
|
||||
status_t err = st.addReleaseFenceLocked(st.mCurrentTexture,
|
||||
mCurrentTextureImage->graphicBuffer(), fence);
|
||||
if (err != OK) {
|
||||
EGC_LOGE(
|
||||
"syncForReleaseLocked: error adding release fence: "
|
||||
"%s (%d)",
|
||||
strerror(-err), err);
|
||||
return err;
|
||||
}
|
||||
} else if (st.mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
|
||||
EGLSyncKHR fence = mEglSlots[st.mCurrentTexture].mEglFence;
|
||||
if (fence != EGL_NO_SYNC_KHR) {
|
||||
// There is already a fence for the current slot. We need to
|
||||
// wait on that before replacing it with another fence to
|
||||
// ensure that all outstanding buffer accesses have completed
|
||||
// before the producer accesses it.
|
||||
EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
|
||||
if (result == EGL_FALSE) {
|
||||
EGC_LOGE(
|
||||
"syncForReleaseLocked: error waiting for previous "
|
||||
"fence: %#x",
|
||||
eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
} else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
|
||||
EGC_LOGE(
|
||||
"syncForReleaseLocked: timeout waiting for previous "
|
||||
"fence");
|
||||
return TIMED_OUT;
|
||||
}
|
||||
eglDestroySyncKHR(dpy, fence);
|
||||
}
|
||||
|
||||
// Create a fence for the outstanding accesses in the current
|
||||
// OpenGL ES context.
|
||||
fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
|
||||
if (fence == EGL_NO_SYNC_KHR) {
|
||||
EGC_LOGE("syncForReleaseLocked: error creating fence: %#x", eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
glFlush();
|
||||
mEglSlots[st.mCurrentTexture].mEglFence = fence;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
status_t EGLConsumer::doGLFenceWaitLocked(SurfaceTexture& st) const {
|
||||
EGLDisplay dpy = eglGetCurrentDisplay();
|
||||
EGLContext ctx = eglGetCurrentContext();
|
||||
|
||||
if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
|
||||
EGC_LOGE("doGLFenceWait: invalid current EGLDisplay");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
|
||||
EGC_LOGE("doGLFenceWait: invalid current EGLContext");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (st.mCurrentFence->isValid()) {
|
||||
if (SyncFeatures::getInstance().useWaitSync() &&
|
||||
SyncFeatures::getInstance().useNativeFenceSync()) {
|
||||
// Create an EGLSyncKHR from the current fence.
|
||||
int fenceFd = st.mCurrentFence->dup();
|
||||
if (fenceFd == -1) {
|
||||
EGC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
|
||||
return -errno;
|
||||
}
|
||||
EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
|
||||
EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
|
||||
if (sync == EGL_NO_SYNC_KHR) {
|
||||
close(fenceFd);
|
||||
EGC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
// XXX: The spec draft is inconsistent as to whether this should
|
||||
// return an EGLint or void. Ignore the return value for now, as
|
||||
// it's not strictly needed.
|
||||
eglWaitSyncKHR(dpy, sync, 0);
|
||||
EGLint eglErr = eglGetError();
|
||||
eglDestroySyncKHR(dpy, sync);
|
||||
if (eglErr != EGL_SUCCESS) {
|
||||
EGC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
} else {
|
||||
status_t err = st.mCurrentFence->waitForever("EGLConsumer::doGLFenceWaitLocked");
|
||||
if (err != NO_ERROR) {
|
||||
EGC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
void EGLConsumer::onFreeBufferLocked(int slotIndex) {
|
||||
mEglSlots[slotIndex].mEglImage.clear();
|
||||
}
|
||||
|
||||
void EGLConsumer::onAbandonLocked() {
|
||||
mCurrentTextureImage.clear();
|
||||
}
|
||||
|
||||
EGLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer)
|
||||
: mGraphicBuffer(graphicBuffer), mEglImage(EGL_NO_IMAGE_KHR), mEglDisplay(EGL_NO_DISPLAY) {}
|
||||
|
||||
EGLConsumer::EglImage::~EglImage() {
|
||||
if (mEglImage != EGL_NO_IMAGE_KHR) {
|
||||
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
|
||||
ALOGE("~EglImage: eglDestroyImageKHR failed");
|
||||
}
|
||||
eglTerminate(mEglDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
status_t EGLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, bool forceCreation) {
|
||||
// If there's an image and it's no longer valid, destroy it.
|
||||
bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
|
||||
bool displayInvalid = mEglDisplay != eglDisplay;
|
||||
if (haveImage && (displayInvalid || forceCreation)) {
|
||||
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
|
||||
ALOGE("createIfNeeded: eglDestroyImageKHR failed");
|
||||
}
|
||||
eglTerminate(mEglDisplay);
|
||||
mEglImage = EGL_NO_IMAGE_KHR;
|
||||
mEglDisplay = EGL_NO_DISPLAY;
|
||||
}
|
||||
|
||||
// If there's no image, create one.
|
||||
if (mEglImage == EGL_NO_IMAGE_KHR) {
|
||||
mEglDisplay = eglDisplay;
|
||||
mEglImage = createImage(mEglDisplay, mGraphicBuffer);
|
||||
}
|
||||
|
||||
// Fail if we can't create a valid image.
|
||||
if (mEglImage == EGL_NO_IMAGE_KHR) {
|
||||
mEglDisplay = EGL_NO_DISPLAY;
|
||||
const sp<GraphicBuffer>& buffer = mGraphicBuffer;
|
||||
ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
|
||||
buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
|
||||
buffer->getPixelFormat());
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void EGLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
|
||||
glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage));
|
||||
}
|
||||
|
||||
EGLImageKHR EGLConsumer::EglImage::createImage(EGLDisplay dpy,
|
||||
const sp<GraphicBuffer>& graphicBuffer) {
|
||||
EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
|
||||
const bool createProtectedImage =
|
||||
(graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent();
|
||||
EGLint attrs[] = {
|
||||
EGL_IMAGE_PRESERVED_KHR,
|
||||
EGL_TRUE,
|
||||
createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
|
||||
createProtectedImage ? EGL_TRUE : EGL_NONE,
|
||||
EGL_NONE,
|
||||
};
|
||||
eglInitialize(dpy, nullptr, nullptr);
|
||||
EGLImageKHR image =
|
||||
eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
|
||||
if (image == EGL_NO_IMAGE_KHR) {
|
||||
EGLint error = eglGetError();
|
||||
ALOGE("error creating EGLImage: %#x", error);
|
||||
eglTerminate(dpy);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
@@ -1,311 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <EGL/eglext.h>
|
||||
|
||||
#include <gui/BufferQueueDefs.h>
|
||||
|
||||
#include <ui/FenceTime.h>
|
||||
#include <ui/GraphicBuffer.h>
|
||||
#include <utils/Mutex.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
class SurfaceTexture;
|
||||
|
||||
/*
|
||||
* EGLConsumer implements the parts of SurfaceTexture that deal with
|
||||
* textures attached to an GL context.
|
||||
*/
|
||||
class EGLConsumer {
|
||||
public:
|
||||
EGLConsumer();
|
||||
|
||||
/**
|
||||
* updateTexImage acquires the most recently queued buffer, and sets the
|
||||
* image contents of the target texture to it.
|
||||
*
|
||||
* This call may only be made while the OpenGL ES context to which the
|
||||
* target texture belongs is bound to the calling thread.
|
||||
*
|
||||
* This calls doGLFenceWait to ensure proper synchronization.
|
||||
*/
|
||||
status_t updateTexImage(SurfaceTexture& st);
|
||||
|
||||
/*
|
||||
* releaseTexImage releases the texture acquired in updateTexImage().
|
||||
* This is intended to be used in single buffer mode.
|
||||
*
|
||||
* This call may only be made while the OpenGL ES context to which the
|
||||
* target texture belongs is bound to the calling thread.
|
||||
*/
|
||||
status_t releaseTexImage(SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* detachFromContext detaches the EGLConsumer from the calling thread's
|
||||
* current OpenGL ES context. This context must be the same as the context
|
||||
* that was current for previous calls to updateTexImage.
|
||||
*
|
||||
* Detaching a EGLConsumer from an OpenGL ES context will result in the
|
||||
* deletion of the OpenGL ES texture object into which the images were being
|
||||
* streamed. After a EGLConsumer has been detached from the OpenGL ES
|
||||
* context calls to updateTexImage will fail returning INVALID_OPERATION
|
||||
* until the EGLConsumer is attached to a new OpenGL ES context using the
|
||||
* attachToContext method.
|
||||
*/
|
||||
status_t detachFromContext(SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* attachToContext attaches a EGLConsumer that is currently in the
|
||||
* 'detached' state to the current OpenGL ES context. A EGLConsumer is
|
||||
* in the 'detached' state iff detachFromContext has successfully been
|
||||
* called and no calls to attachToContext have succeeded since the last
|
||||
* detachFromContext call. Calls to attachToContext made on a
|
||||
* EGLConsumer that is not in the 'detached' state will result in an
|
||||
* INVALID_OPERATION error.
|
||||
*
|
||||
* The tex argument specifies the OpenGL ES texture object name in the
|
||||
* new context into which the image contents will be streamed. A successful
|
||||
* call to attachToContext will result in this texture object being bound to
|
||||
* the texture target and populated with the image contents that were
|
||||
* current at the time of the last call to detachFromContext.
|
||||
*/
|
||||
status_t attachToContext(uint32_t tex, SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* onAcquireBufferLocked amends the ConsumerBase method to update the
|
||||
* mEglSlots array in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
void onAcquireBufferLocked(BufferItem* item, SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* onReleaseBufferLocked amends the ConsumerBase method to update the
|
||||
* mEglSlots array in addition to the ConsumerBase.
|
||||
*/
|
||||
void onReleaseBufferLocked(int slot);
|
||||
|
||||
/**
|
||||
* onFreeBufferLocked frees up the given buffer slot. If the slot has been
|
||||
* initialized this will release the reference to the GraphicBuffer in that
|
||||
* slot and destroy the EGLImage in that slot. Otherwise it has no effect.
|
||||
*/
|
||||
void onFreeBufferLocked(int slotIndex);
|
||||
|
||||
/**
|
||||
* onAbandonLocked amends the ConsumerBase method to clear
|
||||
* mCurrentTextureImage in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
void onAbandonLocked();
|
||||
|
||||
protected:
|
||||
struct PendingRelease {
|
||||
PendingRelease()
|
||||
: isPending(false)
|
||||
, currentTexture(-1)
|
||||
, graphicBuffer()
|
||||
, display(nullptr)
|
||||
, fence(nullptr) {}
|
||||
|
||||
bool isPending;
|
||||
int currentTexture;
|
||||
sp<GraphicBuffer> graphicBuffer;
|
||||
EGLDisplay display;
|
||||
EGLSyncKHR fence;
|
||||
};
|
||||
|
||||
/**
|
||||
* This releases the buffer in the slot referenced by mCurrentTexture,
|
||||
* then updates state to refer to the BufferItem, which must be a
|
||||
* newly-acquired buffer. If pendingRelease is not null, the parameters
|
||||
* which would have been passed to releaseBufferLocked upon the successful
|
||||
* completion of the method will instead be returned to the caller, so that
|
||||
* it may call releaseBufferLocked itself later.
|
||||
*/
|
||||
status_t updateAndReleaseLocked(const BufferItem& item, PendingRelease* pendingRelease,
|
||||
SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* Binds mTexName and the current buffer to mTexTarget. Uses
|
||||
* mCurrentTexture if it's set, mCurrentTextureImage if not. If the
|
||||
* bind succeeds, this calls doGLFenceWait.
|
||||
*/
|
||||
status_t bindTextureImageLocked(SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* Gets the current EGLDisplay and EGLContext values, and compares them
|
||||
* to mEglDisplay and mEglContext. If the fields have been previously
|
||||
* set, the values must match; if not, the fields are set to the current
|
||||
* values.
|
||||
* The contextCheck argument is used to ensure that a GL context is
|
||||
* properly set; when set to false, the check is not performed.
|
||||
*/
|
||||
status_t checkAndUpdateEglStateLocked(SurfaceTexture& st, bool contextCheck = false);
|
||||
|
||||
/**
|
||||
* EglImage is a utility class for tracking and creating EGLImageKHRs. There
|
||||
* is primarily just one image per slot, but there is also special cases:
|
||||
* - For releaseTexImage, we use a debug image (mReleasedTexImage)
|
||||
* - After freeBuffer, we must still keep the current image/buffer
|
||||
* Reference counting EGLImages lets us handle all these cases easily while
|
||||
* also only creating new EGLImages from buffers when required.
|
||||
*/
|
||||
class EglImage : public LightRefBase<EglImage> {
|
||||
public:
|
||||
EglImage(sp<GraphicBuffer> graphicBuffer);
|
||||
|
||||
/**
|
||||
* createIfNeeded creates an EGLImage if required (we haven't created
|
||||
* one yet, or the EGLDisplay or crop-rect has changed).
|
||||
*/
|
||||
status_t createIfNeeded(EGLDisplay display, bool forceCreate = false);
|
||||
|
||||
/**
|
||||
* This calls glEGLImageTargetTexture2DOES to bind the image to the
|
||||
* texture in the specified texture target.
|
||||
*/
|
||||
void bindToTextureTarget(uint32_t texTarget);
|
||||
|
||||
const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
|
||||
const native_handle* graphicBufferHandle() {
|
||||
return mGraphicBuffer == nullptr ? nullptr : mGraphicBuffer->handle;
|
||||
}
|
||||
|
||||
private:
|
||||
// Only allow instantiation using ref counting.
|
||||
friend class LightRefBase<EglImage>;
|
||||
virtual ~EglImage();
|
||||
|
||||
// createImage creates a new EGLImage from a GraphicBuffer.
|
||||
EGLImageKHR createImage(EGLDisplay dpy, const sp<GraphicBuffer>& graphicBuffer);
|
||||
|
||||
// Disallow copying
|
||||
EglImage(const EglImage& rhs);
|
||||
void operator=(const EglImage& rhs);
|
||||
|
||||
// mGraphicBuffer is the buffer that was used to create this image.
|
||||
sp<GraphicBuffer> mGraphicBuffer;
|
||||
|
||||
// mEglImage is the EGLImage created from mGraphicBuffer.
|
||||
EGLImageKHR mEglImage;
|
||||
|
||||
// mEGLDisplay is the EGLDisplay that was used to create mEglImage.
|
||||
EGLDisplay mEglDisplay;
|
||||
|
||||
// mCropRect is the crop rectangle passed to EGL when mEglImage
|
||||
// was created.
|
||||
Rect mCropRect;
|
||||
};
|
||||
|
||||
/**
|
||||
* doGLFenceWaitLocked inserts a wait command into the OpenGL ES command
|
||||
* stream to ensure that it is safe for future OpenGL ES commands to
|
||||
* access the current texture buffer.
|
||||
*/
|
||||
status_t doGLFenceWaitLocked(SurfaceTexture& st) const;
|
||||
|
||||
/**
|
||||
* syncForReleaseLocked performs the synchronization needed to release the
|
||||
* current slot from an OpenGL ES context. If needed it will set the
|
||||
* current slot's fence to guard against a producer accessing the buffer
|
||||
* before the outstanding accesses have completed.
|
||||
*/
|
||||
status_t syncForReleaseLocked(EGLDisplay dpy, SurfaceTexture& st);
|
||||
|
||||
/**
|
||||
* returns a graphic buffer used when the texture image has been released
|
||||
*/
|
||||
static sp<GraphicBuffer> getDebugTexImageBuffer();
|
||||
|
||||
/**
|
||||
* The default consumer usage flags that EGLConsumer always sets on its
|
||||
* BufferQueue instance; these will be OR:d with any additional flags passed
|
||||
* from the EGLConsumer user. In particular, EGLConsumer will always
|
||||
* consume buffers as hardware textures.
|
||||
*/
|
||||
static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
|
||||
|
||||
/**
|
||||
* mCurrentTextureImage is the EglImage/buffer of the current texture. It's
|
||||
* possible that this buffer is not associated with any buffer slot, so we
|
||||
* must track it separately in order to support the getCurrentBuffer method.
|
||||
*/
|
||||
sp<EglImage> mCurrentTextureImage;
|
||||
|
||||
/**
|
||||
* EGLSlot contains the information and object references that
|
||||
* EGLConsumer maintains about a BufferQueue buffer slot.
|
||||
*/
|
||||
struct EglSlot {
|
||||
EglSlot() : mEglFence(EGL_NO_SYNC_KHR) {}
|
||||
|
||||
/**
|
||||
* mEglImage is the EGLImage created from mGraphicBuffer.
|
||||
*/
|
||||
sp<EglImage> mEglImage;
|
||||
|
||||
/**
|
||||
* mFence is the EGL sync object that must signal before the buffer
|
||||
* associated with this buffer slot may be dequeued. It is initialized
|
||||
* to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
|
||||
* on a compile-time option) set to a new sync object in updateTexImage.
|
||||
*/
|
||||
EGLSyncKHR mEglFence;
|
||||
};
|
||||
|
||||
/**
|
||||
* mEglDisplay is the EGLDisplay with which this EGLConsumer is currently
|
||||
* associated. It is intialized to EGL_NO_DISPLAY and gets set to the
|
||||
* current display when updateTexImage is called for the first time and when
|
||||
* attachToContext is called.
|
||||
*/
|
||||
EGLDisplay mEglDisplay;
|
||||
|
||||
/**
|
||||
* mEglContext is the OpenGL ES context with which this EGLConsumer is
|
||||
* currently associated. It is initialized to EGL_NO_CONTEXT and gets set
|
||||
* to the current GL context when updateTexImage is called for the first
|
||||
* time and when attachToContext is called.
|
||||
*/
|
||||
EGLContext mEglContext;
|
||||
|
||||
/**
|
||||
* mEGLSlots stores the buffers that have been allocated by the BufferQueue
|
||||
* for each buffer slot. It is initialized to null pointers, and gets
|
||||
* filled in with the result of BufferQueue::acquire when the
|
||||
* client dequeues a buffer from a
|
||||
* slot that has not yet been used. The buffer allocated to a slot will also
|
||||
* be replaced if the requested buffer usage or geometry differs from that
|
||||
* of the buffer allocated to a slot.
|
||||
*/
|
||||
EglSlot mEglSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
|
||||
|
||||
/**
|
||||
* protects static initialization
|
||||
*/
|
||||
static Mutex sStaticInitLock;
|
||||
|
||||
/**
|
||||
* mReleasedTexImageBuffer is a dummy buffer used when in single buffer
|
||||
* mode and releaseTexImage() has been called
|
||||
*/
|
||||
static sp<GraphicBuffer> sReleasedTexImageBuffer;
|
||||
sp<EglImage> mReleasedTexImage;
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
@@ -1,299 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ImageConsumer.h"
|
||||
#include <gui/BufferQueue.h>
|
||||
#include "Properties.h"
|
||||
#include "SurfaceTexture.h"
|
||||
#include "renderstate/RenderState.h"
|
||||
#include "renderthread/EglManager.h"
|
||||
#include "renderthread/RenderThread.h"
|
||||
#include "renderthread/VulkanManager.h"
|
||||
#include "utils/Color.h"
|
||||
#include <GrAHardwareBufferUtils.h>
|
||||
#include <GrBackendSurface.h>
|
||||
|
||||
// Macro for including the SurfaceTexture name in log messages
|
||||
#define IMG_LOGE(x, ...) ALOGE("[%s] " x, st.mName.string(), ##__VA_ARGS__)
|
||||
|
||||
using namespace android::uirenderer::renderthread;
|
||||
|
||||
namespace android {
|
||||
|
||||
void ImageConsumer::onFreeBufferLocked(int slotIndex) {
|
||||
// This callback may be invoked on any thread.
|
||||
mImageSlots[slotIndex].clear();
|
||||
}
|
||||
|
||||
void ImageConsumer::onAcquireBufferLocked(BufferItem* item) {
|
||||
// If item->mGraphicBuffer is not null, this buffer has not been acquired
|
||||
// before, so any prior SkImage is created with a stale buffer. This resets the stale SkImage.
|
||||
if (item->mGraphicBuffer != nullptr) {
|
||||
mImageSlots[item->mSlot].clear();
|
||||
}
|
||||
}
|
||||
|
||||
void ImageConsumer::onReleaseBufferLocked(int buf) {
|
||||
mImageSlots[buf].eglFence() = EGL_NO_SYNC_KHR;
|
||||
}
|
||||
|
||||
/**
|
||||
* AutoBackendTextureRelease manages EglImage/VkImage lifetime. It is a ref-counted object
|
||||
* that keeps GPU resources alive until the last SKImage object using them is destroyed.
|
||||
*/
|
||||
class AutoBackendTextureRelease {
|
||||
public:
|
||||
static void releaseProc(SkImage::ReleaseContext releaseContext);
|
||||
|
||||
AutoBackendTextureRelease(GrContext* context, GraphicBuffer* buffer);
|
||||
|
||||
const GrBackendTexture& getTexture() const { return mBackendTexture; }
|
||||
|
||||
void ref() { mUsageCount++; }
|
||||
|
||||
void unref(bool releaseImage);
|
||||
|
||||
inline sk_sp<SkImage> getImage() { return mImage; }
|
||||
|
||||
void makeImage(sp<GraphicBuffer>& graphicBuffer, android_dataspace dataspace,
|
||||
GrContext* context);
|
||||
|
||||
void newBufferContent(GrContext* context);
|
||||
|
||||
private:
|
||||
// The only way to invoke dtor is with unref, when mUsageCount is 0.
|
||||
~AutoBackendTextureRelease() {}
|
||||
|
||||
GrBackendTexture mBackendTexture;
|
||||
GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
|
||||
GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
|
||||
GrAHardwareBufferUtils::TexImageCtx mImageCtx;
|
||||
|
||||
// Starting with refcount 1, because the first ref is held by SurfaceTexture. Additional refs
|
||||
// are held by SkImages.
|
||||
int mUsageCount = 1;
|
||||
|
||||
// mImage is the SkImage created from mBackendTexture.
|
||||
sk_sp<SkImage> mImage;
|
||||
};
|
||||
|
||||
AutoBackendTextureRelease::AutoBackendTextureRelease(GrContext* context, GraphicBuffer* buffer) {
|
||||
bool createProtectedImage =
|
||||
0 != (buffer->getUsage() & GraphicBuffer::USAGE_PROTECTED);
|
||||
GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(
|
||||
context,
|
||||
reinterpret_cast<AHardwareBuffer*>(buffer),
|
||||
buffer->getPixelFormat(),
|
||||
false);
|
||||
mBackendTexture = GrAHardwareBufferUtils::MakeBackendTexture(
|
||||
context,
|
||||
reinterpret_cast<AHardwareBuffer*>(buffer),
|
||||
buffer->getWidth(),
|
||||
buffer->getHeight(),
|
||||
&mDeleteProc,
|
||||
&mUpdateProc,
|
||||
&mImageCtx,
|
||||
createProtectedImage,
|
||||
backendFormat,
|
||||
false);
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::unref(bool releaseImage) {
|
||||
if (!RenderThread::isCurrent()) {
|
||||
// EGLImage needs to be destroyed on RenderThread to prevent memory leak.
|
||||
// ~SkImage dtor for both pipelines needs to be invoked on RenderThread, because it is not
|
||||
// thread safe.
|
||||
RenderThread::getInstance().queue().post([this, releaseImage]() { unref(releaseImage); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (releaseImage) {
|
||||
mImage.reset();
|
||||
}
|
||||
|
||||
mUsageCount--;
|
||||
if (mUsageCount <= 0) {
|
||||
if (mBackendTexture.isValid()) {
|
||||
mDeleteProc(mImageCtx);
|
||||
mBackendTexture = {};
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::releaseProc(SkImage::ReleaseContext releaseContext) {
|
||||
AutoBackendTextureRelease* textureRelease =
|
||||
reinterpret_cast<AutoBackendTextureRelease*>(releaseContext);
|
||||
textureRelease->unref(false);
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::makeImage(sp<GraphicBuffer>& graphicBuffer,
|
||||
android_dataspace dataspace, GrContext* context) {
|
||||
SkColorType colorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(
|
||||
graphicBuffer->getPixelFormat());
|
||||
mImage = SkImage::MakeFromTexture(context,
|
||||
mBackendTexture,
|
||||
kTopLeft_GrSurfaceOrigin,
|
||||
colorType,
|
||||
kPremul_SkAlphaType,
|
||||
uirenderer::DataSpaceToColorSpace(dataspace),
|
||||
releaseProc,
|
||||
this);
|
||||
if (mImage.get()) {
|
||||
// The following ref will be counteracted by releaseProc, when SkImage is discarded.
|
||||
ref();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBackendTextureRelease::newBufferContent(GrContext* context) {
|
||||
if (mBackendTexture.isValid()) {
|
||||
mUpdateProc(mImageCtx, context);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageConsumer::ImageSlot::createIfNeeded(sp<GraphicBuffer> graphicBuffer,
|
||||
android_dataspace dataspace, bool forceCreate,
|
||||
GrContext* context) {
|
||||
if (!mTextureRelease || !mTextureRelease->getImage().get() || dataspace != mDataspace
|
||||
|| forceCreate) {
|
||||
if (!graphicBuffer.get()) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mTextureRelease) {
|
||||
mTextureRelease = new AutoBackendTextureRelease(context, graphicBuffer.get());
|
||||
} else {
|
||||
mTextureRelease->newBufferContent(context);
|
||||
}
|
||||
|
||||
mDataspace = dataspace;
|
||||
mTextureRelease->makeImage(graphicBuffer, dataspace, context);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageConsumer::ImageSlot::clear() {
|
||||
if (mTextureRelease) {
|
||||
// The following unref counteracts the initial mUsageCount of 1, set by default initializer.
|
||||
mTextureRelease->unref(true);
|
||||
mTextureRelease = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
sk_sp<SkImage> ImageConsumer::ImageSlot::getImage() {
|
||||
return mTextureRelease ? mTextureRelease->getImage() : nullptr;
|
||||
}
|
||||
|
||||
sk_sp<SkImage> ImageConsumer::dequeueImage(bool* queueEmpty, SurfaceTexture& st,
|
||||
uirenderer::RenderState& renderState) {
|
||||
BufferItem item;
|
||||
status_t err;
|
||||
err = st.acquireBufferLocked(&item, 0);
|
||||
if (err != OK) {
|
||||
if (err != BufferQueue::NO_BUFFER_AVAILABLE) {
|
||||
IMG_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
|
||||
} else {
|
||||
int slot = st.mCurrentTexture;
|
||||
if (slot != BufferItem::INVALID_BUFFER_SLOT) {
|
||||
*queueEmpty = true;
|
||||
mImageSlots[slot].createIfNeeded(st.mSlots[slot].mGraphicBuffer,
|
||||
st.mCurrentDataSpace, false, renderState.getRenderThread().getGrContext());
|
||||
return mImageSlots[slot].getImage();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int slot = item.mSlot;
|
||||
if (item.mFence->isValid()) {
|
||||
// Wait on the producer fence for the buffer to be ready.
|
||||
if (uirenderer::Properties::getRenderPipelineType() ==
|
||||
uirenderer::RenderPipelineType::SkiaGL) {
|
||||
err = renderState.getRenderThread().eglManager().fenceWait(item.mFence);
|
||||
} else {
|
||||
err = renderState.getRenderThread().vulkanManager().fenceWait(
|
||||
item.mFence, renderState.getRenderThread().getGrContext());
|
||||
}
|
||||
if (err != OK) {
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
|
||||
EGL_NO_SYNC_KHR);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Release old buffer.
|
||||
if (st.mCurrentTexture != BufferItem::INVALID_BUFFER_SLOT) {
|
||||
// If needed, set the released slot's fence to guard against a producer accessing the
|
||||
// buffer before the outstanding accesses have completed.
|
||||
sp<Fence> releaseFence;
|
||||
EGLDisplay display = EGL_NO_DISPLAY;
|
||||
if (uirenderer::Properties::getRenderPipelineType() ==
|
||||
uirenderer::RenderPipelineType::SkiaGL) {
|
||||
auto& eglManager = renderState.getRenderThread().eglManager();
|
||||
display = eglManager.eglDisplay();
|
||||
err = eglManager.createReleaseFence(st.mUseFenceSync, &mImageSlots[slot].eglFence(),
|
||||
releaseFence);
|
||||
} else {
|
||||
err = renderState.getRenderThread().vulkanManager().createReleaseFence(
|
||||
releaseFence, renderState.getRenderThread().getGrContext());
|
||||
}
|
||||
if (OK != err) {
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
|
||||
EGL_NO_SYNC_KHR);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (releaseFence.get()) {
|
||||
status_t err = st.addReleaseFenceLocked(
|
||||
st.mCurrentTexture, st.mSlots[st.mCurrentTexture].mGraphicBuffer, releaseFence);
|
||||
if (err != OK) {
|
||||
IMG_LOGE("dequeueImage: error adding release fence: %s (%d)", strerror(-err), err);
|
||||
st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
|
||||
EGL_NO_SYNC_KHR);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally release the old buffer.
|
||||
status_t status = st.releaseBufferLocked(
|
||||
st.mCurrentTexture, st.mSlots[st.mCurrentTexture].mGraphicBuffer, display,
|
||||
mImageSlots[st.mCurrentTexture].eglFence());
|
||||
if (status < NO_ERROR) {
|
||||
IMG_LOGE("dequeueImage: failed to release buffer: %s (%d)", strerror(-status), status);
|
||||
err = status;
|
||||
// Keep going, with error raised.
|
||||
}
|
||||
}
|
||||
|
||||
// Update the state.
|
||||
st.mCurrentTexture = slot;
|
||||
st.mCurrentCrop = item.mCrop;
|
||||
st.mCurrentTransform = item.mTransform;
|
||||
st.mCurrentScalingMode = item.mScalingMode;
|
||||
st.mCurrentTimestamp = item.mTimestamp;
|
||||
st.mCurrentDataSpace = item.mDataSpace;
|
||||
st.mCurrentFence = item.mFence;
|
||||
st.mCurrentFenceTime = item.mFenceTime;
|
||||
st.mCurrentFrameNumber = item.mFrameNumber;
|
||||
st.computeCurrentTransformMatrixLocked();
|
||||
|
||||
*queueEmpty = false;
|
||||
mImageSlots[slot].createIfNeeded(st.mSlots[slot].mGraphicBuffer, item.mDataSpace, true,
|
||||
renderState.getRenderThread().getGrContext());
|
||||
return mImageSlots[slot].getImage();
|
||||
}
|
||||
|
||||
} /* namespace android */
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <EGL/eglext.h>
|
||||
|
||||
#include <gui/BufferQueueDefs.h>
|
||||
|
||||
#include <SkImage.h>
|
||||
#include <cutils/compiler.h>
|
||||
#include <gui/BufferItem.h>
|
||||
#include <system/graphics.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
namespace uirenderer {
|
||||
class RenderState;
|
||||
}
|
||||
|
||||
class AutoBackendTextureRelease;
|
||||
class SurfaceTexture;
|
||||
|
||||
/*
|
||||
* ImageConsumer implements the parts of SurfaceTexture that deal with
|
||||
* images consumed by HWUI view system.
|
||||
*/
|
||||
class ImageConsumer {
|
||||
public:
|
||||
sk_sp<SkImage> dequeueImage(bool* queueEmpty, SurfaceTexture& cb,
|
||||
uirenderer::RenderState& renderState);
|
||||
|
||||
/**
|
||||
* onAcquireBufferLocked amends the ConsumerBase method to update the
|
||||
* mImageSlots array in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
void onAcquireBufferLocked(BufferItem* item);
|
||||
|
||||
/**
|
||||
* onReleaseBufferLocked amends the ConsumerBase method to update the
|
||||
* mImageSlots array in addition to the ConsumerBase.
|
||||
*/
|
||||
void onReleaseBufferLocked(int slot);
|
||||
|
||||
/**
|
||||
* onFreeBufferLocked frees up the given buffer slot. If the slot has been
|
||||
* initialized this will release the reference to the GraphicBuffer in that
|
||||
* slot and destroy the SkImage in that slot. Otherwise it has no effect.
|
||||
*/
|
||||
void onFreeBufferLocked(int slotIndex);
|
||||
|
||||
private:
|
||||
/**
|
||||
* ImageSlot contains the information and object references that
|
||||
* ImageConsumer maintains about a BufferQueue buffer slot.
|
||||
*/
|
||||
class ImageSlot {
|
||||
public:
|
||||
ImageSlot() : mDataspace(HAL_DATASPACE_UNKNOWN), mEglFence(EGL_NO_SYNC_KHR) {}
|
||||
|
||||
~ImageSlot() { clear(); }
|
||||
|
||||
void createIfNeeded(sp<GraphicBuffer> graphicBuffer, android_dataspace dataspace,
|
||||
bool forceCreate, GrContext* context);
|
||||
|
||||
void clear();
|
||||
|
||||
inline EGLSyncKHR& eglFence() { return mEglFence; }
|
||||
|
||||
sk_sp<SkImage> getImage();
|
||||
|
||||
private:
|
||||
// the dataspace associated with the current image
|
||||
android_dataspace mDataspace;
|
||||
|
||||
/**
|
||||
* mEglFence is the EGL sync object that must signal before the buffer
|
||||
* associated with this buffer slot may be dequeued.
|
||||
*/
|
||||
EGLSyncKHR mEglFence;
|
||||
|
||||
/**
|
||||
* mTextureRelease may outlive ImageConsumer, if the last ref is held by an SkImage.
|
||||
* ImageConsumer holds one ref to mTextureRelease, which is decremented by "clear".
|
||||
*/
|
||||
AutoBackendTextureRelease* mTextureRelease = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* ImageConsumer stores the SkImages that have been allocated by the BufferQueue
|
||||
* for each buffer slot. It is initialized to null pointers, and gets
|
||||
* filled in with the result of BufferQueue::acquire when the
|
||||
* client dequeues a buffer from a
|
||||
* slot that has not yet been used. The buffer allocated to a slot will also
|
||||
* be replaced if the requested buffer usage or geometry differs from that
|
||||
* of the buffer allocated to a slot.
|
||||
*/
|
||||
ImageSlot mImageSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
|
||||
};
|
||||
|
||||
} /* namespace android */
|
||||
@@ -1,499 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cutils/compiler.h>
|
||||
#include <gui/BufferQueue.h>
|
||||
#include <math/mat4.h>
|
||||
#include <system/window.h>
|
||||
|
||||
#include <utils/Trace.h>
|
||||
|
||||
#include "Matrix.h"
|
||||
#include "SurfaceTexture.h"
|
||||
#include "ImageConsumer.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
// Macros for including the SurfaceTexture name in log messages
|
||||
#define SFT_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
|
||||
#define SFT_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
|
||||
#define SFT_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
|
||||
#define SFT_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
|
||||
|
||||
static const mat4 mtxIdentity;
|
||||
|
||||
SurfaceTexture::SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
|
||||
uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
|
||||
: ConsumerBase(bq, isControlledByApp)
|
||||
, mCurrentCrop(Rect::EMPTY_RECT)
|
||||
, mCurrentTransform(0)
|
||||
, mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE)
|
||||
, mCurrentFence(Fence::NO_FENCE)
|
||||
, mCurrentTimestamp(0)
|
||||
, mCurrentDataSpace(HAL_DATASPACE_UNKNOWN)
|
||||
, mCurrentFrameNumber(0)
|
||||
, mDefaultWidth(1)
|
||||
, mDefaultHeight(1)
|
||||
, mFilteringEnabled(true)
|
||||
, mTexName(tex)
|
||||
, mUseFenceSync(useFenceSync)
|
||||
, mTexTarget(texTarget)
|
||||
, mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
|
||||
, mOpMode(OpMode::attachedToGL) {
|
||||
SFT_LOGV("SurfaceTexture");
|
||||
|
||||
memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
|
||||
|
||||
mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
|
||||
}
|
||||
|
||||
SurfaceTexture::SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
|
||||
bool useFenceSync, bool isControlledByApp)
|
||||
: ConsumerBase(bq, isControlledByApp)
|
||||
, mCurrentCrop(Rect::EMPTY_RECT)
|
||||
, mCurrentTransform(0)
|
||||
, mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE)
|
||||
, mCurrentFence(Fence::NO_FENCE)
|
||||
, mCurrentTimestamp(0)
|
||||
, mCurrentDataSpace(HAL_DATASPACE_UNKNOWN)
|
||||
, mCurrentFrameNumber(0)
|
||||
, mDefaultWidth(1)
|
||||
, mDefaultHeight(1)
|
||||
, mFilteringEnabled(true)
|
||||
, mTexName(0)
|
||||
, mUseFenceSync(useFenceSync)
|
||||
, mTexTarget(texTarget)
|
||||
, mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
|
||||
, mOpMode(OpMode::detached) {
|
||||
SFT_LOGV("SurfaceTexture");
|
||||
|
||||
memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
|
||||
|
||||
mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("setDefaultBufferSize: SurfaceTexture is abandoned!");
|
||||
return NO_INIT;
|
||||
}
|
||||
mDefaultWidth = w;
|
||||
mDefaultHeight = h;
|
||||
return mConsumer->setDefaultBufferSize(w, h);
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::updateTexImage() {
|
||||
ATRACE_CALL();
|
||||
SFT_LOGV("updateTexImage");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("updateTexImage: SurfaceTexture is abandoned!");
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
return mEGLConsumer.updateTexImage(*this);
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::releaseTexImage() {
|
||||
// releaseTexImage can be invoked even when not attached to a GL context.
|
||||
ATRACE_CALL();
|
||||
SFT_LOGV("releaseTexImage");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("releaseTexImage: SurfaceTexture is abandoned!");
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
return mEGLConsumer.releaseTexImage(*this);
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
|
||||
uint64_t maxFrameNumber) {
|
||||
status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
|
||||
if (err != NO_ERROR) {
|
||||
return err;
|
||||
}
|
||||
|
||||
switch (mOpMode) {
|
||||
case OpMode::attachedToView:
|
||||
mImageConsumer.onAcquireBufferLocked(item);
|
||||
break;
|
||||
case OpMode::attachedToGL:
|
||||
mEGLConsumer.onAcquireBufferLocked(item, *this);
|
||||
break;
|
||||
case OpMode::detached:
|
||||
break;
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::releaseBufferLocked(int buf, sp<GraphicBuffer> graphicBuffer,
|
||||
EGLDisplay display, EGLSyncKHR eglFence) {
|
||||
// release the buffer if it hasn't already been discarded by the
|
||||
// BufferQueue. This can happen, for example, when the producer of this
|
||||
// buffer has reallocated the original buffer slot after this buffer
|
||||
// was acquired.
|
||||
status_t err = ConsumerBase::releaseBufferLocked(buf, graphicBuffer, display, eglFence);
|
||||
// We could be releasing an EGL/Vulkan buffer, even if not currently attached to a GL context.
|
||||
mImageConsumer.onReleaseBufferLocked(buf);
|
||||
mEGLConsumer.onReleaseBufferLocked(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::detachFromContext() {
|
||||
ATRACE_CALL();
|
||||
SFT_LOGV("detachFromContext");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("detachFromContext: abandoned SurfaceTexture");
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
if (mOpMode != OpMode::attachedToGL) {
|
||||
SFT_LOGE("detachFromContext: SurfaceTexture is not attached to a GL context");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
status_t err = mEGLConsumer.detachFromContext(*this);
|
||||
if (err == OK) {
|
||||
mOpMode = OpMode::detached;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::attachToContext(uint32_t tex) {
|
||||
ATRACE_CALL();
|
||||
SFT_LOGV("attachToContext");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("attachToContext: abandoned SurfaceTexture");
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
if (mOpMode != OpMode::detached) {
|
||||
SFT_LOGE(
|
||||
"attachToContext: SurfaceTexture is already attached to a "
|
||||
"context");
|
||||
return INVALID_OPERATION;
|
||||
}
|
||||
|
||||
if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
// release possible ImageConsumer cache
|
||||
mImageConsumer.onFreeBufferLocked(mCurrentTexture);
|
||||
}
|
||||
|
||||
return mEGLConsumer.attachToContext(tex, *this);
|
||||
}
|
||||
|
||||
void SurfaceTexture::attachToView() {
|
||||
ATRACE_CALL();
|
||||
Mutex::Autolock _l(mMutex);
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("attachToView: abandoned SurfaceTexture");
|
||||
return;
|
||||
}
|
||||
if (mOpMode == OpMode::detached) {
|
||||
mOpMode = OpMode::attachedToView;
|
||||
|
||||
if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
// release possible EGLConsumer texture cache
|
||||
mEGLConsumer.onFreeBufferLocked(mCurrentTexture);
|
||||
mEGLConsumer.onAbandonLocked();
|
||||
}
|
||||
} else {
|
||||
SFT_LOGE("attachToView: already attached");
|
||||
}
|
||||
}
|
||||
|
||||
void SurfaceTexture::detachFromView() {
|
||||
ATRACE_CALL();
|
||||
Mutex::Autolock _l(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("detachFromView: abandoned SurfaceTexture");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mOpMode == OpMode::attachedToView) {
|
||||
mOpMode = OpMode::detached;
|
||||
// Free all EglImage and VkImage before the context is destroyed.
|
||||
for (int i=0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; i++) {
|
||||
mImageConsumer.onFreeBufferLocked(i);
|
||||
}
|
||||
} else {
|
||||
SFT_LOGE("detachFromView: not attached to View");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SurfaceTexture::getCurrentTextureTarget() const {
|
||||
return mTexTarget;
|
||||
}
|
||||
|
||||
void SurfaceTexture::getTransformMatrix(float mtx[16]) {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
|
||||
}
|
||||
|
||||
void SurfaceTexture::setFilteringEnabled(bool enabled) {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("setFilteringEnabled: SurfaceTexture is abandoned!");
|
||||
return;
|
||||
}
|
||||
bool needsRecompute = mFilteringEnabled != enabled;
|
||||
mFilteringEnabled = enabled;
|
||||
|
||||
if (needsRecompute && mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
SFT_LOGD("setFilteringEnabled called with no current item");
|
||||
}
|
||||
|
||||
if (needsRecompute && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
||||
computeCurrentTransformMatrixLocked();
|
||||
}
|
||||
}
|
||||
|
||||
void SurfaceTexture::computeCurrentTransformMatrixLocked() {
|
||||
SFT_LOGV("computeCurrentTransformMatrixLocked");
|
||||
sp<GraphicBuffer> buf = (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
|
||||
? nullptr
|
||||
: mSlots[mCurrentTexture].mGraphicBuffer;
|
||||
if (buf == nullptr) {
|
||||
SFT_LOGD("computeCurrentTransformMatrixLocked: no current item");
|
||||
}
|
||||
computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop, mCurrentTransform,
|
||||
mFilteringEnabled);
|
||||
}
|
||||
|
||||
void SurfaceTexture::computeTransformMatrix(float outTransform[16], const sp<GraphicBuffer>& buf,
|
||||
const Rect& cropRect, uint32_t transform,
|
||||
bool filtering) {
|
||||
// Transform matrices
|
||||
static const mat4 mtxFlipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
|
||||
static const mat4 mtxFlipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
|
||||
static const mat4 mtxRot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
|
||||
|
||||
mat4 xform;
|
||||
if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
|
||||
xform *= mtxFlipH;
|
||||
}
|
||||
if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
|
||||
xform *= mtxFlipV;
|
||||
}
|
||||
if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
|
||||
xform *= mtxRot90;
|
||||
}
|
||||
|
||||
if (!cropRect.isEmpty() && buf.get()) {
|
||||
float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
|
||||
float bufferWidth = buf->getWidth();
|
||||
float bufferHeight = buf->getHeight();
|
||||
float shrinkAmount = 0.0f;
|
||||
if (filtering) {
|
||||
// In order to prevent bilinear sampling beyond the edge of the
|
||||
// crop rectangle we may need to shrink it by 2 texels in each
|
||||
// dimension. Normally this would just need to take 1/2 a texel
|
||||
// off each end, but because the chroma channels of YUV420 images
|
||||
// are subsampled we may need to shrink the crop region by a whole
|
||||
// texel on each side.
|
||||
switch (buf->getPixelFormat()) {
|
||||
case PIXEL_FORMAT_RGBA_8888:
|
||||
case PIXEL_FORMAT_RGBX_8888:
|
||||
case PIXEL_FORMAT_RGBA_FP16:
|
||||
case PIXEL_FORMAT_RGBA_1010102:
|
||||
case PIXEL_FORMAT_RGB_888:
|
||||
case PIXEL_FORMAT_RGB_565:
|
||||
case PIXEL_FORMAT_BGRA_8888:
|
||||
// We know there's no subsampling of any channels, so we
|
||||
// only need to shrink by a half a pixel.
|
||||
shrinkAmount = 0.5;
|
||||
break;
|
||||
|
||||
default:
|
||||
// If we don't recognize the format, we must assume the
|
||||
// worst case (that we care about), which is YUV420.
|
||||
shrinkAmount = 1.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Only shrink the dimensions that are not the size of the buffer.
|
||||
if (cropRect.width() < bufferWidth) {
|
||||
tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
|
||||
sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / bufferWidth;
|
||||
}
|
||||
if (cropRect.height() < bufferHeight) {
|
||||
ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / bufferHeight;
|
||||
sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / bufferHeight;
|
||||
}
|
||||
|
||||
mat4 crop(sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1);
|
||||
xform = crop * xform;
|
||||
}
|
||||
|
||||
// SurfaceFlinger expects the top of its window textures to be at a Y
|
||||
// coordinate of 0, so SurfaceTexture must behave the same way. We don't
|
||||
// want to expose this to applications, however, so we must add an
|
||||
// additional vertical flip to the transform after all the other transforms.
|
||||
xform = mtxFlipV * xform;
|
||||
|
||||
memcpy(outTransform, xform.asArray(), sizeof(xform));
|
||||
}
|
||||
|
||||
Rect SurfaceTexture::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
|
||||
Rect outCrop = crop;
|
||||
|
||||
uint32_t newWidth = static_cast<uint32_t>(crop.width());
|
||||
uint32_t newHeight = static_cast<uint32_t>(crop.height());
|
||||
|
||||
if (newWidth * bufferHeight > newHeight * bufferWidth) {
|
||||
newWidth = newHeight * bufferWidth / bufferHeight;
|
||||
ALOGV("too wide: newWidth = %d", newWidth);
|
||||
} else if (newWidth * bufferHeight < newHeight * bufferWidth) {
|
||||
newHeight = newWidth * bufferHeight / bufferWidth;
|
||||
ALOGV("too tall: newHeight = %d", newHeight);
|
||||
}
|
||||
|
||||
uint32_t currentWidth = static_cast<uint32_t>(crop.width());
|
||||
uint32_t currentHeight = static_cast<uint32_t>(crop.height());
|
||||
|
||||
// The crop is too wide
|
||||
if (newWidth < currentWidth) {
|
||||
uint32_t dw = currentWidth - newWidth;
|
||||
auto halfdw = dw / 2;
|
||||
outCrop.left += halfdw;
|
||||
// Not halfdw because it would subtract 1 too few when dw is odd
|
||||
outCrop.right -= (dw - halfdw);
|
||||
// The crop is too tall
|
||||
} else if (newHeight < currentHeight) {
|
||||
uint32_t dh = currentHeight - newHeight;
|
||||
auto halfdh = dh / 2;
|
||||
outCrop.top += halfdh;
|
||||
// Not halfdh because it would subtract 1 too few when dh is odd
|
||||
outCrop.bottom -= (dh - halfdh);
|
||||
}
|
||||
|
||||
ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]", outCrop.left, outCrop.top, outCrop.right,
|
||||
outCrop.bottom);
|
||||
|
||||
return outCrop;
|
||||
}
|
||||
|
||||
nsecs_t SurfaceTexture::getTimestamp() {
|
||||
SFT_LOGV("getTimestamp");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentTimestamp;
|
||||
}
|
||||
|
||||
android_dataspace SurfaceTexture::getCurrentDataSpace() {
|
||||
SFT_LOGV("getCurrentDataSpace");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentDataSpace;
|
||||
}
|
||||
|
||||
uint64_t SurfaceTexture::getFrameNumber() {
|
||||
SFT_LOGV("getFrameNumber");
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentFrameNumber;
|
||||
}
|
||||
|
||||
Rect SurfaceTexture::getCurrentCrop() const {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
|
||||
? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
|
||||
: mCurrentCrop;
|
||||
}
|
||||
|
||||
uint32_t SurfaceTexture::getCurrentTransform() const {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentTransform;
|
||||
}
|
||||
|
||||
uint32_t SurfaceTexture::getCurrentScalingMode() const {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentScalingMode;
|
||||
}
|
||||
|
||||
sp<Fence> SurfaceTexture::getCurrentFence() const {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentFence;
|
||||
}
|
||||
|
||||
std::shared_ptr<FenceTime> SurfaceTexture::getCurrentFenceTime() const {
|
||||
Mutex::Autolock lock(mMutex);
|
||||
return mCurrentFenceTime;
|
||||
}
|
||||
|
||||
void SurfaceTexture::freeBufferLocked(int slotIndex) {
|
||||
SFT_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
|
||||
if (slotIndex == mCurrentTexture) {
|
||||
mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
|
||||
}
|
||||
// The slotIndex buffer could have EGL or SkImage cache, but there is no way to tell for sure.
|
||||
// Buffers can be freed after SurfaceTexture has detached from GL context or View.
|
||||
mImageConsumer.onFreeBufferLocked(slotIndex);
|
||||
mEGLConsumer.onFreeBufferLocked(slotIndex);
|
||||
ConsumerBase::freeBufferLocked(slotIndex);
|
||||
}
|
||||
|
||||
void SurfaceTexture::abandonLocked() {
|
||||
SFT_LOGV("abandonLocked");
|
||||
mEGLConsumer.onAbandonLocked();
|
||||
ConsumerBase::abandonLocked();
|
||||
}
|
||||
|
||||
status_t SurfaceTexture::setConsumerUsageBits(uint64_t usage) {
|
||||
return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
|
||||
}
|
||||
|
||||
void SurfaceTexture::dumpLocked(String8& result, const char* prefix) const {
|
||||
result.appendFormat(
|
||||
"%smTexName=%d mCurrentTexture=%d\n"
|
||||
"%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
|
||||
prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, mCurrentCrop.top,
|
||||
mCurrentCrop.right, mCurrentCrop.bottom, mCurrentTransform);
|
||||
|
||||
ConsumerBase::dumpLocked(result, prefix);
|
||||
}
|
||||
|
||||
sk_sp<SkImage> SurfaceTexture::dequeueImage(SkMatrix& transformMatrix, bool* queueEmpty,
|
||||
uirenderer::RenderState& renderState) {
|
||||
Mutex::Autolock _l(mMutex);
|
||||
|
||||
if (mAbandoned) {
|
||||
SFT_LOGE("dequeueImage: SurfaceTexture is abandoned!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mOpMode != OpMode::attachedToView) {
|
||||
SFT_LOGE("dequeueImage: SurfaceTexture is not attached to a View");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto image = mImageConsumer.dequeueImage(queueEmpty, *this, renderState);
|
||||
if (image.get()) {
|
||||
uirenderer::mat4(mCurrentTransformMatrix).copyTo(transformMatrix);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
@@ -1,452 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gui/BufferQueueDefs.h>
|
||||
#include <gui/ConsumerBase.h>
|
||||
|
||||
#include <ui/FenceTime.h>
|
||||
#include <ui/GraphicBuffer.h>
|
||||
|
||||
#include <utils/Mutex.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
#include "EGLConsumer.h"
|
||||
#include "ImageConsumer.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
namespace uirenderer {
|
||||
class RenderState;
|
||||
}
|
||||
|
||||
/*
|
||||
* SurfaceTexture consumes buffers of graphics data from a BufferQueue,
|
||||
* and makes them available to HWUI render thread as a SkImage and to
|
||||
* an application GL render thread as an OpenGL texture.
|
||||
*
|
||||
* When attached to an application GL render thread, a typical usage
|
||||
* pattern is to set up the SurfaceTexture with the
|
||||
* desired options, and call updateTexImage() when a new frame is desired.
|
||||
* If a new frame is available, the texture will be updated. If not,
|
||||
* the previous contents are retained.
|
||||
*
|
||||
* When attached to a HWUI render thread, the TextureView implementation
|
||||
* calls dequeueImage, which either pulls a new SkImage or returns the
|
||||
* last cached SkImage if BufferQueue is empty.
|
||||
* When attached to HWUI render thread, SurfaceTexture is compatible to
|
||||
* both Vulkan and GL drawing pipelines.
|
||||
*/
|
||||
class ANDROID_API SurfaceTexture : public ConsumerBase {
|
||||
public:
|
||||
enum { TEXTURE_EXTERNAL = 0x8D65 }; // GL_TEXTURE_EXTERNAL_OES
|
||||
typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
|
||||
|
||||
/**
|
||||
* SurfaceTexture constructs a new SurfaceTexture object. If the constructor with
|
||||
* the tex parameter is used, tex indicates the name of the OpenGL ES
|
||||
* texture to which images are to be streamed. texTarget specifies the
|
||||
* OpenGL ES texture target to which the texture will be bound in
|
||||
* updateTexImage. useFenceSync specifies whether fences should be used to
|
||||
* synchronize access to buffers if that behavior is enabled at
|
||||
* compile-time.
|
||||
*
|
||||
* A SurfaceTexture may be detached from one OpenGL ES context and then
|
||||
* attached to a different context using the detachFromContext and
|
||||
* attachToContext methods, respectively. The intention of these methods is
|
||||
* purely to allow a SurfaceTexture to be transferred from one consumer
|
||||
* context to another. If such a transfer is not needed there is no
|
||||
* requirement that either of these methods be called.
|
||||
*
|
||||
* If the constructor with the tex parameter is used, the SurfaceTexture is
|
||||
* created in a state where it is considered attached to an OpenGL ES
|
||||
* context for the purposes of the attachToContext and detachFromContext
|
||||
* methods. However, despite being considered "attached" to a context, the
|
||||
* specific OpenGL ES context doesn't get latched until the first call to
|
||||
* updateTexImage. After that point, all calls to updateTexImage must be
|
||||
* made with the same OpenGL ES context current.
|
||||
*
|
||||
* If the constructor without the tex parameter is used, the SurfaceTexture is
|
||||
* created in a detached state, and attachToContext must be called before
|
||||
* calls to updateTexImage.
|
||||
*/
|
||||
SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, uint32_t texureTarget,
|
||||
bool useFenceSync, bool isControlledByApp);
|
||||
|
||||
SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t texureTarget, bool useFenceSync,
|
||||
bool isControlledByApp);
|
||||
|
||||
/**
|
||||
* updateTexImage acquires the most recently queued buffer, and sets the
|
||||
* image contents of the target texture to it.
|
||||
*
|
||||
* This call may only be made while the OpenGL ES context to which the
|
||||
* target texture belongs is bound to the calling thread.
|
||||
*
|
||||
* This calls doGLFenceWait to ensure proper synchronization.
|
||||
*/
|
||||
status_t updateTexImage();
|
||||
|
||||
/**
|
||||
* releaseTexImage releases the texture acquired in updateTexImage().
|
||||
* This is intended to be used in single buffer mode.
|
||||
*
|
||||
* This call may only be made while the OpenGL ES context to which the
|
||||
* target texture belongs is bound to the calling thread.
|
||||
*/
|
||||
status_t releaseTexImage();
|
||||
|
||||
/**
|
||||
* getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
|
||||
* associated with the texture image set by the most recent call to
|
||||
* updateTexImage.
|
||||
*
|
||||
* This transform matrix maps 2D homogeneous texture coordinates of the form
|
||||
* (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
|
||||
* coordinate that should be used to sample that location from the texture.
|
||||
* Sampling the texture outside of the range of this transform is undefined.
|
||||
*
|
||||
* This transform is necessary to compensate for transforms that the stream
|
||||
* content producer may implicitly apply to the content. By forcing users of
|
||||
* a SurfaceTexture to apply this transform we avoid performing an extra
|
||||
* copy of the data that would be needed to hide the transform from the
|
||||
* user.
|
||||
*
|
||||
* The matrix is stored in column-major order so that it may be passed
|
||||
* directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
|
||||
* functions.
|
||||
*/
|
||||
void getTransformMatrix(float mtx[16]);
|
||||
|
||||
/**
|
||||
* Computes the transform matrix documented by getTransformMatrix
|
||||
* from the BufferItem sub parts.
|
||||
*/
|
||||
static void computeTransformMatrix(float outTransform[16], const sp<GraphicBuffer>& buf,
|
||||
const Rect& cropRect, uint32_t transform, bool filtering);
|
||||
|
||||
/**
|
||||
* Scale the crop down horizontally or vertically such that it has the
|
||||
* same aspect ratio as the buffer does.
|
||||
*/
|
||||
static Rect scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight);
|
||||
|
||||
/**
|
||||
* getTimestamp retrieves the timestamp associated with the texture image
|
||||
* set by the most recent call to updateTexImage.
|
||||
*
|
||||
* The timestamp is in nanoseconds, and is monotonically increasing. Its
|
||||
* other semantics (zero point, etc) are source-dependent and should be
|
||||
* documented by the source.
|
||||
*/
|
||||
int64_t getTimestamp();
|
||||
|
||||
/**
|
||||
* getDataSpace retrieves the DataSpace associated with the texture image
|
||||
* set by the most recent call to updateTexImage.
|
||||
*/
|
||||
android_dataspace getCurrentDataSpace();
|
||||
|
||||
/**
|
||||
* getFrameNumber retrieves the frame number associated with the texture
|
||||
* image set by the most recent call to updateTexImage.
|
||||
*
|
||||
* The frame number is an incrementing counter set to 0 at the creation of
|
||||
* the BufferQueue associated with this consumer.
|
||||
*/
|
||||
uint64_t getFrameNumber();
|
||||
|
||||
/**
|
||||
* setDefaultBufferSize is used to set the size of buffers returned by
|
||||
* requestBuffers when a with and height of zero is requested.
|
||||
* A call to setDefaultBufferSize() may trigger requestBuffers() to
|
||||
* be called from the client.
|
||||
* The width and height parameters must be no greater than the minimum of
|
||||
* GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
|
||||
* An error due to invalid dimensions might not be reported until
|
||||
* updateTexImage() is called.
|
||||
*/
|
||||
status_t setDefaultBufferSize(uint32_t width, uint32_t height);
|
||||
|
||||
/**
|
||||
* setFilteringEnabled sets whether the transform matrix should be computed
|
||||
* for use with bilinear filtering.
|
||||
*/
|
||||
void setFilteringEnabled(bool enabled);
|
||||
|
||||
/**
|
||||
* getCurrentTextureTarget returns the texture target of the current
|
||||
* texture as returned by updateTexImage().
|
||||
*/
|
||||
uint32_t getCurrentTextureTarget() const;
|
||||
|
||||
/**
|
||||
* getCurrentCrop returns the cropping rectangle of the current buffer.
|
||||
*/
|
||||
Rect getCurrentCrop() const;
|
||||
|
||||
/**
|
||||
* getCurrentTransform returns the transform of the current buffer.
|
||||
*/
|
||||
uint32_t getCurrentTransform() const;
|
||||
|
||||
/**
|
||||
* getCurrentScalingMode returns the scaling mode of the current buffer.
|
||||
*/
|
||||
uint32_t getCurrentScalingMode() const;
|
||||
|
||||
/**
|
||||
* getCurrentFence returns the fence indicating when the current buffer is
|
||||
* ready to be read from.
|
||||
*/
|
||||
sp<Fence> getCurrentFence() const;
|
||||
|
||||
/**
|
||||
* getCurrentFence returns the FenceTime indicating when the current
|
||||
* buffer is ready to be read from.
|
||||
*/
|
||||
std::shared_ptr<FenceTime> getCurrentFenceTime() const;
|
||||
|
||||
/**
|
||||
* setConsumerUsageBits overrides the ConsumerBase method to OR
|
||||
* DEFAULT_USAGE_FLAGS to usage.
|
||||
*/
|
||||
status_t setConsumerUsageBits(uint64_t usage);
|
||||
|
||||
/**
|
||||
* detachFromContext detaches the SurfaceTexture from the calling thread's
|
||||
* current OpenGL ES context. This context must be the same as the context
|
||||
* that was current for previous calls to updateTexImage.
|
||||
*
|
||||
* Detaching a SurfaceTexture from an OpenGL ES context will result in the
|
||||
* deletion of the OpenGL ES texture object into which the images were being
|
||||
* streamed. After a SurfaceTexture has been detached from the OpenGL ES
|
||||
* context calls to updateTexImage will fail returning INVALID_OPERATION
|
||||
* until the SurfaceTexture is attached to a new OpenGL ES context using the
|
||||
* attachToContext method.
|
||||
*/
|
||||
status_t detachFromContext();
|
||||
|
||||
/**
|
||||
* attachToContext attaches a SurfaceTexture that is currently in the
|
||||
* 'detached' state to the current OpenGL ES context. A SurfaceTexture is
|
||||
* in the 'detached' state iff detachFromContext has successfully been
|
||||
* called and no calls to attachToContext have succeeded since the last
|
||||
* detachFromContext call. Calls to attachToContext made on a
|
||||
* SurfaceTexture that is not in the 'detached' state will result in an
|
||||
* INVALID_OPERATION error.
|
||||
*
|
||||
* The tex argument specifies the OpenGL ES texture object name in the
|
||||
* new context into which the image contents will be streamed. A successful
|
||||
* call to attachToContext will result in this texture object being bound to
|
||||
* the texture target and populated with the image contents that were
|
||||
* current at the time of the last call to detachFromContext.
|
||||
*/
|
||||
status_t attachToContext(uint32_t tex);
|
||||
|
||||
sk_sp<SkImage> dequeueImage(SkMatrix& transformMatrix, bool* queueEmpty,
|
||||
uirenderer::RenderState& renderState);
|
||||
|
||||
/**
|
||||
* attachToView attaches a SurfaceTexture that is currently in the
|
||||
* 'detached' state to HWUI View system.
|
||||
*/
|
||||
void attachToView();
|
||||
|
||||
/**
|
||||
* detachFromView detaches a SurfaceTexture from HWUI View system.
|
||||
*/
|
||||
void detachFromView();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* abandonLocked overrides the ConsumerBase method to clear
|
||||
* mCurrentTextureImage in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
virtual void abandonLocked();
|
||||
|
||||
/**
|
||||
* dumpLocked overrides the ConsumerBase method to dump SurfaceTexture-
|
||||
* specific info in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
virtual void dumpLocked(String8& result, const char* prefix) const override;
|
||||
|
||||
/**
|
||||
* acquireBufferLocked overrides the ConsumerBase method to update the
|
||||
* mEglSlots array in addition to the ConsumerBase behavior.
|
||||
*/
|
||||
virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
|
||||
uint64_t maxFrameNumber = 0) override;
|
||||
|
||||
/**
|
||||
* releaseBufferLocked overrides the ConsumerBase method to update the
|
||||
* mEglSlots array in addition to the ConsumerBase.
|
||||
*/
|
||||
virtual status_t releaseBufferLocked(int slot, const sp<GraphicBuffer> graphicBuffer,
|
||||
EGLDisplay display, EGLSyncKHR eglFence) override;
|
||||
|
||||
/**
|
||||
* freeBufferLocked frees up the given buffer slot. If the slot has been
|
||||
* initialized this will release the reference to the GraphicBuffer in that
|
||||
* slot and destroy the EGLImage in that slot. Otherwise it has no effect.
|
||||
*
|
||||
* This method must be called with mMutex locked.
|
||||
*/
|
||||
virtual void freeBufferLocked(int slotIndex);
|
||||
|
||||
/**
|
||||
* computeCurrentTransformMatrixLocked computes the transform matrix for the
|
||||
* current texture. It uses mCurrentTransform and the current GraphicBuffer
|
||||
* to compute this matrix and stores it in mCurrentTransformMatrix.
|
||||
* mCurrentTextureImage must not be NULL.
|
||||
*/
|
||||
void computeCurrentTransformMatrixLocked();
|
||||
|
||||
/**
|
||||
* The default consumer usage flags that SurfaceTexture always sets on its
|
||||
* BufferQueue instance; these will be OR:d with any additional flags passed
|
||||
* from the SurfaceTexture user. In particular, SurfaceTexture will always
|
||||
* consume buffers as hardware textures.
|
||||
*/
|
||||
static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
|
||||
|
||||
/**
|
||||
* mCurrentCrop is the crop rectangle that applies to the current texture.
|
||||
* It gets set each time updateTexImage is called.
|
||||
*/
|
||||
Rect mCurrentCrop;
|
||||
|
||||
/**
|
||||
* mCurrentTransform is the transform identifier for the current texture. It
|
||||
* gets set each time updateTexImage is called.
|
||||
*/
|
||||
uint32_t mCurrentTransform;
|
||||
|
||||
/**
|
||||
* mCurrentScalingMode is the scaling mode for the current texture. It gets
|
||||
* set each time updateTexImage is called.
|
||||
*/
|
||||
uint32_t mCurrentScalingMode;
|
||||
|
||||
/**
|
||||
* mCurrentFence is the fence received from BufferQueue in updateTexImage.
|
||||
*/
|
||||
sp<Fence> mCurrentFence;
|
||||
|
||||
/**
|
||||
* The FenceTime wrapper around mCurrentFence.
|
||||
*/
|
||||
std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
|
||||
|
||||
/**
|
||||
* mCurrentTransformMatrix is the transform matrix for the current texture.
|
||||
* It gets computed by computeTransformMatrix each time updateTexImage is
|
||||
* called.
|
||||
*/
|
||||
float mCurrentTransformMatrix[16];
|
||||
|
||||
/**
|
||||
* mCurrentTimestamp is the timestamp for the current texture. It
|
||||
* gets set each time updateTexImage is called.
|
||||
*/
|
||||
int64_t mCurrentTimestamp;
|
||||
|
||||
/**
|
||||
* mCurrentDataSpace is the dataspace for the current texture. It
|
||||
* gets set each time updateTexImage is called.
|
||||
*/
|
||||
android_dataspace mCurrentDataSpace;
|
||||
|
||||
/**
|
||||
* mCurrentFrameNumber is the frame counter for the current texture.
|
||||
* It gets set each time updateTexImage is called.
|
||||
*/
|
||||
uint64_t mCurrentFrameNumber;
|
||||
|
||||
uint32_t mDefaultWidth, mDefaultHeight;
|
||||
|
||||
/**
|
||||
* mFilteringEnabled indicates whether the transform matrix is computed for
|
||||
* use with bilinear filtering. It defaults to true and is changed by
|
||||
* setFilteringEnabled().
|
||||
*/
|
||||
bool mFilteringEnabled;
|
||||
|
||||
/**
|
||||
* mTexName is the name of the OpenGL texture to which streamed images will
|
||||
* be bound when updateTexImage is called. It is set at construction time
|
||||
* and can be changed with a call to attachToContext.
|
||||
*/
|
||||
uint32_t mTexName;
|
||||
|
||||
/**
|
||||
* mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync
|
||||
* extension should be used to prevent buffers from being dequeued before
|
||||
* it's safe for them to be written. It gets set at construction time and
|
||||
* never changes.
|
||||
*/
|
||||
const bool mUseFenceSync;
|
||||
|
||||
/**
|
||||
* mTexTarget is the GL texture target with which the GL texture object is
|
||||
* associated. It is set in the constructor and never changed. It is
|
||||
* almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
|
||||
* Browser. In that case it is set to GL_TEXTURE_2D to allow
|
||||
* glCopyTexSubImage to read from the texture. This is a hack to work
|
||||
* around a GL driver limitation on the number of FBO attachments, which the
|
||||
* browser's tile cache exceeds.
|
||||
*/
|
||||
const uint32_t mTexTarget;
|
||||
|
||||
/**
|
||||
* mCurrentTexture is the buffer slot index of the buffer that is currently
|
||||
* bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
|
||||
* indicating that no buffer slot is currently bound to the texture. Note,
|
||||
* however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
|
||||
* that no buffer is bound to the texture. A call to setBufferCount will
|
||||
* reset mCurrentTexture to INVALID_BUFFER_SLOT.
|
||||
*/
|
||||
int mCurrentTexture;
|
||||
|
||||
enum class OpMode { detached, attachedToView, attachedToGL };
|
||||
/**
|
||||
* mOpMode indicates whether the SurfaceTexture is currently attached to
|
||||
* an OpenGL ES context or the HWUI view system. For legacy reasons, this is initialized to,
|
||||
* "attachedToGL" indicating that the SurfaceTexture is considered to be attached to
|
||||
* whatever GL context is current at the time of the first updateTexImage call.
|
||||
* It is set to "detached" by detachFromContext, and then set to "attachedToGL" again by
|
||||
* attachToContext.
|
||||
* attachToView/detachFromView are used to attach/detach from HWUI view system.
|
||||
*/
|
||||
OpMode mOpMode;
|
||||
|
||||
/**
|
||||
* mEGLConsumer has SurfaceTexture logic used when attached to GL context.
|
||||
*/
|
||||
EGLConsumer mEGLConsumer;
|
||||
|
||||
/**
|
||||
* mImageConsumer has SurfaceTexture logic used when attached to HWUI view system.
|
||||
*/
|
||||
ImageConsumer mImageConsumer;
|
||||
|
||||
friend class ImageConsumer;
|
||||
friend class EGLConsumer;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
} // namespace android
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace android {
|
||||
namespace uirenderer {
|
||||
|
||||
@@ -23,25 +23,19 @@
|
||||
|
||||
#include <gui/Surface.h>
|
||||
|
||||
#include <gui/surfacetexture/surface_texture_platform.h>
|
||||
|
||||
#include <android_runtime/android_graphics_SurfaceTexture.h>
|
||||
|
||||
#include "surfacetexture/SurfaceTexture.h"
|
||||
|
||||
using namespace android;
|
||||
|
||||
struct ASurfaceTexture {
|
||||
sp<SurfaceTexture> consumer;
|
||||
sp<IGraphicBufferProducer> producer;
|
||||
};
|
||||
|
||||
ASurfaceTexture* ASurfaceTexture_fromSurfaceTexture(JNIEnv* env, jobject surfacetexture) {
|
||||
if (!surfacetexture || !android_SurfaceTexture_isInstanceOf(env, surfacetexture)) {
|
||||
return nullptr;
|
||||
}
|
||||
ASurfaceTexture* ast = new ASurfaceTexture;
|
||||
ast->consumer = SurfaceTexture_getSurfaceTexture(env, surfacetexture);
|
||||
ast->producer = SurfaceTexture_getProducer(env, surfacetexture);
|
||||
return ast;
|
||||
auto consumer = SurfaceTexture_getSurfaceTexture(env, surfacetexture);
|
||||
auto producer = SurfaceTexture_getProducer(env, surfacetexture);
|
||||
return ASurfaceTexture_create(consumer, producer);
|
||||
}
|
||||
|
||||
ANativeWindow* ASurfaceTexture_acquireANativeWindow(ASurfaceTexture* st) {
|
||||
|
||||
Reference in New Issue
Block a user