blob: 0ceca3368c063f7e2ad8a66b41bb64a6fda8b3d8 [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 */
sergeyv3e9999b2017-01-19 15:37:02 -080016#include "DeferredLayerUpdater.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050017#include "GlLayer.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050018#include "VkLayer.h"
John Reck38e0c322015-11-10 12:19:17 -080019#include <GpuMemoryTracker.h>
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include "renderstate/RenderState.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)
34 : mRenderThread(thread)
John Reck3b202512014-06-23 13:13:08 -070035 , mViewportWidth(0)
36 , mViewportHeight(0)
37 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070038 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070039}
40
41RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080042 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080043 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070044}
45
46void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080047 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080048 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050049 GpuMemoryTracker::onGpuContextCreated();
John Reck38e0c322015-11-10 12:19:17 -080050
Chris Craik44eb2c02015-01-29 09:45:09 -080051 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080052 mMeshState = new MeshState();
53 mScissor = new Scissor();
54 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080055
John Reck642ebea2017-07-17 09:55:02 -070056 // Deferred because creation needs GL context for texture limits
57 if (!mLayerPool) {
58 mLayerPool = new OffscreenBufferPool();
59 }
60
Chris Craik96a5c4c2015-01-27 15:46:35 -080061 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080062 if (!mCaches) {
63 mCaches = &Caches::createInstance(*this);
64 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080065 mCaches->init();
John Reck3b202512014-06-23 13:13:08 -070066}
67
John Reck49bc4ac2015-01-29 12:53:38 -080068static void layerLostGlContext(Layer* layer) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050069 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
70 "layerLostGlContext on non GL layer");
71 static_cast<GlLayer*>(layer)->onGlContextLost();
John Reck49bc4ac2015-01-29 12:53:38 -080072}
73
Chris Craik1d477422014-08-26 17:30:15 -070074void RenderState::onGLContextDestroyed() {
John Reck642ebea2017-07-17 09:55:02 -070075 mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -080076
Chris Craik96a5c4c2015-01-27 15:46:35 -080077 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080078 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
Chris Craik96a5c4c2015-01-27 15:46:35 -080079
Chris Craik44eb2c02015-01-29 09:45:09 -080080 mCaches->terminate();
81
82 delete mBlend;
83 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080084 delete mMeshState;
85 mMeshState = nullptr;
86 delete mScissor;
87 mScissor = nullptr;
88 delete mStencil;
89 mStencil = nullptr;
John Reck38e0c322015-11-10 12:19:17 -080090
sergeyvc3f13162017-02-06 11:45:14 -080091 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -050092 GpuMemoryTracker::onGpuContextDestroyed();
93}
94
95void RenderState::onVkContextCreated() {
96 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
97 "State object lifecycle not managed correctly");
98 GpuMemoryTracker::onGpuContextCreated();
99}
100
101static void layerDestroyedVkContext(Layer* layer) {
102 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
103 "layerLostVkContext on non Vulkan layer");
104 static_cast<VkLayer*>(layer)->onVkContextDestroyed();
105}
106
107void RenderState::onVkContextDestroyed() {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500108 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
Derek Sollenberger7a4216b2017-10-25 09:52:23 -0400109 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500110 GpuMemoryTracker::onGpuContextDestroyed();
111}
112
113GrContext* RenderState::getGrContext() const {
114 return mRenderThread.getGrContext();
Chris Craik1d477422014-08-26 17:30:15 -0700115}
116
Chris Craik9fded232015-11-11 16:42:34 -0800117void RenderState::flush(Caches::FlushMode mode) {
118 switch (mode) {
119 case Caches::FlushMode::Full:
120 // fall through
121 case Caches::FlushMode::Moderate:
122 // fall through
123 case Caches::FlushMode::Layers:
John Reck642ebea2017-07-17 09:55:02 -0700124 if (mLayerPool) mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -0800125 break;
126 }
John Reck642ebea2017-07-17 09:55:02 -0700127 if (mCaches) mCaches->flush(mode);
Chris Craik9fded232015-11-11 16:42:34 -0800128}
129
John Reck9a814872017-05-22 15:04:21 -0700130void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
John Reck36393c32017-05-23 15:32:08 -0700131 if (mCaches && mCaches->textureCache.destroyTexture(pixelRefId)) {
John Reck9a814872017-05-22 15:04:21 -0700132 glFlush();
133 GL_CHECKPOINT(MODERATE);
134 }
135}
136
John Reck3b202512014-06-23 13:13:08 -0700137void RenderState::setViewport(GLsizei width, GLsizei height) {
138 mViewportWidth = width;
139 mViewportHeight = height;
140 glViewport(0, 0, mViewportWidth, mViewportHeight);
141}
142
143
144void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
145 *outWidth = mViewportWidth;
146 *outHeight = mViewportHeight;
147}
148
149void RenderState::bindFramebuffer(GLuint fbo) {
150 if (mFramebuffer != fbo) {
151 mFramebuffer = fbo;
152 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
153 }
154}
155
John Reck0b8d0672016-01-29 14:18:22 -0800156GLuint RenderState::createFramebuffer() {
Chris Craik818c9fb2015-10-23 14:33:42 -0700157 GLuint ret;
158 glGenFramebuffers(1, &ret);
159 return ret;
160}
161
162void RenderState::deleteFramebuffer(GLuint fbo) {
163 if (mFramebuffer == fbo) {
164 // GL defines that deleting the currently bound FBO rebinds FBO 0.
165 // Reflect this in our cached value.
166 mFramebuffer = 0;
167 }
168 glDeleteFramebuffers(1, &fbo);
169}
170
John Reck3b202512014-06-23 13:13:08 -0700171void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
John Reck95cd24b2015-08-04 11:17:39 -0700172 if (mode == DrawGlInfo::kModeProcessNoContext) {
173 // If there's no context we don't need to interrupt as there's
174 // no gl state to save/restore
175 (*functor)(mode, info);
176 } else {
177 interruptForFunctorInvoke();
178 (*functor)(mode, info);
179 resumeFromFunctorInvoke();
180 }
John Reck3b202512014-06-23 13:13:08 -0700181}
182
183void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800184 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800185 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800186 meshState().unbindMeshBuffer();
187 meshState().unbindIndicesBuffer();
188 meshState().resetVertexPointers();
189 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700190 debugOverdraw(false, false);
Romain Guy253f2c22016-09-28 17:34:42 -0700191 // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
Romain Guyefb4b062017-02-27 11:00:04 -0800192 if (mCaches->extensions().hasLinearBlending() &&
193 mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700194 glDisable(GL_FRAMEBUFFER_SRGB_EXT);
195 }
John Reck3b202512014-06-23 13:13:08 -0700196}
197
198void RenderState::resumeFromFunctorInvoke() {
Romain Guyefb4b062017-02-27 11:00:04 -0800199 if (mCaches->extensions().hasLinearBlending() &&
200 mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700201 glEnable(GL_FRAMEBUFFER_SRGB_EXT);
202 }
203
John Reck3b202512014-06-23 13:13:08 -0700204 glViewport(0, 0, mViewportWidth, mViewportHeight);
205 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
206 debugOverdraw(false, false);
207
208 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
209
Chris Craik65fe5ee2015-01-26 18:06:29 -0800210 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800211 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700212
Chris Craik44eb2c02015-01-29 09:45:09 -0800213 mCaches->textureState().activateTexture(0);
214 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700215}
216
217void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700218 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700219 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800220 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800221 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700222 }
223 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800224 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700225 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800226 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700227 }
228 }
229}
230
sergeyv3e9999b2017-01-19 15:37:02 -0800231static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) {
232 layerUpdater->destroyLayer();
233}
234
235void RenderState::destroyLayersInUpdater() {
236 std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater);
237}
238
John Reck0e89e2b2014-10-31 14:49:06 -0700239void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800240 if (pthread_equal(mThreadId, pthread_self())) {
241 object->decStrong(nullptr);
242 } else {
John Reckf8441e62017-10-23 13:10:41 -0700243 mRenderThread.queue().post([object]() { object->decStrong(nullptr); });
John Recka55b5d62016-01-14 15:09:10 -0800244 }
John Reck0e89e2b2014-10-31 14:49:06 -0700245}
246
Chris Craik6c15ffa2015-02-02 13:50:55 -0800247///////////////////////////////////////////////////////////////////////////////
248// Render
249///////////////////////////////////////////////////////////////////////////////
250
Arun530a2b42017-01-23 12:47:57 +0000251void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix,
252 bool overrideDisableBlending) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800253 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800254 const Glop::Mesh::Vertices& vertices = mesh.vertices;
255 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800256 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800257
John Reck975591a2016-01-22 16:28:07 -0800258 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800259
Chris Craik0519c8102015-02-11 13:17:06 -0800260 // ---------------------------------------------
261 // ---------- Program + uniform setup ----------
262 // ---------------------------------------------
263 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800264
Chris Craik0519c8102015-02-11 13:17:06 -0800265 if (fill.colorEnabled) {
266 fill.program->setColor(fill.color);
267 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800268
Chris Craik12efe642015-09-28 15:52:12 -0700269 fill.program->set(orthoMatrix,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800270 glop.transform.modelView,
Chris Craik53e51e42015-06-01 10:35:35 -0700271 glop.transform.meshTransform(),
272 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800273
Chris Craik0519c8102015-02-11 13:17:06 -0800274 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700275 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800276 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800277 glUniform4f(mCaches->program().getUniform("colorBlend"),
278 color.r, color.g, color.b, color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700279 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800280 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800281 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800282 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800283 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800284 }
285
Chris Craik0519c8102015-02-11 13:17:06 -0800286 // Round rect clipping uniforms
287 if (glop.roundRectClipState) {
288 // TODO: avoid query, and cache values (or RRCS ptr) in program
289 const RoundRectClipState* state = glop.roundRectClipState;
290 const Rect& innerRect = state->innerRect;
Chris Craik0519c8102015-02-11 13:17:06 -0800291
292 // add half pixel to round out integer rect space to cover pixel centers
293 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000294
295 // Divide by the radius to simplify the calculations in the fragment shader
296 // roundRectPos is also passed from vertex shader relative to top/left & radius
297 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
298 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
299 (innerRect.right - innerRect.left) / roundedOutRadius,
300 (innerRect.bottom - innerRect.top) / roundedOutRadius);
301
302 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
303 1, GL_FALSE, &state->matrix.data[0]);
304
Chris Craik0519c8102015-02-11 13:17:06 -0800305 glUniform1f(fill.program->getUniform("roundRectRadius"),
306 roundedOutRadius);
307 }
Chris Craikef250742015-02-25 17:16:16 -0800308
John Reck975591a2016-01-22 16:28:07 -0800309 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800310
Chris Craik117bdbc2015-02-05 10:12:38 -0800311 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800312 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800313 // --------------------------------
314 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800315 meshState().bindMeshBuffer(vertices.bufferObject);
316 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800317
318 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800319 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800320
Chris Craik138c21f2016-04-28 16:59:42 -0700321 // texture
322 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800323 const Glop::Fill::TextureData& texture = fill.texture;
324 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800325 mCaches->textureState().activateTexture(0);
326
sergeyv2a38c422016-10-25 15:21:50 -0700327 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800328 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700329 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800330 }
Chris Craik26bf3422015-02-26 16:28:17 -0800331 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700332 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800333 }
Chris Craik0519c8102015-02-11 13:17:06 -0800334
Chris Craik26bf3422015-02-26 16:28:17 -0800335 if (texture.textureTransform) {
336 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
337 GL_FALSE, &texture.textureTransform->data[0]);
338 }
Chris Craik138c21f2016-04-28 16:59:42 -0700339 }
340
341 // vertex attributes (tex coord, color, alpha)
342 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
343 meshState().enableTexCoordsVertexArray();
344 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800345 } else {
346 meshState().disableTexCoordsVertexArray();
347 }
Chris Craikef250742015-02-25 17:16:16 -0800348 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700349 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800350 colorLocation = fill.program->getAttrib("colors");
351 glEnableVertexAttribArray(colorLocation);
352 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800353 }
Chris Craikef250742015-02-25 17:16:16 -0800354 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700355 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800356 // NOTE: alpha vertex position is computed assuming no VBO
357 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
358 alphaLocation = fill.program->getAttrib("vtxAlpha");
359 glEnableVertexAttribArray(alphaLocation);
360 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800361 }
Chris Craik922d3a72015-02-13 17:47:21 -0800362 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700363 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800364
John Reck975591a2016-01-22 16:28:07 -0800365 GL_CHECKPOINT(MODERATE);
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900366 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
367 fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
368 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800369
Romain Guycaaaa662017-03-27 00:40:21 -0700370 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
371 // which means the color space conversion applies to the shader's bitmap
372 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
373 if (colorSpaceTexture != nullptr) {
374 if (colorSpaceTexture->hasColorSpaceConversion()) {
375 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
376 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
377 GL_FALSE, connector->getTransform().asArray());
378 }
379
380 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
381 if (transferFunction != TransferFunctionType::None) {
382 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
383 const ColorSpace& source = connector->getSource();
384
385 switch (transferFunction) {
386 case TransferFunctionType::None:
387 break;
388 case TransferFunctionType::Full:
389 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
390 reinterpret_cast<const float*>(&source.getTransferParameters().g));
391 break;
392 case TransferFunctionType::Limited:
393 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
394 reinterpret_cast<const float*>(&source.getTransferParameters().g));
395 break;
396 case TransferFunctionType::Gamma:
397 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
398 source.getTransferParameters().g);
399 break;
400 }
401 }
402 }
403
Chris Craik117bdbc2015-02-05 10:12:38 -0800404 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800405 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800406 // ------------------------------------
Arun530a2b42017-01-23 12:47:57 +0000407 if (CC_UNLIKELY(overrideDisableBlending)) {
408 blend().setFactors(GL_ZERO, GL_ZERO);
409 } else {
410 blend().setFactors(glop.blend.src, glop.blend.dst);
411 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800412
John Reck975591a2016-01-22 16:28:07 -0800413 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800414
Chris Craik117bdbc2015-02-05 10:12:38 -0800415 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800416 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800417 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800418 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800419 // Since the indexed quad list is of limited length, we loop over
420 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800421 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800422 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800423 while (elementsCount > 0) {
Chris Craik9db58c02015-08-19 15:19:18 -0700424 GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000425 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800426 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700427 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik1b7db402016-02-24 18:25:32 -0800428 meshState().bindTexCoordsVertexPointer(
Chris Craikef250742015-02-25 17:16:16 -0800429 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800430 }
431
Arunb0a94772017-01-23 11:41:06 +0000432 if (mCaches->extensions().getMajorGlVersion() >= 3) {
433 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount-1, drawCount, GL_UNSIGNED_SHORT, nullptr);
434 } else {
435 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
436 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800437 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000438 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800439 }
Chris Craikef250742015-02-25 17:16:16 -0800440 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000441 if (mCaches->extensions().getMajorGlVersion() >= 3) {
442 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine the min/max index values)
443 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount-1, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
444 } else {
445 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
446 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800447 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800448 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800449 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800450
John Reck975591a2016-01-22 16:28:07 -0800451 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800452
Chris Craik2ab95d72015-02-06 15:25:51 -0800453 // -----------------------------------
454 // ---------- Mesh teardown ----------
455 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700456 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800457 glDisableVertexAttribArray(alphaLocation);
458 }
Chris Craik53e51e42015-06-01 10:35:35 -0700459 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800460 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800461 }
John Reck9372ac32016-01-19 11:46:52 -0800462
John Reck975591a2016-01-22 16:28:07 -0800463 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800464}
465
466void RenderState::dump() {
467 blend().dump();
468 meshState().dump();
469 scissor().dump();
470 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800471}
472
John Reck3b202512014-06-23 13:13:08 -0700473} /* namespace uirenderer */
474} /* namespace android */