Asynchronous texture upload. (flutter/engine#2837)

This commit is contained in:
Chinmay Garde
2016-08-01 16:05:45 -07:00
committed by GitHub
parent 9586997a02
commit 1645b8b802
3 changed files with 164 additions and 5 deletions

View File

@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/trace_event.h"
#include "flow/texture_image.h"
#include "flow/open_gl.h"
#include "third_party/skia/include/gpu/gl/GrGLTypes.h"
namespace flow {
inline GLint ToGLFormat(TextureImageFormat format) {
static inline GLint ToGLFormat(TextureImageFormat format) {
switch (format) {
case TextureImageFormat::RGBA:
return GL_RGBA;
@@ -21,7 +23,7 @@ inline GLint ToGLFormat(TextureImageFormat format) {
return GL_NONE;
}
inline GLint ToGLDataFormat(TextureImageDataFormat dataFormat) {
static inline GLint ToGLDataFormat(TextureImageDataFormat dataFormat) {
switch (dataFormat) {
case TextureImageDataFormat::UnsignedByte:
return GL_UNSIGNED_BYTE;
@@ -31,11 +33,125 @@ inline GLint ToGLDataFormat(TextureImageDataFormat dataFormat) {
return GL_NONE;
}
static inline GrPixelConfig ToGrPixelConfig(TextureImageDataFormat dataFormat) {
switch (dataFormat) {
case TextureImageDataFormat::UnsignedByte:
return kRGBA_8888_GrPixelConfig;
case TextureImageDataFormat::UnsignedShort565:
return kRGB_565_GrPixelConfig;
}
return kUnknown_GrPixelConfig;
}
static inline SkColorType ToSkColorType(TextureImageFormat format) {
switch (format) {
case TextureImageFormat::RGBA:
return SkColorType::kRGBA_8888_SkColorType;
case TextureImageFormat::RGB:
case TextureImageFormat::Grey:
case TextureImageFormat::GreyAlpha:
// Add more specializations for greyscale images.
return SkColorType::kRGB_565_SkColorType;
}
return kRGB_565_SkColorType;
}
sk_sp<SkImage> TextureImageCreate(GrContext* context, const SkBitmap& bitmap) {
if (context == nullptr) {
return nullptr;
}
if (bitmap.drawsNothing()) {
return nullptr;
}
TextureImageDataFormat dataFormat = TextureImageDataFormat::UnsignedByte;
TextureImageFormat imageFormat = TextureImageFormat::RGBA;
switch (bitmap.colorType()) {
case kRGB_565_SkColorType:
dataFormat = TextureImageDataFormat::UnsignedShort565;
imageFormat = TextureImageFormat::RGB;
break;
case kRGBA_8888_SkColorType:
dataFormat = TextureImageDataFormat::UnsignedByte;
imageFormat = TextureImageFormat::RGBA;
break;
default:
// Add more as supported.
return nullptr;
}
return TextureImageCreate(
context, // context
imageFormat, // image format
SkISize::Make(bitmap.width(), bitmap.height()), // size
dataFormat, // data format
reinterpret_cast<const uint8_t*>(bitmap.getPixels()) // data
);
}
sk_sp<SkImage> TextureImageCreate(GrContext* context,
std::unique_ptr<SkImageGenerator> generator) {
if (context == nullptr || generator == nullptr) {
return nullptr;
}
const SkImageInfo& info = generator->getInfo();
if (info.isEmpty()) {
return nullptr;
}
TextureImageFormat imageFormat = TextureImageFormat::RGBA;
bool preferOpaque = SkAlphaTypeIsOpaque(info.alphaType());
if (preferOpaque) {
imageFormat = TextureImageFormat::RGB;
}
auto preferredImageInfo =
SkImageInfo::Make(info.bounds().width(), // width
info.bounds().height(), // height
ToSkColorType(imageFormat), // color type
preferOpaque ? SkAlphaType::kOpaque_SkAlphaType
: SkAlphaType::kPremul_SkAlphaType);
SkBitmap bitmap;
{
TRACE_EVENT1("flutter", "DecodePrimaryPreferrence", "Type",
preferOpaque ? "RGB565" : "RGBA8888");
// Try our preferred config.
if (generator->tryGenerateBitmap(&bitmap, preferredImageInfo, nullptr)) {
// Our got our preferred bitmap.
return TextureImageCreate(context, bitmap);
}
}
{
TRACE_EVENT0("flutter", "DecodeRecommended");
// Try the guessed config.
if (generator->tryGenerateBitmap(&bitmap)) {
return TextureImageCreate(context, bitmap);
}
}
return nullptr;
}
sk_sp<SkImage> TextureImageCreate(GrContext* context,
TextureImageFormat format,
const SkISize& size,
TextureImageDataFormat dataFormat,
const uint8_t* data) {
TRACE_EVENT2("flutter", __func__, "width", size.width(), "height",
size.height());
if (context == nullptr) {
return nullptr;
}
GLuint handle = GL_NONE;
// Generate the texture handle.
@@ -70,12 +186,20 @@ sk_sp<SkImage> TextureImageCreate(GrContext* context,
// Clear the binding. We are done.
glBindTexture(GL_TEXTURE_2D, GL_NONE);
GrGLTextureInfo texInfo;
texInfo.fTarget = GL_TEXTURE_2D;
texInfo.fID = handle;
// Create an SkImage handle from the texture.
GrBackendTextureDesc desc;
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
desc.fFlags = kNone_GrBackendTextureFlag;
desc.fWidth = size.fWidth;
desc.fHeight = size.fHeight;
desc.fTextureHandle = handle;
desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&texInfo);
desc.fConfig = ToGrPixelConfig(dataFormat);
if (auto image = SkImage::MakeFromAdoptedTexture(context, desc)) {
// Texture handle was successfully adopted by the SkImage.

View File

@@ -6,6 +6,7 @@
#define FLOW_TEXTURE_IMAGE_H_
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageGenerator.h"
namespace flow {
@@ -21,6 +22,11 @@ enum class TextureImageDataFormat {
UnsignedShort565,
};
sk_sp<SkImage> TextureImageCreate(GrContext* context, const SkBitmap& bitmap);
sk_sp<SkImage> TextureImageCreate(GrContext* context,
std::unique_ptr<SkImageGenerator> generator);
sk_sp<SkImage> TextureImageCreate(GrContext* context,
TextureImageFormat format,
const SkISize& size,

View File

@@ -128,6 +128,12 @@ config("skia_config") {
if (is_ios) {
defines += [ "SK_BUILD_FOR_IOS" ]
}
defines += [
"SK_HAS_JPEG_LIBRARY",
"SK_HAS_PNG_LIBRARY",
]
}
# Internal-facing config for Skia library code.
@@ -137,14 +143,16 @@ config("skia_library_config") {
# matches the GYP build.
include_dirs = [
"//third_party/skia/include/private",
"//third_party/skia/include/codec",
"//third_party/skia/src/codec",
"//third_party/skia/src/core",
"//third_party/skia/src/image",
"//third_party/skia/src/lazy",
"//third_party/skia/src/opts",
"//third_party/skia/src/pdf",
"//third_party/skia/src/ports",
"//third_party/skia/src/sfnt",
"//third_party/skia/src/utils",
"//third_party/skia/src/lazy",
]
if (is_mac || is_ios) {
include_dirs += [ "//third_party/skia/include/utils/mac" ]
@@ -271,7 +279,26 @@ component("skia") {
sources += gypi_skia_utils.sources
sources += [
"//third_party/skia/src/ports/SkImageGenerator_none.cpp",
"//third_party/skia/src/ports/SkImageGenerator_skia.cpp",
"//third_party/skia/src/codec/SkAndroidCodec.cpp",
"//third_party/skia/src/codec/SkBmpCodec.cpp",
"//third_party/skia/src/codec/SkBmpMaskCodec.cpp",
"//third_party/skia/src/codec/SkBmpRLECodec.cpp",
"//third_party/skia/src/codec/SkBmpStandardCodec.cpp",
"//third_party/skia/src/codec/SkCodec.cpp",
"//third_party/skia/src/codec/SkCodecImageGenerator.cpp",
"//third_party/skia/src/codec/SkIcoCodec.cpp",
"//third_party/skia/src/codec/SkJpegCodec.cpp",
"//third_party/skia/src/codec/SkJpegDecoderMgr.cpp",
"//third_party/skia/src/codec/SkJpegUtility.cpp",
"//third_party/skia/src/codec/SkMaskSwizzler.cpp",
"//third_party/skia/src/codec/SkMasks.cpp",
"//third_party/skia/src/codec/SkPngCodec.cpp",
"//third_party/skia/src/codec/SkSampledCodec.cpp",
"//third_party/skia/src/codec/SkSampler.cpp",
"//third_party/skia/src/codec/SkSwizzler.cpp",
"//third_party/skia/src/codec/SkWbmpCodec.cpp",
"//third_party/skia/include/images/SkMovie.h",
"//third_party/skia/include/ports/SkTypeface_win.h",
@@ -482,6 +509,8 @@ component("skia") {
"//base",
"//base/third_party/dynamic_annotations",
"//third_party/zlib",
"//third_party/libpng",
"//third_party/libjpeg_turbo:libjpeg",
]
if (is_linux) {