blob: a60e2b5c815ea83d11416f37fd93f4b848e8056e [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 Reckd9d7f122018-05-03 14:40:56 -070021#include "Snapshot.h"
John Reck3b202512014-06-23 13:13:08 -070022
John Reck443a7142014-09-04 17:40:05 -070023#include "renderthread/CanvasContext.h"
John Reck0e89e2b2014-10-31 14:49:06 -070024#include "renderthread/EglManager.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080025#include "utils/GLUtils.h"
Romain Guycaaaa662017-03-27 00:40:21 -070026
Chris Craik9db58c02015-08-19 15:19:18 -070027#include <algorithm>
28
Romain Guycaaaa662017-03-27 00:40:21 -070029#include <ui/ColorSpace.h>
30
John Reck3b202512014-06-23 13:13:08 -070031namespace android {
32namespace uirenderer {
33
John Reck0e89e2b2014-10-31 14:49:06 -070034RenderState::RenderState(renderthread::RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070035 : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070036 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070037}
38
39RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080040 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070041 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070042}
43
44void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080045 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070046 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050047 GpuMemoryTracker::onGpuContextCreated();
John Reck38e0c322015-11-10 12:19:17 -080048
Chris Craik44eb2c02015-01-29 09:45:09 -080049 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080050 mMeshState = new MeshState();
51 mScissor = new Scissor();
52 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080053
John Reck642ebea2017-07-17 09:55:02 -070054 // Deferred because creation needs GL context for texture limits
55 if (!mLayerPool) {
56 mLayerPool = new OffscreenBufferPool();
57 }
58
Chris Craik96a5c4c2015-01-27 15:46:35 -080059 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080060 if (!mCaches) {
61 mCaches = &Caches::createInstance(*this);
62 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080063 mCaches->init();
John Reck3b202512014-06-23 13:13:08 -070064}
65
John Reck49bc4ac2015-01-29 12:53:38 -080066static void layerLostGlContext(Layer* layer) {
Greg Daniel8cd3edf2017-01-09 14:15:41 -050067 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
John Reck1bcacfd2017-11-03 10:12:19 -070068 "layerLostGlContext on non GL layer");
Greg Daniel8cd3edf2017-01-09 14:15:41 -050069 static_cast<GlLayer*>(layer)->onGlContextLost();
John Reck49bc4ac2015-01-29 12:53:38 -080070}
71
Chris Craik1d477422014-08-26 17:30:15 -070072void RenderState::onGLContextDestroyed() {
John Reck642ebea2017-07-17 09:55:02 -070073 mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -080074
Chris Craik96a5c4c2015-01-27 15:46:35 -080075 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080076 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
Chris Craik96a5c4c2015-01-27 15:46:35 -080077
Chris Craik44eb2c02015-01-29 09:45:09 -080078 mCaches->terminate();
79
80 delete mBlend;
81 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080082 delete mMeshState;
83 mMeshState = nullptr;
84 delete mScissor;
85 mScissor = nullptr;
86 delete mStencil;
87 mStencil = nullptr;
John Reck38e0c322015-11-10 12:19:17 -080088
sergeyvc3f13162017-02-06 11:45:14 -080089 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -050090 GpuMemoryTracker::onGpuContextDestroyed();
91}
92
93void RenderState::onVkContextCreated() {
94 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
John Reck1bcacfd2017-11-03 10:12:19 -070095 "State object lifecycle not managed correctly");
Greg Daniel45ec62b2017-01-04 14:27:00 -050096 GpuMemoryTracker::onGpuContextCreated();
97}
98
99static void layerDestroyedVkContext(Layer* layer) {
100 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
101 "layerLostVkContext on non Vulkan layer");
102 static_cast<VkLayer*>(layer)->onVkContextDestroyed();
103}
104
105void RenderState::onVkContextDestroyed() {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500106 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
Derek Sollenberger7a4216b2017-10-25 09:52:23 -0400107 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500108 GpuMemoryTracker::onGpuContextDestroyed();
109}
110
111GrContext* RenderState::getGrContext() const {
112 return mRenderThread.getGrContext();
Chris Craik1d477422014-08-26 17:30:15 -0700113}
114
Chris Craik9fded232015-11-11 16:42:34 -0800115void RenderState::flush(Caches::FlushMode mode) {
116 switch (mode) {
117 case Caches::FlushMode::Full:
John Reck1bcacfd2017-11-03 10:12:19 -0700118 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800119 case Caches::FlushMode::Moderate:
John Reck1bcacfd2017-11-03 10:12:19 -0700120 // fall through
Chris Craik9fded232015-11-11 16:42:34 -0800121 case Caches::FlushMode::Layers:
John Reck642ebea2017-07-17 09:55:02 -0700122 if (mLayerPool) mLayerPool->clear();
Chris Craik9fded232015-11-11 16:42:34 -0800123 break;
124 }
John Reck642ebea2017-07-17 09:55:02 -0700125 if (mCaches) mCaches->flush(mode);
Chris Craik9fded232015-11-11 16:42:34 -0800126}
127
John Reck9a814872017-05-22 15:04:21 -0700128void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
Mike Reed8cafcc62018-05-03 11:32:46 -0400129 // DEAD CODE
John Reck9a814872017-05-22 15:04:21 -0700130}
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
John Reck3b202512014-06-23 13:13:08 -0700138void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
139 *outWidth = mViewportWidth;
140 *outHeight = mViewportHeight;
141}
142
143void RenderState::bindFramebuffer(GLuint fbo) {
144 if (mFramebuffer != fbo) {
145 mFramebuffer = fbo;
146 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
147 }
148}
149
John Reck0b8d0672016-01-29 14:18:22 -0800150GLuint RenderState::createFramebuffer() {
Chris Craik818c9fb2015-10-23 14:33:42 -0700151 GLuint ret;
152 glGenFramebuffers(1, &ret);
153 return ret;
154}
155
156void RenderState::deleteFramebuffer(GLuint fbo) {
157 if (mFramebuffer == fbo) {
158 // GL defines that deleting the currently bound FBO rebinds FBO 0.
159 // Reflect this in our cached value.
160 mFramebuffer = 0;
161 }
162 glDeleteFramebuffers(1, &fbo);
163}
164
John Reck3b202512014-06-23 13:13:08 -0700165void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
John Reck95cd24b2015-08-04 11:17:39 -0700166 if (mode == DrawGlInfo::kModeProcessNoContext) {
167 // If there's no context we don't need to interrupt as there's
168 // no gl state to save/restore
169 (*functor)(mode, info);
170 } else {
171 interruptForFunctorInvoke();
172 (*functor)(mode, info);
173 resumeFromFunctorInvoke();
174 }
John Reck3b202512014-06-23 13:13:08 -0700175}
176
177void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800178 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800179 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800180 meshState().unbindMeshBuffer();
181 meshState().unbindIndicesBuffer();
182 meshState().resetVertexPointers();
183 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700184 debugOverdraw(false, false);
Romain Guy253f2c22016-09-28 17:34:42 -0700185 // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
John Reck1bcacfd2017-11-03 10:12:19 -0700186 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700187 glDisable(GL_FRAMEBUFFER_SRGB_EXT);
188 }
John Reck3b202512014-06-23 13:13:08 -0700189}
190
191void RenderState::resumeFromFunctorInvoke() {
John Reck1bcacfd2017-11-03 10:12:19 -0700192 if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
Romain Guy253f2c22016-09-28 17:34:42 -0700193 glEnable(GL_FRAMEBUFFER_SRGB_EXT);
194 }
195
John Reck3b202512014-06-23 13:13:08 -0700196 glViewport(0, 0, mViewportWidth, mViewportHeight);
197 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
198 debugOverdraw(false, false);
199
200 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
201
Chris Craik65fe5ee2015-01-26 18:06:29 -0800202 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800203 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700204
Chris Craik44eb2c02015-01-29 09:45:09 -0800205 mCaches->textureState().activateTexture(0);
206 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700207}
208
209void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700210 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700211 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800212 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800213 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700214 }
215 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800216 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700217 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800218 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700219 }
220 }
221}
222
sergeyv3e9999b2017-01-19 15:37:02 -0800223static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) {
224 layerUpdater->destroyLayer();
225}
226
227void RenderState::destroyLayersInUpdater() {
228 std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater);
229}
230
John Reck0e89e2b2014-10-31 14:49:06 -0700231void RenderState::postDecStrong(VirtualLightRefBase* object) {
John Recka55b5d62016-01-14 15:09:10 -0800232 if (pthread_equal(mThreadId, pthread_self())) {
233 object->decStrong(nullptr);
234 } else {
John Reckf8441e62017-10-23 13:10:41 -0700235 mRenderThread.queue().post([object]() { object->decStrong(nullptr); });
John Recka55b5d62016-01-14 15:09:10 -0800236 }
John Reck0e89e2b2014-10-31 14:49:06 -0700237}
238
Chris Craik6c15ffa2015-02-02 13:50:55 -0800239///////////////////////////////////////////////////////////////////////////////
240// Render
241///////////////////////////////////////////////////////////////////////////////
242
Arun530a2b42017-01-23 12:47:57 +0000243void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700244 bool overrideDisableBlending) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800245 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800246 const Glop::Mesh::Vertices& vertices = mesh.vertices;
247 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800248 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800249
John Reck975591a2016-01-22 16:28:07 -0800250 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800251
Chris Craik0519c8102015-02-11 13:17:06 -0800252 // ---------------------------------------------
253 // ---------- Program + uniform setup ----------
254 // ---------------------------------------------
255 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800256
Chris Craik0519c8102015-02-11 13:17:06 -0800257 if (fill.colorEnabled) {
258 fill.program->setColor(fill.color);
259 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800260
John Reck1bcacfd2017-11-03 10:12:19 -0700261 fill.program->set(orthoMatrix, glop.transform.modelView, glop.transform.meshTransform(),
262 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800263
Chris Craik0519c8102015-02-11 13:17:06 -0800264 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700265 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800266 const FloatColor& color = fill.filter.color;
John Reck1bcacfd2017-11-03 10:12:19 -0700267 glUniform4f(mCaches->program().getUniform("colorBlend"), color.r, color.g, color.b,
268 color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700269 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800270 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700271 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800272 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
John Reck1bcacfd2017-11-03 10:12:19 -0700273 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800274 }
275
Chris Craik0519c8102015-02-11 13:17:06 -0800276 // Round rect clipping uniforms
277 if (glop.roundRectClipState) {
278 // TODO: avoid query, and cache values (or RRCS ptr) in program
279 const RoundRectClipState* state = glop.roundRectClipState;
280 const Rect& innerRect = state->innerRect;
Chris Craik0519c8102015-02-11 13:17:06 -0800281
282 // add half pixel to round out integer rect space to cover pixel centers
283 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000284
285 // Divide by the radius to simplify the calculations in the fragment shader
286 // roundRectPos is also passed from vertex shader relative to top/left & radius
287 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
John Reck1bcacfd2017-11-03 10:12:19 -0700288 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
289 (innerRect.right - innerRect.left) / roundedOutRadius,
290 (innerRect.bottom - innerRect.top) / roundedOutRadius);
Arun06e9f322017-01-23 11:59:21 +0000291
John Reck1bcacfd2017-11-03 10:12:19 -0700292 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), 1, GL_FALSE,
293 &state->matrix.data[0]);
Arun06e9f322017-01-23 11:59:21 +0000294
John Reck1bcacfd2017-11-03 10:12:19 -0700295 glUniform1f(fill.program->getUniform("roundRectRadius"), roundedOutRadius);
Chris Craik0519c8102015-02-11 13:17:06 -0800296 }
Chris Craikef250742015-02-25 17:16:16 -0800297
John Reck975591a2016-01-22 16:28:07 -0800298 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800299
Chris Craik117bdbc2015-02-05 10:12:38 -0800300 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800301 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800302 // --------------------------------
303 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800304 meshState().bindMeshBuffer(vertices.bufferObject);
305 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800306
307 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800308 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800309
Chris Craik138c21f2016-04-28 16:59:42 -0700310 // texture
311 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800312 const Glop::Fill::TextureData& texture = fill.texture;
313 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800314 mCaches->textureState().activateTexture(0);
315
sergeyv2a38c422016-10-25 15:21:50 -0700316 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800317 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700318 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800319 }
Chris Craik26bf3422015-02-26 16:28:17 -0800320 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700321 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800322 }
Chris Craik0519c8102015-02-11 13:17:06 -0800323
Chris Craik26bf3422015-02-26 16:28:17 -0800324 if (texture.textureTransform) {
John Reck1bcacfd2017-11-03 10:12:19 -0700325 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1, GL_FALSE,
326 &texture.textureTransform->data[0]);
Chris Craik26bf3422015-02-26 16:28:17 -0800327 }
Chris Craik138c21f2016-04-28 16:59:42 -0700328 }
329
330 // vertex attributes (tex coord, color, alpha)
331 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
332 meshState().enableTexCoordsVertexArray();
333 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800334 } else {
335 meshState().disableTexCoordsVertexArray();
336 }
Chris Craikef250742015-02-25 17:16:16 -0800337 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700338 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800339 colorLocation = fill.program->getAttrib("colors");
340 glEnableVertexAttribArray(colorLocation);
John Reck1bcacfd2017-11-03 10:12:19 -0700341 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride,
342 vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800343 }
Chris Craikef250742015-02-25 17:16:16 -0800344 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700345 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800346 // NOTE: alpha vertex position is computed assuming no VBO
John Reck1bcacfd2017-11-03 10:12:19 -0700347 const void* alphaCoords = ((const GLbyte*)vertices.position) + kVertexAlphaOffset;
Chris Craikef250742015-02-25 17:16:16 -0800348 alphaLocation = fill.program->getAttrib("vtxAlpha");
349 glEnableVertexAttribArray(alphaLocation);
350 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800351 }
Chris Craik922d3a72015-02-13 17:47:21 -0800352 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700353 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800354
John Reck975591a2016-01-22 16:28:07 -0800355 GL_CHECKPOINT(MODERATE);
John Reck1bcacfd2017-11-03 10:12:19 -0700356 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType)
357 ? fill.skiaShaderData.bitmapData.bitmapTexture
358 : nullptr;
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900359 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800360
Romain Guycaaaa662017-03-27 00:40:21 -0700361 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
362 // which means the color space conversion applies to the shader's bitmap
363 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
364 if (colorSpaceTexture != nullptr) {
365 if (colorSpaceTexture->hasColorSpaceConversion()) {
366 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
John Reck1bcacfd2017-11-03 10:12:19 -0700367 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1, GL_FALSE,
368 connector->getTransform().asArray());
Romain Guycaaaa662017-03-27 00:40:21 -0700369 }
370
371 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
372 if (transferFunction != TransferFunctionType::None) {
373 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
374 const ColorSpace& source = connector->getSource();
375
376 switch (transferFunction) {
377 case TransferFunctionType::None:
378 break;
379 case TransferFunctionType::Full:
380 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
John Reck1bcacfd2017-11-03 10:12:19 -0700381 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700382 break;
383 case TransferFunctionType::Limited:
384 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
John Reck1bcacfd2017-11-03 10:12:19 -0700385 reinterpret_cast<const float*>(&source.getTransferParameters().g));
Romain Guycaaaa662017-03-27 00:40:21 -0700386 break;
387 case TransferFunctionType::Gamma:
388 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
John Reck1bcacfd2017-11-03 10:12:19 -0700389 source.getTransferParameters().g);
Romain Guycaaaa662017-03-27 00:40:21 -0700390 break;
391 }
392 }
393 }
394
Chris Craik117bdbc2015-02-05 10:12:38 -0800395 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800396 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800397 // ------------------------------------
Arun530a2b42017-01-23 12:47:57 +0000398 if (CC_UNLIKELY(overrideDisableBlending)) {
399 blend().setFactors(GL_ZERO, GL_ZERO);
400 } else {
401 blend().setFactors(glop.blend.src, glop.blend.dst);
402 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800403
John Reck975591a2016-01-22 16:28:07 -0800404 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800405
Chris Craik117bdbc2015-02-05 10:12:38 -0800406 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800407 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800408 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800409 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800410 // Since the indexed quad list is of limited length, we loop over
411 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800412 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800413 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800414 while (elementsCount > 0) {
John Reck1bcacfd2017-11-03 10:12:19 -0700415 GLsizei drawCount = std::min(elementsCount, (GLsizei)kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000416 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800417 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700418 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
John Reck1bcacfd2017-11-03 10:12:19 -0700419 meshState().bindTexCoordsVertexPointer(vertexData + kMeshTextureOffset,
420 vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800421 }
422
Arunb0a94772017-01-23 11:41:06 +0000423 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700424 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount - 1, drawCount,
425 GL_UNSIGNED_SHORT, nullptr);
Arunb0a94772017-01-23 11:41:06 +0000426 } else {
427 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
428 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800429 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000430 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800431 }
Chris Craikef250742015-02-25 17:16:16 -0800432 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000433 if (mCaches->extensions().getMajorGlVersion() >= 3) {
John Reck1bcacfd2017-11-03 10:12:19 -0700434 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine
435 // the min/max index values)
436 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount - 1, mesh.elementCount,
437 GL_UNSIGNED_SHORT, indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000438 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700439 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT,
440 indices.indices);
Arunb0a94772017-01-23 11:41:06 +0000441 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800442 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800443 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800444 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800445
John Reck975591a2016-01-22 16:28:07 -0800446 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800447
Chris Craik2ab95d72015-02-06 15:25:51 -0800448 // -----------------------------------
449 // ---------- Mesh teardown ----------
450 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700451 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800452 glDisableVertexAttribArray(alphaLocation);
453 }
Chris Craik53e51e42015-06-01 10:35:35 -0700454 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800455 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800456 }
John Reck9372ac32016-01-19 11:46:52 -0800457
John Reck975591a2016-01-22 16:28:07 -0800458 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800459}
460
461void RenderState::dump() {
462 blend().dump();
463 meshState().dump();
464 scissor().dump();
465 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800466}
467
John Reck3b202512014-06-23 13:13:08 -0700468} /* namespace uirenderer */
469} /* namespace android */