blob: 88817efa80acb29eee0d6a48a24c40b88479779f [file] [log] [blame]
Chet Haased15ebf22012-09-05 11:40:29 -07001/*
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
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "Layer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070018
John Reck113e0822014-03-18 09:22:59 -070019#include "Caches.h"
John Reck113e0822014-03-18 09:22:59 -070020#include "RenderNode.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "renderstate/RenderState.h"
Chris Craik70850ea2014-11-18 10:49:23 -080022#include "utils/TraceUtils.h"
23
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include <utils/Log.h>
25
Chris Craik70850ea2014-11-18 10:49:23 -080026#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())
Chet Haased15ebf22012-09-05 11:40:29 -070031
32namespace android {
33namespace uirenderer {
34
Chris Craik5e00c7c2016-07-06 16:10:09 -070035Layer::Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight)
John Reck38e0c322015-11-10 12:19:17 -080036 : GpuMemoryTracker(GpuObjectType::Layer)
37 , state(State::Uncached)
Chris Craikbfd1cd62014-09-10 13:04:31 -070038 , caches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070039 , renderState(renderState)
Chris Craik5e00c7c2016-07-06 16:10:09 -070040 , texture(caches) {
John Reck0e89e2b2014-10-31 14:49:06 -070041 // 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...
Chris Craikd41c4d82015-01-05 15:51:13 -080043 incStrong(nullptr);
John Reck38e0c322015-11-10 12:19:17 -080044 texture.mWidth = layerWidth;
45 texture.mHeight = layerHeight;
John Reck0e89e2b2014-10-31 14:49:06 -070046 renderState.registerLayer(this);
Chet Haase603f6de2012-09-14 15:31:25 -070047}
48
Chet Haased15ebf22012-09-05 11:40:29 -070049Layer::~Layer() {
John Reck0e89e2b2014-10-31 14:49:06 -070050 renderState.unregisterLayer(this);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050051 SkSafeUnref(colorFilter);
John Reck57998012015-01-29 10:17:57 -080052
Chris Craik5e00c7c2016-07-06 16:10:09 -070053 if (texture.mId) {
John Reck38e0c322015-11-10 12:19:17 -080054 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080055 }
Romain Guy97dc9172012-09-23 17:46:45 -070056}
57
John Reck57998012015-01-29 10:17:57 -080058void Layer::onGlContextLost() {
John Reck38e0c322015-11-10 12:19:17 -080059 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080060}
61
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050062void Layer::setColorFilter(SkColorFilter* filter) {
63 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -070064}
65
66void Layer::bindTexture() const {
John Reck38e0c322015-11-10 12:19:17 -080067 if (texture.mId) {
sergeyv2a38c422016-10-25 15:21:50 -070068 caches.textureState().bindTexture(texture.target(), texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070069 }
70}
71
Romain Guy8aa195d2013-06-04 18:00:09 -070072void Layer::generateTexture() {
John Reck38e0c322015-11-10 12:19:17 -080073 if (!texture.mId) {
74 glGenTextures(1, &texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070075 }
76}
77
78void Layer::clearTexture() {
John Reckdb009172016-03-17 11:02:07 -070079 // 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 }
John Reck38e0c322015-11-10 12:19:17 -080085 texture.mId = 0;
Romain Guy8aa195d2013-06-04 18:00:09 -070086}
87
John Reck0e89e2b2014-10-31 14:49:06 -070088void Layer::postDecStrong() {
89 renderState.postDecStrong(this);
90}
91
Chet Haased15ebf22012-09-05 11:40:29 -070092}; // namespace uirenderer
93}; // namespace android