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-30 19:18:16 -07:00
|
|
|
#define REQUIRED_TEXTURE_UNITS_COUNT 3
|
|
|
|
|
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
|
|
|
|
2010-07-29 14:37:42 -07:00
|
|
|
/**
|
|
|
|
* Structure mapping Skia xfermodes to OpenGL blending factors.
|
|
|
|
*/
|
|
|
|
struct Blender {
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
GLenum src;
|
|
|
|
GLenum dst;
|
|
|
|
}; // struct Blender
|
|
|
|
|
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-29 14:37:42 -07:00
|
|
|
static const GLenum gTextureUnits[] = {
|
2010-07-30 19:18:16 -07:00
|
|
|
GL_TEXTURE0,
|
|
|
|
GL_TEXTURE1,
|
|
|
|
GL_TEXTURE2
|
2010-07-14 19:18:51 -07:00
|
|
|
};
|
|
|
|
|
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-29 14:37:42 -07:00
|
|
|
mCurrentProgram = NULL;
|
2010-07-30 19:18:16 -07:00
|
|
|
mShader = NULL;
|
2010-08-02 18:50:22 -07:00
|
|
|
mColorFilter = NULL;
|
2010-06-28 17:12:22 -07:00
|
|
|
|
2010-07-27 17:39:27 -07:00
|
|
|
memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
|
|
|
|
|
2010-07-29 18:48:04 -07:00
|
|
|
mFirstSnapshot = new Snapshot;
|
|
|
|
|
|
|
|
GLint maxTextureUnits;
|
|
|
|
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
|
|
|
|
if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
|
2010-07-29 14:37:42 -07:00
|
|
|
LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
|
|
|
|
}
|
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-29 18:48:04 -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-07-30 11:36:12 -07:00
|
|
|
mSnapshot = new Snapshot(mFirstSnapshot);
|
2010-06-25 13:46:18 -07:00
|
|
|
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-07-30 11:36:12 -07:00
|
|
|
mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
|
2010-06-22 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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-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();
|
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
GLuint textureUnit = 0;
|
2010-08-02 18:50:22 -07:00
|
|
|
// Needs to be set prior to calling FontRenderer::getTexture()
|
|
|
|
glActiveTexture(gTextureUnits[textureUnit]);
|
2010-07-30 19:18:16 -07:00
|
|
|
|
2010-07-29 14:37:42 -07:00
|
|
|
ProgramDescription description;
|
|
|
|
description.hasTexture = true;
|
|
|
|
description.hasAlpha8Texture = true;
|
2010-07-30 19:18:16 -07:00
|
|
|
if (mShader) {
|
|
|
|
mShader->describe(description, mExtensions);
|
|
|
|
}
|
2010-08-02 18:50:22 -07:00
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->describe(description, mExtensions);
|
|
|
|
}
|
2010-07-29 14:37:42 -07:00
|
|
|
|
|
|
|
useProgram(mProgramCache.get(description));
|
|
|
|
mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2010-08-02 18:50:22 -07:00
|
|
|
// Text is always blended, no need to check the shader
|
2010-07-21 21:33:20 -07:00
|
|
|
chooseBlending(true, mode);
|
2010-07-30 19:18:16 -07:00
|
|
|
bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
|
|
|
|
glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
|
2010-07-29 14:37:42 -07:00
|
|
|
|
|
|
|
int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
|
|
|
|
glEnableVertexAttribArray(texCoordsSlot);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
|
|
|
// Always premultiplied
|
2010-07-29 14:37:42 -07:00
|
|
|
glUniform4f(mCurrentProgram->color, r, g, b, a);
|
2010-07-21 21:33:20 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
textureUnit++;
|
|
|
|
// Setup attributes and uniforms required by the shaders
|
|
|
|
if (mShader) {
|
|
|
|
mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
|
|
|
|
}
|
2010-08-02 18:50:22 -07:00
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->setupProgram(mCurrentProgram);
|
|
|
|
}
|
2010-07-30 19:18:16 -07:00
|
|
|
|
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-29 14:37:42 -07:00
|
|
|
glDisableVertexAttribArray(texCoordsSlot);
|
2010-07-21 21:33:20 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 19:18:51 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Shaders
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void OpenGLRenderer::resetShader() {
|
2010-07-30 19:18:16 -07:00
|
|
|
mShader = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::setupShader(SkiaShader* shader) {
|
|
|
|
mShader = shader;
|
|
|
|
if (mShader) {
|
|
|
|
mShader->set(&mTextureCache, &mGradientCache);
|
|
|
|
}
|
2010-07-16 17:10:13 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 18:50:22 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Color filters
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void OpenGLRenderer::resetColorFilter() {
|
|
|
|
mColorFilter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
|
|
|
|
mColorFilter = filter;
|
|
|
|
}
|
|
|
|
|
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
|
2010-07-30 19:18:16 -07:00
|
|
|
if (mShader) {
|
2010-07-14 19:18:51 -07:00
|
|
|
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;
|
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-30 19:18:16 -07:00
|
|
|
GLuint textureUnit = 0;
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Setup the blending mode
|
|
|
|
chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Describe the required shaders
|
2010-07-29 14:37:42 -07:00
|
|
|
ProgramDescription description;
|
2010-07-30 19:18:16 -07:00
|
|
|
if (mShader) {
|
|
|
|
mShader->describe(description, mExtensions);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
2010-08-02 18:50:22 -07:00
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->describe(description, mExtensions);
|
|
|
|
}
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Build and use the appropriate shader
|
2010-07-29 14:37:42 -07:00
|
|
|
useProgram(mProgramCache.get(description));
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Setup attributes
|
2010-07-29 14:37:42 -07:00
|
|
|
glVertexAttribPointer(mCurrentProgram->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-30 19:18:16 -07:00
|
|
|
// Setup uniforms
|
2010-07-29 14:37:42 -07:00
|
|
|
mModelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mModelView.scale(right - left, bottom - top, 1.0f);
|
2010-07-30 19:18:16 -07:00
|
|
|
if (!ignoreTransform) {
|
|
|
|
mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
|
2010-07-29 14:37:42 -07:00
|
|
|
} else {
|
2010-07-30 19:18:16 -07:00
|
|
|
mat4 identity;
|
|
|
|
mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
|
2010-07-29 14:37:42 -07:00
|
|
|
}
|
2010-07-30 19:18:16 -07:00
|
|
|
glUniform4f(mCurrentProgram->color, r, g, b, a);
|
2010-07-14 19:18:51 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Setup attributes and uniforms required by the shaders
|
|
|
|
if (mShader) {
|
|
|
|
mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
|
2010-07-14 19:18:51 -07:00
|
|
|
}
|
2010-08-02 18:50:22 -07:00
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->setupProgram(mCurrentProgram);
|
|
|
|
}
|
2010-07-14 19:18:51 -07:00
|
|
|
|
2010-07-30 19:18:16 -07:00
|
|
|
// Draw the mesh
|
2010-07-29 14:37:42 -07:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
|
2010-07-14 19:18:51 -07:00
|
|
|
}
|
|
|
|
|
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-07-29 14:37:42 -07:00
|
|
|
ProgramDescription description;
|
|
|
|
description.hasTexture = true;
|
2010-08-02 18:50:22 -07:00
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->describe(description, mExtensions);
|
|
|
|
}
|
2010-07-29 14:37:42 -07:00
|
|
|
|
2010-06-26 00:13:53 -07:00
|
|
|
mModelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mModelView.scale(right - left, bottom - top, 1.0f);
|
|
|
|
|
2010-07-29 14:37:42 -07:00
|
|
|
useProgram(mProgramCache.get(description));
|
|
|
|
mCurrentProgram->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-29 14:37:42 -07:00
|
|
|
|
|
|
|
// Texture
|
2010-07-30 19:18:16 -07:00
|
|
|
bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
|
2010-07-29 14:37:42 -07:00
|
|
|
glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
|
2010-06-30 17:56:19 -07:00
|
|
|
|
2010-07-13 11:37:54 -07:00
|
|
|
// Always premultiplied
|
2010-07-29 14:37:42 -07:00
|
|
|
glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-07-29 14:37:42 -07:00
|
|
|
// Mesh
|
|
|
|
int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
|
|
|
|
glEnableVertexAttribArray(texCoordsSlot);
|
|
|
|
glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
|
2010-07-27 17:39:27 -07:00
|
|
|
gMeshStride, vertices);
|
2010-07-29 14:37:42 -07:00
|
|
|
glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2010-08-02 18:50:22 -07:00
|
|
|
// Color filter
|
|
|
|
if (mColorFilter) {
|
|
|
|
mColorFilter->setupProgram(mCurrentProgram);
|
|
|
|
}
|
|
|
|
|
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-29 14:37:42 -07:00
|
|
|
glDisableVertexAttribArray(texCoordsSlot);
|
2010-07-09 13:25:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
|
|
|
|
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-29 14:37:42 -07:00
|
|
|
bool OpenGLRenderer::useProgram(Program* program) {
|
2010-07-14 19:18:51 -07:00
|
|
|
if (!program->isInUse()) {
|
2010-07-29 14:37:42 -07:00
|
|
|
if (mCurrentProgram != NULL) mCurrentProgram->remove();
|
2010-07-14 19:18:51 -07:00
|
|
|
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-29 14:37:42 -07:00
|
|
|
void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
|
|
|
|
glActiveTexture(gTextureUnits[textureUnit]);
|
2010-07-29 18:48:04 -07:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
2010-07-20 13:09:13 -07:00
|
|
|
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
|