blob: ededffb0f4bb26b0b4d232f583824ec1ba4c4c94 [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() {
John Reck642ebea2017-07-17 09:55:02 -0700108 mLayerPool->clear();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500109 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
110 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 -0700239class DecStrongTask : public renderthread::RenderTask {
240public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700241 explicit DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
John Reck0e89e2b2014-10-31 14:49:06 -0700242
Chris Craikd41c4d82015-01-05 15:51:13 -0800243 virtual void run() override {
244 mObject->decStrong(nullptr);
245 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700246 delete this;
247 }
248
249private:
250 VirtualLightRefBase* mObject;
251};
252
253void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800254 if (pthread_equal(mThreadId, pthread_self())) {
255 object->decStrong(nullptr);
256 } else {
257 mRenderThread.queue(new DecStrongTask(object));
258 }
John Reck0e89e2b2014-10-31 14:49:06 -0700259}
260
Chris Craik6c15ffa2015-02-02 13:50:55 -0800261///////////////////////////////////////////////////////////////////////////////
262// Render
263///////////////////////////////////////////////////////////////////////////////
264
Chris Craik12efe642015-09-28 15:52:12 -0700265void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800266 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800267 const Glop::Mesh::Vertices& vertices = mesh.vertices;
268 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800269 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800270
John Reck975591a2016-01-22 16:28:07 -0800271 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800272
Chris Craik0519c8102015-02-11 13:17:06 -0800273 // ---------------------------------------------
274 // ---------- Program + uniform setup ----------
275 // ---------------------------------------------
276 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800277
Chris Craik0519c8102015-02-11 13:17:06 -0800278 if (fill.colorEnabled) {
279 fill.program->setColor(fill.color);
280 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800281
Chris Craik12efe642015-09-28 15:52:12 -0700282 fill.program->set(orthoMatrix,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800283 glop.transform.modelView,
Chris Craik53e51e42015-06-01 10:35:35 -0700284 glop.transform.meshTransform(),
285 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800286
Chris Craik0519c8102015-02-11 13:17:06 -0800287 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700288 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800289 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800290 glUniform4f(mCaches->program().getUniform("colorBlend"),
291 color.r, color.g, color.b, color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700292 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800293 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800294 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800295 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800296 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800297 }
298
Chris Craik0519c8102015-02-11 13:17:06 -0800299 // Round rect clipping uniforms
300 if (glop.roundRectClipState) {
301 // TODO: avoid query, and cache values (or RRCS ptr) in program
302 const RoundRectClipState* state = glop.roundRectClipState;
303 const Rect& innerRect = state->innerRect;
Chris Craik0519c8102015-02-11 13:17:06 -0800304
305 // add half pixel to round out integer rect space to cover pixel centers
306 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000307
308 // Divide by the radius to simplify the calculations in the fragment shader
309 // roundRectPos is also passed from vertex shader relative to top/left & radius
310 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
311 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
312 (innerRect.right - innerRect.left) / roundedOutRadius,
313 (innerRect.bottom - innerRect.top) / roundedOutRadius);
314
315 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
316 1, GL_FALSE, &state->matrix.data[0]);
317
Chris Craik0519c8102015-02-11 13:17:06 -0800318 glUniform1f(fill.program->getUniform("roundRectRadius"),
319 roundedOutRadius);
320 }
Chris Craikef250742015-02-25 17:16:16 -0800321
John Reck975591a2016-01-22 16:28:07 -0800322 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800323
Chris Craik117bdbc2015-02-05 10:12:38 -0800324 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800325 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800326 // --------------------------------
327 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800328 meshState().bindMeshBuffer(vertices.bufferObject);
329 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800330
331 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800332 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800333
Chris Craik138c21f2016-04-28 16:59:42 -0700334 // texture
335 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800336 const Glop::Fill::TextureData& texture = fill.texture;
337 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800338 mCaches->textureState().activateTexture(0);
339
sergeyv2a38c422016-10-25 15:21:50 -0700340 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800341 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700342 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800343 }
Chris Craik26bf3422015-02-26 16:28:17 -0800344 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700345 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800346 }
Chris Craik0519c8102015-02-11 13:17:06 -0800347
Chris Craik26bf3422015-02-26 16:28:17 -0800348 if (texture.textureTransform) {
349 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
350 GL_FALSE, &texture.textureTransform->data[0]);
351 }
Chris Craik138c21f2016-04-28 16:59:42 -0700352 }
353
354 // vertex attributes (tex coord, color, alpha)
355 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
356 meshState().enableTexCoordsVertexArray();
357 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800358 } else {
359 meshState().disableTexCoordsVertexArray();
360 }
Chris Craikef250742015-02-25 17:16:16 -0800361 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700362 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800363 colorLocation = fill.program->getAttrib("colors");
364 glEnableVertexAttribArray(colorLocation);
365 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800366 }
Chris Craikef250742015-02-25 17:16:16 -0800367 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700368 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800369 // NOTE: alpha vertex position is computed assuming no VBO
370 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
371 alphaLocation = fill.program->getAttrib("vtxAlpha");
372 glEnableVertexAttribArray(alphaLocation);
373 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800374 }
Chris Craik922d3a72015-02-13 17:47:21 -0800375 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700376 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800377
John Reck975591a2016-01-22 16:28:07 -0800378 GL_CHECKPOINT(MODERATE);
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900379 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
380 fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
381 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800382
Romain Guycaaaa662017-03-27 00:40:21 -0700383 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
384 // which means the color space conversion applies to the shader's bitmap
385 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
386 if (colorSpaceTexture != nullptr) {
387 if (colorSpaceTexture->hasColorSpaceConversion()) {
388 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
389 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
390 GL_FALSE, connector->getTransform().asArray());
391 }
392
393 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
394 if (transferFunction != TransferFunctionType::None) {
395 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
396 const ColorSpace& source = connector->getSource();
397
398 switch (transferFunction) {
399 case TransferFunctionType::None:
400 break;
401 case TransferFunctionType::Full:
402 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
403 reinterpret_cast<const float*>(&source.getTransferParameters().g));
404 break;
405 case TransferFunctionType::Limited:
406 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
407 reinterpret_cast<const float*>(&source.getTransferParameters().g));
408 break;
409 case TransferFunctionType::Gamma:
410 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
411 source.getTransferParameters().g);
412 break;
413 }
414 }
415 }
416
Chris Craik117bdbc2015-02-05 10:12:38 -0800417 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800418 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800419 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800420 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800421
John Reck975591a2016-01-22 16:28:07 -0800422 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800423
Chris Craik117bdbc2015-02-05 10:12:38 -0800424 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800425 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800426 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800427 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800428 // Since the indexed quad list is of limited length, we loop over
429 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800430 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800431 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800432 while (elementsCount > 0) {
Chris Craik9db58c02015-08-19 15:19:18 -0700433 GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000434 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800435 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700436 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik1b7db402016-02-24 18:25:32 -0800437 meshState().bindTexCoordsVertexPointer(
Chris Craikef250742015-02-25 17:16:16 -0800438 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800439 }
440
Arunb0a94772017-01-23 11:41:06 +0000441 if (mCaches->extensions().getMajorGlVersion() >= 3) {
442 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount-1, drawCount, GL_UNSIGNED_SHORT, nullptr);
443 } else {
444 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
445 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800446 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000447 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800448 }
Chris Craikef250742015-02-25 17:16:16 -0800449 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000450 if (mCaches->extensions().getMajorGlVersion() >= 3) {
451 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine the min/max index values)
452 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount-1, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
453 } else {
454 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
455 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800456 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800457 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800458 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800459
John Reck975591a2016-01-22 16:28:07 -0800460 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800461
Chris Craik2ab95d72015-02-06 15:25:51 -0800462 // -----------------------------------
463 // ---------- Mesh teardown ----------
464 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700465 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800466 glDisableVertexAttribArray(alphaLocation);
467 }
Chris Craik53e51e42015-06-01 10:35:35 -0700468 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800469 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800470 }
John Reck9372ac32016-01-19 11:46:52 -0800471
John Reck975591a2016-01-22 16:28:07 -0800472 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800473}
474
475void RenderState::dump() {
476 blend().dump();
477 meshState().dump();
478 scissor().dump();
479 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800480}
481
John Reck3b202512014-06-23 13:13:08 -0700482} /* namespace uirenderer */
483} /* namespace android */