blob: 2c92924cc12cc2aeff934160a61d8b5e2fbac301 [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;
Chris Craik0519c812015-02-11 13:17:06 -0800299
300 // add half pixel to round out integer rect space to cover pixel centers
301 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000302
303 // Divide by the radius to simplify the calculations in the fragment shader
304 // roundRectPos is also passed from vertex shader relative to top/left & radius
305 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
306 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
307 (innerRect.right - innerRect.left) / roundedOutRadius,
308 (innerRect.bottom - innerRect.top) / roundedOutRadius);
309
310 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
311 1, GL_FALSE, &state->matrix.data[0]);
312
Chris Craik0519c812015-02-11 13:17:06 -0800313 glUniform1f(fill.program->getUniform("roundRectRadius"),
314 roundedOutRadius);
315 }
Chris Craikef250742015-02-25 17:16:16 -0800316
John Reck975591a2016-01-22 16:28:07 -0800317 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800318
Chris Craik117bdbc2015-02-05 10:12:38 -0800319 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800320 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800321 // --------------------------------
322 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800323 meshState().bindMeshBuffer(vertices.bufferObject);
324 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800325
326 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800327 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800328
Chris Craik138c21f2016-04-28 16:59:42 -0700329 // texture
330 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800331 const Glop::Fill::TextureData& texture = fill.texture;
332 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c812015-02-11 13:17:06 -0800333 mCaches->textureState().activateTexture(0);
334
sergeyv2a38c422016-10-25 15:21:50 -0700335 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800336 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700337 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800338 }
Chris Craik26bf3422015-02-26 16:28:17 -0800339 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700340 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800341 }
Chris Craik0519c812015-02-11 13:17:06 -0800342
Chris Craik26bf3422015-02-26 16:28:17 -0800343 if (texture.textureTransform) {
344 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
345 GL_FALSE, &texture.textureTransform->data[0]);
346 }
Chris Craik138c21f2016-04-28 16:59:42 -0700347 }
348
349 // vertex attributes (tex coord, color, alpha)
350 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
351 meshState().enableTexCoordsVertexArray();
352 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800353 } else {
354 meshState().disableTexCoordsVertexArray();
355 }
Chris Craikef250742015-02-25 17:16:16 -0800356 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700357 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800358 colorLocation = fill.program->getAttrib("colors");
359 glEnableVertexAttribArray(colorLocation);
360 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800361 }
Chris Craikef250742015-02-25 17:16:16 -0800362 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700363 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800364 // NOTE: alpha vertex position is computed assuming no VBO
365 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
366 alphaLocation = fill.program->getAttrib("vtxAlpha");
367 glEnableVertexAttribArray(alphaLocation);
368 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800369 }
Chris Craik922d3a72015-02-13 17:47:21 -0800370 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700371 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800372
John Reck975591a2016-01-22 16:28:07 -0800373 GL_CHECKPOINT(MODERATE);
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900374 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
375 fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
376 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800377
Romain Guycaaaa662017-03-27 00:40:21 -0700378 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
379 // which means the color space conversion applies to the shader's bitmap
380 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
381 if (colorSpaceTexture != nullptr) {
382 if (colorSpaceTexture->hasColorSpaceConversion()) {
383 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
384 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
385 GL_FALSE, connector->getTransform().asArray());
386 }
387
388 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
389 if (transferFunction != TransferFunctionType::None) {
390 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
391 const ColorSpace& source = connector->getSource();
392
393 switch (transferFunction) {
394 case TransferFunctionType::None:
395 break;
396 case TransferFunctionType::Full:
397 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
398 reinterpret_cast<const float*>(&source.getTransferParameters().g));
399 break;
400 case TransferFunctionType::Limited:
401 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
402 reinterpret_cast<const float*>(&source.getTransferParameters().g));
403 break;
404 case TransferFunctionType::Gamma:
405 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
406 source.getTransferParameters().g);
407 break;
408 }
409 }
410 }
411
Chris Craik117bdbc2015-02-05 10:12:38 -0800412 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800413 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800414 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800415 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800416
John Reck975591a2016-01-22 16:28:07 -0800417 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800418
Chris Craik117bdbc2015-02-05 10:12:38 -0800419 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800420 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800421 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800422 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800423 // Since the indexed quad list is of limited length, we loop over
424 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c812015-02-11 13:17:06 -0800425 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800426 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800427 while (elementsCount > 0) {
Chris Craik9db58c02015-08-19 15:19:18 -0700428 GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000429 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800430 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700431 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik1b7db402016-02-24 18:25:32 -0800432 meshState().bindTexCoordsVertexPointer(
Chris Craikef250742015-02-25 17:16:16 -0800433 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800434 }
435
Arunb0a94772017-01-23 11:41:06 +0000436 if (mCaches->extensions().getMajorGlVersion() >= 3) {
437 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount-1, drawCount, GL_UNSIGNED_SHORT, nullptr);
438 } else {
439 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
440 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800441 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000442 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800443 }
Chris Craikef250742015-02-25 17:16:16 -0800444 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000445 if (mCaches->extensions().getMajorGlVersion() >= 3) {
446 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine the min/max index values)
447 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount-1, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
448 } else {
449 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
450 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800451 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800452 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800453 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800454
John Reck975591a2016-01-22 16:28:07 -0800455 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800456
Chris Craik2ab95d72015-02-06 15:25:51 -0800457 // -----------------------------------
458 // ---------- Mesh teardown ----------
459 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700460 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800461 glDisableVertexAttribArray(alphaLocation);
462 }
Chris Craik53e51e42015-06-01 10:35:35 -0700463 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800464 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800465 }
John Reck9372ac32016-01-19 11:46:52 -0800466
John Reck975591a2016-01-22 16:28:07 -0800467 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800468}
469
470void RenderState::dump() {
471 blend().dump();
472 meshState().dump();
473 scissor().dump();
474 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800475}
476
John Reck3b202512014-06-23 13:13:08 -0700477} /* namespace uirenderer */
478} /* namespace android */