blob: 5e33353c3ac6bea53045e68bd5f3d1c4b0048567 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
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 Reck1bcacfd2017-11-03 10:12:19 -070016#include "renderstate/RenderState.h"
17#include <GpuMemoryTracker.h>
sergeyv3e9999b2017-01-19 15:37:02 -080018#include "DeferredLayerUpdater.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050019#include "GlLayer.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050020#include "VkLayer.h"
John Reck3b202512014-06-23 13:13:08 -070021
John Reck443a7142014-09-04 17:40:05 -070022#include "renderthread/CanvasContext.h"
John Reck0e89e2b2014-10-31 14:49:06 -070023#include "renderthread/EglManager.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080024#include "utils/GLUtils.h"
Romain Guycaaaa662017-03-27 00:40:21 -070025
Chris Craik9db58c02015-08-19 15:19:18 -070026#include <algorithm>
27
Romain Guycaaaa662017-03-27 00:40:21 -070028#include <ui/ColorSpace.h>
29
John Reck3b202512014-06-23 13:13:08 -070030namespace android {
31namespace uirenderer {
32
John Reck0e89e2b2014-10-31 14:49:06 -070033RenderState::RenderState(renderthread::RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070034 : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070035 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070036}
37
38RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080039 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070040 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070041}
42
43void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080044 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070045 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050046 GpuMemoryTracker::onGpuContextCreated();
John Reck38e0c322015-11-10 12:19:17 -080047
Chris Craik44eb2c02015-01-29 09:45:09 -080048 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080049 mMeshState = new MeshState();
50 mScissor = new Scissor();
51 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080052
John Reck642ebea2017-07-17 09:55:02 -070053 // Deferred because creation needs GL context for texture limits
54 if (!mLayerPool) {
55 mLayerPool = new OffscreenBufferPool();
56 }
57
Chris Craik96a5c4c2015-01-27 15:46:35 -080058 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080059 if (!mCaches) {
60 mCaches = &Caches::createInstance(*this);
61 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080062 mCaches->init();
John Reck3b202512014-06-23 13:13:08 -070063}
64
John Reck49bc4ac2015-01-29 12:53:38 -080065static void layerLostGlContext(Layer* layer) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050066 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
John Reck1bcacfd2017-11-03 10:12:19 -070067 "layerLostGlContext on non GL layer");
Greg Daniel8cd3edf2017-01-09 14:15:41 -050068 static_cast<GlLayer*>(layer)->onGlContextLost();
John Reck49bc4ac2015-01-29 12:53:38 -080069}
70
Chris Craik1d477422014-08-26 17:30:15 -070071void RenderState::onGLContextDestroyed() {
John Reck642ebea2017-07-17 09:55:02 -070072 mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -080073
Chris Craik96a5c4c2015-01-27 15:46:35 -080074 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080075 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
Chris Craik96a5c4c2015-01-27 15:46:35 -080076
Chris Craik44eb2c02015-01-29 09:45:09 -080077 mCaches->terminate();
78
79 delete mBlend;
80 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080081 delete mMeshState;
82 mMeshState = nullptr;
83 delete mScissor;
84 mScissor = nullptr;
85 delete mStencil;
86 mStencil = nullptr;
John Reck38e0c322015-11-10 12:19:17 -080087
sergeyvc3f13162017-02-06 11:45:14 -080088 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -050089 GpuMemoryTracker::onGpuContextDestroyed();
90}
91
92void RenderState::onVkContextCreated() {
93 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070094 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050095 GpuMemoryTracker::onGpuContextCreated();
96}
97
98static void layerDestroyedVkContext(Layer* layer) {
99 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
100 "layerLostVkContext on non Vulkan layer");
101 static_cast<VkLayer*>(layer)->onVkContextDestroyed();
102}
103
104void RenderState::onVkContextDestroyed() {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500105 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
Derek Sollenberger7a4216b2017-10-25 09:52:23 -0400106 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500107 GpuMemoryTracker::onGpuContextDestroyed();
108}
109
110GrContext* RenderState::getGrContext() const {
111 return mRenderThread.getGrContext();
Chris Craik1d477422014-08-26 17:30:15 -0700112}
113
Chris Craik9fded232015-11-11 16:42:34 -0800114void RenderState::flush(Caches::FlushMode mode) {
115 switch (mode) {
116 case Caches::FlushMode::Full:
John Reck1bcacfd2017-11-03 10:12:19 -0700117 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800118 case Caches::FlushMode::Moderate:
John Reck1bcacfd2017-11-03 10:12:19 -0700119 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800120 case Caches::FlushMode::Layers:
John Reck642ebea2017-07-17 09:55:02 -0700121 if (mLayerPool) mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -0800122 break;
123 }
John Reck642ebea2017-07-17 09:55:02 -0700124 if (mCaches) mCaches->flush(mode);
Chris Craik9fded232015-11-11 16:42:34 -0800125}
126
John Reck9a814872017-05-22 15:04:21 -0700127void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
John Reck36393c32017-05-23 15:32:08 -0700128 if (mCaches && mCaches->textureCache.destroyTexture(pixelRefId)) {
John Reck9a814872017-05-22 15:04:21 -0700129 glFlush();
130 GL_CHECKPOINT(MODERATE);
131 }
132}
133
John Reck3b202512014-06-23 13:13:08 -0700134void RenderState::setViewport(GLsizei width, GLsizei height) {
135 mViewportWidth = width;
136 mViewportHeight = height;
137 glViewport(0, 0, mViewportWidth, mViewportHeight);
138}
139
John Reck3b202512014-06-23 13:13:08 -0700140void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
141 *outWidth = mViewportWidth;
142 *outHeight = mViewportHeight;
143}
144
145void RenderState::bindFramebuffer(GLuint fbo) {
146 if (mFramebuffer != fbo) {
147 mFramebuffer = fbo;
148 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
149 }
150}
151
John Reck0b8d0672016-01-29 14:18:22 -0800152GLuint RenderState::createFramebuffer() {
Chris Craik818c9fb2015-10-23 14:33:42 -0700153 GLuint ret;
154 glGenFramebuffers(1, &ret);
155 return ret;
156}
157
158void RenderState::deleteFramebuffer(GLuint fbo) {
159 if (mFramebuffer == fbo) {
160 // GL defines that deleting the currently bound FBO rebinds FBO 0.
161 // Reflect this in our cached value.
162 mFramebuffer = 0;
163 }
164 glDeleteFramebuffers(1, &fbo);
165}
166
John Reck3b202512014-06-23 13:13:08 -0700167void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
John Reck95cd24b2015-08-04 11:17:39 -0700168 if (mode == DrawGlInfo::kModeProcessNoContext) {
169 // If there's no context we don't need to interrupt as there's
170 // no gl state to save/restore
171 (*functor)(mode, info);
172 } else {
173 interruptForFunctorInvoke();
174 (*functor)(mode, info);
175 resumeFromFunctorInvoke();
176 }
John Reck3b202512014-06-23 13:13:08 -0700177}
178
179void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800180 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800181 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800182 meshState().unbindMeshBuffer();
183 meshState().unbindIndicesBuffer();
184 meshState().resetVertexPointers();
185 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700186 debugOverdraw(false, false);
Romain Guy253f2c22016-09-28 17:34:42 -0700187 // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
John Reck1bcacfd2017-11-03 10:12:19 -0700188 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700189 glDisable(GL_FRAMEBUFFER_SRGB_EXT);
190 }
John Reck3b202512014-06-23 13:13:08 -0700191}
192
193void RenderState::resumeFromFunctorInvoke() {
John Reck1bcacfd2017-11-03 10:12:19 -0700194 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700195 glEnable(GL_FRAMEBUFFER_SRGB_EXT);
196 }
197
John Reck3b202512014-06-23 13:13:08 -0700198 glViewport(0, 0, mViewportWidth, mViewportHeight);
199 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
200 debugOverdraw(false, false);
201
202 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
203
Chris Craik65fe5ee2015-01-26 18:06:29 -0800204 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800205 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700206
Chris Craik44eb2c02015-01-29 09:45:09 -0800207 mCaches->textureState().activateTexture(0);
208 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700209}
210
211void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700212 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700213 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800214 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800215 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700216 }
217 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800218 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700219 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800220 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700221 }
222 }
223}
224
sergeyv3e9999b2017-01-19 15:37:02 -0800225static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) {
226 layerUpdater->destroyLayer();
227}
228
229void RenderState::destroyLayersInUpdater() {
230 std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater);
231}
232
John Reck0e89e2b2014-10-31 14:49:06 -0700233void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800234 if (pthread_equal(mThreadId, pthread_self())) {
235 object->decStrong(nullptr);
236 } else {
John Reckf8441e62017-10-23 13:10:41 -0700237 mRenderThread.queue().post([object]() { object->decStrong(nullptr); });
John Recka55b5d62016-01-14 15:09:10 -0800238 }
John Reck0e89e2b2014-10-31 14:49:06 -0700239}
240
Chris Craik6c15ffa2015-02-02 13:50:55 -0800241///////////////////////////////////////////////////////////////////////////////
242// Render
243///////////////////////////////////////////////////////////////////////////////
244
Arun530a2b42017-01-23 12:47:57 +0000245void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700246 bool overrideDisableBlending) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800247 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800248 const Glop::Mesh::Vertices& vertices = mesh.vertices;
249 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c812015-02-11 13:17:06 -0800250 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800251
John Reck975591a2016-01-22 16:28:07 -0800252 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800253
Chris Craik0519c812015-02-11 13:17:06 -0800254 // ---------------------------------------------
255 // ---------- Program + uniform setup ----------
256 // ---------------------------------------------
257 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800258
Chris Craik0519c812015-02-11 13:17:06 -0800259 if (fill.colorEnabled) {
260 fill.program->setColor(fill.color);
261 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800262
John Reck1bcacfd2017-11-03 10:12:19 -0700263 fill.program->set(orthoMatrix, glop.transform.modelView, glop.transform.meshTransform(),
264 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800265
Chris Craik0519c812015-02-11 13:17:06 -0800266 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700267 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800268 const FloatColor& color = fill.filter.color;
John Reck1bcacfd2017-11-03 10:12:19 -0700269 glUniform4f(mCaches->program().getUniform("colorBlend"), color.r, color.g, color.b,
270 color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700271 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800272 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700273 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800274 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
John Reck1bcacfd2017-11-03 10:12:19 -0700275 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800276 }
277
Chris Craik0519c812015-02-11 13:17:06 -0800278 // Round rect clipping uniforms
279 if (glop.roundRectClipState) {
280 // TODO: avoid query, and cache values (or RRCS ptr) in program
281 const RoundRectClipState* state = glop.roundRectClipState;
282 const Rect& innerRect = state->innerRect;
Chris Craik0519c812015-02-11 13:17:06 -0800283
284 // add half pixel to round out integer rect space to cover pixel centers
285 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000286
287 // Divide by the radius to simplify the calculations in the fragment shader
288 // roundRectPos is also passed from vertex shader relative to top/left & radius
289 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
John Reck1bcacfd2017-11-03 10:12:19 -0700290 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
291 (innerRect.right - innerRect.left) / roundedOutRadius,
292 (innerRect.bottom - innerRect.top) / roundedOutRadius);
Arun06e9f322017-01-23 11:59:21 +0000293
John Reck1bcacfd2017-11-03 10:12:19 -0700294 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), 1, GL_FALSE,
295 &state->matrix.data[0]);
Arun06e9f322017-01-23 11:59:21 +0000296
John Reck1bcacfd2017-11-03 10:12:19 -0700297 glUniform1f(fill.program->getUniform("roundRectRadius"), roundedOutRadius);
Chris Craik0519c812015-02-11 13:17:06 -0800298 }
Chris Craikef250742015-02-25 17:16:16 -0800299
John Reck975591a2016-01-22 16:28:07 -0800300 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800301
Chris Craik117bdbc2015-02-05 10:12:38 -0800302 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800303 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800304 // --------------------------------
305 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800306 meshState().bindMeshBuffer(vertices.bufferObject);
307 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800308
309 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800310 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800311
Chris Craik138c21f2016-04-28 16:59:42 -0700312 // texture
313 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800314 const Glop::Fill::TextureData& texture = fill.texture;
315 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c812015-02-11 13:17:06 -0800316 mCaches->textureState().activateTexture(0);
317
sergeyv2a38c422016-10-25 15:21:50 -0700318 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800319 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700320 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800321 }
Chris Craik26bf3422015-02-26 16:28:17 -0800322 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700323 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800324 }
Chris Craik0519c812015-02-11 13:17:06 -0800325
Chris Craik26bf3422015-02-26 16:28:17 -0800326 if (texture.textureTransform) {
John Reck1bcacfd2017-11-03 10:12:19 -0700327 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1, GL_FALSE,
328 &texture.textureTransform->data[0]);
Chris Craik26bf3422015-02-26 16:28:17 -0800329 }
Chris Craik138c21f2016-04-28 16:59:42 -0700330 }
331
332 // vertex attributes (tex coord, color, alpha)
333 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
334 meshState().enableTexCoordsVertexArray();
335 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800336 } else {
337 meshState().disableTexCoordsVertexArray();
338 }
Chris Craikef250742015-02-25 17:16:16 -0800339 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700340 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800341 colorLocation = fill.program->getAttrib("colors");
342 glEnableVertexAttribArray(colorLocation);
John Reck1bcacfd2017-11-03 10:12:19 -0700343 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride,
344 vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800345 }
Chris Craikef250742015-02-25 17:16:16 -0800346 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700347 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800348 // NOTE: alpha vertex position is computed assuming no VBO
John Reck1bcacfd2017-11-03 10:12:19 -0700349 const void* alphaCoords = ((const GLbyte*)vertices.position) + kVertexAlphaOffset;
Chris Craikef250742015-02-25 17:16:16 -0800350 alphaLocation = fill.program->getAttrib("vtxAlpha");
351 glEnableVertexAttribArray(alphaLocation);
352 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800353 }
Chris Craik922d3a72015-02-13 17:47:21 -0800354 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700355 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800356
John Reck975591a2016-01-22 16:28:07 -0800357 GL_CHECKPOINT(MODERATE);
John Reck1bcacfd2017-11-03 10:12:19 -0700358 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType)
359 ? fill.skiaShaderData.bitmapData.bitmapTexture
360 : nullptr;
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900361 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800362
Romain Guycaaaa662017-03-27 00:40:21 -0700363 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
364 // which means the color space conversion applies to the shader's bitmap
365 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
366 if (colorSpaceTexture != nullptr) {
367 if (colorSpaceTexture->hasColorSpaceConversion()) {
368 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
John Reck1bcacfd2017-11-03 10:12:19 -0700369 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1, GL_FALSE,
370 connector->getTransform().asArray());
Romain Guycaaaa662017-03-27 00:40:21 -0700371 }
372
373 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
374 if (transferFunction != TransferFunctionType::None) {
375 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
376 const ColorSpace& source = connector->getSource();
377
378 switch (transferFunction) {
379 case TransferFunctionType::None:
380 break;
381 case TransferFunctionType::Full:
382 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
John Reck1bcacfd2017-11-03 10:12:19 -0700383 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700384 break;
385 case TransferFunctionType::Limited:
386 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
John Reck1bcacfd2017-11-03 10:12:19 -0700387 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700388 break;
389 case TransferFunctionType::Gamma:
390 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
John Reck1bcacfd2017-11-03 10:12:19 -0700391 source.getTransferParameters().g);
Romain Guycaaaa662017-03-27 00:40:21 -0700392 break;
393 }
394 }
395 }
396
Chris Craik117bdbc2015-02-05 10:12:38 -0800397 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800398 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800399 // ------------------------------------
Arun530a2b42017-01-23 12:47:57 +0000400 if (CC_UNLIKELY(overrideDisableBlending)) {
401 blend().setFactors(GL_ZERO, GL_ZERO);
402 } else {
403 blend().setFactors(glop.blend.src, glop.blend.dst);
404 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800405
John Reck975591a2016-01-22 16:28:07 -0800406 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800407
Chris Craik117bdbc2015-02-05 10:12:38 -0800408 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800409 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800410 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800411 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800412 // Since the indexed quad list is of limited length, we loop over
413 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c812015-02-11 13:17:06 -0800414 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800415 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800416 while (elementsCount > 0) {
John Reck1bcacfd2017-11-03 10:12:19 -0700417 GLsizei drawCount = std::min(elementsCount, (GLsizei)kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000418 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800419 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700420 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
John Reck1bcacfd2017-11-03 10:12:19 -0700421 meshState().bindTexCoordsVertexPointer(vertexData + kMeshTextureOffset,
422 vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800423 }
424
Arunb0a94772017-01-23 11:41:06 +0000425 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700426 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount - 1, drawCount,
427 GL_UNSIGNED_SHORT, nullptr);
Arunb0a94772017-01-23 11:41:06 +0000428 } else {
429 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
430 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800431 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000432 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800433 }
Chris Craikef250742015-02-25 17:16:16 -0800434 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000435 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700436 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine
437 // the min/max index values)
438 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount - 1, mesh.elementCount,
439 GL_UNSIGNED_SHORT, indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000440 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700441 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT,
442 indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000443 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800444 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800445 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800446 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800447
John Reck975591a2016-01-22 16:28:07 -0800448 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800449
Chris Craik2ab95d72015-02-06 15:25:51 -0800450 // -----------------------------------
451 // ---------- Mesh teardown ----------
452 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700453 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800454 glDisableVertexAttribArray(alphaLocation);
455 }
Chris Craik53e51e42015-06-01 10:35:35 -0700456 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800457 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800458 }
John Reck9372ac32016-01-19 11:46:52 -0800459
John Reck975591a2016-01-22 16:28:07 -0800460 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800461}
462
463void RenderState::dump() {
464 blend().dump();
465 meshState().dump();
466 scissor().dump();
467 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800468}
469
John Reck3b202512014-06-23 13:13:08 -0700470} /* namespace uirenderer */
471} /* namespace android */