blob: e4d9cb2e95aafc92268643980849b2d4749802bd [file] [log] [blame]
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/gl/GrGLGpu.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrBackendSemaphore.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/private/SkHalf.h"
#include "include/private/SkTemplates.h"
#include "include/private/SkTo.h"
#include "src/core/SkAutoMalloc.h"
#include "src/core/SkCompressedDataUtils.h"
#include "src/core/SkMipmap.h"
#include "src/core/SkTraceEvent.h"
#include "src/gpu/GrBackendUtils.h"
#include "src/gpu/GrCpuBuffer.h"
#include "src/gpu/GrDataUtils.h"
#include "src/gpu/GrDirectContextPriv.h"
#include "src/gpu/GrGpuResourcePriv.h"
#include "src/gpu/GrPipeline.h"
#include "src/gpu/GrProgramInfo.h"
#include "src/gpu/GrRenderTarget.h"
#include "src/gpu/GrShaderCaps.h"
#include "src/gpu/GrSurfaceProxyPriv.h"
#include "src/gpu/GrTexture.h"
#include "src/gpu/gl/GrGLAttachment.h"
#include "src/gpu/gl/GrGLBuffer.h"
#include "src/gpu/gl/GrGLOpsRenderPass.h"
#include "src/gpu/gl/GrGLSemaphore.h"
#include "src/gpu/gl/GrGLTextureRenderTarget.h"
#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
#include "src/sksl/SkSLCompiler.h"
#include <cmath>
#include <memory>
#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
#define GL_ALLOC_CALL(call) \
[&] { \
if (this->glCaps().skipErrorChecks()) { \
GR_GL_CALL(this->glInterface(), call); \
return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
} else { \
this->clearErrorsAndCheckForOOM(); \
GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
return this->getErrorAndCheckForOOM(); \
} \
}()
//#define USE_NSIGHT
///////////////////////////////////////////////////////////////////////////////
static const GrGLenum gXfermodeEquation2Blend[] = {
// Basic OpenGL blend equations.
GR_GL_FUNC_ADD,
GR_GL_FUNC_SUBTRACT,
GR_GL_FUNC_REVERSE_SUBTRACT,
// GL_KHR_blend_equation_advanced.
GR_GL_SCREEN,
GR_GL_OVERLAY,
GR_GL_DARKEN,
GR_GL_LIGHTEN,
GR_GL_COLORDODGE,
GR_GL_COLORBURN,
GR_GL_HARDLIGHT,
GR_GL_SOFTLIGHT,
GR_GL_DIFFERENCE,
GR_GL_EXCLUSION,
GR_GL_MULTIPLY,
GR_GL_HSL_HUE,
GR_GL_HSL_SATURATION,
GR_GL_HSL_COLOR,
GR_GL_HSL_LUMINOSITY,
// Illegal... needs to map to something.
GR_GL_FUNC_ADD,
};
static_assert(0 == kAdd_GrBlendEquation);
static_assert(1 == kSubtract_GrBlendEquation);
static_assert(2 == kReverseSubtract_GrBlendEquation);
static_assert(3 == kScreen_GrBlendEquation);
static_assert(4 == kOverlay_GrBlendEquation);
static_assert(5 == kDarken_GrBlendEquation);
static_assert(6 == kLighten_GrBlendEquation);
static_assert(7 == kColorDodge_GrBlendEquation);
static_assert(8 == kColorBurn_GrBlendEquation);
static_assert(9 == kHardLight_GrBlendEquation);
static_assert(10 == kSoftLight_GrBlendEquation);
static_assert(11 == kDifference_GrBlendEquation);
static_assert(12 == kExclusion_GrBlendEquation);
static_assert(13 == kMultiply_GrBlendEquation);
static_assert(14 == kHSLHue_GrBlendEquation);
static_assert(15 == kHSLSaturation_GrBlendEquation);
static_assert(16 == kHSLColor_GrBlendEquation);
static_assert(17 == kHSLLuminosity_GrBlendEquation);
static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
static const GrGLenum gXfermodeCoeff2Blend[] = {
GR_GL_ZERO,
GR_GL_ONE,
GR_GL_SRC_COLOR,
GR_GL_ONE_MINUS_SRC_COLOR,
GR_GL_DST_COLOR,
GR_GL_ONE_MINUS_DST_COLOR,
GR_GL_SRC_ALPHA,
GR_GL_ONE_MINUS_SRC_ALPHA,
GR_GL_DST_ALPHA,
GR_GL_ONE_MINUS_DST_ALPHA,
GR_GL_CONSTANT_COLOR,
GR_GL_ONE_MINUS_CONSTANT_COLOR,
// extended blend coeffs
GR_GL_SRC1_COLOR,
GR_GL_ONE_MINUS_SRC1_COLOR,
GR_GL_SRC1_ALPHA,
GR_GL_ONE_MINUS_SRC1_ALPHA,
// Illegal... needs to map to something.
GR_GL_ZERO,
};
//////////////////////////////////////////////////////////////////////////////
static int gl_target_to_binding_index(GrGLenum target) {
switch (target) {
case GR_GL_TEXTURE_2D:
return 0;
case GR_GL_TEXTURE_RECTANGLE:
return 1;
case GR_GL_TEXTURE_EXTERNAL:
return 2;
}
SK_ABORT("Unexpected GL texture target.");
}
GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
}
bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
}
void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
int targetIndex = gl_target_to_binding_index(target);
fTargetBindings[targetIndex].fBoundResourceID = resourceID;
fTargetBindings[targetIndex].fHasBeenModified = true;
}
void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
this->setBoundID(target, GrGpuResource::UniqueID());
}
void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
for (auto& targetBinding : fTargetBindings) {
targetBinding.fBoundResourceID.makeInvalid();
if (markUnmodified) {
targetBinding.fHasBeenModified = false;
}
}
}
//////////////////////////////////////////////////////////////////////////////
static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
switch (filter) {
case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR;
}
SkUNREACHABLE;
}
static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter,
GrSamplerState::MipmapMode mm) {
switch (mm) {
case GrSamplerState::MipmapMode::kNone:
return filter_to_gl_mag_filter(filter);
case GrSamplerState::MipmapMode::kNearest:
switch (filter) {
case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_NEAREST;
case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_NEAREST;
}
SkUNREACHABLE;
case GrSamplerState::MipmapMode::kLinear:
switch (filter) {
case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_LINEAR;
case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_LINEAR;
}
SkUNREACHABLE;
}
SkUNREACHABLE;
}
static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
const GrCaps& caps) {
switch (wrapMode) {
case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
case GrSamplerState::WrapMode::kClampToBorder:
// May not be supported but should have been caught earlier
SkASSERT(caps.clampToBorderSupport());
return GR_GL_CLAMP_TO_BORDER;
}
SkUNREACHABLE;
}
///////////////////////////////////////////////////////////////////////////////
class GrGLGpu::SamplerObjectCache {
public:
SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
fTextureUnitStates = std::make_unique<UnitState[]>(fNumTextureUnits);
std::fill_n(fSamplers, kNumSamplers, 0);
}
~SamplerObjectCache() {
if (!fNumTextureUnits) {
// We've already been abandoned.
return;
}
for (GrGLuint sampler : fSamplers) {
// The spec states that "zero" values should be silently ignored, however they still
// trigger GL errors on some NVIDIA platforms.
if (sampler) {
GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
}
}
}
void bindSampler(int unitIdx, GrSamplerState state) {
int index = state.asIndex();
if (!fSamplers[index]) {
GrGLuint s;
GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
if (!s) {
return;
}
fSamplers[index] = s;
GrGLenum minFilter = filter_to_gl_min_filter(state.filter(), state.mipmapMode());
GrGLenum magFilter = filter_to_gl_mag_filter(state.filter());
GrGLenum wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
GrGLenum wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
GR_GL_CALL(fGpu->glInterface(),
SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
GR_GL_CALL(fGpu->glInterface(),
SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
}
if (!fTextureUnitStates[unitIdx].fKnown ||
fTextureUnitStates[unitIdx].fSamplerIDIfKnown != fSamplers[index]) {
GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
fTextureUnitStates[unitIdx].fSamplerIDIfKnown = fSamplers[index];
fTextureUnitStates[unitIdx].fKnown = true;
}
}
void unbindSampler(int unitIdx) {
if (!fTextureUnitStates[unitIdx].fKnown ||
fTextureUnitStates[unitIdx].fSamplerIDIfKnown != 0) {
GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, 0));
fTextureUnitStates[unitIdx].fSamplerIDIfKnown = 0;
fTextureUnitStates[unitIdx].fKnown = true;
}
}
void invalidateBindings() {
std::fill_n(fTextureUnitStates.get(), fNumTextureUnits, UnitState{});
}
void abandon() {
fTextureUnitStates.reset();
fNumTextureUnits = 0;
}
void release() {
if (!fNumTextureUnits) {
// We've already been abandoned.
return;
}
GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
std::fill_n(fSamplers, kNumSamplers, 0);
// Deleting a bound sampler implicitly binds sampler 0. We just invalidate all of our
// knowledge.
std::fill_n(fTextureUnitStates.get(), fNumTextureUnits, UnitState{});
}
private:
static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
struct UnitState {
bool fKnown = false;
GrGLuint fSamplerIDIfKnown = 0;
};
GrGLGpu* fGpu;
std::unique_ptr<UnitState[]> fTextureUnitStates;
GrGLuint fSamplers[kNumSamplers];
int fNumTextureUnits;
};
///////////////////////////////////////////////////////////////////////////////
sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
GrDirectContext* direct) {
if (!interface) {
interface = GrGLMakeNativeInterface();
// For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
// to GrGLMakeNativeInterface.
if (!interface) {
interface = sk_ref_sp(GrGLCreateNativeInterface());
}
if (!interface) {
return nullptr;
}
}
#ifdef USE_NSIGHT
const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
#endif
auto glContext = GrGLContext::Make(std::move(interface), options);
if (!glContext) {
return nullptr;
}
return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
}
GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* dContext)
: GrGpu(dContext)
, fGLContext(std::move(ctx))
, fProgramCache(new ProgramCache(dContext->priv().options().fRuntimeProgramCacheSize))
, fHWProgramID(0)
, fTempSrcFBOID(0)
, fTempDstFBOID(0)
, fStencilClearFBOID(0)
, fFinishCallbacks(this) {
SkASSERT(fGLContext);
// Clear errors so we don't get confused whether we caused an error.
this->clearErrorsAndCheckForOOM();
// Toss out any pre-existing OOM that was hanging around before we got started.
this->checkAndResetOOMed();
this->initCapsAndCompiler(sk_ref_sp(fGLContext->caps()));
fHWTextureUnitBindings.reset(this->numTextureUnits());
this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
} else {
this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
}
for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
fHWBufferState[i].invalidate();
}
static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
if (this->glCaps().useSamplerObjects()) {
fSamplerObjectCache = std::make_unique<SamplerObjectCache>(this);
}
}
GrGLGpu::~GrGLGpu() {
// Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
// to release the resources held by the objects themselves.
fCopyProgramArrayBuffer.reset();
fMipmapProgramArrayBuffer.reset();
if (fProgramCache) {
fProgramCache->reset();
}
fHWProgram.reset();
if (fHWProgramID) {
// detach the current program so there is no confusion on OpenGL's part
// that we want it to be deleted
GL_CALL(UseProgram(0));
}
if (fTempSrcFBOID) {
this->deleteFramebuffer(fTempSrcFBOID);
}
if (fTempDstFBOID) {
this->deleteFramebuffer(fTempDstFBOID);
}
if (fStencilClearFBOID) {
this->deleteFramebuffer(fStencilClearFBOID);
}
for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
if (0 != fCopyPrograms[i].fProgram) {
GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
}
}
for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
if (0 != fMipmapPrograms[i].fProgram) {
GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
}
}
fSamplerObjectCache.reset();
fFinishCallbacks.callAll(true);
}
void GrGLGpu::disconnect(DisconnectType type) {
INHERITED::disconnect(type);
if (DisconnectType::kCleanup == type) {
if (fHWProgramID) {
GL_CALL(UseProgram(0));
}
if (fTempSrcFBOID) {
this->deleteFramebuffer(fTempSrcFBOID);
}
if (fTempDstFBOID) {
this->deleteFramebuffer(fTempDstFBOID);
}
if (fStencilClearFBOID) {
this->deleteFramebuffer(fStencilClearFBOID);
}
for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
if (fCopyPrograms[i].fProgram) {
GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
}
}
for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
if (fMipmapPrograms[i].fProgram) {
GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
}
}
if (fSamplerObjectCache) {
fSamplerObjectCache->release();
}
} else {
if (fProgramCache) {
fProgramCache->abandon();
}
if (fSamplerObjectCache) {
fSamplerObjectCache->abandon();
}
}
fHWProgram.reset();
fProgramCache->reset();
fProgramCache.reset();
fHWProgramID = 0;
fTempSrcFBOID = 0;
fTempDstFBOID = 0;
fStencilClearFBOID = 0;
fCopyProgramArrayBuffer.reset();
for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
fCopyPrograms[i].fProgram = 0;
}
fMipmapProgramArrayBuffer.reset();
for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
fMipmapPrograms[i].fProgram = 0;
}
fFinishCallbacks.callAll(/* doDelete */ DisconnectType::kCleanup == type);
}
GrThreadSafePipelineBuilder* GrGLGpu::pipelineBuilder() {
return fProgramCache.get();
}
sk_sp<GrThreadSafePipelineBuilder> GrGLGpu::refPipelineBuilder() {
return fProgramCache;
}
///////////////////////////////////////////////////////////////////////////////
void GrGLGpu::onResetContext(uint32_t resetBits) {
if (resetBits & kMisc_GrGLBackendState) {
// we don't use the zb at all
GL_CALL(Disable(GR_GL_DEPTH_TEST));
GL_CALL(DepthMask(GR_GL_FALSE));
// We don't use face culling.
GL_CALL(Disable(GR_GL_CULL_FACE));
// We do use separate stencil. Our algorithms don't care which face is front vs. back so
// just set this to the default for self-consistency.
GL_CALL(FrontFace(GR_GL_CCW));
this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
if (GR_IS_GR_GL(this->glStandard())) {
#ifndef USE_NSIGHT
// Desktop-only state that we never change
if (!this->glCaps().isCoreProfile()) {
GL_CALL(Disable(GR_GL_POINT_SMOOTH));
GL_CALL(Disable(GR_GL_LINE_SMOOTH));
GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
}
// The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
// core profile. This seems like a bug since the core spec removes any mention of
// GL_ARB_imaging.
if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
GL_CALL(Disable(GR_GL_COLOR_TABLE));
}
GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
fHWWireframeEnabled = kUnknown_TriState;
#endif
// Since ES doesn't support glPointSize at all we always use the VS to
// set the point size
GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
}
if (GR_IS_GR_GL_ES(this->glStandard()) &&
this->glCaps().fbFetchRequiresEnablePerSample()) {
// The arm extension requires specifically enabling MSAA fetching per sample.
// On some devices this may have a perf hit. Also multiple render targets are disabled
GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
}
fHWWriteToColor = kUnknown_TriState;
// we only ever use lines in hairline mode
GL_CALL(LineWidth(1));
GL_CALL(Disable(GR_GL_DITHER));
fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
}
if (resetBits & kMSAAEnable_GrGLBackendState) {
fMSAAEnabled = kUnknown_TriState;
if (this->caps()->mixedSamplesSupport()) {
// The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
// modulation. This state has no effect when not rendering to a mixed sampled target.
GL_CALL(CoverageModulation(GR_GL_RGBA));
}
fHWConservativeRasterEnabled = kUnknown_TriState;
}
fHWActiveTextureUnitIdx = -1; // invalid
fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
if (resetBits & kTextureBinding_GrGLBackendState) {
for (int s = 0; s < this->numTextureUnits(); ++s) {
fHWTextureUnitBindings[s].invalidateAllTargets(false);
}
if (fSamplerObjectCache) {
fSamplerObjectCache->invalidateBindings();
}
}
if (resetBits & kBlend_GrGLBackendState) {
fHWBlendState.invalidate();
}
if (resetBits & kView_GrGLBackendState) {
fHWScissorSettings.invalidate();
fHWWindowRectsState.invalidate();
fHWViewport.invalidate();
}
if (resetBits & kStencil_GrGLBackendState) {
fHWStencilSettings.invalidate();
fHWStencilTestEnabled = kUnknown_TriState;
}
// Vertex
if (resetBits & kVertex_GrGLBackendState) {
fHWVertexArrayState.invalidate();
this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
fHWPatchVertexCount = 0;
}
if (resetBits & kRenderTarget_GrGLBackendState) {
fHWBoundRenderTargetUniqueID.makeInvalid();
fHWSRGBFramebuffer = kUnknown_TriState;
fBoundDrawFramebuffer = 0;
}
// we assume these values
if (resetBits & kPixelStore_GrGLBackendState) {
if (this->caps()->writePixelsRowBytesSupport()) {
GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
}
if (this->glCaps().readPixelsRowBytesSupport()) {
GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
}
if (this->glCaps().packFlipYSupport()) {
GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
}
}
if (resetBits & kProgram_GrGLBackendState) {
fHWProgramID = 0;
fHWProgram.reset();
}
++fResetTimestampForTextureParameters;
}
static bool check_backend_texture(const GrBackendTexture& backendTex,
const GrGLCaps& caps,
GrGLTexture::Desc* desc,
bool skipRectTexSupportCheck = false) {
GrGLTextureInfo info;
if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
return false;
}
desc->fSize = {backendTex.width(), backendTex.height()};
desc->fTarget = info.fTarget;
desc->fID = info.fID;
desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
if (desc->fFormat == GrGLFormat::kUnknown) {
return false;
}
if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
if (!caps.shaderCaps()->externalTextureSupport()) {
return false;
}
} else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
return false;
}
} else if (GR_GL_TEXTURE_2D != desc->fTarget) {
return false;
}
if (backendTex.isProtected()) {
// Not supported in GL backend at this time.
return false;
}
return true;
}
sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
GrWrapOwnership ownership,
GrWrapCacheable cacheable,
GrIOType ioType) {
GrGLTexture::Desc desc;
if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
return nullptr;
}
if (kBorrow_GrWrapOwnership == ownership) {
desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
} else {
desc.fOwnership = GrBackendObjectOwnership::kOwned;
}
GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
: GrMipmapStatus::kNotAllocated;
auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
backendTex.getGLTextureParams(), cacheable, ioType);
if (this->glCaps().isFormatRenderable(backendTex.getBackendFormat(), 1)) {
// Pessimistically assume this external texture may have been bound to a FBO.
texture->baseLevelWasBoundToFBO();
}
return std::move(texture);
}
static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
const GrGLCaps& caps, GrGLTexture::Desc* desc,
bool skipRectTexSupportCheck = false) {
GrGLTextureInfo info;
if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
return false;
}
desc->fSize = {backendTex.width(), backendTex.height()};
desc->fTarget = info.fTarget;
desc->fID = info.fID;
desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
if (desc->fFormat == GrGLFormat::kUnknown) {
return false;
}
if (GR_GL_TEXTURE_2D != desc->fTarget) {
return false;
}
if (backendTex.isProtected()) {
// Not supported in GL backend at this time.
return false;
}
return true;
}
sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
GrWrapOwnership ownership,
GrWrapCacheable cacheable) {
GrGLTexture::Desc desc;
if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
return nullptr;
}
if (kBorrow_GrWrapOwnership == ownership) {
desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
} else {
desc.fOwnership = GrBackendObjectOwnership::kOwned;
}
GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
: GrMipmapStatus::kNotAllocated;
auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
backendTex.getGLTextureParams(), cacheable,
kRead_GrIOType);
return std::move(texture);
}
sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
int sampleCnt,
GrWrapOwnership ownership,
GrWrapCacheable cacheable) {
const GrGLCaps& caps = this->glCaps();
GrGLTexture::Desc desc;
if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
return nullptr;
}
SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
SkASSERT(caps.isFormatTexturable(desc.fFormat));
// We don't support rendering to a EXTERNAL texture.
if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
return nullptr;
}
if (kBorrow_GrWrapOwnership == ownership) {
desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
} else {
desc.fOwnership = GrBackendObjectOwnership::kOwned;
}
sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
SkASSERT(sampleCnt);
GrGLRenderTarget::IDs rtIDs;
if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
return nullptr;
}
GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
: GrMipmapStatus::kNotAllocated;
sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
mipmapStatus));
texRT->baseLevelWasBoundToFBO();
return std::move(texRT);
}
sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
GrGLFramebufferInfo info;
if (!backendRT.getGLFramebufferInfo(&info)) {
return nullptr;
}
if (backendRT.isProtected()) {
// Not supported in GL at this time.
return nullptr;
}
const auto format = backendRT.getBackendFormat().asGLFormat();
if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
return nullptr;
}
GrGLRenderTarget::IDs rtIDs;
rtIDs.fRTFBOID = info.fFBOID;
rtIDs.fMSColorRenderbufferID = 0;
rtIDs.fSingleSampleFBOID = GrGLRenderTarget::kUnresolvableFBOID;
rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
backendRT.stencilBits());
}
static bool check_write_and_transfer_input(GrGLTexture* glTex) {
if (!glTex) {
return false;
}
// Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
return false;
}
return true;
}
bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
GrColorType surfaceColorType, GrColorType srcColorType,
const GrMipLevel texels[], int mipLevelCount,
bool prepForTexSampling) {
auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
if (!check_write_and_transfer_input(glTex)) {
return false;
}
this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
// If we have mips make sure the base/max levels cover the full range so that the uploads go to
// the right levels. We've found some Radeons require this.
if (mipLevelCount && this->glCaps().mipmapLevelControlSupport()) {
auto params = glTex->parameters();
GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
int maxLevel = glTex->maxMipmapLevel();
if (params->nonsamplerState().fBaseMipMapLevel != 0) {
GL_CALL(TexParameteri(glTex->target(), GR_GL_TEXTURE_BASE_LEVEL, 0));
nonsamplerState.fBaseMipMapLevel = 0;
}
if (params->nonsamplerState().fMaxMipmapLevel != maxLevel) {
GL_CALL(TexParameteri(glTex->target(), GR_GL_TEXTURE_MAX_LEVEL, maxLevel));
nonsamplerState.fBaseMipMapLevel = maxLevel;
}
params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
}
SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
glTex->target(), dstRect, srcColorType, texels,
mipLevelCount);
}
bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
GrColorType textureColorType, GrColorType bufferColorType,
sk_sp<GrGpuBuffer> transferBuffer, size_t offset,
size_t rowBytes) {
GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
// Can't transfer compressed data
SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
if (!check_write_and_transfer_input(glTex)) {
return false;
}
static_assert(sizeof(int) == sizeof(int32_t), "");
if (width <= 0 || height <= 0) {
return false;
}
this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
SkASSERT(!transferBuffer->isMapped());
SkASSERT(!transferBuffer->isCpuBuffer());
const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer.get());
this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
SkDEBUGCODE(
SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
SkASSERT(bounds.contains(subRect));
)
size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
const size_t trimRowBytes = width * bpp;
const void* pixels = (void*)offset;
if (width < 0 || height < 0) {
return false;
}
bool restoreGLRowLength = false;
if (trimRowBytes != rowBytes) {
// we should have checked for this support already
SkASSERT(this->glCaps().writePixelsRowBytesSupport());
GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
restoreGLRowLength = true;
}
GrGLFormat textureFormat = glTex->format();
// External format and type come from the upload data.
GrGLenum externalFormat = 0;
GrGLenum externalType = 0;
this->glCaps().getTexSubImageExternalFormatAndType(
textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
if (!externalFormat || !externalType) {
return false;
}
GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
GL_CALL(TexSubImage2D(glTex->target(),
0,
left, top,
width,
height,
externalFormat, externalType,
pixels));
if (restoreGLRowLength) {
GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
}
return true;
}
bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
GrColorType surfaceColorType, GrColorType dstColorType,
sk_sp<GrGpuBuffer> transferBuffer, size_t offset) {
auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer.get());
this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
auto offsetAsPtr = reinterpret_cast<void*>(offset);
return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
dstColorType, offsetAsPtr, width);
}
void GrGLGpu::unbindXferBuffer(GrGpuBufferType type) {
SkASSERT(type == GrGpuBufferType::kXferCpuToGpu || type == GrGpuBufferType::kXferGpuToCpu);
auto* xferBufferState = this->hwBufferState(type);
if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
xferBufferState->invalidate();
}
}
bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
GrColorType textureColorType,
SkISize texDims,
GrGLenum target,
SkIRect dstRect,
GrColorType srcColorType,
const GrMipLevel texels[],
int mipLevelCount) {
// If we're uploading compressed data then we should be using uploadCompressedTexData
SkASSERT(!GrGLFormatIsCompressed(textureFormat));
SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
// External format and type come from the upload data.
GrGLenum externalFormat;
GrGLenum externalType;
this->glCaps().getTexSubImageExternalFormatAndType(
textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
if (!externalFormat || !externalType) {
return false;
}
this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
mipLevelCount);
return true;
}
bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
SkISize texDims,
GrGLenum target,
SkColor4f color,
uint32_t levelMask) {
GrColorType colorType;
GrGLenum externalFormat, externalType;
this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
&externalType, &colorType);
if (colorType == GrColorType::kUnknown) {
return false;
}
std::unique_ptr<char[]> pixelStorage;
size_t bpp = 0;
int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
SkSTArray<16, GrMipLevel> levels;
levels.resize(numLevels);
SkISize levelDims = texDims;
for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
std::max(levelDims.height() >> 1, 1)}) {
if (levelMask & (1 << i)) {
if (!pixelStorage) {
// Make one tight image at the first size and reuse it for smaller levels.
GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
size_t rb = ii.minRowBytes();
pixelStorage.reset(new char[rb * levelDims.height()]);
if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
return false;
}
bpp = ii.bpp();
}
levels[i] = {pixelStorage.get(), levelDims.width()*bpp, nullptr};
}
}
this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
bpp, levels.begin(), levels.count());
return true;
}
void GrGLGpu::uploadTexData(SkISize texDims,
GrGLenum target,
SkIRect dstRect,
GrGLenum externalFormat,
GrGLenum externalType,
size_t bpp,
const GrMipLevel texels[],
int mipLevelCount) {
SkASSERT(!texDims.isEmpty());
SkASSERT(!dstRect.isEmpty());
SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
const GrGLCaps& caps = this->glCaps();
bool restoreGLRowLength = false;
this->unbindXferBuffer(GrGpuBufferType::kXferCpuToGpu);
GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
SkISize dims = dstRect.size();
for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
std::max(dims.height() >> 1, 1)}) {
if (!texels[level].fPixels) {
continue;
}
const size_t trimRowBytes = dims.width() * bpp;
const size_t rowBytes = texels[level].fRowBytes;
if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
restoreGLRowLength = true;
} else {
SkASSERT(rowBytes == trimRowBytes);
}
GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
externalFormat, externalType, texels[level].fPixels));
}
if (restoreGLRowLength) {
SkASSERT(caps.writePixelsRowBytesSupport());
GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
}
}
bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
GrGLFormat format,
SkISize dimensions,
GrMipmapped mipMapped,
GrGLenum target,
const void* data, size_t dataSize) {
SkASSERT(format != GrGLFormat::kUnknown);
const GrGLCaps& caps = this->glCaps();
// We only need the internal format for compressed 2D textures.
GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
if (!internalFormat) {
return false;
}
SkASSERT(compressionType != SkImage::CompressionType::kNone);
bool useTexStorage = caps.formatSupportsTexStorage(format);
int numMipLevels = 1;
if (mipMapped == GrMipmapped::kYes) {
numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
}
// TODO: Make sure that the width and height that we pass to OpenGL
// is a multiple of the block size.
if (useTexStorage) {
// We never resize or change formats of textures.
GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
dimensions.width(), dimensions.height()));
if (error != GR_GL_NO_ERROR) {
return false;
}
size_t offset = 0;
for (int level = 0; level < numMipLevels; ++level) {
size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
nullptr, false);
error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
level,
0, // left
0, // top
dimensions.width(),
dimensions.height(),
internalFormat,
SkToInt(levelDataSize),
&((char*)data)[offset]));
if (error != GR_GL_NO_ERROR) {
return false;
}
offset += levelDataSize;
dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
}
} else {
size_t offset = 0;
for (int level = 0; level < numMipLevels; ++level) {
size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
nullptr, false);
const char* rawLevelData = &((char*)data)[offset];
GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
level,
internalFormat,
dimensions.width(),
dimensions.height(),
0, // border
SkToInt(levelDataSize),
rawLevelData));
if (error != GR_GL_NO_ERROR) {
return false;
}
offset += levelDataSize;
dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
}
}
return true;
}
bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
int width, int height) {
SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
GrGLenum error;
switch (ctx.caps()->msFBOType()) {
case GrGLCaps::kStandard_MSFBOType:
error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
format, width, height));
break;
case GrGLCaps::kES_Apple_MSFBOType:
error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
GR_GL_RENDERBUFFER, sampleCount, format, width, height));
break;
case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
GR_GL_RENDERBUFFER, sampleCount, format, width, height));
break;
case GrGLCaps::kNone_MSFBOType:
SkUNREACHABLE;
break;
}
return error == GR_GL_NO_ERROR;
}
bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
int sampleCount,
GrGLRenderTarget::IDs* rtIDs) {
rtIDs->fMSColorRenderbufferID = 0;
rtIDs->fRTFBOID = 0;
rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
rtIDs->fSingleSampleFBOID = 0;
GrGLenum colorRenderbufferFormat = 0; // suppress warning
if (desc.fFormat == GrGLFormat::kUnknown) {
goto FAILED;
}
if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
goto FAILED;
}
GL_CALL(GenFramebuffers(1, &rtIDs->fSingleSampleFBOID));
if (!rtIDs->fSingleSampleFBOID) {
goto FAILED;
}
// If we are using multisampling we will create two FBOS. We render to one and then resolve to
// the texture bound to the other. The exception is the IMG multisample extension. With this
// extension the texture is multisampled when rendered to and then auto-resolves it when it is
// rendered from.
if (sampleCount <= 1) {
rtIDs->fRTFBOID = rtIDs->fSingleSampleFBOID;
} else {
GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
if (!rtIDs->fRTFBOID) {
goto FAILED;
}
if (!this->glCaps().usesImplicitMSAAResolve()) {
GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
if (!rtIDs->fMSColorRenderbufferID) {
goto FAILED;
}
colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
}
}
// below here we may bind the FBO
fHWBoundRenderTargetUniqueID.makeInvalid();
if (rtIDs->fMSColorRenderbufferID) {
SkASSERT(sampleCount > 1);
GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
desc.fSize.width(), desc.fSize.height())) {
goto FAILED;
}
this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_COLOR_ATTACHMENT0,
GR_GL_RENDERBUFFER,
rtIDs->fMSColorRenderbufferID));
} else if (sampleCount > 1) {
// multisampled_render_to_texture
SkASSERT(this->glCaps().usesImplicitMSAAResolve()); // Otherwise fMSColorRenderbufferID!=0.
SkASSERT(rtIDs->fRTFBOID != rtIDs->fSingleSampleFBOID);
this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
GR_GL_COLOR_ATTACHMENT0,
desc.fTarget,
desc.fID,
0,
sampleCount));
}
this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fSingleSampleFBOID);
GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
GR_GL_COLOR_ATTACHMENT0,
desc.fTarget,
desc.fID,
0));
return true;
FAILED:
if (rtIDs->fMSColorRenderbufferID) {
GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
}
if (rtIDs->fRTFBOID != rtIDs->fSingleSampleFBOID) {
this->deleteFramebuffer(rtIDs->fRTFBOID);
}
if (rtIDs->fSingleSampleFBOID) {
this->deleteFramebuffer(rtIDs->fSingleSampleFBOID);
}
return false;
}
// good to set a break-point here to know when createTexture fails
static sk_sp<GrTexture> return_null_texture() {
// SkDEBUGFAIL("null texture");
return nullptr;
}
static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
const GrGLInterface* interface, GrGLenum target) {
// Some drivers like to know filter/wrap before seeing glTexImage2D. Some
// drivers have a bug where an FBO won't be complete if it includes a
// texture that is not mipmap complete (considering the filter in use).
GrGLTextureParameters::SamplerOverriddenState state;
state.fMinFilter = GR_GL_NEAREST;
state.fMagFilter = GR_GL_NEAREST;
state.fWrapS = GR_GL_CLAMP_TO_EDGE;
state.fWrapT = GR_GL_CLAMP_TO_EDGE;
GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
return state;
}
sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
const GrBackendFormat& format,
GrRenderable renderable,
int renderTargetSampleCnt,
SkBudgeted budgeted,
GrProtected isProtected,
int mipLevelCount,
uint32_t levelClearMask) {
// We don't support protected textures in GL.
if (isProtected == GrProtected::kYes) {
return nullptr;
}
SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
SkASSERT(mipLevelCount > 0);
GrMipmapStatus mipmapStatus =
mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
GrGLTextureParameters::SamplerOverriddenState initialState;
GrGLTexture::Desc texDesc;
texDesc.fSize = dimensions;
switch (format.textureType()) {
case GrTextureType::kExternal:
case GrTextureType::kNone:
return nullptr;
case GrTextureType::k2D:
texDesc.fTarget = GR_GL_TEXTURE_2D;
break;
case GrTextureType::kRectangle:
if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
return nullptr;
}
texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
break;
}
texDesc.fFormat = format.asGLFormat();
texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
&initialState, mipLevelCount);
if (!texDesc.fID) {
return return_null_texture();
}
sk_sp<GrGLTexture> tex;
if (renderable == GrRenderable::kYes) {
// unbind the texture from the texture unit before binding it to the frame buffer
GL_CALL(BindTexture(texDesc.fTarget, 0));
GrGLRenderTarget::IDs rtIDDesc;
if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
GL_CALL(DeleteTextures(1, &texDesc.fID));
return return_null_texture();
}
tex = sk_make_sp<GrGLTextureRenderTarget>(
this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
tex->baseLevelWasBoundToFBO();
} else {
tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
}
// The non-sampler params are still at their default values.
tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
fResetTimestampForTextureParameters);
if (levelClearMask) {
if (this->glCaps().clearTextureSupport()) {
GrGLenum externalFormat, externalType;
GrColorType colorType;
this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
texDesc.fFormat, &externalFormat, &externalType, &colorType);
for (int i = 0; i < mipLevelCount; ++i) {
if (levelClearMask & (1U << i)) {
GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
nullptr));
}
}
} else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
!this->glCaps().performColorClearsAsDraws()) {
this->flushScissorTest(GrScissorTest::kDisabled);
this->disableWindowRectangles();
this->flushColorWrite(true);
this->flushClearColor({0, 0, 0, 0});
for (int i = 0; i < mipLevelCount; ++i) {
if (levelClearMask & (1U << i)) {
this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
kDst_TempFBOTarget);
GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
}
}
fHWBoundRenderTargetUniqueID.makeInvalid();
} else {
this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
levelClearMask);
}
}
return std::move(tex);
}
sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
const GrBackendFormat& format,
SkBudgeted budgeted,
GrMipmapped mipMapped,
GrProtected isProtected,
const void* data, size_t dataSize) {
// We don't support protected textures in GL.
if (isProtected == GrProtected::kYes) {
return nullptr;
}
SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
GrGLTextureParameters::SamplerOverriddenState initialState;
GrGLTexture::Desc desc;
desc.fSize = dimensions;
desc.fTarget = GR_GL_TEXTURE_2D;
desc.fOwnership = GrBackendObjectOwnership::kOwned;
desc.fFormat = format.asGLFormat();
desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
mipMapped, &initialState);
if (!desc.fID) {
return nullptr;
}
if (data) {
if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
GR_GL_TEXTURE_2D, data, dataSize)) {
GL_CALL(DeleteTextures(1, &desc.fID));
return nullptr;
}
}
// Unbind this texture from the scratch texture unit.
this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
? GrMipmapStatus::kValid
: GrMipmapStatus::kNotAllocated;
auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
// The non-sampler params are still at their default values.
tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
fResetTimestampForTextureParameters);
return std::move(tex);
}
GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
GrProtected isProtected) {
// We don't support protected textures in GL.
if (isProtected == GrProtected::kYes) {
return {};
}
this->handleDirtyContext();
GrGLFormat glFormat = format.asGLFormat();
if (glFormat == GrGLFormat::kUnknown) {
return {};
}
SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
GrGLTextureInfo info;
GrGLTextureParameters::SamplerOverriddenState initialState;
info.fTarget = GR_GL_TEXTURE_2D;
info.fFormat = GrGLFormatToEnum(glFormat);
info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
mipMapped, &initialState);
if (!info.fID) {
return {};
}
// Unbind this texture from the scratch texture unit.
this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
auto parameters = sk_make_sp<GrGLTextureParameters>();
// The non-sampler params are still at their default values.
parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
fResetTimestampForTextureParameters);
return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
std::move(parameters));
}
bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
sk_sp<GrRefCntedCallback> finishedCallback,
const BackendTextureData* data) {
SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
this->handleDirtyContext();
GrGLTextureInfo info;
SkAssertResult(backendTexture.getGLTextureInfo(&info));
GrBackendFormat format = backendTexture.getBackendFormat();
GrGLFormat glFormat = format.asGLFormat();
if (glFormat == GrGLFormat::kUnknown) {
return false;
}
SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
const char* rawData = nullptr;
size_t rawDataSize = 0;
SkAutoMalloc am;
if (data->type() == BackendTextureData::Type::kCompressed) {
rawData = (const char*)data->compressedData();
rawDataSize = data->compressedSize();
} else {
SkASSERT(data->type() == BackendTextureData::Type::kColor);
SkASSERT(compression != SkImage::CompressionType::kNone);
rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
backendTexture.hasMipmaps());
am.reset(rawDataSize);
GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
data->color());
rawData = (const char*)am.get();
}
this->bindTextureToScratchUnit(info.fTarget, info.fID);
// If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
// so that the uploads go to the right levels.
if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelControlSupport()) {
auto params = backendTexture.getGLTextureParams();
GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
if (params->nonsamplerState().fBaseMipMapLevel != 0) {
GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
nonsamplerState.fBaseMipMapLevel = 0;
}
int numMipLevels =
SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
}
params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
}
bool result = this->uploadCompressedTexData(
compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
rawData, rawDataSize);
// Unbind this texture from the scratch texture unit.
this->bindTextureToScratchUnit(info.fTarget, 0);
return result;
}
int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
static const int kSize = 16;
SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
// Default to unsupported, set this if we find a stencil format that works.
int firstWorkingStencilFormatIndex = -1;
GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
GrRenderable::kYes, nullptr, 1);
if (!colorID) {
return -1;
}
// unbind the texture from the texture unit before binding it to the frame buffer
GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
// Create Framebuffer
GrGLuint fb = 0;
GL_CALL(GenFramebuffers(1, &fb));
this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
fHWBoundRenderTargetUniqueID.makeInvalid();
GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
GR_GL_COLOR_ATTACHMENT0,
GR_GL_TEXTURE_2D,
colorID,
0));
GrGLuint sbRBID = 0;
GL_CALL(GenRenderbuffers(1, &sbRBID));
// look over formats till I find a compatible one
int stencilFmtCnt = this->glCaps().stencilFormats().count();
if (sbRBID) {
GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
GrGLFormat sFmt = this->glCaps().stencilFormats()[i];
GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
GR_GL_RENDERBUFFER, GrGLFormatToEnum(sFmt), kSize, kSize));
if (error == GR_GL_NO_ERROR) {
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_STENCIL_ATTACHMENT,
GR_GL_RENDERBUFFER, sbRBID));
if (GrGLFormatIsPackedDepthStencil(sFmt)) {
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_DEPTH_ATTACHMENT,
GR_GL_RENDERBUFFER, sbRBID));
} else {
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_DEPTH_ATTACHMENT,
GR_GL_RENDERBUFFER, 0));
}
GrGLenum status;
GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
firstWorkingStencilFormatIndex = i;
break;
}
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_STENCIL_ATTACHMENT,
GR_GL_RENDERBUFFER, 0));
if (GrGLFormatIsPackedDepthStencil(sFmt)) {
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
GR_GL_DEPTH_ATTACHMENT,
GR_GL_RENDERBUFFER, 0));
}
}
}
GL_CALL(DeleteRenderbuffers(1, &sbRBID));
}
GL_CALL(DeleteTextures(1, &colorID));
this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
this->deleteFramebuffer(fb);
fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
}
return this->glCaps().getStencilFormatIndexForFormat(format);
}
GrGLuint GrGLGpu::createCompressedTexture2D(
SkISize dimensions,
SkImage::CompressionType compression,
GrGLFormat format,
GrMipmapped mipMapped,
GrGLTextureParameters::SamplerOverriddenState* initialState) {
if (format == GrGLFormat::kUnknown) {
return 0;
}
GrGLuint id = 0;
GL_CALL(GenTextures(1, &id));
if (!id) {
return 0;
}
this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
*initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
return id;
}
GrGLuint GrGLGpu::createTexture(SkISize dimensions,
GrGLFormat format,
GrGLenum target,
GrRenderable renderable,
GrGLTextureParameters::SamplerOverriddenState* initialState,
int mipLevelCount) {
SkASSERT(format != GrGLFormat::kUnknown);
SkASSERT(!GrGLFormatIsCompressed(format));
GrGLuint id = 0;
GL_CALL(GenTextures(1, &id));
if (!id) {
return 0;
}
this->bindTextureToScratchUnit(target, id);
if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
// provides a hint about how this texture will be used
GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
}
if (initialState) {
*initialState = set_initial_texture_params(this->glInterface(), target);
} else {
set_initial_texture_params(this->glInterface(), target);
}
GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
bool success = false;
if (internalFormat) {
if (this->glCaps().formatSupportsTexStorage(format)) {
auto levelCount = std::max(mipLevelCount, 1);
GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
dimensions.width(), dimensions.height()));
success = (error == GR_GL_NO_ERROR);
} else {
GrGLenum externalFormat, externalType;
this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
GrGLenum error = GR_GL_NO_ERROR;
if (externalFormat && externalType) {
for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
const int twoToTheMipLevel = 1 << level;
const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
currentHeight, 0, externalFormat, externalType,
nullptr));
}
success = (error == GR_GL_NO_ERROR);
}
}
}
if (success) {
return id;
}
GL_CALL(DeleteTextures(1, &id));
return 0;
}
sk_sp<GrAttachment> GrGLGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
SkISize dimensions,
int numStencilSamples) {
SkASSERT(dimensions.width() >= rt->width());
SkASSERT(dimensions.height() >= rt->height());
GrGLAttachment::IDDesc sbDesc;
int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
if (sIdx < 0) {
return nullptr;
}
if (!sbDesc.fRenderbufferID) {
GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
}
if (!sbDesc.fRenderbufferID) {
return nullptr;
}
GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
GrGLFormat sFmt = this->glCaps().stencilFormats()[sIdx];
GrGLenum glFmt = GrGLFormatToEnum(sFmt);
// we do this "if" so that we don't call the multisample
// version on a GL that doesn't have an MSAA extension.
if (numStencilSamples > 1) {
if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, glFmt,
dimensions.width(), dimensions.height())) {
GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
return nullptr;
}
} else {
GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, glFmt,
dimensions.width(),
dimensions.height()));
if (error != GR_GL_NO_ERROR) {
GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
return nullptr;
}
}
fStats.incStencilAttachmentCreates();
return sk_sp<GrAttachment>(new GrGLAttachment(
this, sbDesc, dimensions, GrAttachment::UsageFlags::kStencilAttachment,
numStencilSamples, sFmt));
}
////////////////////////////////////////////////////////////////////////////////
sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
GrAccessPattern accessPattern, const void* data) {
return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
}
void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
if (GrScissorTest::kEnabled == scissorTest) {
if (kYes_TriState != fHWScissorSettings.fEnabled) {
GL_CALL(Enable(GR_GL_SCISSOR_TEST));
fHWScissorSettings.fEnabled = kYes_TriState;
}
} else {
if (kNo_TriState != fHWScissorSettings.fEnabled) {
GL_CALL(Disable(GR_GL_SCISSOR_TEST));
fHWScissorSettings.fEnabled = kNo_TriState;
}
}
}
void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtHeight, GrSurfaceOrigin rtOrigin) {
SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
if (fHWScissorSettings.fRect != nativeScissor) {
GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
nativeScissor.fHeight));
fHWScissorSettings.fRect = nativeScissor;
}
}
void GrGLGpu::flushViewport(const SkIRect& viewport, int rtHeight, GrSurfaceOrigin rtOrigin) {
auto nativeViewport = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, viewport);
if (fHWViewport != nativeViewport) {
GL_CALL(Viewport(nativeViewport.fX, nativeViewport.fY,
nativeViewport.fWidth, nativeViewport.fHeight));
fHWViewport = nativeViewport;
}
}
void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
#ifndef USE_NSIGHT
typedef GrWindowRectsState::Mode Mode;
SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
if (!this->caps()->maxWindowRectangles() ||
fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
return;
}
// This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
// assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
SkASSERT(windowState.numWindows() == numWindows);
GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
const SkIRect* skwindows = windowState.windows().data();
for (int i = 0; i < numWindows; ++i) {
glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
}
GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
#endif
}
void GrGLGpu::disableWindowRectangles() {
#ifndef USE_NSIGHT
if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
return;
}
GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
fHWWindowRectsState.setDisabled();
#endif
}
bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
this->handleDirtyContext();
sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(this->getContext(),
renderTarget, programInfo);
if (!program) {
GrCapsDebugf(this->caps(), "Failed to create program!\n");
return false;
}
this->flushProgram(std::move(program));
if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
}
// Swizzle the blend to match what the shader will output.
this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
programInfo.pipeline().writeSwizzle());
fHWProgram->updateUniforms(renderTarget, programInfo);
GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
GrStencilSettings stencil;
if (programInfo.isStencilEnabled()) {
SkASSERT(glRT->getStencilAttachment());
stencil.reset(*programInfo.userStencilSettings(),
programInfo.pipeline().hasStencilClip(),
glRT->numStencilBits());
}
this->flushStencil(stencil, programInfo.origin());
this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
glRT, programInfo.origin());
this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
this->flushWireframeState(programInfo.pipeline().isWireframe());
// This must come after textures are flushed because a texture may need
// to be msaa-resolved (which will modify bound FBO state).
this->flushRenderTarget(glRT);
return true;
}
void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
if (!program) {
fHWProgram.reset();
fHWProgramID = 0;
return;
}
SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
if (program == fHWProgram) {
return;
}
auto id = program->programID();
SkASSERT(id);
GL_CALL(UseProgram(id));
fHWProgram = std::move(program);
fHWProgramID = id;
}
void GrGLGpu::flushProgram(GrGLuint id) {
SkASSERT(id);
if (fHWProgramID == id) {
SkASSERT(!fHWProgram);
return;
}
fHWProgram.reset();
GL_CALL(UseProgram(id));
fHWProgramID = id;
}
GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
this->handleDirtyContext();
// Index buffer state is tied to the vertex array.
if (GrGpuBufferType::kIndex == type) {
this->bindVertexArray(0);
}
auto* bufferState = this->hwBufferState(type);
if (buffer->isCpuBuffer()) {
if (!bufferState->fBufferZeroKnownBound) {
GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
bufferState->fBufferZeroKnownBound = true;
bufferState->fBoundBufferUniqueID.makeInvalid();
}
} else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
bufferState->fBoundBufferUniqueID) {
const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
bufferState->fBufferZeroKnownBound = false;
bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
}
return bufferState->fGLTarget;
}
void GrGLGpu::clear(const GrScissorState& scissor,
std::array<float, 4> color,
GrRenderTarget* target,
GrSurfaceOrigin origin) {
// parent class should never let us get here with no RT
SkASSERT(target);
SkASSERT(!this->caps()->performColorClearsAsDraws());
SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
this->handleDirtyContext();
GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
if (scissor.enabled()) {
this->flushRenderTarget(glRT, origin, scissor.rect());
} else {
this->flushRenderTarget(glRT);
}
this->flushScissor(scissor, glRT->height(), origin);
this->disableWindowRectangles();
this->flushColorWrite(true);
this->flushClearColor(color);
GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
}
static bool use_tiled_rendering(const GrGLCaps& glCaps,
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
// Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
// Otherwise it's faster to just not use it.
return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
}
void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
this->handleDirtyContext();
auto glRT = static_cast<GrGLRenderTarget*>(rt);
this->flushRenderTarget(glRT);
SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
nativeBounds.fHeight, preserveMask));
}
GrGLbitfield clearMask = 0;
if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
SkASSERT(!this->caps()->performColorClearsAsDraws());
this->flushClearColor(colorLoadStore.fClearColor);
this->flushColorWrite(true);
clearMask |= GR_GL_COLOR_BUFFER_BIT;
}
if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
SkASSERT(!this->caps()->performStencilClearsAsDraws());
GL_CALL(StencilMask(0xffffffff));
GL_CALL(ClearStencil(0));
clearMask |= GR_GL_STENCIL_BUFFER_BIT;
}
if (clearMask) {
this->flushScissorTest(GrScissorTest::kDisabled);
this->disableWindowRectangles();
GL_CALL(Clear(clearMask));
}
}
void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
this->handleDirtyContext();
if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
// The framebuffer binding changed in the middle of a command buffer. We should have already
// printed a warning during onFBOChanged.
return;
}
if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
auto glRT = static_cast<GrGLRenderTarget*>(rt);
SkSTArray<2, GrGLenum> discardAttachments;
if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
discardAttachments.push_back(
(0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
}
if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
discardAttachments.push_back(
(0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
}
if (!discardAttachments.empty()) {
if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
discardAttachments.begin()));
} else {
SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
discardAttachments.begin()));
}
}
}
if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
// Handled by use_tiled_rendering().
SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
GL_CALL(EndTiling(preserveMask));
}
SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
}
void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
GrRenderTarget* target, GrSurfaceOrigin origin) {
SkASSERT(target);
SkASSERT(!this->caps()->performStencilClearsAsDraws());
SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
this->handleDirtyContext();
GrAttachment* sb = target->getStencilAttachment();
if (!sb) {
// We should only get here if we marked a proxy as requiring a SB. However,
// the SB creation could later fail. Likely clipping is going to go awry now.
return;
}
GrGLint stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat());
#if 0
SkASSERT(stencilBitCount > 0);
GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
#else
// we could just clear the clip bit but when we go through
// ANGLE a partial stencil mask will cause clears to be
// turned into draws. Our contract on GrOpsTask says that
// changing the clip between stencil passes may or may not
// zero the client's clip bits. So we just clear the whole thing.
static const GrGLint clipStencilMask = ~0;
#endif
GrGLint value;
if (insideStencilMask) {
value = (1 << (stencilBitCount - 1));
} else {
value = 0;
}
GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
this->flushRenderTargetNoColorWrites(glRT);
this->flushScissor(scissor, glRT->height(), origin);
this->disableWindowRectangles();
GL_CALL(StencilMask((uint32_t) clipStencilMask));
GL_CALL(ClearStencil(value));
GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
fHWStencilSettings.invalidate();
}
bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
GrColorType surfaceColorType, GrColorType dstColorType,
void* offsetOrPtr, int rowWidthInPixels) {
SkASSERT(surface);
auto format = surface->backendFormat().asGLFormat();
GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
return false;
}
GrGLenum externalFormat = 0;
GrGLenum externalType = 0;
this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
surfaceColorType,
dstColorType,
&externalFormat,
&externalType);
if (!externalFormat || !externalType) {
return false;
}
if (renderTarget) {
if (renderTarget->numSamples() > 1) {
// Bind the texture FBO since we can't read pixels from an MSAA framebuffer.
if (renderTarget->singleSampleFBOID() == GrGLRenderTarget::kUnresolvableFBOID) {
return false;
}
// we don't track the state of the READ FBO ID.
this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->singleSampleFBOID());
} else {
this->flushRenderTargetNoColorWrites(renderTarget);
}
} else {
// Use a temporary FBO.
this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
fHWBoundRenderTargetUniqueID.makeInvalid();
}
// the read rect is viewport-relative
GrNativeRect readRect = {left, top, width, height};
// determine if GL can read using the passed rowBytes or if we need a scratch buffer.
if (rowWidthInPixels != width) {
SkASSERT(this->glCaps().readPixelsRowBytesSupport());
GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
}
GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bool reattachStencil = false;
if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
renderTarget &&
renderTarget->getStencilAttachment() &&
renderTarget->numSamples() > 1) {
// Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
reattachStencil = true;
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
GR_GL_RENDERBUFFER, 0));
}
GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
externalFormat, externalType, offsetOrPtr));
if (reattachStencil) {
auto* stencilAttachment =
static_cast<GrGLAttachment*>(renderTarget->getStencilAttachment());
GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
}
if (rowWidthInPixels != width) {
SkASSERT(this->glCaps().readPixelsRowBytesSupport());
GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
}
if (!renderTarget) {
this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
}
return true;
}
bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
size_t rowBytes) {
SkASSERT(surface);
size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
// GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
int rowPixelWidth;
if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
rowPixelWidth = width;
} else {
SkASSERT(!(rowBytes % bytesPerPixel));
rowPixelWidth = rowBytes / bytesPerPixel;
}
this->unbindXferBuffer(GrGpuBufferType::kXferGpuToCpu);
return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
dstColorType, buffer, rowPixelWidth);
}
GrOpsRenderPass* GrGLGpu::onGetOpsRenderPass(
GrRenderTarget* rt,
GrAttachment*,
GrSurfaceOrigin origin,
const SkIRect& bounds,
const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
GrXferBarrierFlags renderPassXferBarriers) {
if (!fCachedOpsRenderPass) {
fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
}
fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
return fCachedOpsRenderPass.get();
}
void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
const SkIRect& bounds) {
this->flushRenderTargetNoColorWrites(target);
this->didWriteToSurface(target, origin, &bounds);
}
void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
this->flushRenderTargetNoColorWrites(target);
this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
}
void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
SkASSERT(target);
GrGpuResource::UniqueID rtID = target->uniqueID();
if (fHWBoundRenderTargetUniqueID != rtID) {
this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
#ifdef SK_DEBUG
// don't do this check in Chromium -- this is causing
// lots of repeated command buffer flushes when the compositor is
// rendering with Ganesh, which is really slow; even too slow for
// Debug mode.
if (!this->glCaps().skipErrorChecks()) {
GrGLenum status;
GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
SkDebugf("GrGLGpu::flushRenderTargetNoColorWrites glCheckFramebufferStatus %x\n",
status);
}
}
#endif
fHWBoundRenderTargetUniqueID = rtID;
this->flushViewport(SkIRect::MakeSize(target->dimensions()),
target->height(),
kTopLeft_GrSurfaceOrigin); // the origin is irrelevant in this case
}
if (this->caps()->workarounds().force_update_scissor_state_when_binding_fbo0) {
// The driver forgets the correct scissor state when using FBO 0.
if (!fHWScissorSettings.fRect.isInvalid()) {
const GrNativeRect& r = fHWScissorSettings.fRect;
GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
}
if (fHWScissorSettings.fEnabled == kYes_TriState) {
GL_CALL(Disable(GR_GL_SCISSOR_TEST));
GL_CALL(Enable(GR_GL_SCISSOR_TEST));
} else if (fHWScissorSettings.fEnabled == kNo_TriState) {
GL_CALL(Enable(GR_GL_SCISSOR_TEST));
GL_CALL(Disable(GR_GL_SCISSOR_TEST));
}
}
if (this->glCaps().srgbWriteControl()) {
this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
}
if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
GrGLint format;
GrGLint type;
GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
}
}
void GrGLGpu::flushFramebufferSRGB(bool enable) {
if (enable && kYes_TriState != fHWSRGBFramebuffer) {
GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
fHWSRGBFramebuffer = kYes_TriState;
} else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
fHWSRGBFramebuffer = kNo_TriState;
}
}
GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
fStats.incNumDraws();
if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
GL_CALL(Enable(GR_GL_CULL_FACE));
GL_CALL(Disable(GR_GL_CULL_FACE));
}
fLastPrimitiveType = primitiveType;
switch (primitiveType) {
case GrPrimitiveType::kTriangles:
return GR_GL_TRIANGLES;
case GrPrimitiveType::kTriangleStrip:
return GR_GL_TRIANGLE_STRIP;
case GrPrimitiveType::kPoints:
return GR_GL_POINTS;
case GrPrimitiveType::kLines:
return GR_GL_LINES;
case GrPrimitiveType::kLineStrip:
return GR_GL_LINE_STRIP;
case GrPrimitiveType::kPatches:
return GR_GL_PATCHES;
case GrPrimitiveType::kPath:
SK_ABORT("non-mesh-based GrPrimitiveType");
return 0;
}
SK_ABORT("invalid GrPrimitiveType");
}
void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
// Some extensions automatically resolves the texture when it is read.
SkASSERT(this->glCaps().usesMSAARenderBuffers());
GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
SkASSERT(rt->requiresManualMSAAResolve());
SkASSERT(rt->singleSampleFBOID() != 0 && rt->renderFBOID() != 0);
this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->singleSampleFBOID());
// make sure we go through flushRenderTarget() since we've modified
// the bound DRAW FBO ID.
fHWBoundRenderTargetUniqueID.makeInvalid();
if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
// Apple's extension uses the scissor as the blit bounds.
// Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
// happens inside flushScissor since resolveRect is already in native device coordinates.
GrScissorState scissor(rt->dimensions());
SkAssertResult(scissor.set(resolveRect));
this->flushScissor(scissor, rt->height(), kTopLeft_GrSurfaceOrigin);
this->disableWindowRectangles();
GL_CALL(ResolveMultisampleFramebuffer());
} else {
int l, b, r, t;
if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
this->glCaps().blitFramebufferSupportFlags()) {
l = 0;
b = 0;
r = target->width();
t = target->height();
} else {
l = resolveRect.x();
b = resolveRect.y();
r = resolveRect.x() + resolveRect.width();
t = resolveRect.y() + resolveRect.height();
}
// BlitFrameBuffer respects the scissor, so disable it.
this->flushScissorTest(GrScissorTest::kDisabled);
this->disableWindowRectangles();
GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
}
}
namespace {
GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
static const GrGLenum gTable[kGrStencilOpCount] = {
GR_GL_KEEP, // kKeep
GR_GL_ZERO, // kZero
GR_GL_REPLACE, // kReplace
GR_GL_INVERT, // kInvert
GR_GL_INCR_WRAP, // kIncWrap
GR_GL_DECR_WRAP, // kDecWrap
GR_GL_INCR, // kIncClamp
GR_GL_DECR, // kDecClamp
};
static_assert(0 == (int)GrStencilOp::kKeep);
static_assert(1 == (int)GrStencilOp::kZero);
static_assert(2 == (int)GrStencilOp::kReplace);
static_assert(3 == (int)GrStencilOp::kInvert);
static_assert(4 == (int)GrStencilOp::kIncWrap);
static_assert(5 == (int)GrStencilOp::kDecWrap);
static_assert(6 == (int)GrStencilOp::kIncClamp);
static_assert(7 == (int)GrStencilOp::kDecClamp);
SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
return gTable[(int)op];
}
void set_gl_stencil(const GrGLInterface* gl,
const GrStencilSettings::Face& face,
GrGLenum glFace) {
GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
GrGLint ref = face.fRef;
GrGLint mask = face.fTestMask;
GrGLint writeMask = face.fWriteMask;
if (GR_GL_FRONT_AND_BACK == glFace) {
// we call the combined func just in case separate stencil is not
// supported.
GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
GR_GL_CALL(gl, StencilMask(writeMask));
GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
} else {
GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
}
}
} // namespace
void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
if (stencilSettings.isDisabled()) {
this->disableStencil();
} else if (fHWStencilSettings != stencilSettings ||
(stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
if (kYes_TriState != fHWStencilTestEnabled) {
GL_CALL(Enable(GR_GL_STENCIL_TEST));
fHWStencilTestEnabled = kYes_TriState;
}
if (!stencilSettings.isTwoSided()) {
set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
GR_GL_FRONT_AND_BACK);
} else {
set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
GR_GL_FRONT);
set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
GR_GL_BACK);
}
fHWStencilSettings = stencilSettings;
fHWStencilOrigin = origin;
}
}
void GrGLGpu::disableStencil() {
if (kNo_TriState != fHWStencilTestEnabled) {
GL_CALL(Disable(GR_GL_STENCIL_TEST));
fHWStencilTestEnabled = kNo_TriState;
fHWStencilSettings.invalidate();
}
}
void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
// rt is only optional if useHWAA is false.
SkASSERT(rt || !useHWAA);
#ifdef SK_DEBUG
if (useHWAA && rt->numSamples() <= 1) {
SkASSERT(this->caps()->mixedSamplesSupport());
SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
SkASSERT(rt->getStencilAttachment());
}
#endif
if (this->caps()->multisampleDisableSupport()) {
if (useHWAA) {
if (kYes_TriState != fMSAAEnabled) {
GL_CALL(Enable(GR_GL_MULTISAMPLE));
fMSAAEnabled = kYes_TriState;
}
} else {
if (kNo_TriState != fMSAAEnabled) {
GL_CALL(Disable(GR_GL_MULTISAMPLE));
fMSAAEnabled = kNo_TriState;
}
}
}
}
void GrGLGpu::flushConservativeRasterState(bool enabled) {
if (this->caps()->conservative