blob: f173136d6f8d36e9795602cd8100e00e39403c50 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
John Reck1bcacfd2017-11-03 10:12:19 -070016#include "renderstate/RenderState.h"
17#include <GpuMemoryTracker.h>
sergeyv3e9999b2017-01-19 15:37:02 -080018#include "DeferredLayerUpdater.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050019#include "GlLayer.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050020#include "VkLayer.h"
John Reck3b202512014-06-23 13:13:08 -070021
John Reck443a7142014-09-04 17:40:05 -070022#include "renderthread/CanvasContext.h"
John Reck0e89e2b2014-10-31 14:49:06 -070023#include "renderthread/EglManager.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080024#include "utils/GLUtils.h"
Romain Guycaaaa662017-03-27 00:40:21 -070025
Chris Craik9db58c02015-08-19 15:19:18 -070026#include <algorithm>
27
Romain Guycaaaa662017-03-27 00:40:21 -070028#include <ui/ColorSpace.h>
29
John Reck3b202512014-06-23 13:13:08 -070030namespace android {
31namespace uirenderer {
32
John Reck0e89e2b2014-10-31 14:49:06 -070033RenderState::RenderState(renderthread::RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070034 : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070035 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070036}
37
38RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080039 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070040 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070041}
42
43void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080044 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070045 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050046 GpuMemoryTracker::onGpuContextCreated();
John Reck38e0c322015-11-10 12:19:17 -080047
Chris Craik44eb2c02015-01-29 09:45:09 -080048 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080049 mMeshState = new MeshState();
50 mScissor = new Scissor();
51 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080052
John Reck642ebea2017-07-17 09:55:02 -070053 // Deferred because creation needs GL context for texture limits
54 if (!mLayerPool) {
55 mLayerPool = new OffscreenBufferPool();
56 }
57
Chris Craik96a5c4c2015-01-27 15:46:35 -080058 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080059 if (!mCaches) {
60 mCaches = &Caches::createInstance(*this);
61 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080062 mCaches->init();
John Reck3b202512014-06-23 13:13:08 -070063}
64
John Reck49bc4ac2015-01-29 12:53:38 -080065static void layerLostGlContext(Layer* layer) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050066 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
John Reck1bcacfd2017-11-03 10:12:19 -070067 "layerLostGlContext on non GL layer");
Greg Daniel8cd3edf2017-01-09 14:15:41 -050068 static_cast<GlLayer*>(layer)->onGlContextLost();
John Reck49bc4ac2015-01-29 12:53:38 -080069}
70
Chris Craik1d477422014-08-26 17:30:15 -070071void RenderState::onGLContextDestroyed() {
John Reck642ebea2017-07-17 09:55:02 -070072 mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -080073
Chris Craik96a5c4c2015-01-27 15:46:35 -080074 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080075 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
Chris Craik96a5c4c2015-01-27 15:46:35 -080076
Chris Craik44eb2c02015-01-29 09:45:09 -080077 mCaches->terminate();
78
79 delete mBlend;
80 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080081 delete mMeshState;
82 mMeshState = nullptr;
83 delete mScissor;
84 mScissor = nullptr;
85 delete mStencil;
86 mStencil = nullptr;
John Reck38e0c322015-11-10 12:19:17 -080087
sergeyvc3f13162017-02-06 11:45:14 -080088 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -050089 GpuMemoryTracker::onGpuContextDestroyed();
90}
91
92void RenderState::onVkContextCreated() {
93 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070094 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050095 GpuMemoryTracker::onGpuContextCreated();
96}
97
98static void layerDestroyedVkContext(Layer* layer) {
99 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
100 "layerLostVkContext on non Vulkan layer");
101 static_cast<VkLayer*>(layer)->onVkContextDestroyed();
102}
103
104void RenderState::onVkContextDestroyed() {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500105 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
Derek Sollenberger7a4216b2017-10-25 09:52:23 -0400106 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500107 GpuMemoryTracker::onGpuContextDestroyed();
108}
109
110GrContext* RenderState::getGrContext() const {
111 return mRenderThread.getGrContext();
Chris Craik1d477422014-08-26 17:30:15 -0700112}
113
Chris Craik9fded232015-11-11 16:42:34 -0800114void RenderState::flush(Caches::FlushMode mode) {
115 switch (mode) {
116 case Caches::FlushMode::Full:
John Reck1bcacfd2017-11-03 10:12:19 -0700117 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800118 case Caches::FlushMode::Moderate:
John Reck1bcacfd2017-11-03 10:12:19 -0700119 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800120 case Caches::FlushMode::Layers:
John Reck642ebea2017-07-17 09:55:02 -0700121 if (mLayerPool) mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -0800122 break;
123 }
John Reck642ebea2017-07-17 09:55:02 -0700124 if (mCaches) mCaches->flush(mode);
Chris Craik9fded232015-11-11 16:42:34 -0800125}
126
John Reck9a814872017-05-22 15:04:21 -0700127void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
Mike Reed8cafcc62018-05-03 11:32:46 -0400128 // DEAD CODE
John Reck9a814872017-05-22 15:04:21 -0700129}
130
John Reck3b202512014-06-23 13:13:08 -0700131void RenderState::setViewport(GLsizei width, GLsizei height) {
132 mViewportWidth = width;
133 mViewportHeight = height;
134 glViewport(0, 0, mViewportWidth, mViewportHeight);
135}
136
John Reck3b202512014-06-23 13:13:08 -0700137void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
138 *outWidth = mViewportWidth;
139 *outHeight = mViewportHeight;
140}
141
142void RenderState::bindFramebuffer(GLuint fbo) {
143 if (mFramebuffer != fbo) {
144 mFramebuffer = fbo;
145 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
146 }
147}
148
John Reck0b8d0672016-01-29 14:18:22 -0800149GLuint RenderState::createFramebuffer() {
Chris Craik818c9fb2015-10-23 14:33:42 -0700150 GLuint ret;
151 glGenFramebuffers(1, &ret);
152 return ret;
153}
154
155void RenderState::deleteFramebuffer(GLuint fbo) {
156 if (mFramebuffer == fbo) {
157 // GL defines that deleting the currently bound FBO rebinds FBO 0.
158 // Reflect this in our cached value.
159 mFramebuffer = 0;
160 }
161 glDeleteFramebuffers(1, &fbo);
162}
163
John Reck3b202512014-06-23 13:13:08 -0700164void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
John Reck95cd24b2015-08-04 11:17:39 -0700165 if (mode == DrawGlInfo::kModeProcessNoContext) {
166 // If there's no context we don't need to interrupt as there's
167 // no gl state to save/restore
168 (*functor)(mode, info);
169 } else {
170 interruptForFunctorInvoke();
171 (*functor)(mode, info);
172 resumeFromFunctorInvoke();
173 }
John Reck3b202512014-06-23 13:13:08 -0700174}
175
176void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800177 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800178 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800179 meshState().unbindMeshBuffer();
180 meshState().unbindIndicesBuffer();
181 meshState().resetVertexPointers();
182 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700183 debugOverdraw(false, false);
Romain Guy253f2c22016-09-28 17:34:42 -0700184 // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
John Reck1bcacfd2017-11-03 10:12:19 -0700185 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700186 glDisable(GL_FRAMEBUFFER_SRGB_EXT);
187 }
John Reck3b202512014-06-23 13:13:08 -0700188}
189
190void RenderState::resumeFromFunctorInvoke() {
John Reck1bcacfd2017-11-03 10:12:19 -0700191 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700192 glEnable(GL_FRAMEBUFFER_SRGB_EXT);
193 }
194
John Reck3b202512014-06-23 13:13:08 -0700195 glViewport(0, 0, mViewportWidth, mViewportHeight);
196 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
197 debugOverdraw(false, false);
198
199 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
200
Chris Craik65fe5ee2015-01-26 18:06:29 -0800201 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800202 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700203
Chris Craik44eb2c02015-01-29 09:45:09 -0800204 mCaches->textureState().activateTexture(0);
205 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700206}
207
208void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700209 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700210 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800211 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800212 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700213 }
214 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800215 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700216 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800217 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700218 }
219 }
220}
221
sergeyv3e9999b2017-01-19 15:37:02 -0800222static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) {
223 layerUpdater->destroyLayer();
224}
225
226void RenderState::destroyLayersInUpdater() {
227 std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater);
228}
229
John Reck0e89e2b2014-10-31 14:49:06 -0700230void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800231 if (pthread_equal(mThreadId, pthread_self())) {
232 object->decStrong(nullptr);
233 } else {
John Reckf8441e62017-10-23 13:10:41 -0700234 mRenderThread.queue().post([object]() { object->decStrong(nullptr); });
John Recka55b5d62016-01-14 15:09:10 -0800235 }
John Reck0e89e2b2014-10-31 14:49:06 -0700236}
237
Chris Craik6c15ffa2015-02-02 13:50:55 -0800238///////////////////////////////////////////////////////////////////////////////
239// Render
240///////////////////////////////////////////////////////////////////////////////
241
Arun530a2b42017-01-23 12:47:57 +0000242void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700243 bool overrideDisableBlending) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800244 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800245 const Glop::Mesh::Vertices& vertices = mesh.vertices;
246 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800247 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800248
John Reck975591a2016-01-22 16:28:07 -0800249 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800250
Chris Craik0519c8102015-02-11 13:17:06 -0800251 // ---------------------------------------------
252 // ---------- Program + uniform setup ----------
253 // ---------------------------------------------
254 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800255
Chris Craik0519c8102015-02-11 13:17:06 -0800256 if (fill.colorEnabled) {
257 fill.program->setColor(fill.color);
258 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800259
John Reck1bcacfd2017-11-03 10:12:19 -0700260 fill.program->set(orthoMatrix, glop.transform.modelView, glop.transform.meshTransform(),
261 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800262
Chris Craik0519c8102015-02-11 13:17:06 -0800263 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700264 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800265 const FloatColor& color = fill.filter.color;
John Reck1bcacfd2017-11-03 10:12:19 -0700266 glUniform4f(mCaches->program().getUniform("colorBlend"), color.r, color.g, color.b,
267 color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700268 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800269 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700270 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800271 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
John Reck1bcacfd2017-11-03 10:12:19 -0700272 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800273 }
274
Chris Craik0519c8102015-02-11 13:17:06 -0800275 // Round rect clipping uniforms
276 if (glop.roundRectClipState) {
277 // TODO: avoid query, and cache values (or RRCS ptr) in program
278 const RoundRectClipState* state = glop.roundRectClipState;
279 const Rect& innerRect = state->innerRect;
Chris Craik0519c8102015-02-11 13:17:06 -0800280
281 // add half pixel to round out integer rect space to cover pixel centers
282 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000283
284 // Divide by the radius to simplify the calculations in the fragment shader
285 // roundRectPos is also passed from vertex shader relative to top/left & radius
286 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
John Reck1bcacfd2017-11-03 10:12:19 -0700287 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
288 (innerRect.right - innerRect.left) / roundedOutRadius,
289 (innerRect.bottom - innerRect.top) / roundedOutRadius);
Arun06e9f322017-01-23 11:59:21 +0000290
John Reck1bcacfd2017-11-03 10:12:19 -0700291 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), 1, GL_FALSE,
292 &state->matrix.data[0]);
Arun06e9f322017-01-23 11:59:21 +0000293
John Reck1bcacfd2017-11-03 10:12:19 -0700294 glUniform1f(fill.program->getUniform("roundRectRadius"), roundedOutRadius);
Chris Craik0519c8102015-02-11 13:17:06 -0800295 }
Chris Craikef250742015-02-25 17:16:16 -0800296
John Reck975591a2016-01-22 16:28:07 -0800297 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800298
Chris Craik117bdbc2015-02-05 10:12:38 -0800299 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800300 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800301 // --------------------------------
302 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800303 meshState().bindMeshBuffer(vertices.bufferObject);
304 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800305
306 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800307 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800308
Chris Craik138c21f2016-04-28 16:59:42 -0700309 // texture
310 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800311 const Glop::Fill::TextureData& texture = fill.texture;
312 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800313 mCaches->textureState().activateTexture(0);
314
sergeyv2a38c422016-10-25 15:21:50 -0700315 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800316 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700317 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800318 }
Chris Craik26bf3422015-02-26 16:28:17 -0800319 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700320 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800321 }
Chris Craik0519c8102015-02-11 13:17:06 -0800322
Chris Craik26bf3422015-02-26 16:28:17 -0800323 if (texture.textureTransform) {
John Reck1bcacfd2017-11-03 10:12:19 -0700324 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1, GL_FALSE,
325 &texture.textureTransform->data[0]);
Chris Craik26bf3422015-02-26 16:28:17 -0800326 }
Chris Craik138c21f2016-04-28 16:59:42 -0700327 }
328
329 // vertex attributes (tex coord, color, alpha)
330 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
331 meshState().enableTexCoordsVertexArray();
332 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800333 } else {
334 meshState().disableTexCoordsVertexArray();
335 }
Chris Craikef250742015-02-25 17:16:16 -0800336 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700337 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800338 colorLocation = fill.program->getAttrib("colors");
339 glEnableVertexAttribArray(colorLocation);
John Reck1bcacfd2017-11-03 10:12:19 -0700340 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride,
341 vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800342 }
Chris Craikef250742015-02-25 17:16:16 -0800343 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700344 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800345 // NOTE: alpha vertex position is computed assuming no VBO
John Reck1bcacfd2017-11-03 10:12:19 -0700346 const void* alphaCoords = ((const GLbyte*)vertices.position) + kVertexAlphaOffset;
Chris Craikef250742015-02-25 17:16:16 -0800347 alphaLocation = fill.program->getAttrib("vtxAlpha");
348 glEnableVertexAttribArray(alphaLocation);
349 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800350 }
Chris Craik922d3a72015-02-13 17:47:21 -0800351 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700352 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800353
John Reck975591a2016-01-22 16:28:07 -0800354 GL_CHECKPOINT(MODERATE);
John Reck1bcacfd2017-11-03 10:12:19 -0700355 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType)
356 ? fill.skiaShaderData.bitmapData.bitmapTexture
357 : nullptr;
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900358 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800359
Romain Guycaaaa662017-03-27 00:40:21 -0700360 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
361 // which means the color space conversion applies to the shader's bitmap
362 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
363 if (colorSpaceTexture != nullptr) {
364 if (colorSpaceTexture->hasColorSpaceConversion()) {
365 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
John Reck1bcacfd2017-11-03 10:12:19 -0700366 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1, GL_FALSE,
367 connector->getTransform().asArray());
Romain Guycaaaa662017-03-27 00:40:21 -0700368 }
369
370 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
371 if (transferFunction != TransferFunctionType::None) {
372 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
373 const ColorSpace& source = connector->getSource();
374
375 switch (transferFunction) {
376 case TransferFunctionType::None:
377 break;
378 case TransferFunctionType::Full:
379 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
John Reck1bcacfd2017-11-03 10:12:19 -0700380 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700381 break;
382 case TransferFunctionType::Limited:
383 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
John Reck1bcacfd2017-11-03 10:12:19 -0700384 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700385 break;
386 case TransferFunctionType::Gamma:
387 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
John Reck1bcacfd2017-11-03 10:12:19 -0700388 source.getTransferParameters().g);
Romain Guycaaaa662017-03-27 00:40:21 -0700389 break;
390 }
391 }
392 }
393
Chris Craik117bdbc2015-02-05 10:12:38 -0800394 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800395 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800396 // ------------------------------------
Arun530a2b42017-01-23 12:47:57 +0000397 if (CC_UNLIKELY(overrideDisableBlending)) {
398 blend().setFactors(GL_ZERO, GL_ZERO);
399 } else {
400 blend().setFactors(glop.blend.src, glop.blend.dst);
401 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800402
John Reck975591a2016-01-22 16:28:07 -0800403 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800404
Chris Craik117bdbc2015-02-05 10:12:38 -0800405 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800406 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800407 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800408 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800409 // Since the indexed quad list is of limited length, we loop over
410 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800411 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800412 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800413 while (elementsCount > 0) {
John Reck1bcacfd2017-11-03 10:12:19 -0700414 GLsizei drawCount = std::min(elementsCount, (GLsizei)kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000415 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800416 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700417 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
John Reck1bcacfd2017-11-03 10:12:19 -0700418 meshState().bindTexCoordsVertexPointer(vertexData + kMeshTextureOffset,
419 vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800420 }
421
Arunb0a94772017-01-23 11:41:06 +0000422 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700423 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount - 1, drawCount,
424 GL_UNSIGNED_SHORT, nullptr);
Arunb0a94772017-01-23 11:41:06 +0000425 } else {
426 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
427 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800428 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000429 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800430 }
Chris Craikef250742015-02-25 17:16:16 -0800431 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000432 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700433 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine
434 // the min/max index values)
435 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount - 1, mesh.elementCount,
436 GL_UNSIGNED_SHORT, indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000437 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700438 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT,
439 indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000440 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800441 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800442 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800443 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800444
John Reck975591a2016-01-22 16:28:07 -0800445 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800446
Chris Craik2ab95d72015-02-06 15:25:51 -0800447 // -----------------------------------
448 // ---------- Mesh teardown ----------
449 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700450 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800451 glDisableVertexAttribArray(alphaLocation);
452 }
Chris Craik53e51e42015-06-01 10:35:35 -0700453 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800454 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800455 }
John Reck9372ac32016-01-19 11:46:52 -0800456
John Reck975591a2016-01-22 16:28:07 -0800457 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800458}
459
460void RenderState::dump() {
461 blend().dump();
462 meshState().dump();
463 scissor().dump();
464 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800465}
466
John Reck3b202512014-06-23 13:13:08 -0700467} /* namespace uirenderer */
468} /* namespace android */