blob: 0bdf15333647664b2270daf287782413943da0bf [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
19#include "Layer.h"
20#include "RenderNode.h"
21#include "LayerDrawable.h"
22#include "NinePatchUtils.h"
23#include "pipeline/skia/AnimatedDrawables.h"
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050024#include <SkImagePriv.h>
Stan Iliev021693b2016-10-17 16:26:15 -040025
26namespace android {
27namespace uirenderer {
28namespace skiapipeline {
29
30// ----------------------------------------------------------------------------
31// Recording Canvas Setup
32// ----------------------------------------------------------------------------
33
34void SkiaRecordingCanvas::initDisplayList(uirenderer::RenderNode* renderNode, int width,
35 int height) {
Stan Iliev021693b2016-10-17 16:26:15 -040036 mCurrentBarrier = nullptr;
37 SkASSERT(mDisplayList.get() == nullptr);
38
39 if (renderNode) {
40 mDisplayList = renderNode->detachAvailableList();
41 }
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -050042 if (!mDisplayList) {
43 mDisplayList.reset(new SkiaDisplayList());
Stan Iliev021693b2016-10-17 16:26:15 -040044 }
45
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -050046 mDisplayList->attachRecorder(&mRecorder, SkIRect::MakeWH(width, height));
Stan Iliev021693b2016-10-17 16:26:15 -040047 SkiaCanvas::reset(&mRecorder);
48}
49
50uirenderer::DisplayList* SkiaRecordingCanvas::finishRecording() {
51 // close any existing chunks if necessary
52 insertReorderBarrier(false);
53 mRecorder.restoreToCount(1);
54 return mDisplayList.release();
55}
56
57// ----------------------------------------------------------------------------
58// Recording Canvas draw operations: View System
59// ----------------------------------------------------------------------------
60
61void SkiaRecordingCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
62 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
63 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
64 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -040065 // Destructor of drawables created with allocateDrawable, will be invoked by ~LinearAllocator.
Stan Iliev021693b2016-10-17 16:26:15 -040066 drawDrawable(mDisplayList->allocateDrawable<AnimatedRoundRect>(left, top, right, bottom,
67 rx, ry, paint));
68}
69
70void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
71 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
72 uirenderer::CanvasPropertyPaint* paint) {
73 drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint));
74}
75
76void SkiaRecordingCanvas::insertReorderBarrier(bool enableReorder) {
Stan Iliev021693b2016-10-17 16:26:15 -040077 if (nullptr != mCurrentBarrier) {
78 // finish off the existing chunk
79 SkDrawable* drawable =
80 mDisplayList->allocateDrawable<EndReorderBarrierDrawable>(
81 mCurrentBarrier);
82 mCurrentBarrier = nullptr;
83 drawDrawable(drawable);
84 }
Stan Iliev88e08912016-11-22 18:19:29 -050085 if (enableReorder) {
86 mCurrentBarrier = (StartReorderBarrierDrawable*)
87 mDisplayList->allocateDrawable<StartReorderBarrierDrawable>(
88 mDisplayList.get());
89 drawDrawable(mCurrentBarrier);
90 }
Stan Iliev021693b2016-10-17 16:26:15 -040091}
92
93void SkiaRecordingCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040094 if (layerUpdater != nullptr) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -040095 // Create a ref-counted drawable, which is kept alive by sk_sp in SkLiteDL.
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040096 sk_sp<SkDrawable> drawable(new LayerDrawable(layerUpdater));
Stan Iliev021693b2016-10-17 16:26:15 -040097 drawDrawable(drawable.get());
98 }
99}
100
101void SkiaRecordingCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400102 // Record the child node. Drawable dtor will be invoked when mChildNodes deque is cleared.
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400103 mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500104 auto& renderNodeDrawable = mDisplayList->mChildNodes.back();
105 drawDrawable(&renderNodeDrawable);
Stan Iliev021693b2016-10-17 16:26:15 -0400106
107 // use staging property, since recording on UI thread
108 if (renderNode->stagingProperties().isProjectionReceiver()) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500109 mDisplayList->mProjectionReceiver = &renderNodeDrawable;
Stan Iliev021693b2016-10-17 16:26:15 -0400110 // set projectionReceiveIndex so that RenderNode.hasProjectionReceiver returns true
111 mDisplayList->projectionReceiveIndex = mDisplayList->mChildNodes.size() - 1;
112 }
113}
114
115void SkiaRecordingCanvas::callDrawGLFunction(Functor* functor,
116 uirenderer::GlFunctorLifecycleListener* listener) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400117 // Drawable dtor will be invoked when mChildFunctors deque is cleared.
Stan Iliev021693b2016-10-17 16:26:15 -0400118 mDisplayList->mChildFunctors.emplace_back(functor, listener, asSkCanvas());
119 drawDrawable(&mDisplayList->mChildFunctors.back());
120}
121
122class VectorDrawable : public SkDrawable {
123 public:
124 VectorDrawable(VectorDrawableRoot* tree) : mRoot(tree) {}
125
126 protected:
127 virtual SkRect onGetBounds() override {
128 return SkRect::MakeLargest();
129 }
130 virtual void onDraw(SkCanvas* canvas) override {
Stan Iliev23c38a92017-03-23 00:12:50 -0400131 mRoot->draw(canvas);
Stan Iliev021693b2016-10-17 16:26:15 -0400132 }
133
134 private:
135 sp<VectorDrawableRoot> mRoot;
136};
137
138void SkiaRecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
139 drawDrawable(mDisplayList->allocateDrawable<VectorDrawable>(tree));
140 mDisplayList->mVectorDrawables.push_back(tree);
141}
142
143// ----------------------------------------------------------------------------
144// Recording Canvas draw operations: Bitmaps
145// ----------------------------------------------------------------------------
146
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400147inline static const SkPaint* bitmapPaint(const SkPaint* origPaint, SkPaint* tmpPaint,
148 sk_sp<SkColorFilter> colorFilter) {
149 if ((origPaint && origPaint->isAntiAlias()) || colorFilter) {
150 if (origPaint) {
151 *tmpPaint = *origPaint;
152 }
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400153
154 sk_sp<SkColorFilter> filter;
155 if (colorFilter && tmpPaint->getColorFilter()) {
156 filter = SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorFilter);
157 LOG_ALWAYS_FATAL_IF(!filter);
158 } else {
159 filter = colorFilter;
160 }
161
Stan Iliev021693b2016-10-17 16:26:15 -0400162 tmpPaint->setAntiAlias(false);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400163 tmpPaint->setColorFilter(filter);
Stan Iliev021693b2016-10-17 16:26:15 -0400164 return tmpPaint;
165 } else {
166 return origPaint;
167 }
168}
169
170void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400171 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400172 sk_sp<SkColorFilter> colorFilter;
173 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
174 mRecorder.drawImage(image, left, top, bitmapPaint(paint, &tmpPaint, colorFilter));
Stan Iliev0cc4e362017-05-18 14:21:23 -0400175 // if image->unique() is true, then mRecorder.drawImage failed for some reason. It also means
176 // it is not safe to store a raw SkImage pointer, because the image object will be destroyed
177 // when this function ends.
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400178 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400179 mDisplayList->mMutableImages.push_back(image.get());
180 }
Stan Iliev021693b2016-10-17 16:26:15 -0400181}
182
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400183void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix,
Stan Iliev021693b2016-10-17 16:26:15 -0400184 const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400185 SkAutoCanvasRestore acr(&mRecorder, true);
186 concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400187
Stan Iliev021693b2016-10-17 16:26:15 -0400188 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400189 sk_sp<SkColorFilter> colorFilter;
190 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
191 mRecorder.drawImage(image, 0, 0, bitmapPaint(paint, &tmpPaint, colorFilter));
192 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400193 mDisplayList->mMutableImages.push_back(image.get());
194 }
Stan Iliev021693b2016-10-17 16:26:15 -0400195}
196
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400197void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
Stan Iliev021693b2016-10-17 16:26:15 -0400198 float srcRight, float srcBottom, float dstLeft, float dstTop, float dstRight,
199 float dstBottom, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400200 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
201 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400202
Stan Iliev021693b2016-10-17 16:26:15 -0400203 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400204 sk_sp<SkColorFilter> colorFilter;
205 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400206 mRecorder.drawImageRect(image, srcRect, dstRect, bitmapPaint(paint, &tmpPaint, colorFilter),
207 SkCanvas::kFast_SrcRectConstraint);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400208 if (!bitmap.isImmutable() && image.get() && !image->unique() && !srcRect.isEmpty()
Stan Iliev0cc4e362017-05-18 14:21:23 -0400209 && !dstRect.isEmpty()) {
210 mDisplayList->mMutableImages.push_back(image.get());
211 }
Stan Iliev021693b2016-10-17 16:26:15 -0400212}
213
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400214void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk,
Stan Iliev021693b2016-10-17 16:26:15 -0400215 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400216 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400217 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Iliev021693b2016-10-17 16:26:15 -0400218
219 lattice.fFlags = nullptr;
220 int numFlags = 0;
221 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
222 // We can expect the framework to give us a color for every distinct rect.
223 // Skia requires placeholder flags for degenerate rects.
224 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
225 }
226
227 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
228 if (numFlags > 0) {
229 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
230 }
231
232 lattice.fBounds = nullptr;
233 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Stan Iliev021693b2016-10-17 16:26:15 -0400234
235 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400236 sk_sp<SkColorFilter> colorFilter;
237 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
238 mRecorder.drawImageLattice(image.get(), lattice, dst,
239 bitmapPaint(paint, &tmpPaint, colorFilter));
240 if (!bitmap.isImmutable() && image.get() && !image->unique() && !dst.isEmpty()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400241 mDisplayList->mMutableImages.push_back(image.get());
242 }
Stan Iliev021693b2016-10-17 16:26:15 -0400243}
244
245}; // namespace skiapipeline
246}; // namespace uirenderer
247}; // namespace android