Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 989bb371 authored by John Reck's avatar John Reck Committed by Android (Google) Code Review
Browse files

Merge "Make HardwareRenderer public API"

parents b45f78f1 fe5dfcac
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -14096,6 +14096,34 @@ package android.graphics {
    ctor @Deprecated public EmbossMaskFilter(float[], float, float, float);
  }
  public class HardwareRenderer {
    ctor public HardwareRenderer();
    method public void clearContent();
    method public android.graphics.HardwareRenderer.FrameRenderRequest createRenderRequest();
    method public void destroy();
    method public boolean isOpaque();
    method public void notifyFramePending();
    method public void setContentRoot(@Nullable android.graphics.RenderNode);
    method public void setLightSourceAlpha(@FloatRange(from=0.0f, to=1.0f) float, @FloatRange(from=0.0f, to=1.0f) float);
    method public void setLightSourceGeometry(float, float, float, float);
    method public void setName(String);
    method public void setOpaque(boolean);
    method public void setStopped(boolean);
    method public void setSurface(@Nullable android.view.Surface);
    field public static final int SYNC_CONTEXT_IS_STOPPED = 4; // 0x4
    field public static final int SYNC_FRAME_DROPPED = 8; // 0x8
    field public static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 2; // 0x2
    field public static final int SYNC_OK = 0; // 0x0
    field public static final int SYNC_REDRAW_REQUESTED = 1; // 0x1
  }
  public final class HardwareRenderer.FrameRenderRequest {
    method public android.graphics.HardwareRenderer.FrameRenderRequest setFrameCommitCallback(@NonNull java.util.concurrent.Executor, @NonNull Runnable);
    method public android.graphics.HardwareRenderer.FrameRenderRequest setVsyncTime(long);
    method public android.graphics.HardwareRenderer.FrameRenderRequest setWaitForPresent(boolean);
    method public int syncAndDraw();
  }
  public final class ImageDecoder implements java.lang.AutoCloseable {
    method public void close();
    method @AnyThread @NonNull public static android.graphics.ImageDecoder.Source createSource(@NonNull android.content.res.Resources, int);
+6 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.graphics.Paint;
import android.graphics.RecordingCanvas;
import android.graphics.RenderNode;
import android.os.Build;
import android.os.Handler;
import android.util.SparseIntArray;

import com.android.internal.util.VirtualRefBasePtr;
@@ -84,6 +85,7 @@ public class RenderNodeAnimator extends Animator {

    private VirtualRefBasePtr mNativePtr;

    private Handler mHandler;
    private RenderNode mTarget;
    private View mViewTarget;
    private int mRenderProperty = -1;
@@ -222,6 +224,9 @@ public class RenderNodeAnimator extends Animator {
    private void moveToRunningState() {
        mState = STATE_RUNNING;
        if (mNativePtr != null) {
            if (mHandler == null) {
                mHandler = new Handler();
            }
            nStart(mNativePtr.get());
        }
        notifyStartListeners();
@@ -497,7 +502,7 @@ public class RenderNodeAnimator extends Animator {
    // Called by native
    @UnsupportedAppUsage
    private static void callOnFinished(RenderNodeAnimator animator) {
        animator.onFinished();
        animator.mHandler.post(animator::onFinished);
    }

    @Override
+4 −2
Original line number Diff line number Diff line
@@ -453,7 +453,7 @@ public final class ThreadedRenderer extends HardwareRenderer {
     */
    void destroyHardwareResources(View view) {
        destroyResources(view);
        destroyHardwareResources();
        clearContent();
    }

    private static void destroyResources(View view) {
@@ -735,7 +735,9 @@ public final class ThreadedRenderer extends HardwareRenderer {
            if (callback != null) {
                setFrameCallback(callback);
            }
            syncAndDrawFrame(vsync);
            createRenderRequest()
                    .setVsyncTime(vsync)
                    .syncAndDraw();
        }
    }
}
+18 −14
Original line number Diff line number Diff line
@@ -3404,7 +3404,9 @@ public final class ViewRootImpl implements ViewParent,
                    .captureFrameCommitCallbacks();
            if (mReportNextDraw) {
                usingAsyncReport = true;
                mAttachInfo.mThreadedRenderer.setFrameCompleteCallback((long frameNr) -> {
                final Handler handler = mAttachInfo.mHandler;
                mAttachInfo.mThreadedRenderer.setFrameCompleteCallback((long frameNr) ->
                        handler.post(() -> {
                            // TODO: Use the frame number
                            pendingDrawFinished();
                            if (commitCallbacks != null) {
@@ -3412,13 +3414,15 @@ public final class ViewRootImpl implements ViewParent,
                                    commitCallbacks.get(i).run();
                                }
                            }
                });
                        }));
            } else if (commitCallbacks != null && commitCallbacks.size() > 0) {
                mAttachInfo.mThreadedRenderer.setFrameCompleteCallback((long frameNr) -> {
                final Handler handler = mAttachInfo.mHandler;
                mAttachInfo.mThreadedRenderer.setFrameCompleteCallback((long frameNr) ->
                        handler.post(() -> {
                            for (int i = 0; i < commitCallbacks.size(); i++) {
                                commitCallbacks.get(i).run();
                            }
                });
                        }));
            }
        }

+13 −67
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@

#include <private/EGL/cache.h>

#include <utils/Looper.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
#include <utils/Timers.h>
@@ -144,52 +143,22 @@ private:
    uint32_t mRequestId;
};

class RenderingException : public MessageHandler {
class FrameCompleteWrapper : public LightRefBase<FrameCompleteWrapper> {
public:
    RenderingException(JavaVM* vm, const std::string& message)
            : mVm(vm)
            , mMessage(message) {
    }

    virtual void handleMessage(const Message&) {
        throwException(mVm, mMessage);
    }

    static void throwException(JavaVM* vm, const std::string& message) {
        JNIEnv* env = getenv(vm);
        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
    }

private:
    JavaVM* mVm;
    std::string mMessage;
};

class FrameCompleteWrapper : public MessageHandler {
public:
    FrameCompleteWrapper(JNIEnv* env, jobject jobject) {
        mLooper = Looper::getForThread();
        LOG_ALWAYS_FATAL_IF(!mLooper.get(), "Must create runnable on a Looper thread!");
    explicit FrameCompleteWrapper(JNIEnv* env, jobject jobject) {
        env->GetJavaVM(&mVm);
        mObject = env->NewGlobalRef(jobject);
        LOG_ALWAYS_FATAL_IF(!mObject, "Failed to make global ref");
    }

    virtual ~FrameCompleteWrapper() {
    ~FrameCompleteWrapper() {
        releaseObject();
    }

    void postFrameComplete(int64_t frameNr) {
        if (mObject) {
            mFrameNr = frameNr;
            mLooper->sendMessage(this, 0);
        }
    }

    virtual void handleMessage(const Message&) {
    void onFrameComplete(int64_t frameNr) {
        if (mObject) {
            ATRACE_FORMAT("frameComplete %" PRId64, mFrameNr);
            getenv(mVm)->CallVoidMethod(mObject, gFrameCompleteCallback.onFrameComplete, mFrameNr);
            ATRACE_FORMAT("frameComplete %" PRId64, frameNr);
            getenv(mVm)->CallVoidMethod(mObject, gFrameCompleteCallback.onFrameComplete, frameNr);
            releaseObject();
        }
    }
@@ -197,8 +166,6 @@ public:
private:
    JavaVM* mVm;
    jobject mObject;
    sp<Looper> mLooper;
    int64_t mFrameNr = -1;

    void releaseObject() {
        if (mObject) {
@@ -211,16 +178,14 @@ private:
class RootRenderNode : public RenderNode, ErrorHandler {
public:
    explicit RootRenderNode(JNIEnv* env) : RenderNode() {
        mLooper = Looper::getForThread();
        LOG_ALWAYS_FATAL_IF(!mLooper.get(),
                "Must create RootRenderNode on a thread with a looper!");
        env->GetJavaVM(&mVm);
    }

    virtual ~RootRenderNode() {}

    virtual void onError(const std::string& message) override {
        mLooper->sendMessage(new RenderingException(mVm, message), 0);
        JNIEnv* env = getenv(mVm);
        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
    }

    virtual void prepareTree(TreeInfo& info) override {
@@ -249,14 +214,6 @@ public:
        info.errorHandler = nullptr;
    }

    void sendMessage(const sp<MessageHandler>& handler) {
        mLooper->sendMessage(handler, 0);
    }

    void sendMessageDelayed(const sp<MessageHandler>& handler, nsecs_t delayInMs) {
        mLooper->sendMessageDelayed(ms2ns(delayInMs), handler, 0);
    }

    void attachAnimatingNode(RenderNode* animatingNode) {
        mPendingAnimatingRenderNodes.push_back(animatingNode);
    }
@@ -404,7 +361,6 @@ public:
    }

private:
    sp<Looper> mLooper;
    JavaVM* mVm;
    std::vector< sp<RenderNode> > mPendingAnimatingRenderNodes;
    std::set< sp<PropertyValuesAnimatorSet> > mPendingVectorDrawableAnimators;
@@ -435,7 +391,9 @@ private:
            // the onFinished callback will then be ignored.
            sp<FinishAndInvokeListener> message
                    = new FinishAndInvokeListener(anim);
            sendMessageDelayed(message, remainingTimeInMs);
            auto looper = Looper::getForThread();
            LOG_ALWAYS_FATAL_IF(looper == nullptr, "Not on a looper thread?");
            looper->sendMessageDelayed(ms2ns(remainingTimeInMs), message, 0);
            anim->clearOneShotListener();
        }
    }
@@ -463,7 +421,6 @@ public:
    virtual void runRemainingAnimations(TreeInfo& info) {
        AnimationContext::runRemainingAnimations(info);
        mRootNode->runVectorDrawableAnimators(this, info);
        postOnFinishedEvents();
    }

    virtual void pauseAnimators() override {
@@ -471,27 +428,16 @@ public:
    }

    virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) {
        OnFinishedEvent event(animator, listener);
        mOnFinishedEvents.push_back(event);
        listener->onAnimationFinished(animator);
    }

    virtual void destroy() {
        AnimationContext::destroy();
        mRootNode->detachAnimators();
        postOnFinishedEvents();
    }

private:
    sp<RootRenderNode> mRootNode;
    std::vector<OnFinishedEvent> mOnFinishedEvents;

    void postOnFinishedEvents() {
        if (mOnFinishedEvents.size()) {
            sp<InvokeAnimationListeners> message
                    = new InvokeAnimationListeners(mOnFinishedEvents);
            mRootNode->sendMessage(message);
        }
    }
};

class ContextFactoryImpl : public IContextFactory {
@@ -958,7 +904,7 @@ static void android_view_ThreadedRenderer_setFrameCompleteCallback(JNIEnv* env,
    } else {
        sp<FrameCompleteWrapper> wrapper = new FrameCompleteWrapper{env, callback};
        proxy->setFrameCompleteCallback([wrapper](int64_t frameNr) {
            wrapper->postFrameComplete(frameNr);
            wrapper->onFrameComplete(frameNr);
        });
    }
}
Loading