Layer.cpp revision 2a38c42e921451abebb4ee5f5ecd738f1b6b04ed
1/*
2 * Copyright (C) 2012 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 "Layer.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", \
28            label, \
29            (renderNode.get() != NULL) ? renderNode->getName() : "", \
30            getWidth(), getHeight())
31
32namespace android {
33namespace uirenderer {
34
35Layer::Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight)
36        : GpuMemoryTracker(GpuObjectType::Layer)
37        , state(State::Uncached)
38        , caches(Caches::getInstance())
39        , renderState(renderState)
40        , texture(caches) {
41    // TODO: This is a violation of Android's typical ref counting, but it
42    // preserves the old inc/dec ref locations. This should be changed...
43    incStrong(nullptr);
44    texture.mWidth = layerWidth;
45    texture.mHeight = layerHeight;
46    renderState.registerLayer(this);
47}
48
49Layer::~Layer() {
50    renderState.unregisterLayer(this);
51    SkSafeUnref(colorFilter);
52
53    if (texture.mId) {
54        texture.deleteTexture();
55    }
56}
57
58void Layer::onGlContextLost() {
59    texture.deleteTexture();
60}
61
62void Layer::setColorFilter(SkColorFilter* filter) {
63    SkRefCnt_SafeAssign(colorFilter, filter);
64}
65
66void Layer::bindTexture() const {
67    if (texture.mId) {
68        caches.textureState().bindTexture(texture.target(), texture.mId);
69    }
70}
71
72void Layer::generateTexture() {
73    if (!texture.mId) {
74        glGenTextures(1, &texture.mId);
75    }
76}
77
78void Layer::clearTexture() {
79    // There's a rare possibility that Caches could have been destroyed already
80    // since this method is queued up as a task.
81    // Since this is a reset method, treat this as non-fatal.
82    if (caches.isInitialized()) {
83        caches.textureState().unbindTexture(texture.mId);
84    }
85    texture.mId = 0;
86}
87
88void Layer::postDecStrong() {
89    renderState.postDecStrong(this);
90}
91
92}; // namespace uirenderer
93}; // namespace android
94