2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "DeferredLayerUpdater.h"
18 #include "OpenGLRenderer.h"
20 #include "LayerRenderer.h"
21 #include "renderthread/EglManager.h"
22 #include "renderthread/RenderTask.h"
25 namespace uirenderer {
27 DeferredLayerUpdater::DeferredLayerUpdater(Layer* layer)
28 : mSurfaceTexture(nullptr)
30 , mNeedsGLContextAttach(false)
31 , mUpdateTexImage(false)
33 , mCaches(Caches::getInstance()) {
34 mWidth = mLayer->layer.getWidth();
35 mHeight = mLayer->layer.getHeight();
36 mBlend = mLayer->isBlend();
37 mColorFilter = SkSafeRef(mLayer->getColorFilter());
38 mAlpha = mLayer->getAlpha();
39 mMode = mLayer->getMode();
42 DeferredLayerUpdater::~DeferredLayerUpdater() {
43 SkSafeUnref(mColorFilter);
44 setTransform(nullptr);
45 mLayer->postDecStrong();
49 void DeferredLayerUpdater::setPaint(const SkPaint* paint) {
50 mAlpha = PaintUtils::getAlphaDirect(paint);
51 mMode = PaintUtils::getXfermodeDirect(paint);
52 SkColorFilter* colorFilter = (paint) ? paint->getColorFilter() : nullptr;
53 SkRefCnt_SafeAssign(mColorFilter, colorFilter);
56 void DeferredLayerUpdater::apply() {
57 // These properties are applied the same to both layer types
58 mLayer->setColorFilter(mColorFilter);
59 mLayer->setAlpha(mAlpha, mMode);
61 if (mSurfaceTexture.get()) {
62 if (mNeedsGLContextAttach) {
63 mNeedsGLContextAttach = false;
64 mSurfaceTexture->attachToContext(mLayer->getTextureId());
66 if (mUpdateTexImage) {
67 mUpdateTexImage = false;
71 mLayer->getTransform().load(*mTransform);
72 setTransform(nullptr);
77 void DeferredLayerUpdater::doUpdateTexImage() {
78 if (mSurfaceTexture->updateTexImage() == NO_ERROR) {
81 int64_t frameNumber = mSurfaceTexture->getFrameNumber();
82 // If the GLConsumer queue is in synchronous mode, need to discard all
83 // but latest frame, using the frame number to tell when we no longer
84 // have newer frames to target. Since we can't tell which mode it is in,
85 // do this unconditionally.
87 while (mSurfaceTexture->updateTexImage() == NO_ERROR) {
88 int64_t newFrameNumber = mSurfaceTexture->getFrameNumber();
89 if (newFrameNumber == frameNumber) break;
90 frameNumber = newFrameNumber;
94 bool forceFilter = false;
95 sp<GraphicBuffer> buffer = mSurfaceTexture->getCurrentBuffer();
96 if (buffer != nullptr) {
97 // force filtration if buffer size != layer size
98 forceFilter = mWidth != static_cast<int>(buffer->getWidth())
99 || mHeight != static_cast<int>(buffer->getHeight());
103 if (dropCounter > 0) {
104 RENDERER_LOGD("Dropped %d frames on texture layer update", dropCounter);
107 mSurfaceTexture->getTransformMatrix(transform);
108 GLenum renderTarget = mSurfaceTexture->getCurrentTextureTarget();
110 LOG_ALWAYS_FATAL_IF(renderTarget != GL_TEXTURE_2D && renderTarget != GL_TEXTURE_EXTERNAL_OES,
111 "doUpdateTexImage target %x, 2d %x, EXT %x",
112 renderTarget, GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL_OES);
113 LayerRenderer::updateTextureLayer(mLayer, mWidth, mHeight,
114 !mBlend, forceFilter, renderTarget, transform);
118 void DeferredLayerUpdater::detachSurfaceTexture() {
119 if (mSurfaceTexture.get()) {
120 status_t err = mSurfaceTexture->detachFromContext();
122 // TODO: Elevate to fatal exception
123 ALOGE("Failed to detach SurfaceTexture from context %d", err);
125 mSurfaceTexture = nullptr;
126 mLayer->clearTexture();
130 } /* namespace uirenderer */
131 } /* namespace android */