Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #ifndef ANDROID_HWUI_RENDERER_H |
| 18 | #define ANDROID_HWUI_RENDERER_H |
| 19 | |
Chris Craik | c5b5f05 | 2014-10-01 16:40:16 -0700 | [diff] [blame] | 20 | #include <SkColorFilter.h> |
| 21 | #include <SkPaint.h> |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 22 | #include <SkRegion.h> |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 23 | #include <utils/String8.h> |
| 24 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 25 | #include "AssetAtlas.h" |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 26 | |
Derek Sollenberger | 09c2d4f | 2014-10-15 09:21:10 -0400 | [diff] [blame] | 27 | class SkDrawFilter; |
| 28 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 29 | namespace android { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 30 | |
| 31 | class Functor; |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 32 | struct Res_png_9patch; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 33 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 34 | namespace uirenderer { |
| 35 | |
John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 36 | class RenderNode; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 37 | class Layer; |
| 38 | class Matrix4; |
| 39 | class SkiaColorFilter; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 40 | class Patch; |
| 41 | |
| 42 | enum DrawOpMode { |
| 43 | kDrawOpMode_Immediate, |
| 44 | kDrawOpMode_Defer, |
| 45 | kDrawOpMode_Flush |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Hwui's abstract version of Canvas. |
| 50 | * |
| 51 | * Provides methods for frame state operations, as well as the SkCanvas style transform/clip state, |
| 52 | * and varied drawing operations. |
| 53 | * |
| 54 | * Should at some point interact with native SkCanvas. |
| 55 | */ |
| 56 | class ANDROID_API Renderer { |
| 57 | public: |
| 58 | virtual ~Renderer() {} |
| 59 | |
| 60 | /** |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 61 | * Safely retrieves the mode from the specified xfermode. If the specified |
| 62 | * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. |
| 63 | */ |
| 64 | static inline SkXfermode::Mode getXfermode(SkXfermode* mode) { |
| 65 | SkXfermode::Mode resultMode; |
| 66 | if (!SkXfermode::AsMode(mode, &resultMode)) { |
| 67 | resultMode = SkXfermode::kSrcOver_Mode; |
| 68 | } |
| 69 | return resultMode; |
| 70 | } |
| 71 | |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 72 | // TODO: move to a method on android:Paint |
| 73 | static inline bool paintWillNotDraw(const SkPaint& paint) { |
| 74 | return paint.getAlpha() == 0 |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 75 | && !paint.getColorFilter() |
Chris Craik | bc34191 | 2014-09-22 18:17:39 -0700 | [diff] [blame] | 76 | && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode; |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // TODO: move to a method on android:Paint |
| 80 | static inline bool paintWillNotDrawText(const SkPaint& paint) { |
| 81 | return paint.getAlpha() == 0 |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 82 | && paint.getLooper() == nullptr |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 83 | && !paint.getColorFilter() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 84 | && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode; |
| 85 | } |
Chris Craik | c5b5f05 | 2014-10-01 16:40:16 -0700 | [diff] [blame] | 86 | |
| 87 | static bool isBlendedColorFilter(const SkColorFilter* filter) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 88 | if (filter == nullptr) { |
Chris Craik | c5b5f05 | 2014-10-01 16:40:16 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | return (filter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag) == 0; |
| 92 | } |
| 93 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 94 | // ---------------------------------------------------------------------------- |
| 95 | // Frame state operations |
| 96 | // ---------------------------------------------------------------------------- |
| 97 | /** |
| 98 | * Sets the dimension of the underlying drawing surface. This method must |
| 99 | * be called at least once every time the drawing surface changes size. |
| 100 | * |
| 101 | * @param width The width in pixels of the underlysing surface |
| 102 | * @param height The height in pixels of the underlysing surface |
| 103 | */ |
| 104 | virtual void setViewport(int width, int height) = 0; |
| 105 | |
| 106 | /** |
| 107 | * Prepares the renderer to draw a frame. This method must be invoked |
| 108 | * at the beginning of each frame. When this method is invoked, the |
| 109 | * entire drawing surface is assumed to be redrawn. |
| 110 | * |
| 111 | * @param opaque If true, the target surface is considered opaque |
| 112 | * and will not be cleared. If false, the target surface |
| 113 | * will be cleared |
| 114 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 115 | virtual void prepare(bool opaque) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Prepares the renderer to draw a frame. This method must be invoked |
| 119 | * at the beginning of each frame. Only the specified rectangle of the |
| 120 | * frame is assumed to be dirty. A clip will automatically be set to |
| 121 | * the specified rectangle. |
| 122 | * |
| 123 | * @param left The left coordinate of the dirty rectangle |
| 124 | * @param top The top coordinate of the dirty rectangle |
| 125 | * @param right The right coordinate of the dirty rectangle |
| 126 | * @param bottom The bottom coordinate of the dirty rectangle |
| 127 | * @param opaque If true, the target surface is considered opaque |
| 128 | * and will not be cleared. If false, the target surface |
| 129 | * will be cleared in the specified dirty rectangle |
| 130 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 131 | virtual void prepareDirty(float left, float top, float right, float bottom, |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 132 | bool opaque) = 0; |
| 133 | |
| 134 | /** |
| 135 | * Indicates the end of a frame. This method must be invoked whenever |
| 136 | * the caller is done rendering a frame. |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 137 | * Returns true if any drawing was done during the frame (the output |
| 138 | * has changed / is "dirty" and should be displayed to the user). |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 139 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 140 | virtual bool finish() = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 141 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 142 | // ---------------------------------------------------------------------------- |
| 143 | // Canvas state operations |
| 144 | // ---------------------------------------------------------------------------- |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 145 | // Save (layer) |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 146 | virtual int getSaveCount() const = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 147 | virtual int save(int flags) = 0; |
| 148 | virtual void restore() = 0; |
| 149 | virtual void restoreToCount(int saveCount) = 0; |
| 150 | |
Derek Sollenberger | d44fbe5 | 2014-02-05 16:47:00 -0500 | [diff] [blame] | 151 | virtual int saveLayer(float left, float top, float right, float bottom, |
| 152 | const SkPaint* paint, int flags) = 0; |
| 153 | |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 154 | int saveLayerAlpha(float left, float top, float right, float bottom, |
| 155 | int alpha, int flags) { |
Derek Sollenberger | d44fbe5 | 2014-02-05 16:47:00 -0500 | [diff] [blame] | 156 | SkPaint paint; |
| 157 | paint.setAlpha(alpha); |
| 158 | return saveLayer(left, top, right, bottom, &paint, flags); |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 159 | } |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 160 | |
| 161 | // Matrix |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 162 | virtual void getMatrix(SkMatrix* outMatrix) const = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 163 | virtual void translate(float dx, float dy, float dz = 0.0f) = 0; |
| 164 | virtual void rotate(float degrees) = 0; |
| 165 | virtual void scale(float sx, float sy) = 0; |
| 166 | virtual void skew(float sx, float sy) = 0; |
| 167 | |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 168 | virtual void setMatrix(const SkMatrix& matrix) = 0; |
| 169 | virtual void concatMatrix(const SkMatrix& matrix) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 170 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 171 | // clip |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 172 | virtual const Rect& getLocalClipBounds() const = 0; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 173 | virtual bool quickRejectConservative(float left, float top, |
| 174 | float right, float bottom) const = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 175 | virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0; |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 176 | virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0; |
| 177 | virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 178 | |
Derek Sollenberger | 09c2d4f | 2014-10-15 09:21:10 -0400 | [diff] [blame] | 179 | // Misc |
| 180 | virtual void setDrawFilter(SkDrawFilter* filter) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 181 | |
| 182 | // ---------------------------------------------------------------------------- |
| 183 | // Canvas draw operations |
| 184 | // ---------------------------------------------------------------------------- |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 185 | virtual void drawColor(int color, SkXfermode::Mode mode) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 186 | |
| 187 | // Bitmap-based |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 188 | virtual void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) = 0; |
| 189 | virtual void drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 190 | float srcRight, float srcBottom, float dstLeft, float dstTop, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 191 | float dstRight, float dstBottom, const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 192 | virtual void drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) = 0; |
| 193 | virtual void drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 194 | const float* vertices, const int* colors, const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 195 | virtual void drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 196 | float left, float top, float right, float bottom, const SkPaint* paint) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 197 | |
| 198 | // Shapes |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 199 | virtual void drawRect(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 200 | const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 201 | virtual void drawRects(const float* rects, int count, const SkPaint* paint) = 0; |
| 202 | virtual void drawRoundRect(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 203 | float rx, float ry, const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 204 | virtual void drawCircle(float x, float y, float radius, const SkPaint* paint) = 0; |
| 205 | virtual void drawOval(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 206 | const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 207 | virtual void drawArc(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 208 | float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 209 | virtual void drawPath(const SkPath* path, const SkPaint* paint) = 0; |
| 210 | virtual void drawLines(const float* points, int count, const SkPaint* paint) = 0; |
| 211 | virtual void drawPoints(const float* points, int count, const SkPaint* paint) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 212 | |
| 213 | // Text |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 214 | virtual void drawText(const char* text, int bytesCount, int count, float x, float y, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 215 | const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds, |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 216 | DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 217 | virtual void drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 218 | float hOffset, float vOffset, const SkPaint* paint) = 0; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 219 | virtual void drawPosText(const char* text, int bytesCount, int count, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 220 | const float* positions, const SkPaint* paint) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 221 | |
| 222 | // ---------------------------------------------------------------------------- |
| 223 | // Canvas draw operations - special |
| 224 | // ---------------------------------------------------------------------------- |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 225 | virtual void drawRenderNode(RenderNode* renderNode, Rect& dirty, |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 226 | int32_t replayFlags) = 0; |
| 227 | |
| 228 | // TODO: rename for consistency |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 229 | virtual void callDrawGLFunction(Functor* functor, Rect& dirty) = 0; |
Chris Craik | b458942 | 2013-12-26 15:13:13 -0800 | [diff] [blame] | 230 | }; // class Renderer |
| 231 | |
| 232 | }; // namespace uirenderer |
| 233 | }; // namespace android |
| 234 | |
| 235 | #endif // ANDROID_HWUI_RENDERER_H |