104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck/*
204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * Copyright (C) 2014 The Android Open Source Project
304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck *
404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * Licensed under the Apache License, Version 2.0 (the "License");
504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * you may not use this file except in compliance with the License.
604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * You may obtain a copy of the License at
704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck *
804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck *      http://www.apache.org/licenses/LICENSE-2.0
904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck *
1004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * Unless required by applicable law or agreed to in writing, software
1104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * distributed under the License is distributed on an "AS IS" BASIS,
1204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * See the License for the specific language governing permissions and
1404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck * limitations under the License.
1504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck */
165e00c7ce063116c11315639f0035aca8ad73e8ccChris Craik
175e00c7ce063116c11315639f0035aca8ad73e8ccChris Craik#pragma once
1804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
1904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include <cutils/compiler.h>
2004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include <gui/GLConsumer.h>
2104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include <SkColorFilter.h>
2204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include <SkMatrix.h>
2304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include <utils/StrongPointer.h>
2404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
258cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel#include <GLES2/gl2.h>
268cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel#include <GLES2/gl2ext.h>
278cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel
2804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include "Layer.h"
2904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck#include "Rect.h"
30749906b468912dab7bf69a86e852deac3e80b0ccJohn Reck#include "renderthread/RenderThread.h"
3104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
3204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Recknamespace android {
3304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Recknamespace uirenderer {
3404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
353e9999bd866fac71c72e6b484a9836c87c328a08sergeyvclass RenderState;
363e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
3704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck// Container to hold the properties a layer should be set to at the start
3804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck// of a render pass
39d72e0a339b54af0c4e731513bbad120dff694723John Reckclass DeferredLayerUpdater : public VirtualLightRefBase {
4004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reckpublic:
41a39dd595791b41222ab310b5e8a76a64faa8ba5cJohn Reck    // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
42a39dd595791b41222ab310b5e8a76a64faa8ba5cJohn Reck    // and will not call incrementRef on it as a result.
433e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    typedef std::function<Layer*(RenderState& renderState, uint32_t layerWidth,
443e9999bd866fac71c72e6b484a9836c87c328a08sergeyv            uint32_t layerHeight, SkColorFilter* colorFilter, int alpha,
453e9999bd866fac71c72e6b484a9836c87c328a08sergeyv            SkBlendMode mode, bool blend)> CreateLayerFn;
463e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState,
473e9999bd866fac71c72e6b484a9836c87c328a08sergeyv            CreateLayerFn createLayerFn, Layer::Api layerApi);
483e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
4904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    ANDROID_API ~DeferredLayerUpdater();
5004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
51a41f29cc000680f402d51972dfad0a46c5a3cb07Dan Stoza    ANDROID_API bool setSize(int width, int height) {
5204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        if (mWidth != width || mHeight != height) {
5304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck            mWidth = width;
5404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck            mHeight = height;
5504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck            return true;
5604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        }
5704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        return false;
5804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
5904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
60417ed6d4337e5409d52f58cc93677c8715193f32John Reck    int getWidth() { return mWidth; }
61417ed6d4337e5409d52f58cc93677c8715193f32John Reck    int getHeight() { return mHeight; }
62417ed6d4337e5409d52f58cc93677c8715193f32John Reck
6304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    ANDROID_API bool setBlend(bool blend) {
6404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        if (blend != mBlend) {
6504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck            mBlend = blend;
6604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck            return true;
6704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        }
6804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        return false;
6904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
7004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
7100eb43dbc04083eab85fbb1a9589e2548f2004edsergeyv    ANDROID_API void setSurfaceTexture(const sp<GLConsumer>& texture) {
7204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        if (texture.get() != mSurfaceTexture.get()) {
7345ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel            mSurfaceTexture = texture;
7448f650cb24e5b028deaff01baddc1d154f78d91aChris Craik
7548f650cb24e5b028deaff01baddc1d154f78d91aChris Craik            GLenum target = texture->getCurrentTextureTarget();
7648f650cb24e5b028deaff01baddc1d154f78d91aChris Craik            LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
7748f650cb24e5b028deaff01baddc1d154f78d91aChris Craik                    "set unsupported GLConsumer with target %x", target);
7804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        }
7904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
8004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
8104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    ANDROID_API void updateTexImage() {
8204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        mUpdateTexImage = true;
8304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
8404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
8504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    ANDROID_API void setTransform(const SkMatrix* matrix) {
8604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        delete mTransform;
87d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mTransform = matrix ? new SkMatrix(*matrix) : nullptr;
8804fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
8904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
90417ed6d4337e5409d52f58cc93677c8715193f32John Reck    SkMatrix* getTransform() {
91417ed6d4337e5409d52f58cc93677c8715193f32John Reck        return mTransform;
92417ed6d4337e5409d52f58cc93677c8715193f32John Reck    }
93417ed6d4337e5409d52f58cc93677c8715193f32John Reck
94674554fc36932ca50b15bba41ac6f650254d4e72Derek Sollenberger    ANDROID_API void setPaint(const SkPaint* paint);
9504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
96d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    void apply();
9704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
9812f5e3433226f0a2886a98b0b8da8d5e947c5cdeJohn Reck    Layer* backingLayer() {
9904fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck        return mLayer;
10004fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    }
10104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
102d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    void detachSurfaceTexture();
103918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck
10409df887c9078a34f2bda32c83734b7f75c0a60ffChris Craik    void updateLayer(bool forceFilter, const float* textureTransform);
10556ad6ec42f814e9e61030ff819cac4e5d31def8bDerek Sollenberger
1063e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    void destroyLayer();
1073e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
1083e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    Layer::Api getBackingLayerApi() {
1093e9999bd866fac71c72e6b484a9836c87c328a08sergeyv        return mLayerApi;
1103e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    }
1113e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
11204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reckprivate:
1133e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    RenderState& mRenderState;
1143e9999bd866fac71c72e6b484a9836c87c328a08sergeyv
11504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    // Generic properties
1163e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    int mWidth = 0;
1173e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    int mHeight = 0;
1183e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    bool mBlend = false;
1193e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    SkColorFilter* mColorFilter = nullptr;
1203e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    int mAlpha = 255;
1213e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    SkBlendMode mMode = SkBlendMode::kSrcOver;
12204fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    sp<GLConsumer> mSurfaceTexture;
12304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    SkMatrix* mTransform;
12400eb43dbc04083eab85fbb1a9589e2548f2004edsergeyv    bool mGLContextAttached;
12504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    bool mUpdateTexImage;
12604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
12704fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    Layer* mLayer;
1283e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    Layer::Api mLayerApi;
1293e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    CreateLayerFn mCreateLayerFn;
130d72e0a339b54af0c4e731513bbad120dff694723John Reck
13104fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck    void doUpdateTexImage();
13245ec62ba72c5017fae7d8baab20bfb0d4c99c627Greg Daniel    void doUpdateVkTexImage();
13304fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck};
13404fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck
13504fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck} /* namespace uirenderer */
13604fc583c3dd3144bc6b718fcac4b3e1afdfdb067John Reck} /* namespace android */
137