blob: d9456355cb88bc0b89da9ae350178ac737a40995 [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
17#include "SkiaRecordingCanvas.h"
18
John Reck1bcacfd2017-11-03 10:12:19 -070019#include <SkImagePriv.h>
John Reck9ce2bf72018-07-02 18:33:32 -070020#include "CanvasTransform.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "Layer.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022#include "LayerDrawable.h"
23#include "NinePatchUtils.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "RenderNode.h"
Stan Iliev021693b2016-10-17 16:26:15 -040025#include "pipeline/skia/AnimatedDrawables.h"
Stan Iliev11606ff2018-09-17 14:01:16 -040026#include "pipeline/skia/GLFunctorDrawable.h"
Chris Blume41423392018-11-06 11:47:03 -080027#include "pipeline/skia/VkFunctorDrawable.h"
John Reck283bb462018-12-13 16:40:14 -080028#include "pipeline/skia/VkInteropFunctorDrawable.h"
Stan Iliev021693b2016-10-17 16:26:15 -040029
30namespace android {
31namespace uirenderer {
32namespace skiapipeline {
33
34// ----------------------------------------------------------------------------
35// Recording Canvas Setup
36// ----------------------------------------------------------------------------
37
38void SkiaRecordingCanvas::initDisplayList(uirenderer::RenderNode* renderNode, int width,
John Reck1bcacfd2017-11-03 10:12:19 -070039 int height) {
Stan Iliev021693b2016-10-17 16:26:15 -040040 mCurrentBarrier = nullptr;
41 SkASSERT(mDisplayList.get() == nullptr);
42
43 if (renderNode) {
44 mDisplayList = renderNode->detachAvailableList();
45 }
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -050046 if (!mDisplayList) {
47 mDisplayList.reset(new SkiaDisplayList());
Stan Iliev021693b2016-10-17 16:26:15 -040048 }
49
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -050050 mDisplayList->attachRecorder(&mRecorder, SkIRect::MakeWH(width, height));
John Reck8f45d4a2018-08-15 10:17:12 -070051 SkiaCanvas::reset(&mRecorder);
Stan Iliev021693b2016-10-17 16:26:15 -040052}
53
54uirenderer::DisplayList* SkiaRecordingCanvas::finishRecording() {
55 // close any existing chunks if necessary
56 insertReorderBarrier(false);
57 mRecorder.restoreToCount(1);
58 return mDisplayList.release();
59}
60
61// ----------------------------------------------------------------------------
62// Recording Canvas draw operations: View System
63// ----------------------------------------------------------------------------
64
65void SkiaRecordingCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -070066 uirenderer::CanvasPropertyPrimitive* top,
67 uirenderer::CanvasPropertyPrimitive* right,
68 uirenderer::CanvasPropertyPrimitive* bottom,
69 uirenderer::CanvasPropertyPrimitive* rx,
70 uirenderer::CanvasPropertyPrimitive* ry,
71 uirenderer::CanvasPropertyPaint* paint) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -040072 // Destructor of drawables created with allocateDrawable, will be invoked by ~LinearAllocator.
John Reck1bcacfd2017-11-03 10:12:19 -070073 drawDrawable(mDisplayList->allocateDrawable<AnimatedRoundRect>(left, top, right, bottom, rx, ry,
74 paint));
Stan Iliev021693b2016-10-17 16:26:15 -040075}
76
77void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -070078 uirenderer::CanvasPropertyPrimitive* y,
79 uirenderer::CanvasPropertyPrimitive* radius,
80 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -040081 drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint));
82}
83
84void SkiaRecordingCanvas::insertReorderBarrier(bool enableReorder) {
John Reckddeaa482018-10-30 10:47:43 -070085 if (mCurrentBarrier && enableReorder) {
86 // Already in a re-order section, nothing to do
87 return;
88 }
89
Stan Iliev021693b2016-10-17 16:26:15 -040090 if (nullptr != mCurrentBarrier) {
91 // finish off the existing chunk
92 SkDrawable* drawable =
John Reck1bcacfd2017-11-03 10:12:19 -070093 mDisplayList->allocateDrawable<EndReorderBarrierDrawable>(mCurrentBarrier);
Stan Iliev021693b2016-10-17 16:26:15 -040094 mCurrentBarrier = nullptr;
95 drawDrawable(drawable);
96 }
Stan Iliev88e08912016-11-22 18:19:29 -050097 if (enableReorder) {
John Reck283bb462018-12-13 16:40:14 -080098 mCurrentBarrier =
99 mDisplayList->allocateDrawable<StartReorderBarrierDrawable>(mDisplayList.get());
Stan Iliev88e08912016-11-22 18:19:29 -0500100 drawDrawable(mCurrentBarrier);
101 }
Stan Iliev021693b2016-10-17 16:26:15 -0400102}
103
104void SkiaRecordingCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -0400105 if (layerUpdater != nullptr) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400106 // Create a ref-counted drawable, which is kept alive by sk_sp in SkLiteDL.
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -0400107 sk_sp<SkDrawable> drawable(new LayerDrawable(layerUpdater));
Stan Iliev021693b2016-10-17 16:26:15 -0400108 drawDrawable(drawable.get());
109 }
110}
111
112void SkiaRecordingCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400113 // Record the child node. Drawable dtor will be invoked when mChildNodes deque is cleared.
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400114 mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500115 auto& renderNodeDrawable = mDisplayList->mChildNodes.back();
Stan Ilievf09ee582018-11-06 17:35:50 -0500116 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
117 // Put Vulkan WebViews with non-rectangular clips in a HW layer
118 renderNode->mutateStagingProperties().setClipMayBeComplex(mRecorder.isClipMayBeComplex());
119 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500120 drawDrawable(&renderNodeDrawable);
Stan Iliev021693b2016-10-17 16:26:15 -0400121
122 // use staging property, since recording on UI thread
123 if (renderNode->stagingProperties().isProjectionReceiver()) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500124 mDisplayList->mProjectionReceiver = &renderNodeDrawable;
Stan Iliev021693b2016-10-17 16:26:15 -0400125 }
126}
127
128void SkiaRecordingCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700129 uirenderer::GlFunctorLifecycleListener* listener) {
Stan Iliev11606ff2018-09-17 14:01:16 -0400130 FunctorDrawable* functorDrawable;
131 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
John Reck283bb462018-12-13 16:40:14 -0800132 functorDrawable = mDisplayList->allocateDrawable<VkInteropFunctorDrawable>(
133 functor, listener, asSkCanvas());
Stan Iliev11606ff2018-09-17 14:01:16 -0400134 } else {
John Reck283bb462018-12-13 16:40:14 -0800135 functorDrawable =
136 mDisplayList->allocateDrawable<GLFunctorDrawable>(functor, listener, asSkCanvas());
137 }
138 mDisplayList->mChildFunctors.push_back(functorDrawable);
139 drawDrawable(functorDrawable);
140}
141
142void SkiaRecordingCanvas::drawWebViewFunctor(int functor) {
143 FunctorDrawable* functorDrawable;
144 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
John Reck283bb462018-12-13 16:40:14 -0800145 functorDrawable =
Bo Liud25d1342019-02-04 14:55:02 -0800146 mDisplayList->allocateDrawable<VkFunctorDrawable>(functor, asSkCanvas());
John Reck283bb462018-12-13 16:40:14 -0800147 } else {
148 functorDrawable = mDisplayList->allocateDrawable<GLFunctorDrawable>(functor, asSkCanvas());
Stan Iliev11606ff2018-09-17 14:01:16 -0400149 }
150 mDisplayList->mChildFunctors.push_back(functorDrawable);
151 drawDrawable(functorDrawable);
Stan Iliev021693b2016-10-17 16:26:15 -0400152}
153
Stan Iliev021693b2016-10-17 16:26:15 -0400154void SkiaRecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
John Reck08ee8152018-09-20 16:27:46 -0700155 mRecorder.drawVectorDrawable(tree);
Stan Iliev021693b2016-10-17 16:26:15 -0400156 mDisplayList->mVectorDrawables.push_back(tree);
157}
158
159// ----------------------------------------------------------------------------
160// Recording Canvas draw operations: Bitmaps
161// ----------------------------------------------------------------------------
162
Ben Wagner0ed10be2018-06-28 17:08:16 -0400163SkiaCanvas::PaintCoW&& SkiaRecordingCanvas::filterBitmap(PaintCoW&& paint,
164 sk_sp<SkColorFilter> colorSpaceFilter) {
165 bool fixBlending = false;
166 bool fixAA = false;
167 if (paint) {
168 // kClear blend mode is drawn as kDstOut on HW for compatibility with Android O and
169 // older.
170 fixBlending = sApiLevel <= 27 && paint->getBlendMode() == SkBlendMode::kClear;
171 fixAA = paint->isAntiAlias();
172 }
173
174 if (fixBlending || fixAA || colorSpaceFilter) {
175 SkPaint& tmpPaint = paint.writeable();
176
177 if (fixBlending) {
178 tmpPaint.setBlendMode(SkBlendMode::kDstOut);
179 }
180
181 if (colorSpaceFilter) {
182 if (tmpPaint.getColorFilter()) {
183 tmpPaint.setColorFilter(SkColorFilter::MakeComposeFilter(
John Reck283bb462018-12-13 16:40:14 -0800184 tmpPaint.refColorFilter(), std::move(colorSpaceFilter)));
Ben Wagner0ed10be2018-06-28 17:08:16 -0400185 } else {
186 tmpPaint.setColorFilter(std::move(colorSpaceFilter));
187 }
188 LOG_ALWAYS_FATAL_IF(!tmpPaint.getColorFilter());
189 }
190
191 // disabling AA on bitmap draws matches legacy HWUI behavior
192 tmpPaint.setAntiAlias(false);
193 }
194
195 return filterPaint(std::move(paint));
196}
Stan Iliev021693b2016-10-17 16:26:15 -0400197
198void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400199 sk_sp<SkImage> image = bitmap.makeImage();
200 mRecorder.drawImage(image, left, top, filterPaint(paint), bitmap.palette());
Stan Iliev0cc4e362017-05-18 14:21:23 -0400201 // if image->unique() is true, then mRecorder.drawImage failed for some reason. It also means
202 // it is not safe to store a raw SkImage pointer, because the image object will be destroyed
203 // when this function ends.
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400204 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400205 mDisplayList->mMutableImages.push_back(image.get());
206 }
Stan Iliev021693b2016-10-17 16:26:15 -0400207}
208
John Reck1bcacfd2017-11-03 10:12:19 -0700209void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400210 SkAutoCanvasRestore acr(&mRecorder, true);
211 concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400212
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400213 sk_sp<SkImage> image = bitmap.makeImage();
214 mRecorder.drawImage(image, 0, 0, filterPaint(paint), bitmap.palette());
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400215 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400216 mDisplayList->mMutableImages.push_back(image.get());
217 }
Stan Iliev021693b2016-10-17 16:26:15 -0400218}
219
John Reck1bcacfd2017-11-03 10:12:19 -0700220void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
221 float srcBottom, float dstLeft, float dstTop, float dstRight,
222 float dstBottom, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400223 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
224 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400225
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400226 sk_sp<SkImage> image = bitmap.makeImage();
227 mRecorder.drawImageRect(image, srcRect, dstRect, filterPaint(paint),
John Reckf3c724f2018-09-20 13:00:04 -0700228 SkCanvas::kFast_SrcRectConstraint, bitmap.palette());
John Reck1bcacfd2017-11-03 10:12:19 -0700229 if (!bitmap.isImmutable() && image.get() && !image->unique() && !srcRect.isEmpty() &&
230 !dstRect.isEmpty()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400231 mDisplayList->mMutableImages.push_back(image.get());
232 }
Stan Iliev021693b2016-10-17 16:26:15 -0400233}
234
John Reck1bcacfd2017-11-03 10:12:19 -0700235void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
236 float dstTop, float dstRight, float dstBottom,
237 const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400238 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400239 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Iliev021693b2016-10-17 16:26:15 -0400240
Stan Ilieve12d7312017-12-04 14:48:27 -0500241 lattice.fRectTypes = nullptr;
242 lattice.fColors = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400243 int numFlags = 0;
244 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
245 // We can expect the framework to give us a color for every distinct rect.
246 // Skia requires placeholder flags for degenerate rects.
247 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
248 }
249
Stan Ilieve12d7312017-12-04 14:48:27 -0500250 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
251 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Iliev021693b2016-10-17 16:26:15 -0400252 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500253 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Iliev021693b2016-10-17 16:26:15 -0400254 }
255
256 lattice.fBounds = nullptr;
257 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Stan Iliev021693b2016-10-17 16:26:15 -0400258
Ben Wagner0ed10be2018-06-28 17:08:16 -0400259 PaintCoW filteredPaint(paint);
Stan Iliev038fc372018-07-30 18:31:46 -0400260 // HWUI always draws 9-patches with bilinear filtering, regardless of what is set in the Paint.
261 if (!filteredPaint || filteredPaint->getFilterQuality() != kLow_SkFilterQuality) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400262 filteredPaint.writeable().setFilterQuality(kLow_SkFilterQuality);
Leon Scroggins III5a663762018-04-23 11:15:00 -0400263 }
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400264 sk_sp<SkImage> image = bitmap.makeImage();
John Reck283bb462018-12-13 16:40:14 -0800265 mRecorder.drawImageLattice(image, lattice, dst, filterPaint(std::move(filteredPaint)),
John Reck3b4510cd2018-09-27 17:39:45 -0700266 bitmap.palette());
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400267 if (!bitmap.isImmutable() && image.get() && !image->unique() && !dst.isEmpty()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400268 mDisplayList->mMutableImages.push_back(image.get());
269 }
Stan Iliev021693b2016-10-17 16:26:15 -0400270}
271
Derek Sollenberger2d142132018-01-22 10:25:26 -0500272double SkiaRecordingCanvas::drawAnimatedImage(AnimatedImageDrawable* animatedImage) {
273 drawDrawable(animatedImage);
274 mDisplayList->mAnimatedImages.push_back(animatedImage);
275 return 0;
276}
277
Chris Blume7b8a8082018-11-30 15:51:58 -0800278} // namespace skiapipeline
279} // namespace uirenderer
280} // namespace android