1e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy/*
2e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Copyright (C) 2010 The Android Open Source Project
3e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
4e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * you may not use this file except in compliance with the License.
6e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * You may obtain a copy of the License at
7e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
8e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
10e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Unless required by applicable law or agreed to in writing, software
11e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * See the License for the specific language governing permissions and
14e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * limitations under the License.
15e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy */
16e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
17a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy#define LOG_TAG "OpenGLRenderer"
18a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
197d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy#include <EGL/egl.h>
207d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
21e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include "jni.h"
22e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy#include "GraphicsJNI.h"
23e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include <nativehelper/JNIHelp.h>
246554943a1dd6854c0f4976900956e556767b49e1Romain Guy
25e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include <android_runtime/AndroidRuntime.h>
26e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guy#include <android_runtime/android_graphics_SurfaceTexture.h>
276554943a1dd6854c0f4976900956e556767b49e1Romain Guy#include <gui/SurfaceTexture.h>
286554943a1dd6854c0f4976900956e556767b49e1Romain Guy
29b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/ResourceTypes.h>
30e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
316554943a1dd6854c0f4976900956e556767b49e1Romain Guy#include <private/hwui/DrawGlInfo.h>
326554943a1dd6854c0f4976900956e556767b49e1Romain Guy
336554943a1dd6854c0f4976900956e556767b49e1Romain Guy#include <cutils/properties.h>
348f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy
35ce0537b80087a6225273040a987414b1dd081aa0Romain Guy#include <SkBitmap.h>
36c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy#include <SkCanvas.h>
37f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy#include <SkMatrix.h>
38c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy#include <SkPaint.h>
39079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy#include <SkRegion.h>
40a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy#include <SkScalerContext.h>
4161c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy#include <SkTemplates.h>
4285bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy#include <SkXfermode.h>
43e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
44b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy#include <DisplayListRenderer.h>
456c319ca1275c8db892c39b48fc54864c949f9171Romain Guy#include <LayerRenderer.h>
4685bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy#include <OpenGLRenderer.h>
4706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy#include <SkiaShader.h>
48db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy#include <SkiaColorFilter.h>
490baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy#include <Stencil.h>
509d5316e3f56d138504565ff311145ac01621dff4Romain Guy#include <Rect.h>
5185bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
52d1d4bb70704e8f37d0823837eacdae21ebe0ed05Romain Guy#include <TextLayout.h>
53b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio#include <TextLayoutCache.h>
54e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
55e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guynamespace android {
56e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
579d5316e3f56d138504565ff311145ac01621dff4Romain Guyusing namespace uirenderer;
589d5316e3f56d138504565ff311145ac01621dff4Romain Guy
59163935113919a184122b8b3bd672ef08c8df65dcRomain Guy/**
60163935113919a184122b8b3bd672ef08c8df65dcRomain Guy * Note: OpenGLRenderer JNI layer is generated and compiled only on supported
61163935113919a184122b8b3bd672ef08c8df65dcRomain Guy *       devices. This means all the logic must be compiled only when the
62163935113919a184122b8b3bd672ef08c8df65dcRomain Guy *       preprocessor variable USE_OPENGL_RENDERER is defined.
63163935113919a184122b8b3bd672ef08c8df65dcRomain Guy */
64163935113919a184122b8b3bd672ef08c8df65dcRomain Guy#ifdef USE_OPENGL_RENDERER
65163935113919a184122b8b3bd672ef08c8df65dcRomain Guy
66244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy// ----------------------------------------------------------------------------
67bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy// Defines
68244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy// ----------------------------------------------------------------------------
69bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy
70bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy// Debug
71e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy#define DEBUG_RENDERER 0
72bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy
73bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy// Debug
74bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy#if DEBUG_RENDERER
755baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    #define RENDERER_LOGD(...) ALOGD(__VA_ARGS__)
76bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy#else
77bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy    #define RENDERER_LOGD(...)
78bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy#endif
79e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
80a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy#define MODIFIER_SHADOW 1
81a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy#define MODIFIER_SHADER 2
82a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy#define MODIFIER_COLOR_FILTER 4
83a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy
849d5316e3f56d138504565ff311145ac01621dff4Romain Guy// ----------------------------------------------------------------------------
859d5316e3f56d138504565ff311145ac01621dff4Romain Guy
869d5316e3f56d138504565ff311145ac01621dff4Romain Guystatic struct {
877ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    jmethodID set;
889d5316e3f56d138504565ff311145ac01621dff4Romain Guy} gRectClassInfo;
899d5316e3f56d138504565ff311145ac01621dff4Romain Guy
90e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// ----------------------------------------------------------------------------
91244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy// Caching
927d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy// ----------------------------------------------------------------------------
937d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
94bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guystatic void android_view_GLES20Canvas_flushCaches(JNIEnv* env, jobject clazz,
95bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy        Caches::FlushMode mode) {
96bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    if (Caches::hasInstance()) {
97bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy        Caches::getInstance().flush(mode);
98bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    }
99bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy}
100bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy
1018ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guystatic void android_view_GLES20Canvas_initCaches(JNIEnv* env, jobject clazz) {
1028ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    if (Caches::hasInstance()) {
1038ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy        Caches::getInstance().init();
1048ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    }
1058ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy}
1068ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
1078ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guystatic void android_view_GLES20Canvas_terminateCaches(JNIEnv* env, jobject clazz) {
1088ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    if (Caches::hasInstance()) {
1098ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy        Caches::getInstance().terminate();
1108ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    }
1118ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy}
1128ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
1137d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy// ----------------------------------------------------------------------------
114e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// Constructors
115e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// ----------------------------------------------------------------------------
116e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1176c319ca1275c8db892c39b48fc54864c949f9171Romain Guystatic OpenGLRenderer* android_view_GLES20Canvas_createRenderer(JNIEnv* env, jobject clazz) {
118bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy    RENDERER_LOGD("Create OpenGLRenderer");
11987e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy    OpenGLRenderer* renderer = new OpenGLRenderer();
12087e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy    renderer->initProperties();
12187e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy    return renderer;
122e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
123e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1245c13d89c1332fcc499379b9064b891187b75ca32Chet Haasestatic void android_view_GLES20Canvas_destroyRenderer(JNIEnv* env, jobject clazz,
125f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer) {
126bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy    RENDERER_LOGD("Destroy OpenGLRenderer");
127f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    delete renderer;
128e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
129e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
130e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// ----------------------------------------------------------------------------
131e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// Setup
132e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// ----------------------------------------------------------------------------
133e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1347d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_setViewport(JNIEnv* env, jobject clazz,
135f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, jint width, jint height) {
136f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    renderer->setViewport(width, height);
137e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
138e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
13944b2fe3fc114ee5f7273c6b0fee2cc999bf244a2Chet Haasestatic int android_view_GLES20Canvas_prepare(JNIEnv* env, jobject clazz,
1406b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        OpenGLRenderer* renderer, jboolean opaque) {
14144b2fe3fc114ee5f7273c6b0fee2cc999bf244a2Chet Haase    return renderer->prepare(opaque);
142e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
143e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
14444b2fe3fc114ee5f7273c6b0fee2cc999bf244a2Chet Haasestatic int android_view_GLES20Canvas_prepareDirty(JNIEnv* env, jobject clazz,
1457d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy        OpenGLRenderer* renderer, jint left, jint top, jint right, jint bottom,
1467d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy        jboolean opaque) {
14744b2fe3fc114ee5f7273c6b0fee2cc999bf244a2Chet Haase    return renderer->prepareDirty(left, top, right, bottom, opaque);
1487d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy}
1497d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1507d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_finish(JNIEnv* env, jobject clazz,
151b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        OpenGLRenderer* renderer) {
152b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    renderer->finish();
153b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy}
154b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy
155530041d3191ce817832a0108514617768e43cda6Romain Guystatic jint android_view_GLES20Canvas_getStencilSize(JNIEnv* env, jobject clazz) {
1560baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy    return Stencil::getStencilSize();
157530041d3191ce817832a0108514617768e43cda6Romain Guy}
158530041d3191ce817832a0108514617768e43cda6Romain Guy
159530041d3191ce817832a0108514617768e43cda6Romain Guy// ----------------------------------------------------------------------------
160530041d3191ce817832a0108514617768e43cda6Romain Guy// Functor
161530041d3191ce817832a0108514617768e43cda6Romain Guy// ----------------------------------------------------------------------------
162530041d3191ce817832a0108514617768e43cda6Romain Guy
1636554943a1dd6854c0f4976900956e556767b49e1Romain Guystatic jint android_view_GLES20Canvas_callDrawGLFunction(JNIEnv* env, jobject clazz,
164ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy        OpenGLRenderer* renderer, Functor* functor) {
165cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    android::uirenderer::Rect dirty;
166cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    return renderer->callDrawGLFunction(functor, dirty);
167daf98e941e140e8739458126640183b9f296a2abChet Haase}
168daf98e941e140e8739458126640183b9f296a2abChet Haase
169ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guystatic void android_view_GLES20Canvas_detachFunctor(JNIEnv* env,
170ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy        jobject clazz, OpenGLRenderer* renderer, Functor* functor) {
171ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy    renderer->detachFunctor(functor);
172ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy}
173ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy
174ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guystatic void android_view_GLES20Canvas_attachFunctor(JNIEnv* env,
175ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy        jobject clazz, OpenGLRenderer* renderer, Functor* functor) {
176ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy    renderer->attachFunctor(functor);
177ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy}
178ba6be8a62dcdb3ffd210cd36b9af4e3a658eac47Romain Guy
1798f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guystatic jint android_view_GLES20Canvas_invokeFunctors(JNIEnv* env,
1808f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy        jobject clazz, OpenGLRenderer* renderer, jobject dirty) {
1818f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy    android::uirenderer::Rect bounds;
1828f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy    status_t status = renderer->invokeFunctors(bounds);
1838f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy    if (status != DrawGlInfo::kStatusDone && dirty != NULL) {
1848f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy        env->CallVoidMethod(dirty, gRectClassInfo.set,
1858f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy                int(bounds.left), int(bounds.top), int(bounds.right), int(bounds.bottom));
1868f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy    }
1878f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy    return status;
1888f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy}
1898f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy
1908f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy// ----------------------------------------------------------------------------
1918f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy// Misc
1928f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy// ----------------------------------------------------------------------------
1938f3b8e32993d190a26c70c839a63d8ce4c3b16d9Romain Guy
194f61970fc79e9c5cf340fa942597628242361864aRomain Guystatic jint android_view_GLES20Canvas_getMaxTextureWidth(JNIEnv* env, jobject clazz) {
195f61970fc79e9c5cf340fa942597628242361864aRomain Guy    return Caches::getInstance().maxTextureSize;
196f61970fc79e9c5cf340fa942597628242361864aRomain Guy}
197f61970fc79e9c5cf340fa942597628242361864aRomain Guy
198f61970fc79e9c5cf340fa942597628242361864aRomain Guystatic jint android_view_GLES20Canvas_getMaxTextureHeight(JNIEnv* env, jobject clazz) {
199f61970fc79e9c5cf340fa942597628242361864aRomain Guy    return Caches::getInstance().maxTextureSize;
200f61970fc79e9c5cf340fa942597628242361864aRomain Guy}
201f61970fc79e9c5cf340fa942597628242361864aRomain Guy
202e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy// ----------------------------------------------------------------------------
203bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// State
204bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// ----------------------------------------------------------------------------
205bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2067d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic jint android_view_GLES20Canvas_save(JNIEnv* env, jobject clazz, OpenGLRenderer* renderer,
207bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy        jint flags) {
208f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    return renderer->save(flags);
209f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
210bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2117d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic jint android_view_GLES20Canvas_getSaveCount(JNIEnv* env, jobject clazz,
212f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer) {
213f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    return renderer->getSaveCount();
214bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
215bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2167d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_restore(JNIEnv* env, jobject clazz,
217f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer) {
218f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    renderer->restore();
219bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
220bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2217d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_restoreToCount(JNIEnv* env, jobject clazz,
222f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, jint saveCount) {
223f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy    renderer->restoreToCount(saveCount);
224bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
225bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
226f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// ----------------------------------------------------------------------------
227bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// Layers
228bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// ----------------------------------------------------------------------------
229bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
2307d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic jint android_view_GLES20Canvas_saveLayer(JNIEnv* env, jobject clazz,
231bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
232bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        SkPaint* paint, jint saveFlags) {
233bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    return renderer->saveLayer(left, top, right, bottom, paint, saveFlags);
234bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
235bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
236189887e0ae171c1b7601991442104a30e56c50abRomain Guystatic jint android_view_GLES20Canvas_saveLayerClip(JNIEnv* env, jobject clazz,
237189887e0ae171c1b7601991442104a30e56c50abRomain Guy        OpenGLRenderer* renderer, SkPaint* paint, jint saveFlags) {
238189887e0ae171c1b7601991442104a30e56c50abRomain Guy    const android::uirenderer::Rect& bounds(renderer->getClipBounds());
239189887e0ae171c1b7601991442104a30e56c50abRomain Guy    return renderer->saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
240189887e0ae171c1b7601991442104a30e56c50abRomain Guy            paint, saveFlags);
241189887e0ae171c1b7601991442104a30e56c50abRomain Guy}
242189887e0ae171c1b7601991442104a30e56c50abRomain Guy
2437d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic jint android_view_GLES20Canvas_saveLayerAlpha(JNIEnv* env, jobject clazz,
244bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
245bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        jint alpha, jint saveFlags) {
246bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    return renderer->saveLayerAlpha(left, top, right, bottom, alpha, saveFlags);
247bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
248bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
249189887e0ae171c1b7601991442104a30e56c50abRomain Guystatic jint android_view_GLES20Canvas_saveLayerAlphaClip(JNIEnv* env, jobject clazz,
250189887e0ae171c1b7601991442104a30e56c50abRomain Guy        OpenGLRenderer* renderer, jint alpha, jint saveFlags) {
251189887e0ae171c1b7601991442104a30e56c50abRomain Guy    const android::uirenderer::Rect& bounds(renderer->getClipBounds());
252189887e0ae171c1b7601991442104a30e56c50abRomain Guy    return renderer->saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom,
253189887e0ae171c1b7601991442104a30e56c50abRomain Guy            alpha, saveFlags);
254189887e0ae171c1b7601991442104a30e56c50abRomain Guy}
255189887e0ae171c1b7601991442104a30e56c50abRomain Guy
256bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// ----------------------------------------------------------------------------
257f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Clipping
258f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// ----------------------------------------------------------------------------
259f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
2607d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic bool android_view_GLES20Canvas_quickReject(JNIEnv* env, jobject clazz,
261c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
262c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy        SkCanvas::EdgeType edge) {
263c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy    return renderer->quickReject(left, top, right, bottom);
264c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
265c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
2667d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic bool android_view_GLES20Canvas_clipRectF(JNIEnv* env, jobject clazz,
267079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
268079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy        SkRegion::Op op) {
269079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return renderer->clipRect(left, top, right, bottom, op);
270f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
271bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2727d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic bool android_view_GLES20Canvas_clipRect(JNIEnv* env, jobject clazz,
273079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy        OpenGLRenderer* renderer, jint left, jint top, jint right, jint bottom,
274079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy        SkRegion::Op op) {
275079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return renderer->clipRect(float(left), float(top), float(right), float(bottom), op);
276bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
277bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2787d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic bool android_view_GLES20Canvas_getClipBounds(JNIEnv* env, jobject clazz,
2799d5316e3f56d138504565ff311145ac01621dff4Romain Guy        OpenGLRenderer* renderer, jobject rect) {
2807ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    const android::uirenderer::Rect& bounds(renderer->getClipBounds());
2819d5316e3f56d138504565ff311145ac01621dff4Romain Guy
2827ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    env->CallVoidMethod(rect, gRectClassInfo.set,
2837ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy            int(bounds.left), int(bounds.top), int(bounds.right), int(bounds.bottom));
2849d5316e3f56d138504565ff311145ac01621dff4Romain Guy
2857ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    return !bounds.isEmpty();
2869d5316e3f56d138504565ff311145ac01621dff4Romain Guy}
2879d5316e3f56d138504565ff311145ac01621dff4Romain Guy
288bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// ----------------------------------------------------------------------------
289f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Transforms
290bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// ----------------------------------------------------------------------------
291bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2927d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_translate(JNIEnv* env, jobject clazz,
293f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, jfloat dx, jfloat dy) {
2947ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->translate(dx, dy);
295f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
296bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2977d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_rotate(JNIEnv* env, jobject clazz,
298f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, jfloat degrees) {
2997ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->rotate(degrees);
300bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
301bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
3027d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_scale(JNIEnv* env, jobject clazz,
303f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, jfloat sx, jfloat sy) {
3047ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->scale(sx, sy);
305f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
306bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
3077d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_skew(JNIEnv* env, jobject clazz,
308807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy        OpenGLRenderer* renderer, jfloat sx, jfloat sy) {
309807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy    renderer->skew(sx, sy);
310807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy}
311807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy
3127d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_setMatrix(JNIEnv* env, jobject clazz,
313f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, SkMatrix* matrix) {
3147ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->setMatrix(matrix);
315f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
316f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
3177d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_getMatrix(JNIEnv* env, jobject clazz,
318f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, SkMatrix* matrix) {
3197ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->getMatrix(matrix);
320f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
321f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
3227d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_concatMatrix(JNIEnv* env, jobject clazz,
323f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy        OpenGLRenderer* renderer, SkMatrix* matrix) {
3247ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    renderer->concatMatrix(matrix);
325bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
326bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
327bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// ----------------------------------------------------------------------------
328bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy// Drawing
32985bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy// ----------------------------------------------------------------------------
33085bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
3317d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawBitmap(JNIEnv* env, jobject clazz,
332e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
333e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        jfloat left, jfloat top, SkPaint* paint) {
334e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    // This object allows the renderer to allocate a global JNI ref to the buffer object.
335e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
336e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
337dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy    renderer->drawBitmap(bitmap, left, top, paint);
338ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
339ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
3407d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawBitmapRect(JNIEnv* env, jobject clazz,
341e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
342ce0537b80087a6225273040a987414b1dd081aa0Romain Guy        float srcLeft, float srcTop, float srcRight, float srcBottom,
343dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy        float dstLeft, float dstTop, float dstRight, float dstBottom, SkPaint* paint) {
344e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    // This object allows the renderer to allocate a global JNI ref to the buffer object.
345e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
346e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
347dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy    renderer->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom,
348dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy            dstLeft, dstTop, dstRight, dstBottom, paint);
349f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy}
3508ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
3517d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawBitmapMatrix(JNIEnv* env, jobject clazz,
352e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer, SkMatrix* matrix,
353e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        SkPaint* paint) {
354e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    // This object allows the renderer to allocate a global JNI ref to the buffer object.
355e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
356e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
357dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy    renderer->drawBitmap(bitmap, matrix, paint);
358ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
359ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
360e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guystatic void android_view_GLES20Canvas_drawBitmapData(JNIEnv* env, jobject clazz,
361e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        OpenGLRenderer* renderer, jintArray colors, jint offset, jint stride,
362e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        jfloat left, jfloat top, jint width, jint height, jboolean hasAlpha, SkPaint* paint) {
36349c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    SkBitmap* bitmap = new SkBitmap;
36449c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    bitmap->setConfig(hasAlpha ? SkBitmap::kARGB_8888_Config : SkBitmap::kRGB_565_Config,
36549c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy            width, height);
366e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy
36749c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    if (!bitmap->allocPixels()) {
36849c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy        delete bitmap;
369e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        return;
370e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy    }
371e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy
37249c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    if (!GraphicsJNI::SetPixels(env, colors, offset, stride, 0, 0, width, height, *bitmap)) {
37349c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy        delete bitmap;
374e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy        return;
375e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy    }
376e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy
37749c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    renderer->drawBitmapData(bitmap, left, top, paint);
37849c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy
37949c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    // If the renderer is a deferred renderer it will own the bitmap
38049c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    if (!renderer->isDeferred()) {
38149c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy        delete bitmap;
38249c5fc0b9e850497233e189ff9dcc71a78ebe6e7Romain Guy    }
383e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy}
384e651cc6239616a202f6e96ebc2ed93b4b8b3627cRomain Guy
3857d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawBitmapMesh(JNIEnv* env, jobject clazz,
3865a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
3875a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jint meshWidth, jint meshHeight, jfloatArray vertices, jint offset,
3885a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jintArray colors, jint colorOffset, SkPaint* paint) {
3895a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    // This object allows the renderer to allocate a global JNI ref to the buffer object.
3905a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
3915a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
3925a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    jfloat* verticesArray = vertices ? env->GetFloatArrayElements(vertices, NULL) + offset : NULL;
3935a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    jint* colorsArray = colors ? env->GetIntArrayElements(colors, NULL) + colorOffset : NULL;
3945a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
3955a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    renderer->drawBitmapMesh(bitmap, meshWidth, meshHeight, verticesArray, colorsArray, paint);
3965a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
3975a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (vertices) env->ReleaseFloatArrayElements(vertices, verticesArray, 0);
3985a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (colors) env->ReleaseIntArrayElements(colors, colorsArray, 0);
3995a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy}
4005a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
4017d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawPatch(JNIEnv* env, jobject clazz,
402e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer, jbyteArray chunks,
403dbd77cd444f89d94ec5333223c1bc17dbe0c90cdRomain Guy        float left, float top, float right, float bottom, SkPaint* paint) {
404e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    // This object allows the renderer to allocate a global JNI ref to the buffer object.
405e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
406e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
407deba785f122a47915756ffd991f5540d952cf937Romain Guy    jbyte* storage = env->GetByteArrayElements(chunks, NULL);
408deba785f122a47915756ffd991f5540d952cf937Romain Guy    Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(storage);
409deba785f122a47915756ffd991f5540d952cf937Romain Guy    Res_png_9patch::deserialize(patch);
410deba785f122a47915756ffd991f5540d952cf937Romain Guy
411e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    renderer->drawPatch(bitmap, &patch->xDivs[0], &patch->yDivs[0],
412e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            &patch->colors[0], patch->numXDivs, patch->numYDivs, patch->numColors,
4134bb942083a0d4db746adf95349108dd8ef842e32Romain Guy            left, top, right, bottom, paint);
414deba785f122a47915756ffd991f5540d952cf937Romain Guy
415deba785f122a47915756ffd991f5540d952cf937Romain Guy    env->ReleaseByteArrayElements(chunks, storage, 0);
416deba785f122a47915756ffd991f5540d952cf937Romain Guy}
417deba785f122a47915756ffd991f5540d952cf937Romain Guy
4187d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawColor(JNIEnv* env, jobject clazz,
419ce0537b80087a6225273040a987414b1dd081aa0Romain Guy        OpenGLRenderer* renderer, jint color, SkXfermode::Mode mode) {
420ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    renderer->drawColor(color, mode);
42185bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy}
42285bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
4237d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawRect(JNIEnv* env, jobject clazz,
424c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
425c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy        SkPaint* paint) {
426c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy    renderer->drawRect(left, top, right, bottom, paint);
427c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
428c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
4297d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawRoundRect(JNIEnv* env, jobject clazz,
43001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
43101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        jfloat rx, jfloat ry, SkPaint* paint) {
43201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    renderer->drawRoundRect(left, top, right, bottom, rx, ry, paint);
43301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
43401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
4357d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawCircle(JNIEnv* env, jobject clazz,
43601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        OpenGLRenderer* renderer, jfloat x, jfloat y, jfloat radius, SkPaint* paint) {
43701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    renderer->drawCircle(x, y, radius, paint);
43801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
43901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
4407d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawOval(JNIEnv* env, jobject clazz,
441c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
442c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        SkPaint* paint) {
443c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    renderer->drawOval(left, top, right, bottom, paint);
444c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
445c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
4467d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawArc(JNIEnv* env, jobject clazz,
4478b2f5267f16c295f12faab810527cd6311997e34Romain Guy        OpenGLRenderer* renderer, jfloat left, jfloat top, jfloat right, jfloat bottom,
4488b2f5267f16c295f12faab810527cd6311997e34Romain Guy        jfloat startAngle, jfloat sweepAngle, jboolean useCenter, SkPaint* paint) {
4498b2f5267f16c295f12faab810527cd6311997e34Romain Guy    renderer->drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint);
4508b2f5267f16c295f12faab810527cd6311997e34Romain Guy}
4518b2f5267f16c295f12faab810527cd6311997e34Romain Guy
4527d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawRects(JNIEnv* env, jobject clazz,
453a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy        OpenGLRenderer* renderer, SkRegion* region, SkPaint* paint) {
454a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy    SkRegion::Iterator it(*region);
455a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy    while (!it.done()) {
456a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy        const SkIRect& r = it.rect();
457a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy        renderer->drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
458a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy        it.next();
459a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy    }
460a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy}
461a48a1a87ba17f20f7006eaab21dcedf86c015c13Romain Guy
462ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guystatic void android_view_GLES20Canvas_drawPoints(JNIEnv* env, jobject clazz,
463ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy        OpenGLRenderer* renderer, jfloatArray points, jint offset, jint count, SkPaint* paint) {
464ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy    jfloat* storage = env->GetFloatArrayElements(points, NULL);
465ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy    renderer->drawPoints(storage + offset, count, paint);
466ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy    env->ReleaseFloatArrayElements(points, storage, 0);
467ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy}
468ed6fcb034b44d9a6ac2fc72fee6030417811f234Romain Guy
4697d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawPath(JNIEnv* env, jobject clazz,
4707fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy        OpenGLRenderer* renderer, SkPath* path, SkPaint* paint) {
4717fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy    renderer->drawPath(path, paint);
4727fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
4737fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
4747d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawLines(JNIEnv* env, jobject clazz,
475759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy        OpenGLRenderer* renderer, jfloatArray points, jint offset, jint count, SkPaint* paint) {
476759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    jfloat* storage = env->GetFloatArrayElements(points, NULL);
477759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    renderer->drawLines(storage + offset, count, paint);
478759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    env->ReleaseFloatArrayElements(points, storage, 0);
479759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy}
480759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
48185bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy// ----------------------------------------------------------------------------
482db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy// Shaders and color filters
483d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// ----------------------------------------------------------------------------
484d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
4857d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_resetModifiers(JNIEnv* env, jobject clazz,
486a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy        OpenGLRenderer* renderer, jint modifiers) {
487a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy    if (modifiers & MODIFIER_SHADOW) renderer->resetShadow();
488a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy    if (modifiers & MODIFIER_SHADER) renderer->resetShader();
489a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy    if (modifiers & MODIFIER_COLOR_FILTER) renderer->resetColorFilter();
490d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy}
491d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
4927d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_setupShader(JNIEnv* env, jobject clazz,
49306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy        OpenGLRenderer* renderer, SkiaShader* shader) {
49406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    renderer->setupShader(shader);
4957fac2e18339f765320d759e8d4c090f92431959eRomain Guy}
4967fac2e18339f765320d759e8d4c090f92431959eRomain Guy
4977d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_setupColorFilter(JNIEnv* env, jobject clazz,
498db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy        OpenGLRenderer* renderer, SkiaColorFilter* filter) {
499db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    renderer->setupColorFilter(filter);
500db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
501db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
5027d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_setupShadow(JNIEnv* env, jobject clazz,
5031e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        OpenGLRenderer* renderer, jfloat radius, jfloat dx, jfloat dy, jint color) {
5041e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    renderer->setupShadow(radius, dx, dy, color);
5051e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
5061e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
507d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// ----------------------------------------------------------------------------
5085ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy// Draw filters
5095ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy// ----------------------------------------------------------------------------
5105ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy
5115ff9df658230d49e42c43586997a02d8e4dd417eRomain Guystatic void android_view_GLES20Canvas_setupPaintFilter(JNIEnv* env, jobject clazz,
5125ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy        OpenGLRenderer* renderer, jint clearBits, jint setBits) {
5135ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy    renderer->setupPaintFilter(clearBits, setBits);
5145ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy}
5155ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy
5165ff9df658230d49e42c43586997a02d8e4dd417eRomain Guystatic void android_view_GLES20Canvas_resetPaintFilter(JNIEnv* env, jobject clazz,
5175ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy        OpenGLRenderer* renderer) {
5185ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy    renderer->resetPaintFilter();
5195ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy}
5205ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy
5215ff9df658230d49e42c43586997a02d8e4dd417eRomain Guy// ----------------------------------------------------------------------------
522a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy// Text
523a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy// ----------------------------------------------------------------------------
524a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
525e8e62a4a032a80409114a37908b5f18ab0080848Romain Guystatic void renderText(OpenGLRenderer* renderer, const jchar* text, int count,
526e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        jfloat x, jfloat y, int flags, SkPaint* paint) {
527a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio    sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
528a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            text, 0, count, count, flags);
5292ea75880142152b3a9993cb245754d73c05749a7Fabrice Di Meglio    if (value == NULL) {
530b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio        return;
5312ea75880142152b3a9993cb245754d73c05749a7Fabrice Di Meglio    }
5325c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio    const jchar* glyphs = value->getGlyphs();
5335c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio    size_t glyphsCount = value->getGlyphsCount();
534996e57c84368058be793897ebc355b917a59abc2Raph Levien    jfloat totalAdvance = value->getTotalAdvance();
535996e57c84368058be793897ebc355b917a59abc2Raph Levien    const float* positions = value->getPos();
53679df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio    int bytesCount = glyphsCount * sizeof(jchar);
537c25259519f1b74bb62a2b051b74537f073436b5cRomain Guy    renderer->drawText((const char*) glyphs, bytesCount, glyphsCount, x, y,
538996e57c84368058be793897ebc355b917a59abc2Raph Levien            positions, paint, totalAdvance);
539e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy}
540e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
541325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guystatic void renderTextOnPath(OpenGLRenderer* renderer, const jchar* text, int count,
542325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        SkPath* path, jfloat hOffset, jfloat vOffset, int flags, SkPaint* paint) {
543325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
544325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy            text, 0, count, count, flags);
545325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    if (value == NULL) {
546325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        return;
547325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    }
548325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    const jchar* glyphs = value->getGlyphs();
549325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    size_t glyphsCount = value->getGlyphsCount();
550325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    int bytesCount = glyphsCount * sizeof(jchar);
551325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    renderer->drawTextOnPath((const char*) glyphs, bytesCount, glyphsCount, path,
552325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy            hOffset, vOffset, paint);
553325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy}
554325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy
55561c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guystatic void renderTextRun(OpenGLRenderer* renderer, const jchar* text,
55661c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy        jint start, jint count, jint contextCount, jfloat x, jfloat y,
55761c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy        int flags, SkPaint* paint) {
558a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio    sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
559a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            text, start, count, contextCount, flags);
5602ea75880142152b3a9993cb245754d73c05749a7Fabrice Di Meglio    if (value == NULL) {
561b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio        return;
5622ea75880142152b3a9993cb245754d73c05749a7Fabrice Di Meglio    }
5635c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio    const jchar* glyphs = value->getGlyphs();
5645c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio    size_t glyphsCount = value->getGlyphsCount();
565996e57c84368058be793897ebc355b917a59abc2Raph Levien    jfloat totalAdvance = value->getTotalAdvance();
566996e57c84368058be793897ebc355b917a59abc2Raph Levien    const float* positions = value->getPos();
56779df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio    int bytesCount = glyphsCount * sizeof(jchar);
568c25259519f1b74bb62a2b051b74537f073436b5cRomain Guy    renderer->drawText((const char*) glyphs, bytesCount, glyphsCount, x, y,
569996e57c84368058be793897ebc355b917a59abc2Raph Levien            positions, paint, totalAdvance);
57061c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy}
57161c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy
5727d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawTextArray(JNIEnv* env, jobject clazz,
5735a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
5745a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jfloat x, jfloat y, jint flags, SkPaint* paint) {
575a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy    jchar* textArray = env->GetCharArrayElements(text, NULL);
576e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    renderText(renderer, textArray + index, count, x, y, flags, paint);
577a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy    env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
578a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
579a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
5807d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawText(JNIEnv* env, jobject clazz,
5815a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        OpenGLRenderer* renderer, jstring text, jint start, jint end,
5825a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jfloat x, jfloat y, jint flags, SkPaint* paint) {
583a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy    const jchar* textArray = env->GetStringChars(text, NULL);
584e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    renderText(renderer, textArray + start, end - start, x, y, flags, paint);
585a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy    env->ReleaseStringChars(text, textArray);
586a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
587a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
588325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guystatic void android_view_GLES20Canvas_drawTextArrayOnPath(JNIEnv* env, jobject clazz,
589325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
590325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        SkPath* path, jfloat hOffset, jfloat vOffset, jint flags, SkPaint* paint) {
591325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    jchar* textArray = env->GetCharArrayElements(text, NULL);
592325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    renderTextOnPath(renderer, textArray + index, count, path,
593325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy            hOffset, vOffset, flags, paint);
594325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
595325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy}
596325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy
597325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guystatic void android_view_GLES20Canvas_drawTextOnPath(JNIEnv* env, jobject clazz,
598325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        OpenGLRenderer* renderer, jstring text, jint start, jint end,
599325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy        SkPath* path, jfloat hOffset, jfloat vOffset, jint flags, SkPaint* paint) {
600325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    const jchar* textArray = env->GetStringChars(text, NULL);
601325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    renderTextOnPath(renderer, textArray + start, end - start, path,
602325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy            hOffset, vOffset, flags, paint);
603325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy    env->ReleaseStringChars(text, textArray);
604325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy}
605325740fb444af8fc7fb0119b2e30ce322c2ae134Romain Guy
6067d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawTextRunArray(JNIEnv* env, jobject clazz,
6075a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
6085a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jint contextIndex, jint contextCount, jfloat x, jfloat y, jint dirFlags,
60961c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy        SkPaint* paint) {
61061c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    jchar* textArray = env->GetCharArrayElements(text, NULL);
61161c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    renderTextRun(renderer, textArray + contextIndex, index - contextIndex,
61261c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy            count, contextCount, x, y, dirFlags, paint);
61361c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
61461c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy }
61561c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy
6167d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guystatic void android_view_GLES20Canvas_drawTextRun(JNIEnv* env, jobject clazz,
6175a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        OpenGLRenderer* renderer, jstring text, jint start, jint end,
6185a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        jint contextStart, int contextEnd, jfloat x, jfloat y, jint dirFlags,
61961c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy        SkPaint* paint) {
62061c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    const jchar* textArray = env->GetStringChars(text, NULL);
62161c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    jint count = end - start;
62261c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    jint contextCount = contextEnd - contextStart;
62361c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    renderTextRun(renderer, textArray + contextStart, start - contextStart,
62461c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy            count, contextCount, x, y, dirFlags, paint);
62561c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy    env->ReleaseStringChars(text, textArray);
62661c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy}
62761c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy
628eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guystatic void renderPosText(OpenGLRenderer* renderer, const jchar* text, int count,
629eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        const jfloat* positions, jint dirFlags, SkPaint* paint) {
630a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio    sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
631a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            text, 0, count, count, dirFlags);
632eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    if (value == NULL) {
633eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        return;
634eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    }
635eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    const jchar* glyphs = value->getGlyphs();
636eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    size_t glyphsCount = value->getGlyphsCount();
637671d6cf460531825a321edb200523d0faa7792c9Romain Guy    if (count < int(glyphsCount)) glyphsCount = count;
638eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    int bytesCount = glyphsCount * sizeof(jchar);
639eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
640671d6cf460531825a321edb200523d0faa7792c9Romain Guy    renderer->drawPosText((const char*) glyphs, bytesCount, glyphsCount, positions, paint);
641eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy}
642eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
643eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guystatic void android_view_GLES20Canvas_drawPosTextArray(JNIEnv* env, jobject clazz,
644eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
645eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        jfloatArray pos, SkPaint* paint) {
646eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    jchar* textArray = env->GetCharArrayElements(text, NULL);
647eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    jfloat* positions = env->GetFloatArrayElements(pos, NULL);
648eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
649eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    renderPosText(renderer, textArray + index, count, positions, kBidi_LTR, paint);
650eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
651eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    env->ReleaseFloatArrayElements(pos, positions, JNI_ABORT);
652eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
653eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy}
654eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
655eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guystatic void android_view_GLES20Canvas_drawPosText(JNIEnv* env, jobject clazz,
656eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        OpenGLRenderer* renderer, jstring text, jint start, jint end,
657eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy        jfloatArray pos, SkPaint* paint) {
658eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    const jchar* textArray = env->GetStringChars(text, NULL);
659eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    jfloat* positions = env->GetFloatArrayElements(pos, NULL);
660eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
661eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    renderPosText(renderer, textArray + start, end - start, positions, kBidi_LTR, paint);
662eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
663eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    env->ReleaseFloatArrayElements(pos, positions, JNI_ABORT);
664eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy    env->ReleaseStringChars(text, textArray);
665eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy}
666eb9a5367e8f0e970db8509ffb2584f5376bc62edRomain Guy
667b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy// ----------------------------------------------------------------------------
668b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy// Display lists
669b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy// ----------------------------------------------------------------------------
670b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy
6715977baa1fa24125c148a72699b53e62abaf08960Chet Haasestatic DisplayList* android_view_GLES20Canvas_getDisplayList(JNIEnv* env,
672162a0217563f4665da6eb183dfce0fef740f641fJeff Brown        jobject clazz, DisplayListRenderer* renderer, DisplayList* displayList) {
673162a0217563f4665da6eb183dfce0fef740f641fJeff Brown    return renderer->getDisplayList(displayList);
674162a0217563f4665da6eb183dfce0fef740f641fJeff Brown}
675162a0217563f4665da6eb183dfce0fef740f641fJeff Brown
67665b345fa22b878e141b8fd8ece9c208df00fa40fRomain Guystatic jint android_view_GLES20Canvas_getDisplayListSize(JNIEnv* env,
67765b345fa22b878e141b8fd8ece9c208df00fa40fRomain Guy        jobject clazz, DisplayList* displayList) {
67865b345fa22b878e141b8fd8ece9c208df00fa40fRomain Guy    return displayList->getSize();
67965b345fa22b878e141b8fd8ece9c208df00fa40fRomain Guy}
68065b345fa22b878e141b8fd8ece9c208df00fa40fRomain Guy
68113631f3da855f200a151e7837ed9f6b079622b58Romain Guystatic void android_view_GLES20Canvas_setDisplayListName(JNIEnv* env,
68213631f3da855f200a151e7837ed9f6b079622b58Romain Guy        jobject clazz, DisplayList* displayList, jstring name) {
68313631f3da855f200a151e7837ed9f6b079622b58Romain Guy    if (name != NULL) {
68413631f3da855f200a151e7837ed9f6b079622b58Romain Guy        const char* textArray = env->GetStringUTFChars(name, NULL);
68513631f3da855f200a151e7837ed9f6b079622b58Romain Guy        displayList->setName(textArray);
68613631f3da855f200a151e7837ed9f6b079622b58Romain Guy        env->ReleaseStringUTFChars(name, textArray);
68713631f3da855f200a151e7837ed9f6b079622b58Romain Guy    }
68813631f3da855f200a151e7837ed9f6b079622b58Romain Guy}
68913631f3da855f200a151e7837ed9f6b079622b58Romain Guy
690162a0217563f4665da6eb183dfce0fef740f641fJeff Brownstatic OpenGLRenderer* android_view_GLES20Canvas_createDisplayListRenderer(JNIEnv* env,
691162a0217563f4665da6eb183dfce0fef740f641fJeff Brown        jobject clazz) {
692162a0217563f4665da6eb183dfce0fef740f641fJeff Brown    return new DisplayListRenderer;
693b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy}
694b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy
695162a0217563f4665da6eb183dfce0fef740f641fJeff Brownstatic void android_view_GLES20Canvas_resetDisplayListRenderer(JNIEnv* env,
6965977baa1fa24125c148a72699b53e62abaf08960Chet Haase        jobject clazz, DisplayListRenderer* renderer) {
697162a0217563f4665da6eb183dfce0fef740f641fJeff Brown    renderer->reset();
6985977baa1fa24125c148a72699b53e62abaf08960Chet Haase}
6995977baa1fa24125c148a72699b53e62abaf08960Chet Haase
700b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guystatic void android_view_GLES20Canvas_destroyDisplayList(JNIEnv* env,
7015c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        jobject clazz, DisplayList* displayList) {
702bb0acdf9e1d862a7cf0e2533321fc1105c29b5e3Romain Guy    DisplayList::destroyDisplayListDeferred(displayList);
703b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy}
704b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy
7056554943a1dd6854c0f4976900956e556767b49e1Romain Guystatic jint android_view_GLES20Canvas_drawDisplayList(JNIEnv* env,
7067b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy        jobject clazz, OpenGLRenderer* renderer, DisplayList* displayList,
7071271e2cc80b01d577e9db339459ef0222bb9320dChet Haase        jobject dirty, jint flags) {
708cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    android::uirenderer::Rect bounds;
7091271e2cc80b01d577e9db339459ef0222bb9320dChet Haase    status_t status = renderer->drawDisplayList(displayList, bounds, flags);
7106554943a1dd6854c0f4976900956e556767b49e1Romain Guy    if (status != DrawGlInfo::kStatusDone && dirty != NULL) {
711cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy        env->CallVoidMethod(dirty, gRectClassInfo.set,
712cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy                int(bounds.left), int(bounds.top), int(bounds.right), int(bounds.bottom));
713cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    }
7146554943a1dd6854c0f4976900956e556767b49e1Romain Guy    return status;
715b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy}
716b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy
717ed30fd8e9a2d65ee5c8520de55b0089c219f390cChet Haasestatic void android_view_GLES20Canvas_outputDisplayList(JNIEnv* env,
718ed30fd8e9a2d65ee5c8520de55b0089c219f390cChet Haase        jobject clazz, OpenGLRenderer* renderer, DisplayList* displayList) {
719ed30fd8e9a2d65ee5c8520de55b0089c219f390cChet Haase    renderer->outputDisplayList(displayList);
720ed30fd8e9a2d65ee5c8520de55b0089c219f390cChet Haase}
721ed30fd8e9a2d65ee5c8520de55b0089c219f390cChet Haase
7226c319ca1275c8db892c39b48fc54864c949f9171Romain Guy// ----------------------------------------------------------------------------
7236c319ca1275c8db892c39b48fc54864c949f9171Romain Guy// Layers
7246c319ca1275c8db892c39b48fc54864c949f9171Romain Guy// ----------------------------------------------------------------------------
7256c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
726c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guystatic void android_view_GLES20Canvas_interrupt(JNIEnv* env, jobject clazz,
727c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy        OpenGLRenderer* renderer) {
728c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy    renderer->interrupt();
729c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy}
730c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy
731c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guystatic void android_view_GLES20Canvas_resume(JNIEnv* env, jobject clazz,
732c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy        OpenGLRenderer* renderer) {
733c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy    renderer->resume();
734c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy}
735c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy
7366c319ca1275c8db892c39b48fc54864c949f9171Romain Guystatic OpenGLRenderer* android_view_GLES20Canvas_createLayerRenderer(JNIEnv* env,
737ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        jobject clazz, Layer* layer) {
738ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (layer) {
73987e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy        OpenGLRenderer* renderer = new LayerRenderer(layer);
74087e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy        renderer->initProperties();
74187e2f757be9b24d369bab354e37c276e851b1fc7Romain Guy        return renderer;
742ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    }
743ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    return NULL;
7446c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
7456c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
746aa6c24c21c727a196451332448d4e3b11a80be69Romain Guystatic Layer* android_view_GLES20Canvas_createTextureLayer(JNIEnv* env, jobject clazz,
747a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy        jboolean isOpaque, jintArray layerInfo) {
748a9489274d67b540804aafb587a226f7c2ae4464dRomain Guy    Layer* layer = LayerRenderer::createTextureLayer(isOpaque);
749aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
750aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    if (layer) {
751aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        jint* storage = env->GetIntArrayElements(layerInfo, NULL);
7529ace8f5e79e76893fe4ca9e4d10f6c4056330485Romain Guy        storage[0] = layer->getTexture();
753aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        env->ReleaseIntArrayElements(layerInfo, storage, 0);
754aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
755aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
756aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    return layer;
757aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy}
758aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
759ada830f639591b99c3e40de22b07296c7932a33fRomain Guystatic Layer* android_view_GLES20Canvas_createLayer(JNIEnv* env, jobject clazz,
760ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        jint width, jint height, jboolean isOpaque, jintArray layerInfo) {
761ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    Layer* layer = LayerRenderer::createLayer(width, height, isOpaque);
7626c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
763ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (layer) {
7646c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        jint* storage = env->GetIntArrayElements(layerInfo, NULL);
7659ace8f5e79e76893fe4ca9e4d10f6c4056330485Romain Guy        storage[0] = layer->getWidth();
7669ace8f5e79e76893fe4ca9e4d10f6c4056330485Romain Guy        storage[1] = layer->getHeight();
7676c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        env->ReleaseIntArrayElements(layerInfo, storage, 0);
7686c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
7696c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
770ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    return layer;
7716c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
7726c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
773603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haasestatic bool android_view_GLES20Canvas_resizeLayer(JNIEnv* env, jobject clazz,
774ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        Layer* layer, jint width, jint height, jintArray layerInfo) {
775603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase    if (LayerRenderer::resizeLayer(layer, width, height)) {
776603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase        jint* storage = env->GetIntArrayElements(layerInfo, NULL);
777603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase        storage[0] = layer->getWidth();
778603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase        storage[1] = layer->getHeight();
779603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase        env->ReleaseIntArrayElements(layerInfo, storage, 0);
780603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase        return true;
781603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase    }
782603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase    return false;
7836c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
7846c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
785d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haasestatic void android_view_GLES20Canvas_setLayerPaint(JNIEnv* env, jobject clazz,
786d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase        Layer* layer, SkPaint* paint) {
787d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase    if (layer) {
788d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase        layer->setPaint(paint);
789d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase    }
790d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase}
791d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
792d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haasestatic void android_view_GLES20Canvas_setLayerColorFilter(JNIEnv* env, jobject clazz,
793