blob: 93807a5476e62a0f5a1078cb493810249bd96fdf [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -04001/*
2 * Copyright (C) 2016 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#pragma once
17
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <SkLiteRecorder.h>
19#include "ReorderBarrierDrawables.h"
Stan Iliev021693b2016-10-17 16:26:15 -040020#include "SkiaCanvas.h"
21#include "SkiaDisplayList.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022
23namespace android {
24namespace uirenderer {
25namespace skiapipeline {
26
27/**
28 * A SkiaCanvas implementation that records drawing operations for deferred rendering backed by a
29 * SkLiteRecorder and a SkiaDisplayList.
30 */
31class SkiaRecordingCanvas : public SkiaCanvas {
John Reck1bcacfd2017-11-03 10:12:19 -070032public:
Stan Iliev021693b2016-10-17 16:26:15 -040033 explicit SkiaRecordingCanvas(uirenderer::RenderNode* renderNode, int width, int height) {
34 initDisplayList(renderNode, width, height);
35 }
36
37 virtual void setBitmap(const SkBitmap& bitmap) override {
38 LOG_ALWAYS_FATAL("DisplayListCanvas is not backed by a bitmap.");
39 }
40
41 virtual void resetRecording(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -070042 uirenderer::RenderNode* renderNode) override {
Stan Iliev021693b2016-10-17 16:26:15 -040043 initDisplayList(renderNode, width, height);
44 }
45
46 virtual uirenderer::DisplayList* finishRecording() override;
47
John Reck1bcacfd2017-11-03 10:12:19 -070048 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override;
49 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override;
50 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
51 float srcBottom, float dstLeft, float dstTop, float dstRight,
52 float dstBottom, const SkPaint* paint) override;
Stan Iliev021693b2016-10-17 16:26:15 -040053 virtual void drawNinePatch(Bitmap& hwuiBitmap, const android::Res_png_9patch& chunk,
John Reck1bcacfd2017-11-03 10:12:19 -070054 float dstLeft, float dstTop, float dstRight, float dstBottom,
55 const SkPaint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -050056 virtual double drawAnimatedImage(AnimatedImageDrawable* animatedImage) override;
Stan Iliev021693b2016-10-17 16:26:15 -040057
58 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -070059 uirenderer::CanvasPropertyPrimitive* top,
60 uirenderer::CanvasPropertyPrimitive* right,
61 uirenderer::CanvasPropertyPrimitive* bottom,
62 uirenderer::CanvasPropertyPrimitive* rx,
63 uirenderer::CanvasPropertyPrimitive* ry,
64 uirenderer::CanvasPropertyPaint* paint) override;
Stan Iliev021693b2016-10-17 16:26:15 -040065 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -070066 uirenderer::CanvasPropertyPrimitive* y,
67 uirenderer::CanvasPropertyPrimitive* radius,
68 uirenderer::CanvasPropertyPaint* paint) override;
Stan Iliev021693b2016-10-17 16:26:15 -040069
70 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
71
72 virtual void insertReorderBarrier(bool enableReorder) override;
73 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
74 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
75 virtual void callDrawGLFunction(Functor* functor,
76 uirenderer::GlFunctorLifecycleListener* listener) override;
77
78private:
79 SkLiteRecorder mRecorder;
80 std::unique_ptr<SkiaDisplayList> mDisplayList;
Stan Iliev021693b2016-10-17 16:26:15 -040081 StartReorderBarrierDrawable* mCurrentBarrier;
82
83 /**
84 * A new SkiaDisplayList is created or recycled if available.
85 *
86 * @param renderNode is optional and used to recycle an old display list.
87 * @param width used to calculate recording bounds.
88 * @param height used to calculate recording bounds.
89 */
90 void initDisplayList(uirenderer::RenderNode* renderNode, int width, int height);
Stan Ilievcebb6472018-03-02 13:20:26 -050091
92 inline static const SkPaint* bitmapPaint(const SkPaint* origPaint, SkPaint* tmpPaint,
93 sk_sp<SkColorFilter> colorSpaceFilter) {
94 bool fixBlending = false;
95 bool fixAA = false;
96 if (origPaint) {
97 // kClear blend mode is drawn as kDstOut on HW for compatibility with Android O and
98 // older.
99 fixBlending = sApiLevel <= 27 && origPaint->getBlendMode() == SkBlendMode::kClear;
100 fixAA = origPaint->isAntiAlias();
101 }
102
103 if (fixBlending || fixAA || colorSpaceFilter) {
104 if (origPaint) {
105 *tmpPaint = *origPaint;
106 }
107
108 if (fixBlending) {
109 tmpPaint->setBlendMode(SkBlendMode::kDstOut);
110 }
111
112 if (colorSpaceFilter) {
113 if (tmpPaint->getColorFilter()) {
114 tmpPaint->setColorFilter(SkColorFilter::MakeComposeFilter(
115 tmpPaint->refColorFilter(), colorSpaceFilter));
116 } else {
117 tmpPaint->setColorFilter(colorSpaceFilter);
118 }
119 LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
120 }
121
122 // disabling AA on bitmap draws matches legacy HWUI behavior
123 tmpPaint->setAntiAlias(false);
124 return tmpPaint;
125 } else {
126 return origPaint;
127 }
128 }
129
Stan Iliev021693b2016-10-17 16:26:15 -0400130};
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132}; // namespace skiapipeline
133}; // namespace uirenderer
134}; // namespace android