Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 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 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 17 | #include "Layer.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 18 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 19 | #include "Caches.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 20 | #include "RenderNode.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 21 | #include "renderstate/RenderState.h" |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 22 | #include "utils/TraceUtils.h" |
| 23 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 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()) |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame^] | 35 | Layer::Layer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight) |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 36 | : GpuMemoryTracker(GpuObjectType::Layer) |
| 37 | , state(State::Uncached) |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 38 | , caches(Caches::getInstance()) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 39 | , renderState(renderState) |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame^] | 40 | , texture(caches) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 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... |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 43 | incStrong(nullptr); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 44 | renderTarget = GL_TEXTURE_2D; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 45 | texture.mWidth = layerWidth; |
| 46 | texture.mHeight = layerHeight; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 47 | renderState.registerLayer(this); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 50 | Layer::~Layer() { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 51 | renderState.unregisterLayer(this); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 52 | SkSafeUnref(colorFilter); |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 53 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame^] | 54 | if (texture.mId) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 55 | texture.deleteTexture(); |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 56 | } |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 57 | |
| 58 | delete[] mesh; |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 59 | } |
| 60 | |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 61 | void Layer::onGlContextLost() { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 62 | texture.deleteTexture(); |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 65 | void Layer::setColorFilter(SkColorFilter* filter) { |
| 66 | SkRefCnt_SafeAssign(colorFilter, filter); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Layer::bindTexture() const { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 70 | if (texture.mId) { |
| 71 | caches.textureState().bindTexture(renderTarget, texture.mId); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 75 | void Layer::generateTexture() { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 76 | if (!texture.mId) { |
| 77 | glGenTextures(1, &texture.mId); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | void Layer::clearTexture() { |
John Reck | db00917 | 2016-03-17 11:02:07 -0700 | [diff] [blame] | 82 | // 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 Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 88 | texture.mId = 0; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 91 | void Layer::postDecStrong() { |
| 92 | renderState.postDecStrong(this); |
| 93 | } |
| 94 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 95 | }; // namespace uirenderer |
| 96 | }; // namespace android |