1 /*
2 * Copyright (C) 2017 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
17 #include "GlLayer.h"
18
19 #include "Caches.h"
20 #include "RenderNode.h"
21 #include "renderstate/RenderState.h"
22 #include "utils/TraceUtils.h"
23
24 #include <utils/Log.h>
25
26 #define ATRACE_LAYER_WORK(label) \
27 ATRACE_FORMAT("%s HW Layer DisplayList %s %ux%u", label, \
28 (renderNode.get() != NULL) ? renderNode->getName() : "", getWidth(), \
29 getHeight())
30
31 namespace android {
32 namespace uirenderer {
33
GlLayer(RenderState & renderState,uint32_t layerWidth,uint32_t layerHeight,sk_sp<SkColorFilter> colorFilter,int alpha,SkBlendMode mode,bool blend)34 GlLayer::GlLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
35 sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode, bool blend)
36 : Layer(renderState, Api::OpenGL, colorFilter, alpha, mode)
37 , caches(Caches::getInstance())
38 , texture(caches) {
39 texture.mWidth = layerWidth;
40 texture.mHeight = layerHeight;
41 texture.blend = blend;
42 }
43
~GlLayer()44 GlLayer::~GlLayer() {
45 // There's a rare possibility that Caches could have been destroyed already
46 // since this method is queued up as a task.
47 // Since this is a reset method, treat this as non-fatal.
48 if (caches.isInitialized() && texture.mId) {
49 texture.deleteTexture();
50 }
51 }
52
onGlContextLost()53 void GlLayer::onGlContextLost() {
54 texture.deleteTexture();
55 }
56
setRenderTarget(GLenum renderTarget)57 void GlLayer::setRenderTarget(GLenum renderTarget) {
58 if (renderTarget != getRenderTarget()) {
59 // new render target: bind with new target, and update filter/wrap
60 texture.mTarget = renderTarget;
61 if (texture.mId) {
62 caches.textureState().bindTexture(texture.target(), texture.mId);
63 }
64 texture.setFilter(GL_NEAREST, false, true);
65 texture.setWrap(GL_CLAMP_TO_EDGE, false, true);
66 }
67 }
68
generateTexture()69 void GlLayer::generateTexture() {
70 if (!texture.mId) {
71 glGenTextures(1, &texture.mId);
72 }
73 }
74
75 }; // namespace uirenderer
76 }; // namespace android
77