blob: f0da660f17b032af60c1ea1025ec9424094188b9 [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>
Stan Iliev021693b2016-10-17 16:26:15 -040020#include "Layer.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "LayerDrawable.h"
22#include "NinePatchUtils.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "RenderNode.h"
Stan Iliev021693b2016-10-17 16:26:15 -040024#include "pipeline/skia/AnimatedDrawables.h"
25
26namespace android {
27namespace uirenderer {
28namespace skiapipeline {
29
30// ----------------------------------------------------------------------------
31// Recording Canvas Setup
32// ----------------------------------------------------------------------------
33
34void SkiaRecordingCanvas::initDisplayList(uirenderer::RenderNode* renderNode, int width,
John Reck1bcacfd2017-11-03 10:12:19 -070035 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,
John Reck1bcacfd2017-11-03 10:12:19 -070062 uirenderer::CanvasPropertyPrimitive* top,
63 uirenderer::CanvasPropertyPrimitive* right,
64 uirenderer::CanvasPropertyPrimitive* bottom,
65 uirenderer::CanvasPropertyPrimitive* rx,
66 uirenderer::CanvasPropertyPrimitive* ry,
67 uirenderer::CanvasPropertyPaint* paint) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -040068 // Destructor of drawables created with allocateDrawable, will be invoked by ~LinearAllocator.
John Reck1bcacfd2017-11-03 10:12:19 -070069 drawDrawable(mDisplayList->allocateDrawable<AnimatedRoundRect>(left, top, right, bottom, rx, ry,
70 paint));
Stan Iliev021693b2016-10-17 16:26:15 -040071}
72
73void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -070074 uirenderer::CanvasPropertyPrimitive* y,
75 uirenderer::CanvasPropertyPrimitive* radius,
76 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -040077 drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint));
78}
79
80void SkiaRecordingCanvas::insertReorderBarrier(bool enableReorder) {
Stan Iliev021693b2016-10-17 16:26:15 -040081 if (nullptr != mCurrentBarrier) {
82 // finish off the existing chunk
83 SkDrawable* drawable =
John Reck1bcacfd2017-11-03 10:12:19 -070084 mDisplayList->allocateDrawable<EndReorderBarrierDrawable>(mCurrentBarrier);
Stan Iliev021693b2016-10-17 16:26:15 -040085 mCurrentBarrier = nullptr;
86 drawDrawable(drawable);
87 }
Stan Iliev88e08912016-11-22 18:19:29 -050088 if (enableReorder) {
89 mCurrentBarrier = (StartReorderBarrierDrawable*)
John Reck1bcacfd2017-11-03 10:12:19 -070090 mDisplayList->allocateDrawable<StartReorderBarrierDrawable>(
91 mDisplayList.get());
Stan Iliev88e08912016-11-22 18:19:29 -050092 drawDrawable(mCurrentBarrier);
93 }
Stan Iliev021693b2016-10-17 16:26:15 -040094}
95
96void SkiaRecordingCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040097 if (layerUpdater != nullptr) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -040098 // Create a ref-counted drawable, which is kept alive by sk_sp in SkLiteDL.
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040099 sk_sp<SkDrawable> drawable(new LayerDrawable(layerUpdater));
Stan Iliev021693b2016-10-17 16:26:15 -0400100 drawDrawable(drawable.get());
101 }
102}
103
104void SkiaRecordingCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400105 // Record the child node. Drawable dtor will be invoked when mChildNodes deque is cleared.
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400106 mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500107 auto& renderNodeDrawable = mDisplayList->mChildNodes.back();
108 drawDrawable(&renderNodeDrawable);
Stan Iliev021693b2016-10-17 16:26:15 -0400109
110 // use staging property, since recording on UI thread
111 if (renderNode->stagingProperties().isProjectionReceiver()) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500112 mDisplayList->mProjectionReceiver = &renderNodeDrawable;
Stan Iliev021693b2016-10-17 16:26:15 -0400113 // set projectionReceiveIndex so that RenderNode.hasProjectionReceiver returns true
114 mDisplayList->projectionReceiveIndex = mDisplayList->mChildNodes.size() - 1;
115 }
116}
117
118void SkiaRecordingCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700119 uirenderer::GlFunctorLifecycleListener* listener) {
Stan Ilievf7aa6cd2017-03-29 18:25:12 -0400120 // Drawable dtor will be invoked when mChildFunctors deque is cleared.
Stan Iliev021693b2016-10-17 16:26:15 -0400121 mDisplayList->mChildFunctors.emplace_back(functor, listener, asSkCanvas());
122 drawDrawable(&mDisplayList->mChildFunctors.back());
123}
124
125class VectorDrawable : public SkDrawable {
John Reck1bcacfd2017-11-03 10:12:19 -0700126public:
Stan Iliev65e678f2018-02-07 14:07:30 -0500127 VectorDrawable(VectorDrawableRoot* tree)
128 : mRoot(tree)
129 , mBounds(tree->stagingProperties()->getBounds()) {}
Stan Iliev021693b2016-10-17 16:26:15 -0400130
John Reck1bcacfd2017-11-03 10:12:19 -0700131protected:
Stan Iliev65e678f2018-02-07 14:07:30 -0500132 virtual SkRect onGetBounds() override { return mBounds; }
133 virtual void onDraw(SkCanvas* canvas) override {
134 mRoot->draw(canvas, mBounds);
135 }
Stan Iliev021693b2016-10-17 16:26:15 -0400136
John Reck1bcacfd2017-11-03 10:12:19 -0700137private:
Stan Iliev021693b2016-10-17 16:26:15 -0400138 sp<VectorDrawableRoot> mRoot;
Stan Iliev65e678f2018-02-07 14:07:30 -0500139 SkRect mBounds;
Stan Iliev021693b2016-10-17 16:26:15 -0400140};
141
142void SkiaRecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
143 drawDrawable(mDisplayList->allocateDrawable<VectorDrawable>(tree));
144 mDisplayList->mVectorDrawables.push_back(tree);
145}
146
147// ----------------------------------------------------------------------------
148// Recording Canvas draw operations: Bitmaps
149// ----------------------------------------------------------------------------
150
Stan Iliev021693b2016-10-17 16:26:15 -0400151
152void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400153 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400154 sk_sp<SkColorFilter> colorFilter;
155 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
156 mRecorder.drawImage(image, left, top, bitmapPaint(paint, &tmpPaint, colorFilter));
Stan Iliev0cc4e362017-05-18 14:21:23 -0400157 // if image->unique() is true, then mRecorder.drawImage failed for some reason. It also means
158 // it is not safe to store a raw SkImage pointer, because the image object will be destroyed
159 // when this function ends.
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400160 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400161 mDisplayList->mMutableImages.push_back(image.get());
162 }
Stan Iliev021693b2016-10-17 16:26:15 -0400163}
164
John Reck1bcacfd2017-11-03 10:12:19 -0700165void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400166 SkAutoCanvasRestore acr(&mRecorder, true);
167 concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400168
Stan Iliev021693b2016-10-17 16:26:15 -0400169 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400170 sk_sp<SkColorFilter> colorFilter;
171 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
172 mRecorder.drawImage(image, 0, 0, bitmapPaint(paint, &tmpPaint, colorFilter));
173 if (!bitmap.isImmutable() && image.get() && !image->unique()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400174 mDisplayList->mMutableImages.push_back(image.get());
175 }
Stan Iliev021693b2016-10-17 16:26:15 -0400176}
177
John Reck1bcacfd2017-11-03 10:12:19 -0700178void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
179 float srcBottom, float dstLeft, float dstTop, float dstRight,
180 float dstBottom, const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400181 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
182 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400183
Stan Iliev021693b2016-10-17 16:26:15 -0400184 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400185 sk_sp<SkColorFilter> colorFilter;
186 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400187 mRecorder.drawImageRect(image, srcRect, dstRect, bitmapPaint(paint, &tmpPaint, colorFilter),
John Reck1bcacfd2017-11-03 10:12:19 -0700188 SkCanvas::kFast_SrcRectConstraint);
189 if (!bitmap.isImmutable() && image.get() && !image->unique() && !srcRect.isEmpty() &&
190 !dstRect.isEmpty()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400191 mDisplayList->mMutableImages.push_back(image.get());
192 }
Stan Iliev021693b2016-10-17 16:26:15 -0400193}
194
John Reck1bcacfd2017-11-03 10:12:19 -0700195void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
196 float dstTop, float dstRight, float dstBottom,
197 const SkPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400198 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400199 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Iliev021693b2016-10-17 16:26:15 -0400200
Stan Ilieve12d7312017-12-04 14:48:27 -0500201 lattice.fRectTypes = nullptr;
202 lattice.fColors = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400203 int numFlags = 0;
204 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
205 // We can expect the framework to give us a color for every distinct rect.
206 // Skia requires placeholder flags for degenerate rects.
207 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
208 }
209
Stan Ilieve12d7312017-12-04 14:48:27 -0500210 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
211 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Iliev021693b2016-10-17 16:26:15 -0400212 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500213 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Iliev021693b2016-10-17 16:26:15 -0400214 }
215
216 lattice.fBounds = nullptr;
217 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Stan Iliev021693b2016-10-17 16:26:15 -0400218
219 SkPaint tmpPaint;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400220 sk_sp<SkColorFilter> colorFilter;
221 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Leon Scroggins III5a663762018-04-23 11:15:00 -0400222 const SkPaint* filteredPaint = bitmapPaint(paint, &tmpPaint, colorFilter);
223 // Besides kNone, the other three SkFilterQualities are treated the same. And Android's
224 // Java API only supports kLow and kNone anyway.
225 if (!filteredPaint || filteredPaint->getFilterQuality() == kNone_SkFilterQuality) {
226 if (filteredPaint != &tmpPaint) {
227 if (paint) {
228 tmpPaint = *paint;
229 }
230 filteredPaint = &tmpPaint;
231 }
232 tmpPaint.setFilterQuality(kLow_SkFilterQuality);
233 }
234
235 mRecorder.drawImageLattice(image.get(), lattice, dst, filteredPaint);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400236 if (!bitmap.isImmutable() && image.get() && !image->unique() && !dst.isEmpty()) {
Stan Iliev0cc4e362017-05-18 14:21:23 -0400237 mDisplayList->mMutableImages.push_back(image.get());
238 }
Stan Iliev021693b2016-10-17 16:26:15 -0400239}
240
Derek Sollenberger2d142132018-01-22 10:25:26 -0500241double SkiaRecordingCanvas::drawAnimatedImage(AnimatedImageDrawable* animatedImage) {
242 drawDrawable(animatedImage);
243 mDisplayList->mAnimatedImages.push_back(animatedImage);
244 return 0;
245}
246
John Reck1bcacfd2017-11-03 10:12:19 -0700247}; // namespace skiapipeline
248}; // namespace uirenderer
249}; // namespace android