blob: 8c797d5eebd7db8c5ee6783f4bba1bc0349d17b0 [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);
Chet Haase603f6de2012-09-14 15:31:25 -070044 renderTarget = GL_TEXTURE_2D;
John Reck38e0c322015-11-10 12:19:17 -080045 texture.mWidth = layerWidth;
46 texture.mHeight = layerHeight;
John Reck0e89e2b2014-10-31 14:49:06 -070047 renderState.registerLayer(this);
Chet Haase603f6de2012-09-14 15:31:25 -070048}
49
Chet Haased15ebf22012-09-05 11:40:29 -070050Layer::~Layer() {
John Reck0e89e2b2014-10-31 14:49:06 -070051 renderState.unregisterLayer(this);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050052 SkSafeUnref(colorFilter);
John Reck57998012015-01-29 10:17:57 -080053
Chris Craik5e00c7c2016-07-06 16:10:09 -070054 if (texture.mId) {
John Reck38e0c322015-11-10 12:19:17 -080055 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080056 }
Romain Guy96885eb2013-03-26 15:05:58 -070057
58 delete[] mesh;
Romain Guy97dc9172012-09-23 17:46:45 -070059}
60
John Reck57998012015-01-29 10:17:57 -080061void Layer::onGlContextLost() {
John Reck38e0c322015-11-10 12:19:17 -080062 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080063}
64
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050065void Layer::setColorFilter(SkColorFilter* filter) {
66 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -070067}
68
69void Layer::bindTexture() const {
John Reck38e0c322015-11-10 12:19:17 -080070 if (texture.mId) {
71 caches.textureState().bindTexture(renderTarget, texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070072 }
73}
74
Romain Guy8aa195d2013-06-04 18:00:09 -070075void Layer::generateTexture() {
John Reck38e0c322015-11-10 12:19:17 -080076 if (!texture.mId) {
77 glGenTextures(1, &texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070078 }
79}
80
81void Layer::clearTexture() {
John Reckdb009172016-03-17 11:02:07 -070082 // There's a rare possibility that Caches could have been destroyed already
83 // since this method is queued up as a task.
84 // Since this is a reset method, treat this as non-fatal.
85 if (caches.isInitialized()) {
86 caches.textureState().unbindTexture(texture.mId);
87 }
John Reck38e0c322015-11-10 12:19:17 -080088 texture.mId = 0;
Romain Guy8aa195d2013-06-04 18:00:09 -070089}
90
John Reck0e89e2b2014-10-31 14:49:06 -070091void Layer::postDecStrong() {
92 renderState.postDecStrong(this);
93}
94
Chet Haased15ebf22012-09-05 11:40:29 -070095}; // namespace uirenderer
96}; // namespace android