blob: ae8928ea84615b69d0686b6e02a56c4ce617e027 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
2 * Copyright (C) 2015 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 */
16
Chris Craik419a1e72016-03-08 16:24:12 -080017#pragma once
Chris Craikb565df12015-10-05 13:00:52 -070018
19#include "BakedOpState.h"
20#include "Matrix.h"
Chris Craik7435eb12016-01-07 17:41:40 -080021#include "utils/Macros.h"
Chris Craikb565df12015-10-05 13:00:52 -070022
23namespace android {
24namespace uirenderer {
25
26class Caches;
27struct Glop;
Chris Craik818c9fb2015-10-23 14:33:42 -070028class Layer;
Mike Reed8cafcc62018-05-03 11:32:46 -040029struct RenderBuffer;
Chris Craikb565df12015-10-05 13:00:52 -070030class RenderState;
Chris Craike4db79d2015-12-22 16:32:23 -080031struct ClipBase;
Chris Craikb565df12015-10-05 13:00:52 -070032
Chris Craik5854b342015-10-26 15:49:56 -070033/**
Chris Craik5854b342015-10-26 15:49:56 -070034 * Main rendering manager for a collection of work - one frame + any contained FBOs.
35 *
36 * Manages frame and FBO lifecycle, binding the GL framebuffer as appropriate. This is the only
37 * place where FBOs are bound, created, and destroyed.
38 *
39 * All rendering operations will be sent by the Dispatcher, a collection of static methods,
40 * which has intentionally limited access to the renderer functionality.
41 */
Chris Craikb565df12015-10-05 13:00:52 -070042class BakedOpRenderer {
43public:
Chris Craik419a1e72016-03-08 16:24:12 -080044 typedef void (*GlopReceiver)(BakedOpRenderer&, const Rect*, const ClipBase*, const Glop&);
Chris Craik98787e62015-11-13 10:55:30 -080045 /**
46 * Position agnostic shadow lighting info. Used with all shadow ops in scene.
47 */
48 struct LightInfo {
Chris Craik6e068c012016-01-15 16:15:30 -080049 LightInfo() : LightInfo(0, 0) {}
John Reck1bcacfd2017-11-03 10:12:19 -070050 LightInfo(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha)
51 : ambientShadowAlpha(ambientShadowAlpha), spotShadowAlpha(spotShadowAlpha) {}
John Reck7db5ffb2016-01-15 13:17:09 -080052 uint8_t ambientShadowAlpha;
53 uint8_t spotShadowAlpha;
Chris Craik98787e62015-11-13 10:55:30 -080054 };
55
Romain Guy07ae5052017-06-13 18:25:32 -070056 BakedOpRenderer(Caches& caches, RenderState& renderState, bool opaque, bool wideColorGamut,
John Reck1bcacfd2017-11-03 10:12:19 -070057 const LightInfo& lightInfo)
Chris Craik419a1e72016-03-08 16:24:12 -080058 : mGlopReceiver(DefaultGlopReceiver)
59 , mRenderState(renderState)
Chris Craik5854b342015-10-26 15:49:56 -070060 , mCaches(caches)
Chris Craik98787e62015-11-13 10:55:30 -080061 , mOpaque(opaque)
Romain Guy07ae5052017-06-13 18:25:32 -070062 , mWideColorGamut(wideColorGamut)
John Reck1bcacfd2017-11-03 10:12:19 -070063 , mLightInfo(lightInfo) {}
Chris Craikb565df12015-10-05 13:00:52 -070064
Chris Craik5854b342015-10-26 15:49:56 -070065 RenderState& renderState() { return mRenderState; }
66 Caches& caches() { return mCaches; }
Chris Craik818c9fb2015-10-23 14:33:42 -070067
Chris Craik98787e62015-11-13 10:55:30 -080068 void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect);
Chris Craike4db79d2015-12-22 16:32:23 -080069 void endFrame(const Rect& repaintRect);
Chris Craik7435eb12016-01-07 17:41:40 -080070 WARN_UNUSED_RESULT OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height);
Chris Craik74af6e22016-04-05 13:18:56 -070071 void recycleTemporaryLayer(OffscreenBuffer* offscreenBuffer);
Chris Craik98787e62015-11-13 10:55:30 -080072 void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect);
Chris Craik5854b342015-10-26 15:49:56 -070073 void endLayer();
Chris Craik7435eb12016-01-07 17:41:40 -080074 WARN_UNUSED_RESULT OffscreenBuffer* copyToLayer(const Rect& area);
Chris Craikb565df12015-10-05 13:00:52 -070075
sergeyvec4a4b12016-10-20 18:39:04 -070076 Texture* getTexture(Bitmap* bitmap);
Chris Craike29ce6f2015-12-10 16:25:13 -080077 const LightInfo& getLightInfo() const { return mLightInfo; }
Chris Craikb565df12015-10-05 13:00:52 -070078
Chris Craik15c3f192015-12-03 12:16:56 -080079 void renderGlop(const BakedOpState& state, const Glop& glop) {
John Reck1bcacfd2017-11-03 10:12:19 -070080 renderGlop(&state.computedState.clippedBounds, state.computedState.getClipIfNeeded(), glop);
Chris Craik15c3f192015-12-03 12:16:56 -080081 }
Chris Craike29ce6f2015-12-10 16:25:13 -080082 void renderFunctor(const FunctorOp& op, const BakedOpState& state);
Chris Craik15c3f192015-12-03 12:16:56 -080083
Chris Craik419a1e72016-03-08 16:24:12 -080084 void renderGlop(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop) {
85 mGlopReceiver(*this, dirtyBounds, clip, glop);
86 }
Chris Craik15c3f192015-12-03 12:16:56 -080087 bool offscreenRenderTarget() { return mRenderTarget.offscreenBuffer != nullptr; }
88 void dirtyRenderTarget(const Rect& dirtyRect);
Chris Craike29ce6f2015-12-10 16:25:13 -080089 bool didDraw() const { return mHasDrawn; }
Chris Craik1dfa0702016-03-04 15:59:24 -080090
91 uint32_t getViewportWidth() const { return mRenderTarget.viewportWidth; }
92 uint32_t getViewportHeight() const { return mRenderTarget.viewportHeight; }
93
94 // simple draw methods, to be used for end frame decoration
95 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -070096 float ltrb[4] = {left, top, right, bottom};
Chris Craik1dfa0702016-03-04 15:59:24 -080097 drawRects(ltrb, 4, paint);
98 }
99 void drawRects(const float* rects, int count, const SkPaint* paint);
John Reck1bcacfd2017-11-03 10:12:19 -0700100
Chris Craik419a1e72016-03-08 16:24:12 -0800101protected:
102 GlopReceiver mGlopReceiver;
John Reck1bcacfd2017-11-03 10:12:19 -0700103
Chris Craik5854b342015-10-26 15:49:56 -0700104private:
Chris Craik419a1e72016-03-08 16:24:12 -0800105 static void DefaultGlopReceiver(BakedOpRenderer& renderer, const Rect* dirtyBounds,
John Reck1bcacfd2017-11-03 10:12:19 -0700106 const ClipBase* clip, const Glop& glop) {
Chris Craik419a1e72016-03-08 16:24:12 -0800107 renderer.renderGlopImpl(dirtyBounds, clip, glop);
108 }
109 void renderGlopImpl(const Rect* dirtyBounds, const ClipBase* clip, const Glop& glop);
Chris Craik5854b342015-10-26 15:49:56 -0700110 void setViewport(uint32_t width, uint32_t height);
Chris Craik98787e62015-11-13 10:55:30 -0800111 void clearColorBuffer(const Rect& clearRect);
Chris Craike4db79d2015-12-22 16:32:23 -0800112 void prepareRender(const Rect* dirtyBounds, const ClipBase* clip);
113 void setupStencilRectList(const ClipBase* clip);
114 void setupStencilRegion(const ClipBase* clip);
115 void setupStencilQuads(std::vector<Vertex>& quadVertices, int incrementThreshold);
Chris Craikb565df12015-10-05 13:00:52 -0700116
Chris Craik5854b342015-10-26 15:49:56 -0700117 RenderState& mRenderState;
118 Caches& mCaches;
119 bool mOpaque;
Romain Guy07ae5052017-06-13 18:25:32 -0700120 bool mWideColorGamut;
Chris Craik5854b342015-10-26 15:49:56 -0700121 bool mHasDrawn = false;
Chris Craikb565df12015-10-05 13:00:52 -0700122
Chris Craik5854b342015-10-26 15:49:56 -0700123 // render target state - setup by start/end layer/frame
124 // only valid to use in between start/end pairs.
125 struct {
Chris Craike5b50192016-01-04 15:09:19 -0800126 // If not drawing to a layer: fbo = 0, offscreenBuffer = null,
127 // Otherwise these refer to currently painting layer's state
Chris Craik5854b342015-10-26 15:49:56 -0700128 GLuint frameBufferId = 0;
129 OffscreenBuffer* offscreenBuffer = nullptr;
Chris Craike5b50192016-01-04 15:09:19 -0800130
131 // Used when drawing to a layer and using stencil clipping. otherwise null.
132 RenderBuffer* stencil = nullptr;
133
134 // value representing the ClipRectList* or ClipRegion* currently stored in
135 // the stencil of the current render target
136 const ClipBase* lastStencilClip = nullptr;
137
138 // Size of renderable region in current render target - for layers, may not match actual
139 // bounds of FBO texture. offscreenBuffer->texture has this information.
Chris Craik818c9fb2015-10-23 14:33:42 -0700140 uint32_t viewportWidth = 0;
141 uint32_t viewportHeight = 0;
Chris Craike5b50192016-01-04 15:09:19 -0800142
Chris Craikb565df12015-10-05 13:00:52 -0700143 Matrix4 orthoMatrix;
Chris Craik5854b342015-10-26 15:49:56 -0700144 } mRenderTarget;
Chris Craik98787e62015-11-13 10:55:30 -0800145
146 const LightInfo mLightInfo;
Chris Craik5854b342015-10-26 15:49:56 -0700147};
Chris Craikb565df12015-10-05 13:00:52 -0700148
John Reck1bcacfd2017-11-03 10:12:19 -0700149}; // namespace uirenderer
150}; // namespace android