--- /dev/null
+
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+// This test only works with the GPU backend.
+
+#include "gm.h"
+
+#if SK_SUPPORT_GPU
+
+#include "GrContext.h"
+#include "GrTest.h"
+#include "SkBitmap.h"
+#include "SkGradientShader.h"
+#include "SkImage.h"
+
+namespace skiagm {
+class RectangleTexture : public GM {
+public:
+ RectangleTexture() {
+ this->setBGColor(0xFFFFFFFF);
+ }
+
+protected:
+ SkString onShortName() override {
+ return SkString("rectangle_texture");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(1035, 240);
+ }
+
+ void fillPixels(int width, int height, void *pixels) {
+ SkBitmap bmp;
+ bmp.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType), width * 4);
+ bmp.setPixels(pixels);
+ SkPaint paint;
+ SkCanvas canvas(bmp);
+ SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} };
+ SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 };
+ paint.setShader(SkGradientShader::CreateLinear(pts, colors0, nullptr, 2,
+ SkShader::kClamp_TileMode))->unref();
+ canvas.drawPaint(paint);
+
+ SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 };
+ paint.setAntiAlias(true);
+ paint.setShader(SkGradientShader::CreateLinear(pts, colors1, nullptr, 2,
+ SkShader::kClamp_TileMode))->unref();
+ canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2,
+ SkIntToScalar(width + height) / 5, paint);
+ }
+
+ SkImage* createRectangleTextureImg(GrContext* context, int width, int height, void* pixels) {
+ if (!context) {
+ return nullptr;
+ }
+ GrGpu* gpu = context->getGpu();
+ if (!gpu) {
+ return nullptr;
+ }
+ const GrGLContext* glCtx = gpu->glContextForTesting();
+ if (!glCtx) {
+ return nullptr;
+ }
+
+ if (!(kGL_GrGLStandard == glCtx->standard() && glCtx->version() >= GR_GL_VER(3, 1)) &&
+ !glCtx->hasExtension("GL_ARB_texture_rectangle")) {
+ return nullptr;
+ }
+
+ // We will always create the GL texture as GL_RGBA, however the pixels uploaded may be
+ // be RGBA or BGRA, depending on how SkPMColor was compiled.
+ GrGLenum format;
+ if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
+ format = GR_GL_BGRA;
+ } else {
+ SkASSERT(kSkia8888_GrPixelConfig == kRGBA_8888_GrPixelConfig);
+ format = GR_GL_RGBA;
+ }
+
+ const GrGLInterface* gl = glCtx->interface();
+// Useful for debugging whether errors result from use of RECTANGLE
+// #define TARGET GR_GL_TEXTURE_2D
+#define TARGET GR_GL_TEXTURE_RECTANGLE
+ GrGLuint id;
+ GR_GL_CALL(gl, GenTextures(1, &id));
+ GR_GL_CALL(gl, BindTexture(TARGET, id));
+ GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MAG_FILTER,
+ GR_GL_NEAREST));
+ GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MIN_FILTER,
+ GR_GL_NEAREST));
+ GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_S,
+ GR_GL_CLAMP_TO_EDGE));
+ GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_T,
+ GR_GL_CLAMP_TO_EDGE));
+ GR_GL_CALL(gl, TexImage2D(TARGET, 0, GR_GL_RGBA, width, height, 0,
+ format, GR_GL_UNSIGNED_BYTE, pixels));
+
+
+ context->resetContext();
+ GrGLTextureInfo info;
+ info.fID = id;
+ info.fTarget = TARGET;
+ GrBackendTextureDesc desc;
+ desc.fConfig = kRGBA_8888_GrPixelConfig;
+ desc.fWidth = width;
+ desc.fHeight = height;
+ desc.fOrigin = kTopLeft_GrSurfaceOrigin;
+ desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&info);
+ if (SkImage* image = SkImage::NewFromAdoptedTexture(context, desc)) {
+ return image;
+ }
+ GR_GL_CALL(gl, DeleteTextures(1, &id));
+ return nullptr;
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
+ GrContext* context;
+ if (!rt || !(context = rt->getContext())) {
+ skiagm::GM::DrawGpuOnlyMessage(canvas);
+ return;
+ }
+
+ static const int kWidth = 50;
+ static const int kHeight = 50;
+ static const SkScalar kPad = 5.f;
+
+ SkPMColor pixels[kWidth * kHeight];
+ this->fillPixels(kWidth, kHeight, pixels);
+ SkAutoTUnref<SkImage> rectImg(this->createRectangleTextureImg(context, kWidth, kHeight,
+ pixels));
+
+ if (!rectImg) {
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ static const char* kMsg = "Could not create rectangle texture image.";
+ canvas->drawText(kMsg, strlen(kMsg), 10, 100, paint);
+ return;
+ }
+
+ static const SkFilterQuality kQualities[] = {
+ kNone_SkFilterQuality,
+ kLow_SkFilterQuality,
+ kMedium_SkFilterQuality,
+ kHigh_SkFilterQuality,
+ };
+
+ static const SkScalar kScales[] = { 1.0f, 1.2f, 0.75f };
+
+ canvas->translate(kPad, kPad);
+ for (auto s : kScales) {
+ canvas->save();
+ canvas->scale(s, s);
+ for (auto q : kQualities) {
+ SkPaint plainPaint;
+ plainPaint.setFilterQuality(q);
+ canvas->drawImage(rectImg, 0, 0, &plainPaint);
+ canvas->translate(kWidth + kPad, 0);
+
+ SkPaint clampPaint;
+ clampPaint.setFilterQuality(q);
+ clampPaint.setShader(rectImg->newShader(SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode))->unref();
+ canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint);
+ canvas->translate(kWidth * 1.5f + kPad, 0);
+
+ SkPaint repeatPaint;
+ repeatPaint.setFilterQuality(q);
+ repeatPaint.setShader(rectImg->newShader(SkShader::kRepeat_TileMode,
+ SkShader::kMirror_TileMode))->unref();
+ canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint);
+ canvas->translate(1.5f * kWidth + kPad, 0);
+ }
+ canvas->restore();
+ canvas->translate(0, kPad + 1.5f * kHeight * s);
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+};
+
+DEF_GM(return new RectangleTexture;)
+}
+
+#endif
// draws an outline rectangle for debugging/visualization purposes.
virtual void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) = 0;
- // Determines whether a copy of a texture must be made in order to be compatible with
- // a given GrTextureParams. If so, the width, height and filter used for the copy are
- // output via the CopyParams.
+ // Determines whether a texture will need to be rescaled in order to be used with the
+ // GrTextureParams. This variation is called when the caller will create a new texture using the
+ // texture provider from a non-texture src (cpu-backed image, ...).
bool makeCopyForTextureParams(int width, int height, const GrTextureParams&,
- GrTextureProducer::CopyParams*) const;
+ GrTextureProducer::CopyParams*) const;
+
+ // Like the above but this variation should be called when the caller is not creating the
+ // original texture but rather was handed the original texture. It adds additional checks
+ // relevant to original textures that were created external to Skia via
+ // GrTextureProvider::wrap methods.
+ bool makeCopyForTextureParams(GrTexture* texture, const GrTextureParams& params,
+ GrTextureProducer::CopyParams* copyParams) const {
+ if (this->makeCopyForTextureParams(texture->width(), texture->height(), params,
+ copyParams)) {
+ return true;
+ }
+ return this->onMakeCopyForTextureParams(texture, params, copyParams);
+ }
// This is only to be used in GL-specific tests.
virtual const GrGLContext* glContextForTesting() const { return nullptr; }
// overridden by backend-specific derived class to perform the draw call.
virtual void onDraw(const DrawArgs&, const GrNonInstancedVertices&) = 0;
+ virtual bool onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams&,
+ GrTextureProducer::CopyParams*) const { return false; }
+
virtual bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight,
size_t rowBytes, GrPixelConfig readConfig, DrawPreference*,
ReadPixelTempDrawInfo*) = 0;
}
}
+GrTexture* GrTextureAdjuster::refCopy(const CopyParams& copyParams) {
+ GrTexture* texture = this->originalTexture();
+ GrContext* context = texture->getContext();
+ const SkIRect* contentArea = this->contentAreaOrNull();
+ GrUniqueKey key;
+ this->makeCopyKey(copyParams, &key);
+ if (key.isValid()) {
+ GrTexture* cachedCopy = context->textureProvider()->findAndRefTextureByUniqueKey(key);
+ if (cachedCopy) {
+ return cachedCopy;
+ }
+ }
+ GrTexture* copy = copy_on_gpu(texture, contentArea, copyParams);
+ if (copy) {
+ if (key.isValid()) {
+ copy->resourcePriv().setUniqueKey(key);
+ this->didCacheCopy(key);
+ }
+ }
+ return copy;
+}
+
GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& params,
SkIPoint* outOffset) {
GrTexture* texture = this->originalTexture();
copyParams.fWidth = contentArea->width();
copyParams.fHeight = contentArea->height();
copyParams.fFilter = GrTextureParams::kBilerp_FilterMode;
- } else if (!context->getGpu()->makeCopyForTextureParams(texture->width(), texture->height(),
- params, ©Params)) {
+ } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, ©Params)) {
if (outOffset) {
if (contentArea) {
outOffset->set(contentArea->fLeft, contentArea->fRight);
}
return SkRef(texture);
}
- GrUniqueKey key;
- this->makeCopyKey(copyParams, &key);
- if (key.isValid()) {
- GrTexture* result = context->textureProvider()->findAndRefTextureByUniqueKey(key);
- if (result) {
- return result;
- }
- }
- GrTexture* result = copy_on_gpu(texture, contentArea, copyParams);
- if (result) {
- if (key.isValid()) {
- result->resourcePriv().setUniqueKey(key);
- this->didCacheCopy(key);
- }
- if (outOffset) {
- outOffset->set(0, 0);
- }
+
+ GrTexture* copy = this->refCopy(copyParams);
+ if (copy && outOffset) {
+ outOffset->set(0, 0);
}
- return result;
+ return copy;
}
enum DomainMode {
return GrBicubicEffect::Create(texture, textureMatrix, domain);
} else {
static const SkShader::TileMode kClampClamp[] =
- { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
+ { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp);
}
}
}
SkRect domain;
- GrTexture* texture = this->originalTexture();
+ GrTextureParams params;
+ if (filterOrNullForBicubic) {
+ params.setFilterMode(*filterOrNullForBicubic);
+ }
+ SkAutoTUnref<GrTexture> texture(this->refTextureSafeForParams(params, nullptr));
+ if (!texture) {
+ return nullptr;
+ }
+ // If we made a copy then we only copied the contentArea, in which case the new texture is all
+ // content.
+ if (texture != this->originalTexture()) {
+ contentArea = nullptr;
+ }
+
DomainMode domainMode =
determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect,
texture->width(), texture->height(),
SkTLazy<SkIRect> fContentArea;
GrTexture* fOriginal;
+ GrTexture* refCopy(const CopyParams ©Params);
+
typedef GrTextureProducer INHERITED;
};
}
}
- if ((kGL_GrGLStandard == standard && version >= GR_GL_VER(3, 2)) ||
+ if ((kGL_GrGLStandard == standard && version >= GR_GL_VER(3, 1)) ||
ctxInfo.hasExtension("GL_ARB_texture_rectangle")) {
- fRectangleTextureSupport = true;
+ // We also require textureSize() support for rectangle 2D samplers which was added in GLSL
+ // 1.40.
+ if (ctxInfo.glslGeneration() >= k140_GrGLSLGeneration) {
+ fRectangleTextureSupport = true;
+ }
}
if (kGL_GrGLStandard == standard) {
}
return attribState;
}
+
+bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
+ GrTextureProducer::CopyParams* copyParams) const {
+ if (textureParams.isTiled() ||
+ GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
+ GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
+ if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
+ GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
+ copyParams->fFilter = GrTextureParams::kNone_FilterMode;
+ copyParams->fWidth = texture->width();
+ copyParams->fHeight = texture->height();
+ return true;
+ }
+ }
+ return false;
+}
void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
+ bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
+ GrTextureProducer::CopyParams*) const override;
+
bool onReadPixels(GrSurface*,
int left, int top,
int width, int height,
GrGLint SkGLContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
GrGLenum externalFormat, GrGLenum externalType,
GrGLvoid* data) {
- if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 2)) &&
+ if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) &&
!fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
return 0;
}
+
+ if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) {
+ return 0;
+ }
+
GrGLuint id;
GR_GL_CALL(fGL, GenTextures(1, &id));
GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
GrSLType samplerType = uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType();
- out->appendf("%s(%s, %s)",
- GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
- uniformHandler->getUniformCStr(sampler.fSamplerUniform),
- coordName);
+ if (samplerType == kSampler2DRect_GrSLType) {
+ if (varyingType == kVec2f_GrSLType) {
+ out->appendf("%s(%s, textureSize(%s) * %s)",
+ GrGLSLTexture2DFunctionName(varyingType, samplerType,
+ glslCaps->generation()),
+ uniformHandler->getUniformCStr(sampler.fSamplerUniform),
+ uniformHandler->getUniformCStr(sampler.fSamplerUniform),
+ coordName);
+ } else {
+ out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))",
+ GrGLSLTexture2DFunctionName(varyingType, samplerType,
+ glslCaps->generation()),
+ uniformHandler->getUniformCStr(sampler.fSamplerUniform),
+ uniformHandler->getUniformCStr(sampler.fSamplerUniform),
+ coordName,
+ coordName);
+ }
+ } else {
+ out->appendf("%s(%s, %s)",
+ GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
+ uniformHandler->getUniformCStr(sampler.fSamplerUniform),
+ coordName);
+ }
// This refers to any swizzling we may need to get from some backend internal format to the
// format used in GrPixelConfig. If this is implemented by the GrGpu object, then swizzle will