| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
| 9 #include "DMSrcSinkAndroid.h" | 9 #include "DMSrcSinkAndroid.h" |
| 10 | 10 |
| 11 #include "AnimationContext.h" | 11 #include "AnimationContext.h" |
| 12 #include "DisplayListRenderer.h" | 12 #include "DisplayListRenderer.h" |
| 13 #include "IContextFactory.h" | 13 #include "IContextFactory.h" |
| 14 #include "RenderNode.h" | 14 #include "RenderNode.h" |
| 15 #include "SkAndroidSDKCanvas.h" |
| 15 #include "SkCanvas.h" | 16 #include "SkCanvas.h" |
| 16 #include "SkiaCanvasProxy.h" | 17 #include "SkiaCanvasProxy.h" |
| 17 #include "SkTLazy.h" | 18 #include "SkTLazy.h" |
| 18 #include "SkMaskFilter.h" | 19 #include "SkMaskFilter.h" |
| 19 #include "SkPictureRecorder.h" | 20 #include "SkPictureRecorder.h" |
| 20 #include "SkStream.h" | 21 #include "SkStream.h" |
| 21 #include "android/rect.h" | 22 #include "android/rect.h" |
| 22 #include "android/native_window.h" | 23 #include "android/native_window.h" |
| 23 #include "gui/BufferQueue.h" | 24 #include "gui/BufferQueue.h" |
| 24 #include "gui/CpuConsumer.h" | 25 #include "gui/CpuConsumer.h" |
| 25 #include "gui/IGraphicBufferConsumer.h" | 26 #include "gui/IGraphicBufferConsumer.h" |
| 26 #include "gui/IGraphicBufferProducer.h" | 27 #include "gui/IGraphicBufferProducer.h" |
| 27 #include "gui/Surface.h" | 28 #include "gui/Surface.h" |
| 28 #include "renderthread/RenderProxy.h" | 29 #include "renderthread/RenderProxy.h" |
| 29 #include "renderthread/TimeLord.h" | 30 #include "renderthread/TimeLord.h" |
| 30 | 31 |
| 31 /* These functions are only compiled in the Android Framework. */ | 32 /* These functions are only compiled in the Android Framework. */ |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 /** Discard SkShaders not exposed by the Android Java API. */ | |
| 36 | |
| 37 void CheckShader(SkPaint* paint) { | |
| 38 SkShader* shader = paint->getShader(); | |
| 39 if (!shader) { | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 if (shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType) { | |
| 44 return; | |
| 45 } | |
| 46 if (shader->asACompose(NULL)) { | |
| 47 return; | |
| 48 } | |
| 49 SkShader::GradientType gtype = shader->asAGradient(NULL); | |
| 50 if (gtype == SkShader::kLinear_GradientType || | |
| 51 gtype == SkShader::kRadial_GradientType || | |
| 52 gtype == SkShader::kSweep_GradientType) { | |
| 53 return; | |
| 54 } | |
| 55 paint->setShader(NULL); | |
| 56 } | |
| 57 | |
| 58 /** Simplify a paint. */ | |
| 59 | |
| 60 void Filter(SkPaint* paint) { | |
| 61 | |
| 62 uint32_t flags = paint->getFlags(); | |
| 63 flags &= ~SkPaint::kLCDRenderText_Flag; | |
| 64 paint->setFlags(flags); | |
| 65 | |
| 66 // Android doesn't support Xfermodes above kLighten_Mode | |
| 67 SkXfermode::Mode mode; | |
| 68 SkXfermode::AsMode(paint->getXfermode(), &mode); | |
| 69 if (mode > SkXfermode::kLighten_Mode) { | |
| 70 paint->setXfermode(NULL); | |
| 71 } | |
| 72 | |
| 73 // Force bilinear scaling or none | |
| 74 if (paint->getFilterQuality() != kNone_SkFilterQuality) { | |
| 75 paint->setFilterQuality(kLow_SkFilterQuality); | |
| 76 } | |
| 77 | |
| 78 CheckShader(paint); | |
| 79 | |
| 80 // Android SDK only supports mode & matrix color filters | |
| 81 // (and, again, no modes above kLighten_Mode). | |
| 82 SkColorFilter* cf = paint->getColorFilter(); | |
| 83 if (cf) { | |
| 84 SkColor color; | |
| 85 SkXfermode::Mode mode; | |
| 86 SkScalar srcColorMatrix[20]; | |
| 87 bool isMode = cf->asColorMode(&color, &mode); | |
| 88 if (isMode && mode > SkXfermode::kLighten_Mode) { | |
| 89 paint->setColorFilter( | |
| 90 SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode
)); | |
| 91 } else if (!isMode && !cf->asColorMatrix(srcColorMatrix)) { | |
| 92 paint->setColorFilter(NULL); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 SkPathEffect* pe = paint->getPathEffect(); | |
| 97 if (pe && !pe->exposedInAndroidJavaAPI()) { | |
| 98 paint->setPathEffect(NULL); | |
| 99 } | |
| 100 | |
| 101 // TODO: Android doesn't support all the flags that can be passed to | |
| 102 // blur filters; we need plumbing to get them out. | |
| 103 | |
| 104 paint->setImageFilter(NULL); | |
| 105 paint->setLooper(NULL); | |
| 106 }; | |
| 107 | |
| 108 /** SkDrawFilter is likely to be deprecated; this is a proxy | |
| 109 canvas that does the same thing: alter SkPaint fields. | |
| 110 | |
| 111 onDraw*() functions may have their SkPaint modified, and are then | |
| 112 passed on to the same function on proxyTarget. | |
| 113 | |
| 114 This still suffers one of the same architectural flaws as SkDrawFilter: | |
| 115 TextBlob paints are incomplete when filter is called. | |
| 116 */ | |
| 117 | |
| 118 #define FILTER(p) \ | |
| 119 SkPaint filteredPaint(p); \ | |
| 120 Filter(&filteredPaint); | |
| 121 | |
| 122 #define FILTER_PTR(p) \ | |
| 123 SkTLazy<SkPaint> lazyPaint; \ | |
| 124 SkPaint* filteredPaint = (SkPaint*) p; \ | |
| 125 if (p) { \ | |
| 126 filteredPaint = lazyPaint.set(*p); \ | |
| 127 Filter(filteredPaint); \ | |
| 128 } | |
| 129 | |
| 130 | |
| 131 class FilteringCanvas : public SkCanvas { | |
| 132 public: | |
| 133 FilteringCanvas(SkCanvas* proxyTarget) : fProxyTarget(proxyTarget) { } | |
| 134 | |
| 135 protected: | |
| 136 void onDrawPaint(const SkPaint& paint) SK_OVERRIDE { | |
| 137 FILTER(paint); | |
| 138 fProxyTarget->drawPaint(filteredPaint); | |
| 139 } | |
| 140 void onDrawPoints(PointMode pMode, size_t count, const SkPoint pts[], | |
| 141 const SkPaint& paint) SK_OVERRIDE { | |
| 142 FILTER(paint); | |
| 143 fProxyTarget->drawPoints(pMode, count, pts, filteredPaint); | |
| 144 } | |
| 145 void onDrawOval(const SkRect& r, const SkPaint& paint) SK_OVERRIDE { | |
| 146 FILTER(paint); | |
| 147 fProxyTarget->drawOval(r, filteredPaint); | |
| 148 } | |
| 149 void onDrawRect(const SkRect& r, const SkPaint& paint) SK_OVERRIDE { | |
| 150 FILTER(paint); | |
| 151 fProxyTarget->drawRect(r, filteredPaint); | |
| 152 } | |
| 153 void onDrawRRect(const SkRRect& r, const SkPaint& paint) SK_OVERRIDE { | |
| 154 FILTER(paint); | |
| 155 fProxyTarget->drawRRect(r, filteredPaint); | |
| 156 } | |
| 157 void onDrawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE { | |
| 158 FILTER(paint); | |
| 159 fProxyTarget->drawPath(path, filteredPaint); | |
| 160 } | |
| 161 void onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, | |
| 162 const SkPaint* paint) SK_OVERRIDE { | |
| 163 FILTER_PTR(paint); | |
| 164 fProxyTarget->drawBitmap(bitmap, left, top, filteredPaint); | |
| 165 } | |
| 166 void onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRec
t& dst, | |
| 167 const SkPaint* paint, DrawBitmapRectFlags flags) SK_OV
ERRIDE { | |
| 168 FILTER_PTR(paint); | |
| 169 fProxyTarget->drawBitmapRectToRect(bitmap, src, dst, filteredPaint, flag
s); | |
| 170 } | |
| 171 void onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, | |
| 172 const SkRect& dst, const SkPaint* paint) SK_OVERRIDE { | |
| 173 FILTER_PTR(paint); | |
| 174 fProxyTarget->drawBitmapNine(bitmap, center, dst, filteredPaint); | |
| 175 } | |
| 176 void onDrawSprite(const SkBitmap& bitmap, int left, int top, | |
| 177 const SkPaint* paint) SK_OVERRIDE { | |
| 178 FILTER_PTR(paint); | |
| 179 fProxyTarget->drawSprite(bitmap, left, top, filteredPaint); | |
| 180 } | |
| 181 void onDrawVertices(VertexMode vMode, int vertexCount, const SkPoint vertice
s[], | |
| 182 const SkPoint texs[], const SkColor colors[], SkXfermode
* xMode, | |
| 183 const uint16_t indices[], int indexCount, | |
| 184 const SkPaint& paint) SK_OVERRIDE { | |
| 185 FILTER(paint); | |
| 186 fProxyTarget->drawVertices(vMode, vertexCount, vertices, texs, colors, | |
| 187 xMode, indices, indexCount, filteredPaint); | |
| 188 } | |
| 189 | |
| 190 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, | |
| 191 const SkPaint& paint) SK_OVERRIDE { | |
| 192 FILTER(paint); | |
| 193 fProxyTarget->drawDRRect(outer, inner, filteredPaint); | |
| 194 } | |
| 195 | |
| 196 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, | |
| 197 const SkPaint& paint) SK_OVERRIDE { | |
| 198 FILTER(paint); | |
| 199 fProxyTarget->drawText(text, byteLength, x, y, filteredPaint); | |
| 200 } | |
| 201 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], | |
| 202 const SkPaint& paint) SK_OVERRIDE { | |
| 203 FILTER(paint); | |
| 204 fProxyTarget->drawPosText(text, byteLength, pos, filteredPaint); | |
| 205 } | |
| 206 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos
[], | |
| 207 SkScalar constY, const SkPaint& paint) SK_OVERRIDE { | |
| 208 FILTER(paint); | |
| 209 fProxyTarget->drawPosTextH(text, byteLength, xpos, constY, filteredPaint
); | |
| 210 } | |
| 211 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat
h, | |
| 212 const SkMatrix* matrix, const SkPaint& paint) SK_OVERR
IDE { | |
| 213 FILTER(paint); | |
| 214 fProxyTarget->drawTextOnPath(text, byteLength, path, matrix, filteredPai
nt); | |
| 215 } | |
| 216 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, | |
| 217 const SkPaint& paint) SK_OVERRIDE { | |
| 218 FILTER(paint); | |
| 219 fProxyTarget->drawTextBlob(blob, x, y, filteredPaint); | |
| 220 } | |
| 221 | |
| 222 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], | |
| 223 const SkPoint texCoords[4], SkXfermode* xmode, | |
| 224 const SkPaint& paint) SK_OVERRIDE { | |
| 225 FILTER(paint); | |
| 226 fProxyTarget->drawPatch(cubics, colors, texCoords, xmode, filteredPaint)
; | |
| 227 } | |
| 228 | |
| 229 protected: | |
| 230 SkCanvas* fProxyTarget; | |
| 231 }; | |
| 232 | |
| 233 /** | 36 /** |
| 234 * Helper class for setting up android::uirenderer::renderthread::RenderProxy. | 37 * Helper class for setting up android::uirenderer::renderthread::RenderProxy. |
| 235 */ | 38 */ |
| 236 class ContextFactory : public android::uirenderer::IContextFactory { | 39 class ContextFactory : public android::uirenderer::IContextFactory { |
| 237 public: | 40 public: |
| 238 android::uirenderer::AnimationContext* createAnimationContext | 41 android::uirenderer::AnimationContext* createAnimationContext |
| 239 (android::uirenderer::renderthread::TimeLord& clock) SK_OVERRIDE { | 42 (android::uirenderer::renderthread::TimeLord& clock) SK_OVERRIDE { |
| 240 return new android::uirenderer::AnimationContext(clock); | 43 return new android::uirenderer::AnimationContext(clock); |
| 241 } | 44 } |
| 242 }; | 45 }; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 ProxySrc(const Src& src) | 181 ProxySrc(const Src& src) |
| 379 : fSrc(src) {} | 182 : fSrc(src) {} |
| 380 | 183 |
| 381 Error draw(SkCanvas* canvas) const SK_OVERRIDE { | 184 Error draw(SkCanvas* canvas) const SK_OVERRIDE { |
| 382 // Pass through HWUI's upper layers to get operational transforms | 185 // Pass through HWUI's upper layers to get operational transforms |
| 383 SkAutoTDelete<android::Canvas> ac (android::Canvas::create_canvas(ca
nvas)); | 186 SkAutoTDelete<android::Canvas> ac (android::Canvas::create_canvas(ca
nvas)); |
| 384 SkAutoTUnref<android::uirenderer::SkiaCanvasProxy> scProxy | 187 SkAutoTUnref<android::uirenderer::SkiaCanvasProxy> scProxy |
| 385 (new android::uirenderer::SkiaCanvasProxy(ac)); | 188 (new android::uirenderer::SkiaCanvasProxy(ac)); |
| 386 | 189 |
| 387 // Pass through another proxy to get paint transforms | 190 // Pass through another proxy to get paint transforms |
| 388 FilteringCanvas fc(scProxy); | 191 SkAndroidSDKCanvas fc; |
| 192 fc.reset(scProxy); |
| 389 | 193 |
| 390 fSrc.draw(&fc); | 194 fSrc.draw(&fc); |
| 391 | 195 |
| 392 return ""; | 196 return ""; |
| 393 } | 197 } |
| 394 SkISize size() const SK_OVERRIDE { return fSrc.size(); } | 198 SkISize size() const SK_OVERRIDE { return fSrc.size(); } |
| 395 Name name() const SK_OVERRIDE { sk_throw(); return ""; } | 199 Name name() const SK_OVERRIDE { sk_throw(); return ""; } |
| 396 } proxy(src); | 200 } proxy(src); |
| 397 | 201 |
| 398 return fSink->draw(proxy, bitmap, stream, log); | 202 return fSink->draw(proxy, bitmap, stream, log); |
| 399 } | 203 } |
| 400 | 204 |
| 401 } // namespace DM | 205 } // namespace DM |
| OLD | NEW |