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