blob: 18764afde1385a2020f934776bcad2ac91492722 [file] [log] [blame]
Derek Sollenberger2d142132018-01-22 10:25:26 -05001/*
2 * Copyright (C) 2018 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#pragma once
18
19#include <cutils/compiler.h>
20#include <utils/RefBase.h>
21
22#include <SkAnimatedImage.h>
23#include <SkCanvas.h>
24#include <SkColorFilter.h>
25#include <SkDrawable.h>
26#include <SkMutex.h>
27
28class SkPicture;
29
30namespace android {
31
32namespace uirenderer {
33class TaskManager;
34}
35
36/**
37 * Native component of android.graphics.drawable.AnimatedImageDrawables.java. This class can be
38 * drawn into Canvas.h and maintains the state needed to drive the animation from the RenderThread.
39 */
40class ANDROID_API AnimatedImageDrawable : public SkDrawable {
41public:
42 AnimatedImageDrawable(sk_sp<SkAnimatedImage> animatedImage);
43
44 /**
45 * This returns true if the animation has updated and signals that the next draw will contain
46 * new content.
47 */
48 bool isDirty() const { return mIsDirty; }
49
50 int getStagingAlpha() const { return mStagingAlpha; }
51 void setStagingAlpha(int alpha) { mStagingAlpha = alpha; }
52 void setStagingColorFilter(sk_sp<SkColorFilter> filter) { mStagingColorFilter = filter; }
53 void syncProperties();
54
55 virtual SkRect onGetBounds() override {
56 return mSkAnimatedImage->getBounds();
57 }
58
59 double drawStaging(SkCanvas* canvas);
60
61 void start();
62 void stop();
63 bool isRunning();
64
65 void scheduleUpdate(uirenderer::TaskManager* taskManager);
66
67protected:
68 virtual void onDraw(SkCanvas* canvas) override;
69
70private:
71 void update();
72
73 sk_sp<SkAnimatedImage> mSkAnimatedImage;
74 sk_sp<SkPicture> mSnapshot;
75 SkMutex mLock;
76
77 int mStagingAlpha = SK_AlphaOPAQUE;
78 sk_sp<SkColorFilter> mStagingColorFilter;
79
80 int mAlpha = SK_AlphaOPAQUE;
81 sk_sp<SkColorFilter> mColorFilter;
82 double mNextFrameTime = 0.0;
83 bool mIsDirty = false;
84
85 class AnimatedImageTask;
86 class AnimatedImageTaskProcessor;
87 sp<AnimatedImageTask> mDecodeTask;
88 sp<AnimatedImageTaskProcessor> mDecodeTaskProcessor;
89};
90
91}; // namespace android