| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/android/SkAndroidFrameworkUtils.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/effects/SkGradient.h" |
| 11 |
#include "include/private/base/SkTemplates.h" |
| 12 |
#include "include/utils/SkPaintFilterCanvas.h" |
| 13 |
#include "src/base/SkTLazy.h" |
| 14 |
#include "src/core/SkDevice.h" |
| 15 |
#include "src/image/SkSurface_Base.h" |
| 16 |
#include "src/shaders/SkShaderBase.h" |
| 17 |
|
| 18 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 19 |
#include <log/log.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#if defined(SK_GANESH) |
| 23 |
bool SkAndroidFrameworkUtils::clipWithStencil(SkCanvas* canvas) { |
| 24 |
return canvas->rootDevice()->android_utils_clipWithStencil(); |
| 25 |
} |
| 26 |
#endif |
| 27 |
|
| 28 |
void SkAndroidFrameworkUtils::SafetyNetLog(const char* bugNumber) { |
| 29 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 30 |
android_errorWriteLog(0x534e4554, bugNumber); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
sk_sp<SkSurface> SkAndroidFrameworkUtils::getSurfaceFromCanvas(SkCanvas* canvas) { |
| 35 |
sk_sp<SkSurface> surface(SkSafeRef(canvas->getSurfaceBase())); |
| 36 |
return surface; |
| 37 |
} |
| 38 |
|
| 39 |
int SkAndroidFrameworkUtils::SaveBehind(SkCanvas* canvas, const SkRect* subset) { |
| 40 |
return canvas->only_axis_aligned_saveBehind(subset); |
| 41 |
} |
| 42 |
|
| 43 |
void SkAndroidFrameworkUtils::ResetClip(SkCanvas* canvas) { |
| 44 |
canvas->internal_private_resetClip(); |
| 45 |
} |
| 46 |
|
| 47 |
SkCanvas* SkAndroidFrameworkUtils::getBaseWrappedCanvas(SkCanvas* canvas) { |
| 48 |
auto pfc = canvas->internal_private_asPaintFilterCanvas(); |
| 49 |
auto result = canvas; |
| 50 |
while (pfc) { |
| 51 |
result = pfc->proxy(); |
| 52 |
pfc = result->internal_private_asPaintFilterCanvas(); |
| 53 |
} |
| 54 |
return result; |
| 55 |
} |
| 56 |
|
| 57 |
// Skia's GradientInfo now stores bool for premulInterp. This is the legacy |
| 58 |
// flag it used to use to mean the same thing. |
| 59 |
constexpr uint8_t gSkGradientShader_Legacy_PremulFlag = 1; |
| 60 |
|
| 61 |
bool SkAndroidFrameworkUtils::ShaderAsALinearGradient(SkShader* shader, |
| 62 |
LinearGradientInfo* info) { |
| 63 |
SkASSERT(shader); |
| 64 |
std::optional<SkShaderBase::GradientInfo> baseInfo; |
| 65 |
if (info) { |
| 66 |
baseInfo.emplace(); |
| 67 |
baseInfo->fColorCount = info->fColorCount; |
| 68 |
baseInfo->fColors = info->fColors; |
| 69 |
baseInfo->fColorOffsets = info->fColorOffsets; |
| 70 |
} |
| 71 |
if (as_SB(shader)->asGradient(SkOptAddressOrNull(baseInfo)) != |
| 72 |
SkShaderBase::GradientType::kLinear) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
if (info) { |
| 76 |
info->fColorCount = baseInfo->fColorCount; // this is inout in asGradient() |
| 77 |
info->fPoints[0] = baseInfo->fPoint[0]; |
| 78 |
info->fPoints[1] = baseInfo->fPoint[1]; |
| 79 |
info->fTileMode = baseInfo->fTileMode; |
| 80 |
info->fGradientFlags = baseInfo->fPremulInterp ? gSkGradientShader_Legacy_PremulFlag : 0; |
| 81 |
} |
| 82 |
return true; |
| 83 |
} |