BakedOpRenderer.cpp revision 6fe991e5e76f9af9dab960100d5768d96d5f4daa
1/*
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
17#include "BakedOpRenderer.h"
18
19#include "Caches.h"
20#include "Glop.h"
21#include "GlopBuilder.h"
22#include "renderstate/RenderState.h"
23#include "utils/GLUtils.h"
24
25namespace android {
26namespace uirenderer {
27
28Texture* BakedOpRenderer::Info::getTexture(const SkBitmap* bitmap) {
29    Texture* texture = renderState.assetAtlas().getEntryTexture(bitmap);
30    if (!texture) {
31        return caches.textureCache.get(bitmap);
32    }
33    return texture;
34}
35
36void BakedOpRenderer::Info::renderGlop(const BakedOpState& state, const Glop& glop) {
37    bool useScissor = state.computedState.clipSideFlags != OpClipSideFlags::None;
38    renderState.scissor().setEnabled(useScissor);
39    if (useScissor) {
40        const Rect& clip = state.computedState.clipRect;
41        renderState.scissor().set(clip.left, viewportHeight - clip.bottom,
42            clip.getWidth(), clip.getHeight());
43    }
44    renderState.render(glop, orthoMatrix);
45    didDraw = true;
46}
47
48void BakedOpRenderer::startFrame(Info& info) {
49    info.renderState.setViewport(info.viewportWidth, info.viewportHeight);
50    info.renderState.blend().syncEnabled();
51    Caches::getInstance().clearGarbage();
52
53    if (!info.opaque) {
54        // TODO: partial invalidate!
55        info.renderState.scissor().setEnabled(false);
56        glClear(GL_COLOR_BUFFER_BIT);
57        info.didDraw = true;
58    }
59}
60void BakedOpRenderer::endFrame(Info& info) {
61    info.caches.pathCache.trim();
62    info.caches.tessellationCache.trim();
63
64#if DEBUG_OPENGL
65    GLUtils::dumpGLErrors();
66#endif
67
68#if DEBUG_MEMORY_USAGE
69    info.caches.dumpMemoryUsage();
70#else
71    if (Properties::debugLevel & kDebugMemory) {
72        info.caches.dumpMemoryUsage();
73    }
74#endif
75}
76
77void BakedOpRenderer::onRenderNodeOp(Info&, const RenderNodeOp&, const BakedOpState&) {
78    LOG_ALWAYS_FATAL("unsupported operation");
79}
80
81void BakedOpRenderer::onBitmapOp(Info& info, const BitmapOp& op, const BakedOpState& state) {
82    info.caches.textureState().activateTexture(0); // TODO: should this be automatic, and/or elsewhere?
83    Texture* texture = info.getTexture(op.bitmap);
84    if (!texture) return;
85    const AutoTexture autoCleanup(texture);
86
87    const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType)
88            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
89    Glop glop;
90    GlopBuilder(info.renderState, info.caches, &glop)
91            .setRoundRectClipState(state.roundRectClipState)
92            .setMeshTexturedUnitQuad(texture->uvMapper)
93            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
94            .setTransform(state.computedState.transform, TransformFlags::None)
95            .setModelViewMapUnitToRectSnap(Rect(0, 0, texture->width, texture->height))
96            .build();
97    info.renderGlop(state, glop);
98}
99
100void BakedOpRenderer::onRectOp(Info& info, const RectOp& op, const BakedOpState& state) {
101    Glop glop;
102    GlopBuilder(info.renderState, info.caches, &glop)
103            .setRoundRectClipState(state.roundRectClipState)
104            .setMeshUnitQuad()
105            .setFillPaint(*op.paint, state.alpha)
106            .setTransform(state.computedState.transform, TransformFlags::None)
107            .setModelViewMapUnitToRect(op.unmappedBounds)
108            .build();
109    info.renderGlop(state, glop);
110}
111
112void BakedOpRenderer::onSimpleRectsOp(Info& info, const SimpleRectsOp& op, const BakedOpState& state) {
113    Glop glop;
114    GlopBuilder(info.renderState, info.caches, &glop)
115            .setRoundRectClipState(state.roundRectClipState)
116            .setMeshIndexedQuads(&op.vertices[0], op.vertexCount / 4)
117            .setFillPaint(*op.paint, state.alpha)
118            .setTransform(state.computedState.transform, TransformFlags::None)
119            .setModelViewOffsetRect(0, 0, op.unmappedBounds)
120            .build();
121    info.renderGlop(state, glop);
122}
123
124void BakedOpRenderer::onBeginLayerOp(Info& info, const BeginLayerOp& op, const BakedOpState& state) {
125    LOG_ALWAYS_FATAL("unsupported operation");
126}
127
128void BakedOpRenderer::onEndLayerOp(Info& info, const EndLayerOp& op, const BakedOpState& state) {
129    LOG_ALWAYS_FATAL("unsupported operation");
130}
131
132void BakedOpRenderer::onLayerOp(Info& info, const LayerOp& op, const BakedOpState& state) {
133    LOG_ALWAYS_FATAL("unsupported operation");
134}
135
136} // namespace uirenderer
137} // namespace android
138