2010-06-16 18:44:05 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-06-22 13:11:24 -07:00
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
2010-06-16 18:44:05 -07:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
#include <SkCanvas.h>
|
2010-07-21 21:33:20 -07:00
|
|
|
#include <SkTypeface.h>
|
2010-06-27 22:59:20 -07:00
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
#include <cutils/properties.h>
|
2010-06-27 22:59:20 -07:00
|
|
|
#include <utils/Log.h>
|
2010-06-22 13:11:24 -07:00
|
|
|
|
|
|
|
#include "OpenGLRenderer.h"
|
2010-07-23 00:28:00 -07:00
|
|
|
#include "Properties.h"
|
2010-06-16 18:44:05 -07:00
|
|
|
|
|
|
|
namespace android {
|
2010-06-24 19:30:36 -07:00
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Defines
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
|
|
|
|
#define DEFAULT_LAYER_CACHE_SIZE 10.0f
|
2010-07-08 19:17:03 -07:00
|
|
|
#define DEFAULT_PATCH_CACHE_SIZE 100
|
2010-07-19 18:43:02 -07:00
|
|
|
#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
|
2010-07-06 11:39:32 -07:00
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
// Converts a number of mega-bytes into bytes
|
|
|
|
#define MB(s) s * 1024 * 1024
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
// Generates simple and textured vertices
|
2010-06-26 00:13:53 -07:00
|
|
|
#define FV(x, y, u, v) { { x, y }, { u, v } }
|
2010-06-24 19:30:36 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Globals
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-28 17:12:22 -07:00
|
|
|
// This array is never used directly but used as a memcpy source in the
|
|
|
|
// OpenGLRenderer constructor
|
2010-07-27 17:39:27 -07:00
|
|
|
static const TextureVertex gMeshVertices[] = {
|
2010-06-30 17:56:19 -07:00
|
|
|
FV(0.0f, 0.0f, 0.0f, 0.0f),
|
|
|
|
FV(1.0f, 0.0f, 1.0f, 0.0f),
|
|
|
|
FV(0.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
FV(1.0f, 1.0f, 1.0f, 1.0f)
|
2010-06-26 00:13:53 -07:00
|
|
|
};
|
2010-07-27 17:39:27 -07:00
|
|
|
static const GLsizei gMeshStride = sizeof(TextureVertex);
|
|
|
|
static const GLsizei gMeshCount = 4;
|
2010-06-28 17:12:22 -07:00
|
|
|
|
|
|
|
// In this array, the index of each Blender equals the value of the first
|
|
|
|
// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
|
|
|
|
static const Blender gBlends[] = {
|
|
|
|
{ SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
|
|
|
|
{ SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
|
|
|
|
{ SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
|
|
|
|
{ SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
|
|
|
|
{ SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
|
|
|
|
{ SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
|
|
|
|
{ SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
|
|
|
|
{ SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
|
|
|
|
};
|
2010-06-16 18:44:05 -07:00
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
static const GLint gTileModes[] = {
|
|
|
|
GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
|
|
|
|
GL_REPEAT, // == SkShader::kRepeat_Mode
|
|
|
|
GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
|
|
|
|
};
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
OpenGLRenderer::OpenGLRenderer():
|
2010-07-09 13:25:56 -07:00
|
|
|
mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
|
2010-07-06 11:39:32 -07:00
|
|
|
mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
|
2010-07-08 19:17:03 -07:00
|
|
|
mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
|
2010-07-19 18:43:02 -07:00
|
|
|
mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
|
2010-07-08 19:17:03 -07:00
|
|
|
mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
|
2010-06-22 13:11:24 -07:00
|
|
|
LOGD("Create OpenGLRenderer");
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
|
2010-07-06 11:39:32 -07:00
|
|
|
LOGD(" Setting texture cache size to %sMB", property);
|
2010-07-19 18:43:02 -07:00
|
|
|
mTextureCache.setMaxSize(MB(atof(property)));
|
2010-07-06 11:39:32 -07:00
|
|
|
} else {
|
2010-07-19 18:43:02 -07:00
|
|
|
LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
|
2010-07-06 11:39:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
|
|
|
|
LOGD(" Setting layer cache size to %sMB", property);
|
2010-07-19 18:43:02 -07:00
|
|
|
mLayerCache.setMaxSize(MB(atof(property)));
|
2010-07-06 11:39:32 -07:00
|
|
|
} else {
|
2010-07-19 18:43:02 -07:00
|
|
|
LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
|
|
|
|
LOGD(" Setting gradient cache size to %sMB", property);
|
|
|
|
mLayerCache.setMaxSize(MB(atof(property)));
|
|
|
|
} else {
|
|
|
|
LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
|
2010-07-01 18:26:52 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
mDrawColorProgram = new DrawColorProgram;
|
|
|
|
mDrawTextureProgram = new DrawTextureProgram;
|
2010-07-21 21:33:20 -07:00
|
|
|
mDrawTextProgram = new DrawTextProgram;
|
2010-07-16 23:13:33 -07:00
|
|
|
mDrawLinearGradientProgram = new DrawLinearGradientProgram;
|
2010-07-14 19:18:51 -07:00
|
|
|
mCurrentProgram = mDrawTextureProgram;
|
|
|
|
|
|
|
|
mShader = kShaderNone;
|
2010-07-20 13:09:13 -07:00
|
|
|
mShaderTileX = GL_CLAMP_TO_EDGE;
|
|
|
|
mShaderTileY = GL_CLAMP_TO_EDGE;
|
2010-07-14 19:18:51 -07:00
|
|
|
mShaderMatrix = NULL;
|
|
|
|
mShaderBitmap = NULL;
|
2010-06-28 17:12:22 -07:00
|
|
|
|
2010-07-16 15:07:42 -07:00
|
|
|
mLastTexture = 0;
|
|
|
|
|
2010-07-27 17:39:27 -07:00
|
|
|
memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
|
|
|
|
|
|
|
|
ProgramDescription d;
|
|
|
|
mProgramCache.get(d);
|
|
|
|
d.hasTexture = true;
|
|
|
|
mProgramCache.get(d);
|
|
|
|
d.hasAlpha8Texture = true;
|
|
|
|
d.hasGradient = true;
|
|
|
|
d.hasBitmap = true;
|
|
|
|
d.shadersMode = SkXfermode::kDstOut_Mode;
|
|
|
|
d.colorOp = ProgramDescription::kColorMatrix;
|
|
|
|
mProgramCache.get(d);
|
2010-06-16 18:44:05 -07:00
|
|
|
}
|
|
|
|
|
2010-06-22 13:11:24 -07:00
|
|
|
OpenGLRenderer::~OpenGLRenderer() {
|
|
|
|
LOGD("Destroy OpenGLRenderer");
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
mTextureCache.clear();
|
2010-07-06 11:39:32 -07:00
|
|
|
mLayerCache.clear();
|
2010-07-19 18:43:02 -07:00
|
|
|
mGradientCache.clear();
|
2010-07-12 20:20:03 -07:00
|
|
|
mPatchCache.clear();
|
2010-06-16 18:44:05 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Setup
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-22 13:11:24 -07:00
|
|
|
void OpenGLRenderer::setViewport(int width, int height) {
|
2010-06-21 19:35:50 -07:00
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
|
2010-07-12 14:41:06 -07:00
|
|
|
mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
|
2010-06-22 18:56:38 -07:00
|
|
|
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
2010-07-01 11:05:42 -07:00
|
|
|
mFirstSnapshot.height = height;
|
2010-06-16 18:44:05 -07:00
|
|
|
}
|
|
|
|
|
2010-06-22 13:11:24 -07:00
|
|
|
void OpenGLRenderer::prepare() {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot = &mFirstSnapshot;
|
|
|
|
mSaveCount = 0;
|
2010-06-23 17:47:49 -07:00
|
|
|
|
2010-06-21 19:35:50 -07:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-21 19:35:50 -07:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-21 19:35:50 -07:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2010-06-25 13:41:57 -07:00
|
|
|
glScissor(0, 0, mWidth, mHeight);
|
2010-06-23 17:47:49 -07:00
|
|
|
|
2010-06-22 18:56:38 -07:00
|
|
|
mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
|
|
|
|
}
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// State management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-22 18:56:38 -07:00
|
|
|
int OpenGLRenderer::getSaveCount() const {
|
2010-06-25 13:46:18 -07:00
|
|
|
return mSaveCount;
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenGLRenderer::save(int flags) {
|
2010-06-25 13:46:18 -07:00
|
|
|
return saveSnapshot();
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::restore() {
|
2010-06-25 13:46:18 -07:00
|
|
|
if (mSaveCount == 0) return;
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
if (restoreSnapshot()) {
|
|
|
|
setScissorFromClip();
|
|
|
|
}
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::restoreToCount(int saveCount) {
|
2010-06-25 13:46:18 -07:00
|
|
|
if (saveCount <= 0 || saveCount > mSaveCount) return;
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
bool restoreClip = false;
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
while (mSaveCount != saveCount - 1) {
|
|
|
|
restoreClip |= restoreSnapshot();
|
|
|
|
}
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
if (restoreClip) {
|
|
|
|
setScissorFromClip();
|
|
|
|
}
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenGLRenderer::saveSnapshot() {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot = new Snapshot(mSnapshot);
|
|
|
|
return ++mSaveCount;
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenGLRenderer::restoreSnapshot() {
|
2010-06-25 13:46:18 -07:00
|
|
|
bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
|
2010-06-26 00:13:53 -07:00
|
|
|
bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
|
2010-07-01 11:05:42 -07:00
|
|
|
bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
sp<Snapshot> current = mSnapshot;
|
2010-06-25 13:46:18 -07:00
|
|
|
sp<Snapshot> previous = mSnapshot->previous;
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
if (restoreOrtho) {
|
2010-07-12 14:41:06 -07:00
|
|
|
mOrthoMatrix.load(current->orthoMatrix);
|
2010-07-01 11:05:42 -07:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
if (restoreLayer) {
|
2010-06-28 17:42:46 -07:00
|
|
|
composeLayer(current, previous);
|
|
|
|
}
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
mSnapshot = previous;
|
|
|
|
mSaveCount--;
|
2010-06-27 22:59:20 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
return restoreClip;
|
|
|
|
}
|
2010-06-27 22:59:20 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
|
2010-07-06 11:39:32 -07:00
|
|
|
if (!current->layer) {
|
|
|
|
LOGE("Attempting to compose a layer that does not exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
// Unbind current FBO and restore previous one
|
|
|
|
// Most of the time, previous->fbo will be 0 to bind the default buffer
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
|
2010-06-27 22:59:20 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
// Restore the clip from the previous snapshot
|
2010-07-16 14:12:24 -07:00
|
|
|
const Rect& clip = previous->clipRect;
|
2010-06-28 17:42:46 -07:00
|
|
|
glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
Layer* layer = current->layer;
|
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
// Compute the correct texture coordinates for the FBO texture
|
|
|
|
// The texture is currently as big as the window but drawn with
|
|
|
|
// a quad of the appropriate size
|
2010-07-06 11:39:32 -07:00
|
|
|
const Rect& rect = layer->layer;
|
|
|
|
|
|
|
|
drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
|
2010-07-13 11:37:54 -07:00
|
|
|
layer->texture, layer->alpha, layer->mode, layer->blend);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
LayerSize size(rect.getWidth(), rect.getHeight());
|
2010-07-08 11:45:51 -07:00
|
|
|
// Failing to add the layer to the cache should happen only if the
|
|
|
|
// layer is too large
|
2010-07-06 11:39:32 -07:00
|
|
|
if (!mLayerCache.put(size, layer)) {
|
|
|
|
LAYER_LOGD("Deleting layer");
|
2010-06-22 18:56:38 -07:00
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
glDeleteFramebuffers(1, &layer->fbo);
|
|
|
|
glDeleteTextures(1, &layer->texture);
|
|
|
|
|
|
|
|
delete layer;
|
|
|
|
}
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Layers
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
|
|
|
|
const SkPaint* p, int flags) {
|
2010-06-28 17:42:46 -07:00
|
|
|
int count = saveSnapshot();
|
|
|
|
|
|
|
|
int alpha = 255;
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
alpha = p->getAlpha();
|
|
|
|
const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
|
|
|
|
if (!isMode) {
|
|
|
|
// Assume SRC_OVER
|
|
|
|
mode = SkXfermode::kSrcOver_Mode;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mode = SkXfermode::kSrcOver_Mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
|
|
|
|
|
|
|
|
return count;
|
2010-06-26 00:13:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
|
|
|
|
int alpha, int flags) {
|
|
|
|
int count = saveSnapshot();
|
2010-06-28 17:42:46 -07:00
|
|
|
createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
|
|
|
|
return count;
|
|
|
|
}
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
|
|
|
|
float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
|
2010-07-06 11:39:32 -07:00
|
|
|
LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
|
|
|
|
|
2010-07-08 11:45:51 -07:00
|
|
|
GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
|
|
|
|
LayerSize size(right - left, bottom - top);
|
2010-07-06 11:39:32 -07:00
|
|
|
|
2010-07-08 11:45:51 -07:00
|
|
|
Layer* layer = mLayerCache.get(size, previousFbo);
|
|
|
|
if (!layer) {
|
|
|
|
return false;
|
2010-06-26 00:13:53 -07:00
|
|
|
}
|
|
|
|
|
2010-07-08 11:45:51 -07:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
|
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
// Clear the FBO
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
// Save the layer in the snapshot
|
2010-06-28 17:42:46 -07:00
|
|
|
snapshot->flags |= Snapshot::kFlagIsLayer;
|
2010-07-06 11:39:32 -07:00
|
|
|
layer->mode = mode;
|
|
|
|
layer->alpha = alpha / 255.0f;
|
|
|
|
layer->layer.set(left, top, right, bottom);
|
|
|
|
|
|
|
|
snapshot->layer = layer;
|
|
|
|
snapshot->fbo = layer->fbo;
|
2010-06-28 17:42:46 -07:00
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
// Creates a new snapshot to draw into the FBO
|
|
|
|
saveSnapshot();
|
|
|
|
// TODO: This doesn't preserve other transformations (check Skia first)
|
|
|
|
mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
|
2010-07-16 14:12:24 -07:00
|
|
|
mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
|
2010-07-01 11:05:42 -07:00
|
|
|
mSnapshot->height = bottom - top;
|
|
|
|
setScissorFromClip();
|
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
|
2010-07-12 14:41:06 -07:00
|
|
|
mSnapshot->orthoMatrix.load(mOrthoMatrix);
|
2010-07-01 11:05:42 -07:00
|
|
|
|
|
|
|
// Change the ortho projection
|
2010-07-12 14:41:06 -07:00
|
|
|
mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
|
2010-07-01 11:05:42 -07:00
|
|
|
|
2010-06-28 17:42:46 -07:00
|
|
|
return true;
|
2010-06-26 00:13:53 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Transforms
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void OpenGLRenderer::translate(float dx, float dy) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot->transform.translate(dx, dy, 0.0f);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::rotate(float degrees) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::scale(float sx, float sy) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot->transform.scale(sx, sy, 1.0f);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot->transform.load(*matrix);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mSnapshot->transform.copyTo(*matrix);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
|
2010-06-25 13:46:18 -07:00
|
|
|
mat4 m(*matrix);
|
|
|
|
mSnapshot->transform.multiply(m);
|
2010-06-23 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clipping
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-22 18:56:38 -07:00
|
|
|
void OpenGLRenderer::setScissorFromClip() {
|
2010-07-16 14:12:24 -07:00
|
|
|
const Rect& clip = mSnapshot->clipRect;
|
2010-07-01 11:05:42 -07:00
|
|
|
glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
|
2010-06-24 19:30:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const Rect& OpenGLRenderer::getClipBounds() {
|
2010-07-16 14:12:24 -07:00
|
|
|
return mSnapshot->getLocalClip();
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
2010-06-25 13:41:57 -07:00
|
|
|
bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
|
|
|
|
Rect r(left, top, right, bottom);
|
2010-07-16 14:12:24 -07:00
|
|
|
mSnapshot->transform.mapRect(r);
|
2010-06-25 13:41:57 -07:00
|
|
|
return !mSnapshot->clipRect.intersects(r);
|
|
|
|
}
|
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
|
|
|
|
bool clipped = mSnapshot->clip(left, top, right, bottom, op);
|
2010-06-25 13:46:18 -07:00
|
|
|
if (clipped) {
|
|
|
|
setScissorFromClip();
|
|
|
|
}
|
2010-07-16 14:12:24 -07:00
|
|
|
return !mSnapshot->clipRect.isEmpty();
|
2010-06-16 18:44:05 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Drawing
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-30 17:56:19 -07:00
|
|
|
void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
|
2010-07-12 20:20:03 -07:00
|
|
|
const float right = left + bitmap->width();
|
|
|
|
const float bottom = top + bitmap->height();
|
|
|
|
|
|
|
|
if (quickReject(left, top, right, bottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
const Texture* texture = mTextureCache.get(bitmap);
|
2010-07-12 20:20:03 -07:00
|
|
|
drawTextureRect(left, top, right, bottom, texture, paint);
|
2010-06-30 19:21:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
|
|
|
|
Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
|
|
|
|
const mat4 transform(*matrix);
|
|
|
|
transform.mapRect(r);
|
|
|
|
|
2010-07-12 20:20:03 -07:00
|
|
|
if (quickReject(r.left, r.top, r.right, r.bottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
const Texture* texture = mTextureCache.get(bitmap);
|
2010-07-09 13:25:56 -07:00
|
|
|
drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
|
2010-07-01 11:05:42 -07:00
|
|
|
}
|
|
|
|
|
2010-06-30 19:21:21 -07:00
|
|
|
void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
|
|
|
|
float srcLeft, float srcTop, float srcRight, float srcBottom,
|
|
|
|
float dstLeft, float dstTop, float dstRight, float dstBottom,
|
2010-07-01 11:05:42 -07:00
|
|
|
const SkPaint* paint) {
|
2010-07-12 20:20:03 -07:00
|
|
|
if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:05:42 -07:00
|
|
|
const Texture* texture = mTextureCache.get(bitmap);
|
2010-06-30 19:21:21 -07:00
|
|
|
|
|
|
|
const float width = texture->width;
|
|
|
|
const float height = texture->height;
|
2010-06-30 17:56:19 -07:00
|
|
|
|
2010-06-30 19:21:21 -07:00
|
|
|
const float u1 = srcLeft / width;
|
|
|
|
const float v1 = srcTop / height;
|
|
|
|
const float u2 = srcRight / width;
|
|
|
|
const float v2 = srcBottom / height;
|
2010-06-30 17:56:19 -07:00
|
|
|
|
2010-06-30 19:21:21 -07:00
|
|
|
resetDrawTextureTexCoords(u1, v1, u2, v2);
|
|
|
|
|
2010-07-09 13:25:56 -07:00
|
|
|
drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
|
2010-06-30 19:21:21 -07:00
|
|
|
|
|
|
|
resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-07 17:54:48 -07:00
|
|
|
void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
|
|
|
|
float left, float top, float right, float bottom, const SkPaint* paint) {
|
2010-07-12 20:20:03 -07:00
|
|
|
if (quickReject(left, top, right, bottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:17:03 -07:00
|
|
|
const Texture* texture = mTextureCache.get(bitmap);
|
|
|
|
|
|
|
|
int alpha;
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
getAlphaAndMode(paint, &alpha, &mode);
|
|
|
|
|
|
|
|
Patch* mesh = mPatchCache.get(patch);
|
2010-07-09 13:52:56 -07:00
|
|
|
mesh->updateVertices(bitmap, left, top, right, bottom,
|
|
|
|
&patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
|
2010-07-08 19:17:03 -07:00
|
|
|
|
|
|
|
// Specify right and bottom as +1.0f from left/top to prevent scaling since the
|
|
|
|
// patch mesh already defines the final size
|
2010-07-13 11:37:54 -07:00
|
|
|
drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
|
|
|
|
mode, texture->blend, &mesh->vertices[0].position[0],
|
2010-07-09 16:13:28 -07:00
|
|
|
&mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
|
2010-07-08 19:17:03 -07:00
|
|
|
}
|
|
|
|
|
2010-06-22 13:11:24 -07:00
|
|
|
void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
|
2010-07-16 14:12:24 -07:00
|
|
|
const Rect& clip = mSnapshot->clipRect;
|
2010-07-14 16:34:53 -07:00
|
|
|
drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
|
2010-06-25 13:41:57 -07:00
|
|
|
}
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
|
2010-07-12 20:20:03 -07:00
|
|
|
if (quickReject(left, top, right, bottom)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-28 17:12:22 -07:00
|
|
|
SkXfermode::Mode mode;
|
|
|
|
|
|
|
|
const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
|
|
|
|
if (!isMode) {
|
|
|
|
// Assume SRC_OVER
|
|
|
|
mode = SkXfermode::kSrcOver_Mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skia draws using the color's alpha channel if < 255
|
|
|
|
// Otherwise, it uses the paint's alpha
|
|
|
|
int color = p->getColor();
|
2010-07-14 19:18:51 -07:00
|
|
|
if (((color >> 24) & 0xff) == 255) {
|
2010-06-28 17:12:22 -07:00
|
|
|
color |= p->getAlpha() << 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
drawColorRect(left, top, right, bottom, color, mode);
|
2010-06-25 13:41:57 -07:00
|
|
|
}
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-23 18:55:21 -07:00
|
|
|
void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
|
|
|
|
float x, float y, SkPaint* paint) {
|
|
|
|
if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float length;
|
|
|
|
switch (paint->getTextAlign()) {
|
|
|
|
case SkPaint::kCenter_Align:
|
|
|
|
length = paint->measureText(text, bytesCount);
|
|
|
|
x -= length / 2.0f;
|
|
|
|
break;
|
|
|
|
case SkPaint::kRight_Align:
|
|
|
|
length = paint->measureText(text, bytesCount);
|
|
|
|
x -= length;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
int alpha;
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
getAlphaAndMode(paint, &alpha, &mode);
|
|
|
|
|
|
|
|
uint32_t color = paint->getColor();
|
|
|
|
const GLfloat a = alpha / 255.0f;
|
|
|
|
const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
|
|
|
|
const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
|
|
|
|
const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
|
|
|
|
|
|
|
|
mModelView.loadIdentity();
|
|
|
|
|
|
|
|
useProgram(mDrawTextProgram);
|
|
|
|
mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
|
|
|
|
|
|
|
chooseBlending(true, mode);
|
|
|
|
bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
// Always premultiplied
|
|
|
|
glUniform4f(mDrawTextProgram->color, r, g, b, a);
|
|
|
|
|
2010-07-22 13:08:20 -07:00
|
|
|
// TODO: Implement scale properly
|
|
|
|
const Rect& clip = mSnapshot->getLocalClip();
|
|
|
|
|
2010-07-26 11:09:33 -07:00
|
|
|
mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
|
2010-07-23 18:55:21 -07:00
|
|
|
mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Shaders
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void OpenGLRenderer::resetShader() {
|
|
|
|
mShader = OpenGLRenderer::kShaderNone;
|
2010-07-16 23:13:33 -07:00
|
|
|
mShaderKey = NULL;
|
2010-07-14 19:18:51 -07:00
|
|
|
mShaderBlend = false;
|
2010-07-20 13:09:13 -07:00
|
|
|
mShaderTileX = GL_CLAMP_TO_EDGE;
|
|
|
|
mShaderTileY = GL_CLAMP_TO_EDGE;
|
2010-07-14 19:18:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
|
|
|
|
SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
|
2010-07-16 17:10:13 -07:00
|
|
|
mShader = OpenGLRenderer::kShaderBitmap;
|
2010-07-14 19:18:51 -07:00
|
|
|
mShaderBlend = hasAlpha;
|
|
|
|
mShaderBitmap = bitmap;
|
2010-07-20 13:09:13 -07:00
|
|
|
mShaderTileX = gTileModes[tileX];
|
|
|
|
mShaderTileY = gTileModes[tileY];
|
2010-07-14 19:18:51 -07:00
|
|
|
mShaderMatrix = matrix;
|
|
|
|
}
|
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
|
|
|
|
float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix,
|
|
|
|
bool hasAlpha) {
|
2010-07-16 23:13:33 -07:00
|
|
|
// TODO: We should use a struct to describe each shader
|
2010-07-16 17:10:13 -07:00
|
|
|
mShader = OpenGLRenderer::kShaderLinearGradient;
|
2010-07-16 23:13:33 -07:00
|
|
|
mShaderKey = shader;
|
2010-07-16 17:10:13 -07:00
|
|
|
mShaderBlend = hasAlpha;
|
2010-07-20 13:09:13 -07:00
|
|
|
mShaderTileX = gTileModes[tileMode];
|
|
|
|
mShaderTileY = gTileModes[tileMode];
|
2010-07-16 17:10:13 -07:00
|
|
|
mShaderMatrix = matrix;
|
|
|
|
mShaderBounds = bounds;
|
|
|
|
mShaderColors = colors;
|
|
|
|
mShaderPositions = positions;
|
2010-07-19 18:43:02 -07:00
|
|
|
mShaderCount = count;
|
2010-07-16 17:10:13 -07:00
|
|
|
}
|
|
|
|
|
2010-07-12 20:20:03 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Drawing implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-28 17:12:22 -07:00
|
|
|
void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
|
2010-07-14 16:34:53 -07:00
|
|
|
int color, SkXfermode::Mode mode, bool ignoreTransform) {
|
2010-07-14 19:18:51 -07:00
|
|
|
// If a shader is set, preserve only the alpha
|
|
|
|
if (mShader != kShaderNone) {
|
|
|
|
color |= 0x00ffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render using pre-multiplied alpha
|
2010-06-28 17:12:22 -07:00
|
|
|
const int alpha = (color >> 24) & 0xFF;
|
2010-07-14 19:18:51 -07:00
|
|
|
const GLfloat a = alpha / 255.0f;
|
|
|
|
|
|
|
|
switch (mShader) {
|
2010-07-16 17:10:13 -07:00
|
|
|
case OpenGLRenderer::kShaderBitmap:
|
2010-07-14 19:18:51 -07:00
|
|
|
drawBitmapShader(left, top, right, bottom, a, mode);
|
|
|
|
return;
|
2010-07-16 17:10:13 -07:00
|
|
|
case OpenGLRenderer::kShaderLinearGradient:
|
2010-07-19 18:43:02 -07:00
|
|
|
drawLinearGradientShader(left, top, right, bottom, a, mode);
|
|
|
|
return;
|
2010-07-14 19:18:51 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-06-28 17:12:22 -07:00
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
|
|
|
|
const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
|
|
|
|
const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
|
|
|
|
|
2010-07-12 20:20:03 -07:00
|
|
|
// Pre-multiplication happens when setting the shader color
|
2010-07-14 19:18:51 -07:00
|
|
|
chooseBlending(alpha < 255 || mShaderBlend, mode);
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-06-25 13:41:57 -07:00
|
|
|
mModelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mModelView.scale(right - left, bottom - top, 1.0f);
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
if (!useProgram(mDrawColorProgram)) {
|
2010-07-27 17:39:27 -07:00
|
|
|
const GLvoid* vertices = &mMeshVertices[0].position[0];
|
|
|
|
const GLvoid* texCoords = &mMeshVertices[0].texture[0];
|
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
|
2010-07-27 17:39:27 -07:00
|
|
|
gMeshStride, vertices);
|
|
|
|
glVertexAttribPointer(mDrawColorProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
gMeshStride, texCoords);
|
2010-07-14 19:18:51 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 16:34:53 -07:00
|
|
|
if (!ignoreTransform) {
|
2010-07-16 14:12:24 -07:00
|
|
|
mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
2010-07-14 16:34:53 -07:00
|
|
|
} else {
|
|
|
|
mat4 identity;
|
2010-07-16 14:12:24 -07:00
|
|
|
mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
|
2010-07-14 16:34:53 -07:00
|
|
|
}
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
glUniform4f(mDrawColorProgram->color, r, g, b, a);
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-27 17:39:27 -07:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
|
2010-07-09 13:25:56 -07:00
|
|
|
}
|
2010-06-28 17:12:22 -07:00
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
|
|
|
|
float alpha, SkXfermode::Mode mode) {
|
|
|
|
Texture* texture = mGradientCache.get(mShaderKey);
|
|
|
|
if (!texture) {
|
2010-07-20 13:09:13 -07:00
|
|
|
SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
|
|
|
|
switch (mShaderTileX) {
|
|
|
|
case GL_REPEAT:
|
|
|
|
tileMode = SkShader::kRepeat_TileMode;
|
|
|
|
break;
|
|
|
|
case GL_MIRRORED_REPEAT:
|
|
|
|
tileMode = SkShader::kMirror_TileMode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
|
2010-07-20 13:09:13 -07:00
|
|
|
mShaderPositions, mShaderCount, tileMode);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mModelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mModelView.scale(right - left, bottom - top, 1.0f);
|
|
|
|
|
|
|
|
useProgram(mDrawLinearGradientProgram);
|
|
|
|
mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
|
|
|
|
|
|
|
chooseBlending(mShaderBlend || alpha < 1.0f, mode);
|
2010-07-20 13:09:13 -07:00
|
|
|
bindTexture(texture->id, mShaderTileX, mShaderTileY);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
|
|
|
Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
|
|
|
|
if (mShaderMatrix) {
|
|
|
|
mat4 shaderMatrix(*mShaderMatrix);
|
|
|
|
shaderMatrix.mapRect(start);
|
|
|
|
}
|
|
|
|
mSnapshot->transform.mapRect(start);
|
|
|
|
|
|
|
|
const float gradientX = start.right - start.left;
|
|
|
|
const float gradientY = start.bottom - start.top;
|
|
|
|
|
|
|
|
mat4 screenSpace(mSnapshot->transform);
|
|
|
|
screenSpace.multiply(mModelView);
|
|
|
|
|
|
|
|
// Always premultiplied
|
|
|
|
glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha);
|
|
|
|
glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top);
|
|
|
|
glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY);
|
|
|
|
glUniform1f(mDrawLinearGradientProgram->gradientLength,
|
|
|
|
1.0f / (gradientX * gradientX + gradientY * gradientY));
|
|
|
|
glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE,
|
|
|
|
&screenSpace.data[0]);
|
|
|
|
|
|
|
|
glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE,
|
2010-07-27 17:39:27 -07:00
|
|
|
gMeshStride, &mMeshVertices[0].position[0]);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2010-07-27 17:39:27 -07:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
|
|
|
|
float alpha, SkXfermode::Mode mode) {
|
|
|
|
const Texture* texture = mTextureCache.get(mShaderBitmap);
|
|
|
|
|
|
|
|
const float width = texture->width;
|
|
|
|
const float height = texture->height;
|
|
|
|
|
|
|
|
// This could be done in the vertex shader but we have only 4 vertices
|
|
|
|
float u1 = 0.0f;
|
|
|
|
float v1 = 0.0f;
|
|
|
|
float u2 = right - left;
|
|
|
|
float v2 = bottom - top;
|
|
|
|
|
2010-07-21 21:33:20 -07:00
|
|
|
// TODO: If the texture is not pow, use a shader to support repeat/mirror
|
2010-07-14 19:18:51 -07:00
|
|
|
if (mShaderMatrix) {
|
|
|
|
SkMatrix inverse;
|
|
|
|
mShaderMatrix->invert(&inverse);
|
|
|
|
mat4 m(inverse);
|
|
|
|
Rect r(u1, v1, u2, v2);
|
|
|
|
m.mapRect(r);
|
|
|
|
|
|
|
|
u1 = r.left;
|
|
|
|
u2 = r.right;
|
|
|
|
v1 = r.top;
|
|
|
|
v2 = r.bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
u1 /= width;
|
|
|
|
u2 /= width;
|
|
|
|
v1 /= height;
|
|
|
|
v2 /= height;
|
|
|
|
|
|
|
|
resetDrawTextureTexCoords(u1, v1, u2, v2);
|
|
|
|
|
|
|
|
drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
|
2010-07-27 17:39:27 -07:00
|
|
|
&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
|
2010-07-14 19:18:51 -07:00
|
|
|
|
|
|
|
resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2010-07-09 13:25:56 -07:00
|
|
|
void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
|
2010-07-13 11:37:54 -07:00
|
|
|
const Texture* texture, const SkPaint* paint) {
|
2010-07-09 13:25:56 -07:00
|
|
|
int alpha;
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
getAlphaAndMode(paint, &alpha, &mode);
|
|
|
|
|
2010-07-13 11:37:54 -07:00
|
|
|
drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
|
2010-07-27 17:39:27 -07:00
|
|
|
&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
|
2010-06-22 13:11:24 -07:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
|
2010-07-13 11:37:54 -07:00
|
|
|
GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
|
|
|
|
drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
|
2010-07-27 17:39:27 -07:00
|
|
|
&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
|
2010-07-08 19:17:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
|
2010-07-13 11:37:54 -07:00
|
|
|
GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
|
2010-07-08 19:17:03 -07:00
|
|
|
GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
|
2010-06-26 00:13:53 -07:00
|
|
|
mModelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mModelView.scale(right - left, bottom - top, 1.0f);
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
useProgram(mDrawTextureProgram);
|
|
|
|
mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-13 11:37:54 -07:00
|
|
|
chooseBlending(blend || alpha < 1.0f, mode);
|
2010-07-20 13:09:13 -07:00
|
|
|
bindTexture(texture, mShaderTileX, mShaderTileY);
|
2010-06-30 17:56:19 -07:00
|
|
|
|
2010-07-13 11:37:54 -07:00
|
|
|
// Always premultiplied
|
2010-07-14 19:18:51 -07:00
|
|
|
glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
|
2010-07-27 17:39:27 -07:00
|
|
|
gMeshStride, vertices);
|
2010-07-14 19:18:51 -07:00
|
|
|
glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
|
2010-07-27 17:39:27 -07:00
|
|
|
gMeshStride, texCoords);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-08 19:17:03 -07:00
|
|
|
if (!indices) {
|
2010-07-27 17:39:27 -07:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
|
2010-07-08 19:17:03 -07:00
|
|
|
} else {
|
|
|
|
glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
|
|
|
|
}
|
2010-07-09 13:25:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
|
|
|
|
// In theory we should not blend if the mode is Src, but it's rare enough
|
|
|
|
// that it's not worth it
|
|
|
|
blend = blend || mode != SkXfermode::kSrcOver_Mode;
|
|
|
|
if (blend) {
|
|
|
|
if (!mBlend) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum sourceMode = gBlends[mode].src;
|
|
|
|
GLenum destMode = gBlends[mode].dst;
|
|
|
|
if (!isPremultiplied && sourceMode == GL_ONE) {
|
|
|
|
sourceMode = GL_SRC_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
|
|
|
|
glBlendFunc(sourceMode, destMode);
|
|
|
|
mLastSrcMode = sourceMode;
|
|
|
|
mLastDstMode = destMode;
|
|
|
|
}
|
|
|
|
} else if (mBlend) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
mBlend = blend;
|
2010-06-26 00:13:53 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
bool OpenGLRenderer::useProgram(const sp<Program>& program) {
|
|
|
|
if (!program->isInUse()) {
|
|
|
|
mCurrentProgram->remove();
|
|
|
|
program->use();
|
|
|
|
mCurrentProgram = program;
|
2010-07-12 20:20:03 -07:00
|
|
|
return false;
|
2010-07-12 14:41:06 -07:00
|
|
|
}
|
2010-07-12 20:20:03 -07:00
|
|
|
return true;
|
2010-07-12 14:41:06 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 17:12:22 -07:00
|
|
|
void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
|
2010-07-27 17:39:27 -07:00
|
|
|
TextureVertex* v = &mMeshVertices[0];
|
2010-07-09 13:25:56 -07:00
|
|
|
TextureVertex::setUV(v++, u1, v1);
|
|
|
|
TextureVertex::setUV(v++, u2, v1);
|
|
|
|
TextureVertex::setUV(v++, u1, v2);
|
|
|
|
TextureVertex::setUV(v++, u2, v2);
|
2010-06-30 19:21:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
|
|
|
|
if (paint) {
|
|
|
|
const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
|
|
|
|
if (!isMode) {
|
|
|
|
// Assume SRC_OVER
|
|
|
|
*mode = SkXfermode::kSrcOver_Mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skia draws using the color's alpha channel if < 255
|
|
|
|
// Otherwise, it uses the paint's alpha
|
|
|
|
int color = paint->getColor();
|
|
|
|
*alpha = (color >> 24) & 0xFF;
|
|
|
|
if (*alpha == 255) {
|
|
|
|
*alpha = paint->getAlpha();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*mode = SkXfermode::kSrcOver_Mode;
|
|
|
|
*alpha = 255;
|
|
|
|
}
|
2010-06-28 17:12:22 -07:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:09:13 -07:00
|
|
|
void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) {
|
|
|
|
if (texture != mLastTexture) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
mLastTexture = texture;
|
|
|
|
}
|
|
|
|
// TODO: Don't set the texture parameters every time
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
|
|
|
|
}
|
|
|
|
|
2010-06-24 19:30:36 -07:00
|
|
|
}; // namespace uirenderer
|
2010-06-16 18:44:05 -07:00
|
|
|
}; // namespace android
|