diff --git a/flow/BUILD.gn b/flow/BUILD.gn index 00370e6500eff25bba1609436c2e4ef9bcecb6d8..560d960f4998a879343e0239e1d72751577b06ac 100644 --- a/flow/BUILD.gn +++ b/flow/BUILD.gn @@ -38,8 +38,11 @@ source_set("flow") { "layers/shader_mask_layer.h", "layers/transform_layer.cc", "layers/transform_layer.h", + "open_gl.h", "raster_cache.cc", "raster_cache.h", + "texture_image.cc", + "texture_image.h", ] deps = [ diff --git a/flow/open_gl.h b/flow/open_gl.h new file mode 100644 index 0000000000000000000000000000000000000000..ee0b0831505d866627b74b22f00dd7605b13f701 --- /dev/null +++ b/flow/open_gl.h @@ -0,0 +1,28 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLOW_OPEN_GL_H_ +#define FLOW_OPEN_GL_H_ + +#include "build/build_config.h" + +#if OS_IOS + +#include + +#elif OS_MACOSX + +#include + +#elif OS_ANDROID + +#include + +#else + +#error OpenGL headers not found for this platform. + +#endif + +#endif // FLOW_OPEN_GL_H_ diff --git a/flow/texture_image.cc b/flow/texture_image.cc new file mode 100644 index 0000000000000000000000000000000000000000..3c34135ba9cce2226a03af167ecf05afb0f6b0fe --- /dev/null +++ b/flow/texture_image.cc @@ -0,0 +1,92 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flow/texture_image.h" +#include "flow/open_gl.h" + +namespace flow { + +inline GLint ToGLFormat(TextureImageFormat format) { + switch (format) { + case TextureImageFormat::RGBA: + return GL_RGBA; + case TextureImageFormat::RGB: + return GL_RGB; + case TextureImageFormat::Grey: + return GL_LUMINANCE; + case TextureImageFormat::GreyAlpha: + return GL_LUMINANCE_ALPHA; + } + return GL_NONE; +} + +inline GLint ToGLDataFormat(TextureImageDataFormat dataFormat) { + switch (dataFormat) { + case TextureImageDataFormat::UnsignedByte: + return GL_UNSIGNED_BYTE; + case TextureImageDataFormat::UnsignedShort565: + return GL_UNSIGNED_SHORT_5_6_5; + } + return GL_NONE; +} + +sk_sp TextureImageCreate(GrContext* context, + TextureImageFormat format, + const SkISize& size, + TextureImageDataFormat dataFormat, + const uint8_t* data) { + GLuint handle = GL_NONE; + + // Generate the texture handle. + glGenTextures(1, &handle); + + // Bind the texture. + glBindTexture(GL_TEXTURE_2D, handle); + + // Specify default texture properties. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Update unpack alignment based on format. + glPixelStorei(GL_UNPACK_ALIGNMENT, + dataFormat == TextureImageDataFormat::UnsignedByte ? 4 : 2); + + GLint gl_format = ToGLFormat(format); + + // Upload the texture. + glTexImage2D(GL_TEXTURE_2D, // target + 0, // level + gl_format, // internal format + size.fWidth, // width + size.fHeight, // height + 0, // border + gl_format, // format + ToGLDataFormat(dataFormat), // format + data); + + // Clear the binding. We are done. + glBindTexture(GL_TEXTURE_2D, GL_NONE); + + // Create an SkImage handle from the texture. + GrBackendTextureDesc desc; + + desc.fWidth = size.fWidth; + desc.fHeight = size.fHeight; + desc.fTextureHandle = handle; + + if (auto image = SkImage::MakeFromAdoptedTexture(context, desc)) { + // Texture handle was successfully adopted by the SkImage. + return image; + } + + // We could not create an SkImage from the texture. Since it could not be + // adopted, delete the handle and return null. + glDeleteTextures(1, &handle); + + return nullptr; +} + +} // namespace flow diff --git a/flow/texture_image.h b/flow/texture_image.h new file mode 100644 index 0000000000000000000000000000000000000000..732371c71247dbe52047163876a6e66d8b084696 --- /dev/null +++ b/flow/texture_image.h @@ -0,0 +1,33 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLOW_TEXTURE_IMAGE_H_ +#define FLOW_TEXTURE_IMAGE_H_ + +#include "base/macros.h" +#include "third_party/skia/include/core/SkImage.h" + +namespace flow { + +enum class TextureImageFormat { + Grey, + GreyAlpha, + RGB, + RGBA, +}; + +enum class TextureImageDataFormat { + UnsignedByte, + UnsignedShort565, +}; + +sk_sp TextureImageCreate(GrContext* context, + TextureImageFormat format, + const SkISize& size, + TextureImageDataFormat dataFormat, + const uint8_t* data); + +} // namespace flow + +#endif // FLOW_TEXTURE_IMAGE_H_