blob: 6c606f738aaf216d949706ce1c5bdc6dcfe76dd5 [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() {
Greg Daniel45ec62b2017-01-04 14:27:00 -0500108 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
Derek Sollenberger7a4216b2017-10-25 09:52:23 -0400109 destroyLayersInUpdater();
Greg Daniel45ec62b2017-01-04 14:27:00 -0500110 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
Arun530a2b42017-01-23 12:47:57 +0000265void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix,
266 bool overrideDisableBlending) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800267 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800268 const Glop::Mesh::Vertices& vertices = mesh.vertices;
269 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800270 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800271
John Reck975591a2016-01-22 16:28:07 -0800272 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800273
Chris Craik0519c8102015-02-11 13:17:06 -0800274 // ---------------------------------------------
275 // ---------- Program + uniform setup ----------
276 // ---------------------------------------------
277 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800278
Chris Craik0519c8102015-02-11 13:17:06 -0800279 if (fill.colorEnabled) {
280 fill.program->setColor(fill.color);
281 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800282
Chris Craik12efe642015-09-28 15:52:12 -0700283 fill.program->set(orthoMatrix,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800284 glop.transform.modelView,
Chris Craik53e51e42015-06-01 10:35:35 -0700285 glop.transform.meshTransform(),
286 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800287
Chris Craik0519c8102015-02-11 13:17:06 -0800288 // Color filter uniforms
Chris Craikb9ce116d2015-08-20 15:14:06 -0700289 if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
Chris Craikef250742015-02-25 17:16:16 -0800290 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800291 glUniform4f(mCaches->program().getUniform("colorBlend"),
292 color.r, color.g, color.b, color.a);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700293 } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800294 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800295 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800296 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800297 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800298 }
299
Chris Craik0519c8102015-02-11 13:17:06 -0800300 // Round rect clipping uniforms
301 if (glop.roundRectClipState) {
302 // TODO: avoid query, and cache values (or RRCS ptr) in program
303 const RoundRectClipState* state = glop.roundRectClipState;
304 const Rect& innerRect = state->innerRect;
Chris Craik0519c8102015-02-11 13:17:06 -0800305
306 // add half pixel to round out integer rect space to cover pixel centers
307 float roundedOutRadius = state->radius + 0.5f;
Arun06e9f322017-01-23 11:59:21 +0000308
309 // Divide by the radius to simplify the calculations in the fragment shader
310 // roundRectPos is also passed from vertex shader relative to top/left & radius
311 glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
312 innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
313 (innerRect.right - innerRect.left) / roundedOutRadius,
314 (innerRect.bottom - innerRect.top) / roundedOutRadius);
315
316 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
317 1, GL_FALSE, &state->matrix.data[0]);
318
Chris Craik0519c8102015-02-11 13:17:06 -0800319 glUniform1f(fill.program->getUniform("roundRectRadius"),
320 roundedOutRadius);
321 }
Chris Craikef250742015-02-25 17:16:16 -0800322
John Reck975591a2016-01-22 16:28:07 -0800323 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800324
Chris Craik117bdbc2015-02-05 10:12:38 -0800325 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800326 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800327 // --------------------------------
328 // vertices
Chris Craik1b7db402016-02-24 18:25:32 -0800329 meshState().bindMeshBuffer(vertices.bufferObject);
330 meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800331
332 // indices
Chris Craik1b7db402016-02-24 18:25:32 -0800333 meshState().bindIndicesBuffer(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800334
Chris Craik138c21f2016-04-28 16:59:42 -0700335 // texture
336 if (fill.texture.texture != nullptr) {
Chris Craik26bf3422015-02-26 16:28:17 -0800337 const Glop::Fill::TextureData& texture = fill.texture;
338 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800339 mCaches->textureState().activateTexture(0);
340
sergeyv2a38c422016-10-25 15:21:50 -0700341 mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id());
Chris Craik26bf3422015-02-26 16:28:17 -0800342 if (texture.clamp != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700343 texture.texture->setWrap(texture.clamp, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800344 }
Chris Craik26bf3422015-02-26 16:28:17 -0800345 if (texture.filter != GL_INVALID_ENUM) {
sergeyv2a38c422016-10-25 15:21:50 -0700346 texture.texture->setFilter(texture.filter, false, false);
Chris Craik30036092015-02-12 10:41:39 -0800347 }
Chris Craik0519c8102015-02-11 13:17:06 -0800348
Chris Craik26bf3422015-02-26 16:28:17 -0800349 if (texture.textureTransform) {
350 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
351 GL_FALSE, &texture.textureTransform->data[0]);
352 }
Chris Craik138c21f2016-04-28 16:59:42 -0700353 }
354
355 // vertex attributes (tex coord, color, alpha)
356 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
357 meshState().enableTexCoordsVertexArray();
358 meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800359 } else {
360 meshState().disableTexCoordsVertexArray();
361 }
Chris Craikef250742015-02-25 17:16:16 -0800362 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700363 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800364 colorLocation = fill.program->getAttrib("colors");
365 glEnableVertexAttribArray(colorLocation);
366 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800367 }
Chris Craikef250742015-02-25 17:16:16 -0800368 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700369 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800370 // NOTE: alpha vertex position is computed assuming no VBO
371 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
372 alphaLocation = fill.program->getAttrib("vtxAlpha");
373 glEnableVertexAttribArray(alphaLocation);
374 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800375 }
Chris Craik922d3a72015-02-13 17:47:21 -0800376 // Shader uniforms
Romain Guy253f2c22016-09-28 17:34:42 -0700377 SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight);
Chris Craik922d3a72015-02-13 17:47:21 -0800378
John Reck975591a2016-01-22 16:28:07 -0800379 GL_CHECKPOINT(MODERATE);
Dohyun Leec5a3efd2016-01-21 13:46:21 +0900380 Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
381 fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
382 const AutoTexture autoCleanup(texture);
John Reck9372ac32016-01-19 11:46:52 -0800383
Romain Guycaaaa662017-03-27 00:40:21 -0700384 // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
385 // which means the color space conversion applies to the shader's bitmap
386 Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
387 if (colorSpaceTexture != nullptr) {
388 if (colorSpaceTexture->hasColorSpaceConversion()) {
389 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
390 glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
391 GL_FALSE, connector->getTransform().asArray());
392 }
393
394 TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
395 if (transferFunction != TransferFunctionType::None) {
396 const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
397 const ColorSpace& source = connector->getSource();
398
399 switch (transferFunction) {
400 case TransferFunctionType::None:
401 break;
402 case TransferFunctionType::Full:
403 glUniform1fv(fill.program->getUniform("transferFunction"), 7,
404 reinterpret_cast<const float*>(&source.getTransferParameters().g));
405 break;
406 case TransferFunctionType::Limited:
407 glUniform1fv(fill.program->getUniform("transferFunction"), 5,
408 reinterpret_cast<const float*>(&source.getTransferParameters().g));
409 break;
410 case TransferFunctionType::Gamma:
411 glUniform1f(fill.program->getUniform("transferFunctionGamma"),
412 source.getTransferParameters().g);
413 break;
414 }
415 }
416 }
417
Chris Craik117bdbc2015-02-05 10:12:38 -0800418 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800419 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800420 // ------------------------------------
Arun530a2b42017-01-23 12:47:57 +0000421 if (CC_UNLIKELY(overrideDisableBlending)) {
422 blend().setFactors(GL_ZERO, GL_ZERO);
423 } else {
424 blend().setFactors(glop.blend.src, glop.blend.dst);
425 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800426
John Reck975591a2016-01-22 16:28:07 -0800427 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800428
Chris Craik117bdbc2015-02-05 10:12:38 -0800429 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800430 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800431 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800432 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800433 // Since the indexed quad list is of limited length, we loop over
434 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800435 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800436 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800437 while (elementsCount > 0) {
Chris Craik9db58c02015-08-19 15:19:18 -0700438 GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Arunb0a94772017-01-23 11:41:06 +0000439 GLsizei vertexCount = (drawCount / 6) * 4;
Chris Craik1b7db402016-02-24 18:25:32 -0800440 meshState().bindPositionVertexPointer(vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700441 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik1b7db402016-02-24 18:25:32 -0800442 meshState().bindTexCoordsVertexPointer(
Chris Craikef250742015-02-25 17:16:16 -0800443 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800444 }
445
Arunb0a94772017-01-23 11:41:06 +0000446 if (mCaches->extensions().getMajorGlVersion() >= 3) {
447 glDrawRangeElements(mesh.primitiveMode, 0, vertexCount-1, drawCount, GL_UNSIGNED_SHORT, nullptr);
448 } else {
449 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
450 }
Chris Craik2ab95d72015-02-06 15:25:51 -0800451 elementsCount -= drawCount;
Arunb0a94772017-01-23 11:41:06 +0000452 vertexData += vertexCount * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800453 }
Chris Craikef250742015-02-25 17:16:16 -0800454 } else if (indices.bufferObject || indices.indices) {
Arunb0a94772017-01-23 11:41:06 +0000455 if (mCaches->extensions().getMajorGlVersion() >= 3) {
456 // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine the min/max index values)
457 glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount-1, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
458 } else {
459 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
460 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800461 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800462 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800463 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800464
John Reck975591a2016-01-22 16:28:07 -0800465 GL_CHECKPOINT(MODERATE);
John Reck9372ac32016-01-19 11:46:52 -0800466
Chris Craik2ab95d72015-02-06 15:25:51 -0800467 // -----------------------------------
468 // ---------- Mesh teardown ----------
469 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700470 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800471 glDisableVertexAttribArray(alphaLocation);
472 }
Chris Craik53e51e42015-06-01 10:35:35 -0700473 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800474 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800475 }
John Reck9372ac32016-01-19 11:46:52 -0800476
John Reck975591a2016-01-22 16:28:07 -0800477 GL_CHECKPOINT(MODERATE);
Chris Craik117bdbc2015-02-05 10:12:38 -0800478}
479
480void RenderState::dump() {
481 blend().dump();
482 meshState().dump();
483 scissor().dump();
484 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800485}
486
John Reck3b202512014-06-23 13:13:08 -0700487} /* namespace uirenderer */
488} /* namespace android */