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