John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 16 | #include "renderstate/RenderState.h" |
| 17 | #include <GpuMemoryTracker.h> |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 18 | #include "DeferredLayerUpdater.h" |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 19 | #include "Snapshot.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 20 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 21 | #include "renderthread/CanvasContext.h" |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 22 | #include "renderthread/EglManager.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 23 | #include "utils/GLUtils.h" |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 24 | |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 27 | #include <ui/ColorSpace.h> |
| 28 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 32 | RenderState::RenderState(renderthread::RenderThread& thread) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 34 | mThreadId = pthread_self(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | RenderState::~RenderState() { |
| 38 | } |
| 39 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 40 | void RenderState::onContextCreated() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 41 | GpuMemoryTracker::onGpuContextCreated(); |
| 42 | } |
| 43 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 44 | void RenderState::onContextDestroyed() { |
Derek Sollenberger | 7a4216b | 2017-10-25 09:52:23 -0400 | [diff] [blame] | 45 | destroyLayersInUpdater(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 46 | GpuMemoryTracker::onGpuContextDestroyed(); |
| 47 | } |
| 48 | |
| 49 | GrContext* RenderState::getGrContext() const { |
| 50 | return mRenderThread.getGrContext(); |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 51 | } |
| 52 | |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 53 | void RenderState::onBitmapDestroyed(uint32_t pixelRefId) { |
Mike Reed | 8cafcc6 | 2018-05-03 11:32:46 -0400 | [diff] [blame] | 54 | // DEAD CODE |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 57 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 58 | mViewportWidth = width; |
| 59 | mViewportHeight = height; |
| 60 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 61 | } |
| 62 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 63 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 64 | *outWidth = mViewportWidth; |
| 65 | *outHeight = mViewportHeight; |
| 66 | } |
| 67 | |
| 68 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 69 | if (mFramebuffer != fbo) { |
| 70 | mFramebuffer = fbo; |
| 71 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 72 | } |
| 73 | } |
| 74 | |
John Reck | 0b8d067 | 2016-01-29 14:18:22 -0800 | [diff] [blame] | 75 | GLuint RenderState::createFramebuffer() { |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 76 | GLuint ret; |
| 77 | glGenFramebuffers(1, &ret); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | void RenderState::deleteFramebuffer(GLuint fbo) { |
| 82 | if (mFramebuffer == fbo) { |
| 83 | // GL defines that deleting the currently bound FBO rebinds FBO 0. |
| 84 | // Reflect this in our cached value. |
| 85 | mFramebuffer = 0; |
| 86 | } |
| 87 | glDeleteFramebuffers(1, &fbo); |
| 88 | } |
| 89 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 90 | void RenderState::debugOverdraw(bool enable, bool clear) { |
Mike Reed | e7f688b | 2018-05-04 15:21:43 -0400 | [diff] [blame] | 91 | // DEAD CODE |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 94 | static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) { |
| 95 | layerUpdater->destroyLayer(); |
| 96 | } |
| 97 | |
| 98 | void RenderState::destroyLayersInUpdater() { |
| 99 | std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater); |
| 100 | } |
| 101 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 102 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 103 | if (pthread_equal(mThreadId, pthread_self())) { |
| 104 | object->decStrong(nullptr); |
| 105 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 106 | mRenderThread.queue().post([object]() { object->decStrong(nullptr); }); |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 107 | } |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | // Render |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 114 | void RenderState::dump() { |
Mike Reed | e7f688b | 2018-05-04 15:21:43 -0400 | [diff] [blame] | 115 | // DEAD CODE |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 118 | renderthread::RenderThread& RenderState::getRenderThread() { |
| 119 | return mRenderThread; |
| 120 | } |
| 121 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 122 | } /* namespace uirenderer */ |
| 123 | } /* namespace android */ |