• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "DeferredLayerUpdater.h"
17 
18 #include "OpenGLRenderer.h"
19 
20 #include "LayerRenderer.h"
21 #include "renderthread/EglManager.h"
22 #include "renderthread/RenderTask.h"
23 
24 namespace android {
25 namespace uirenderer {
26 
DeferredLayerUpdater(Layer * layer)27 DeferredLayerUpdater::DeferredLayerUpdater(Layer* layer)
28         : mSurfaceTexture(nullptr)
29         , mTransform(nullptr)
30         , mNeedsGLContextAttach(false)
31         , mUpdateTexImage(false)
32         , mLayer(layer)
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();
40 }
41 
~DeferredLayerUpdater()42 DeferredLayerUpdater::~DeferredLayerUpdater() {
43     SkSafeUnref(mColorFilter);
44     setTransform(nullptr);
45     mLayer->postDecStrong();
46     mLayer = nullptr;
47 }
48 
setPaint(const SkPaint * paint)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);
54 }
55 
apply()56 void DeferredLayerUpdater::apply() {
57     // These properties are applied the same to both layer types
58     mLayer->setColorFilter(mColorFilter);
59     mLayer->setAlpha(mAlpha, mMode);
60 
61     if (mSurfaceTexture.get()) {
62         if (mNeedsGLContextAttach) {
63             mNeedsGLContextAttach = false;
64             mSurfaceTexture->attachToContext(mLayer->getTextureId());
65         }
66         if (mUpdateTexImage) {
67             mUpdateTexImage = false;
68             doUpdateTexImage();
69         }
70         if (mTransform) {
71             mLayer->getTransform().load(*mTransform);
72             setTransform(nullptr);
73         }
74     }
75 }
76 
doUpdateTexImage()77 void DeferredLayerUpdater::doUpdateTexImage() {
78     if (mSurfaceTexture->updateTexImage() == NO_ERROR) {
79         float transform[16];
80 
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.
86         int dropCounter = 0;
87         while (mSurfaceTexture->updateTexImage() == NO_ERROR) {
88             int64_t newFrameNumber = mSurfaceTexture->getFrameNumber();
89             if (newFrameNumber == frameNumber) break;
90             frameNumber = newFrameNumber;
91             dropCounter++;
92         }
93 
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());
100         }
101 
102         #if DEBUG_RENDERER
103         if (dropCounter > 0) {
104             RENDERER_LOGD("Dropped %d frames on texture layer update", dropCounter);
105         }
106         #endif
107         mSurfaceTexture->getTransformMatrix(transform);
108         GLenum renderTarget = mSurfaceTexture->getCurrentTextureTarget();
109 
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);
115     }
116 }
117 
detachSurfaceTexture()118 void DeferredLayerUpdater::detachSurfaceTexture() {
119     if (mSurfaceTexture.get()) {
120         status_t err = mSurfaceTexture->detachFromContext();
121         if (err != 0) {
122             // TODO: Elevate to fatal exception
123             ALOGE("Failed to detach SurfaceTexture from context %d", err);
124         }
125         mSurfaceTexture = nullptr;
126         mLayer->clearTexture();
127     }
128 }
129 
130 } /* namespace uirenderer */
131 } /* namespace android */
132