blob: ed96d49bbc151022bca91ced7d47b2ddc7be68a1 [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080056 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080057 if (!mCaches) {
58 mCaches = &Caches::createInstance(*this);
59 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080060 mCaches->init();
John Reck3b202512014-06-23 13:13:08 -070061}
62
John Reck49bc4ac2015-01-29 12:53:38 -080063static void layerLostGlContext(Layer* layer) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050064 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
65 "layerLostGlContext on non GL layer");
66 static_cast<GlLayer*>(layer)->onGlContextLost();
John Reck49bc4ac2015-01-29 12:53:38 -080067}
68
Chris Craik1d477422014-08-26 17:30:15 -070069void RenderState::onGLContextDestroyed() {
Chris Craik9fded232015-11-11 16:42:34 -080070 mLayerPool.clear();
71
Chris Craik96a5c4c2015-01-27 15:46:35 -080072 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080073 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
Chris Craik96a5c4c2015-01-27 15:46:35 -080074
Chris Craik44eb2c02015-01-29 09:45:09 -080075 mCaches->terminate();
76
77 delete mBlend;
78 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080079 delete mMeshState;
80 mMeshState = nullptr;
81 delete mScissor;
82 mScissor = nullptr;
83 delete mStencil;
84 mStencil = nullptr;
John Reck38e0c322015-11-10 12:19:17 -080085
sergeyvc3f13162017-02-06 11:45:14 -080086 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -050087 GpuMemoryTracker::onGpuContextDestroyed();
88}
89
90void RenderState::onVkContextCreated() {
91 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
92 "State object lifecycle not managed correctly");
93 GpuMemoryTracker::onGpuContextCreated();
94}
95
96static void layerDestroyedVkContext(Layer* layer) {
97 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
98 "layerLostVkContext on non Vulkan layer");
99 static_cast<VkLayer*>(layer)->onVkContextDestroyed();
100}
101
102void RenderState::onVkContextDestroyed() {
103 mLayerPool.clear();
104 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
105 GpuMemoryTracker::onGpuContextDestroyed();
106}
107
108GrContext* RenderState::getGrContext() const {
109 return mRenderThread.getGrContext();
Chris Craik1d477422014-08-26 17:30:15 -0700110}
111
Chris Craik9fded232015-11-11 16:42:34 -0800112void RenderState::flush(Caches::FlushMode mode) {
113 switch (mode) {
114 case Caches::FlushMode::Full:
115 // fall through
116 case Caches::FlushMode::Moderate:
117 // fall through
118 case Caches::FlushMode::Layers:
119 mLayerPool.clear();
120 break;
121 }
122 mCaches->flush(mode);
123}
124
John Reck9a814872017-05-22 15:04:21 -0700125void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
John Reck36393c32017-05-23 15:32:08 -0700126 if (mCaches && mCaches->textureCache.destroyTexture(pixelRefId)) {
John Reck9a814872017-05-22 15:04:21 -0700127 glFlush();
128 GL_CHECKPOINT(MODERATE);
129 }
130}
131
John Reck3b202512014-06-23 13:13:08 -0700132void RenderState::setViewport(GLsizei width, GLsizei height) {
133 mViewportWidth = width;
134 mViewportHeight = height;
135 glViewport(0, 0, mViewportWidth, mViewportHeight);
136}
137
138
139void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
140 *outWidth = mViewportWidth;
141 *outHeight = mViewportHeight;
142}
143
144void RenderState::bindFramebuffer(GLuint fbo) {
145 if (mFramebuffer != fbo) {
146 mFramebuffer = fbo;
147 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
148 }
149}
150
John Reck0b8d0672016-01-29 14:18:22 -0800151GLuint RenderState::createFramebuffer() {
Chris Craik818c9fb2015-10-23 14:33:42 -0700152 GLuint ret;
153 glGenFramebuffers(1, &ret);
154 return ret;
155}
156
157void RenderState::deleteFramebuffer(GLuint fbo) {
158 if (mFramebuffer == fbo) {
159 // GL defines that deleting the currently bound FBO rebinds FBO 0.
160 // Reflect this in our cached value.
161 mFramebuffer = 0;
162 }
163 glDeleteFramebuffers(1, &fbo);
164}
165
John Reck3b202512014-06-23 13:13:08 -0700166void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
John Reck95cd24b2015-08-04 11:17:39 -0700167 if (mode == DrawGlInfo::kModeProcessNoContext) {
168 // If there's no context we don't need to interrupt as there's
169 // no gl state to save/restore
170 (*functor)(mode, info);
171 } else {
172 interruptForFunctorInvoke();
173 (*functor)(mode, info);
174 resumeFromFunctorInvoke();
175 }
John Reck3b202512014-06-23 13:13:08 -0700176}
177
178void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800179 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800180 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800181 meshState().unbindMeshBuffer();
182 meshState().unbindIndicesBuffer();
183 meshState().resetVertexPointers();
184 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700185 debugOverdraw(false, false);
Romain Guy253f2c22016-09-28 17:34:42 -0700186 // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
Romain Guyefb4b062017-02-27 11:00:04 -0800187 if (mCaches->extensions().hasLinearBlending() &&
188 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() {
Romain Guyefb4b062017-02-27 11:00:04 -0800194 if (mCaches->extensions().hasLinearBlending() &&
195 mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700196 glEnable(GL_FRAMEBUFFER_SRGB_EXT);
197 }
198
John Reck3b202512014-06-23 13:13:08 -0700199 glViewport(0, 0, mViewportWidth, mViewportHeight);
200 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
201 debugOverdraw(false, false);
202
203 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
204
Chris Craik65fe5ee2015-01-26 18:06:29 -0800205 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800206 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700207
Chris Craik44eb2c02015-01-29 09:45:09 -0800208 mCaches->textureState().activateTexture(0);
209 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700210}
211
212void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700213 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700214 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800215 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800216 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700217 }
218 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800219 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700220 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800221 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700222 }
223 }
224}
225
sergeyv3e9999b2017-01-19 15:37:02 -0800226static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) {
227 layerUpdater->destroyLayer();
228}
229
230void RenderState::destroyLayersInUpdater() {
231 std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater);
232}
233
John Reck0e89e2b2014-10-31 14:49:06 -0700234class DecStrongTask : public renderthread::RenderTask {
235public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700236 explicit DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
John Reck0e89e2b2014-10-31 14:49:06 -0700237
Chris Craikd41c4d82015-01-05 15:51:13 -0800238 virtual void run() override {
239 mObject->decStrong(nullptr);
240 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700241 delete this;
242 }
243
244private:
245 VirtualLightRefBase* mObject;
246};
247
248void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800249 if (pthread_equal(mThreadId, pthread_self())) {
250 object->decStrong(nullptr);
251 } else {
252 mRenderThread.queue(new DecStrongTask(object));
253 }
John Reck0e89e2b2014-10-31 14:49:06 -0700254}
255
Chris Craik6c15ffa2015-02-02 13:50:55 -0800256///////////////////////////////////////////////////////////////////////////////
257// Render
258///////////////////////////////////////////////////////////////////////////////
259
Chris Craik12efe642015-09-28 15:52:12 -0700260void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800261 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800262 const Glop::Mesh::Vertices& vertices = mesh.vertices;
263 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c812015-02-11 13:17:06 -0800264 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800265
John Reck975591a2016-01-22 16:28:07 -0800266 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800267
Chris Craik0519c812015-02-11 13:17:06 -0800268 // ---------------------------------------------
269 // ---------- Program + uniform setup ----------
270 // ---------------------------------------------
271 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800272
Chris Craik0519c812015-02-11 13:17:06 -0800273 if (fill.colorEnabled) {
274 fill.program->setColor(fill.color);
275 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800276
Chris Craik12efe642015-09-28 15:52:12 -0700277 fill.program->set(orthoMatrix,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800278 glop.transform.modelView,
Chris Craik53e51e42015-06-01 10:35:35 -0700279 glop.transform.meshTransform(),
280 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800281
Chris Craik0519c812015-02-11 13:17:06 -0800282 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700283 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800284 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800285 glUniform4f(mCaches->program().getUniform("colorBlend"),
286 color.r, color.g, color.b, color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700287 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800288 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800289 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800290 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800291 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800292 }
293
Chris Craik0519c812015-02-11 13:17:06 -0800294 // Round rect clipping uniforms
295 if (glop.roundRectClipState) {
296 // TODO: avoid query, and cache values (or RRCS ptr) in program
297 const RoundRectClipState* state = glop.roundRectClipState;
298 const Rect& innerRect = state->innerRect;
299 glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
300 innerRect.left, innerRect.top,
301 innerRect.right, innerRect.bottom);
302 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
303 1, GL_FALSE, &state->matrix.data[0]);
304
305 // add half pixel to round out integer rect space to cover pixel centers
306 float roundedOutRadius = state->radius + 0.5f;
307 glUniform1f(fill.program->getUniform("roundRectRadius"),
308 roundedOutRadius);
309 }
Chris Craikef250742015-02-25 17:16:16 -0800310
John Reck975591a2016-01-22 16:28:07 -0800311 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800312
Chris Craik117bdbc2015-02-05 10:12:38 -0800313 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800314 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800315 // --------------------------------
316 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800317 meshState().bindMeshBuffer(vertices.bufferObject);
318 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800319
320 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800321 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800322
Chris Craik138c21f2016-04-28 16:59:42 -0700323 // texture
324 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800325 const Glop::Fill::TextureData& texture = fill.texture;
326 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c812015-02-11 13:17:06 -0800327 mCaches->textureState().activateTexture(0);
328
sergeyv2a38c422016-10-25 15:21:50 -0700329 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800330 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700331 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800332 }
Chris Craik26bf3422015-02-26 16:28:17 -0800333 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700334 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800335 }
Chris Craik0519c812015-02-11 13:17:06 -0800336
Chris Craik26bf3422015-02-26 16:28:17 -0800337 if (texture.textureTransform) {
338 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
339 GL_FALSE, &texture.textureTransform->data[0]);
340 }
Chris Craik138c21f2016-04-28 16:59:42 -0700341 }
342
343 // vertex attributes (tex coord, color, alpha)
344 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
345 meshState().enableTexCoordsVertexArray();
346 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800347 } else {
348 meshState().disableTexCoordsVertexArray();
349 }
Chris Craikef250742015-02-25 17:16:16 -0800350 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700351 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800352 colorLocation = fill.program->getAttrib("colors");
353 glEnableVertexAttribArray(colorLocation);
354 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800355 }
Chris Craikef250742015-02-25 17:16:16 -0800356 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700357 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800358 // NOTE: alpha vertex position is computed assuming no VBO
359 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
360 alphaLocation = fill.program->getAttrib("vtxAlpha");
361 glEnableVertexAttribArray(alphaLocation);
362 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800363 }
Chris Craik922d3a72015-02-13 17:47:21 -0800364 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700365 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800366
John Reck975591a2016-01-22 16:28:07 -0800367 GL_CHECKPOINT(MODERATE);
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900368 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
369 fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
370 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800371
Romain Guycaaaa662017-03-27 00:40:21 -0700372 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
373 // which means the color space conversion applies to the shader's bitmap
374 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
375 if (colorSpaceTexture != nullptr) {
376 if (colorSpaceTexture->hasColorSpaceConversion()) {
377 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
378 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
379 GL_FALSE, connector->getTransform().asArray());
380 }
381
382 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
383 if (transferFunction != TransferFunctionType::None) {
384 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
385 const ColorSpace& source = connector->getSource();
386
387 switch (transferFunction) {
388 case TransferFunctionType::None:
389 break;
390 case TransferFunctionType::Full:
391 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
392 reinterpret_cast<const float*>(&source.getTransferParameters().g));
393 break;
394 case TransferFunctionType::Limited:
395 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
396 reinterpret_cast<const float*>(&source.getTransferParameters().g));
397 break;
398 case TransferFunctionType::Gamma:
399 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
400 source.getTransferParameters().g);
401 break;
402 }
403 }
404 }
405
Chris Craik117bdbc2015-02-05 10:12:38 -0800406 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800407 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800408 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800409 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800410
John Reck975591a2016-01-22 16:28:07 -0800411 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800412
Chris Craik117bdbc2015-02-05 10:12:38 -0800413 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800414 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800415 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800416 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800417 // Since the indexed quad list is of limited length, we loop over
418 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c812015-02-11 13:17:06 -0800419 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800420 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800421 while (elementsCount > 0) {
Chris Craik9db58c02015-08-19 15:19:18 -0700422 GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Chris Craik1b7db402016-02-24 18:25:32 -0800423 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700424 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik1b7db402016-02-24 18:25:32 -0800425 meshState().bindTexCoordsVertexPointer(
Chris Craikef250742015-02-25 17:16:16 -0800426 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800427 }
428
Chris Craik2ab95d72015-02-06 15:25:51 -0800429 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
430 elementsCount -= drawCount;
Chris Craikef250742015-02-25 17:16:16 -0800431 vertexData += (drawCount / 6) * 4 * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800432 }
Chris Craikef250742015-02-25 17:16:16 -0800433 } else if (indices.bufferObject || indices.indices) {
434 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800435 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800436 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800437 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800438
John Reck975591a2016-01-22 16:28:07 -0800439 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800440
Chris Craik2ab95d72015-02-06 15:25:51 -0800441 // -----------------------------------
442 // ---------- Mesh teardown ----------
443 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700444 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800445 glDisableVertexAttribArray(alphaLocation);
446 }
Chris Craik53e51e42015-06-01 10:35:35 -0700447 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800448 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800449 }
John Reck9372ac32016-01-19 11:46:52 -0800450
John Reck975591a2016-01-22 16:28:07 -0800451 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800452}
453
454void RenderState::dump() {
455 blend().dump();
456 meshState().dump();
457 scissor().dump();
458 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800459}
460
John Reck3b202512014-06-23 13:13:08 -0700461} /* namespace uirenderer */
462} /* namespace android */